2 * NSS crypto backend implementation
4 * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2014, 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.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;
62 static struct hash_alg *_get_alg(const char *name)
66 while (name && hash_algs[i].name) {
67 if (!strcmp(name, hash_algs[i].name))
74 int crypt_backend_init(struct crypt_device *ctx)
76 if (crypto_backend_initialised)
79 if (NSS_NoDB_Init(".") != SECSuccess)
82 #if HAVE_DECL_NSS_GETVERSION
83 snprintf(version, 64, "NSS %s", NSS_GetVersion());
85 snprintf(version, 64, "NSS");
87 crypto_backend_initialised = 1;
91 uint32_t crypt_backend_flags(void)
96 const char *crypt_backend_version(void)
98 return crypto_backend_initialised ? version : "";
102 int crypt_hash_size(const char *name)
104 struct hash_alg *ha = _get_alg(name);
106 return ha ? ha->length : -EINVAL;
109 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
111 struct crypt_hash *h;
113 h = malloc(sizeof(*h));
117 h->hash = _get_alg(name);
123 h->md = PK11_CreateDigestContext(h->hash->oid);
129 if (PK11_DigestBegin(h->md) != SECSuccess) {
130 PK11_DestroyContext(h->md, PR_TRUE);
139 static int crypt_hash_restart(struct crypt_hash *ctx)
141 if (PK11_DigestBegin(ctx->md) != SECSuccess)
147 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
149 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
155 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
157 unsigned char tmp[64];
158 unsigned int tmp_len;
160 if (length > (size_t)ctx->hash->length)
163 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
166 memcpy(buffer, tmp, length);
167 crypt_backend_memzero(tmp, sizeof(tmp));
169 if (tmp_len < length)
172 if (crypt_hash_restart(ctx))
178 int crypt_hash_destroy(struct crypt_hash *ctx)
180 PK11_DestroyContext(ctx->md, PR_TRUE);
181 memset(ctx, 0, sizeof(*ctx));
187 int crypt_hmac_size(const char *name)
189 return crypt_hash_size(name);
192 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
193 const void *buffer, size_t length)
195 struct crypt_hmac *h;
199 keyItem.type = siBuffer;
200 keyItem.data = CONST_CAST(unsigned char *)buffer;
201 keyItem.len = (int)length;
203 noParams.type = siBuffer;
207 h = malloc(sizeof(*h));
210 memset(ctx, 0, sizeof(*ctx));
213 h->hash = _get_alg(name);
217 h->slot = PK11_GetInternalKeySlot();
221 h->key = PK11_ImportSymKey(h->slot, h->hash->ck_type, PK11_OriginUnwrap,
222 CKA_SIGN, &keyItem, NULL);
226 h->md = PK11_CreateContextBySymKey(h->hash->ck_type, CKA_SIGN, h->key,
231 if (PK11_DigestBegin(h->md) != SECSuccess)
237 crypt_hmac_destroy(h);
241 static int crypt_hmac_restart(struct crypt_hmac *ctx)
243 if (PK11_DigestBegin(ctx->md) != SECSuccess)
249 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
251 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
257 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
259 unsigned char tmp[64];
260 unsigned int tmp_len;
262 if (length > (size_t)ctx->hash->length)
265 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
268 memcpy(buffer, tmp, length);
269 crypt_backend_memzero(tmp, sizeof(tmp));
271 if (tmp_len < length)
274 if (crypt_hmac_restart(ctx))
280 int crypt_hmac_destroy(struct crypt_hmac *ctx)
283 PK11_FreeSymKey(ctx->key);
285 PK11_FreeSlot(ctx->slot);
287 PK11_DestroyContext(ctx->md, PR_TRUE);
288 memset(ctx, 0, sizeof(*ctx));
294 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
299 if (PK11_GenerateRandom((unsigned char *)buffer, length) != SECSuccess)
306 int crypt_pbkdf(const char *kdf, const char *hash,
307 const char *password, size_t password_length,
308 const char *salt, size_t salt_length,
309 char *key, size_t key_length,
310 unsigned int iterations)
312 struct hash_alg *ha = _get_alg(hash);
314 if (!ha || !kdf || strncmp(kdf, "pbkdf2", 6))
317 return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
318 iterations, key_length, key, ha->block_length);