2 * NSS crypto backend implementation
4 * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "crypto_backend.h"
26 #define CONST_CAST(x) (x)(uintptr_t)
28 static int crypto_backend_initialised = 0;
29 static char version[64];
34 CK_MECHANISM_TYPE ck_type;
38 static struct hash_alg hash_algs[] = {
39 { "sha1", SEC_OID_SHA1, CKM_SHA_1_HMAC, 20 },
40 { "sha256", SEC_OID_SHA256, CKM_SHA256_HMAC, 32 },
41 { "sha384", SEC_OID_SHA384, CKM_SHA384_HMAC, 48 },
42 { "sha512", SEC_OID_SHA512, CKM_SHA512_HMAC, 64 },
43 // { "ripemd160", SEC_OID_RIPEMD160, CKM_RIPEMD160_HMAC, 20 },
49 const struct hash_alg *hash;
56 const struct hash_alg *hash;
59 static struct hash_alg *_get_alg(const char *name)
63 while (name && hash_algs[i].name) {
64 if (!strcmp(name, hash_algs[i].name))
71 int crypt_backend_init(struct crypt_device *ctx)
73 if (crypto_backend_initialised)
76 if (NSS_NoDB_Init(".") != SECSuccess)
79 snprintf(version, 64, "NSS %s", NSS_GetVersion());
80 crypto_backend_initialised = 1;
84 uint32_t crypt_backend_flags(void)
89 const char *crypt_backend_version(void)
91 return crypto_backend_initialised ? version : "";
95 int crypt_hash_size(const char *name)
97 struct hash_alg *ha = _get_alg(name);
99 return ha ? ha->length : -EINVAL;
102 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
104 struct crypt_hash *h;
106 h = malloc(sizeof(*h));
110 h->hash = _get_alg(name);
116 h->md = PK11_CreateDigestContext(h->hash->oid);
122 if (PK11_DigestBegin(h->md) != SECSuccess) {
123 PK11_DestroyContext(h->md, PR_TRUE);
132 static int crypt_hash_restart(struct crypt_hash *ctx)
134 if (PK11_DigestBegin(ctx->md) != SECSuccess)
140 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
142 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
148 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
150 unsigned char tmp[64];
151 unsigned int tmp_len;
153 if (length > (size_t)ctx->hash->length)
156 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
159 memcpy(buffer, tmp, length);
160 memset(tmp, 0, sizeof(tmp));
162 if (tmp_len < length)
165 if (crypt_hash_restart(ctx))
171 int crypt_hash_destroy(struct crypt_hash *ctx)
173 PK11_DestroyContext(ctx->md, PR_TRUE);
174 memset(ctx, 0, sizeof(*ctx));
180 int crypt_hmac_size(const char *name)
182 return crypt_hash_size(name);
185 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
186 const void *buffer, size_t length)
188 struct crypt_hmac *h;
192 keyItem.type = siBuffer;
193 keyItem.data = CONST_CAST(unsigned char *)buffer;
194 keyItem.len = (int)length;
196 noParams.type = siBuffer;
200 h = malloc(sizeof(*h));
203 memset(ctx, 0, sizeof(*ctx));
206 h->hash = _get_alg(name);
210 h->slot = PK11_GetInternalKeySlot();
214 h->key = PK11_ImportSymKey(h->slot, h->hash->ck_type, PK11_OriginUnwrap,
215 CKA_SIGN, &keyItem, NULL);
219 h->md = PK11_CreateContextBySymKey(h->hash->ck_type, CKA_SIGN, h->key,
224 if (PK11_DigestBegin(h->md) != SECSuccess)
230 crypt_hmac_destroy(h);
234 static int crypt_hmac_restart(struct crypt_hmac *ctx)
236 if (PK11_DigestBegin(ctx->md) != SECSuccess)
242 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
244 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
250 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
252 unsigned char tmp[64];
253 unsigned int tmp_len;
255 if (length > (size_t)ctx->hash->length)
258 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
261 memcpy(buffer, tmp, length);
262 memset(tmp, 0, sizeof(tmp));
264 if (tmp_len < length)
267 if (crypt_hmac_restart(ctx))
273 int crypt_hmac_destroy(struct crypt_hmac *ctx)
276 PK11_FreeSymKey(ctx->key);
278 PK11_FreeSlot(ctx->slot);
280 PK11_DestroyContext(ctx->md, PR_TRUE);
281 memset(ctx, 0, sizeof(*ctx));
287 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
292 if (PK11_GenerateRandom((unsigned char *)buffer, length) != SECSuccess)