aboutsummaryrefslogtreecommitdiff
path: root/src/libmbedcrypto.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/libmbedcrypto.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/libmbedcrypto.c')
-rw-r--r--src/libmbedcrypto.c69
1 files changed, 6 insertions, 63 deletions
diff --git a/src/libmbedcrypto.c b/src/libmbedcrypto.c
index 748b3abb..02adffc0 100644
--- a/src/libmbedcrypto.c
+++ b/src/libmbedcrypto.c
@@ -37,10 +37,6 @@
static mbedtls_entropy_context ssh_mbedtls_entropy;
static mbedtls_ctr_drbg_context ssh_mbedtls_ctr_drbg;
-struct ssh_mac_ctx_struct {
- enum ssh_mac_e mac_type;
- mbedtls_md_context_t ctx;
-};
static int libmbedcrypto_initialized = 0;
void ssh_reseed(void)
@@ -386,66 +382,13 @@ void md5_final(unsigned char *md, MD5CTX c)
SAFE_FREE(c);
}
-ssh_mac_ctx ssh_mac_ctx_init(enum ssh_mac_e type)
-{
- ssh_mac_ctx ctx = malloc(sizeof (struct ssh_mac_ctx_struct));
- const mbedtls_md_info_t *md_info;
- int rc;
- if (ctx == NULL) {
- return NULL;
- }
-
- ctx->mac_type=type;
- switch(type) {
- case SSH_MAC_SHA1:
- md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
- break;
- case SSH_MAC_SHA256:
- md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
- break;
- case SSH_MAC_SHA384:
- md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
- break;
- case SSH_MAC_SHA512:
- md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
- break;
- default:
- goto error;
- }
-
- if (md_info == NULL) {
- goto error;
- }
-
- mbedtls_md_init(&ctx->ctx);
-
- rc = mbedtls_md_setup(&ctx->ctx, md_info, 0);
- if (rc != 0) {
- goto error;
- }
-
- rc = mbedtls_md_starts(&ctx->ctx);
- if (rc != 0) {
- goto error;
- }
-
- return ctx;
-
-error:
- SAFE_FREE(ctx);
- return NULL;
-}
-
-void ssh_mac_update(ssh_mac_ctx ctx, const void *data, unsigned long len)
+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)
{
- mbedtls_md_update(&ctx->ctx, data, len);
-}
-
-void ssh_mac_final(unsigned char *md, ssh_mac_ctx ctx)
-{
- mbedtls_md_finish(&ctx->ctx, md);
- mbedtls_md_free(&ctx->ctx);
- SAFE_FREE(ctx);
+ 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)