2 * Nettle crypto backend implementation
4 * Copyright (C) 2011-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2011-2020 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 "crypto_backend_internal.h"
31 #if HAVE_NETTLE_VERSION_H
32 #include <nettle/version.h>
33 #define VSTR(s) STR(s)
35 static const char *version = "Nettle "VSTR(NETTLE_VERSION_MAJOR)"."VSTR(NETTLE_VERSION_MINOR);
37 static const char *version = "Nettle";
40 typedef void (*init_func) (void *);
41 typedef void (*update_func) (void *, size_t, const uint8_t *);
42 typedef void (*digest_func) (void *, size_t, uint8_t *);
43 typedef void (*set_key_func) (void *, size_t, const uint8_t *);
51 update_func hmac_update;
52 digest_func hmac_digest;
53 set_key_func hmac_set_key;
56 /* Missing HMAC wrappers in Nettle */
57 #define HMAC_FCE(xxx) \
58 struct xhmac_##xxx##_ctx HMAC_CTX(struct xxx##_ctx); \
59 static void xhmac_##xxx##_set_key(struct xhmac_##xxx##_ctx *ctx, \
60 size_t key_length, const uint8_t *key) \
61 {HMAC_SET_KEY(ctx, &nettle_##xxx, key_length, key);} \
62 static void xhmac_##xxx##_update(struct xhmac_##xxx##_ctx *ctx, \
63 size_t length, const uint8_t *data) \
64 {xxx##_update(&ctx->state, length, data);} \
65 static void xhmac_##xxx##_digest(struct xhmac_##xxx##_ctx *ctx, \
66 size_t length, uint8_t *digest) \
67 {HMAC_DIGEST(ctx, &nettle_##xxx, length, digest);}
74 static struct hash_alg hash_algs[] = {
75 { "sha1", SHA1_DIGEST_SIZE,
76 (init_func) sha1_init,
77 (update_func) sha1_update,
78 (digest_func) sha1_digest,
79 (update_func) hmac_sha1_update,
80 (digest_func) hmac_sha1_digest,
81 (set_key_func) hmac_sha1_set_key,
83 { "sha224", SHA224_DIGEST_SIZE,
84 (init_func) sha224_init,
85 (update_func) sha224_update,
86 (digest_func) sha224_digest,
87 (update_func) hmac_sha224_update,
88 (digest_func) hmac_sha224_digest,
89 (set_key_func) hmac_sha224_set_key,
91 { "sha256", SHA256_DIGEST_SIZE,
92 (init_func) sha256_init,
93 (update_func) sha256_update,
94 (digest_func) sha256_digest,
95 (update_func) hmac_sha256_update,
96 (digest_func) hmac_sha256_digest,
97 (set_key_func) hmac_sha256_set_key,
99 { "sha384", SHA384_DIGEST_SIZE,
100 (init_func) sha384_init,
101 (update_func) sha384_update,
102 (digest_func) sha384_digest,
103 (update_func) hmac_sha384_update,
104 (digest_func) hmac_sha384_digest,
105 (set_key_func) hmac_sha384_set_key,
107 { "sha512", SHA512_DIGEST_SIZE,
108 (init_func) sha512_init,
109 (update_func) sha512_update,
110 (digest_func) sha512_digest,
111 (update_func) hmac_sha512_update,
112 (digest_func) hmac_sha512_digest,
113 (set_key_func) hmac_sha512_set_key,
115 { "ripemd160", RIPEMD160_DIGEST_SIZE,
116 (init_func) ripemd160_init,
117 (update_func) ripemd160_update,
118 (digest_func) ripemd160_digest,
119 (update_func) hmac_ripemd160_update,
120 (digest_func) hmac_ripemd160_digest,
121 (set_key_func) hmac_ripemd160_set_key,
123 /* Nettle prior to version 3.2 has incompatible SHA3 implementation */
124 #if NETTLE_SHA3_FIPS202
125 { "sha3-224", SHA3_224_DIGEST_SIZE,
126 (init_func) sha3_224_init,
127 (update_func) sha3_224_update,
128 (digest_func) sha3_224_digest,
129 (update_func) xhmac_sha3_224_update,
130 (digest_func) xhmac_sha3_224_digest,
131 (set_key_func) xhmac_sha3_224_set_key,
133 { "sha3-256", SHA3_256_DIGEST_SIZE,
134 (init_func) sha3_256_init,
135 (update_func) sha3_256_update,
136 (digest_func) sha3_256_digest,
137 (update_func) xhmac_sha3_256_update,
138 (digest_func) xhmac_sha3_256_digest,
139 (set_key_func) xhmac_sha3_256_set_key,
141 { "sha3-384", SHA3_384_DIGEST_SIZE,
142 (init_func) sha3_384_init,
143 (update_func) sha3_384_update,
144 (digest_func) sha3_384_digest,
145 (update_func) xhmac_sha3_384_update,
146 (digest_func) xhmac_sha3_384_digest,
147 (set_key_func) xhmac_sha3_384_set_key,
149 { "sha3-512", SHA3_512_DIGEST_SIZE,
150 (init_func) sha3_512_init,
151 (update_func) sha3_512_update,
152 (digest_func) sha3_512_digest,
153 (update_func) xhmac_sha3_512_update,
154 (digest_func) xhmac_sha3_512_digest,
155 (set_key_func) xhmac_sha3_512_set_key,
158 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, }
162 const struct hash_alg *hash;
164 struct sha1_ctx sha1;
165 struct sha224_ctx sha224;
166 struct sha256_ctx sha256;
167 struct sha384_ctx sha384;
168 struct sha512_ctx sha512;
169 struct ripemd160_ctx ripemd160;
170 struct sha3_224_ctx sha3_224;
171 struct sha3_256_ctx sha3_256;
172 struct sha3_384_ctx sha3_384;
173 struct sha3_512_ctx sha3_512;
178 const struct hash_alg *hash;
180 struct hmac_sha1_ctx sha1;
181 struct hmac_sha224_ctx sha224;
182 struct hmac_sha256_ctx sha256;
183 struct hmac_sha384_ctx sha384;
184 struct hmac_sha512_ctx sha512;
185 struct hmac_ripemd160_ctx ripemd160;
186 struct xhmac_sha3_224_ctx sha3_224;
187 struct xhmac_sha3_256_ctx sha3_256;
188 struct xhmac_sha3_384_ctx sha3_384;
189 struct xhmac_sha3_512_ctx sha3_512;
195 struct crypt_cipher {
196 struct crypt_cipher_kernel ck;
199 uint32_t crypt_backend_flags(void)
204 static struct hash_alg *_get_alg(const char *name)
208 while (name && hash_algs[i].name) {
209 if (!strcmp(name, hash_algs[i].name))
210 return &hash_algs[i];
216 int crypt_backend_init(void)
221 void crypt_backend_destroy(void)
226 const char *crypt_backend_version(void)
232 int crypt_hash_size(const char *name)
234 struct hash_alg *ha = _get_alg(name);
236 return ha ? ha->length : -EINVAL;
239 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
241 struct crypt_hash *h;
243 h = malloc(sizeof(*h));
247 h->hash = _get_alg(name);
253 h->hash->init(&h->nettle_ctx);
259 static void crypt_hash_restart(struct crypt_hash *ctx)
261 ctx->hash->init(&ctx->nettle_ctx);
264 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
266 ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer);
270 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
272 if (length > (size_t)ctx->hash->length)
275 ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
276 crypt_hash_restart(ctx);
280 void crypt_hash_destroy(struct crypt_hash *ctx)
282 memset(ctx, 0, sizeof(*ctx));
287 int crypt_hmac_size(const char *name)
289 return crypt_hash_size(name);
292 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
293 const void *key, size_t key_length)
295 struct crypt_hmac *h;
297 h = malloc(sizeof(*h));
300 memset(ctx, 0, sizeof(*ctx));
303 h->hash = _get_alg(name);
307 h->key = malloc(key_length);
311 memcpy(h->key, key, key_length);
312 h->key_length = key_length;
314 h->hash->init(&h->nettle_ctx);
315 h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key);
324 static void crypt_hmac_restart(struct crypt_hmac *ctx)
326 ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key);
329 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
331 ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer);
335 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
337 if (length > (size_t)ctx->hash->length)
340 ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
341 crypt_hmac_restart(ctx);
345 void crypt_hmac_destroy(struct crypt_hmac *ctx)
347 memset(ctx->key, 0, ctx->key_length);
349 memset(ctx, 0, sizeof(*ctx));
354 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
360 int crypt_pbkdf(const char *kdf, const char *hash,
361 const char *password, size_t password_length,
362 const char *salt, size_t salt_length,
363 char *key, size_t key_length,
364 uint32_t iterations, uint32_t memory, uint32_t parallel)
366 struct crypt_hmac *h;
372 if (!strcmp(kdf, "pbkdf2")) {
373 r = crypt_hmac_init(&h, hash, password, password_length);
377 nettle_pbkdf2(&h->nettle_ctx, h->hash->hmac_update,
378 h->hash->hmac_digest, h->hash->length, iterations,
379 salt_length, (const uint8_t *)salt, key_length,
381 crypt_hmac_destroy(h);
383 } else if (!strncmp(kdf, "argon2", 6)) {
384 return argon2(kdf, password, password_length, salt, salt_length,
385 key, key_length, iterations, memory, parallel);
392 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
393 const char *mode, const void *key, size_t key_length)
395 struct crypt_cipher *h;
398 h = malloc(sizeof(*h));
402 r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
412 void crypt_cipher_destroy(struct crypt_cipher *ctx)
414 crypt_cipher_destroy_kernel(&ctx->ck);
418 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
419 const char *in, char *out, size_t length,
420 const char *iv, size_t iv_length)
422 return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
425 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
426 const char *in, char *out, size_t length,
427 const char *iv, size_t iv_length)
429 return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
432 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
437 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
438 const char *in, char *out, size_t length,
439 const char *iv, size_t iv_length,
440 const char *tag, size_t tag_length)
442 return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
443 iv, iv_length, tag, tag_length);