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.c166
1 files changed, 84 insertions, 82 deletions
diff --git a/src/lib/libcrypto/hmac/hmac.c b/src/lib/libcrypto/hmac/hmac.c
index 690715db2c..0c29156d80 100644
--- a/src/lib/libcrypto/hmac/hmac.c
+++ b/src/lib/libcrypto/hmac/hmac.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: hmac.c,v 1.18 2014/06/12 15:49:29 deraadt Exp $ */ 1/* $OpenBSD: hmac.c,v 1.19 2014/06/21 12:00:01 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -61,108 +61,107 @@
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, 64int
65 const EVP_MD *md, ENGINE *impl) 65HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md,
66 { 66 ENGINE *impl)
67 int i,j,reset=0; 67{
68 int i, j, reset = 0;
68 unsigned char pad[HMAC_MAX_MD_CBLOCK]; 69 unsigned char pad[HMAC_MAX_MD_CBLOCK];
69 70
70 if (md != NULL) 71 if (md != NULL) {
71 { 72 reset = 1;
72 reset=1; 73 ctx->md = md;
73 ctx->md=md; 74 } else
74 } 75 md = ctx->md;
75 else
76 md=ctx->md;
77 76
78 if (key != NULL) 77 if (key != NULL) {
79 { 78 reset = 1;
80 reset=1; 79 j = EVP_MD_block_size(md);
81 j=EVP_MD_block_size(md);
82 OPENSSL_assert(j <= (int)sizeof(ctx->key)); 80 OPENSSL_assert(j <= (int)sizeof(ctx->key));
83 if (j < len) 81 if (j < len) {
84 { 82 if (!EVP_DigestInit_ex(&ctx->md_ctx, md, impl))
85 if (!EVP_DigestInit_ex(&ctx->md_ctx,md, impl))
86 goto err; 83 goto err;
87 if (!EVP_DigestUpdate(&ctx->md_ctx,key,len)) 84 if (!EVP_DigestUpdate(&ctx->md_ctx, key, len))
88 goto err; 85 goto err;
89 if (!EVP_DigestFinal_ex(&(ctx->md_ctx),ctx->key, 86 if (!EVP_DigestFinal_ex(&(ctx->md_ctx), ctx->key,
90 &ctx->key_length)) 87 &ctx->key_length))
91 goto err; 88 goto err;
92 } 89 } else {
93 else
94 {
95 OPENSSL_assert(len>=0 && len<=(int)sizeof(ctx->key)); 90 OPENSSL_assert(len>=0 && len<=(int)sizeof(ctx->key));
96 memcpy(ctx->key,key,len); 91 memcpy(ctx->key,key,len);
97 ctx->key_length=len; 92 ctx->key_length = len;
98 } 93 }
99 if(ctx->key_length != HMAC_MAX_MD_CBLOCK) 94 if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
100 memset(&ctx->key[ctx->key_length], 0, 95 memset(&ctx->key[ctx->key_length], 0,
101 HMAC_MAX_MD_CBLOCK - ctx->key_length); 96 HMAC_MAX_MD_CBLOCK - ctx->key_length);
102 } 97 }
103 98
104 if (reset) 99 if (reset) {
105 { 100 for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
106 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++) 101 pad[i] = 0x36 ^ ctx->key[i];
107 pad[i]=0x36^ctx->key[i]; 102 if (!EVP_DigestInit_ex(&ctx->i_ctx, md, impl))
108 if (!EVP_DigestInit_ex(&ctx->i_ctx,md, impl))
109 goto err; 103 goto err;
110 if (!EVP_DigestUpdate(&ctx->i_ctx,pad,EVP_MD_block_size(md))) 104 if (!EVP_DigestUpdate(&ctx->i_ctx, pad, EVP_MD_block_size(md)))
111 goto err; 105 goto err;
112 106
113 for (i=0; i<HMAC_MAX_MD_CBLOCK; i++) 107 for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
114 pad[i]=0x5c^ctx->key[i]; 108 pad[i] = 0x5c ^ ctx->key[i];
115 if (!EVP_DigestInit_ex(&ctx->o_ctx,md, impl)) 109 if (!EVP_DigestInit_ex(&ctx->o_ctx, md, impl))
116 goto err; 110 goto err;
117 if (!EVP_DigestUpdate(&ctx->o_ctx,pad,EVP_MD_block_size(md))) 111 if (!EVP_DigestUpdate(&ctx->o_ctx, pad, EVP_MD_block_size(md)))
118 goto err;
119 }
120 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->i_ctx))
121 goto err; 112 goto err;
113 }
114 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->i_ctx))
115 goto err;
122 return 1; 116 return 1;
123 err: 117err:
124 return 0; 118 return 0;
125 } 119}
126 120
127int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md) 121int
128 { 122HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
129 if(key && md) 123{
124 if (key && md)
130 HMAC_CTX_init(ctx); 125 HMAC_CTX_init(ctx);
131 return HMAC_Init_ex(ctx,key,len,md, NULL); 126 return HMAC_Init_ex(ctx,key,len,md, NULL);
132 } 127}
133 128
134int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) 129int
135 { 130HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
131{
136 return EVP_DigestUpdate(&ctx->md_ctx,data,len); 132 return EVP_DigestUpdate(&ctx->md_ctx,data,len);
137 } 133}
138 134
139int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) 135int
140 { 136HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
137{
141 unsigned int i; 138 unsigned int i;
142 unsigned char buf[EVP_MAX_MD_SIZE]; 139 unsigned char buf[EVP_MAX_MD_SIZE];
143 140
144 if (!EVP_DigestFinal_ex(&ctx->md_ctx,buf,&i)) 141 if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i))
145 goto err; 142 goto err;
146 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->o_ctx)) 143 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx))
147 goto err; 144 goto err;
148 if (!EVP_DigestUpdate(&ctx->md_ctx,buf,i)) 145 if (!EVP_DigestUpdate(&ctx->md_ctx, buf, i))
149 goto err; 146 goto err;
150 if (!EVP_DigestFinal_ex(&ctx->md_ctx,md,len)) 147 if (!EVP_DigestFinal_ex(&ctx->md_ctx, md, len))
151 goto err; 148 goto err;
152 return 1; 149 return 1;
153 err: 150err:
154 return 0; 151 return 0;
155 } 152}
156 153
157void HMAC_CTX_init(HMAC_CTX *ctx) 154void
158 { 155HMAC_CTX_init(HMAC_CTX *ctx)
156{
159 EVP_MD_CTX_init(&ctx->i_ctx); 157 EVP_MD_CTX_init(&ctx->i_ctx);
160 EVP_MD_CTX_init(&ctx->o_ctx); 158 EVP_MD_CTX_init(&ctx->o_ctx);
161 EVP_MD_CTX_init(&ctx->md_ctx); 159 EVP_MD_CTX_init(&ctx->md_ctx);
162 } 160}
163 161
164int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) 162int
165 { 163HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
164{
166 if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx)) 165 if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx))
167 goto err; 166 goto err;
168 if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx)) 167 if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx))
@@ -173,42 +172,45 @@ int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
173 dctx->key_length = sctx->key_length; 172 dctx->key_length = sctx->key_length;
174 dctx->md = sctx->md; 173 dctx->md = sctx->md;
175 return 1; 174 return 1;
176 err: 175err:
177 return 0; 176 return 0;
178 } 177}
179 178
180void HMAC_CTX_cleanup(HMAC_CTX *ctx) 179void
181 { 180HMAC_CTX_cleanup(HMAC_CTX *ctx)
181{
182 EVP_MD_CTX_cleanup(&ctx->i_ctx); 182 EVP_MD_CTX_cleanup(&ctx->i_ctx);
183 EVP_MD_CTX_cleanup(&ctx->o_ctx); 183 EVP_MD_CTX_cleanup(&ctx->o_ctx);
184 EVP_MD_CTX_cleanup(&ctx->md_ctx); 184 EVP_MD_CTX_cleanup(&ctx->md_ctx);
185 memset(ctx,0,sizeof *ctx); 185 memset(ctx, 0, sizeof *ctx);
186 } 186}
187 187
188unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, 188unsigned char *
189 const unsigned char *d, size_t n, unsigned char *md, 189HMAC(const EVP_MD *evp_md, const void *key, int key_len, const unsigned char *d,
190 unsigned int *md_len) 190 size_t n, unsigned char *md, unsigned int *md_len)
191 { 191{
192 HMAC_CTX c; 192 HMAC_CTX c;
193 static unsigned char m[EVP_MAX_MD_SIZE]; 193 static unsigned char m[EVP_MAX_MD_SIZE];
194 194
195 if (md == NULL) md=m; 195 if (md == NULL)
196 md = m;
196 HMAC_CTX_init(&c); 197 HMAC_CTX_init(&c);
197 if (!HMAC_Init(&c,key,key_len,evp_md)) 198 if (!HMAC_Init(&c, key, key_len, evp_md))
198 goto err; 199 goto err;
199 if (!HMAC_Update(&c,d,n)) 200 if (!HMAC_Update(&c, d, n))
200 goto err; 201 goto err;
201 if (!HMAC_Final(&c,md,md_len)) 202 if (!HMAC_Final(&c, md, md_len))
202 goto err; 203 goto err;
203 HMAC_CTX_cleanup(&c); 204 HMAC_CTX_cleanup(&c);
204 return md; 205 return md;
205 err: 206err:
206 return NULL; 207 return NULL;
207 } 208}
208 209
209void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) 210void
210 { 211HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
212{
211 EVP_MD_CTX_set_flags(&ctx->i_ctx, flags); 213 EVP_MD_CTX_set_flags(&ctx->i_ctx, flags);
212 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags); 214 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags);
213 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags); 215 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags);
214 } 216}