2edec7bb665472b01d6c25f9a3d06f74fd5aca44
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_openssl.c
1 /*
2  * OPENSSL crypto backend implementation
3  *
4  * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2010-2020 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 /*
67  * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0
68  */
69 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
70     (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
71
72 static void openssl_backend_init(void)
73 {
74         OpenSSL_add_all_algorithms();
75 }
76
77 static const char *openssl_backend_version(void)
78 {
79         return SSLeay_version(SSLEAY_VERSION);
80 }
81
82 static EVP_MD_CTX *EVP_MD_CTX_new(void)
83 {
84         EVP_MD_CTX *md = malloc(sizeof(*md));
85
86         if (md)
87                 EVP_MD_CTX_init(md);
88
89         return md;
90 }
91
92 static void EVP_MD_CTX_free(EVP_MD_CTX *md)
93 {
94         EVP_MD_CTX_cleanup(md);
95         free(md);
96 }
97
98 static HMAC_CTX *HMAC_CTX_new(void)
99 {
100         HMAC_CTX *md = malloc(sizeof(*md));
101
102         if (md)
103                 HMAC_CTX_init(md);
104
105         return md;
106 }
107
108 static void HMAC_CTX_free(HMAC_CTX *md)
109 {
110         HMAC_CTX_cleanup(md);
111         free(md);
112 }
113 #else
114 static void openssl_backend_init(void)
115 {
116 }
117
118 static const char *openssl_backend_version(void)
119 {
120     return OpenSSL_version(OPENSSL_VERSION);
121 }
122 #endif
123
124 int crypt_backend_init(void)
125 {
126         if (crypto_backend_initialised)
127                 return 0;
128
129         openssl_backend_init();
130
131         crypto_backend_initialised = 1;
132         return 0;
133 }
134
135 void crypt_backend_destroy(void)
136 {
137         crypto_backend_initialised = 0;
138 }
139
140 uint32_t crypt_backend_flags(void)
141 {
142         return 0;
143 }
144
145 const char *crypt_backend_version(void)
146 {
147         return openssl_backend_version();
148 }
149
150 /* HASH */
151 int crypt_hash_size(const char *name)
152 {
153         const EVP_MD *hash_id = EVP_get_digestbyname(name);
154
155         if (!hash_id)
156                 return -EINVAL;
157
158         return EVP_MD_size(hash_id);
159 }
160
161 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
162 {
163         struct crypt_hash *h;
164
165         h = malloc(sizeof(*h));
166         if (!h)
167                 return -ENOMEM;
168
169         h->md = EVP_MD_CTX_new();
170         if (!h->md) {
171                 free(h);
172                 return -ENOMEM;
173         }
174
175         h->hash_id = EVP_get_digestbyname(name);
176         if (!h->hash_id) {
177                 EVP_MD_CTX_free(h->md);
178                 free(h);
179                 return -EINVAL;
180         }
181
182         if (EVP_DigestInit_ex(h->md, h->hash_id, NULL) != 1) {
183                 EVP_MD_CTX_free(h->md);
184                 free(h);
185                 return -EINVAL;
186         }
187
188         h->hash_len = EVP_MD_size(h->hash_id);
189         *ctx = h;
190         return 0;
191 }
192
193 static int crypt_hash_restart(struct crypt_hash *ctx)
194 {
195         if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1)
196                 return -EINVAL;
197
198         return 0;
199 }
200
201 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
202 {
203         if (EVP_DigestUpdate(ctx->md, buffer, length) != 1)
204                 return -EINVAL;
205
206         return 0;
207 }
208
209 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
210 {
211         unsigned char tmp[EVP_MAX_MD_SIZE];
212         unsigned int tmp_len = 0;
213
214         if (length > (size_t)ctx->hash_len)
215                 return -EINVAL;
216
217         if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1)
218                 return -EINVAL;
219
220         memcpy(buffer, tmp, length);
221         crypt_backend_memzero(tmp, sizeof(tmp));
222
223         if (tmp_len < length)
224                 return -EINVAL;
225
226         if (crypt_hash_restart(ctx))
227                 return -EINVAL;
228
229         return 0;
230 }
231
232 void crypt_hash_destroy(struct crypt_hash *ctx)
233 {
234         EVP_MD_CTX_free(ctx->md);
235         memset(ctx, 0, sizeof(*ctx));
236         free(ctx);
237 }
238
239 /* HMAC */
240 int crypt_hmac_size(const char *name)
241 {
242         return crypt_hash_size(name);
243 }
244
245 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
246                     const void *key, size_t key_length)
247 {
248         struct crypt_hmac *h;
249
250         h = malloc(sizeof(*h));
251         if (!h)
252                 return -ENOMEM;
253
254         h->md = HMAC_CTX_new();
255         if (!h->md) {
256                 free(h);
257                 return -ENOMEM;
258         }
259
260         h->hash_id = EVP_get_digestbyname(name);
261         if (!h->hash_id) {
262                 HMAC_CTX_free(h->md);
263                 free(h);
264                 return -EINVAL;
265         }
266
267         HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL);
268
269         h->hash_len = EVP_MD_size(h->hash_id);
270         *ctx = h;
271         return 0;
272 }
273
274 static void crypt_hmac_restart(struct crypt_hmac *ctx)
275 {
276         HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL);
277 }
278
279 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
280 {
281         HMAC_Update(ctx->md, (const unsigned char *)buffer, length);
282         return 0;
283 }
284
285 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
286 {
287         unsigned char tmp[EVP_MAX_MD_SIZE];
288         unsigned int tmp_len = 0;
289
290         if (length > (size_t)ctx->hash_len)
291                 return -EINVAL;
292
293         HMAC_Final(ctx->md, tmp, &tmp_len);
294
295         memcpy(buffer, tmp, length);
296         crypt_backend_memzero(tmp, sizeof(tmp));
297
298         if (tmp_len < length)
299                 return -EINVAL;
300
301         crypt_hmac_restart(ctx);
302
303         return 0;
304 }
305
306 void crypt_hmac_destroy(struct crypt_hmac *ctx)
307 {
308         HMAC_CTX_free(ctx->md);
309         memset(ctx, 0, sizeof(*ctx));
310         free(ctx);
311 }
312
313 /* RNG */
314 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
315 {
316         if (RAND_bytes((unsigned char *)buffer, length) != 1)
317                 return -EINVAL;
318
319         return 0;
320 }
321
322 /* PBKDF */
323 int crypt_pbkdf(const char *kdf, const char *hash,
324                 const char *password, size_t password_length,
325                 const char *salt, size_t salt_length,
326                 char *key, size_t key_length,
327                 uint32_t iterations, uint32_t memory, uint32_t parallel)
328
329 {
330         const EVP_MD *hash_id;
331
332         if (!kdf)
333                 return -EINVAL;
334
335         if (!strcmp(kdf, "pbkdf2")) {
336                 hash_id = EVP_get_digestbyname(hash);
337                 if (!hash_id)
338                         return -EINVAL;
339
340                 if (!PKCS5_PBKDF2_HMAC(password, (int)password_length,
341                     (const unsigned char *)salt, (int)salt_length,
342                     (int)iterations, hash_id, (int)key_length, (unsigned char *)key))
343                         return -EINVAL;
344                 return 0;
345         } else if (!strncmp(kdf, "argon2", 6)) {
346                 return argon2(kdf, password, password_length, salt, salt_length,
347                               key, key_length, iterations, memory, parallel);
348         }
349
350         return -EINVAL;
351 }
352
353 /* Block ciphers */
354 static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec)
355 {
356         EVP_CIPHER_CTX_free(*hd_enc);
357         *hd_enc = NULL;
358
359         EVP_CIPHER_CTX_free(*hd_dec);
360         *hd_dec = NULL;
361 }
362
363 static int _cipher_init(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const char *name,
364                         const char *mode, const void *key, size_t key_length, size_t *iv_length)
365 {
366         char cipher_name[256];
367         const EVP_CIPHER *type;
368         int r, key_bits;
369
370         key_bits = key_length * 8;
371         if (!strcmp(mode, "xts"))
372                 key_bits /= 2;
373
374         r = snprintf(cipher_name, sizeof(cipher_name), "%s-%d-%s", name, key_bits, mode);
375         if (r < 0 || r >= (int)sizeof(cipher_name))
376                 return -EINVAL;
377
378         type = EVP_get_cipherbyname(cipher_name);
379         if (!type)
380                 return -ENOENT;
381
382         if (EVP_CIPHER_key_length(type) != (int)key_length)
383                 return -EINVAL;
384
385         *hd_enc = EVP_CIPHER_CTX_new();
386         *hd_dec = EVP_CIPHER_CTX_new();
387         *iv_length = EVP_CIPHER_iv_length(type);
388
389         if (!*hd_enc || !*hd_dec)
390                 return -EINVAL;
391
392         if (EVP_EncryptInit_ex(*hd_enc, type, NULL, key, NULL) != 1 ||
393             EVP_DecryptInit_ex(*hd_dec, type, NULL, key, NULL) != 1) {
394                 _cipher_destroy(hd_enc, hd_dec);
395                 return -EINVAL;
396         }
397
398         if (EVP_CIPHER_CTX_set_padding(*hd_enc, 0) != 1 ||
399             EVP_CIPHER_CTX_set_padding(*hd_dec, 0) != 1) {
400                 _cipher_destroy(hd_enc, hd_dec);
401                 return -EINVAL;
402         }
403
404         return 0;
405 }
406
407 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
408                     const char *mode, const void *key, size_t key_length)
409 {
410         struct crypt_cipher *h;
411         int r;
412
413         h = malloc(sizeof(*h));
414         if (!h)
415                 return -ENOMEM;
416
417         if (!_cipher_init(&h->u.lib.hd_enc, &h->u.lib.hd_dec, name, mode, key,
418                           key_length, &h->u.lib.iv_length)) {
419                 h->use_kernel = false;
420                 *ctx = h;
421                 return 0;
422         }
423
424         r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
425         if (r < 0) {
426                 free(h);
427                 return r;
428         }
429
430         h->use_kernel = true;
431         *ctx = h;
432         return 0;
433 }
434
435 void crypt_cipher_destroy(struct crypt_cipher *ctx)
436 {
437         if (ctx->use_kernel)
438                 crypt_cipher_destroy_kernel(&ctx->u.kernel);
439         else
440                 _cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec);
441         free(ctx);
442 }
443
444 static int _cipher_encrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
445                            int length, const unsigned char *iv, size_t iv_length)
446 {
447         int len;
448
449         if (ctx->u.lib.iv_length != iv_length)
450                 return -EINVAL;
451
452         if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1)
453                 return -EINVAL;
454
455         if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, length) != 1)
456                 return -EINVAL;
457
458         if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1)
459                 return -EINVAL;
460
461         return 0;
462 }
463
464 static int _cipher_decrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
465                            int length, const unsigned char *iv, size_t iv_length)
466 {
467         int len;
468
469         if (ctx->u.lib.iv_length != iv_length)
470                 return -EINVAL;
471
472         if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1)
473                 return -EINVAL;
474
475         if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, length) != 1)
476                 return -EINVAL;
477
478         if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1)
479                 return -EINVAL;
480
481         return 0;
482 }
483
484 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
485                          const char *in, char *out, size_t length,
486                          const char *iv, size_t iv_length)
487 {
488         if (ctx->use_kernel)
489                 return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
490
491         return _cipher_encrypt(ctx, (const unsigned char*)in,
492                                (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
493 }
494
495 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
496                          const char *in, char *out, size_t length,
497                          const char *iv, size_t iv_length)
498 {
499         if (ctx->use_kernel)
500                 return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
501
502         return _cipher_decrypt(ctx, (const unsigned char*)in,
503                                (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
504 }
505
506 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
507 {
508         return ctx->use_kernel;
509 }
510
511 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
512                             const char *in, char *out, size_t length,
513                             const char *iv, size_t iv_length,
514                             const char *tag, size_t tag_length)
515 {
516 #ifdef EVP_CTRL_CCM_SET_IVLEN
517         EVP_CIPHER_CTX *ctx;
518         int len = 0, r = -EINVAL;
519
520         ctx = EVP_CIPHER_CTX_new();
521         if (!ctx)
522                 return -EINVAL;
523
524         if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1)
525                 goto out;
526
527         //EVP_CIPHER_CTX_key_length(ctx)
528         //EVP_CIPHER_CTX_iv_length(ctx)
529
530         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1)
531                 goto out;
532         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1)
533                 goto out;
534
535         if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1)
536                 goto out;
537
538         if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1)
539                 r = 0;
540 out:
541         EVP_CIPHER_CTX_free(ctx);
542         return r;
543 #else
544         return -ENOTSUP;
545 #endif
546 }