2 * Nettle crypto backend implementation
4 * Copyright (C) 2011 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <nettle/sha.h>
24 #include <nettle/hmac.h>
25 #include "crypto_backend.h"
27 typedef void (*init_func) (void *);
28 typedef void (*update_func) (void *, unsigned, const uint8_t *);
29 typedef void (*digest_func) (void *, unsigned, uint8_t *);
30 typedef void (*set_key_func) (void *, unsigned, const uint8_t *);
38 update_func hmac_update;
39 digest_func hmac_digest;
40 set_key_func hmac_set_key;
43 static struct hash_alg hash_algs[] = {
44 { "sha1", SHA1_DIGEST_SIZE,
45 (init_func) sha1_init,
46 (update_func) sha1_update,
47 (digest_func) sha1_digest,
48 (update_func) hmac_sha1_update,
49 (digest_func) hmac_sha1_digest,
50 (set_key_func) hmac_sha1_set_key,
52 { "sha224", SHA224_DIGEST_SIZE,
53 (init_func) sha224_init,
54 (update_func) sha224_update,
55 (digest_func) sha224_digest,
56 (update_func) hmac_sha224_update,
57 (digest_func) hmac_sha224_digest,
58 (set_key_func) hmac_sha224_set_key,
60 { "sha256", SHA256_DIGEST_SIZE,
61 (init_func) sha256_init,
62 (update_func) sha256_update,
63 (digest_func) sha256_digest,
64 (update_func) hmac_sha256_update,
65 (digest_func) hmac_sha256_digest,
66 (set_key_func) hmac_sha256_set_key,
68 { "sha384", SHA384_DIGEST_SIZE,
69 (init_func) sha384_init,
70 (update_func) sha384_update,
71 (digest_func) sha384_digest,
72 (update_func) hmac_sha384_update,
73 (digest_func) hmac_sha384_digest,
74 (set_key_func) hmac_sha384_set_key,
76 { "sha512", SHA512_DIGEST_SIZE,
77 (init_func) sha512_init,
78 (update_func) sha512_update,
79 (digest_func) sha512_digest,
80 (update_func) hmac_sha512_update,
81 (digest_func) hmac_sha512_digest,
82 (set_key_func) hmac_sha512_set_key,
84 { "ripemd160", RIPEMD160_DIGEST_SIZE,
85 (init_func) ripemd160_init,
86 (update_func) ripemd160_update,
87 (digest_func) ripemd160_digest,
88 (update_func) hmac_ripemd160_update,
89 (digest_func) hmac_ripemd160_digest,
90 (set_key_func) hmac_ripemd160_set_key,
92 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, }
96 const struct hash_alg *hash;
99 struct sha224_ctx sha224;
100 struct sha256_ctx sha256;
101 struct sha384_ctx sha384;
102 struct sha512_ctx sha512;
107 const struct hash_alg *hash;
109 struct hmac_sha1_ctx sha1;
110 struct hmac_sha224_ctx sha224;
111 struct hmac_sha256_ctx sha256;
112 struct hmac_sha384_ctx sha384;
113 struct hmac_sha512_ctx sha512;
119 uint32_t crypt_backend_flags(void)
124 static struct hash_alg *_get_alg(const char *name)
128 while (name && hash_algs[i].name) {
129 if (!strcmp(name, hash_algs[i].name))
130 return &hash_algs[i];
136 int crypt_backend_init(struct crypt_device *ctx)
138 log_dbg("Initialising Nettle crypto backend.");
143 int crypt_hash_size(const char *name)
145 struct hash_alg *ha = _get_alg(name);
147 return ha ? ha->length : -EINVAL;
150 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
152 struct crypt_hash *h;
154 h = malloc(sizeof(*h));
158 h->hash = _get_alg(name);
164 h->hash->init(&h->nettle_ctx);
170 static void crypt_hash_restart(struct crypt_hash *ctx)
172 ctx->hash->init(&ctx->nettle_ctx);
175 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
177 ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer);
181 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
183 if (length > (size_t)ctx->hash->length)
186 ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
187 crypt_hash_restart(ctx);
191 int crypt_hash_destroy(struct crypt_hash *ctx)
193 memset(ctx, 0, sizeof(*ctx));
199 int crypt_hmac_size(const char *name)
201 return crypt_hash_size(name);
204 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
205 const void *buffer, size_t length)
207 struct crypt_hmac *h;
209 h = malloc(sizeof(*h));
212 memset(ctx, 0, sizeof(*ctx));
215 h->hash = _get_alg(name);
219 h->key = malloc(length);
223 memcpy(h->key, buffer, length);
224 h->key_length = length;
226 h->hash->init(&h->nettle_ctx);
227 h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key);
236 static void crypt_hmac_restart(struct crypt_hmac *ctx)
238 ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key);
241 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
243 ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer);
247 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
249 if (length > (size_t)ctx->hash->length)
252 ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
253 crypt_hmac_restart(ctx);
257 int crypt_hmac_destroy(struct crypt_hmac *ctx)
259 memset(ctx->key, 0, ctx->key_length);
260 memset(ctx, 0, sizeof(*ctx));