/src/cryptsetup/lib/crypto_backend/crypto_openssl.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: LGPL-2.1-or-later WITH cryptsetup-OpenSSL-exception |
2 | | /* |
3 | | * OPENSSL crypto backend implementation |
4 | | * |
5 | | * Copyright (C) 2010-2026 Red Hat, Inc. All rights reserved. |
6 | | * Copyright (C) 2010-2026 Milan Broz |
7 | | */ |
8 | | |
9 | | #include <stdio.h> |
10 | | #include <errno.h> |
11 | | #include <limits.h> |
12 | | #include <strings.h> |
13 | | #include <openssl/crypto.h> |
14 | | #include <openssl/evp.h> |
15 | | #include <openssl/hmac.h> |
16 | | #include <openssl/rand.h> |
17 | | #include "crypto_backend_internal.h" |
18 | | |
19 | | /* |
20 | | * LibreSSL defines OPENSSL_VERSION_MAJOR >= 3 for compatibility but does not |
21 | | * implement the OpenSSL 3.x provider/KDF API. Use this macro instead. |
22 | | */ |
23 | | #if OPENSSL_VERSION_MAJOR >= 3 && !defined(LIBRESSL_VERSION_NUMBER) |
24 | | #define OPENSSL3_API 1 |
25 | | #else |
26 | | #define OPENSSL3_API 0 |
27 | | #endif |
28 | | |
29 | | #if OPENSSL3_API |
30 | | #include <openssl/provider.h> |
31 | | #include <openssl/kdf.h> |
32 | | #include <openssl/core_names.h> |
33 | | #include <openssl/err.h> |
34 | | static OSSL_PROVIDER *ossl_legacy = NULL; |
35 | | static OSSL_PROVIDER *ossl_default = NULL; |
36 | | static OSSL_LIB_CTX *ossl_ctx = NULL; |
37 | | static char backend_version[256] = "OpenSSL"; |
38 | | |
39 | 6 | #define MAX_THREADS 64 |
40 | | #if !HAVE_DECL_OSSL_GET_MAX_THREADS |
41 | | static int OSSL_set_max_threads(OSSL_LIB_CTX *ctx __attribute__((unused)), |
42 | | uint64_t max_threads __attribute__((unused))) { return 0; } |
43 | | static uint64_t OSSL_get_max_threads(OSSL_LIB_CTX *ctx __attribute__((unused))) { return 0; } |
44 | | #else |
45 | | #include <openssl/thread.h> |
46 | | #endif |
47 | | |
48 | | #endif |
49 | | |
50 | 736k | #define CONST_CAST(x) (x)(uintptr_t) |
51 | | #define UNUSED(x) (void)(x) |
52 | | |
53 | | static int crypto_backend_initialised = 0; |
54 | | |
55 | | struct crypt_hash { |
56 | | EVP_MD_CTX *md; |
57 | | const EVP_MD *hash_id; |
58 | | int hash_len; |
59 | | }; |
60 | | |
61 | | struct crypt_hmac { |
62 | | #if OPENSSL3_API |
63 | | EVP_MAC *mac; |
64 | | EVP_MAC_CTX *md; |
65 | | EVP_MAC_CTX *md_org; |
66 | | #else |
67 | | HMAC_CTX *md; |
68 | | const EVP_MD *hash_id; |
69 | | #endif |
70 | | int hash_len; |
71 | | }; |
72 | | |
73 | | struct crypt_cipher { |
74 | | bool use_kernel; |
75 | | union { |
76 | | struct crypt_cipher_kernel kernel; |
77 | | struct { |
78 | | EVP_CIPHER_CTX *hd_enc; |
79 | | EVP_CIPHER_CTX *hd_dec; |
80 | | const EVP_CIPHER *cipher_type; |
81 | | size_t iv_length; |
82 | | } lib; |
83 | | } u; |
84 | | }; |
85 | | |
86 | | struct hash_alg { |
87 | | const char *name; |
88 | | const char *openssl_name; |
89 | | }; |
90 | | |
91 | | /* |
92 | | * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0 |
93 | | */ |
94 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
95 | | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
96 | | |
97 | | static int openssl_backend_init(bool fips __attribute__((unused))) |
98 | | { |
99 | | OpenSSL_add_all_algorithms(); |
100 | | return 0; |
101 | | } |
102 | | |
103 | | static void openssl_backend_exit(void) |
104 | | { |
105 | | } |
106 | | |
107 | | static const char *openssl_backend_version(void) |
108 | | { |
109 | | return SSLeay_version(SSLEAY_VERSION); |
110 | | } |
111 | | |
112 | | static EVP_MD_CTX *EVP_MD_CTX_new(void) |
113 | | { |
114 | | EVP_MD_CTX *md = malloc(sizeof(*md)); |
115 | | |
116 | | if (md) |
117 | | EVP_MD_CTX_init(md); |
118 | | |
119 | | return md; |
120 | | } |
121 | | |
122 | | static void EVP_MD_CTX_free(EVP_MD_CTX *md) |
123 | | { |
124 | | EVP_MD_CTX_cleanup(md); |
125 | | free(md); |
126 | | } |
127 | | |
128 | | static HMAC_CTX *HMAC_CTX_new(void) |
129 | | { |
130 | | HMAC_CTX *md = malloc(sizeof(*md)); |
131 | | |
132 | | if (md) |
133 | | HMAC_CTX_init(md); |
134 | | |
135 | | return md; |
136 | | } |
137 | | |
138 | | static void HMAC_CTX_free(HMAC_CTX *md) |
139 | | { |
140 | | HMAC_CTX_cleanup(md); |
141 | | free(md); |
142 | | } |
143 | | #else |
144 | | static void openssl_backend_exit(void) |
145 | 0 | { |
146 | 0 | #if OPENSSL3_API |
147 | 0 | if (ossl_legacy) |
148 | 0 | OSSL_PROVIDER_unload(ossl_legacy); |
149 | 0 | if (ossl_default) |
150 | 0 | OSSL_PROVIDER_unload(ossl_default); |
151 | 0 | if (ossl_ctx) |
152 | 0 | OSSL_LIB_CTX_free(ossl_ctx); |
153 | |
|
154 | 0 | ossl_legacy = NULL; |
155 | 0 | ossl_default = NULL; |
156 | 0 | ossl_ctx = NULL; |
157 | 0 | #endif |
158 | 0 | } |
159 | | |
160 | | static int openssl_backend_init(bool fips) |
161 | 3 | { |
162 | | /* |
163 | | * OpenSSL >= 3.0.0 provides some algorithms in legacy provider |
164 | | */ |
165 | 3 | #if OPENSSL3_API |
166 | 3 | int r; |
167 | 3 | bool ossl_threads = false; |
168 | | |
169 | | /* |
170 | | * In FIPS mode we keep default OpenSSL context & global config |
171 | | */ |
172 | 3 | if (!fips) { |
173 | 3 | ossl_ctx = OSSL_LIB_CTX_new(); |
174 | 3 | if (!ossl_ctx) |
175 | 0 | return -EINVAL; |
176 | | |
177 | 3 | ossl_default = OSSL_PROVIDER_try_load(ossl_ctx, "default", 0); |
178 | 3 | if (!ossl_default) { |
179 | 0 | OSSL_LIB_CTX_free(ossl_ctx); |
180 | 0 | return -EINVAL; |
181 | 0 | } |
182 | | |
183 | | /* Optional */ |
184 | 3 | ossl_legacy = OSSL_PROVIDER_try_load(ossl_ctx, "legacy", 0); |
185 | 3 | } |
186 | | |
187 | 3 | if (OSSL_set_max_threads(ossl_ctx, MAX_THREADS) == 1 && |
188 | 3 | OSSL_get_max_threads(ossl_ctx) == MAX_THREADS) |
189 | 3 | ossl_threads = true; |
190 | | |
191 | 3 | r = snprintf(backend_version, sizeof(backend_version), "%s %s%s%s%s%s", |
192 | 3 | OpenSSL_version(OPENSSL_VERSION), |
193 | 3 | ossl_default ? "[default]" : "", |
194 | 3 | ossl_legacy ? "[legacy]" : "", |
195 | 3 | fips ? "[fips]" : "", |
196 | 3 | ossl_threads ? "[threads]" : "", |
197 | 3 | crypt_backend_flags() & CRYPT_BACKEND_ARGON2 ? "[argon2]" : ""); |
198 | | |
199 | 3 | if (r < 0 || (size_t)r >= sizeof(backend_version)) { |
200 | 0 | openssl_backend_exit(); |
201 | 0 | return -EINVAL; |
202 | 0 | } |
203 | | #else |
204 | | UNUSED(fips); |
205 | | #endif |
206 | 3 | return 0; |
207 | 3 | } |
208 | | |
209 | | static const char *openssl_backend_version(void) |
210 | 3 | { |
211 | 3 | #if OPENSSL3_API |
212 | 3 | return backend_version; |
213 | | #else |
214 | | return OpenSSL_version(OPENSSL_VERSION); |
215 | | #endif |
216 | 3 | } |
217 | | #endif |
218 | | |
219 | | int crypt_backend_init(void) |
220 | 18.6k | { |
221 | 18.6k | if (crypto_backend_initialised) |
222 | 18.6k | return 0; |
223 | | |
224 | 3 | if (openssl_backend_init(crypt_fips_mode())) |
225 | 0 | return -EINVAL; |
226 | | |
227 | 3 | crypto_backend_initialised = 1; |
228 | 3 | return 0; |
229 | 3 | } |
230 | | |
231 | | void crypt_backend_destroy(void) |
232 | 0 | { |
233 | | /* |
234 | | * If Destructor was already called, we must not call it again |
235 | | */ |
236 | 0 | if (!crypto_backend_initialised) |
237 | 0 | return; |
238 | | |
239 | 0 | crypto_backend_initialised = 0; |
240 | |
|
241 | 0 | openssl_backend_exit(); |
242 | 0 | } |
243 | | |
244 | | uint32_t crypt_backend_flags(void) |
245 | 6 | { |
246 | 6 | uint32_t flags = 0; |
247 | | #if !OPENSSL3_API |
248 | | flags |= CRYPT_BACKEND_PBKDF2_INT; |
249 | | #endif |
250 | 6 | #if OPENSSL3_API && HAVE_DECL_OSSL_KDF_PARAM_ARGON2_VERSION |
251 | 6 | flags |= CRYPT_BACKEND_ARGON2; |
252 | 6 | #endif |
253 | 6 | return flags; |
254 | 6 | } |
255 | | |
256 | | const char *crypt_backend_version(void) |
257 | 3 | { |
258 | 3 | return openssl_backend_version(); |
259 | 3 | } |
260 | | |
261 | | static const char *crypt_hash_compat_name(const char *name) |
262 | 737k | { |
263 | 737k | const char *hash_name = name; |
264 | 737k | int i; |
265 | 737k | static struct hash_alg hash_algs[] = { |
266 | 737k | { "blake2b-512", "blake2b512" }, |
267 | 737k | { "blake2s-256", "blake2s256" }, |
268 | 737k | { NULL, NULL, }}; |
269 | | |
270 | 737k | if (!name) |
271 | 0 | return NULL; |
272 | | |
273 | 737k | i = 0; |
274 | 2.19M | while (hash_algs[i].name) { |
275 | 1.47M | if (!strcasecmp(name, hash_algs[i].name)) { |
276 | 7.57k | hash_name = hash_algs[i].openssl_name; |
277 | 7.57k | break; |
278 | 7.57k | } |
279 | 1.46M | i++; |
280 | 1.46M | } |
281 | | |
282 | 737k | return hash_name; |
283 | 737k | } |
284 | | |
285 | | static const EVP_MD *hash_id_get(const char *name) |
286 | 737k | { |
287 | 737k | #if OPENSSL3_API |
288 | 737k | return EVP_MD_fetch(ossl_ctx, crypt_hash_compat_name(name), NULL); |
289 | | #else |
290 | | return EVP_get_digestbyname(crypt_hash_compat_name(name)); |
291 | | #endif |
292 | 737k | } |
293 | | |
294 | | static void hash_id_free(const EVP_MD *hash_id) |
295 | 736k | { |
296 | 736k | #if OPENSSL3_API |
297 | 736k | EVP_MD_free(CONST_CAST(EVP_MD*)hash_id); |
298 | | #else |
299 | | UNUSED(hash_id); |
300 | | #endif |
301 | 736k | } |
302 | | |
303 | | static const EVP_CIPHER *cipher_type_get(const char *name) |
304 | 244 | { |
305 | 244 | #if OPENSSL3_API |
306 | 244 | return EVP_CIPHER_fetch(ossl_ctx, name, NULL); |
307 | | #else |
308 | | return EVP_get_cipherbyname(name); |
309 | | #endif |
310 | 244 | } |
311 | | |
312 | | static void cipher_type_free(const EVP_CIPHER *cipher_type) |
313 | 244 | { |
314 | 244 | #if OPENSSL3_API |
315 | 244 | EVP_CIPHER_free(CONST_CAST(EVP_CIPHER*)cipher_type); |
316 | | #else |
317 | | UNUSED(cipher_type); |
318 | | #endif |
319 | 244 | } |
320 | | |
321 | | /* HASH */ |
322 | | int crypt_hash_size(const char *name) |
323 | 729k | { |
324 | 729k | int size; |
325 | 729k | const EVP_MD *hash_id; |
326 | | |
327 | 729k | hash_id = hash_id_get(name); |
328 | 729k | if (!hash_id) |
329 | 1.11k | return -EINVAL; |
330 | | |
331 | 727k | size = EVP_MD_size(hash_id); |
332 | 727k | hash_id_free(hash_id); |
333 | 727k | return size; |
334 | 729k | } |
335 | | |
336 | | int crypt_hash_init(struct crypt_hash **ctx, const char *name) |
337 | 8.35k | { |
338 | 8.35k | struct crypt_hash *h; |
339 | | |
340 | 8.35k | h = malloc(sizeof(*h)); |
341 | 8.35k | if (!h) |
342 | 0 | return -ENOMEM; |
343 | | |
344 | 8.35k | h->md = EVP_MD_CTX_new(); |
345 | 8.35k | if (!h->md) { |
346 | 0 | free(h); |
347 | 0 | return -ENOMEM; |
348 | 0 | } |
349 | | |
350 | 8.35k | h->hash_id = hash_id_get(name); |
351 | 8.35k | if (!h->hash_id) { |
352 | 0 | EVP_MD_CTX_free(h->md); |
353 | 0 | free(h); |
354 | 0 | return -EINVAL; |
355 | 0 | } |
356 | | |
357 | 8.35k | if (EVP_DigestInit_ex(h->md, h->hash_id, NULL) != 1) { |
358 | 0 | hash_id_free(h->hash_id); |
359 | 0 | EVP_MD_CTX_free(h->md); |
360 | 0 | free(h); |
361 | 0 | return -EINVAL; |
362 | 0 | } |
363 | | |
364 | 8.35k | h->hash_len = EVP_MD_size(h->hash_id); |
365 | 8.35k | *ctx = h; |
366 | 8.35k | return 0; |
367 | 8.35k | } |
368 | | |
369 | | static int crypt_hash_restart(struct crypt_hash *ctx) |
370 | 8.35k | { |
371 | 8.35k | if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1) |
372 | 0 | return -EINVAL; |
373 | | |
374 | 8.35k | return 0; |
375 | 8.35k | } |
376 | | |
377 | | int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length) |
378 | 16.6k | { |
379 | 16.6k | if (EVP_DigestUpdate(ctx->md, buffer, length) != 1) |
380 | 0 | return -EINVAL; |
381 | | |
382 | 16.6k | return 0; |
383 | 16.6k | } |
384 | | |
385 | | int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length) |
386 | 8.35k | { |
387 | 8.35k | unsigned char tmp[EVP_MAX_MD_SIZE]; |
388 | 8.35k | unsigned int tmp_len = 0; |
389 | | |
390 | 8.35k | if (length > (size_t)ctx->hash_len) |
391 | 0 | return -EINVAL; |
392 | | |
393 | 8.35k | if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1) |
394 | 0 | return -EINVAL; |
395 | | |
396 | 8.35k | if (tmp_len < length) { |
397 | 0 | crypt_backend_memzero(tmp, sizeof(tmp)); |
398 | 0 | return -EINVAL; |
399 | 0 | } |
400 | | |
401 | 8.35k | crypt_backend_memcpy(buffer, tmp, length); |
402 | 8.35k | crypt_backend_memzero(tmp, sizeof(tmp)); |
403 | | |
404 | 8.35k | if (crypt_hash_restart(ctx)) |
405 | 0 | return -EINVAL; |
406 | | |
407 | 8.35k | return 0; |
408 | 8.35k | } |
409 | | |
410 | | void crypt_hash_destroy(struct crypt_hash *ctx) |
411 | 8.35k | { |
412 | 8.35k | hash_id_free(ctx->hash_id); |
413 | 8.35k | EVP_MD_CTX_free(ctx->md); |
414 | 8.35k | free(ctx); |
415 | 8.35k | } |
416 | | |
417 | | /* HMAC */ |
418 | | int crypt_hmac_size(const char *name) |
419 | 590 | { |
420 | 590 | return crypt_hash_size(name); |
421 | 590 | } |
422 | | |
423 | | int crypt_hmac_init(struct crypt_hmac **ctx, const char *name, |
424 | | const void *key, size_t key_length) |
425 | 0 | { |
426 | 0 | struct crypt_hmac *h; |
427 | 0 | #if OPENSSL3_API |
428 | 0 | OSSL_PARAM params[] = { |
429 | 0 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, CONST_CAST(void*)name, 0), |
430 | 0 | OSSL_PARAM_END |
431 | 0 | }; |
432 | |
|
433 | 0 | h = malloc(sizeof(*h)); |
434 | 0 | if (!h) |
435 | 0 | return -ENOMEM; |
436 | | |
437 | 0 | h->mac = EVP_MAC_fetch(ossl_ctx, OSSL_MAC_NAME_HMAC, NULL); |
438 | 0 | if (!h->mac) { |
439 | 0 | free(h); |
440 | 0 | return -EINVAL; |
441 | 0 | } |
442 | | |
443 | 0 | h->md = EVP_MAC_CTX_new(h->mac); |
444 | 0 | if (!h->md) { |
445 | 0 | EVP_MAC_free(h->mac); |
446 | 0 | free(h); |
447 | 0 | return -ENOMEM; |
448 | 0 | } |
449 | | |
450 | 0 | if (EVP_MAC_init(h->md, key, key_length, params) != 1) { |
451 | 0 | EVP_MAC_CTX_free(h->md); |
452 | 0 | EVP_MAC_free(h->mac); |
453 | 0 | free(h); |
454 | 0 | return -EINVAL; |
455 | 0 | } |
456 | | |
457 | 0 | h->hash_len = EVP_MAC_CTX_get_mac_size(h->md); |
458 | 0 | h->md_org = EVP_MAC_CTX_dup(h->md); |
459 | 0 | if (!h->md_org) { |
460 | 0 | EVP_MAC_CTX_free(h->md); |
461 | 0 | EVP_MAC_free(h->mac); |
462 | 0 | free(h); |
463 | 0 | return -EINVAL; |
464 | 0 | } |
465 | | #else |
466 | | h = malloc(sizeof(*h)); |
467 | | if (!h) |
468 | | return -ENOMEM; |
469 | | |
470 | | h->md = HMAC_CTX_new(); |
471 | | if (!h->md) { |
472 | | free(h); |
473 | | return -ENOMEM; |
474 | | } |
475 | | |
476 | | h->hash_id = hash_id_get(name); |
477 | | if (!h->hash_id) { |
478 | | HMAC_CTX_free(h->md); |
479 | | free(h); |
480 | | return -EINVAL; |
481 | | } |
482 | | |
483 | | if (HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL) != 1) { |
484 | | hash_id_free(h->hash_id); |
485 | | HMAC_CTX_free(h->md); |
486 | | free(h); |
487 | | return -EINVAL; |
488 | | } |
489 | | |
490 | | h->hash_len = EVP_MD_size(h->hash_id); |
491 | | #endif |
492 | 0 | *ctx = h; |
493 | 0 | return 0; |
494 | 0 | } |
495 | | |
496 | | static int crypt_hmac_restart(struct crypt_hmac *ctx) |
497 | 0 | { |
498 | 0 | #if OPENSSL3_API |
499 | 0 | EVP_MAC_CTX_free(ctx->md); |
500 | 0 | ctx->md = EVP_MAC_CTX_dup(ctx->md_org); |
501 | 0 | if (!ctx->md) |
502 | 0 | return -EINVAL; |
503 | | #else |
504 | | if (HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL) != 1) |
505 | | return -EINVAL; |
506 | | #endif |
507 | 0 | return 0; |
508 | 0 | } |
509 | | |
510 | | int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length) |
511 | 0 | { |
512 | 0 | #if OPENSSL3_API |
513 | 0 | return EVP_MAC_update(ctx->md, (const unsigned char *)buffer, length) == 1 ? 0 : -EINVAL; |
514 | | #else |
515 | | return HMAC_Update(ctx->md, (const unsigned char *)buffer, length) == 1 ? 0 : -EINVAL; |
516 | | #endif |
517 | 0 | } |
518 | | |
519 | | int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length) |
520 | 0 | { |
521 | 0 | unsigned char tmp[EVP_MAX_MD_SIZE]; |
522 | 0 | #if OPENSSL3_API |
523 | 0 | size_t tmp_len = 0; |
524 | |
|
525 | 0 | if (length > (size_t)ctx->hash_len) |
526 | 0 | return -EINVAL; |
527 | | |
528 | 0 | if (EVP_MAC_final(ctx->md, tmp, &tmp_len, sizeof(tmp)) != 1) |
529 | 0 | return -EINVAL; |
530 | | #else |
531 | | unsigned int tmp_len = 0; |
532 | | |
533 | | if (length > (size_t)ctx->hash_len) |
534 | | return -EINVAL; |
535 | | |
536 | | if (HMAC_Final(ctx->md, tmp, &tmp_len) != 1) |
537 | | return -EINVAL; |
538 | | #endif |
539 | 0 | if (tmp_len < length) { |
540 | 0 | crypt_backend_memzero(tmp, sizeof(tmp)); |
541 | 0 | return -EINVAL; |
542 | 0 | } |
543 | | |
544 | 0 | crypt_backend_memcpy(buffer, tmp, length); |
545 | 0 | crypt_backend_memzero(tmp, sizeof(tmp)); |
546 | |
|
547 | 0 | if (crypt_hmac_restart(ctx)) |
548 | 0 | return -EINVAL; |
549 | | |
550 | 0 | return 0; |
551 | 0 | } |
552 | | |
553 | | void crypt_hmac_destroy(struct crypt_hmac *ctx) |
554 | 0 | { |
555 | 0 | #if OPENSSL3_API |
556 | 0 | EVP_MAC_CTX_free(ctx->md); |
557 | 0 | EVP_MAC_CTX_free(ctx->md_org); |
558 | 0 | EVP_MAC_free(ctx->mac); |
559 | | #else |
560 | | hash_id_free(ctx->hash_id); |
561 | | HMAC_CTX_free(ctx->md); |
562 | | #endif |
563 | 0 | free(ctx); |
564 | 0 | } |
565 | | |
566 | | /* RNG */ |
567 | | int crypt_backend_rng(char *buffer, size_t length, |
568 | | int quality __attribute__((unused)), int fips __attribute__((unused))) |
569 | 0 | { |
570 | 0 | if (length > INT_MAX) |
571 | 0 | return -EINVAL; |
572 | | |
573 | 0 | if (RAND_bytes((unsigned char *)buffer, length) != 1) |
574 | 0 | return -EINVAL; |
575 | | |
576 | 0 | return 0; |
577 | 0 | } |
578 | | |
579 | | static int openssl_pbkdf2(const char *password, size_t password_length, |
580 | | const char *salt, size_t salt_length, uint32_t iterations, |
581 | | const char *hash, char *key, size_t key_length) |
582 | 0 | { |
583 | 0 | int r; |
584 | 0 | #if OPENSSL3_API |
585 | 0 | EVP_KDF_CTX *ctx; |
586 | 0 | EVP_KDF *pbkdf2; |
587 | 0 | OSSL_PARAM params[] = { |
588 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, |
589 | 0 | CONST_CAST(void*)password, password_length), |
590 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, |
591 | 0 | CONST_CAST(void*)salt, salt_length), |
592 | 0 | OSSL_PARAM_uint32(OSSL_KDF_PARAM_ITER, &iterations), |
593 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, |
594 | 0 | CONST_CAST(void*)hash, 0), |
595 | 0 | OSSL_PARAM_END |
596 | 0 | }; |
597 | |
|
598 | 0 | pbkdf2 = EVP_KDF_fetch(ossl_ctx, "pbkdf2", NULL); |
599 | 0 | if (!pbkdf2) |
600 | 0 | return -EINVAL; |
601 | | |
602 | 0 | ctx = EVP_KDF_CTX_new(pbkdf2); |
603 | 0 | if (!ctx) { |
604 | 0 | EVP_KDF_free(pbkdf2); |
605 | 0 | return -EINVAL; |
606 | 0 | } |
607 | | |
608 | 0 | r = EVP_KDF_derive(ctx, (unsigned char*)key, key_length, params); |
609 | |
|
610 | 0 | EVP_KDF_CTX_free(ctx); |
611 | 0 | EVP_KDF_free(pbkdf2); |
612 | | #else |
613 | | const EVP_MD *hash_id = EVP_get_digestbyname(crypt_hash_compat_name(hash)); |
614 | | if (!hash_id) |
615 | | return -EINVAL; |
616 | | |
617 | | /* OpenSSL2 has iteration as signed int, avoid overflow */ |
618 | | if (iterations > INT_MAX) |
619 | | return -EINVAL; |
620 | | |
621 | | if (password_length > INT_MAX || salt_length > INT_MAX || key_length > INT_MAX) |
622 | | return -EINVAL; |
623 | | |
624 | | r = PKCS5_PBKDF2_HMAC(password, (int)password_length, (const unsigned char *)salt, |
625 | | (int)salt_length, iterations, hash_id, (int)key_length, (unsigned char*) key); |
626 | | #endif |
627 | 0 | return r == 1 ? 0 : -EINVAL; |
628 | 0 | } |
629 | | |
630 | | static int openssl_argon2(const char *type, const char *password, size_t password_length, |
631 | | const char *salt, size_t salt_length, char *key, size_t key_length, |
632 | | uint32_t iterations, uint32_t memory, uint32_t parallel) |
633 | 0 | { |
634 | 0 | #if OPENSSL3_API && HAVE_DECL_OSSL_KDF_PARAM_ARGON2_VERSION |
635 | 0 | EVP_KDF_CTX *ctx; |
636 | 0 | EVP_KDF *argon2; |
637 | 0 | unsigned int threads = parallel; |
638 | 0 | int r; |
639 | 0 | OSSL_PARAM params[] = { |
640 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, |
641 | 0 | CONST_CAST(void*)password, password_length), |
642 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, |
643 | 0 | CONST_CAST(void*)salt, salt_length), |
644 | 0 | OSSL_PARAM_uint32(OSSL_KDF_PARAM_ITER, &iterations), |
645 | 0 | OSSL_PARAM_uint(OSSL_KDF_PARAM_THREADS, &threads), |
646 | 0 | OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_LANES, ¶llel), |
647 | 0 | OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, &memory), |
648 | 0 | OSSL_PARAM_END |
649 | 0 | }; |
650 | |
|
651 | 0 | if (OSSL_get_max_threads(ossl_ctx) == 0) |
652 | 0 | threads = 1; |
653 | |
|
654 | 0 | argon2 = EVP_KDF_fetch(ossl_ctx, type, NULL); |
655 | 0 | if (!argon2) |
656 | 0 | return -EINVAL; |
657 | | |
658 | 0 | ctx = EVP_KDF_CTX_new(argon2); |
659 | 0 | if (!ctx) { |
660 | 0 | EVP_KDF_free(argon2); |
661 | 0 | return -EINVAL; |
662 | 0 | } |
663 | | |
664 | 0 | if (EVP_KDF_CTX_set_params(ctx, params) != 1) { |
665 | 0 | EVP_KDF_CTX_free(ctx); |
666 | 0 | EVP_KDF_free(argon2); |
667 | 0 | return -EINVAL; |
668 | 0 | } |
669 | | |
670 | 0 | r = EVP_KDF_derive(ctx, (unsigned char*)key, key_length, NULL /*params*/); |
671 | |
|
672 | 0 | EVP_KDF_CTX_free(ctx); |
673 | 0 | EVP_KDF_free(argon2); |
674 | | |
675 | | /* Memory allocation is common issue with memory-hard Argon2 */ |
676 | 0 | if (r == 0 && ERR_GET_REASON(ERR_get_error()) == ERR_R_MALLOC_FAILURE) |
677 | 0 | return -ENOMEM; |
678 | | |
679 | | /* _derive() returns 0 or negative value on error, 1 on success */ |
680 | 0 | return r == 1 ? 0 : -EINVAL; |
681 | | #else |
682 | | return argon2(type, password, password_length, salt, salt_length, |
683 | | key, key_length, iterations, memory, parallel); |
684 | | #endif |
685 | 0 | } |
686 | | |
687 | | /* PBKDF */ |
688 | | int crypt_pbkdf(const char *kdf, const char *hash, |
689 | | const char *password, size_t password_length, |
690 | | const char *salt, size_t salt_length, |
691 | | char *key, size_t key_length, |
692 | | uint32_t iterations, uint32_t memory, uint32_t parallel) |
693 | 0 | { |
694 | 0 | if (!kdf) |
695 | 0 | return -EINVAL; |
696 | | |
697 | 0 | if (!strcmp(kdf, "pbkdf2")) |
698 | 0 | return openssl_pbkdf2(password, password_length, salt, salt_length, |
699 | 0 | iterations, hash, key, key_length); |
700 | 0 | if (!strncmp(kdf, "argon2", 6)) |
701 | 0 | return openssl_argon2(kdf, password, password_length, salt, salt_length, |
702 | 0 | key, key_length, iterations, memory, parallel); |
703 | 0 | return -EINVAL; |
704 | 0 | } |
705 | | |
706 | | /* Block ciphers */ |
707 | | static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const EVP_CIPHER **cipher_type) |
708 | 244 | { |
709 | 244 | EVP_CIPHER_CTX_free(*hd_enc); |
710 | 244 | *hd_enc = NULL; |
711 | | |
712 | 244 | EVP_CIPHER_CTX_free(*hd_dec); |
713 | 244 | *hd_dec = NULL; |
714 | | |
715 | 244 | cipher_type_free(*cipher_type); |
716 | 244 | *cipher_type = NULL; |
717 | 244 | } |
718 | | |
719 | | /* |
720 | | * SM4-XTS can have two variants of tweak calculation: |
721 | | * "GB": GB/T 17964-2021 tweak multiplication |
722 | | * "IEEE": IEEE Std 1619-2007 tweak multiplication (as used by AES-XTS) |
723 | | * |
724 | | * "GB" is the default, but we need to use mode implemented by the kernel here (IEEE). |
725 | | */ |
726 | | static int _cipher_xts_ieee(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec) |
727 | 0 | { |
728 | 0 | #if OPENSSL3_API && HAVE_DECL_OSSL_CIPHER_PARAM_XTS_STANDARD |
729 | 0 | OSSL_PARAM p[] = { |
730 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD, CONST_CAST(char*)"IEEE", 0), |
731 | 0 | OSSL_PARAM_construct_end() |
732 | 0 | }; |
733 | |
|
734 | 0 | if (EVP_CIPHER_CTX_set_params(*hd_enc, p) != 1 || |
735 | 0 | EVP_CIPHER_CTX_set_params(*hd_dec, p) != 1) |
736 | 0 | return -EINVAL; |
737 | | #else |
738 | | UNUSED(hd_enc); |
739 | | UNUSED(hd_dec); |
740 | | #endif |
741 | 0 | return 0; |
742 | 0 | } |
743 | | |
744 | | static int _cipher_init(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const EVP_CIPHER **cipher_type, const char *name, |
745 | | const char *mode, const void *key, size_t key_length, size_t *iv_length) |
746 | 244 | { |
747 | 244 | char cipher_name[256]; |
748 | 244 | const EVP_CIPHER *type; |
749 | 244 | int r, key_bits; |
750 | 244 | bool set_xts_ieee = false; |
751 | | |
752 | 244 | key_bits = key_length * 8; |
753 | 244 | if (!strcmp(mode, "xts")) |
754 | 244 | key_bits /= 2; |
755 | | |
756 | 244 | if ((!strcmp(name, "sm4")) && key_bits == 128) { |
757 | 0 | set_xts_ieee = true; |
758 | 0 | r = snprintf(cipher_name, sizeof(cipher_name), "%s-%s", name, mode); |
759 | 0 | } else |
760 | 244 | r = snprintf(cipher_name, sizeof(cipher_name), "%s-%d-%s", name, key_bits, mode); |
761 | 244 | if (r < 0 || (size_t)r >= sizeof(cipher_name)) |
762 | 0 | return -EINVAL; |
763 | | |
764 | 244 | type = cipher_type_get(cipher_name); |
765 | 244 | if (!type) |
766 | 0 | return -ENOENT; |
767 | | |
768 | 244 | if (EVP_CIPHER_key_length(type) != (int)key_length) { |
769 | 0 | cipher_type_free(type); |
770 | 0 | return -EINVAL; |
771 | 0 | } |
772 | | |
773 | 244 | *hd_enc = EVP_CIPHER_CTX_new(); |
774 | 244 | *hd_dec = EVP_CIPHER_CTX_new(); |
775 | 244 | *iv_length = EVP_CIPHER_iv_length(type); |
776 | | |
777 | 244 | if (!*hd_enc || !*hd_dec) { |
778 | 0 | cipher_type_free(type); |
779 | 0 | return -EINVAL; |
780 | 0 | } |
781 | | |
782 | 244 | if (EVP_EncryptInit_ex(*hd_enc, type, NULL, key, NULL) != 1 || |
783 | 243 | EVP_DecryptInit_ex(*hd_dec, type, NULL, key, NULL) != 1) { |
784 | 1 | _cipher_destroy(hd_enc, hd_dec, &type); |
785 | 1 | return -EINVAL; |
786 | 1 | } |
787 | | |
788 | 243 | if (set_xts_ieee && _cipher_xts_ieee(hd_enc, hd_dec) < 0) { |
789 | 0 | _cipher_destroy(hd_enc, hd_dec, &type); |
790 | 0 | return -EINVAL; |
791 | 0 | } |
792 | | |
793 | 243 | if (EVP_CIPHER_CTX_set_padding(*hd_enc, 0) != 1 || |
794 | 243 | EVP_CIPHER_CTX_set_padding(*hd_dec, 0) != 1) { |
795 | 0 | _cipher_destroy(hd_enc, hd_dec, &type); |
796 | 0 | return -EINVAL; |
797 | 0 | } |
798 | | |
799 | 243 | *cipher_type = type; |
800 | | |
801 | 243 | return 0; |
802 | 243 | } |
803 | | |
804 | | int crypt_cipher_init(struct crypt_cipher **ctx, const char *name, |
805 | | const char *mode, const void *key, size_t key_length) |
806 | 244 | { |
807 | 244 | struct crypt_cipher *h; |
808 | 244 | int r; |
809 | | |
810 | 244 | h = malloc(sizeof(*h)); |
811 | 244 | if (!h) |
812 | 0 | return -ENOMEM; |
813 | | |
814 | 244 | if (!_cipher_init(&h->u.lib.hd_enc, &h->u.lib.hd_dec, &h->u.lib.cipher_type, name, mode, key, |
815 | 244 | key_length, &h->u.lib.iv_length)) { |
816 | 243 | h->use_kernel = false; |
817 | 243 | *ctx = h; |
818 | 243 | return 0; |
819 | 243 | } |
820 | | |
821 | 1 | r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length); |
822 | 1 | if (r < 0) { |
823 | 0 | free(h); |
824 | 0 | return r; |
825 | 0 | } |
826 | | |
827 | 1 | h->use_kernel = true; |
828 | 1 | *ctx = h; |
829 | 1 | return 0; |
830 | 1 | } |
831 | | |
832 | | void crypt_cipher_destroy(struct crypt_cipher *ctx) |
833 | 244 | { |
834 | 244 | if (ctx->use_kernel) |
835 | 1 | crypt_cipher_destroy_kernel(&ctx->u.kernel); |
836 | 243 | else |
837 | 243 | _cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec, &ctx->u.lib.cipher_type); |
838 | 244 | free(ctx); |
839 | 244 | } |
840 | | |
841 | | static int _cipher_encrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out, |
842 | | size_t length, const unsigned char *iv, size_t iv_length) |
843 | 0 | { |
844 | 0 | int len; |
845 | |
|
846 | 0 | if (length > INT_MAX) |
847 | 0 | return -EINVAL; |
848 | | |
849 | 0 | if (ctx->u.lib.iv_length != iv_length) |
850 | 0 | return -EINVAL; |
851 | | |
852 | 0 | if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1) |
853 | 0 | return -EINVAL; |
854 | | |
855 | 0 | if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, (int)length) != 1) |
856 | 0 | return -EINVAL; |
857 | | |
858 | 0 | if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1) |
859 | 0 | return -EINVAL; |
860 | | |
861 | 0 | return 0; |
862 | 0 | } |
863 | | |
864 | | static int _cipher_decrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out, |
865 | | size_t length, const unsigned char *iv, size_t iv_length) |
866 | 144 | { |
867 | 144 | int len; |
868 | | |
869 | 144 | if (length > INT_MAX) |
870 | 0 | return -EINVAL; |
871 | | |
872 | 144 | if (ctx->u.lib.iv_length != iv_length) |
873 | 0 | return -EINVAL; |
874 | | |
875 | 144 | if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1) |
876 | 0 | return -EINVAL; |
877 | | |
878 | 144 | if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, (int)length) != 1) |
879 | 0 | return -EINVAL; |
880 | | |
881 | 144 | if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1) |
882 | 0 | return -EINVAL; |
883 | | |
884 | 144 | return 0; |
885 | 144 | } |
886 | | |
887 | | int crypt_cipher_encrypt(struct crypt_cipher *ctx, |
888 | | const char *in, char *out, size_t length, |
889 | | const char *iv, size_t iv_length) |
890 | 0 | { |
891 | 0 | if (ctx->use_kernel) |
892 | 0 | return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length); |
893 | | |
894 | 0 | return _cipher_encrypt(ctx, (const unsigned char*)in, |
895 | 0 | (unsigned char *)out, length, (const unsigned char*)iv, iv_length); |
896 | 0 | } |
897 | | |
898 | | int crypt_cipher_decrypt(struct crypt_cipher *ctx, |
899 | | const char *in, char *out, size_t length, |
900 | | const char *iv, size_t iv_length) |
901 | 145 | { |
902 | 145 | if (ctx->use_kernel) |
903 | 1 | return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length); |
904 | | |
905 | 144 | return _cipher_decrypt(ctx, (const unsigned char*)in, |
906 | 144 | (unsigned char *)out, length, (const unsigned char*)iv, iv_length); |
907 | 145 | } |
908 | | |
909 | | bool crypt_cipher_kernel_only(struct crypt_cipher *ctx) |
910 | 0 | { |
911 | 0 | return ctx->use_kernel; |
912 | 0 | } |
913 | | |
914 | | int crypt_bitlk_decrypt_key(const void *key, size_t key_length __attribute__((unused)), |
915 | | const char *in, char *out, size_t length, |
916 | | const char *iv, size_t iv_length, |
917 | | const char *tag, size_t tag_length) |
918 | 0 | { |
919 | 0 | #ifdef EVP_CTRL_CCM_SET_IVLEN |
920 | 0 | EVP_CIPHER_CTX *ctx; |
921 | 0 | int len = 0, r = -EINVAL; |
922 | |
|
923 | 0 | ctx = EVP_CIPHER_CTX_new(); |
924 | 0 | if (!ctx) |
925 | 0 | return -EINVAL; |
926 | | |
927 | 0 | if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1) |
928 | 0 | goto out; |
929 | | |
930 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1) |
931 | 0 | goto out; |
932 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1) |
933 | 0 | goto out; |
934 | | |
935 | 0 | if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1) |
936 | 0 | goto out; |
937 | | |
938 | 0 | if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1) |
939 | 0 | r = 0; |
940 | 0 | out: |
941 | 0 | EVP_CIPHER_CTX_free(ctx); |
942 | 0 | return r; |
943 | | #else |
944 | | return -ENOTSUP; |
945 | | #endif |
946 | 0 | } |
947 | | |
948 | | int crypt_backend_memeq(const void *m1, const void *m2, size_t n) |
949 | 0 | { |
950 | 0 | return CRYPTO_memcmp(m1, m2, n); |
951 | 0 | } |
952 | | |
953 | | #if !ENABLE_FIPS |
954 | 6.94k | bool crypt_fips_mode(void) { return false; } |
955 | | #else |
956 | | static bool openssl_fips_mode(void) |
957 | | { |
958 | | #if OPENSSL3_API |
959 | | return EVP_default_properties_is_fips_enabled(NULL); |
960 | | #else |
961 | | return FIPS_mode(); |
962 | | #endif |
963 | | } |
964 | | |
965 | | bool crypt_fips_mode(void) |
966 | | { |
967 | | static bool fips_mode = false, fips_checked = false; |
968 | | |
969 | | if (fips_checked) |
970 | | return fips_mode; |
971 | | |
972 | | fips_mode = openssl_fips_mode(); |
973 | | fips_checked = true; |
974 | | |
975 | | return fips_mode; |
976 | | } |
977 | | #endif /* ENABLE FIPS */ |