Merge branch 'master' of https://code.google.com/p/cryptsetup
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_nss.c
1 /*
2  * NSS crypto backend implementation
3  *
4  * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
5  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <string.h>
21 #include <errno.h>
22 #include <nss.h>
23 #include <pk11pub.h>
24 #include "crypto_backend.h"
25
26 #define CONST_CAST(x) (x)(uintptr_t)
27
28 static int crypto_backend_initialised = 0;
29 static char version[64];
30
31 struct hash_alg {
32         const char *name;
33         SECOidTag oid;
34         CK_MECHANISM_TYPE ck_type;
35         int length;
36 };
37
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 },
44         { NULL, 0, 0, 0 }
45 };
46
47 struct crypt_hash {
48         PK11Context *md;
49         const struct hash_alg *hash;
50 };
51
52 struct crypt_hmac {
53         PK11Context *md;
54         PK11SymKey *key;
55         PK11SlotInfo *slot;
56         const struct hash_alg *hash;
57 };
58
59 static struct hash_alg *_get_alg(const char *name)
60 {
61         int i = 0;
62
63         while (name && hash_algs[i].name) {
64                 if (!strcmp(name, hash_algs[i].name))
65                         return &hash_algs[i];
66                 i++;
67         }
68         return NULL;
69 }
70
71 int crypt_backend_init(struct crypt_device *ctx)
72 {
73         if (crypto_backend_initialised)
74                 return 0;
75
76         if (NSS_NoDB_Init(".") != SECSuccess)
77                 return -EINVAL;
78
79 #if HAVE_DECL_NSS_GETVERSION
80         snprintf(version, 64, "NSS %s", NSS_GetVersion());
81 #else
82         snprintf(version, 64, "NSS");
83 #endif
84         crypto_backend_initialised = 1;
85         return 0;
86 }
87
88 uint32_t crypt_backend_flags(void)
89 {
90         return 0;
91 }
92
93 const char *crypt_backend_version(void)
94 {
95         return crypto_backend_initialised ? version : "";
96 }
97
98 /* HASH */
99 int crypt_hash_size(const char *name)
100 {
101         struct hash_alg *ha = _get_alg(name);
102
103         return ha ? ha->length : -EINVAL;
104 }
105
106 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
107 {
108         struct crypt_hash *h;
109
110         h = malloc(sizeof(*h));
111         if (!h)
112                 return -ENOMEM;
113
114         h->hash = _get_alg(name);
115         if (!h->hash) {
116                 free(h);
117                 return -EINVAL;
118         }
119
120         h->md = PK11_CreateDigestContext(h->hash->oid);
121         if (!h->md) {
122                 free(h);
123                 return -EINVAL;
124         }
125
126         if (PK11_DigestBegin(h->md) != SECSuccess) {
127                 PK11_DestroyContext(h->md, PR_TRUE);
128                 free(h);
129                 return -EINVAL;
130         }
131
132         *ctx = h;
133         return 0;
134 }
135
136 static int crypt_hash_restart(struct crypt_hash *ctx)
137 {
138         if (PK11_DigestBegin(ctx->md) != SECSuccess)
139                 return -EINVAL;
140
141         return 0;
142 }
143
144 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
145 {
146         if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
147                 return -EINVAL;
148
149         return 0;
150 }
151
152 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
153 {
154         unsigned char tmp[64];
155         unsigned int tmp_len;
156
157         if (length > (size_t)ctx->hash->length)
158                 return -EINVAL;
159
160         if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
161                 return -EINVAL;
162
163         memcpy(buffer, tmp, length);
164         memset(tmp, 0, sizeof(tmp));
165
166         if (tmp_len < length)
167                 return -EINVAL;
168
169         if (crypt_hash_restart(ctx))
170                 return -EINVAL;
171
172         return 0;
173 }
174
175 int crypt_hash_destroy(struct crypt_hash *ctx)
176 {
177         PK11_DestroyContext(ctx->md, PR_TRUE);
178         memset(ctx, 0, sizeof(*ctx));
179         free(ctx);
180         return 0;
181 }
182
183 /* HMAC */
184 int crypt_hmac_size(const char *name)
185 {
186         return crypt_hash_size(name);
187 }
188
189 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
190                     const void *buffer, size_t length)
191 {
192         struct crypt_hmac *h;
193         SECItem keyItem;
194         SECItem noParams;
195
196         keyItem.type = siBuffer;
197         keyItem.data = CONST_CAST(unsigned char *)buffer;
198         keyItem.len = (int)length;
199
200         noParams.type = siBuffer;
201         noParams.data = 0;
202         noParams.len = 0;
203
204         h = malloc(sizeof(*h));
205         if (!h)
206                 return -ENOMEM;
207         memset(ctx, 0, sizeof(*ctx));
208
209
210         h->hash = _get_alg(name);
211         if (!h->hash)
212                 goto bad;
213
214         h->slot = PK11_GetInternalKeySlot();
215         if (!h->slot)
216                 goto bad;
217
218         h->key = PK11_ImportSymKey(h->slot, h->hash->ck_type, PK11_OriginUnwrap,
219                                    CKA_SIGN,  &keyItem, NULL);
220         if (!h->key)
221                 goto bad;
222
223         h->md = PK11_CreateContextBySymKey(h->hash->ck_type, CKA_SIGN, h->key,
224                                            &noParams);
225         if (!h->md)
226                 goto bad;
227
228         if (PK11_DigestBegin(h->md) != SECSuccess)
229                 goto bad;
230
231         *ctx = h;
232         return 0;
233 bad:
234         crypt_hmac_destroy(h);
235         return -EINVAL;
236 }
237
238 static int crypt_hmac_restart(struct crypt_hmac *ctx)
239 {
240         if (PK11_DigestBegin(ctx->md) != SECSuccess)
241                 return -EINVAL;
242
243         return 0;
244 }
245
246 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
247 {
248         if (PK11_DigestOp(ctx->md, CONST_CAST(unsigned char *)buffer, length) != SECSuccess)
249                 return -EINVAL;
250
251         return 0;
252 }
253
254 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
255 {
256         unsigned char tmp[64];
257         unsigned int tmp_len;
258
259         if (length > (size_t)ctx->hash->length)
260                 return -EINVAL;
261
262         if (PK11_DigestFinal(ctx->md, tmp, &tmp_len, length) != SECSuccess)
263                 return -EINVAL;
264
265         memcpy(buffer, tmp, length);
266         memset(tmp, 0, sizeof(tmp));
267
268         if (tmp_len < length)
269                 return -EINVAL;
270
271         if (crypt_hmac_restart(ctx))
272                 return -EINVAL;
273
274         return 0;
275 }
276
277 int crypt_hmac_destroy(struct crypt_hmac *ctx)
278 {
279         if (ctx->key)
280                 PK11_FreeSymKey(ctx->key);
281         if (ctx->slot)
282                 PK11_FreeSlot(ctx->slot);
283         if (ctx->md)
284                 PK11_DestroyContext(ctx->md, PR_TRUE);
285         memset(ctx, 0, sizeof(*ctx));
286         free(ctx);
287         return 0;
288 }
289
290 /* RNG */
291 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
292 {
293         if (fips)
294                 return -EINVAL;
295
296         if (PK11_GenerateRandom((unsigned char *)buffer, length) != SECSuccess)
297                 return -EINVAL;
298
299         return 0;
300 }