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 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, }
88 const struct hash_alg *hash;
91 struct sha224_ctx sha224;
92 struct sha256_ctx sha256;
93 struct sha384_ctx sha384;
94 struct sha512_ctx sha512;
99 const struct hash_alg *hash;
101 struct hmac_sha1_ctx sha1;
102 struct hmac_sha224_ctx sha224;
103 struct hmac_sha256_ctx sha256;
104 struct hmac_sha384_ctx sha384;
105 struct hmac_sha512_ctx sha512;
111 uint32_t crypt_backend_flags(void)
116 static struct hash_alg *_get_alg(const char *name)
120 while (name && hash_algs[i].name) {
121 if (!strcmp(name, hash_algs[i].name))
122 return &hash_algs[i];
128 int crypt_backend_init(struct crypt_device *ctx)
130 log_dbg("Initialising Nettle crypto backend.");
135 int crypt_hash_size(const char *name)
137 struct hash_alg *ha = _get_alg(name);
139 return ha ? ha->length : -EINVAL;
142 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
144 struct crypt_hash *h;
146 h = malloc(sizeof(*h));
150 h->hash = _get_alg(name);
156 h->hash->init(&h->nettle_ctx);
162 int crypt_hash_restart(struct crypt_hash *ctx)
164 ctx->hash->init(&ctx->nettle_ctx);
168 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
170 ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer);
174 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
176 if (length > (size_t)ctx->hash->length)
179 ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
183 int crypt_hash_destroy(struct crypt_hash *ctx)
185 memset(ctx, 0, sizeof(*ctx));
191 int crypt_hmac_size(const char *name)
193 return crypt_hash_size(name);
196 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
197 const void *buffer, size_t length)
199 struct crypt_hmac *h;
201 h = malloc(sizeof(*h));
204 memset(ctx, 0, sizeof(*ctx));
207 h->hash = _get_alg(name);
211 h->key = malloc(length);
215 memcpy(h->key, buffer, length);
216 h->key_length = length;
218 h->hash->init(&h->nettle_ctx);
219 h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key);
228 int crypt_hmac_restart(struct crypt_hmac *ctx)
230 ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key);
234 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
236 ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer);
240 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
242 if (length > (size_t)ctx->hash->length)
245 ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
249 int crypt_hmac_destroy(struct crypt_hmac *ctx)
251 memset(ctx->key, 0, ctx->key_length);
252 memset(ctx, 0, sizeof(*ctx));