summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/hmac/hmac.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/hmac/hmac.c')
-rw-r--r--src/lib/libcrypto/hmac/hmac.c90
1 files changed, 62 insertions, 28 deletions
diff --git a/src/lib/libcrypto/hmac/hmac.c b/src/lib/libcrypto/hmac/hmac.c
index cbc1c76a57..45015fe754 100644
--- a/src/lib/libcrypto/hmac/hmac.c
+++ b/src/lib/libcrypto/hmac/hmac.c
@@ -61,9 +61,7 @@
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include <openssl/hmac.h> 62#include <openssl/hmac.h>
63 63
64#ifndef OPENSSL_FIPS 64int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
65
66void HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
67 const EVP_MD *md, ENGINE *impl) 65 const EVP_MD *md, ENGINE *impl)
68 { 66 {
69 int i,j,reset=0; 67 int i,j,reset=0;
@@ -84,10 +82,13 @@ void HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
84 OPENSSL_assert(j <= (int)sizeof(ctx->key)); 82 OPENSSL_assert(j <= (int)sizeof(ctx->key));
85 if (j < len) 83 if (j < len)
86 { 84 {
87 EVP_DigestInit_ex(&ctx->md_ctx,md, impl); 85 if (!EVP_DigestInit_ex(&ctx->md_ctx,md, impl))
88 EVP_DigestUpdate(&ctx->md_ctx,key,len); 86 goto err;
89 EVP_DigestFinal_ex(&(ctx->md_ctx),ctx->key, 87 if (!EVP_DigestUpdate(&ctx->md_ctx,key,len))
90 &ctx->key_length); 88 goto err;
89 if (!EVP_DigestFinal_ex(&(ctx->md_ctx),ctx->key,
90 &ctx->key_length))
91 goto err;
91 } 92 }
92 else 93 else
93 { 94 {
@@ -104,31 +105,38 @@ void HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
104 { 105 {
105 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++) 106 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++)
106 pad[i]=0x36^ctx->key[i]; 107 pad[i]=0x36^ctx->key[i];
107 EVP_DigestInit_ex(&ctx->i_ctx,md, impl); 108 if (!EVP_DigestInit_ex(&ctx->i_ctx,md, impl))
108 EVP_DigestUpdate(&ctx->i_ctx,pad,EVP_MD_block_size(md)); 109 goto err;
110 if (!EVP_DigestUpdate(&ctx->i_ctx,pad,EVP_MD_block_size(md)))
111 goto err;
109 112
110 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++) 113 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++)
111 pad[i]=0x5c^ctx->key[i]; 114 pad[i]=0x5c^ctx->key[i];
112 EVP_DigestInit_ex(&ctx->o_ctx,md, impl); 115 if (!EVP_DigestInit_ex(&ctx->o_ctx,md, impl))
113 EVP_DigestUpdate(&ctx->o_ctx,pad,EVP_MD_block_size(md)); 116 goto err;
117 if (!EVP_DigestUpdate(&ctx->o_ctx,pad,EVP_MD_block_size(md)))
118 goto err;
114 } 119 }
115 EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->i_ctx); 120 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->i_ctx))
121 goto err;
122 return 1;
123 err:
124 return 0;
116 } 125 }
117 126
118void HMAC_Init(HMAC_CTX *ctx, const void *key, int len, 127int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
119 const EVP_MD *md)
120 { 128 {
121 if(key && md) 129 if(key && md)
122 HMAC_CTX_init(ctx); 130 HMAC_CTX_init(ctx);
123 HMAC_Init_ex(ctx,key,len,md, NULL); 131 return HMAC_Init_ex(ctx,key,len,md, NULL);
124 } 132 }
125 133
126void HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) 134int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
127 { 135 {
128 EVP_DigestUpdate(&ctx->md_ctx,data,len); 136 return EVP_DigestUpdate(&ctx->md_ctx,data,len);
129 } 137 }
130 138
131void HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) 139int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
132 { 140 {
133 int j; 141 int j;
134 unsigned int i; 142 unsigned int i;
@@ -136,10 +144,17 @@ void HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
136 144
137 j=EVP_MD_block_size(ctx->md); 145 j=EVP_MD_block_size(ctx->md);
138 146
139 EVP_DigestFinal_ex(&ctx->md_ctx,buf,&i); 147 if (!EVP_DigestFinal_ex(&ctx->md_ctx,buf,&i))
140 EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->o_ctx); 148 goto err;
141 EVP_DigestUpdate(&ctx->md_ctx,buf,i); 149 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->o_ctx))
142 EVP_DigestFinal_ex(&ctx->md_ctx,md,len); 150 goto err;
151 if (!EVP_DigestUpdate(&ctx->md_ctx,buf,i))
152 goto err;
153 if (!EVP_DigestFinal_ex(&ctx->md_ctx,md,len))
154 goto err;
155 return 1;
156 err:
157 return 0;
143 } 158 }
144 159
145void HMAC_CTX_init(HMAC_CTX *ctx) 160void HMAC_CTX_init(HMAC_CTX *ctx)
@@ -149,6 +164,22 @@ void HMAC_CTX_init(HMAC_CTX *ctx)
149 EVP_MD_CTX_init(&ctx->md_ctx); 164 EVP_MD_CTX_init(&ctx->md_ctx);
150 } 165 }
151 166
167int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
168 {
169 if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx))
170 goto err;
171 if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx))
172 goto err;
173 if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
174 goto err;
175 memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
176 dctx->key_length = sctx->key_length;
177 dctx->md = sctx->md;
178 return 1;
179 err:
180 return 0;
181 }
182
152void HMAC_CTX_cleanup(HMAC_CTX *ctx) 183void HMAC_CTX_cleanup(HMAC_CTX *ctx)
153 { 184 {
154 EVP_MD_CTX_cleanup(&ctx->i_ctx); 185 EVP_MD_CTX_cleanup(&ctx->i_ctx);
@@ -166,11 +197,16 @@ unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
166 197
167 if (md == NULL) md=m; 198 if (md == NULL) md=m;
168 HMAC_CTX_init(&c); 199 HMAC_CTX_init(&c);
169 HMAC_Init(&c,key,key_len,evp_md); 200 if (!HMAC_Init(&c,key,key_len,evp_md))
170 HMAC_Update(&c,d,n); 201 goto err;
171 HMAC_Final(&c,md,md_len); 202 if (!HMAC_Update(&c,d,n))
203 goto err;
204 if (!HMAC_Final(&c,md,md_len))
205 goto err;
172 HMAC_CTX_cleanup(&c); 206 HMAC_CTX_cleanup(&c);
173 return(md); 207 return md;
208 err:
209 return NULL;
174 } 210 }
175 211
176void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) 212void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
@@ -179,5 +215,3 @@ void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
179 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags); 215 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags);
180 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags); 216 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags);
181 } 217 }
182
183#endif