8681aaba4c78d4705e4a101021e80990db0f9f5d
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_openssl.c
1 /*
2  * OPENSSL crypto backend implementation
3  *
4  * Copyright (C) 2010-2021 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2010-2021 Milan Broz
6  *
7  * This file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  *
27  * You must obey the GNU Lesser General Public License in all respects
28  * for all of the code used other than OpenSSL.
29  */
30
31 #include <string.h>
32 #include <errno.h>
33 #include <openssl/evp.h>
34 #include <openssl/hmac.h>
35 #include <openssl/rand.h>
36 #include "crypto_backend_internal.h"
37
38 #define CONST_CAST(x) (x)(uintptr_t)
39
40 static int crypto_backend_initialised = 0;
41
42 struct crypt_hash {
43         EVP_MD_CTX *md;
44         const EVP_MD *hash_id;
45         int hash_len;
46 };
47
48 struct crypt_hmac {
49         HMAC_CTX *md;
50         const EVP_MD *hash_id;
51         int hash_len;
52 };
53
54 struct crypt_cipher {
55         bool use_kernel;
56         union {
57         struct crypt_cipher_kernel kernel;
58         struct {
59                 EVP_CIPHER_CTX *hd_enc;
60                 EVP_CIPHER_CTX *hd_dec;
61                 size_t iv_length;
62         } lib;
63         } u;
64 };
65
66 struct hash_alg {
67         const char *name;
68         const char *openssl_name;
69 };
70
71 /*
72  * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0
73  */
74 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
75     (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
76
77 static void openssl_backend_init(void)
78 {
79         OpenSSL_add_all_algorithms();
80 }
81
82 static const char *openssl_backend_version(void)
83 {
84         return SSLeay_version(SSLEAY_VERSION);
85 }
86
87 static EVP_MD_CTX *EVP_MD_CTX_new(void)
88 {
89         EVP_MD_CTX *md = malloc(sizeof(*md));
90
91         if (md)
92                 EVP_MD_CTX_init(md);
93
94         return md;
95 }
96
97 static void EVP_MD_CTX_free(EVP_MD_CTX *md)
98 {
99         EVP_MD_CTX_cleanup(md);
100         free(md);
101 }
102
103 static HMAC_CTX *HMAC_CTX_new(void)
104 {
105         HMAC_CTX *md = malloc(sizeof(*md));
106
107         if (md)
108                 HMAC_CTX_init(md);
109
110         return md;
111 }
112
113 static void HMAC_CTX_free(HMAC_CTX *md)
114 {
115         HMAC_CTX_cleanup(md);
116         free(md);
117 }
118 #else
119 static void openssl_backend_init(void)
120 {
121 }
122
123 static const char *openssl_backend_version(void)
124 {
125     return OpenSSL_version(OPENSSL_VERSION);
126 }
127 #endif
128
129 int crypt_backend_init(void)
130 {
131         if (crypto_backend_initialised)
132                 return 0;
133
134         openssl_backend_init();
135
136         crypto_backend_initialised = 1;
137         return 0;
138 }
139
140 void crypt_backend_destroy(void)
141 {
142         crypto_backend_initialised = 0;
143 }
144
145 uint32_t crypt_backend_flags(void)
146 {
147         return 0;
148 }
149
150 const char *crypt_backend_version(void)
151 {
152         return openssl_backend_version();
153 }
154
155 static const char *crypt_hash_compat_name(const char *name)
156 {
157         const char *hash_name = name;
158         int i;
159         static struct hash_alg hash_algs[] = {
160         { "blake2b-512", "blake2b512" },
161         { "blake2s-256", "blake2s256" },
162         { NULL,          NULL,         }};
163
164         if (!name)
165                 return NULL;
166
167         i = 0;
168         while (hash_algs[i].name) {
169                 if (!strcasecmp(name, hash_algs[i].name)) {
170                         hash_name =  hash_algs[i].openssl_name;
171                         break;
172                 }
173                 i++;
174         }
175
176         return hash_name;
177 }
178
179 /* HASH */
180 int crypt_hash_size(const char *name)
181 {
182         const EVP_MD *hash_id;
183
184         hash_id = EVP_get_digestbyname(crypt_hash_compat_name(name));
185         if (!hash_id)
186                 return -EINVAL;
187
188         return EVP_MD_size(hash_id);
189 }
190
191 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
192 {
193         struct crypt_hash *h;
194
195         h = malloc(sizeof(*h));
196         if (!h)
197                 return -ENOMEM;
198
199         h->md = EVP_MD_CTX_new();
200         if (!h->md) {
201                 free(h);
202                 return -ENOMEM;
203         }
204
205         h->hash_id = EVP_get_digestbyname(crypt_hash_compat_name(name));
206         if (!h->hash_id) {
207                 EVP_MD_CTX_free(h->md);
208                 free(h);
209                 return -EINVAL;
210         }
211
212         if (EVP_DigestInit_ex(h->md, h->hash_id, NULL) != 1) {
213                 EVP_MD_CTX_free(h->md);
214                 free(h);
215                 return -EINVAL;
216         }
217
218         h->hash_len = EVP_MD_size(h->hash_id);
219         *ctx = h;
220         return 0;
221 }
222
223 static int crypt_hash_restart(struct crypt_hash *ctx)
224 {
225         if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1)
226                 return -EINVAL;
227
228         return 0;
229 }
230
231 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
232 {
233         if (EVP_DigestUpdate(ctx->md, buffer, length) != 1)
234                 return -EINVAL;
235
236         return 0;
237 }
238
239 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
240 {
241         unsigned char tmp[EVP_MAX_MD_SIZE];
242         unsigned int tmp_len = 0;
243
244         if (length > (size_t)ctx->hash_len)
245                 return -EINVAL;
246
247         if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1)
248                 return -EINVAL;
249
250         memcpy(buffer, tmp, length);
251         crypt_backend_memzero(tmp, sizeof(tmp));
252
253         if (tmp_len < length)
254                 return -EINVAL;
255
256         if (crypt_hash_restart(ctx))
257                 return -EINVAL;
258
259         return 0;
260 }
261
262 void crypt_hash_destroy(struct crypt_hash *ctx)
263 {
264         EVP_MD_CTX_free(ctx->md);
265         memset(ctx, 0, sizeof(*ctx));
266         free(ctx);
267 }
268
269 /* HMAC */
270 int crypt_hmac_size(const char *name)
271 {
272         return crypt_hash_size(name);
273 }
274
275 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
276                     const void *key, size_t key_length)
277 {
278         struct crypt_hmac *h;
279
280         h = malloc(sizeof(*h));
281         if (!h)
282                 return -ENOMEM;
283
284         h->md = HMAC_CTX_new();
285         if (!h->md) {
286                 free(h);
287                 return -ENOMEM;
288         }
289
290         h->hash_id = EVP_get_digestbyname(crypt_hash_compat_name(name));
291         if (!h->hash_id) {
292                 HMAC_CTX_free(h->md);
293                 free(h);
294                 return -EINVAL;
295         }
296
297         HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL);
298
299         h->hash_len = EVP_MD_size(h->hash_id);
300         *ctx = h;
301         return 0;
302 }
303
304 static void crypt_hmac_restart(struct crypt_hmac *ctx)
305 {
306         HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL);
307 }
308
309 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
310 {
311         HMAC_Update(ctx->md, (const unsigned char *)buffer, length);
312         return 0;
313 }
314
315 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
316 {
317         unsigned char tmp[EVP_MAX_MD_SIZE];
318         unsigned int tmp_len = 0;
319
320         if (length > (size_t)ctx->hash_len)
321                 return -EINVAL;
322
323         HMAC_Final(ctx->md, tmp, &tmp_len);
324
325         memcpy(buffer, tmp, length);
326         crypt_backend_memzero(tmp, sizeof(tmp));
327
328         if (tmp_len < length)
329                 return -EINVAL;
330
331         crypt_hmac_restart(ctx);
332
333         return 0;
334 }
335
336 void crypt_hmac_destroy(struct crypt_hmac *ctx)
337 {
338         HMAC_CTX_free(ctx->md);
339         memset(ctx, 0, sizeof(*ctx));
340         free(ctx);
341 }
342
343 /* RNG */
344 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
345 {
346         if (RAND_bytes((unsigned char *)buffer, length) != 1)
347                 return -EINVAL;
348
349         return 0;
350 }
351
352 /* PBKDF */
353 int crypt_pbkdf(const char *kdf, const char *hash,
354                 const char *password, size_t password_length,
355                 const char *salt, size_t salt_length,
356                 char *key, size_t key_length,
357                 uint32_t iterations, uint32_t memory, uint32_t parallel)
358
359 {
360         const EVP_MD *hash_id;
361
362         if (!kdf)
363                 return -EINVAL;
364
365         if (!strcmp(kdf, "pbkdf2")) {
366                 hash_id = EVP_get_digestbyname(crypt_hash_compat_name(hash));
367                 if (!hash_id)
368                         return -EINVAL;
369
370                 if (!PKCS5_PBKDF2_HMAC(password, (int)password_length,
371                     (const unsigned char *)salt, (int)salt_length,
372                     (int)iterations, hash_id, (int)key_length, (unsigned char *)key))
373                         return -EINVAL;
374                 return 0;
375         } else if (!strncmp(kdf, "argon2", 6)) {
376                 return argon2(kdf, password, password_length, salt, salt_length,
377                               key, key_length, iterations, memory, parallel);
378         }
379
380         return -EINVAL;
381 }
382
383 /* Block ciphers */
384 static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec)
385 {
386         EVP_CIPHER_CTX_free(*hd_enc);
387         *hd_enc = NULL;
388
389         EVP_CIPHER_CTX_free(*hd_dec);
390         *hd_dec = NULL;
391 }
392
393 static int _cipher_init(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const char *name,
394                         const char *mode, const void *key, size_t key_length, size_t *iv_length)
395 {
396         char cipher_name[256];
397         const EVP_CIPHER *type;
398         int r, key_bits;
399
400         key_bits = key_length * 8;
401         if (!strcmp(mode, "xts"))
402                 key_bits /= 2;
403
404         r = snprintf(cipher_name, sizeof(cipher_name), "%s-%d-%s", name, key_bits, mode);
405         if (r < 0 || (size_t)r >= sizeof(cipher_name))
406                 return -EINVAL;
407
408         type = EVP_get_cipherbyname(cipher_name);
409         if (!type)
410                 return -ENOENT;
411
412         if (EVP_CIPHER_key_length(type) != (int)key_length)
413                 return -EINVAL;
414
415         *hd_enc = EVP_CIPHER_CTX_new();
416         *hd_dec = EVP_CIPHER_CTX_new();
417         *iv_length = EVP_CIPHER_iv_length(type);
418
419         if (!*hd_enc || !*hd_dec)
420                 return -EINVAL;
421
422         if (EVP_EncryptInit_ex(*hd_enc, type, NULL, key, NULL) != 1 ||
423             EVP_DecryptInit_ex(*hd_dec, type, NULL, key, NULL) != 1) {
424                 _cipher_destroy(hd_enc, hd_dec);
425                 return -EINVAL;
426         }
427
428         if (EVP_CIPHER_CTX_set_padding(*hd_enc, 0) != 1 ||
429             EVP_CIPHER_CTX_set_padding(*hd_dec, 0) != 1) {
430                 _cipher_destroy(hd_enc, hd_dec);
431                 return -EINVAL;
432         }
433
434         return 0;
435 }
436
437 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
438                     const char *mode, const void *key, size_t key_length)
439 {
440         struct crypt_cipher *h;
441         int r;
442
443         h = malloc(sizeof(*h));
444         if (!h)
445                 return -ENOMEM;
446
447         if (!_cipher_init(&h->u.lib.hd_enc, &h->u.lib.hd_dec, name, mode, key,
448                           key_length, &h->u.lib.iv_length)) {
449                 h->use_kernel = false;
450                 *ctx = h;
451                 return 0;
452         }
453
454         r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
455         if (r < 0) {
456                 free(h);
457                 return r;
458         }
459
460         h->use_kernel = true;
461         *ctx = h;
462         return 0;
463 }
464
465 void crypt_cipher_destroy(struct crypt_cipher *ctx)
466 {
467         if (ctx->use_kernel)
468                 crypt_cipher_destroy_kernel(&ctx->u.kernel);
469         else
470                 _cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec);
471         free(ctx);
472 }
473
474 static int _cipher_encrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
475                            int length, const unsigned char *iv, size_t iv_length)
476 {
477         int len;
478
479         if (ctx->u.lib.iv_length != iv_length)
480                 return -EINVAL;
481
482         if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1)
483                 return -EINVAL;
484
485         if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, length) != 1)
486                 return -EINVAL;
487
488         if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1)
489                 return -EINVAL;
490
491         return 0;
492 }
493
494 static int _cipher_decrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
495                            int length, const unsigned char *iv, size_t iv_length)
496 {
497         int len;
498
499         if (ctx->u.lib.iv_length != iv_length)
500                 return -EINVAL;
501
502         if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1)
503                 return -EINVAL;
504
505         if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, length) != 1)
506                 return -EINVAL;
507
508         if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1)
509                 return -EINVAL;
510
511         return 0;
512 }
513
514 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
515                          const char *in, char *out, size_t length,
516                          const char *iv, size_t iv_length)
517 {
518         if (ctx->use_kernel)
519                 return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
520
521         return _cipher_encrypt(ctx, (const unsigned char*)in,
522                                (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
523 }
524
525 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
526                          const char *in, char *out, size_t length,
527                          const char *iv, size_t iv_length)
528 {
529         if (ctx->use_kernel)
530                 return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
531
532         return _cipher_decrypt(ctx, (const unsigned char*)in,
533                                (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
534 }
535
536 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
537 {
538         return ctx->use_kernel;
539 }
540
541 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
542                             const char *in, char *out, size_t length,
543                             const char *iv, size_t iv_length,
544                             const char *tag, size_t tag_length)
545 {
546 #ifdef EVP_CTRL_CCM_SET_IVLEN
547         EVP_CIPHER_CTX *ctx;
548         int len = 0, r = -EINVAL;
549
550         ctx = EVP_CIPHER_CTX_new();
551         if (!ctx)
552                 return -EINVAL;
553
554         if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1)
555                 goto out;
556
557         //EVP_CIPHER_CTX_key_length(ctx)
558         //EVP_CIPHER_CTX_iv_length(ctx)
559
560         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1)
561                 goto out;
562         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1)
563                 goto out;
564
565         if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1)
566                 goto out;
567
568         if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1)
569                 r = 0;
570 out:
571         EVP_CIPHER_CTX_free(ctx);
572         return r;
573 #else
574         return -ENOTSUP;
575 #endif
576 }