diff options
author | djm <> | 2012-10-13 21:23:50 +0000 |
---|---|---|
committer | djm <> | 2012-10-13 21:23:50 +0000 |
commit | e9d65189905c6e99c1062d65e26bf83eebb0a26a (patch) | |
tree | 10ebe51c3542099b0ab8325d8f322372375dc3b4 /src/lib/libcrypto/evp | |
parent | 59625e84c89bf82e1c6d20c55785b618eb56ea72 (diff) | |
parent | 228cae30b117c2493f69ad3c195341cd6ec8d430 (diff) | |
download | openbsd-e9d65189905c6e99c1062d65e26bf83eebb0a26a.tar.gz openbsd-e9d65189905c6e99c1062d65e26bf83eebb0a26a.tar.bz2 openbsd-e9d65189905c6e99c1062d65e26bf83eebb0a26a.zip |
This commit was generated by cvs2git to track changes on a CVS vendor
branch.
Diffstat (limited to 'src/lib/libcrypto/evp')
-rw-r--r-- | src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c | 406 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/e_rc4_hmac_md5.c | 298 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/m_ecdsa.c | 3 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/m_wp.c | 1 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/pmeth_gn.c | 5 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/pmeth_lib.c | 55 |
6 files changed, 765 insertions, 3 deletions
diff --git a/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c b/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c new file mode 100644 index 0000000000..710fb79baf --- /dev/null +++ b/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c | |||
@@ -0,0 +1,406 @@ | |||
1 | /* ==================================================================== | ||
2 | * Copyright (c) 2011 The OpenSSL Project. All rights reserved. | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in | ||
13 | * the documentation and/or other materials provided with the | ||
14 | * distribution. | ||
15 | * | ||
16 | * 3. All advertising materials mentioning features or use of this | ||
17 | * software must display the following acknowledgment: | ||
18 | * "This product includes software developed by the OpenSSL Project | ||
19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
20 | * | ||
21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
22 | * endorse or promote products derived from this software without | ||
23 | * prior written permission. For written permission, please contact | ||
24 | * licensing@OpenSSL.org. | ||
25 | * | ||
26 | * 5. Products derived from this software may not be called "OpenSSL" | ||
27 | * nor may "OpenSSL" appear in their names without prior written | ||
28 | * permission of the OpenSSL Project. | ||
29 | * | ||
30 | * 6. Redistributions of any form whatsoever must retain the following | ||
31 | * acknowledgment: | ||
32 | * "This product includes software developed by the OpenSSL Project | ||
33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
34 | * | ||
35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
46 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
47 | * ==================================================================== | ||
48 | */ | ||
49 | |||
50 | #include <openssl/opensslconf.h> | ||
51 | |||
52 | #include <stdio.h> | ||
53 | #include <string.h> | ||
54 | |||
55 | #if !defined(OPENSSL_NO_AES) && !defined(OPENSSL_NO_SHA1) | ||
56 | |||
57 | #include <openssl/evp.h> | ||
58 | #include <openssl/objects.h> | ||
59 | #include <openssl/aes.h> | ||
60 | #include <openssl/sha.h> | ||
61 | #include "evp_locl.h" | ||
62 | |||
63 | #ifndef EVP_CIPH_FLAG_AEAD_CIPHER | ||
64 | #define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000 | ||
65 | #define EVP_CTRL_AEAD_TLS1_AAD 0x16 | ||
66 | #define EVP_CTRL_AEAD_SET_MAC_KEY 0x17 | ||
67 | #endif | ||
68 | |||
69 | #if !defined(EVP_CIPH_FLAG_DEFAULT_ASN1) | ||
70 | #define EVP_CIPH_FLAG_DEFAULT_ASN1 0 | ||
71 | #endif | ||
72 | |||
73 | #define TLS1_1_VERSION 0x0302 | ||
74 | |||
75 | typedef struct | ||
76 | { | ||
77 | AES_KEY ks; | ||
78 | SHA_CTX head,tail,md; | ||
79 | size_t payload_length; /* AAD length in decrypt case */ | ||
80 | union { | ||
81 | unsigned int tls_ver; | ||
82 | unsigned char tls_aad[16]; /* 13 used */ | ||
83 | } aux; | ||
84 | } EVP_AES_HMAC_SHA1; | ||
85 | |||
86 | #define NO_PAYLOAD_LENGTH ((size_t)-1) | ||
87 | |||
88 | #if defined(AES_ASM) && ( \ | ||
89 | defined(__x86_64) || defined(__x86_64__) || \ | ||
90 | defined(_M_AMD64) || defined(_M_X64) || \ | ||
91 | defined(__INTEL__) ) | ||
92 | |||
93 | extern unsigned int OPENSSL_ia32cap_P[2]; | ||
94 | #define AESNI_CAPABLE (1<<(57-32)) | ||
95 | |||
96 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, | ||
97 | AES_KEY *key); | ||
98 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, | ||
99 | AES_KEY *key); | ||
100 | |||
101 | void aesni_cbc_encrypt(const unsigned char *in, | ||
102 | unsigned char *out, | ||
103 | size_t length, | ||
104 | const AES_KEY *key, | ||
105 | unsigned char *ivec, int enc); | ||
106 | |||
107 | void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks, | ||
108 | const AES_KEY *key, unsigned char iv[16], | ||
109 | SHA_CTX *ctx,const void *in0); | ||
110 | |||
111 | #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data) | ||
112 | |||
113 | static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, | ||
114 | const unsigned char *inkey, | ||
115 | const unsigned char *iv, int enc) | ||
116 | { | ||
117 | EVP_AES_HMAC_SHA1 *key = data(ctx); | ||
118 | int ret; | ||
119 | |||
120 | if (enc) | ||
121 | ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks); | ||
122 | else | ||
123 | ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks); | ||
124 | |||
125 | SHA1_Init(&key->head); /* handy when benchmarking */ | ||
126 | key->tail = key->head; | ||
127 | key->md = key->head; | ||
128 | |||
129 | key->payload_length = NO_PAYLOAD_LENGTH; | ||
130 | |||
131 | return ret<0?0:1; | ||
132 | } | ||
133 | |||
134 | #define STITCHED_CALL | ||
135 | |||
136 | #if !defined(STITCHED_CALL) | ||
137 | #define aes_off 0 | ||
138 | #endif | ||
139 | |||
140 | void sha1_block_data_order (void *c,const void *p,size_t len); | ||
141 | |||
142 | static void sha1_update(SHA_CTX *c,const void *data,size_t len) | ||
143 | { const unsigned char *ptr = data; | ||
144 | size_t res; | ||
145 | |||
146 | if ((res = c->num)) { | ||
147 | res = SHA_CBLOCK-res; | ||
148 | if (len<res) res=len; | ||
149 | SHA1_Update (c,ptr,res); | ||
150 | ptr += res; | ||
151 | len -= res; | ||
152 | } | ||
153 | |||
154 | res = len % SHA_CBLOCK; | ||
155 | len -= res; | ||
156 | |||
157 | if (len) { | ||
158 | sha1_block_data_order(c,ptr,len/SHA_CBLOCK); | ||
159 | |||
160 | ptr += len; | ||
161 | c->Nh += len>>29; | ||
162 | c->Nl += len<<=3; | ||
163 | if (c->Nl<(unsigned int)len) c->Nh++; | ||
164 | } | ||
165 | |||
166 | if (res) | ||
167 | SHA1_Update(c,ptr,res); | ||
168 | } | ||
169 | |||
170 | #define SHA1_Update sha1_update | ||
171 | |||
172 | static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
173 | const unsigned char *in, size_t len) | ||
174 | { | ||
175 | EVP_AES_HMAC_SHA1 *key = data(ctx); | ||
176 | unsigned int l; | ||
177 | size_t plen = key->payload_length, | ||
178 | iv = 0, /* explicit IV in TLS 1.1 and later */ | ||
179 | sha_off = 0; | ||
180 | #if defined(STITCHED_CALL) | ||
181 | size_t aes_off = 0, | ||
182 | blocks; | ||
183 | |||
184 | sha_off = SHA_CBLOCK-key->md.num; | ||
185 | #endif | ||
186 | |||
187 | if (len%AES_BLOCK_SIZE) return 0; | ||
188 | |||
189 | if (ctx->encrypt) { | ||
190 | if (plen==NO_PAYLOAD_LENGTH) | ||
191 | plen = len; | ||
192 | else if (len!=((plen+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)) | ||
193 | return 0; | ||
194 | else if (key->aux.tls_ver >= TLS1_1_VERSION) | ||
195 | iv = AES_BLOCK_SIZE; | ||
196 | |||
197 | #if defined(STITCHED_CALL) | ||
198 | if (plen>(sha_off+iv) && (blocks=(plen-(sha_off+iv))/SHA_CBLOCK)) { | ||
199 | SHA1_Update(&key->md,in+iv,sha_off); | ||
200 | |||
201 | aesni_cbc_sha1_enc(in,out,blocks,&key->ks, | ||
202 | ctx->iv,&key->md,in+iv+sha_off); | ||
203 | blocks *= SHA_CBLOCK; | ||
204 | aes_off += blocks; | ||
205 | sha_off += blocks; | ||
206 | key->md.Nh += blocks>>29; | ||
207 | key->md.Nl += blocks<<=3; | ||
208 | if (key->md.Nl<(unsigned int)blocks) key->md.Nh++; | ||
209 | } else { | ||
210 | sha_off = 0; | ||
211 | } | ||
212 | #endif | ||
213 | sha_off += iv; | ||
214 | SHA1_Update(&key->md,in+sha_off,plen-sha_off); | ||
215 | |||
216 | if (plen!=len) { /* "TLS" mode of operation */ | ||
217 | if (in!=out) | ||
218 | memcpy(out+aes_off,in+aes_off,plen-aes_off); | ||
219 | |||
220 | /* calculate HMAC and append it to payload */ | ||
221 | SHA1_Final(out+plen,&key->md); | ||
222 | key->md = key->tail; | ||
223 | SHA1_Update(&key->md,out+plen,SHA_DIGEST_LENGTH); | ||
224 | SHA1_Final(out+plen,&key->md); | ||
225 | |||
226 | /* pad the payload|hmac */ | ||
227 | plen += SHA_DIGEST_LENGTH; | ||
228 | for (l=len-plen-1;plen<len;plen++) out[plen]=l; | ||
229 | /* encrypt HMAC|padding at once */ | ||
230 | aesni_cbc_encrypt(out+aes_off,out+aes_off,len-aes_off, | ||
231 | &key->ks,ctx->iv,1); | ||
232 | } else { | ||
233 | aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off, | ||
234 | &key->ks,ctx->iv,1); | ||
235 | } | ||
236 | } else { | ||
237 | unsigned char mac[SHA_DIGEST_LENGTH]; | ||
238 | |||
239 | /* decrypt HMAC|padding at once */ | ||
240 | aesni_cbc_encrypt(in,out,len, | ||
241 | &key->ks,ctx->iv,0); | ||
242 | |||
243 | if (plen) { /* "TLS" mode of operation */ | ||
244 | /* figure out payload length */ | ||
245 | if (len<(size_t)(out[len-1]+1+SHA_DIGEST_LENGTH)) | ||
246 | return 0; | ||
247 | |||
248 | len -= (out[len-1]+1+SHA_DIGEST_LENGTH); | ||
249 | |||
250 | if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3]) | ||
251 | >= TLS1_1_VERSION) { | ||
252 | len -= AES_BLOCK_SIZE; | ||
253 | iv = AES_BLOCK_SIZE; | ||
254 | } | ||
255 | |||
256 | key->aux.tls_aad[plen-2] = len>>8; | ||
257 | key->aux.tls_aad[plen-1] = len; | ||
258 | |||
259 | /* calculate HMAC and verify it */ | ||
260 | key->md = key->head; | ||
261 | SHA1_Update(&key->md,key->aux.tls_aad,plen); | ||
262 | SHA1_Update(&key->md,out+iv,len); | ||
263 | SHA1_Final(mac,&key->md); | ||
264 | |||
265 | key->md = key->tail; | ||
266 | SHA1_Update(&key->md,mac,SHA_DIGEST_LENGTH); | ||
267 | SHA1_Final(mac,&key->md); | ||
268 | |||
269 | if (memcmp(out+iv+len,mac,SHA_DIGEST_LENGTH)) | ||
270 | return 0; | ||
271 | } else { | ||
272 | SHA1_Update(&key->md,out,len); | ||
273 | } | ||
274 | } | ||
275 | |||
276 | key->payload_length = NO_PAYLOAD_LENGTH; | ||
277 | |||
278 | return 1; | ||
279 | } | ||
280 | |||
281 | static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) | ||
282 | { | ||
283 | EVP_AES_HMAC_SHA1 *key = data(ctx); | ||
284 | |||
285 | switch (type) | ||
286 | { | ||
287 | case EVP_CTRL_AEAD_SET_MAC_KEY: | ||
288 | { | ||
289 | unsigned int i; | ||
290 | unsigned char hmac_key[64]; | ||
291 | |||
292 | memset (hmac_key,0,sizeof(hmac_key)); | ||
293 | |||
294 | if (arg > (int)sizeof(hmac_key)) { | ||
295 | SHA1_Init(&key->head); | ||
296 | SHA1_Update(&key->head,ptr,arg); | ||
297 | SHA1_Final(hmac_key,&key->head); | ||
298 | } else { | ||
299 | memcpy(hmac_key,ptr,arg); | ||
300 | } | ||
301 | |||
302 | for (i=0;i<sizeof(hmac_key);i++) | ||
303 | hmac_key[i] ^= 0x36; /* ipad */ | ||
304 | SHA1_Init(&key->head); | ||
305 | SHA1_Update(&key->head,hmac_key,sizeof(hmac_key)); | ||
306 | |||
307 | for (i=0;i<sizeof(hmac_key);i++) | ||
308 | hmac_key[i] ^= 0x36^0x5c; /* opad */ | ||
309 | SHA1_Init(&key->tail); | ||
310 | SHA1_Update(&key->tail,hmac_key,sizeof(hmac_key)); | ||
311 | |||
312 | return 1; | ||
313 | } | ||
314 | case EVP_CTRL_AEAD_TLS1_AAD: | ||
315 | { | ||
316 | unsigned char *p=ptr; | ||
317 | unsigned int len=p[arg-2]<<8|p[arg-1]; | ||
318 | |||
319 | if (ctx->encrypt) | ||
320 | { | ||
321 | key->payload_length = len; | ||
322 | if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) { | ||
323 | len -= AES_BLOCK_SIZE; | ||
324 | p[arg-2] = len>>8; | ||
325 | p[arg-1] = len; | ||
326 | } | ||
327 | key->md = key->head; | ||
328 | SHA1_Update(&key->md,p,arg); | ||
329 | |||
330 | return (int)(((len+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE) | ||
331 | - len); | ||
332 | } | ||
333 | else | ||
334 | { | ||
335 | if (arg>13) arg = 13; | ||
336 | memcpy(key->aux.tls_aad,ptr,arg); | ||
337 | key->payload_length = arg; | ||
338 | |||
339 | return SHA_DIGEST_LENGTH; | ||
340 | } | ||
341 | } | ||
342 | default: | ||
343 | return -1; | ||
344 | } | ||
345 | } | ||
346 | |||
347 | static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = | ||
348 | { | ||
349 | #ifdef NID_aes_128_cbc_hmac_sha1 | ||
350 | NID_aes_128_cbc_hmac_sha1, | ||
351 | #else | ||
352 | NID_undef, | ||
353 | #endif | ||
354 | 16,16,16, | ||
355 | EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER, | ||
356 | aesni_cbc_hmac_sha1_init_key, | ||
357 | aesni_cbc_hmac_sha1_cipher, | ||
358 | NULL, | ||
359 | sizeof(EVP_AES_HMAC_SHA1), | ||
360 | EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv, | ||
361 | EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv, | ||
362 | aesni_cbc_hmac_sha1_ctrl, | ||
363 | NULL | ||
364 | }; | ||
365 | |||
366 | static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = | ||
367 | { | ||
368 | #ifdef NID_aes_256_cbc_hmac_sha1 | ||
369 | NID_aes_256_cbc_hmac_sha1, | ||
370 | #else | ||
371 | NID_undef, | ||
372 | #endif | ||
373 | 16,32,16, | ||
374 | EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER, | ||
375 | aesni_cbc_hmac_sha1_init_key, | ||
376 | aesni_cbc_hmac_sha1_cipher, | ||
377 | NULL, | ||
378 | sizeof(EVP_AES_HMAC_SHA1), | ||
379 | EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv, | ||
380 | EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv, | ||
381 | aesni_cbc_hmac_sha1_ctrl, | ||
382 | NULL | ||
383 | }; | ||
384 | |||
385 | const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void) | ||
386 | { | ||
387 | return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE? | ||
388 | &aesni_128_cbc_hmac_sha1_cipher:NULL); | ||
389 | } | ||
390 | |||
391 | const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void) | ||
392 | { | ||
393 | return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE? | ||
394 | &aesni_256_cbc_hmac_sha1_cipher:NULL); | ||
395 | } | ||
396 | #else | ||
397 | const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void) | ||
398 | { | ||
399 | return NULL; | ||
400 | } | ||
401 | const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void) | ||
402 | { | ||
403 | return NULL; | ||
404 | } | ||
405 | #endif | ||
406 | #endif | ||
diff --git a/src/lib/libcrypto/evp/e_rc4_hmac_md5.c b/src/lib/libcrypto/evp/e_rc4_hmac_md5.c new file mode 100644 index 0000000000..56563191ba --- /dev/null +++ b/src/lib/libcrypto/evp/e_rc4_hmac_md5.c | |||
@@ -0,0 +1,298 @@ | |||
1 | /* ==================================================================== | ||
2 | * Copyright (c) 2011 The OpenSSL Project. All rights reserved. | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in | ||
13 | * the documentation and/or other materials provided with the | ||
14 | * distribution. | ||
15 | * | ||
16 | * 3. All advertising materials mentioning features or use of this | ||
17 | * software must display the following acknowledgment: | ||
18 | * "This product includes software developed by the OpenSSL Project | ||
19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
20 | * | ||
21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
22 | * endorse or promote products derived from this software without | ||
23 | * prior written permission. For written permission, please contact | ||
24 | * licensing@OpenSSL.org. | ||
25 | * | ||
26 | * 5. Products derived from this software may not be called "OpenSSL" | ||
27 | * nor may "OpenSSL" appear in their names without prior written | ||
28 | * permission of the OpenSSL Project. | ||
29 | * | ||
30 | * 6. Redistributions of any form whatsoever must retain the following | ||
31 | * acknowledgment: | ||
32 | * "This product includes software developed by the OpenSSL Project | ||
33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
34 | * | ||
35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
46 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
47 | * ==================================================================== | ||
48 | */ | ||
49 | |||
50 | #include <openssl/opensslconf.h> | ||
51 | |||
52 | #include <stdio.h> | ||
53 | #include <string.h> | ||
54 | |||
55 | #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_MD5) | ||
56 | |||
57 | #include <openssl/evp.h> | ||
58 | #include <openssl/objects.h> | ||
59 | #include <openssl/rc4.h> | ||
60 | #include <openssl/md5.h> | ||
61 | |||
62 | #ifndef EVP_CIPH_FLAG_AEAD_CIPHER | ||
63 | #define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000 | ||
64 | #define EVP_CTRL_AEAD_TLS1_AAD 0x16 | ||
65 | #define EVP_CTRL_AEAD_SET_MAC_KEY 0x17 | ||
66 | #endif | ||
67 | |||
68 | /* FIXME: surely this is available elsewhere? */ | ||
69 | #define EVP_RC4_KEY_SIZE 16 | ||
70 | |||
71 | typedef struct | ||
72 | { | ||
73 | RC4_KEY ks; | ||
74 | MD5_CTX head,tail,md; | ||
75 | size_t payload_length; | ||
76 | } EVP_RC4_HMAC_MD5; | ||
77 | |||
78 | #define NO_PAYLOAD_LENGTH ((size_t)-1) | ||
79 | |||
80 | void rc4_md5_enc (RC4_KEY *key, const void *in0, void *out, | ||
81 | MD5_CTX *ctx,const void *inp,size_t blocks); | ||
82 | |||
83 | #define data(ctx) ((EVP_RC4_HMAC_MD5 *)(ctx)->cipher_data) | ||
84 | |||
85 | static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, | ||
86 | const unsigned char *inkey, | ||
87 | const unsigned char *iv, int enc) | ||
88 | { | ||
89 | EVP_RC4_HMAC_MD5 *key = data(ctx); | ||
90 | |||
91 | RC4_set_key(&key->ks,EVP_CIPHER_CTX_key_length(ctx), | ||
92 | inkey); | ||
93 | |||
94 | MD5_Init(&key->head); /* handy when benchmarking */ | ||
95 | key->tail = key->head; | ||
96 | key->md = key->head; | ||
97 | |||
98 | key->payload_length = NO_PAYLOAD_LENGTH; | ||
99 | |||
100 | return 1; | ||
101 | } | ||
102 | |||
103 | #if !defined(OPENSSL_NO_ASM) && ( \ | ||
104 | defined(__x86_64) || defined(__x86_64__) || \ | ||
105 | defined(_M_AMD64) || defined(_M_X64) || \ | ||
106 | defined(__INTEL__) ) && \ | ||
107 | !(defined(__APPLE__) && defined(__MACH__)) | ||
108 | #define STITCHED_CALL | ||
109 | #endif | ||
110 | |||
111 | #if !defined(STITCHED_CALL) | ||
112 | #define rc4_off 0 | ||
113 | #define md5_off 0 | ||
114 | #endif | ||
115 | |||
116 | static int rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
117 | const unsigned char *in, size_t len) | ||
118 | { | ||
119 | EVP_RC4_HMAC_MD5 *key = data(ctx); | ||
120 | #if defined(STITCHED_CALL) | ||
121 | size_t rc4_off = 32-1-(key->ks.x&(32-1)), /* 32 is $MOD from rc4_md5-x86_64.pl */ | ||
122 | md5_off = MD5_CBLOCK-key->md.num, | ||
123 | blocks; | ||
124 | unsigned int l; | ||
125 | extern unsigned int OPENSSL_ia32cap_P[]; | ||
126 | #endif | ||
127 | size_t plen = key->payload_length; | ||
128 | |||
129 | if (plen!=NO_PAYLOAD_LENGTH && len!=(plen+MD5_DIGEST_LENGTH)) return 0; | ||
130 | |||
131 | if (ctx->encrypt) { | ||
132 | if (plen==NO_PAYLOAD_LENGTH) plen = len; | ||
133 | #if defined(STITCHED_CALL) | ||
134 | /* cipher has to "fall behind" */ | ||
135 | if (rc4_off>md5_off) md5_off+=MD5_CBLOCK; | ||
136 | |||
137 | if (plen>md5_off && (blocks=(plen-md5_off)/MD5_CBLOCK) && | ||
138 | (OPENSSL_ia32cap_P[0]&(1<<20))==0) { | ||
139 | MD5_Update(&key->md,in,md5_off); | ||
140 | RC4(&key->ks,rc4_off,in,out); | ||
141 | |||
142 | rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off, | ||
143 | &key->md,in+md5_off,blocks); | ||
144 | blocks *= MD5_CBLOCK; | ||
145 | rc4_off += blocks; | ||
146 | md5_off += blocks; | ||
147 | key->md.Nh += blocks>>29; | ||
148 | key->md.Nl += blocks<<=3; | ||
149 | if (key->md.Nl<(unsigned int)blocks) key->md.Nh++; | ||
150 | } else { | ||
151 | rc4_off = 0; | ||
152 | md5_off = 0; | ||
153 | } | ||
154 | #endif | ||
155 | MD5_Update(&key->md,in+md5_off,plen-md5_off); | ||
156 | |||
157 | if (plen!=len) { /* "TLS" mode of operation */ | ||
158 | if (in!=out) | ||
159 | memcpy(out+rc4_off,in+rc4_off,plen-rc4_off); | ||
160 | |||
161 | /* calculate HMAC and append it to payload */ | ||
162 | MD5_Final(out+plen,&key->md); | ||
163 | key->md = key->tail; | ||
164 | MD5_Update(&key->md,out+plen,MD5_DIGEST_LENGTH); | ||
165 | MD5_Final(out+plen,&key->md); | ||
166 | /* encrypt HMAC at once */ | ||
167 | RC4(&key->ks,len-rc4_off,out+rc4_off,out+rc4_off); | ||
168 | } else { | ||
169 | RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off); | ||
170 | } | ||
171 | } else { | ||
172 | unsigned char mac[MD5_DIGEST_LENGTH]; | ||
173 | #if defined(STITCHED_CALL) | ||
174 | /* digest has to "fall behind" */ | ||
175 | if (md5_off>rc4_off) rc4_off += 2*MD5_CBLOCK; | ||
176 | else rc4_off += MD5_CBLOCK; | ||
177 | |||
178 | if (len>rc4_off && (blocks=(len-rc4_off)/MD5_CBLOCK) && | ||
179 | (OPENSSL_ia32cap_P[0]&(1<<20))==0) { | ||
180 | RC4(&key->ks,rc4_off,in,out); | ||
181 | MD5_Update(&key->md,out,md5_off); | ||
182 | |||
183 | rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off, | ||
184 | &key->md,out+md5_off,blocks); | ||
185 | blocks *= MD5_CBLOCK; | ||
186 | rc4_off += blocks; | ||
187 | md5_off += blocks; | ||
188 | l = (key->md.Nl+(blocks<<3))&0xffffffffU; | ||
189 | if (l<key->md.Nl) key->md.Nh++; | ||
190 | key->md.Nl = l; | ||
191 | key->md.Nh += blocks>>29; | ||
192 | } else { | ||
193 | md5_off=0; | ||
194 | rc4_off=0; | ||
195 | } | ||
196 | #endif | ||
197 | /* decrypt HMAC at once */ | ||
198 | RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off); | ||
199 | if (plen!=NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */ | ||
200 | MD5_Update(&key->md,out+md5_off,plen-md5_off); | ||
201 | |||
202 | /* calculate HMAC and verify it */ | ||
203 | MD5_Final(mac,&key->md); | ||
204 | key->md = key->tail; | ||
205 | MD5_Update(&key->md,mac,MD5_DIGEST_LENGTH); | ||
206 | MD5_Final(mac,&key->md); | ||
207 | |||
208 | if (memcmp(out+plen,mac,MD5_DIGEST_LENGTH)) | ||
209 | return 0; | ||
210 | } else { | ||
211 | MD5_Update(&key->md,out+md5_off,len-md5_off); | ||
212 | } | ||
213 | } | ||
214 | |||
215 | key->payload_length = NO_PAYLOAD_LENGTH; | ||
216 | |||
217 | return 1; | ||
218 | } | ||
219 | |||
220 | static int rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) | ||
221 | { | ||
222 | EVP_RC4_HMAC_MD5 *key = data(ctx); | ||
223 | |||
224 | switch (type) | ||
225 | { | ||
226 | case EVP_CTRL_AEAD_SET_MAC_KEY: | ||
227 | { | ||
228 | unsigned int i; | ||
229 | unsigned char hmac_key[64]; | ||
230 | |||
231 | memset (hmac_key,0,sizeof(hmac_key)); | ||
232 | |||
233 | if (arg > (int)sizeof(hmac_key)) { | ||
234 | MD5_Init(&key->head); | ||
235 | MD5_Update(&key->head,ptr,arg); | ||
236 | MD5_Final(hmac_key,&key->head); | ||
237 | } else { | ||
238 | memcpy(hmac_key,ptr,arg); | ||
239 | } | ||
240 | |||
241 | for (i=0;i<sizeof(hmac_key);i++) | ||
242 | hmac_key[i] ^= 0x36; /* ipad */ | ||
243 | MD5_Init(&key->head); | ||
244 | MD5_Update(&key->head,hmac_key,sizeof(hmac_key)); | ||
245 | |||
246 | for (i=0;i<sizeof(hmac_key);i++) | ||
247 | hmac_key[i] ^= 0x36^0x5c; /* opad */ | ||
248 | MD5_Init(&key->tail); | ||
249 | MD5_Update(&key->tail,hmac_key,sizeof(hmac_key)); | ||
250 | |||
251 | return 1; | ||
252 | } | ||
253 | case EVP_CTRL_AEAD_TLS1_AAD: | ||
254 | { | ||
255 | unsigned char *p=ptr; | ||
256 | unsigned int len=p[arg-2]<<8|p[arg-1]; | ||
257 | |||
258 | if (!ctx->encrypt) | ||
259 | { | ||
260 | len -= MD5_DIGEST_LENGTH; | ||
261 | p[arg-2] = len>>8; | ||
262 | p[arg-1] = len; | ||
263 | } | ||
264 | key->payload_length=len; | ||
265 | key->md = key->head; | ||
266 | MD5_Update(&key->md,p,arg); | ||
267 | |||
268 | return MD5_DIGEST_LENGTH; | ||
269 | } | ||
270 | default: | ||
271 | return -1; | ||
272 | } | ||
273 | } | ||
274 | |||
275 | static EVP_CIPHER r4_hmac_md5_cipher= | ||
276 | { | ||
277 | #ifdef NID_rc4_hmac_md5 | ||
278 | NID_rc4_hmac_md5, | ||
279 | #else | ||
280 | NID_undef, | ||
281 | #endif | ||
282 | 1,EVP_RC4_KEY_SIZE,0, | ||
283 | EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_FLAG_AEAD_CIPHER, | ||
284 | rc4_hmac_md5_init_key, | ||
285 | rc4_hmac_md5_cipher, | ||
286 | NULL, | ||
287 | sizeof(EVP_RC4_HMAC_MD5), | ||
288 | NULL, | ||
289 | NULL, | ||
290 | rc4_hmac_md5_ctrl, | ||
291 | NULL | ||
292 | }; | ||
293 | |||
294 | const EVP_CIPHER *EVP_rc4_hmac_md5(void) | ||
295 | { | ||
296 | return(&r4_hmac_md5_cipher); | ||
297 | } | ||
298 | #endif | ||
diff --git a/src/lib/libcrypto/evp/m_ecdsa.c b/src/lib/libcrypto/evp/m_ecdsa.c index 8d87a49ebe..4b15fb0f6c 100644 --- a/src/lib/libcrypto/evp/m_ecdsa.c +++ b/src/lib/libcrypto/evp/m_ecdsa.c | |||
@@ -116,6 +116,8 @@ | |||
116 | #include <openssl/x509.h> | 116 | #include <openssl/x509.h> |
117 | 117 | ||
118 | #ifndef OPENSSL_NO_SHA | 118 | #ifndef OPENSSL_NO_SHA |
119 | #ifndef OPENSSL_FIPS | ||
120 | |||
119 | static int init(EVP_MD_CTX *ctx) | 121 | static int init(EVP_MD_CTX *ctx) |
120 | { return SHA1_Init(ctx->md_data); } | 122 | { return SHA1_Init(ctx->md_data); } |
121 | 123 | ||
@@ -146,3 +148,4 @@ const EVP_MD *EVP_ecdsa(void) | |||
146 | return(&ecdsa_md); | 148 | return(&ecdsa_md); |
147 | } | 149 | } |
148 | #endif | 150 | #endif |
151 | #endif | ||
diff --git a/src/lib/libcrypto/evp/m_wp.c b/src/lib/libcrypto/evp/m_wp.c index 1ce47c040b..c51bc2d5d1 100644 --- a/src/lib/libcrypto/evp/m_wp.c +++ b/src/lib/libcrypto/evp/m_wp.c | |||
@@ -9,6 +9,7 @@ | |||
9 | #include <openssl/objects.h> | 9 | #include <openssl/objects.h> |
10 | #include <openssl/x509.h> | 10 | #include <openssl/x509.h> |
11 | #include <openssl/whrlpool.h> | 11 | #include <openssl/whrlpool.h> |
12 | #include "evp_locl.h" | ||
12 | 13 | ||
13 | static int init(EVP_MD_CTX *ctx) | 14 | static int init(EVP_MD_CTX *ctx) |
14 | { return WHIRLPOOL_Init(ctx->md_data); } | 15 | { return WHIRLPOOL_Init(ctx->md_data); } |
diff --git a/src/lib/libcrypto/evp/pmeth_gn.c b/src/lib/libcrypto/evp/pmeth_gn.c index 5d74161a09..4651c81370 100644 --- a/src/lib/libcrypto/evp/pmeth_gn.c +++ b/src/lib/libcrypto/evp/pmeth_gn.c | |||
@@ -199,7 +199,7 @@ int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx) | |||
199 | } | 199 | } |
200 | 200 | ||
201 | EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, | 201 | EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, |
202 | unsigned char *key, int keylen) | 202 | const unsigned char *key, int keylen) |
203 | { | 203 | { |
204 | EVP_PKEY_CTX *mac_ctx = NULL; | 204 | EVP_PKEY_CTX *mac_ctx = NULL; |
205 | EVP_PKEY *mac_key = NULL; | 205 | EVP_PKEY *mac_key = NULL; |
@@ -209,7 +209,8 @@ EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, | |||
209 | if (EVP_PKEY_keygen_init(mac_ctx) <= 0) | 209 | if (EVP_PKEY_keygen_init(mac_ctx) <= 0) |
210 | goto merr; | 210 | goto merr; |
211 | if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN, | 211 | if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN, |
212 | EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key) <= 0) | 212 | EVP_PKEY_CTRL_SET_MAC_KEY, |
213 | keylen, (void *)key) <= 0) | ||
213 | goto merr; | 214 | goto merr; |
214 | if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) | 215 | if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) |
215 | goto merr; | 216 | goto merr; |
diff --git a/src/lib/libcrypto/evp/pmeth_lib.c b/src/lib/libcrypto/evp/pmeth_lib.c index 5481d4b8a5..acfa7b6f87 100644 --- a/src/lib/libcrypto/evp/pmeth_lib.c +++ b/src/lib/libcrypto/evp/pmeth_lib.c | |||
@@ -73,7 +73,7 @@ DECLARE_STACK_OF(EVP_PKEY_METHOD) | |||
73 | STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; | 73 | STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
74 | 74 | ||
75 | extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth; | 75 | extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth; |
76 | extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth; | 76 | extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth, cmac_pkey_meth; |
77 | 77 | ||
78 | static const EVP_PKEY_METHOD *standard_methods[] = | 78 | static const EVP_PKEY_METHOD *standard_methods[] = |
79 | { | 79 | { |
@@ -90,6 +90,7 @@ static const EVP_PKEY_METHOD *standard_methods[] = | |||
90 | &ec_pkey_meth, | 90 | &ec_pkey_meth, |
91 | #endif | 91 | #endif |
92 | &hmac_pkey_meth, | 92 | &hmac_pkey_meth, |
93 | &cmac_pkey_meth | ||
93 | }; | 94 | }; |
94 | 95 | ||
95 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, | 96 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *, |
@@ -203,6 +204,8 @@ EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags) | |||
203 | if (!pmeth) | 204 | if (!pmeth) |
204 | return NULL; | 205 | return NULL; |
205 | 206 | ||
207 | memset(pmeth, 0, sizeof(EVP_PKEY_METHOD)); | ||
208 | |||
206 | pmeth->pkey_id = id; | 209 | pmeth->pkey_id = id; |
207 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | 210 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; |
208 | 211 | ||
@@ -235,6 +238,56 @@ EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags) | |||
235 | return pmeth; | 238 | return pmeth; |
236 | } | 239 | } |
237 | 240 | ||
241 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, | ||
242 | const EVP_PKEY_METHOD *meth) | ||
243 | { | ||
244 | if (ppkey_id) | ||
245 | *ppkey_id = meth->pkey_id; | ||
246 | if (pflags) | ||
247 | *pflags = meth->flags; | ||
248 | } | ||
249 | |||
250 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | ||
251 | { | ||
252 | |||
253 | dst->init = src->init; | ||
254 | dst->copy = src->copy; | ||
255 | dst->cleanup = src->cleanup; | ||
256 | |||
257 | dst->paramgen_init = src->paramgen_init; | ||
258 | dst->paramgen = src->paramgen; | ||
259 | |||
260 | dst->keygen_init = src->keygen_init; | ||
261 | dst->keygen = src->keygen; | ||
262 | |||
263 | dst->sign_init = src->sign_init; | ||
264 | dst->sign = src->sign; | ||
265 | |||
266 | dst->verify_init = src->verify_init; | ||
267 | dst->verify = src->verify; | ||
268 | |||
269 | dst->verify_recover_init = src->verify_recover_init; | ||
270 | dst->verify_recover = src->verify_recover; | ||
271 | |||
272 | dst->signctx_init = src->signctx_init; | ||
273 | dst->signctx = src->signctx; | ||
274 | |||
275 | dst->verifyctx_init = src->verifyctx_init; | ||
276 | dst->verifyctx = src->verifyctx; | ||
277 | |||
278 | dst->encrypt_init = src->encrypt_init; | ||
279 | dst->encrypt = src->encrypt; | ||
280 | |||
281 | dst->decrypt_init = src->decrypt_init; | ||
282 | dst->decrypt = src->decrypt; | ||
283 | |||
284 | dst->derive_init = src->derive_init; | ||
285 | dst->derive = src->derive; | ||
286 | |||
287 | dst->ctrl = src->ctrl; | ||
288 | dst->ctrl_str = src->ctrl_str; | ||
289 | } | ||
290 | |||
238 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) | 291 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
239 | { | 292 | { |
240 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | 293 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) |