Debian Codegraph @

src/crypto/psk.c

raw symbols SCIP
1 /* librist. Copyright © 2020 SipRadius LLC. All right reserved.
2 * Author: Gijs Peskens <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "config.h"
9 #include "psk.h"
10 #include "log-private.h"
11 #include "crypto-private.h"
12 #include <string.h>
13
14 #if HAVE_MBEDTLS
15 #include "mbedtls/aes.h"
16 #include "mbedtls/md.h"
17 #include "mbedtls/pkcs5.h"
18 #elif HAVE_NETTLE
19 #include <nettle/pbkdf2.h>
20 #include <nettle/aes.h>
21 #include <nettle/ctr.h>
22 #elif defined(LINUX_CRYPTO)
23 #include "linux-crypto.h"
24 #endif
25 #if !HAVE_MBEDTLS
26 #include "fastpbkdf2.h"
27 #endif
28
29 #ifndef AES_BLOCK_SIZE
30 #define AES_BLOCK_SIZE 16
31 #endif
32
33 #include <stdint.h>
34
35 //TODO: handle failures?
36 int _librist_crypto_psk_rist_key_init(struct rist_key *key, uint32_t key_size, uint32_t rotation, const char *password, bool odd)
37 {
38 key->password_len = strnlen(password, sizeof(key->password) - 1);
39 memcpy(key->password, password, key->password_len);
40 key->password[key->password_len] = '\0';
41 key->key_size = key_size;
42 key->key_rotation = rotation;
43 #if HAVE_MBEDTLS
44 mbedtls_aes_init(&key->mbedtls_aes_ctx);
45 #elif HAVE_NETTLE
46 memset(&key->nettle_ctx, 0, sizeof(key->nettle_ctx));
47 #elif defined(LINUX_CRYPTO)
48 linux_crypto_init(&key->linux_crypto_ctx);
49 #endif
50 key->odd = odd;
51 return 0;
52 }
53
54 int _librist_crypto_psk_rist_key_destroy(struct rist_key *key)
55 {
56 if (key->key_size) {
57 #if HAVE_MBEDTLS
58 mbedtls_aes_free(&key->mbedtls_aes_ctx);
59 #elif HAVE_NETTLE
60 //nothing to do here
61 #elif defined(LINUX_CRYPTO)
62 linux_crypto_free(&key->linux_crypto_ctx);
63 #endif
64 }
65 return 0;
66 }
67
68 int _librist_crypto_psk_rist_key_clone(struct rist_key *key_in, struct rist_key *key_out)
69 {
70 key_out->password_len = key_in->password_len;
71 memcpy(key_out->password, key_in->password, key_in->password_len);
72 key_out->password[key_out->password_len] = '\0';
73 key_out->key_size = key_in->key_size;
74 key_out->key_rotation = key_in->key_rotation;
75 #if HAVE_MBEDTLS
76 mbedtls_aes_init(&key_out->mbedtls_aes_ctx);
77 #elif HAVE_NETTLE
78 memset(&key_out->nettle_ctx, 0, sizeof(key_out->nettle_ctx));
79 #elif defined(LINUX_CRYPTO)
80 linux_crypto_init(&key_out->linux_crypto_ctx);
81 #endif
82 key_out->odd = key_in->odd;
83 return 0;
84 }
85
86 static void _librist_crypto_aes_key(struct rist_key *key)
87 {
88 uint8_t aes_key[256 / 8];
89 #if HAVE_MBEDTLS
90 mbedtls_md_context_t sha_ctx;
91 const mbedtls_md_info_t *info_sha;
92 int ret = -1;
93 mbedtls_md_init(&sha_ctx);
94 info_sha = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
95 if (info_sha == NULL)
96 goto fail;
97
98 ret = mbedtls_md_setup(&sha_ctx, info_sha, 1);
99 if (ret != 0)
100 goto fail;
101
102 ret = mbedtls_pkcs5_pbkdf2_hmac(
103 &sha_ctx, (const unsigned char *)key->password, key->password_len,
104 key->gre_nonce, sizeof(key->gre_nonce),
105 RIST_PBKDF2_HMAC_SHA256_ITERATIONS, key->key_size / 8, aes_key);
106 if (ret != 0)
107 goto fail;
108 mbedtls_md_free(&sha_ctx);
109 #elif HAVE_NETTLE
110 nettle_pbkdf2_hmac_sha256(key->password_len,(const uint8_t*)key->password,
111 RIST_PBKDF2_HMAC_SHA256_ITERATIONS,
112 sizeof(key->gre_nonce), key->gre_nonce,
113 key->key_size/8, aes_key);
114 #else
115 fastpbkdf2_hmac_sha256(
116 (const void *) key->password, key->password_len,
117 (const void *) key->gre_nonce, sizeof(key->gre_nonce),
118 RIST_PBKDF2_HMAC_SHA256_ITERATIONS,
119 aes_key, key->key_size / 8);
120 #endif
121
122
123 #if HAVE_MBEDTLS
124 mbedtls_aes_setkey_enc(&key->mbedtls_aes_ctx, aes_key, key->key_size);
125 #elif HAVE_NETTLE
126 switch(key->key_size) {
127 case 256:
128 nettle_aes256_set_encrypt_key(&key->nettle_ctx.u.ctx256, aes_key);
129 break;
130 case 192:
131 nettle_aes192_set_encrypt_key(&key->nettle_ctx.u.ctx192, aes_key);
132 break;
133 case 128:
134 RIST_FALLTHROUGH;
135 default:
136 nettle_aes128_set_encrypt_key(&key->nettle_ctx.u.ctx128, aes_key);
137 }
138 #elif defined(LINUX_CRYPTO)
139 if (key->linux_crypto_ctx)
140 linux_crypto_set_key(aes_key, key->key_size / 8, key->linux_crypto_ctx);
141 else
142 aes_key_setup(aes_key, key->aes_key_sched, key->key_size);
143 #else
144 aes_key_setup(aes_key, key->aes_key_sched, key->key_size);
145 #endif
146 key->used_times = 0;
147 return;
148 #if HAVE_MBEDTLS
149 fail:
150 mbedtls_md_free(&sha_ctx);
151 /* Leave any prior key install in place but force the lockout flag so we
152 * don't run AES-CTR with whatever happened to be on the stack. */
153 key->bad_decryption = true;
154 return;
155 #endif
156 }
157
158 //This doesn't really belong here (not PSK related), but since all other crypto interop stuff is here it goes in here..
159 void _librist_crypto_aes_ctr(const uint8_t key[], int key_size, uint8_t iv[], const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len) {
160 #if HAVE_MBEDTLS
161 mbedtls_aes_context ctx;
162 mbedtls_aes_init(&ctx);
163 mbedtls_aes_setkey_enc(&ctx, key, key_size);
164 uint8_t stream_block[AES_BLOCK_SIZE] = {0};
165 size_t nc_off = 0;
166 mbedtls_aes_crypt_ctr(&ctx, payload_len, &nc_off, iv, stream_block, inbuf, outbuf);
167 mbedtls_aes_free(&ctx);
168 #elif HAVE_NETTLE
169 struct aes_ctx aes_ctx;
170 memset(&aes_ctx, 0, sizeof(aes_ctx));
171 nettle_cipher_func *f;
172 switch (key_size) {
173 case 256:
174 nettle_aes256_set_encrypt_key(&aes_ctx.u.ctx256, key);
175 f = (nettle_cipher_func *)nettle_aes256_encrypt;
176 break;
177 case 192:
178 nettle_aes192_set_encrypt_key(&aes_ctx.u.ctx192, key);
179 f = (nettle_cipher_func *)nettle_aes192_encrypt;
180 break;
181 case 128:
182 nettle_aes128_set_encrypt_key(&aes_ctx.u.ctx128, key);
183 f = (nettle_cipher_func *)nettle_aes128_encrypt;
184 break;
185 default:
186 return;
187 }
188 nettle_ctr_crypt(&aes_ctx.u, f, AES_BLOCK_SIZE, iv, payload_len, outbuf, inbuf);
189 #else
190 uint32_t aes_key_sched[60];
191 aes_key_setup(key, aes_key_sched, key_size);
192 aes_decrypt_ctr(inbuf, payload_len, outbuf, aes_key_sched, key_size, iv);
193 #endif
194 }
195
196 static void _librist_crypto_psk_aes_ctr(struct rist_key *key, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len)
197 {
198 #if HAVE_MBEDTLS
199 mbedtls_aes_crypt_ctr(&key->mbedtls_aes_ctx, payload_len, &key->aes_offset, key->iv, key->strean_block, inbuf, outbuf);
200 #elif HAVE_NETTLE
201 nettle_cipher_func *f;
202 switch(key->key_size) {
203 case 128:
204 f = (nettle_cipher_func *)nettle_aes128_encrypt;
205 break;
206 case 192:
207 f = (nettle_cipher_func *)nettle_aes192_encrypt;
208 break;
209 case 256:
210 f = (nettle_cipher_func *)nettle_aes256_encrypt;
211 break;
212 default:
213 return;
214 }
215 nettle_ctr_crypt(&key->nettle_ctx.u, f, AES_BLOCK_SIZE, key->iv,payload_len, outbuf, inbuf);
216 #elif defined(LINUX_CRYPTO)
217 if (key->linux_crypto_ctx)
218 linux_crypto_decrypt(inbuf, outbuf, payload_len, key->iv, key->linux_crypto_ctx);
219 else
220 aes_decrypt_ctr(inbuf, payload_len, outbuf, key->aes_key_sched, key->key_size, key->iv);
221 #else
222 aes_decrypt_ctr(inbuf, payload_len, outbuf, key->aes_key_sched, key->key_size, key->iv);
223 #endif
224 key->used_times++;
225 }
226
227 static void _librist_crypto_psk_prepare_iv(struct rist_key *key, uint8_t gre_version, uint32_t seq_nbe) {
228 /* Prepare AES iv */
229 // The byte array needs to be zeroes and then the seq in network byte order
230 uint8_t copy_offset = gre_version >= 1 ? 0 : 12;
231 memset(key->iv, 0, 16);
232 memcpy(key->iv + copy_offset, &seq_nbe, sizeof(seq_nbe));
233 }
234
235 static void _librist_crypto_psk_generate_nonce(struct rist_key *key) {
236 /* Fail-closed CSPRNG: on failure, mark the key locked so encrypt/decrypt
237 * short-circuit instead of running AES under a predictable nonce. */
238 uint32_t nonce_val = 0;
239 for (int attempts = 0; attempts < 8; attempts++) {
240 if (_librist_crypto_random_u32(&nonce_val) != 0) {
241 rist_log_priv3(RIST_LOG_ERROR,
242 "PSK nonce generation: CSPRNG unavailable, "
243 "PSK encrypt/decrypt locked out until passphrase rotation\n");
244 key->csprng_failed = true;
245 key->bad_decryption = true;
246 return;
247 }
248 if (nonce_val != 0)
249 break;
250 }
251 if (nonce_val == 0) {
252 /* 8 zeros in a row from a working CSPRNG is 2^-256; treat as malfunction. */
253 rist_log_priv3(RIST_LOG_ERROR,
254 "PSK nonce generation: CSPRNG returned only zeros, locking out\n");
255 key->csprng_failed = true;
256 key->bad_decryption = true;
257 return;
258 }
259
260 memcpy(key->gre_nonce, &nonce_val, sizeof(key->gre_nonce));
261
262 UNSET_BIT(key->gre_nonce[0], 7);
263 if (key->odd)
264 SET_BIT(key->gre_nonce[0], 7);
265 }
266
267 void _librist_crypto_psk_decrypt(struct rist_key *key, uint8_t nonce[4], uint32_t seq_nbe, uint8_t gre_version, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len)
268 {
269 uint32_t nonce_val;
270 memcpy(&nonce_val, nonce, sizeof(nonce_val));
271 // A zero nonce never comes from a legitimate sender; refuse to decrypt
272 if (!nonce_val) {
273 key->bad_decryption = true;
274 return;
275 }
276
277 if (memcmp(nonce, key->gre_nonce, sizeof(key->gre_nonce)) != 0) {
278 /* Skip PBKDF2 + rekey while locked out. */
279 if (key->bad_decryption)
280 return;
281 memcpy(key->gre_nonce, nonce, sizeof(key->gre_nonce));
282 _librist_crypto_aes_key(key);
283 /* Only clear the flag if _librist_crypto_aes_key succeeded;
284 * it sets bad_decryption=true on PBKDF2/setup failure. */
285 if (!key->bad_decryption)
286 key->bad_count = 0;
287 }
288
289 if (key->used_times > RIST_AES_KEY_REUSE_TIMES) {
290 key->bad_decryption = true;
291 return;
292 }
293
294 _librist_crypto_psk_prepare_iv(key, gre_version, seq_nbe);
295 #if HAVE_MBEDTLS
296 key->aes_offset = 0;
297 #endif
298 _librist_crypto_psk_aes_ctr(key, inbuf, outbuf, payload_len);
299 return;
300 }
301
302 void _librist_crypto_psk_encrypt(struct rist_key *key, uint32_t seq_nbe, uint8_t gre_version,const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len)
303 {
304 uint32_t nonce_val;
305 memcpy(&nonce_val, key->gre_nonce, sizeof(nonce_val));
306 if (!nonce_val || (key->used_times +1) > RIST_AES_KEY_REUSE_TIMES || (key->key_rotation > 0 && key->used_times >= key->key_rotation)) {
307 _librist_crypto_psk_generate_nonce(key);
308 if (key->csprng_failed) {
309 memset(outbuf, 0, payload_len);
310 return;
311 }
312 _librist_crypto_aes_key(key);
313 }
314 if (key->csprng_failed) {
315 memset(outbuf, 0, payload_len);
316 return;
317 }
318 _librist_crypto_psk_prepare_iv(key, gre_version, seq_nbe);
319 #if HAVE_MBEDTLS
320 key->aes_offset = 0;
321 #endif
322 _librist_crypto_psk_aes_ctr(key, inbuf, outbuf, payload_len);
323 return;
324 }
325
326 int _librist_crypto_psk_set_passphrase(struct rist_key *key, const uint8_t *passsphrase, size_t passphrase_len) {
327 if (passphrase_len > sizeof(key->password) -1) {
328 return -1;
329 }
330 if (key->key_size == 0)
331 key->key_size = 256;
332 memcpy(key->password, passsphrase, passphrase_len);
333 key->password_len = passphrase_len;
334 key->used_times = 0;
335 key->csprng_failed = false; /* fresh passphrase, retry CSPRNG */
336 _librist_crypto_psk_generate_nonce(key);
337 _librist_crypto_aes_key(key);
338 return 0;
339 }
340
341 void _librist_crypto_psk_get_passphrase(struct rist_key *key, const uint8_t **passphrase, size_t *passphrase_len) {
342 *passphrase = key->password;
343 *passphrase_len = key->password_len;
344 }
345
346 void _librist_crypto_psk_encrypt_continue(struct rist_key *key, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len) {
347 _librist_crypto_psk_aes_ctr(key, inbuf, outbuf, payload_len);
348 }
349
350 void _librist_crypto_psk_preannounce_nonce(struct rist_key *key, const uint8_t nonce[4], uint32_t key_size_bits) {
351 uint32_t nonce_val;
352 memcpy(&nonce_val, nonce, sizeof(nonce_val));
353 if (!nonce_val)
354 return;
355 if (memcmp(nonce, key->gre_nonce, sizeof(key->gre_nonce)) == 0)
356 return;
357 if (key_size_bits)
358 key->key_size = key_size_bits;
359 memcpy(key->gre_nonce, nonce, sizeof(key->gre_nonce));
360 _librist_crypto_aes_key(key);
361 }