1
1
use aes_gcm:: Key ;
2
+ use cas_lib:: symmetric:: { aes:: { CASAES128 , CASAES256 } , cas_symmetric_encryption:: CASAESEncryption } ;
2
3
use napi_derive:: napi;
3
- use rand:: rngs:: OsRng ;
4
4
use rand:: { RngCore , SeedableRng } ;
5
5
use rand_chacha:: ChaCha20Rng ;
6
6
7
- use aes_gcm:: {
8
- aead:: { generic_array:: GenericArray , Aead } ,
9
- Aes128Gcm , Aes256Gcm , KeyInit , Nonce ,
10
- } ;
11
-
12
- use super :: cas_symmetric_encryption:: { AesKeyFromX25519SharedSecret , CASAESEncryption } ;
13
- pub struct CASAES128 ;
14
- pub struct CASAES256 ;
15
-
16
- impl CASAESEncryption for CASAES256 {
17
- fn generate_key ( ) -> Vec < u8 > {
18
- return Aes256Gcm :: generate_key ( & mut OsRng ) . to_vec ( ) ;
19
- }
20
-
21
- fn encrypt_plaintext ( aes_key : Vec < u8 > , nonce : Vec < u8 > , plaintext : Vec < u8 > ) -> Vec < u8 > {
22
- let key = GenericArray :: from_slice ( & aes_key) ;
23
- let mut cipher = Aes256Gcm :: new ( & key) ;
24
- let nonce = Nonce :: from_slice ( & nonce) ;
25
- let ciphertext = cipher. encrypt ( nonce, plaintext. as_ref ( ) ) . unwrap ( ) ;
26
- ciphertext
27
- }
28
-
29
- fn decrypt_ciphertext ( aes_key : Vec < u8 > , nonce : Vec < u8 > , ciphertext : Vec < u8 > ) -> Vec < u8 > {
30
- let key = GenericArray :: from_slice ( & aes_key) ;
31
- let mut cipher = Aes256Gcm :: new ( & key) ;
32
- let nonce = Nonce :: from_slice ( & nonce) ;
33
- let plaintext = cipher. decrypt ( nonce, ciphertext. as_ref ( ) ) . unwrap ( ) ;
34
- plaintext
35
- }
36
-
37
- fn key_from_x25519_shared_secret ( shared_secret : Vec < u8 > ) -> AesKeyFromX25519SharedSecret {
38
- let aes_key = Key :: < Aes256Gcm > :: from_slice ( & shared_secret) ;
39
- let mut aes_nonce: [ u8 ; 12 ] = Default :: default ( ) ;
40
- aes_nonce. copy_from_slice ( & shared_secret[ ..12 ] ) ;
41
- let result = AesKeyFromX25519SharedSecret {
42
- aes_key : aes_key. to_vec ( ) ,
43
- aes_nonce : aes_nonce. to_vec ( ) ,
44
- } ;
45
- result
46
- }
47
- }
48
-
49
- impl CASAESEncryption for CASAES128 {
50
- fn generate_key ( ) -> Vec < u8 > {
51
- return Aes128Gcm :: generate_key ( & mut OsRng ) . to_vec ( ) ;
52
- }
53
-
54
- fn encrypt_plaintext ( aes_key : Vec < u8 > , nonce : Vec < u8 > , plaintext : Vec < u8 > ) -> Vec < u8 > {
55
- let key = GenericArray :: from_slice ( & aes_key) ;
56
- let mut cipher = Aes128Gcm :: new ( & key) ;
57
- let nonce = Nonce :: from_slice ( & nonce) ;
58
- let ciphertext = cipher. encrypt ( nonce, plaintext. as_ref ( ) ) . unwrap ( ) ;
59
- ciphertext
60
- }
61
-
62
- fn decrypt_ciphertext ( aes_key : Vec < u8 > , nonce : Vec < u8 > , ciphertext : Vec < u8 > ) -> Vec < u8 > {
63
- let key = GenericArray :: from_slice ( & aes_key) ;
64
- let cipher = Aes128Gcm :: new ( & key) ;
65
- let nonce = Nonce :: from_slice ( & nonce) ;
66
- let plaintext = cipher. decrypt ( nonce, ciphertext. as_ref ( ) ) . unwrap ( ) ;
67
- plaintext
68
- }
69
-
70
- fn key_from_x25519_shared_secret ( shared_secret : Vec < u8 > ) -> AesKeyFromX25519SharedSecret {
71
- let mut aes_key: [ u8 ; 16 ] = Default :: default ( ) ;
72
- aes_key. copy_from_slice ( & shared_secret[ ..16 ] ) ;
73
- let aes_key_slice = Key :: < Aes128Gcm > :: from_slice ( & aes_key) ;
74
- let mut aes_nonce: [ u8 ; 12 ] = Default :: default ( ) ;
75
- aes_nonce. copy_from_slice ( & shared_secret[ ..12 ] ) ;
76
- let result = AesKeyFromX25519SharedSecret {
77
- aes_key : aes_key_slice. to_vec ( ) ,
78
- aes_nonce : aes_nonce. to_vec ( ) ,
79
- } ;
80
- result
81
- }
82
- }
7
+ use super :: types:: CASAesKeyFromX25519SharedSecret ;
83
8
84
9
#[ napi]
85
10
pub fn aes_nonce ( ) -> Vec < u8 > {
@@ -92,46 +17,46 @@ pub fn aes_nonce() -> Vec<u8> {
92
17
93
18
#[ napi]
94
19
pub fn aes128_key ( ) -> Vec < u8 > {
95
- return CASAES128 :: generate_key ( ) ;
20
+ return < CASAES128 as CASAESEncryption > :: generate_key ( ) ;
96
21
}
97
22
98
23
#[ napi]
99
24
pub fn aes256_key ( ) -> Vec < u8 > {
100
- return CASAES256 :: generate_key ( ) ;
25
+ return < CASAES256 as CASAESEncryption > :: generate_key ( ) ;
101
26
}
102
27
103
28
#[ napi]
104
29
pub fn aes128_encrypt ( aes_key : Vec < u8 > , nonce : Vec < u8 > , plaintext : Vec < u8 > ) -> Vec < u8 > {
105
- return CASAES128 :: encrypt_plaintext ( aes_key, nonce, plaintext) ;
30
+ return < CASAES128 as CASAESEncryption > :: encrypt_plaintext ( aes_key, nonce, plaintext) ;
106
31
}
107
32
108
33
#[ napi]
109
34
pub fn aes128_decrypt ( aes_key : Vec < u8 > , nonce : Vec < u8 > , ciphertext : Vec < u8 > ) -> Vec < u8 > {
110
- return CASAES128 :: decrypt_ciphertext ( aes_key, nonce, ciphertext) ;
35
+ return < CASAES128 as CASAESEncryption > :: decrypt_ciphertext ( aes_key, nonce, ciphertext) ;
111
36
}
112
37
113
38
#[ napi]
114
39
pub fn aes256_encrypt ( aes_key : Vec < u8 > , nonce : Vec < u8 > , plaintext : Vec < u8 > ) -> Vec < u8 > {
115
- return CASAES256 :: encrypt_plaintext ( aes_key, nonce, plaintext) ;
40
+ return < CASAES256 as CASAESEncryption > :: encrypt_plaintext ( aes_key, nonce, plaintext) ;
116
41
}
117
42
118
43
#[ napi]
119
44
pub fn aes256_decrypt ( aes_key : Vec < u8 > , nonce : Vec < u8 > , ciphertext : Vec < u8 > ) -> Vec < u8 > {
120
- return CASAES256 :: decrypt_ciphertext ( aes_key, nonce, ciphertext) ;
45
+ return < CASAES256 as CASAESEncryption > :: decrypt_ciphertext ( aes_key, nonce, ciphertext) ;
121
46
}
122
47
123
48
#[ napi]
124
49
pub fn aes_256_key_from_x25519_shared_secret (
125
50
shared_secret : Vec < u8 > ,
126
- ) -> AesKeyFromX25519SharedSecret {
127
- return CASAES256 :: key_from_x25519_shared_secret ( shared_secret) ;
51
+ ) -> CASAesKeyFromX25519SharedSecret {
52
+ return < CASAES256 as CASAESEncryption > :: key_from_x25519_shared_secret ( shared_secret) . into ( ) ;
128
53
}
129
54
130
55
#[ napi]
131
56
pub fn aes_128_key_from_x25519_shared_secret (
132
57
shared_secret : Vec < u8 > ,
133
- ) -> AesKeyFromX25519SharedSecret {
134
- return CASAES128 :: key_from_x25519_shared_secret ( shared_secret) ;
58
+ ) -> CASAesKeyFromX25519SharedSecret {
59
+ return < CASAES128 as CASAESEncryption > :: key_from_x25519_shared_secret ( shared_secret) . into ( ) ;
135
60
}
136
61
137
62
#[ test]
0 commit comments