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