1
- pub use crypto2:: aeadcipher:: {
2
- Aes128Ccm , Aes128GcmSiv , Aes128OcbTag128 , Aes192OcbTag128 , Aes256Ccm , Aes256GcmSiv ,
3
- Aes256OcbTag128 , AesSivCmac256 , AesSivCmac384 , AesSivCmac512 , Sm4Ccm , Sm4Gcm ,
4
- } ;
1
+ pub use crypto2:: aeadcipher:: { Aes128Ccm , Aes128GcmSiv , Aes256Ccm , Aes256GcmSiv , Sm4Ccm , Sm4Gcm } ;
5
2
#[ cfg( not( all(
6
3
any(
7
4
target_arch = "x86" ,
@@ -37,13 +34,14 @@ trait AeadCipherExt {
37
34
fn ac_n_min ( & self ) -> usize ;
38
35
fn ac_n_max ( & self ) -> usize ;
39
36
fn ac_tag_len ( & self ) -> usize ;
37
+ fn ac_n_len ( & self ) -> usize ;
40
38
41
39
fn ac_encrypt_slice ( & self , nonce : & [ u8 ] , plaintext_in_ciphertext_out : & mut [ u8 ] ) ;
42
40
fn ac_decrypt_slice ( & self , nonce : & [ u8 ] , ciphertext_in_plaintext_out : & mut [ u8 ] ) -> bool ;
43
41
}
44
42
45
43
macro_rules! impl_aead_cipher {
46
- ( $name: tt, $kind: tt) => {
44
+ ( $name: tt, $kind: tt $ ( , $nlen : expr ) ? ) => {
47
45
impl AeadCipherExt for $name {
48
46
fn ac_kind( & self ) -> CipherKind {
49
47
CipherKind :: $kind
@@ -63,6 +61,7 @@ macro_rules! impl_aead_cipher {
63
61
fn ac_tag_len( & self ) -> usize {
64
62
$name:: TAG_LEN
65
63
}
64
+ impl_aead_cipher!( ac_n_len $( , $nlen) ?) ;
66
65
67
66
fn ac_encrypt_slice( & self , nonce: & [ u8 ] , plaintext_in_ciphertext_out: & mut [ u8 ] ) {
68
67
self . encrypt_slice( nonce, & [ ] , plaintext_in_ciphertext_out) ;
@@ -77,50 +76,17 @@ macro_rules! impl_aead_cipher {
77
76
}
78
77
}
79
78
} ;
80
- }
81
-
82
- #[ cfg( feature = "v1-aead-extra" ) ]
83
- macro_rules! impl_siv_cmac_cipher {
84
- ( $name: tt, $kind: tt) => {
85
- impl AeadCipherExt for $name {
86
- fn ac_kind( & self ) -> CipherKind {
87
- CipherKind :: $kind
88
- }
89
- fn ac_key_len( & self ) -> usize {
90
- $name:: KEY_LEN
91
- }
92
- fn ac_block_len( & self ) -> usize {
93
- $name:: BLOCK_LEN
94
- }
95
- fn ac_n_min( & self ) -> usize {
96
- $name:: N_MIN
97
- }
98
- fn ac_n_max( & self ) -> usize {
99
- $name:: N_MAX
100
- }
101
- fn ac_tag_len( & self ) -> usize {
102
- $name:: TAG_LEN
103
- }
104
-
105
- // NOTE: SIV-CMAC 模式,Nonce 在 AAD 数据的最后面。
106
- // TAG 默认也在 PKT 的前面,为此我们这里需要手动把 TAG 数据放置在 密文的后面。
107
- fn ac_encrypt_slice( & self , nonce: & [ u8 ] , plaintext_in_ciphertext_out: & mut [ u8 ] ) {
108
- let len = plaintext_in_ciphertext_out. len( ) ;
109
- let plen = len - Self :: TAG_LEN ;
110
- let ( plaintext, tag_out) = plaintext_in_ciphertext_out. split_at_mut( plen) ;
111
- self . encrypt_slice_detached( & [ nonce] , plaintext, tag_out) ;
112
- }
113
79
114
- fn ac_decrypt_slice (
115
- & self ,
116
- nonce : & [ u8 ] ,
117
- ciphertext_in_plaintext_out : & mut [ u8 ] ,
118
- ) -> bool {
119
- let len = ciphertext_in_plaintext_out . len ( ) ;
120
- let clen = len - Self :: TAG_LEN ;
121
- let ( ciphertext , tag_in ) = ciphertext_in_plaintext_out . split_at_mut ( clen ) ;
122
- self . decrypt_slice_detached ( & [ nonce ] , ciphertext , & tag_in )
123
- }
80
+ ( ac_n_len ) => {
81
+ fn ac_n_len ( & self ) -> usize {
82
+ debug_assert_eq! ( self . ac_n_min ( ) , self . ac_n_max ( ) ) ;
83
+ self . ac_n_max ( )
84
+ }
85
+ } ;
86
+ ( ac_n_len , $nlen : expr ) => {
87
+ fn ac_n_len ( & self ) -> usize {
88
+ debug_assert! ( $nlen >= self . ac_n_min ( ) && $nlen <= self . ac_n_max ( ) ) ;
89
+ $nlen
124
90
}
125
91
} ;
126
92
}
@@ -138,22 +104,8 @@ impl_aead_cipher!(Aes128GcmSiv, AES_128_GCM_SIV);
138
104
#[ cfg( feature = "v1-aead-extra" ) ]
139
105
impl_aead_cipher ! ( Aes256GcmSiv , AES_256_GCM_SIV ) ;
140
106
141
- #[ cfg( feature = "v1-aead-extra" ) ]
142
- impl_aead_cipher ! ( Aes128OcbTag128 , AES_128_OCB_TAGLEN128 ) ;
143
- #[ cfg( feature = "v1-aead-extra" ) ]
144
- impl_aead_cipher ! ( Aes192OcbTag128 , AES_192_OCB_TAGLEN128 ) ;
145
- #[ cfg( feature = "v1-aead-extra" ) ]
146
- impl_aead_cipher ! ( Aes256OcbTag128 , AES_256_OCB_TAGLEN128 ) ;
147
-
148
107
impl_aead_cipher ! ( Chacha20Poly1305 , CHACHA20_POLY1305 ) ;
149
108
150
- #[ cfg( feature = "v1-aead-extra" ) ]
151
- impl_siv_cmac_cipher ! ( AesSivCmac256 , AES_SIV_CMAC_256 ) ;
152
- #[ cfg( feature = "v1-aead-extra" ) ]
153
- impl_siv_cmac_cipher ! ( AesSivCmac384 , AES_SIV_CMAC_384 ) ;
154
- #[ cfg( feature = "v1-aead-extra" ) ]
155
- impl_siv_cmac_cipher ! ( AesSivCmac512 , AES_SIV_CMAC_512 ) ;
156
-
157
109
#[ cfg( feature = "v1-aead-extra" ) ]
158
110
impl_aead_cipher ! ( XChacha20Poly1305 , XCHACHA20_POLY1305 ) ;
159
111
@@ -235,6 +187,14 @@ macro_rules! aead_cipher_variant {
235
187
) +
236
188
}
237
189
}
190
+ fn ac_n_len( & self ) -> usize {
191
+ match * self {
192
+ $(
193
+ $( #[ cfg( $i_meta) ] ) ?
194
+ AeadCipherInner :: $name( ref c) => c. ac_n_len( ) ,
195
+ ) +
196
+ }
197
+ }
238
198
239
199
fn ac_encrypt_slice( & self , nonce: & [ u8 ] , plaintext_in_ciphertext_out: & mut [ u8 ] ) {
240
200
match * self {
@@ -261,17 +221,9 @@ aead_cipher_variant! {
261
221
#[ cfg( feature = "v1-aead-extra" ) ] Aes128Ccm @ AES_128_CCM ,
262
222
#[ cfg( feature = "v1-aead-extra" ) ] Aes256Ccm @ AES_256_CCM ,
263
223
264
- #[ cfg( feature = "v1-aead-extra" ) ] Aes128OcbTag128 @ AES_128_OCB_TAGLEN128 ,
265
- #[ cfg( feature = "v1-aead-extra" ) ] Aes192OcbTag128 @ AES_192_OCB_TAGLEN128 ,
266
- #[ cfg( feature = "v1-aead-extra" ) ] Aes256OcbTag128 @ AES_256_OCB_TAGLEN128 ,
267
-
268
224
Aes128Gcm @ AES_128_GCM ,
269
225
Aes256Gcm @ AES_256_GCM ,
270
226
271
- #[ cfg( feature = "v1-aead-extra" ) ] AesSivCmac256 @ AES_SIV_CMAC_256 ,
272
- #[ cfg( feature = "v1-aead-extra" ) ] AesSivCmac384 @ AES_SIV_CMAC_384 ,
273
- #[ cfg( feature = "v1-aead-extra" ) ] AesSivCmac512 @ AES_SIV_CMAC_512 ,
274
-
275
227
#[ cfg( feature = "v1-aead-extra" ) ] Aes128GcmSiv @ AES_128_GCM_SIV ,
276
228
#[ cfg( feature = "v1-aead-extra" ) ] Aes256GcmSiv @ AES_256_GCM_SIV ,
277
229
@@ -294,7 +246,8 @@ impl AeadCipher {
294
246
295
247
pub fn new ( kind : CipherKind , key : & [ u8 ] ) -> Self {
296
248
let cipher = AeadCipherInner :: new ( kind, key) ;
297
- let nlen = std:: cmp:: min ( cipher. ac_n_max ( ) , Self :: N_MAX ) ;
249
+ let nlen = cipher. ac_n_len ( ) ;
250
+ debug_assert ! ( nlen <= Self :: N_MAX ) ;
298
251
let nonce = [ 0u8 ; Self :: N_MAX ] ;
299
252
300
253
Self {
0 commit comments