aboutsummaryrefslogtreecommitdiff
path: root/src/libgcrypt.c
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2018-10-01 18:00:01 -0400
committerAndreas Schneider <asn@cryptomilk.org>2019-03-07 12:03:32 +0100
commit104c9dca3f963440e4f1d1170c9244682e700e28 (patch)
treee76fa82a434e0d9ffef553c3efac68fe8c6860be /src/libgcrypt.c
parentc180211c6b804628d308743a51a0270873cf7c6f (diff)
downloadlibssh-104c9dca3f963440e4f1d1170c9244682e700e28.tar.gz
libssh-104c9dca3f963440e4f1d1170c9244682e700e28.tar.xz
libssh-104c9dca3f963440e4f1d1170c9244682e700e28.zip
Use a common KDF function
Cleanup the KDF function to use only one function per crypto backend. Improve the KDF function to properly handle requested lenght and to avoid unnecessarily reallocating buffers. In OpenSSL use the new EVP_KDF API if available. Signed-off-by: Simo Sorce <simo@redhat.com> Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'src/libgcrypt.c')
-rw-r--r--src/libgcrypt.c63
1 files changed, 7 insertions, 56 deletions
diff --git a/src/libgcrypt.c b/src/libgcrypt.c
index 25f2eddf..cccc3b3f 100644
--- a/src/libgcrypt.c
+++ b/src/libgcrypt.c
@@ -36,11 +36,6 @@
#ifdef HAVE_LIBGCRYPT
#include <gcrypt.h>
-struct ssh_mac_ctx_struct {
- enum ssh_mac_e mac_type;
- gcry_md_hd_t ctx;
-};
-
static int libgcrypt_initialized = 0;
static int alloc_key(struct ssh_cipher_struct *cipher) {
@@ -220,57 +215,13 @@ void md5_final(unsigned char *md, MD5CTX c) {
gcry_md_close(c);
}
-ssh_mac_ctx ssh_mac_ctx_init(enum ssh_mac_e type){
- ssh_mac_ctx ctx = malloc(sizeof(struct ssh_mac_ctx_struct));
- if (ctx == NULL) {
- return NULL;
- }
-
- ctx->mac_type=type;
- switch(type){
- case SSH_MAC_SHA1:
- gcry_md_open(&ctx->ctx, GCRY_MD_SHA1, 0);
- break;
- case SSH_MAC_SHA256:
- gcry_md_open(&ctx->ctx, GCRY_MD_SHA256, 0);
- break;
- case SSH_MAC_SHA384:
- gcry_md_open(&ctx->ctx, GCRY_MD_SHA384, 0);
- break;
- case SSH_MAC_SHA512:
- gcry_md_open(&ctx->ctx, GCRY_MD_SHA512, 0);
- break;
- default:
- SAFE_FREE(ctx);
- return NULL;
- }
- return ctx;
-}
-
-void ssh_mac_update(ssh_mac_ctx ctx, const void *data, unsigned long len) {
- gcry_md_write(ctx->ctx,data,len);
-}
-
-void ssh_mac_final(unsigned char *md, ssh_mac_ctx ctx) {
- size_t len = 0;
- switch(ctx->mac_type){
- case SSH_MAC_SHA1:
- len=SHA_DIGEST_LEN;
- break;
- case SSH_MAC_SHA256:
- len=SHA256_DIGEST_LEN;
- break;
- case SSH_MAC_SHA384:
- len=SHA384_DIGEST_LEN;
- break;
- case SSH_MAC_SHA512:
- len=SHA512_DIGEST_LEN;
- break;
- }
- gcry_md_final(ctx->ctx);
- memcpy(md, gcry_md_read(ctx->ctx, 0), len);
- gcry_md_close(ctx->ctx);
- SAFE_FREE(ctx);
+int ssh_kdf(struct ssh_crypto_struct *crypto,
+ unsigned char *key, size_t key_len,
+ int key_type, unsigned char *output,
+ size_t requested_len)
+{
+ return sshkdf_derive_key(crypto, key, key_len,
+ key_type, output, requested_len);
}
HMACCTX hmac_init(const void *key, int len, enum ssh_hmac_e type) {