2 * NSS crypto backend implementation
4 * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-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.
26 #include "crypto_backend_internal.h"
28 #define CONST_CAST(x) (x)(uintptr_t)
30 static int crypto_backend_initialised = 0;
31 static char version[64];
36 CK_MECHANISM_TYPE ck_type;
38 unsigned int block_length;
41 static struct hash_alg hash_algs[] = {
42 { "sha1", SEC_OID_SHA1, CKM_SHA_1_HMAC, 20, 64 },
43 { "sha256", SEC_OID_SHA256, CKM_SHA256_HMAC, 32, 64 },
44 { "sha384", SEC_OID_SHA384, CKM_SHA384_HMAC, 48, 128 },
45 { "sha512", SEC_OID_SHA512, CKM_SHA512_HMAC, 64, 128 },
46 // { "ripemd160", SEC_OID_RIPEMD160, CKM_RIPEMD160_HMAC, 20, 64 },
52 const struct hash_alg *hash;
59 const struct hash_alg *hash;
63 struct crypt_cipher_kernel ck;
66 static struct hash_alg *_get_alg(const char *name)
70 while (name && hash_algs[i].name) {
71 if (!strcmp(name, hash_algs[i].name))
78 int crypt_backend_init(bool fips __attribute__((unused)))
82 if (crypto_backend_initialised)
85 if (NSS_NoDB_Init(".") != SECSuccess)
88 #if HAVE_DECL_NSS_GETVERSION
89 r = snprintf(version, sizeof(version), "NSS %s", NSS_GetVersion());
91 r = snprintf(version, sizeof(version), "NSS");
93 if (r < 0 || (size_t)r >= sizeof(version))
96 crypto_backend_initialised = 1;
100 void crypt_backend_destroy(void)
102 crypto_backend_initialised = 0;
105 uint32_t crypt_backend_flags(void)
110 const char *crypt_backend_version(void)
112 return crypto_backend_initialised ? version : "";
116 int crypt_hash_size(const char *name)
118 struct hash_alg *ha = _get_alg(name);
120 return ha ? ha->length : -EINVAL;
123 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
125 struct crypt_hash *h;
127 h = malloc(sizeof(*h));
131 h->hash = _get_alg(name);
137 h->md = PK11_CreateDigestContext(h->hash->oid);
143 if (PK11_DigestBegin(h->md) != SECSuccess) {
144 PK11_DestroyContext(h->md, PR_TRUE);
153 static int crypt_hash_restart(struct crypt_hash *ctx)
155 if (PK11_DigestBegin(ctx->md) != SECSuccess)
161 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
163 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
169 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
171 unsigned char tmp[64];
172 unsigned int tmp_len;
174 if (length > (size_t)ctx->hash->length)
177 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
180 memcpy(buffer, tmp, length);
181 crypt_backend_memzero(tmp, sizeof(tmp));
183 if (tmp_len < length)
186 if (crypt_hash_restart(ctx))
192 void crypt_hash_destroy(struct crypt_hash *ctx)
194 PK11_DestroyContext(ctx->md, PR_TRUE);
195 memset(ctx, 0, sizeof(*ctx));
200 int crypt_hmac_size(const char *name)
202 return crypt_hash_size(name);
205 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
206 const void *key, size_t key_length)
208 struct crypt_hmac *h;
212 keyItem.type = siBuffer;
213 keyItem.data = CONST_CAST(unsigned char *)key;
214 keyItem.len = (int)key_length;
216 noParams.type = siBuffer;
220 h = malloc(sizeof(*h));
223 memset(ctx, 0, sizeof(*ctx));
226 h->hash = _get_alg(name);
230 h->slot = PK11_GetInternalKeySlot();
234 h->key = PK11_ImportSymKey(h->slot, h->hash->ck_type, PK11_OriginUnwrap,
235 CKA_SIGN, &keyItem, NULL);
239 h->md = PK11_CreateContextBySymKey(h->hash->ck_type, CKA_SIGN, h->key,
244 if (PK11_DigestBegin(h->md) != SECSuccess)
250 crypt_hmac_destroy(h);
254 static int crypt_hmac_restart(struct crypt_hmac *ctx)
256 if (PK11_DigestBegin(ctx->md) != SECSuccess)
262 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
264 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
270 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
272 unsigned char tmp[64];
273 unsigned int tmp_len;
275 if (length > (size_t)ctx->hash->length)
278 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
281 memcpy(buffer, tmp, length);
282 crypt_backend_memzero(tmp, sizeof(tmp));
284 if (tmp_len < length)
287 if (crypt_hmac_restart(ctx))
293 void crypt_hmac_destroy(struct crypt_hmac *ctx)
296 PK11_FreeSymKey(ctx->key);
298 PK11_FreeSlot(ctx->slot);
300 PK11_DestroyContext(ctx->md, PR_TRUE);
301 memset(ctx, 0, sizeof(*ctx));
306 int crypt_backend_rng(char *buffer, size_t length, int quality __attribute__((unused)), int fips)
311 if (PK11_GenerateRandom((unsigned char *)buffer, length) != SECSuccess)
318 int crypt_pbkdf(const char *kdf, const char *hash,
319 const char *password, size_t password_length,
320 const char *salt, size_t salt_length,
321 char *key, size_t key_length,
322 uint32_t iterations, uint32_t memory, uint32_t parallel)
329 if (!strcmp(kdf, "pbkdf2")) {
334 return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
335 iterations, key_length, key, ha->block_length);
336 } else if (!strncmp(kdf, "argon2", 6)) {
337 return argon2(kdf, password, password_length, salt, salt_length,
338 key, key_length, iterations, memory, parallel);
345 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
346 const char *mode, const void *key, size_t key_length)
348 struct crypt_cipher *h;
351 h = malloc(sizeof(*h));
355 r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
365 void crypt_cipher_destroy(struct crypt_cipher *ctx)
367 crypt_cipher_destroy_kernel(&ctx->ck);
371 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
372 const char *in, char *out, size_t length,
373 const char *iv, size_t iv_length)
375 return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
378 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
379 const char *in, char *out, size_t length,
380 const char *iv, size_t iv_length)
382 return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
385 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx __attribute__((unused)))
390 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
391 const char *in, char *out, size_t length,
392 const char *iv, size_t iv_length,
393 const char *tag, size_t tag_length)
395 return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
396 iv, iv_length, tag, tag_length);
399 int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
401 return NSS_SecureMemcmp(m1, m2, n);
404 bool crypt_fips_mode(void)