1
-
2
-
3
1
//use ring::{aead, error};
4
2
5
- macro_rules! test_aead
6
- { ( $pkg: ident ) =>
7
- {
8
- mod $pkg {
9
-
10
- use $pkg:: { aead, error} ;
11
-
12
- use aead:: { AES_128_GCM , Algorithm , NonceSequence , OpeningKey , UnboundKey , BoundKey , SealingKey , Nonce , Aad } ;
13
- use error:: Unspecified ;
3
+ macro_rules! test_aead {
4
+ ( $pkg: ident ) => {
5
+ mod $pkg {
14
6
7
+ use $pkg:: { aead, error} ;
15
8
16
- const AES_128_TEST_KEY : [ u8 ; 16 ] = [ 12 , 124 , 200 , 31 , 226 , 11 , 135 , 192 , 12 , 124 , 200 , 31 , 226 , 11 , 135 , 192 ] ;
17
- const TEST_NONCE : [ u8 ; aead:: NONCE_LEN ] = [ 12 , 124 , 200 , 31 , 226 , 11 , 135 , 192 , 12 , 124 , 200 , 31 ] ;
18
- const PLAINTEXT : & [ u8 ] = "plaintext to be encrypted" . as_bytes( ) ;
9
+ use aead:: {
10
+ Aad , Algorithm , BoundKey , Nonce , NonceSequence , OpeningKey , SealingKey , UnboundKey ,
11
+ AES_128_GCM ,
12
+ } ;
13
+ use error:: Unspecified ;
19
14
20
- struct NotANonce ( Vec <u8 >) ;
15
+ const AES_128_TEST_KEY : [ u8 ; 16 ] = [
16
+ 12 , 124 , 200 , 31 , 226 , 11 , 135 , 192 , 12 , 124 , 200 , 31 , 226 , 11 , 135 , 192 ,
17
+ ] ;
18
+ const TEST_NONCE : [ u8 ; aead:: NONCE_LEN ] =
19
+ [ 12 , 124 , 200 , 31 , 226 , 11 , 135 , 192 , 12 , 124 , 200 , 31 ] ;
20
+ const PLAINTEXT : & [ u8 ] = "plaintext to be encrypted" . as_bytes( ) ;
21
21
22
- impl NotANonce {
23
- fn from( value: Vec <u8 >) -> Self {
24
- NotANonce ( value)
25
- }
26
- }
22
+ struct NotANonce ( Vec <u8 >) ;
27
23
28
- impl NonceSequence for NotANonce {
29
- fn advance( & mut self ) -> Result <Nonce , Unspecified > {
30
- let mut nonce = [ 0u8 ; aead:: NONCE_LEN ] ;
31
- nonce. copy_from_slice( & self . 0 [ 0 ..aead:: NONCE_LEN ] ) ;
32
- Ok ( Nonce :: assume_unique_for_key( nonce) )
33
- }
34
- }
35
-
36
- struct AeadConfig {
37
- algorithm: & ' static Algorithm ,
38
- key: Vec <u8 >,
39
- nonce: Vec <u8 >,
40
- aad: String
41
- }
42
-
43
- impl AeadConfig {
44
- fn new( algorithm: & ' static Algorithm , key: & [ u8 ] , nonce: & [ u8 ] , aad: & str ) -> AeadConfig {
45
- AeadConfig {
46
- algorithm: algorithm,
47
- key: Vec :: from( key) ,
48
- nonce: Vec :: from( nonce) ,
49
- aad: String :: from( aad)
24
+ impl NotANonce {
25
+ fn from( value: Vec <u8 >) -> Self {
26
+ NotANonce ( value)
27
+ }
50
28
}
51
- }
52
29
53
- fn key( & self ) -> UnboundKey {
54
- UnboundKey :: new( self . algorithm, & self . key) . unwrap( )
55
- }
56
- fn aad( & self ) -> Aad <String > {
57
- Aad :: from( self . aad. clone( ) )
58
- }
59
- fn nonce( & self ) -> impl NonceSequence {
60
- //RngNonce{}
61
- //NotANonce::new()
62
- NotANonce :: from( self . nonce. clone( ) )
63
- }
64
- }
30
+ impl NonceSequence for NotANonce {
31
+ fn advance( & mut self ) -> Result <Nonce , Unspecified > {
32
+ let mut nonce = [ 0u8 ; aead:: NONCE_LEN ] ;
33
+ nonce. copy_from_slice( & self . 0 [ 0 ..aead:: NONCE_LEN ] ) ;
34
+ Ok ( Nonce :: assume_unique_for_key( nonce) )
35
+ }
36
+ }
65
37
66
- #[ test]
67
- fn test_aes_128_gcm( ) -> Result <( ) , String > {
68
- let config = AeadConfig :: new( & AES_128_GCM , & AES_128_TEST_KEY , & TEST_NONCE , "test" ) ;
69
- let mut in_out = Vec :: from( PLAINTEXT ) ;
38
+ struct AeadConfig {
39
+ algorithm: & ' static Algorithm ,
40
+ key: Vec <u8 >,
41
+ nonce: Vec <u8 >,
42
+ aad: String ,
43
+ }
70
44
71
- test_aead( config, & mut in_out) ?;
45
+ impl AeadConfig {
46
+ fn new(
47
+ algorithm: & ' static Algorithm ,
48
+ key: & [ u8 ] ,
49
+ nonce: & [ u8 ] ,
50
+ aad: & str ,
51
+ ) -> AeadConfig {
52
+ AeadConfig {
53
+ algorithm: algorithm,
54
+ key: Vec :: from( key) ,
55
+ nonce: Vec :: from( nonce) ,
56
+ aad: String :: from( aad) ,
57
+ }
58
+ }
59
+
60
+ fn key( & self ) -> UnboundKey {
61
+ UnboundKey :: new( self . algorithm, & self . key) . unwrap( )
62
+ }
63
+ fn aad( & self ) -> Aad <String > {
64
+ Aad :: from( self . aad. clone( ) )
65
+ }
66
+ fn nonce( & self ) -> impl NonceSequence {
67
+ //RngNonce{}
68
+ //NotANonce::new()
69
+ NotANonce :: from( self . nonce. clone( ) )
70
+ }
71
+ }
72
72
73
+ #[ test]
74
+ fn test_aes_128_gcm( ) -> Result <( ) , String > {
75
+ let config = AeadConfig :: new( & AES_128_GCM , & AES_128_TEST_KEY , & TEST_NONCE , "test" ) ;
76
+ let mut in_out = Vec :: from( PLAINTEXT ) ;
73
77
74
- Ok ( ( ) )
75
- }
78
+ test_aead( config, & mut in_out) ?;
76
79
77
- fn test_aead( config: AeadConfig , in_out: & mut Vec <u8 >) -> Result <Vec <u8 >, String > {
78
- let mut sealing_key = SealingKey :: new( config. key( ) , config. nonce( ) ) ;
79
- let mut opening_key = OpeningKey :: new( config. key( ) , config. nonce( ) ) ;
80
+ Ok ( ( ) )
81
+ }
80
82
81
- let plaintext = in_out. clone( ) ;
82
- println!( "Plaintext: {:?}" , plaintext) ;
83
+ fn test_aead( config: AeadConfig , in_out: & mut Vec <u8 >) -> Result <Vec <u8 >, String > {
84
+ let mut sealing_key = SealingKey :: new( config. key( ) , config. nonce( ) ) ;
85
+ let mut opening_key = OpeningKey :: new( config. key( ) , config. nonce( ) ) ;
83
86
84
- let tag = sealing_key. seal_in_place_separate_tag( config. aad( ) , in_out. as_mut_slice( ) ) . map_err( |x| x. to_string( ) ) ?;
85
- let cipher_text = in_out. clone( ) ;
86
- println!( "Ciphertext: {:?}" , cipher_text) ;
87
- assert_ne!( plaintext, cipher_text) ;
87
+ let plaintext = in_out. clone( ) ;
88
+ println!( "Plaintext: {:?}" , plaintext) ;
88
89
89
- in_out. extend( tag. as_ref( ) ) ;
90
+ let tag = sealing_key
91
+ . seal_in_place_separate_tag( config. aad( ) , in_out. as_mut_slice( ) )
92
+ . map_err( |x| x. to_string( ) ) ?;
93
+ let cipher_text = in_out. clone( ) ;
94
+ println!( "Ciphertext: {:?}" , cipher_text) ;
95
+ assert_ne!( plaintext, cipher_text) ;
90
96
91
- let result_plaintext = opening_key. open_in_place( config. aad( ) , in_out) . map_err( |x| x. to_string( ) ) ?;
92
- assert_eq!( plaintext, result_plaintext) ;
97
+ in_out. extend( tag. as_ref( ) ) ;
93
98
94
- println!( "Roundtrip: {:?}" , result_plaintext) ;
99
+ let result_plaintext = opening_key
100
+ . open_in_place( config. aad( ) , in_out)
101
+ . map_err( |x| x. to_string( ) ) ?;
102
+ assert_eq!( plaintext, result_plaintext) ;
95
103
104
+ println!( "Roundtrip: {:?}" , result_plaintext) ;
96
105
97
- Ok ( Vec :: from( result_plaintext) )
98
- }
99
- } } }
106
+ Ok ( Vec :: from( result_plaintext) )
107
+ }
108
+ }
109
+ } ;
110
+ }
100
111
101
112
mod test_aead {
102
113
test_aead ! ( ring) ;
103
- }
114
+ }
0 commit comments