From 35d6914779578908e7b37abfdd188ec7c8101404 Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Thu, 28 Apr 2011 15:48:11 +0000 Subject: [PATCH] Add Nettle crypto backend support. git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@516 36d66b0a-2a48-0410-832c-cd162a569da5 --- ChangeLog | 1 + TODO | 1 - configure.in | 19 ++- lib/crypto_backend/Makefile.am | 3 + lib/crypto_backend/crypto_nettle.c | 256 +++++++++++++++++++++++++++++++++++++ 5 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 lib/crypto_backend/crypto_nettle.c diff --git a/ChangeLog b/ChangeLog index 7137ec2..876ea8d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 2011-04-18 Milan Broz * Fix error paths in blockwise code and lseek_write call. + * Add Nettle crypto backend support. 2011-04-05 Milan Broz * Version 1.3.0. diff --git a/TODO b/TODO index 5f52bc5..0022189 100644 --- a/TODO +++ b/TODO @@ -2,4 +2,3 @@ Version 1.4.0: - Remove old API (all calls using crypt_options) - Support separation of metadata device - Wipe device flag -- Add Natty crypto backend diff --git a/configure.in b/configure.in index 0902903..2e5367d 100644 --- a/configure.in +++ b/configure.in @@ -119,6 +119,21 @@ AC_DEFUN([CONFIGURE_KERNEL], [ ]) +AC_DEFUN([CONFIGURE_NETTLE], [ + AC_CHECK_HEADERS(nettle/sha.h,, + [AC_MSG_ERROR('You need Nettle cryptographic library.')]) + + saved_LIBS=$LIBS + AC_CHECK_LIB(nettle, nettle_sha512_init,, + [AC_MSG_ERROR('You need Nettle library version 2.1 or more recent.')]) + CRYPTO_LIBS=$LIBS + LIBS=$saved_LIBS + + AC_MSG_WARN([Nettle backend does NOT provide backward compatibility (missing ripemd160 hash).]) + + CRYPTO_STATIC_LIBS=$CRYPTO_LIBS +]) + dnl ========================================================================== saved_LIBS=$LIBS @@ -165,7 +180,7 @@ LIBS=$saved_LIBS dnl Crypto backend configuration. AC_ARG_WITH([crypto_backend], - AS_HELP_STRING([--with-crypto_backend=BACKEND], [crypto backend (gcrypt/openssl/nss/kernel) [gcrypt]]), + AS_HELP_STRING([--with-crypto_backend=BACKEND], [crypto backend (gcrypt/openssl/nss/kernel/nettle) [gcrypt]]), [], with_crypto_backend=gcrypt ) case $with_crypto_backend in @@ -173,12 +188,14 @@ case $with_crypto_backend in openssl) CONFIGURE_OPENSSL([]) ;; nss) CONFIGURE_NSS([]) ;; kernel) CONFIGURE_KERNEL([]) ;; + nettle) CONFIGURE_NETTLE([]) ;; *) AC_MSG_ERROR([Unknown crypto backend.]) ;; esac AM_CONDITIONAL(CRYPTO_BACKEND_GCRYPT, test $with_crypto_backend = gcrypt) AM_CONDITIONAL(CRYPTO_BACKEND_OPENSSL, test $with_crypto_backend = openssl) AM_CONDITIONAL(CRYPTO_BACKEND_NSS, test $with_crypto_backend = nss) AM_CONDITIONAL(CRYPTO_BACKEND_KERNEL, test $with_crypto_backend = kernel) +AM_CONDITIONAL(CRYPTO_BACKEND_NETTLE, test $with_crypto_backend = nettle) dnl Magic for cryptsetup.static build. if test x$enable_static_cryptsetup = xyes; then diff --git a/lib/crypto_backend/Makefile.am b/lib/crypto_backend/Makefile.am index 59ee5b2..c7953b6 100644 --- a/lib/crypto_backend/Makefile.am +++ b/lib/crypto_backend/Makefile.am @@ -18,5 +18,8 @@ endif if CRYPTO_BACKEND_KERNEL libcrypto_backend_la_SOURCES += crypto_kernel.c endif +if CRYPTO_BACKEND_NETTLE +libcrypto_backend_la_SOURCES += crypto_nettle.c +endif INCLUDES = -D_GNU_SOURCE -I$(top_srcdir)/lib diff --git a/lib/crypto_backend/crypto_nettle.c b/lib/crypto_backend/crypto_nettle.c new file mode 100644 index 0000000..37c9b85 --- /dev/null +++ b/lib/crypto_backend/crypto_nettle.c @@ -0,0 +1,256 @@ +/* + * Nettle crypto backend implementation + * + * Copyright (C) 2011 Red Hat, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include "crypto_backend.h" + +typedef void (*init_func) (void *); +typedef void (*update_func) (void *, unsigned, const uint8_t *); +typedef void (*digest_func) (void *, unsigned, uint8_t *); +typedef void (*set_key_func) (void *, unsigned, const uint8_t *); + +struct hash_alg { + const char *name; + int length; + init_func init; + update_func update; + digest_func digest; + update_func hmac_update; + digest_func hmac_digest; + set_key_func hmac_set_key; +}; + +static struct hash_alg hash_algs[] = { + { "sha1", SHA1_DIGEST_SIZE, + (init_func) sha1_init, + (update_func) sha1_update, + (digest_func) sha1_digest, + (update_func) hmac_sha1_update, + (digest_func) hmac_sha1_digest, + (set_key_func) hmac_sha1_set_key, + }, + { "sha224", SHA224_DIGEST_SIZE, + (init_func) sha224_init, + (update_func) sha224_update, + (digest_func) sha224_digest, + (update_func) hmac_sha224_update, + (digest_func) hmac_sha224_digest, + (set_key_func) hmac_sha224_set_key, + }, + { "sha256", SHA256_DIGEST_SIZE, + (init_func) sha256_init, + (update_func) sha256_update, + (digest_func) sha256_digest, + (update_func) hmac_sha256_update, + (digest_func) hmac_sha256_digest, + (set_key_func) hmac_sha256_set_key, + }, + { "sha384", SHA384_DIGEST_SIZE, + (init_func) sha384_init, + (update_func) sha384_update, + (digest_func) sha384_digest, + (update_func) hmac_sha384_update, + (digest_func) hmac_sha384_digest, + (set_key_func) hmac_sha384_set_key, + }, + { "sha512", SHA512_DIGEST_SIZE, + (init_func) sha512_init, + (update_func) sha512_update, + (digest_func) sha512_digest, + (update_func) hmac_sha512_update, + (digest_func) hmac_sha512_digest, + (set_key_func) hmac_sha512_set_key, + }, + { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, } +}; + +struct crypt_hash { + const struct hash_alg *hash; + union { + struct sha1_ctx sha1; + struct sha224_ctx sha224; + struct sha256_ctx sha256; + struct sha384_ctx sha384; + struct sha512_ctx sha512; + } nettle_ctx; +}; + +struct crypt_hmac { + const struct hash_alg *hash; + union { + struct hmac_sha1_ctx sha1; + struct hmac_sha224_ctx sha224; + struct hmac_sha256_ctx sha256; + struct hmac_sha384_ctx sha384; + struct hmac_sha512_ctx sha512; + } nettle_ctx; + size_t key_length; + uint8_t *key; +}; + +uint32_t crypt_backend_flags(void) +{ + return 0; +} + +static struct hash_alg *_get_alg(const char *name) +{ + int i = 0; + + while (name && hash_algs[i].name) { + if (!strcmp(name, hash_algs[i].name)) + return &hash_algs[i]; + i++; + } + return NULL; +} + +int crypt_backend_init(struct crypt_device *ctx) +{ + log_dbg("Initialising Nettle crypto backend."); + return 0; +} + +/* HASH */ +int crypt_hash_size(const char *name) +{ + struct hash_alg *ha = _get_alg(name); + + return ha ? ha->length : -EINVAL; +} + +int crypt_hash_init(struct crypt_hash **ctx, const char *name) +{ + struct crypt_hash *h; + + h = malloc(sizeof(*h)); + if (!h) + return -ENOMEM; + + h->hash = _get_alg(name); + if (!h->hash) { + free(h); + return -EINVAL; + } + + h->hash->init(&h->nettle_ctx); + + *ctx = h; + return 0; +} + +int crypt_hash_restart(struct crypt_hash *ctx) +{ + ctx->hash->init(&ctx->nettle_ctx); + return 0; +} + +int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length) +{ + ctx->hash->update(&ctx->nettle_ctx, length, (const uint8_t*)buffer); + return 0; +} + +int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length) +{ + if (length > (size_t)ctx->hash->length) + return -EINVAL; + + ctx->hash->digest(&ctx->nettle_ctx, length, (uint8_t *)buffer); + return 0; +} + +int crypt_hash_destroy(struct crypt_hash *ctx) +{ + memset(ctx, 0, sizeof(*ctx)); + free(ctx); + return 0; +} + +/* HMAC */ +int crypt_hmac_size(const char *name) +{ + return crypt_hash_size(name); +} + +int crypt_hmac_init(struct crypt_hmac **ctx, const char *name, + const void *buffer, size_t length) +{ + struct crypt_hmac *h; + + h = malloc(sizeof(*h)); + if (!h) + return -ENOMEM; + memset(ctx, 0, sizeof(*ctx)); + + + h->hash = _get_alg(name); + if (!h->hash) + goto bad; + + h->key = malloc(length); + if (!h->key) + goto bad; + + memcpy(h->key, buffer, length); + h->key_length = length; + + h->hash->init(&h->nettle_ctx); + h->hash->hmac_set_key(&h->nettle_ctx, h->key_length, h->key); + + *ctx = h; + return 0; +bad: + free(h); + return -EINVAL; +} + +int crypt_hmac_restart(struct crypt_hmac *ctx) +{ + ctx->hash->hmac_set_key(&ctx->nettle_ctx, ctx->key_length, ctx->key); + return 0; +} + +int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length) +{ + ctx->hash->hmac_update(&ctx->nettle_ctx, length, (const uint8_t *)buffer); + return 0; +} + +int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length) +{ + if (length > (size_t)ctx->hash->length) + return -EINVAL; + + ctx->hash->hmac_digest(&ctx->nettle_ctx, length, (uint8_t *)buffer); + return 0; +} + +int crypt_hmac_destroy(struct crypt_hmac *ctx) +{ + memset(ctx->key, 0, ctx->key_length); + memset(ctx, 0, sizeof(*ctx)); + free(ctx->key); + free(ctx); + return 0; +} -- 2.7.4