2 * NSS crypto backend implementation
4 * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-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.
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(void)
80 if (crypto_backend_initialised)
83 if (NSS_NoDB_Init(".") != SECSuccess)
86 #if HAVE_DECL_NSS_GETVERSION
87 snprintf(version, 64, "NSS %s", NSS_GetVersion());
89 snprintf(version, 64, "NSS");
91 crypto_backend_initialised = 1;
95 void crypt_backend_destroy(void)
97 crypto_backend_initialised = 0;
100 uint32_t crypt_backend_flags(void)
105 const char *crypt_backend_version(void)
107 return crypto_backend_initialised ? version : "";
111 int crypt_hash_size(const char *name)
113 struct hash_alg *ha = _get_alg(name);
115 return ha ? ha->length : -EINVAL;
118 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
120 struct crypt_hash *h;
122 h = malloc(sizeof(*h));
126 h->hash = _get_alg(name);
132 h->md = PK11_CreateDigestContext(h->hash->oid);
138 if (PK11_DigestBegin(h->md) != SECSuccess) {
139 PK11_DestroyContext(h->md, PR_TRUE);
148 static int crypt_hash_restart(struct crypt_hash *ctx)
150 if (PK11_DigestBegin(ctx->md) != SECSuccess)
156 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
158 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
164 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
166 unsigned char tmp[64];
167 unsigned int tmp_len;
169 if (length > (size_t)ctx->hash->length)
172 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
175 memcpy(buffer, tmp, length);
176 crypt_backend_memzero(tmp, sizeof(tmp));
178 if (tmp_len < length)
181 if (crypt_hash_restart(ctx))
187 void crypt_hash_destroy(struct crypt_hash *ctx)
189 PK11_DestroyContext(ctx->md, PR_TRUE);
190 memset(ctx, 0, sizeof(*ctx));
195 int crypt_hmac_size(const char *name)
197 return crypt_hash_size(name);
200 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
201 const void *key, size_t key_length)
203 struct crypt_hmac *h;
207 keyItem.type = siBuffer;
208 keyItem.data = CONST_CAST(unsigned char *)key;
209 keyItem.len = (int)key_length;
211 noParams.type = siBuffer;
215 h = malloc(sizeof(*h));
218 memset(ctx, 0, sizeof(*ctx));
221 h->hash = _get_alg(name);
225 h->slot = PK11_GetInternalKeySlot();
229 h->key = PK11_ImportSymKey(h->slot, h->hash->ck_type, PK11_OriginUnwrap,
230 CKA_SIGN, &keyItem, NULL);
234 h->md = PK11_CreateContextBySymKey(h->hash->ck_type, CKA_SIGN, h->key,
239 if (PK11_DigestBegin(h->md) != SECSuccess)
245 crypt_hmac_destroy(h);
249 static int crypt_hmac_restart(struct crypt_hmac *ctx)
251 if (PK11_DigestBegin(ctx->md) != SECSuccess)
257 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
259 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
265 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
267 unsigned char tmp[64];
268 unsigned int tmp_len;
270 if (length > (size_t)ctx->hash->length)
273 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
276 memcpy(buffer, tmp, length);
277 crypt_backend_memzero(tmp, sizeof(tmp));
279 if (tmp_len < length)
282 if (crypt_hmac_restart(ctx))
288 void crypt_hmac_destroy(struct crypt_hmac *ctx)
291 PK11_FreeSymKey(ctx->key);
293 PK11_FreeSlot(ctx->slot);
295 PK11_DestroyContext(ctx->md, PR_TRUE);
296 memset(ctx, 0, sizeof(*ctx));
301 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
306 if (PK11_GenerateRandom((unsigned char *)buffer, length) != SECSuccess)
313 int crypt_pbkdf(const char *kdf, const char *hash,
314 const char *password, size_t password_length,
315 const char *salt, size_t salt_length,
316 char *key, size_t key_length,
317 uint32_t iterations, uint32_t memory, uint32_t parallel)
324 if (!strcmp(kdf, "pbkdf2")) {
329 return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
330 iterations, key_length, key, ha->block_length);
331 } else if (!strncmp(kdf, "argon2", 6)) {
332 return argon2(kdf, password, password_length, salt, salt_length,
333 key, key_length, iterations, memory, parallel);
340 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
341 const char *mode, const void *key, size_t key_length)
343 struct crypt_cipher *h;
346 h = malloc(sizeof(*h));
350 r = crypt_cipher_init_kernel(&h->ck, name, mode, key, key_length);
360 void crypt_cipher_destroy(struct crypt_cipher *ctx)
362 crypt_cipher_destroy_kernel(&ctx->ck);
366 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
367 const char *in, char *out, size_t length,
368 const char *iv, size_t iv_length)
370 return crypt_cipher_encrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
373 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
374 const char *in, char *out, size_t length,
375 const char *iv, size_t iv_length)
377 return crypt_cipher_decrypt_kernel(&ctx->ck, in, out, length, iv, iv_length);
380 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
385 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
386 const char *in, char *out, size_t length,
387 const char *iv, size_t iv_length,
388 const char *tag, size_t tag_length)
390 return crypt_bitlk_decrypt_key_kernel(key, key_length, in, out, length,
391 iv, iv_length, tag, tag_length);