2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 /* Macros to code block cipher wrappers */
61 /* Wrapper functions for each cipher mode */
63 #define BLOCK_CIPHER_ecb_loop() \
65 bl = ctx->cipher->block_size;\
66 if(inl < bl) return 1;\
68 for(i=0; i <= inl; i+=bl)
70 #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
71 static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
73 BLOCK_CIPHER_ecb_loop() \
74 cprefix##_ecb_encrypt(in + i, out + i, &((kstruct *)ctx->cipher_data)->ksched, ctx->encrypt);\
78 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
80 #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
81 static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
83 while(inl>=EVP_MAXCHUNK)\
85 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\
91 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\
95 #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
96 static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
98 while(inl>=EVP_MAXCHUNK) \
100 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\
106 cprefix##_cbc_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\
110 #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
111 static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
113 size_t chunk=EVP_MAXCHUNK;\
114 if (cbits==1) chunk>>=3;\
115 if (inl<chunk) chunk=inl;\
116 while(inl && inl>=chunk)\
118 cprefix##_cfb##cbits##_encrypt(in, out, (long)((cbits==1) && !(ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) ?inl*8:inl), &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num, ctx->encrypt);\
122 if(inl<chunk) chunk=inl;\
127 #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
128 BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
129 BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
130 BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
131 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
133 #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
134 key_len, iv_len, flags, init_key, cleanup, \
135 set_asn1, get_asn1, ctrl) \
136 static const EVP_CIPHER cname##_##mode = { \
137 nid##_##nmode, block_size, key_len, iv_len, \
138 flags | EVP_CIPH_##MODE##_MODE, \
140 cname##_##mode##_cipher, \
147 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
149 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
150 iv_len, flags, init_key, cleanup, set_asn1, \
152 BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
153 iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
155 #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
156 iv_len, cbits, flags, init_key, cleanup, \
157 set_asn1, get_asn1, ctrl) \
158 BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
159 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
162 #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
163 iv_len, cbits, flags, init_key, cleanup, \
164 set_asn1, get_asn1, ctrl) \
165 BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
166 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
169 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
170 flags, init_key, cleanup, set_asn1, \
172 BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
173 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
175 #define BLOCK_CIPHER_defs(cname, kstruct, \
176 nid, block_size, key_len, iv_len, cbits, flags, \
177 init_key, cleanup, set_asn1, get_asn1, ctrl) \
178 BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
179 init_key, cleanup, set_asn1, get_asn1, ctrl) \
180 BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
181 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
182 BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
183 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
184 BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
185 init_key, cleanup, set_asn1, get_asn1, ctrl)
189 #define BLOCK_CIPHER_defs(cname, kstruct, \
190 nid, block_size, key_len, iv_len, flags,\
191 init_key, cleanup, set_asn1, get_asn1, ctrl)\
192 static const EVP_CIPHER cname##_cbc = {\
193 nid##_cbc, block_size, key_len, iv_len, \
194 flags | EVP_CIPH_CBC_MODE,\
198 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
199 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
204 const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
205 static const EVP_CIPHER cname##_cfb = {\
206 nid##_cfb64, 1, key_len, iv_len, \
207 flags | EVP_CIPH_CFB_MODE,\
211 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
212 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
217 const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
218 static const EVP_CIPHER cname##_ofb = {\
219 nid##_ofb64, 1, key_len, iv_len, \
220 flags | EVP_CIPH_OFB_MODE,\
224 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
225 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
230 const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
231 static const EVP_CIPHER cname##_ecb = {\
232 nid##_ecb, block_size, key_len, iv_len, \
233 flags | EVP_CIPH_ECB_MODE,\
237 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
238 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
243 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
246 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
247 block_size, key_len, iv_len, cbits, \
249 cleanup, set_asn1, get_asn1, ctrl) \
250 BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
251 BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
252 cbits, flags, init_key, cleanup, set_asn1, \
255 #define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data)
257 #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len) \
258 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
259 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
260 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
261 0, cipher##_init_key, NULL, \
262 EVP_CIPHER_set_asn1_iv, \
263 EVP_CIPHER_get_asn1_iv, \
266 struct evp_pkey_ctx_st
268 /* Method associated with this operation */
269 const EVP_PKEY_METHOD
*pmeth
;
270 /* Engine that implements this method or NULL if builtin */
272 /* Key: may be NULL */
274 /* Peer key for key agreement, may be NULL */
276 /* Actual operation */
278 /* Algorithm specific data */
280 /* Application specific data */
282 /* Keygen callback */
283 EVP_PKEY_gen_cb
*pkey_gencb
;
284 /* implementation specific keygen data */
286 int keygen_info_count
;
287 } /* EVP_PKEY_CTX */;
289 #define EVP_PKEY_FLAG_DYNAMIC 1
291 struct evp_pkey_method_st
296 int (*init
)(EVP_PKEY_CTX
*ctx
);
297 int (*copy
)(EVP_PKEY_CTX
*dst
, EVP_PKEY_CTX
*src
);
298 void (*cleanup
)(EVP_PKEY_CTX
*ctx
);
300 int (*paramgen_init
)(EVP_PKEY_CTX
*ctx
);
301 int (*paramgen
)(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
);
303 int (*keygen_init
)(EVP_PKEY_CTX
*ctx
);
304 int (*keygen
)(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
);
306 int (*sign_init
)(EVP_PKEY_CTX
*ctx
);
307 int (*sign
)(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
308 const unsigned char *tbs
, size_t tbslen
);
310 int (*verify_init
)(EVP_PKEY_CTX
*ctx
);
311 int (*verify
)(EVP_PKEY_CTX
*ctx
,
312 const unsigned char *sig
, size_t siglen
,
313 const unsigned char *tbs
, size_t tbslen
);
315 int (*verify_recover_init
)(EVP_PKEY_CTX
*ctx
);
316 int (*verify_recover
)(EVP_PKEY_CTX
*ctx
,
317 unsigned char *rout
, size_t *routlen
,
318 const unsigned char *sig
, size_t siglen
);
320 int (*signctx_init
)(EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
321 int (*signctx
)(EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
324 int (*verifyctx_init
)(EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
325 int (*verifyctx
)(EVP_PKEY_CTX
*ctx
, const unsigned char *sig
,int siglen
,
328 int (*encrypt_init
)(EVP_PKEY_CTX
*ctx
);
329 int (*encrypt
)(EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
330 const unsigned char *in
, size_t inlen
);
332 int (*decrypt_init
)(EVP_PKEY_CTX
*ctx
);
333 int (*decrypt
)(EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
334 const unsigned char *in
, size_t inlen
);
336 int (*derive_init
)(EVP_PKEY_CTX
*ctx
);
337 int (*derive
)(EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *keylen
);
339 int (*ctrl
)(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
);
340 int (*ctrl_str
)(EVP_PKEY_CTX
*ctx
, const char *type
, const char *value
);
343 } /* EVP_PKEY_METHOD */;
345 void evp_pkey_set_cb_translate(BN_GENCB
*cb
, EVP_PKEY_CTX
*ctx
);
347 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX
*ctx
, const char *pass
, int passlen
,
349 const EVP_CIPHER
*c
, const EVP_MD
*md
, int en_de
);
353 #ifdef OPENSSL_DOING_MAKEDEPEND
360 #undef DES_set_key_unchecked
363 #define RIPEMD160_Init private_RIPEMD160_Init
364 #define WHIRLPOOL_Init private_WHIRLPOOL_Init
365 #define MD5_Init private_MD5_Init
366 #define MD4_Init private_MD4_Init
367 #define MD2_Init private_MD2_Init
368 #define MDC2_Init private_MDC2_Init
369 #define SHA_Init private_SHA_Init
370 #define SHA1_Init private_SHA1_Init
371 #define SHA224_Init private_SHA224_Init
372 #define SHA256_Init private_SHA256_Init
373 #define SHA384_Init private_SHA384_Init
374 #define SHA512_Init private_SHA512_Init
376 #define BF_set_key private_BF_set_key
377 #define CAST_set_key private_CAST_set_key
378 #define idea_set_encrypt_key private_idea_set_encrypt_key
379 #define SEED_set_key private_SEED_set_key
380 #define RC2_set_key private_RC2_set_key
381 #define RC4_set_key private_RC4_set_key
382 #define DES_set_key_unchecked private_DES_set_key_unchecked
383 #define Camellia_set_key private_Camellia_set_key