Some engines (namely VIA C7 Padlock) work only if EVP_MD_CTX_FLAG_ONESHOT
is set before final update. This is because some crypto accelerators cannot
perform non-finalizing transform of the digest.

The usage of EVP_MD_CTX_FLAG_ONESHOT is used semantically slightly
differently here. It is set before the final EVP_DigestUpdate call, not
necessarily before EVP_DigestInit call. This will not cause any problems
though.
---
 crypto/hmac/hmac.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/crypto/hmac/hmac.c b/crypto/hmac/hmac.c
index 45015fe..55ad15c 100644
--- a/crypto/hmac/hmac.c
+++ b/crypto/hmac/hmac.c
@@ -66,6 +66,7 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
        {
        int i,j,reset=0;
        unsigned char pad[HMAC_MAX_MD_CBLOCK];
+       unsigned long flags;
 
        if (md != NULL)
                {
@@ -82,6 +83,7 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
                OPENSSL_assert(j <= (int)sizeof(ctx->key));
                if (j < len)
                        {
+                       EVP_MD_CTX_set_flags(&ctx->md_ctx, 
EVP_MD_CTX_FLAG_ONESHOT);
                        if (!EVP_DigestInit_ex(&ctx->md_ctx,md, impl))
                                goto err;
                        if (!EVP_DigestUpdate(&ctx->md_ctx,key,len))
@@ -105,17 +107,22 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
                {
                for (i=0; i<HMAC_MAX_MD_CBLOCK; i++)
                        pad[i]=0x36^ctx->key[i];
+               flags = EVP_MD_CTX_test_flags(&ctx->i_ctx, 
EVP_MD_CTX_FLAG_ONESHOT);
+               EVP_MD_CTX_clear_flags(&ctx->i_ctx, EVP_MD_CTX_FLAG_ONESHOT);
                if (!EVP_DigestInit_ex(&ctx->i_ctx,md, impl))
                        goto err;
                if (!EVP_DigestUpdate(&ctx->i_ctx,pad,EVP_MD_block_size(md)))
                        goto err;
+               EVP_MD_CTX_set_flags(&ctx->i_ctx, flags);
 
                for (i=0; i<HMAC_MAX_MD_CBLOCK; i++)
                        pad[i]=0x5c^ctx->key[i];
+               EVP_MD_CTX_clear_flags(&ctx->o_ctx, EVP_MD_CTX_FLAG_ONESHOT);
                if (!EVP_DigestInit_ex(&ctx->o_ctx,md, impl))
                        goto err;
                if (!EVP_DigestUpdate(&ctx->o_ctx,pad,EVP_MD_block_size(md)))
                        goto err;
+               EVP_MD_CTX_set_flags(&ctx->o_ctx, EVP_MD_CTX_FLAG_ONESHOT);
                }
        if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->i_ctx))
                        goto err;
@@ -197,7 +204,8 @@ unsigned char *HMAC(const EVP_MD *evp_md, const void *key, 
int key_len,
 
        if (md == NULL) md=m;
        HMAC_CTX_init(&c);
-       if (!HMAC_Init(&c,key,key_len,evp_md))
+       HMAC_CTX_set_flags(&c, EVP_MD_CTX_FLAG_ONESHOT);
+       if (!HMAC_Init_ex(&c,key,key_len,evp_md,NULL))
                goto err;
        if (!HMAC_Update(&c,d,n))
                goto err;
@@ -212,6 +220,6 @@ unsigned char *HMAC(const EVP_MD *evp_md, const void *key, 
int key_len,
 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
        {
        EVP_MD_CTX_set_flags(&ctx->i_ctx, flags);
-       EVP_MD_CTX_set_flags(&ctx->o_ctx, flags);
-       EVP_MD_CTX_set_flags(&ctx->md_ctx, flags);
+       EVP_MD_CTX_set_flags(&ctx->o_ctx, flags & ~EVP_MD_CTX_FLAG_ONESHOT);
+       EVP_MD_CTX_set_flags(&ctx->md_ctx, flags & ~EVP_MD_CTX_FLAG_ONESHOT);
        }
-- 
1.7.0.4

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [email protected]
Automated List Manager                           [email protected]

Reply via email to