Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_nettle.c
1 /*
2  * Nettle crypto backend implementation
3  *
4  * Copyright (C) 2011-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2011-2023 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
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <nettle/sha.h>
26 #include <nettle/sha3.h>
27 #include <nettle/hmac.h>
28 #include <nettle/pbkdf2.h>
29 #include <nettle/memops.h>
30 #include "crypto_backend_internal.h"
31
32 #if HAVE_NETTLE_VERSION_H
33 #include <nettle/version.h>
34 #define VSTR(s) STR(s)
35 #define STR(s) #s
36 static const char *version = "Nettle "VSTR(NETTLE_VERSION_MAJOR)"."VSTR(NETTLE_VERSION_MINOR);
37 #else
38 static const char *version = "Nettle";
39 #endif
40
41 typedef void (*init_func) (void *);
42 typedef void (*update_func) (void *, size_t, const uint8_t *);
43 typedef void (*digest_func) (void *, size_t, uint8_t *);
44 typedef void (*set_key_func) (void *, size_t, const uint8_t *);
45
46 struct hash_alg {
47         const char *name;
48         int length;
49         init_func init;
50         update_func update;
51         digest_func digest;
52         update_func hmac_update;
53         digest_func hmac_digest;
54         set_key_func hmac_set_key;
55 };
56
57 /* Missing HMAC wrappers in Nettle */
58 #define HMAC_FCE(xxx) \
59 struct xhmac_##xxx##_ctx HMAC_CTX(struct xxx##_ctx); \
60 static void xhmac_##xxx##_set_key(struct xhmac_##xxx##_ctx *ctx, \
61 size_t key_length, const uint8_t *key) \
62 {HMAC_SET_KEY(ctx, &nettle_##xxx, key_length, key);} \
63 static void xhmac_##xxx##_update(struct xhmac_##xxx##_ctx *ctx, \
64 size_t length, const uint8_t *data) \
65 {xxx##_update(&ctx->state, length, data);} \
66 static void xhmac_##xxx##_digest(struct xhmac_##xxx##_ctx *ctx, \
67 size_t length, uint8_t *digest) \
68 {HMAC_DIGEST(ctx, &nettle_##xxx, length, digest);}
69
70 HMAC_FCE(sha3_224);
71 HMAC_FCE(sha3_256);
72 HMAC_FCE(sha3_384);
73 HMAC_FCE(sha3_512);
74
75 static struct hash_alg hash_algs[] = {
76         { "sha1", SHA1_DIGEST_SIZE,
77                 (init_func) sha1_init,
78                 (update_func) sha1_update,
79                 (digest_func) sha1_digest,
80                 (update_func) hmac_sha1_update,
81                 (digest_func) hmac_sha1_digest,
82                 (set_key_func) hmac_sha1_set_key,
83         },
84         { "sha224", SHA224_DIGEST_SIZE,
85                 (init_func) sha224_init,
86                 (update_func) sha224_update,
87                 (digest_func) sha224_digest,
88                 (update_func) hmac_sha224_update,
89                 (digest_func) hmac_sha224_digest,
90                 (set_key_func) hmac_sha224_set_key,
91         },
92         { "sha256", SHA256_DIGEST_SIZE,
93                 (init_func) sha256_init,
94                 (update_func) sha256_update,
95                 (digest_func) sha256_digest,
96                 (update_func) hmac_sha256_update,
97                 (digest_func) hmac_sha256_digest,
98                 (set_key_func) hmac_sha256_set_key,
99         },
100         { "sha384", SHA384_DIGEST_SIZE,
101                 (init_func) sha384_init,
102                 (update_func) sha384_update,
103                 (digest_func) sha384_digest,
104                 (update_func) hmac_sha384_update,
105                 (digest_func) hmac_sha384_digest,
106                 (set_key_func) hmac_sha384_set_key,
107         },
108         { "sha512", SHA512_DIGEST_SIZE,
109                 (init_func) sha512_init,
110                 (update_func) sha512_update,
111                 (digest_func) sha512_digest,
112                 (update_func) hmac_sha512_update,
113                 (digest_func) hmac_sha512_digest,
114                 (set_key_func) hmac_sha512_set_key,
115         },
116         { "ripemd160", RIPEMD160_DIGEST_SIZE,
117                 (init_func) ripemd160_init,
118                 (update_func) ripemd160_update,
119                 (digest_func) ripemd160_digest,
120                 (update_func) hmac_ripemd160_update,
121                 (digest_func) hmac_ripemd160_digest,
122                 (set_key_func) hmac_ripemd160_set_key,
123         },
124 /* Nettle prior to version 3.2 has incompatible SHA3 implementation */
125 #if NETTLE_SHA3_FIPS202
126         { "sha3-224", SHA3_224_DIGEST_SIZE,
127                 (init_func) sha3_224_init,
128                 (update_func) sha3_224_update,
129                 (digest_func) sha3_224_digest,
130                 (update_func) xhmac_sha3_224_update,
131                 (digest_func) xhmac_sha3_224_digest,
132                 (set_key_func) xhmac_sha3_224_set_key,
133         },
134         { "sha3-256", SHA3_256_DIGEST_SIZE,
135                 (init_func) sha3_256_init,
136                 (update_func) sha3_256_update,
137                 (digest_func) sha3_256_digest,
138                 (update_func) xhmac_sha3_256_update,
139                 (digest_func) xhmac_sha3_256_digest,
140                 (set_key_func) xhmac_sha3_256_set_key,
141         },
142         { "sha3-384", SHA3_384_DIGEST_SIZE,
143                 (init_func) sha3_384_init,
144                 (update_func) sha3_384_update,
145                 (digest_func) sha3_384_digest,
146                 (update_func) xhmac_sha3_384_update,
147                 (digest_func) xhmac_sha3_384_digest,
148                 (set_key_func) xhmac_sha3_384_set_key,
149         },
150         { "sha3-512", SHA3_512_DIGEST_SIZE,
151                 (init_func) sha3_512_init,
152                 (update_func) sha3_512_update,
153                 (digest_func) sha3_512_digest,
154                 (update_func) xhmac_sha3_512_update,
155                 (digest_func) xhmac_sha3_512_digest,
156                 (set_key_func) xhmac_sha3_512_set_key,
157         },
158 #endif
159         { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, }
160 };
161
162 struct crypt_hash {
163         const struct hash_alg *hash;
164         union {
165                 struct sha1_ctx sha1;
166                 struct sha224_ctx sha224;
167                 struct sha256_ctx sha256;
168                 struct sha384_ctx sha384;
169                 struct sha512_ctx sha512;
170                 struct ripemd160_ctx ripemd160;
171                 struct sha3_224_ctx sha3_224;
172                 struct sha3_256_ctx sha3_256;
173                 struct sha3_384_ctx sha3_384;
174                 struct sha3_512_ctx sha3_512;
175         } nettle_ctx;
176 };
177
178 struct crypt_hmac {
179         const struct hash_alg *hash;
180         union {
181                 struct hmac_sha1_ctx sha1;
182                 struct hmac_sha224_ctx sha224;
183                 struct hmac_sha256_ctx sha256;
184                 struct hmac_sha384_ctx sha384;
185                 struct hmac_sha512_ctx sha512;
186                 struct hmac_ripemd160_ctx ripemd160;
187                 struct xhmac_sha3_224_ctx sha3_224;
188                 struct xhmac_sha3_256_ctx sha3_256;
189                 struct xhmac_sha3_384_ctx sha3_384;
190                 struct xhmac_sha3_512_ctx sha3_512;
191         } nettle_ctx;
192         size_t key_length;
193         uint8_t *key;
194 };
195
196 struct crypt_cipher {
197         struct crypt_cipher_kernel ck;
198 };
199
200 uint32_t crypt_backend_flags(void)
201 {
202         return 0;
203 }
204
205 static struct hash_alg *_get_alg(const char *name)
206 {
207         int i = 0;
208
209         while (name && hash_algs[i].name) {
210                 if (!strcmp(name, hash_algs[i].name))
211                         return &hash_algs[i];
212                 i++;
213         }
214         return NULL;
215 }
216
217 int crypt_backend_init(bool fips __attribute__((unused)))
218 {
219         return 0;
220 }
221
222 void crypt_backend_destroy(void)
223 {
224         return;
225 }
226
227 const char *crypt_backend_version(void)
228 {
229         return version;
230 }
231
232 /* HASH */
233 int crypt_hash_size(const char *name)
234 {
235         struct hash_alg *ha = _get_alg(name);
236
237         return ha ? ha->length : -EINVAL;
238 }
239
240 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
241 {
242         struct crypt_hash *h;
243
244         h = malloc(sizeof(*h));
245         if (!h)
246                 return -ENOMEM;
247
248         h->hash = _get_alg(name);
249         if (!h->hash) {
250                 free(h);
251                 return -EINVAL;
252         }
253
254         h->hash->init(&h->nettle_ctx);
255
256         *ctx = h;
257         return 0;
258 }
259
260 static void crypt_hash_restart(struct crypt_hash *ctx)
261 {
262         ctx->hash->init(&ctx->nettle_ctx);
263 }
264
265 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
266 {
267         ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer);
268         return 0;
269 }
270
271 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
272 {
273         if (length > (size_t)ctx->hash->length)
274                 return -EINVAL;
275
276         ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
277         crypt_hash_restart(ctx);
278         return 0;
279 }
280
281 void crypt_hash_destroy(struct crypt_hash *ctx)
282 {
283         memset(ctx, 0, sizeof(*ctx));
284         free(ctx);
285 }
286
287 /* HMAC */
288 int crypt_hmac_size(const char *name)
289 {
290         return crypt_hash_size(name);
291 }
292
293 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
294                     const void *key, size_t key_length)
295 {
296         struct crypt_hmac *h;
297
298         h = malloc(sizeof(*h));
299         if (!h)
300                 return -ENOMEM;
301         memset(ctx, 0, sizeof(*ctx));
302
303
304         h->hash = _get_alg(name);
305         if (!h->hash) {
306                 free(h);
307                 return -EINVAL;
308         }
309
310         h->key = malloc(key_length);
311         if (!h->key) {
312                 free(h);
313                 return -ENOMEM;
314         }
315
316         memcpy(h->key, key, key_length);
317         h->key_length = key_length;
318
319         h->hash->init(&h->nettle_ctx);
320         h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key);
321
322         *ctx = h;
323         return 0;
324 }
325
326 static void crypt_hmac_restart(struct crypt_hmac *ctx)
327 {
328         ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key);
329 }
330
331 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
332 {
333         ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer);
334         return 0;
335 }
336
337 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
338 {
339         if (length > (size_t)ctx->hash->length)
340                 return -EINVAL;
341
342         ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
343         crypt_hmac_restart(ctx);
344         return 0;
345 }
346
347 void crypt_hmac_destroy(struct crypt_hmac *ctx)
348 {
349         memset(ctx->key, 0, ctx->key_length);
350         free(ctx->key);
351         memset(ctx, 0, sizeof(*ctx));
352         free(ctx);
353 }
354
355 /* RNG - N/A */
356 int crypt_backend_rng(char *buffer __attribute__((unused)),
357                       size_t length __attribute__((unused)),
358                       int quality __attribute__((unused)),
359                       int fips __attribute__((unused)))
360 {
361         return -EINVAL;
362 }
363
364 /* PBKDF */
365 int crypt_pbkdf(const char *kdf, const char *hash,
366                 const char *password, size_t password_length,
367                 const char *salt, size_t salt_length,
368                 char *key, size_t key_length,
369                 uint32_t iterations, uint32_t memory, uint32_t parallel)
370 {
371         struct crypt_hmac *h;
372         int r;
373
374         if (!kdf)
375                 return -EINVAL;
376
377         if (!strcmp(kdf, "pbkdf2")) {
378                 r = crypt_hmac_init(&h, hash, password, password_length);
379                 if (r < 0)
380                         return r;
381
382                 nettle_pbkdf2(&h->nettle_ctx, h->hash->hmac_update,
383                               h->hash->hmac_digest, h->hash->length, iterations,
384                               salt_length, (const uint8_t *)salt, key_length,
385                               (uint8_t *)key);
386                 crypt_hmac_destroy(h);
387                 return 0;
388         } else if (!strncmp(kdf, "argon2", 6)) {
389                 return argon2(kdf, password, password_length, salt, salt_length,
390                               key, key_length, iterations, memory, parallel);
391         }
392
393         return -EINVAL;
394 }
395
396 /* Block ciphers */
397 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
398                     const char *mode, const void *key, size_t key_length)
399 {
400         struct crypt_cipher *h;
401         int r;
402
403         h = malloc(sizeof(*h));
404         if (!h)
405                 return -ENOMEM;
406
407         r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
408         if (r < 0) {
409                 free(h);
410                 return r;
411         }
412
413         *ctx = h;
414         return 0;
415 }
416
417 void crypt_cipher_destroy(struct crypt_cipher *ctx)
418 {
419         crypt_cipher_destroy_kernel(&ctx->ck);
420         free(ctx);
421 }
422
423 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
424                          const char *in, char *out, size_t length,
425                          const char *iv, size_t iv_length)
426 {
427         return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
428 }
429
430 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
431                          const char *in, char *out, size_t length,
432                          const char *iv, size_t iv_length)
433 {
434         return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
435 }
436
437 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx __attribute__((unused)))
438 {
439         return true;
440 }
441
442 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
443                             const char *in, char *out, size_t length,
444                             const char *iv, size_t iv_length,
445                             const char *tag, size_t tag_length)
446 {
447         return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
448                                               iv, iv_length, tag, tag_length);
449 }
450
451 int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
452 {
453         /* The logic is inverse to memcmp... */
454         return !memeql_sec(m1, m2, n);
455 }
456
457 bool crypt_fips_mode(void)
458 {
459         return false;
460 }