1
1
use std:: ffi:: { c_char, c_uchar, CStr , CString } ;
2
-
3
- use ascon_aead:: aead:: { generic_array:: GenericArray , Aead , AeadCore , KeyInit , OsRng } ;
4
- use ascon_aead:: Ascon128 ;
2
+ use cas_lib:: sponges:: ascon_aead:: AsconAead ;
3
+ use cas_lib:: sponges:: cas_ascon_aead:: { CASAsconAead } ;
5
4
6
5
#[ repr( C ) ]
7
6
pub struct Ascon128EncryptResult {
@@ -15,39 +14,85 @@ pub struct Ascon128DecryptResult {
15
14
length : usize ,
16
15
}
17
16
17
+ #[ repr( C ) ]
18
+ pub struct Ascon128Key {
19
+ key : * mut c_uchar ,
20
+ length : usize
21
+ }
22
+
23
+ #[ repr( C ) ]
24
+ pub struct Ascon128Nonce {
25
+ nonce : * mut c_uchar ,
26
+ length : usize
27
+ }
28
+
18
29
#[ no_mangle]
19
- pub extern "C" fn ascon_128_key ( ) -> * mut c_char {
20
- return CString :: new ( base64:: encode ( Ascon128 :: generate_key ( & mut OsRng ) ) )
21
- . unwrap ( )
22
- . into_raw ( ) ;
30
+ pub extern "C" fn ascon_128_key ( ) -> Ascon128Key {
31
+ let mut key = <AsconAead as CASAsconAead >:: generate_key ( ) ;
32
+ let capacity = key. capacity ( ) ;
33
+ key. reserve_exact ( capacity) ;
34
+ let result = Ascon128Key {
35
+ key : key. as_mut_ptr ( ) ,
36
+ length : key. len ( )
37
+ } ;
38
+ std:: mem:: forget ( key) ;
39
+ result
23
40
}
24
41
25
42
#[ no_mangle]
26
- pub extern "C" fn ascon_128_nonce ( ) -> * mut c_char {
27
- return CString :: new ( base64:: encode ( Ascon128 :: generate_nonce ( & mut OsRng ) ) )
28
- . unwrap ( )
29
- . into_raw ( ) ;
43
+ pub extern "C" fn ascon_128_key_threadpool ( ) -> Ascon128Key {
44
+ let mut key = <AsconAead as CASAsconAead >:: generate_key_threadpool ( ) ;
45
+ let capacity = key. capacity ( ) ;
46
+ key. reserve_exact ( capacity) ;
47
+ let result = Ascon128Key {
48
+ key : key. as_mut_ptr ( ) ,
49
+ length : key. len ( )
50
+ } ;
51
+ std:: mem:: forget ( key) ;
52
+ result
53
+ }
54
+
55
+
56
+
57
+ #[ no_mangle]
58
+ pub extern "C" fn ascon_128_nonce ( ) -> Ascon128Nonce {
59
+ let mut nonce = <AsconAead as CASAsconAead >:: generate_nonce ( ) ;
60
+ let capacity = nonce. capacity ( ) ;
61
+ nonce. reserve_exact ( capacity) ;
62
+ let result = Ascon128Nonce {
63
+ nonce : nonce. as_mut_ptr ( ) ,
64
+ length : nonce. len ( )
65
+ } ;
66
+ std:: mem:: forget ( nonce) ;
67
+ result
68
+ }
69
+
70
+ #[ no_mangle]
71
+ pub extern "C" fn ascon_128_nonce_threadpool ( ) -> Ascon128Nonce {
72
+ let mut nonce = <AsconAead as CASAsconAead >:: generate_nonce_threadpool ( ) ;
73
+ let capacity = nonce. capacity ( ) ;
74
+ nonce. reserve_exact ( capacity) ;
75
+ let result = Ascon128Nonce {
76
+ nonce : nonce. as_mut_ptr ( ) ,
77
+ length : nonce. len ( )
78
+ } ;
79
+ std:: mem:: forget ( nonce) ;
80
+ result
30
81
}
31
82
32
83
#[ no_mangle]
33
84
pub extern "C" fn ascon_128_encrypt (
34
- nonce_key : * const c_char ,
35
- key : * const c_char ,
85
+ nonce_key : * const c_uchar ,
86
+ nonce_key_length : usize ,
87
+ key : * const c_uchar ,
88
+ key_length : usize ,
36
89
to_encrypt : * const c_uchar ,
37
90
to_encrypt_length : usize ,
38
91
) -> Ascon128EncryptResult {
39
- let nonce_key = unsafe { CStr :: from_ptr ( nonce_key) } . to_str ( ) . unwrap ( ) ;
40
- let key = unsafe { CStr :: from_ptr ( key) } . to_str ( ) . unwrap ( ) ;
41
- let to_encrypt = unsafe { std:: slice:: from_raw_parts ( to_encrypt, to_encrypt_length) } ;
42
-
43
- let decoded_nonce_key = base64:: decode ( nonce_key) . unwrap ( ) ;
44
- let decoded_key = base64:: decode ( key) . unwrap ( ) ;
45
-
46
- let key = GenericArray :: from_slice ( & decoded_nonce_key) ;
47
- let nonce_key = GenericArray :: from_slice ( & decoded_key) ;
48
-
49
- let cipher = Ascon128 :: new ( key) ;
50
- let mut ciphertext = cipher. encrypt ( & nonce_key, to_encrypt. as_ref ( ) ) . unwrap ( ) ;
92
+ let nonce_key = unsafe { std:: slice:: from_raw_parts ( nonce_key, nonce_key_length) } . to_vec ( ) ;
93
+ let key = unsafe { std:: slice:: from_raw_parts ( key, key_length) } . to_vec ( ) ;
94
+ let to_encrypt = unsafe { std:: slice:: from_raw_parts ( to_encrypt, to_encrypt_length) } . to_vec ( ) ;
95
+ let mut ciphertext = <AsconAead as CASAsconAead >:: encrypt ( key, nonce_key, to_encrypt) ;
51
96
let capacity = ciphertext. capacity ( ) ;
52
97
ciphertext. reserve_exact ( capacity) ;
53
98
let result = Ascon128EncryptResult {
@@ -58,26 +103,66 @@ pub extern "C" fn ascon_128_encrypt(
58
103
result
59
104
}
60
105
61
-
62
106
#[ no_mangle]
63
- pub extern "C" fn ascon_128_decrypt (
64
- nonce_key : * const c_char ,
65
- key : * const c_char ,
107
+ pub extern "C" fn ascon_128_encrypt_threadpool (
108
+ nonce_key : * const c_uchar ,
109
+ nonce_key_length : usize ,
110
+ key : * const c_uchar ,
111
+ key_length : usize ,
66
112
to_encrypt : * const c_uchar ,
67
113
to_encrypt_length : usize ,
68
- ) -> Ascon128DecryptResult {
69
- let nonce_key = unsafe { CStr :: from_ptr ( nonce_key) } . to_str ( ) . unwrap ( ) ;
70
- let key = unsafe { CStr :: from_ptr ( key) } . to_str ( ) . unwrap ( ) ;
71
- let to_encrypt = unsafe { std:: slice:: from_raw_parts ( to_encrypt, to_encrypt_length) } ;
114
+ ) -> Ascon128EncryptResult {
115
+ let nonce_key = unsafe { std:: slice:: from_raw_parts ( nonce_key, nonce_key_length) } . to_vec ( ) ;
116
+ let key = unsafe { std:: slice:: from_raw_parts ( key, key_length) } . to_vec ( ) ;
117
+ let to_encrypt = unsafe { std:: slice:: from_raw_parts ( to_encrypt, to_encrypt_length) } . to_vec ( ) ;
118
+ let mut ciphertext = <AsconAead as CASAsconAead >:: encrypt_threadpool ( key, nonce_key, to_encrypt) ;
119
+ let capacity = ciphertext. capacity ( ) ;
120
+ ciphertext. reserve_exact ( capacity) ;
121
+ let result = Ascon128EncryptResult {
122
+ ciphertext : ciphertext. as_mut_ptr ( ) ,
123
+ length : ciphertext. len ( ) ,
124
+ } ;
125
+ std:: mem:: forget ( ciphertext) ;
126
+ result
127
+ }
72
128
73
- let decoded_nonce_key = base64:: decode ( nonce_key) . unwrap ( ) ;
74
- let decoded_key = base64:: decode ( key) . unwrap ( ) ;
75
129
76
- let key = GenericArray :: from_slice ( & decoded_nonce_key) ;
77
- let nonce_key = GenericArray :: from_slice ( & decoded_key) ;
130
+ #[ no_mangle]
131
+ pub extern "C" fn ascon_128_decrypt (
132
+ nonce_key : * const c_uchar ,
133
+ nonce_key_length : usize ,
134
+ key : * const c_uchar ,
135
+ key_length : usize ,
136
+ to_decrypt : * const c_uchar ,
137
+ to_decrypt_length : usize ,
138
+ ) -> Ascon128DecryptResult {
139
+ let nonce_key = unsafe { std:: slice:: from_raw_parts ( nonce_key, nonce_key_length) } . to_vec ( ) ;
140
+ let key = unsafe { std:: slice:: from_raw_parts ( key, key_length) } . to_vec ( ) ;
141
+ let to_decrypt = unsafe { std:: slice:: from_raw_parts ( to_decrypt, to_decrypt_length) } . to_vec ( ) ;
142
+ let mut plaintext = <AsconAead as CASAsconAead >:: decrypt ( key, nonce_key, to_decrypt) ;
143
+ let capacity = plaintext. capacity ( ) ;
144
+ plaintext. reserve_exact ( capacity) ;
145
+ let result = Ascon128DecryptResult {
146
+ plaintext : plaintext. as_mut_ptr ( ) ,
147
+ length : plaintext. len ( ) ,
148
+ } ;
149
+ std:: mem:: forget ( plaintext) ;
150
+ result
151
+ }
78
152
79
- let cipher = Ascon128 :: new ( key) ;
80
- let mut plaintext = cipher. decrypt ( & nonce_key, to_encrypt. as_ref ( ) ) . unwrap ( ) ;
153
+ #[ no_mangle]
154
+ pub extern "C" fn ascon_128_decrypt_threadpool (
155
+ nonce_key : * const c_uchar ,
156
+ nonce_key_length : usize ,
157
+ key : * const c_uchar ,
158
+ key_length : usize ,
159
+ to_decrypt : * const c_uchar ,
160
+ to_decrypt_length : usize ,
161
+ ) -> Ascon128DecryptResult {
162
+ let nonce_key = unsafe { std:: slice:: from_raw_parts ( nonce_key, nonce_key_length) } . to_vec ( ) ;
163
+ let key = unsafe { std:: slice:: from_raw_parts ( key, key_length) } . to_vec ( ) ;
164
+ let to_decrypt = unsafe { std:: slice:: from_raw_parts ( to_decrypt, to_decrypt_length) } . to_vec ( ) ;
165
+ let mut plaintext = <AsconAead as CASAsconAead >:: decrypt_threadpool ( key, nonce_key, to_decrypt) ;
81
166
let capacity = plaintext. capacity ( ) ;
82
167
plaintext. reserve_exact ( capacity) ;
83
168
let result = Ascon128DecryptResult {
0 commit comments