2 * Nettle crypto backend implementation
4 * Copyright (C) 2011-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2011-2023 Milan Broz
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.
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.
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.
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"
32 #if HAVE_NETTLE_VERSION_H
33 #include <nettle/version.h>
34 #define VSTR(s) STR(s)
36 static const char *version = "Nettle "VSTR(NETTLE_VERSION_MAJOR)"."VSTR(NETTLE_VERSION_MINOR);
38 static const char *version = "Nettle";
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 *);
52 update_func hmac_update;
53 digest_func hmac_digest;
54 set_key_func hmac_set_key;
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);}
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
159 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, }
163 const struct hash_alg *hash;
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;
179 const struct hash_alg *hash;
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;
196 struct crypt_cipher {
197 struct crypt_cipher_kernel ck;
200 uint32_t crypt_backend_flags(void)
205 static struct hash_alg *_get_alg(const char *name)
209 while (name && hash_algs[i].name) {
210 if (!strcmp(name, hash_algs[i].name))
211 return &hash_algs[i];
217 int crypt_backend_init(bool fips __attribute__((unused)))
222 void crypt_backend_destroy(void)
227 const char *crypt_backend_version(void)
233 int crypt_hash_size(const char *name)
235 struct hash_alg *ha = _get_alg(name);
237 return ha ? ha->length : -EINVAL;
240 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
242 struct crypt_hash *h;
244 h = malloc(sizeof(*h));
248 h->hash = _get_alg(name);
254 h->hash->init(&h->nettle_ctx);
260 static void crypt_hash_restart(struct crypt_hash *ctx)
262 ctx->hash->init(&ctx->nettle_ctx);
265 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
267 ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer);
271 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
273 if (length > (size_t)ctx->hash->length)
276 ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
277 crypt_hash_restart(ctx);
281 void crypt_hash_destroy(struct crypt_hash *ctx)
283 memset(ctx, 0, sizeof(*ctx));
288 int crypt_hmac_size(const char *name)
290 return crypt_hash_size(name);
293 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
294 const void *key, size_t key_length)
296 struct crypt_hmac *h;
298 h = malloc(sizeof(*h));
301 memset(ctx, 0, sizeof(*ctx));
304 h->hash = _get_alg(name);
310 h->key = malloc(key_length);
316 memcpy(h->key, key, key_length);
317 h->key_length = key_length;
319 h->hash->init(&h->nettle_ctx);
320 h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key);
326 static void crypt_hmac_restart(struct crypt_hmac *ctx)
328 ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key);
331 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
333 ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer);
337 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
339 if (length > (size_t)ctx->hash->length)
342 ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
343 crypt_hmac_restart(ctx);
347 void crypt_hmac_destroy(struct crypt_hmac *ctx)
349 memset(ctx->key, 0, ctx->key_length);
351 memset(ctx, 0, sizeof(*ctx));
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)))
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)
371 struct crypt_hmac *h;
377 if (!strcmp(kdf, "pbkdf2")) {
378 r = crypt_hmac_init(&h, hash, password, password_length);
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,
386 crypt_hmac_destroy(h);
388 } else if (!strncmp(kdf, "argon2", 6)) {
389 return argon2(kdf, password, password_length, salt, salt_length,
390 key, key_length, iterations, memory, parallel);
397 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
398 const char *mode, const void *key, size_t key_length)
400 struct crypt_cipher *h;
403 h = malloc(sizeof(*h));
407 r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
417 void crypt_cipher_destroy(struct crypt_cipher *ctx)
419 crypt_cipher_destroy_kernel(&ctx->ck);
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)
427 return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
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)
434 return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
437 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx __attribute__((unused)))
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)
447 return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
448 iv, iv_length, tag, tag_length);
451 int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
453 /* The logic is inverse to memcmp... */
454 return !memeql_sec(m1, m2, n);
457 bool crypt_fips_mode(void)