2 * Nettle crypto backend implementation
4 * Copyright (C) 2011-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.
23 #include <nettle/sha.h>
24 #include <nettle/hmac.h>
25 #include "crypto_backend.h"
27 static char *version = "Nettle";
29 typedef void (*init_func) (void *);
30 typedef void (*update_func) (void *, unsigned, const uint8_t *);
31 typedef void (*digest_func) (void *, unsigned, uint8_t *);
32 typedef void (*set_key_func) (void *, unsigned, const uint8_t *);
40 update_func hmac_update;
41 digest_func hmac_digest;
42 set_key_func hmac_set_key;
45 static struct hash_alg hash_algs[] = {
46 { "sha1", SHA1_DIGEST_SIZE,
47 (init_func) sha1_init,
48 (update_func) sha1_update,
49 (digest_func) sha1_digest,
50 (update_func) hmac_sha1_update,
51 (digest_func) hmac_sha1_digest,
52 (set_key_func) hmac_sha1_set_key,
54 { "sha224", SHA224_DIGEST_SIZE,
55 (init_func) sha224_init,
56 (update_func) sha224_update,
57 (digest_func) sha224_digest,
58 (update_func) hmac_sha224_update,
59 (digest_func) hmac_sha224_digest,
60 (set_key_func) hmac_sha224_set_key,
62 { "sha256", SHA256_DIGEST_SIZE,
63 (init_func) sha256_init,
64 (update_func) sha256_update,
65 (digest_func) sha256_digest,
66 (update_func) hmac_sha256_update,
67 (digest_func) hmac_sha256_digest,
68 (set_key_func) hmac_sha256_set_key,
70 { "sha384", SHA384_DIGEST_SIZE,
71 (init_func) sha384_init,
72 (update_func) sha384_update,
73 (digest_func) sha384_digest,
74 (update_func) hmac_sha384_update,
75 (digest_func) hmac_sha384_digest,
76 (set_key_func) hmac_sha384_set_key,
78 { "sha512", SHA512_DIGEST_SIZE,
79 (init_func) sha512_init,
80 (update_func) sha512_update,
81 (digest_func) sha512_digest,
82 (update_func) hmac_sha512_update,
83 (digest_func) hmac_sha512_digest,
84 (set_key_func) hmac_sha512_set_key,
86 { "ripemd160", RIPEMD160_DIGEST_SIZE,
87 (init_func) ripemd160_init,
88 (update_func) ripemd160_update,
89 (digest_func) ripemd160_digest,
90 (update_func) hmac_ripemd160_update,
91 (digest_func) hmac_ripemd160_digest,
92 (set_key_func) hmac_ripemd160_set_key,
94 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, }
98 const struct hash_alg *hash;
100 struct sha1_ctx sha1;
101 struct sha224_ctx sha224;
102 struct sha256_ctx sha256;
103 struct sha384_ctx sha384;
104 struct sha512_ctx sha512;
109 const struct hash_alg *hash;
111 struct hmac_sha1_ctx sha1;
112 struct hmac_sha224_ctx sha224;
113 struct hmac_sha256_ctx sha256;
114 struct hmac_sha384_ctx sha384;
115 struct hmac_sha512_ctx sha512;
121 uint32_t crypt_backend_flags(void)
126 static struct hash_alg *_get_alg(const char *name)
130 while (name && hash_algs[i].name) {
131 if (!strcmp(name, hash_algs[i].name))
132 return &hash_algs[i];
138 int crypt_backend_init(struct crypt_device *ctx)
143 const char *crypt_backend_version(void)
149 int crypt_hash_size(const char *name)
151 struct hash_alg *ha = _get_alg(name);
153 return ha ? ha->length : -EINVAL;
156 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
158 struct crypt_hash *h;
160 h = malloc(sizeof(*h));
164 h->hash = _get_alg(name);
170 h->hash->init(&h->nettle_ctx);
176 static void crypt_hash_restart(struct crypt_hash *ctx)
178 ctx->hash->init(&ctx->nettle_ctx);
181 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
183 ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer);
187 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
189 if (length > (size_t)ctx->hash->length)
192 ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
193 crypt_hash_restart(ctx);
197 int crypt_hash_destroy(struct crypt_hash *ctx)
199 memset(ctx, 0, sizeof(*ctx));
205 int crypt_hmac_size(const char *name)
207 return crypt_hash_size(name);
210 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
211 const void *buffer, size_t length)
213 struct crypt_hmac *h;
215 h = malloc(sizeof(*h));
218 memset(ctx, 0, sizeof(*ctx));
221 h->hash = _get_alg(name);
225 h->key = malloc(length);
229 memcpy(h->key, buffer, length);
230 h->key_length = length;
232 h->hash->init(&h->nettle_ctx);
233 h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key);
242 static void crypt_hmac_restart(struct crypt_hmac *ctx)
244 ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key);
247 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
249 ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer);
253 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
255 if (length > (size_t)ctx->hash->length)
258 ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer);
259 crypt_hmac_restart(ctx);
263 int crypt_hmac_destroy(struct crypt_hmac *ctx)
265 memset(ctx->key, 0, ctx->key_length);
266 memset(ctx, 0, sizeof(*ctx));
273 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)