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 #if HAVE_DECL_NSS_GETVERSION
80 snprintf(version, 64, "NSS %s", NSS_GetVersion());
82 snprintf(version, 64, "NSS");
84 crypto_backend_initialised = 1;
88 uint32_t crypt_backend_flags(void)
93 const char *crypt_backend_version(void)
95 return crypto_backend_initialised ? version : "";
99 int crypt_hash_size(const char *name)
101 struct hash_alg *ha = _get_alg(name);
103 return ha ? ha->length : -EINVAL;
106 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
108 struct crypt_hash *h;
110 h = malloc(sizeof(*h));
114 h->hash = _get_alg(name);
120 h->md = PK11_CreateDigestContext(h->hash->oid);
126 if (PK11_DigestBegin(h->md) != SECSuccess) {
127 PK11_DestroyContext(h->md, PR_TRUE);
136 static int crypt_hash_restart(struct crypt_hash *ctx)
138 if (PK11_DigestBegin(ctx->md) != SECSuccess)
144 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
146 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
152 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
154 unsigned char tmp[64];
155 unsigned int tmp_len;
157 if (length > (size_t)ctx->hash->length)
160 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
163 memcpy(buffer, tmp, length);
164 memset(tmp, 0, sizeof(tmp));
166 if (tmp_len < length)
169 if (crypt_hash_restart(ctx))
175 int crypt_hash_destroy(struct crypt_hash *ctx)
177 PK11_DestroyContext(ctx->md, PR_TRUE);
178 memset(ctx, 0, sizeof(*ctx));
184 int crypt_hmac_size(const char *name)
186 return crypt_hash_size(name);
189 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
190 const void *buffer, size_t length)
192 struct crypt_hmac *h;
196 keyItem.type = siBuffer;
197 keyItem.data = CONST_CAST(unsigned char *)buffer;
198 keyItem.len = (int)length;
200 noParams.type = siBuffer;
204 h = malloc(sizeof(*h));
207 memset(ctx, 0, sizeof(*ctx));
210 h->hash = _get_alg(name);
214 h->slot = PK11_GetInternalKeySlot();
218 h->key = PK11_ImportSymKey(h->slot, h->hash->ck_type, PK11_OriginUnwrap,
219 CKA_SIGN, &keyItem, NULL);
223 h->md = PK11_CreateContextBySymKey(h->hash->ck_type, CKA_SIGN, h->key,
228 if (PK11_DigestBegin(h->md) != SECSuccess)
234 crypt_hmac_destroy(h);
238 static int crypt_hmac_restart(struct crypt_hmac *ctx)
240 if (PK11_DigestBegin(ctx->md) != SECSuccess)
246 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
248 if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
254 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
256 unsigned char tmp[64];
257 unsigned int tmp_len;
259 if (length > (size_t)ctx->hash->length)
262 if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
265 memcpy(buffer, tmp, length);
266 memset(tmp, 0, sizeof(tmp));
268 if (tmp_len < length)
271 if (crypt_hmac_restart(ctx))
277 int crypt_hmac_destroy(struct crypt_hmac *ctx)
280 PK11_FreeSymKey(ctx->key);
282 PK11_FreeSlot(ctx->slot);
284 PK11_DestroyContext(ctx->md, PR_TRUE);
285 memset(ctx, 0, sizeof(*ctx));
291 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
296 if (PK11_GenerateRandom((unsigned char *)buffer, length) != SECSuccess)