From 133f8d09fcef5461e70c3a81c7bf8124778591c6 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Wed, 27 Dec 2017 23:35:26 +0200 Subject: [PATCH] GnuTLS: Implement HMAC functions using libgcrypt Replace the internal HMAC MD5, SHA-1, and SHA256 implementations with the ones from libgcrypt and also add the SHA384 and SHA512 versions. Signed-off-by: Jouni Malinen --- hostapd/Android.mk | 10 ++++ hostapd/Makefile | 10 ++++ src/crypto/crypto_gnutls.c | 116 +++++++++++++++++++++++++++++++++++++ wpa_supplicant/Android.mk | 10 ++++ wpa_supplicant/Makefile | 10 ++++ 5 files changed, 156 insertions(+) diff --git a/hostapd/Android.mk b/hostapd/Android.mk index c185aed28..04167a699 100644 --- a/hostapd/Android.mk +++ b/hostapd/Android.mk @@ -809,8 +809,10 @@ endif SHA1OBJS = ifdef NEED_SHA1 ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) SHA1OBJS += src/crypto/sha1.c endif +endif SHA1OBJS += src/crypto/sha1-prf.c ifdef CONFIG_INTERNAL_SHA1 SHA1OBJS += src/crypto/sha1-internal.c @@ -834,8 +836,10 @@ OBJS += $(SHA1OBJS) endif ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) OBJS += src/crypto/md5.c endif +endif ifdef NEED_MD5 ifdef CONFIG_INTERNAL_MD5 @@ -871,8 +875,10 @@ endif ifdef NEED_SHA256 L_CFLAGS += -DCONFIG_SHA256 ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) OBJS += src/crypto/sha256.c endif +endif OBJS += src/crypto/sha256-prf.c ifdef CONFIG_INTERNAL_SHA256 OBJS += src/crypto/sha256-internal.c @@ -893,17 +899,21 @@ endif ifdef NEED_SHA384 L_CFLAGS += -DCONFIG_SHA384 ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) OBJS += src/crypto/sha384.c endif +endif OBJS += src/crypto/sha384-prf.c endif ifdef NEED_SHA512 L_CFLAGS += -DCONFIG_SHA512 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) OBJS += src/crypto/sha512.c endif endif +endif OBJS += src/crypto/sha512-prf.c endif diff --git a/hostapd/Makefile b/hostapd/Makefile index 456c9b438..cf6f829fd 100644 --- a/hostapd/Makefile +++ b/hostapd/Makefile @@ -894,9 +894,11 @@ endif ifdef NEED_SHA1 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) SHA1OBJS += ../src/crypto/sha1.o endif endif +endif SHA1OBJS += ../src/crypto/sha1-prf.o ifdef CONFIG_INTERNAL_SHA1 SHA1OBJS += ../src/crypto/sha1-internal.o @@ -921,9 +923,11 @@ endif ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) OBJS += ../src/crypto/md5.o endif endif +endif ifdef NEED_MD5 ifdef CONFIG_INTERNAL_MD5 @@ -960,9 +964,11 @@ ifdef NEED_SHA256 CFLAGS += -DCONFIG_SHA256 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) OBJS += ../src/crypto/sha256.o endif endif +endif OBJS += ../src/crypto/sha256-prf.o ifdef CONFIG_INTERNAL_SHA256 OBJS += ../src/crypto/sha256-internal.o @@ -984,18 +990,22 @@ ifdef NEED_SHA384 CFLAGS += -DCONFIG_SHA384 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) OBJS += ../src/crypto/sha384.o endif endif +endif OBJS += ../src/crypto/sha384-prf.o endif ifdef NEED_SHA512 CFLAGS += -DCONFIG_SHA512 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) OBJS += ../src/crypto/sha512.o endif endif +endif OBJS += ../src/crypto/sha512-prf.o endif diff --git a/src/crypto/crypto_gnutls.c b/src/crypto/crypto_gnutls.c index 3fe774c5c..7c036a668 100644 --- a/src/crypto/crypto_gnutls.c +++ b/src/crypto/crypto_gnutls.c @@ -10,6 +10,11 @@ #include #include "common.h" +#include "md5.h" +#include "sha1.h" +#include "sha256.h" +#include "sha384.h" +#include "sha512.h" #include "crypto.h" static int gnutls_digest_vector(int algo, size_t num_elem, @@ -90,6 +95,117 @@ int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) } +static int gnutls_hmac_vector(int algo, const u8 *key, size_t key_len, + size_t num_elem, const u8 *addr[], + const size_t *len, u8 *mac) +{ + gcry_md_hd_t hd; + unsigned char *p; + size_t i; + + if (gcry_md_open(&hd, algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR) + return -1; + if (gcry_md_setkey(hd, key, key_len) != GPG_ERR_NO_ERROR) { + gcry_md_close(hd); + return -1; + } + for (i = 0; i < num_elem; i++) + gcry_md_write(hd, addr[i], len[i]); + p = gcry_md_read(hd, algo); + if (p) + memcpy(mac, p, gcry_md_get_algo_dlen(algo)); + gcry_md_close(hd); + return 0; +} + + +int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem, + const u8 *addr[], const size_t *len, u8 *mac) +{ + return gnutls_hmac_vector(GCRY_MD_MD5, key, key_len, num_elem, addr, + len, mac); +} + + +int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len, + u8 *mac) +{ + return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac); +} + + +int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, + const u8 *addr[], const size_t *len, u8 *mac) +{ + return gnutls_hmac_vector(GCRY_MD_SHA1, key, key_len, num_elem, addr, + len, mac); +} + + +int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, + u8 *mac) +{ + return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); +} + + +#ifdef CONFIG_SHA256 + +int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem, + const u8 *addr[], const size_t *len, u8 *mac) +{ + return gnutls_hmac_vector(GCRY_MD_SHA256, key, key_len, num_elem, addr, + len, mac); +} + + +int hmac_sha256(const u8 *key, size_t key_len, const u8 *data, + size_t data_len, u8 *mac) +{ + return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); +} + +#endif /* CONFIG_SHA256 */ + + +#ifdef CONFIG_SHA384 + +int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem, + const u8 *addr[], const size_t *len, u8 *mac) +{ + return gnutls_hmac_vector(GCRY_MD_SHA384, key, key_len, num_elem, addr, + len, mac); +} + + +int hmac_sha384(const u8 *key, size_t key_len, const u8 *data, + size_t data_len, u8 *mac) +{ + return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac); +} + +#endif /* CONFIG_SHA384 */ + + +#ifdef CONFIG_SHA512 + +int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem, + const u8 *addr[], const size_t *len, u8 *mac) +{ + return gnutls_hmac_vector(GCRY_MD_SHA512, key, key_len, num_elem, addr, + len, mac); +} + + +int hmac_sha512(const u8 *key, size_t key_len, const u8 *data, + size_t data_len, u8 *mac) +{ + return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac); +} + +#endif /* CONFIG_SHA512 */ + + void * aes_encrypt_init(const u8 *key, size_t len) { gcry_cipher_hd_t hd; diff --git a/wpa_supplicant/Android.mk b/wpa_supplicant/Android.mk index ed3dd734e..f632bbdd3 100644 --- a/wpa_supplicant/Android.mk +++ b/wpa_supplicant/Android.mk @@ -1240,8 +1240,10 @@ endif SHA1OBJS = ifdef NEED_SHA1 ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) SHA1OBJS += src/crypto/sha1.c endif +endif SHA1OBJS += src/crypto/sha1-prf.c ifdef CONFIG_INTERNAL_SHA1 SHA1OBJS += src/crypto/sha1-internal.c @@ -1267,9 +1269,11 @@ endif MD5OBJS = ifndef CONFIG_FIPS ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) MD5OBJS += src/crypto/md5.c endif endif +endif ifdef NEED_MD5 ifdef CONFIG_INTERNAL_MD5 MD5OBJS += src/crypto/md5-internal.c @@ -1307,8 +1311,10 @@ SHA256OBJS = # none by default ifdef NEED_SHA256 L_CFLAGS += -DCONFIG_SHA256 ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) SHA256OBJS += src/crypto/sha256.c endif +endif SHA256OBJS += src/crypto/sha256-prf.c ifdef CONFIG_INTERNAL_SHA256 SHA256OBJS += src/crypto/sha256-internal.c @@ -1341,15 +1347,19 @@ endif ifdef NEED_SHA384 L_CFLAGS += -DCONFIG_SHA384 ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) OBJS += src/crypto/sha384.c endif +endif OBJS += src/crypto/sha384-prf.c endif ifdef NEED_SHA512 L_CFLAGS += -DCONFIG_SHA512 ifneq ($(CONFIG_TLS), openssl) +ifneq ($(CONFIG_TLS), gnutls) OBJS += src/crypto/sha512.c endif +endif OBJS += src/crypto/sha512-prf.c endif diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile index e60a05c95..443f5b3a9 100644 --- a/wpa_supplicant/Makefile +++ b/wpa_supplicant/Makefile @@ -1332,9 +1332,11 @@ endif ifdef NEED_SHA1 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) SHA1OBJS += ../src/crypto/sha1.o endif endif +endif SHA1OBJS += ../src/crypto/sha1-prf.o ifdef CONFIG_INTERNAL_SHA1 SHA1OBJS += ../src/crypto/sha1-internal.o @@ -1360,10 +1362,12 @@ endif ifndef CONFIG_FIPS ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) MD5OBJS += ../src/crypto/md5.o endif endif endif +endif ifdef NEED_MD5 ifdef CONFIG_INTERNAL_MD5 MD5OBJS += ../src/crypto/md5-internal.o @@ -1403,9 +1407,11 @@ ifdef NEED_SHA256 CFLAGS += -DCONFIG_SHA256 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) SHA256OBJS += ../src/crypto/sha256.o endif endif +endif SHA256OBJS += ../src/crypto/sha256-prf.o ifdef CONFIG_INTERNAL_SHA256 SHA256OBJS += ../src/crypto/sha256-internal.o @@ -1438,18 +1444,22 @@ endif ifdef NEED_SHA384 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) OBJS += ../src/crypto/sha384.o endif endif +endif CFLAGS += -DCONFIG_SHA384 OBJS += ../src/crypto/sha384-prf.o endif ifdef NEED_SHA512 ifneq ($(CONFIG_TLS), openssl) ifneq ($(CONFIG_TLS), linux) +ifneq ($(CONFIG_TLS), gnutls) OBJS += ../src/crypto/sha512.o endif endif +endif CFLAGS += -DCONFIG_SHA512 OBJS += ../src/crypto/sha512-prf.o endif