21820d9b4725106f580dbf1cb418ce5e02ea8234
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_nettle.c
1 /*
2  * Nettle crypto backend implementation
3  *
4  * Copyright (C) 2011-2012 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2011-2012, Milan Broz
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <nettle/sha.h>
26 #include <nettle/hmac.h>
27 #include "crypto_backend.h"
28
29 static char *version = "Nettle";
30
31 typedef void (*init_func) (void *);
32 typedef void (*update_func) (void *, unsigned, const uint8_t *);
33 typedef void (*digest_func) (void *, unsigned, uint8_t *);
34 typedef void (*set_key_func) (void *, unsigned, const uint8_t *);
35
36 struct hash_alg {
37         const char *name;
38         int length;
39         init_func init;
40         update_func update;
41         digest_func digest;
42         update_func hmac_update;
43         digest_func hmac_digest;
44         set_key_func hmac_set_key;
45 };
46
47 static struct hash_alg hash_algs[] = {
48         { "sha1", SHA1_DIGEST_SIZE,
49                 (init_func) sha1_init,
50                 (update_func) sha1_update,
51                 (digest_func) sha1_digest,
52                 (update_func) hmac_sha1_update,
53                 (digest_func) hmac_sha1_digest,
54                 (set_key_func) hmac_sha1_set_key,
55         },
56         { "sha224", SHA224_DIGEST_SIZE,
57                 (init_func) sha224_init,
58                 (update_func) sha224_update,
59                 (digest_func) sha224_digest,
60                 (update_func) hmac_sha224_update,
61                 (digest_func) hmac_sha224_digest,
62                 (set_key_func) hmac_sha224_set_key,
63         },
64         { "sha256", SHA256_DIGEST_SIZE,
65                 (init_func) sha256_init,
66                 (update_func) sha256_update,
67                 (digest_func) sha256_digest,
68                 (update_func) hmac_sha256_update,
69                 (digest_func) hmac_sha256_digest,
70                 (set_key_func) hmac_sha256_set_key,
71         },
72         { "sha384", SHA384_DIGEST_SIZE,
73                 (init_func) sha384_init,
74                 (update_func) sha384_update,
75                 (digest_func) sha384_digest,
76                 (update_func) hmac_sha384_update,
77                 (digest_func) hmac_sha384_digest,
78                 (set_key_func) hmac_sha384_set_key,
79         },
80         { "sha512", SHA512_DIGEST_SIZE,
81                 (init_func) sha512_init,
82                 (update_func) sha512_update,
83                 (digest_func) sha512_digest,
84                 (update_func) hmac_sha512_update,
85                 (digest_func) hmac_sha512_digest,
86                 (set_key_func) hmac_sha512_set_key,
87         },
88         { "ripemd160", RIPEMD160_DIGEST_SIZE,
89                 (init_func) ripemd160_init,
90                 (update_func) ripemd160_update,
91                 (digest_func) ripemd160_digest,
92                 (update_func) hmac_ripemd160_update,
93                 (digest_func) hmac_ripemd160_digest,
94                 (set_key_func) hmac_ripemd160_set_key,
95         },
96         { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, }
97 };
98
99 struct crypt_hash {
100         const struct hash_alg *hash;
101         union {
102                 struct sha1_ctx sha1;
103                 struct sha224_ctx sha224;
104                 struct sha256_ctx sha256;
105                 struct sha384_ctx sha384;
106                 struct sha512_ctx sha512;
107         } nettle_ctx;
108 };
109
110 struct crypt_hmac {
111         const struct hash_alg *hash;
112         union {
113                 struct hmac_sha1_ctx sha1;
114                 struct hmac_sha224_ctx sha224;
115                 struct hmac_sha256_ctx sha256;
116                 struct hmac_sha384_ctx sha384;
117                 struct hmac_sha512_ctx sha512;
118         } nettle_ctx;
119         size_t key_length;
120         uint8_t *key;
121 };
122
123 uint32_t crypt_backend_flags(void)
124 {
125         return 0;
126 }
127
128 static struct hash_alg *_get_alg(const char *name)
129 {
130         int i = 0;
131
132         while (name && hash_algs[i].name) {
133                 if (!strcmp(name, hash_algs[i].name))
134                         return &hash_algs[i];
135                 i++;
136         }
137         return NULL;
138 }
139
140 int crypt_backend_init(struct crypt_device *ctx)
141 {
142         return 0;
143 }
144
145 const char *crypt_backend_version(void)
146 {
147         return version;
148 }
149
150 /* HASH */
151 int crypt_hash_size(const char *name)
152 {
153         struct hash_alg *ha = _get_alg(name);
154
155         return ha ? ha->length : -EINVAL;
156 }
157
158 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
159 {
160         struct crypt_hash *h;
161
162         h = malloc(sizeof(*h));
163         if (!h)
164                 return -ENOMEM;
165
166         h->hash = _get_alg(name);
167         if (!h->hash) {
168                 free(h);
169                 return -EINVAL;
170         }
171
172         h->hash->init(&h->nettle_ctx);
173
174         *ctx = h;
175         return 0;
176 }
177
178 static void crypt_hash_restart(struct crypt_hash *ctx)
179 {
180         ctx->hash->init(&ctx->nettle_ctx);
181 }
182
183 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
184 {
185         ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer);
186         return 0;
187 }
188
189 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
190 {
191         if (length > (size_t)ctx->hash->length)
192                 return -EINVAL;
193
194         ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
195         crypt_hash_restart(ctx);
196         return 0;
197 }
198
199 int crypt_hash_destroy(struct crypt_hash *ctx)
200 {
201         memset(ctx, 0, sizeof(*ctx));
202         free(ctx);
203         return 0;
204 }
205
206 /* HMAC */
207 int crypt_hmac_size(const char *name)
208 {
209         return crypt_hash_size(name);
210 }
211
212 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
213                     const void *buffer, size_t length)
214 {
215         struct crypt_hmac *h;
216
217         h = malloc(sizeof(*h));
218         if (!h)
219                 return -ENOMEM;
220         memset(ctx, 0, sizeof(*ctx));
221
222
223         h->hash = _get_alg(name);
224         if (!h->hash)
225                 goto bad;
226
227         h->key = malloc(length);
228         if (!h->key)
229                 goto bad;
230
231         memcpy(h->key, buffer, length);
232         h->key_length = length;
233
234         h->hash->init(&h->nettle_ctx);
235         h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key);
236
237         *ctx = h;
238         return 0;
239 bad:
240         free(h);
241         return -EINVAL;
242 }
243
244 static void crypt_hmac_restart(struct crypt_hmac *ctx)
245 {
246         ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key);
247 }
248
249 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
250 {
251         ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer);
252         return 0;
253 }
254
255 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
256 {
257         if (length > (size_t)ctx->hash->length)
258                 return -EINVAL;
259
260         ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
261         crypt_hmac_restart(ctx);
262         return 0;
263 }
264
265 int crypt_hmac_destroy(struct crypt_hmac *ctx)
266 {
267         memset(ctx->key, 0, ctx->key_length);
268         memset(ctx, 0, sizeof(*ctx));
269         free(ctx->key);
270         free(ctx);
271         return 0;
272 }
273
274 /* RNG - N/A */
275 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
276 {
277         return -EINVAL;
278 }
279
280 /* PBKDF */
281 int crypt_pbkdf(const char *kdf, const char *hash,
282                 const char *password, size_t password_length,
283                 const char *salt, size_t salt_length,
284                 char *key, size_t key_length,
285                 unsigned int iterations)
286 {
287         if (!kdf || strncmp(kdf, "pbkdf2", 6))
288                 return -EINVAL;
289
290         /* FIXME: switch to internal implementation in Nettle 2.6 */
291         return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
292                             iterations, key_length, key);
293 }