Blame SOURCES/0001-Changes-for-OpenSSL-1.1.0-compatibility.patch

7784a1
From b1858f6b5fa33b9ef9eeea1f6152185d54bba323 Mon Sep 17 00:00:00 2001
7784a1
From: Wim Taymans <wtaymans@redhat.com>
7784a1
Date: Mon, 3 Sep 2018 13:19:44 +0200
7784a1
Subject: [PATCH] Changes for OpenSSL 1.1.0 compatibility
7784a1
7784a1
---
7784a1
 crypto/cipher/aes_gcm_ossl.c  |  36 +++++----
7784a1
 crypto/cipher/aes_icm_ossl.c  |  18 +++--
7784a1
 crypto/hash/hmac_ossl.c       | 135 ++++++++++++----------------------
7784a1
 crypto/include/aes_gcm_ossl.h |   2 +-
7784a1
 crypto/include/aes_icm_ossl.h |   2 +-
7784a1
 crypto/include/sha1.h         |  14 ++--
7784a1
 6 files changed, 89 insertions(+), 118 deletions(-)
7784a1
7784a1
diff --git a/crypto/cipher/aes_gcm_ossl.c b/crypto/cipher/aes_gcm_ossl.c
7784a1
index dce2a33..943dbd5 100644
7784a1
--- a/crypto/cipher/aes_gcm_ossl.c
7784a1
+++ b/crypto/cipher/aes_gcm_ossl.c
7784a1
@@ -116,6 +116,13 @@ err_status_t aes_gcm_openssl_alloc (cipher_t **c, int key_len, int tlen)
7784a1
     (*c)->state = allptr + sizeof(cipher_t);
7784a1
     gcm = (aes_gcm_ctx_t *)(*c)->state;
7784a1
 
7784a1
+    gcm->ctx = EVP_CIPHER_CTX_new();
7784a1
+    if (gcm->ctx == NULL) {
7784a1
+        crypto_free(*c);
7784a1
+        *c = NULL;
7784a1
+        return (err_status_alloc_fail);
7784a1
+    }
7784a1
+
7784a1
     /* increment ref_count */
7784a1
     switch (key_len) {
7784a1
     case AES_128_GCM_KEYSIZE_WSALT:
7784a1
@@ -136,7 +143,6 @@ err_status_t aes_gcm_openssl_alloc (cipher_t **c, int key_len, int tlen)
7784a1
 
7784a1
     /* set key size        */
7784a1
     (*c)->key_len = key_len;
7784a1
-    EVP_CIPHER_CTX_init(&gcm->ctx);
7784a1
 
7784a1
     return (err_status_ok);
7784a1
 }
7784a1
@@ -151,7 +157,7 @@ err_status_t aes_gcm_openssl_dealloc (cipher_t *c)
7784a1
 
7784a1
     ctx = (aes_gcm_ctx_t*)c->state;
7784a1
     if (ctx) {
7784a1
-	EVP_CIPHER_CTX_cleanup(&ctx->ctx);
7784a1
+	EVP_CIPHER_CTX_free(ctx->ctx);
7784a1
         /* decrement ref_count for the appropriate engine */
7784a1
         switch (ctx->key_size) {
7784a1
         case AES_256_KEYSIZE:
7784a1
@@ -197,7 +203,7 @@ err_status_t aes_gcm_openssl_context_init (aes_gcm_ctx_t *c, const uint8_t *key)
7784a1
 
7784a1
     debug_print(mod_aes_gcm, "key:  %s", v128_hex_string((v128_t*)&c->key));
7784a1
 
7784a1
-    EVP_CIPHER_CTX_cleanup(&c->ctx);
7784a1
+    EVP_CIPHER_CTX_cleanup(c->ctx);
7784a1
 
7784a1
     return (err_status_ok);
7784a1
 }
7784a1
@@ -231,19 +237,19 @@ err_status_t aes_gcm_openssl_set_iv (aes_gcm_ctx_t *c, void *iv,
7784a1
         break;
7784a1
     }
7784a1
 
7784a1
-    if (!EVP_CipherInit_ex(&c->ctx, evp, NULL, (const unsigned char*)&c->key.v8,
7784a1
+    if (!EVP_CipherInit_ex(c->ctx, evp, NULL, (const unsigned char*)&c->key.v8,
7784a1
                            NULL, (c->dir == direction_encrypt ? 1 : 0))) {
7784a1
         return (err_status_init_fail);
7784a1
     }
7784a1
 
7784a1
     /* set IV len  and the IV value, the followiong 3 calls are required */
7784a1
-    if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
7784a1
+    if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
7784a1
         return (err_status_init_fail);
7784a1
     }
7784a1
-    if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_IV_FIXED, -1, iv)) {
7784a1
+    if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IV_FIXED, -1, iv)) {
7784a1
         return (err_status_init_fail);
7784a1
     }
7784a1
-    if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_IV_GEN, 0, iv)) {
7784a1
+    if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_IV_GEN, 0, iv)) {
7784a1
         return (err_status_init_fail);
7784a1
     }
7784a1
 
7784a1
@@ -267,9 +273,9 @@ err_status_t aes_gcm_openssl_set_aad (aes_gcm_ctx_t *c, unsigned char *aad,
7784a1
      * Set dummy tag, OpenSSL requires the Tag to be set before
7784a1
      * processing AAD
7784a1
      */
7784a1
-    EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, aad);
7784a1
+    EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, aad);
7784a1
 
7784a1
-    rv = EVP_Cipher(&c->ctx, NULL, aad, aad_len);
7784a1
+    rv = EVP_Cipher(c->ctx, NULL, aad, aad_len);
7784a1
     if (rv != aad_len) {
7784a1
         return (err_status_algo_fail);
7784a1
     } else {
7784a1
@@ -295,7 +301,7 @@ err_status_t aes_gcm_openssl_encrypt (aes_gcm_ctx_t *c, unsigned char *buf,
7784a1
     /*
7784a1
      * Encrypt the data
7784a1
      */
7784a1
-    EVP_Cipher(&c->ctx, buf, buf, *enc_len);
7784a1
+    EVP_Cipher(c->ctx, buf, buf, *enc_len);
7784a1
 
7784a1
     return (err_status_ok);
7784a1
 }
7784a1
@@ -317,12 +323,12 @@ err_status_t aes_gcm_openssl_get_tag (aes_gcm_ctx_t *c, unsigned char *buf,
7784a1
     /*
7784a1
      * Calculate the tag
7784a1
      */
7784a1
-    EVP_Cipher(&c->ctx, NULL, NULL, 0);
7784a1
+    EVP_Cipher(c->ctx, NULL, NULL, 0);
7784a1
 
7784a1
     /*
7784a1
      * Retreive the tag
7784a1
      */
7784a1
-    EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_GET_TAG, c->tag_len, buf);
7784a1
+    EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_GET_TAG, c->tag_len, buf);
7784a1
 
7784a1
     /*
7784a1
      * Increase encryption length by desired tag size
7784a1
@@ -351,14 +357,14 @@ err_status_t aes_gcm_openssl_decrypt (aes_gcm_ctx_t *c, unsigned char *buf,
7784a1
     /*
7784a1
      * Set the tag before decrypting
7784a1
      */
7784a1
-    EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, 
7784a1
+    EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, 
7784a1
 	                buf + (*enc_len - c->tag_len));
7784a1
-    EVP_Cipher(&c->ctx, buf, buf, *enc_len - c->tag_len);
7784a1
+    EVP_Cipher(c->ctx, buf, buf, *enc_len - c->tag_len);
7784a1
 
7784a1
     /*
7784a1
      * Check the tag
7784a1
      */
7784a1
-    if (EVP_Cipher(&c->ctx, NULL, NULL, 0)) {
7784a1
+    if (EVP_Cipher(c->ctx, NULL, NULL, 0)) {
7784a1
         return (err_status_auth_fail);
7784a1
     }
7784a1
 
7784a1
diff --git a/crypto/cipher/aes_icm_ossl.c b/crypto/cipher/aes_icm_ossl.c
7784a1
index eb58539..1ddd39e 100644
7784a1
--- a/crypto/cipher/aes_icm_ossl.c
7784a1
+++ b/crypto/cipher/aes_icm_ossl.c
7784a1
@@ -143,6 +143,13 @@ err_status_t aes_icm_openssl_alloc (cipher_t **c, int key_len, int tlen)
7784a1
     (*c)->state = allptr + sizeof(cipher_t);
7784a1
     icm = (aes_icm_ctx_t*)(*c)->state;
7784a1
 
7784a1
+    icm->ctx = EVP_CIPHER_CTX_new();
7784a1
+    if (icm->ctx == NULL) {
7784a1
+        crypto_free(*c);
7784a1
+        *c = NULL;
7784a1
+        return err_status_alloc_fail;
7784a1
+    }
7784a1
+
7784a1
     /* increment ref_count */
7784a1
     switch (key_len) {
7784a1
     case AES_128_KEYSIZE_WSALT:
7784a1
@@ -169,7 +176,6 @@ err_status_t aes_icm_openssl_alloc (cipher_t **c, int key_len, int tlen)
7784a1
 
7784a1
     /* set key size        */
7784a1
     (*c)->key_len = key_len;
7784a1
-    EVP_CIPHER_CTX_init(&icm->ctx);
7784a1
 
7784a1
     return err_status_ok;
7784a1
 }
7784a1
@@ -191,7 +197,7 @@ err_status_t aes_icm_openssl_dealloc (cipher_t *c)
7784a1
      */
7784a1
     ctx = (aes_icm_ctx_t*)c->state;
7784a1
     if (ctx != NULL) {
7784a1
-        EVP_CIPHER_CTX_cleanup(&ctx->ctx);
7784a1
+        EVP_CIPHER_CTX_free(ctx->ctx);
7784a1
         /* decrement ref_count for the appropriate engine */
7784a1
         switch (ctx->key_size) {
7784a1
         case AES_256_KEYSIZE:
7784a1
@@ -271,7 +277,7 @@ err_status_t aes_icm_openssl_context_init (aes_icm_ctx_t *c, const uint8_t *key,
7784a1
     debug_print(mod_aes_icm, "key:  %s", v128_hex_string((v128_t*)&c->key));
7784a1
     debug_print(mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
7784a1
 
7784a1
-    EVP_CIPHER_CTX_cleanup(&c->ctx);
7784a1
+    EVP_CIPHER_CTX_cleanup(c->ctx);
7784a1
 
7784a1
     return err_status_ok;
7784a1
 }
7784a1
@@ -312,7 +318,7 @@ err_status_t aes_icm_openssl_set_iv (aes_icm_ctx_t *c, void *iv, int dir)
7784a1
         break;
7784a1
     }
7784a1
 
7784a1
-    if (!EVP_EncryptInit_ex(&c->ctx, evp,
7784a1
+    if (!EVP_EncryptInit_ex(c->ctx, evp,
7784a1
                             NULL, c->key.v8, c->counter.v8)) {
7784a1
         return err_status_fail;
7784a1
     } else {
7784a1
@@ -334,12 +340,12 @@ err_status_t aes_icm_openssl_encrypt (aes_icm_ctx_t *c, unsigned char *buf, unsi
7784a1
 
7784a1
     debug_print(mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
7784a1
 
7784a1
-    if (!EVP_EncryptUpdate(&c->ctx, buf, &len, buf, *enc_len)) {
7784a1
+    if (!EVP_EncryptUpdate(c->ctx, buf, &len, buf, *enc_len)) {
7784a1
         return err_status_cipher_fail;
7784a1
     }
7784a1
     *enc_len = len;
7784a1
 
7784a1
-    if (!EVP_EncryptFinal_ex(&c->ctx, buf, &len)) {
7784a1
+    if (!EVP_EncryptFinal_ex(c->ctx, buf, &len)) {
7784a1
         return err_status_cipher_fail;
7784a1
     }
7784a1
     *enc_len += len;
7784a1
diff --git a/crypto/hash/hmac_ossl.c b/crypto/hash/hmac_ossl.c
7784a1
index f62ce57..3f6f97d 100644
7784a1
--- a/crypto/hash/hmac_ossl.c
7784a1
+++ b/crypto/hash/hmac_ossl.c
7784a1
@@ -49,8 +49,10 @@
7784a1
 #include "hmac.h"
7784a1
 #include "alloc.h"
7784a1
 #include <openssl/evp.h>
7784a1
+#include <openssl/hmac.h>
7784a1
 
7784a1
-#define HMAC_KEYLEN_MAX		20
7784a1
+#define HMAC_KEYLEN_MAX                20
7784a1
+#define SHA1_DIGEST_SIZE               20
7784a1
 
7784a1
 /* the debug module for authentiation */
7784a1
 
7784a1
@@ -64,8 +66,6 @@ err_status_t
7784a1
 hmac_alloc (auth_t **a, int key_len, int out_len)
7784a1
 {
7784a1
     extern auth_type_t hmac;
7784a1
-    uint8_t *pointer;
7784a1
-    hmac_ctx_t *new_hmac_ctx;
7784a1
 
7784a1
     debug_print(mod_hmac, "allocating auth func with key length %d", key_len);
7784a1
     debug_print(mod_hmac, "                          tag length %d", out_len);
7784a1
@@ -79,25 +79,28 @@ hmac_alloc (auth_t **a, int key_len, int out_len)
7784a1
     }
7784a1
 
7784a1
     /* check output length - should be less than 20 bytes */
7784a1
-    if (out_len > HMAC_KEYLEN_MAX) {
7784a1
+    if (out_len > SHA1_DIGEST_SIZE) {
7784a1
         return err_status_bad_param;
7784a1
     }
7784a1
 
7784a1
     /* allocate memory for auth and hmac_ctx_t structures */
7784a1
-    pointer = (uint8_t*)crypto_alloc(sizeof(hmac_ctx_t) + sizeof(auth_t));
7784a1
-    if (pointer == NULL) {
7784a1
+    *a = crypto_alloc(sizeof(auth_t));
7784a1
+    if (*a == NULL) {
7784a1
+        return err_status_alloc_fail;
7784a1
+    }
7784a1
+
7784a1
+    (*a)->state = HMAC_CTX_new();
7784a1
+    if ((*a)->state == NULL) {
7784a1
+        crypto_free(*a);
7784a1
+        *a = NULL;
7784a1
         return err_status_alloc_fail;
7784a1
     }
7784a1
 
7784a1
     /* set pointers */
7784a1
-    *a = (auth_t*)pointer;
7784a1
     (*a)->type = &hmac;
7784a1
-    (*a)->state = pointer + sizeof(auth_t);
7784a1
     (*a)->out_len = out_len;
7784a1
     (*a)->key_len = key_len;
7784a1
     (*a)->prefix_len = 0;
7784a1
-    new_hmac_ctx = (hmac_ctx_t*)((*a)->state);
7784a1
-    memset(new_hmac_ctx, 0, sizeof(hmac_ctx_t));
7784a1
 
7784a1
     /* increment global count of all hmac uses */
7784a1
     hmac.ref_count++;
7784a1
@@ -109,19 +112,14 @@ err_status_t
7784a1
 hmac_dealloc (auth_t *a)
7784a1
 {
7784a1
     extern auth_type_t hmac;
7784a1
-    hmac_ctx_t *hmac_ctx;
7784a1
+    HMAC_CTX *hmac_ctx;
7784a1
 
7784a1
-    hmac_ctx = (hmac_ctx_t*)a->state;
7784a1
-    if (hmac_ctx->ctx_initialized) {
7784a1
-        EVP_MD_CTX_cleanup(&hmac_ctx->ctx);
7784a1
-    }
7784a1
-    if (hmac_ctx->init_ctx_initialized) {
7784a1
-        EVP_MD_CTX_cleanup(&hmac_ctx->init_ctx);
7784a1
-    }
7784a1
+    hmac_ctx = (HMAC_CTX*)a->state;
7784a1
+
7784a1
+    HMAC_CTX_free(hmac_ctx);
7784a1
 
7784a1
     /* zeroize entire state*/
7784a1
-    octet_string_set_to_zero((uint8_t*)a,
7784a1
-                             sizeof(hmac_ctx_t) + sizeof(auth_t));
7784a1
+    octet_string_set_to_zero((uint8_t*)a, sizeof(auth_t));
7784a1
 
7784a1
     /* free memory */
7784a1
     crypto_free(a);
7784a1
@@ -133,109 +131,68 @@ hmac_dealloc (auth_t *a)
7784a1
 }
7784a1
 
7784a1
 err_status_t
7784a1
-hmac_init (hmac_ctx_t *state, const uint8_t *key, int key_len)
7784a1
+hmac_start (hmac_ctx_t *statev)
7784a1
 {
7784a1
-    int i;
7784a1
-    uint8_t ipad[64];
7784a1
-
7784a1
-    /*
7784a1
-     * check key length - note that we don't support keys larger
7784a1
-     * than 20 bytes yet
7784a1
-     */
7784a1
-    if (key_len > HMAC_KEYLEN_MAX) {
7784a1
-        return err_status_bad_param;
7784a1
-    }
7784a1
-
7784a1
-    /*
7784a1
-     * set values of ipad and opad by exoring the key into the
7784a1
-     * appropriate constant values
7784a1
-     */
7784a1
-    for (i = 0; i < key_len; i++) {
7784a1
-        ipad[i] = key[i] ^ 0x36;
7784a1
-        state->opad[i] = key[i] ^ 0x5c;
7784a1
-    }
7784a1
-    /* set the rest of ipad, opad to constant values */
7784a1
-    for (; i < sizeof(ipad); i++) {
7784a1
-        ipad[i] = 0x36;
7784a1
-        ((uint8_t*)state->opad)[i] = 0x5c;
7784a1
-    }
7784a1
-
7784a1
-    debug_print(mod_hmac, "ipad: %s", octet_string_hex_string(ipad, sizeof(ipad)));
7784a1
+    HMAC_CTX *state = (HMAC_CTX *)statev;
7784a1
 
7784a1
-    /* initialize sha1 context */
7784a1
-    sha1_init(&state->init_ctx);
7784a1
-    state->init_ctx_initialized = 1;
7784a1
+    if (HMAC_Init_ex(state, NULL, 0, NULL, NULL) == 0)
7784a1
+        return err_status_auth_fail;
7784a1
 
7784a1
-    /* hash ipad ^ key */
7784a1
-    sha1_update(&state->init_ctx, ipad, sizeof(ipad));
7784a1
-    return (hmac_start(state));
7784a1
+    return err_status_ok;
7784a1
 }
7784a1
 
7784a1
 err_status_t
7784a1
-hmac_start (hmac_ctx_t *state)
7784a1
+hmac_init (hmac_ctx_t *statev, const uint8_t *key, int key_len)
7784a1
 {
7784a1
-    if (state->ctx_initialized) {
7784a1
-        EVP_MD_CTX_cleanup(&state->ctx);
7784a1
-    }
7784a1
-    if (!EVP_MD_CTX_copy(&state->ctx, &state->init_ctx)) {
7784a1
+    HMAC_CTX *state = (HMAC_CTX *)statev;
7784a1
+
7784a1
+    if (HMAC_Init_ex(state, key, key_len, EVP_sha1(), NULL) == 0)
7784a1
         return err_status_auth_fail;
7784a1
-    } else {
7784a1
-        state->ctx_initialized = 1;
7784a1
-        return err_status_ok;
7784a1
-    }
7784a1
+
7784a1
+    return err_status_ok;
7784a1
 }
7784a1
 
7784a1
 err_status_t
7784a1
-hmac_update (hmac_ctx_t *state, const uint8_t *message, int msg_octets)
7784a1
+hmac_update (hmac_ctx_t *statev, const uint8_t *message, int msg_octets)
7784a1
 {
7784a1
+    HMAC_CTX *state = (HMAC_CTX *)statev;
7784a1
+
7784a1
     debug_print(mod_hmac, "input: %s",
7784a1
                 octet_string_hex_string(message, msg_octets));
7784a1
 
7784a1
-    /* hash message into sha1 context */
7784a1
-    sha1_update(&state->ctx, message, msg_octets);
7784a1
+    if (HMAC_Update(state, message, msg_octets) == 0)
7784a1
+        return err_status_auth_fail;
7784a1
 
7784a1
     return err_status_ok;
7784a1
 }
7784a1
 
7784a1
 err_status_t
7784a1
-hmac_compute (hmac_ctx_t *state, const void *message,
7784a1
+hmac_compute (hmac_ctx_t *statev, const void *message,
7784a1
               int msg_octets, int tag_len, uint8_t *result)
7784a1
 {
7784a1
-    uint32_t hash_value[5];
7784a1
-    uint32_t H[5];
7784a1
+    HMAC_CTX *state = (HMAC_CTX *)statev;
7784a1
+    uint8_t hash_value[SHA1_DIGEST_SIZE];
7784a1
     int i;
7784a1
+    unsigned int len;
7784a1
 
7784a1
     /* check tag length, return error if we can't provide the value expected */
7784a1
-    if (tag_len > HMAC_KEYLEN_MAX) {
7784a1
+    if (tag_len > SHA1_DIGEST_SIZE) {
7784a1
         return err_status_bad_param;
7784a1
     }
7784a1
 
7784a1
     /* hash message, copy output into H */
7784a1
-    sha1_update(&state->ctx, message, msg_octets);
7784a1
-    sha1_final(&state->ctx, H);
7784a1
-
7784a1
-    /*
7784a1
-     * note that we don't need to debug_print() the input, since the
7784a1
-     * function hmac_update() already did that for us
7784a1
-     */
7784a1
-    debug_print(mod_hmac, "intermediate state: %s",
7784a1
-                octet_string_hex_string((uint8_t*)H, sizeof(H)));
7784a1
-
7784a1
-    /* re-initialize hash context */
7784a1
-    sha1_init(&state->ctx);
7784a1
-
7784a1
-    /* hash opad ^ key  */
7784a1
-    sha1_update(&state->ctx, (uint8_t*)state->opad, sizeof(state->opad));
7784a1
+    if (HMAC_Update(state, message, msg_octets) == 0)
7784a1
+        return err_status_auth_fail;
7784a1
 
7784a1
-    /* hash the result of the inner hash */
7784a1
-    sha1_update(&state->ctx, (uint8_t*)H, sizeof(H));
7784a1
+    if (HMAC_Final(state, hash_value, &len) == 0)
7784a1
+        return err_status_auth_fail;
7784a1
 
7784a1
-    /* the result is returned in the array hash_value[] */
7784a1
-    sha1_final(&state->ctx, hash_value);
7784a1
+    if (len < tag_len)
7784a1
+        return err_status_auth_fail;
7784a1
 
7784a1
     /* copy hash_value to *result */
7784a1
     for (i = 0; i < tag_len; i++) {
7784a1
-        result[i] = ((uint8_t*)hash_value)[i];
7784a1
+        result[i] = hash_value[i];
7784a1
     }
7784a1
 
7784a1
     debug_print(mod_hmac, "output: %s",
7784a1
diff --git a/crypto/include/aes_gcm_ossl.h b/crypto/include/aes_gcm_ossl.h
7784a1
index 8e7711d..4f49b51 100644
7784a1
--- a/crypto/include/aes_gcm_ossl.h
7784a1
+++ b/crypto/include/aes_gcm_ossl.h
7784a1
@@ -55,7 +55,7 @@ typedef struct {
7784a1
   v256_t   key;
7784a1
   int      key_size;
7784a1
   int      tag_len;
7784a1
-  EVP_CIPHER_CTX ctx;
7784a1
+  EVP_CIPHER_CTX* ctx;
7784a1
   cipher_direction_t dir;
7784a1
 } aes_gcm_ctx_t;
7784a1
 
7784a1
diff --git a/crypto/include/aes_icm_ossl.h b/crypto/include/aes_icm_ossl.h
7784a1
index b4ec40a..af23320 100644
7784a1
--- a/crypto/include/aes_icm_ossl.h
7784a1
+++ b/crypto/include/aes_icm_ossl.h
7784a1
@@ -72,7 +72,7 @@ typedef struct {
7784a1
     v128_t offset;                 /* initial offset value             */
7784a1
     v256_t key;
7784a1
     int key_size;
7784a1
-    EVP_CIPHER_CTX ctx;
7784a1
+    EVP_CIPHER_CTX* ctx;
7784a1
 } aes_icm_ctx_t;
7784a1
 
7784a1
 err_status_t aes_icm_openssl_set_iv(aes_icm_ctx_t *c, void *iv, int dir);
7784a1
diff --git a/crypto/include/sha1.h b/crypto/include/sha1.h
7784a1
index 2ce53e8..fb5bd95 100644
7784a1
--- a/crypto/include/sha1.h
7784a1
+++ b/crypto/include/sha1.h
7784a1
@@ -56,8 +56,6 @@
7784a1
 #include <openssl/evp.h>
7784a1
 #include <stdint.h>
7784a1
 
7784a1
-typedef EVP_MD_CTX sha1_ctx_t;
7784a1
-
7784a1
 /*
7784a1
  * sha1_init(&ctx) initializes the SHA1 context ctx
7784a1
  *
7784a1
@@ -72,23 +70,27 @@ typedef EVP_MD_CTX sha1_ctx_t;
7784a1
  *
7784a1
  */
7784a1
 
7784a1
+typedef EVP_MD_CTX* sha1_ctx_t;
7784a1
+
7784a1
 static inline void sha1_init (sha1_ctx_t *ctx)
7784a1
 {
7784a1
-    EVP_MD_CTX_init(ctx);
7784a1
-    EVP_DigestInit(ctx, EVP_sha1());
7784a1
+    *ctx = EVP_MD_CTX_new();
7784a1
+    EVP_DigestInit(*ctx, EVP_sha1());
7784a1
 }
7784a1
 
7784a1
 static inline void sha1_update (sha1_ctx_t *ctx, const uint8_t *M, int octets_in_msg)
7784a1
 {
7784a1
-    EVP_DigestUpdate(ctx, M, octets_in_msg);
7784a1
+    EVP_DigestUpdate(*ctx, M, octets_in_msg);
7784a1
 }
7784a1
 
7784a1
 static inline void sha1_final (sha1_ctx_t *ctx, uint32_t *output)
7784a1
 {
7784a1
     unsigned int len = 0;
7784a1
 
7784a1
-    EVP_DigestFinal(ctx, (unsigned char*)output, &len;;
7784a1
+    EVP_DigestFinal(*ctx, (unsigned char*)output, &len;;
7784a1
+    EVP_MD_CTX_free(*ctx);
7784a1
 }
7784a1
+
7784a1
 #else
7784a1
 #include "datatypes.h"
7784a1
 
7784a1
-- 
7784a1
2.17.1
7784a1