OLD | NEW |
1 /* crypto/hmac/hmac.c */ | 1 /* crypto/hmac/hmac.c */ |
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 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 * copied and put under another distribution licence | 55 * copied and put under another distribution licence |
56 * [including the GNU Public Licence.] | 56 * [including the GNU Public Licence.] |
57 */ | 57 */ |
58 #include <stdio.h> | 58 #include <stdio.h> |
59 #include <stdlib.h> | 59 #include <stdlib.h> |
60 #include <string.h> | 60 #include <string.h> |
61 #include "cryptlib.h" | 61 #include "cryptlib.h" |
62 #include <openssl/hmac.h> | 62 #include <openssl/hmac.h> |
63 | 63 |
64 #ifndef OPENSSL_FIPS | 64 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, |
65 | |
66 void 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; |
70 unsigned char pad[HMAC_MAX_MD_CBLOCK]; | 68 unsigned char pad[HMAC_MAX_MD_CBLOCK]; |
71 | 69 |
72 if (md != NULL) | 70 if (md != NULL) |
73 { | 71 { |
74 reset=1; | 72 reset=1; |
75 ctx->md=md; | 73 ctx->md=md; |
76 } | 74 } |
77 else | 75 else |
78 md=ctx->md; | 76 md=ctx->md; |
79 | 77 |
80 if (key != NULL) | 78 if (key != NULL) |
81 { | 79 { |
82 reset=1; | 80 reset=1; |
83 j=EVP_MD_block_size(md); | 81 j=EVP_MD_block_size(md); |
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 { |
94 OPENSSL_assert(len>=0 && len<=(int)sizeof(ctx->key)); | 95 OPENSSL_assert(len>=0 && len<=(int)sizeof(ctx->key)); |
95 memcpy(ctx->key,key,len); | 96 memcpy(ctx->key,key,len); |
96 ctx->key_length=len; | 97 ctx->key_length=len; |
97 } | 98 } |
98 if(ctx->key_length != HMAC_MAX_MD_CBLOCK) | 99 if(ctx->key_length != HMAC_MAX_MD_CBLOCK) |
99 memset(&ctx->key[ctx->key_length], 0, | 100 memset(&ctx->key[ctx->key_length], 0, |
100 HMAC_MAX_MD_CBLOCK - ctx->key_length); | 101 HMAC_MAX_MD_CBLOCK - ctx->key_length); |
101 } | 102 } |
102 | 103 |
103 if (reset) | 104 if (reset) |
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 |
118 void HMAC_Init(HMAC_CTX *ctx, const void *key, int len, | 127 int 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 |
126 void HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) | 134 int 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 |
131 void HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) | 139 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) |
132 { | 140 { |
133 int j; | |
134 unsigned int i; | 141 unsigned int i; |
135 unsigned char buf[EVP_MAX_MD_SIZE]; | 142 unsigned char buf[EVP_MAX_MD_SIZE]; |
136 | 143 |
137 » j=EVP_MD_block_size(ctx->md); | 144 » if (!EVP_DigestFinal_ex(&ctx->md_ctx,buf,&i)) |
138 | 145 » » goto err; |
139 » EVP_DigestFinal_ex(&ctx->md_ctx,buf,&i); | 146 » if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->o_ctx)) |
140 » EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->o_ctx); | 147 » » goto err; |
141 » EVP_DigestUpdate(&ctx->md_ctx,buf,i); | 148 » if (!EVP_DigestUpdate(&ctx->md_ctx,buf,i)) |
142 » EVP_DigestFinal_ex(&ctx->md_ctx,md,len); | 149 » » goto err; |
| 150 » if (!EVP_DigestFinal_ex(&ctx->md_ctx,md,len)) |
| 151 » » goto err; |
| 152 » return 1; |
| 153 » err: |
| 154 » return 0; |
143 } | 155 } |
144 | 156 |
145 void HMAC_CTX_init(HMAC_CTX *ctx) | 157 void HMAC_CTX_init(HMAC_CTX *ctx) |
146 { | 158 { |
147 EVP_MD_CTX_init(&ctx->i_ctx); | 159 EVP_MD_CTX_init(&ctx->i_ctx); |
148 EVP_MD_CTX_init(&ctx->o_ctx); | 160 EVP_MD_CTX_init(&ctx->o_ctx); |
149 EVP_MD_CTX_init(&ctx->md_ctx); | 161 EVP_MD_CTX_init(&ctx->md_ctx); |
150 } | 162 } |
151 | 163 |
| 164 int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) |
| 165 { |
| 166 if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx)) |
| 167 goto err; |
| 168 if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx)) |
| 169 goto err; |
| 170 if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx)) |
| 171 goto err; |
| 172 memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK); |
| 173 dctx->key_length = sctx->key_length; |
| 174 dctx->md = sctx->md; |
| 175 return 1; |
| 176 err: |
| 177 return 0; |
| 178 } |
| 179 |
152 void HMAC_CTX_cleanup(HMAC_CTX *ctx) | 180 void HMAC_CTX_cleanup(HMAC_CTX *ctx) |
153 { | 181 { |
154 EVP_MD_CTX_cleanup(&ctx->i_ctx); | 182 EVP_MD_CTX_cleanup(&ctx->i_ctx); |
155 EVP_MD_CTX_cleanup(&ctx->o_ctx); | 183 EVP_MD_CTX_cleanup(&ctx->o_ctx); |
156 EVP_MD_CTX_cleanup(&ctx->md_ctx); | 184 EVP_MD_CTX_cleanup(&ctx->md_ctx); |
157 memset(ctx,0,sizeof *ctx); | 185 memset(ctx,0,sizeof *ctx); |
158 } | 186 } |
159 | 187 |
160 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, | 188 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, |
161 const unsigned char *d, size_t n, unsigned char *md, | 189 const unsigned char *d, size_t n, unsigned char *md, |
162 unsigned int *md_len) | 190 unsigned int *md_len) |
163 { | 191 { |
164 HMAC_CTX c; | 192 HMAC_CTX c; |
165 static unsigned char m[EVP_MAX_MD_SIZE]; | 193 static unsigned char m[EVP_MAX_MD_SIZE]; |
166 | 194 |
167 if (md == NULL) md=m; | 195 if (md == NULL) md=m; |
168 HMAC_CTX_init(&c); | 196 HMAC_CTX_init(&c); |
169 » HMAC_Init(&c,key,key_len,evp_md); | 197 » if (!HMAC_Init(&c,key,key_len,evp_md)) |
170 » HMAC_Update(&c,d,n); | 198 » » goto err; |
171 » HMAC_Final(&c,md,md_len); | 199 » if (!HMAC_Update(&c,d,n)) |
| 200 » » goto err; |
| 201 » if (!HMAC_Final(&c,md,md_len)) |
| 202 » » goto err; |
172 HMAC_CTX_cleanup(&c); | 203 HMAC_CTX_cleanup(&c); |
173 » return(md); | 204 » return md; |
| 205 » err: |
| 206 » return NULL; |
174 } | 207 } |
175 | 208 |
176 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) | 209 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) |
177 { | 210 { |
178 EVP_MD_CTX_set_flags(&ctx->i_ctx, flags); | 211 EVP_MD_CTX_set_flags(&ctx->i_ctx, flags); |
179 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags); | 212 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags); |
180 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags); | 213 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags); |
181 } | 214 } |
182 | |
183 #endif | |
OLD | NEW |