1 /* evp_locl.h */ 2 /* Written by Dr Stephen N Henson (steve (at) openssl.org) for the OpenSSL 3 * project 2000. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 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 18 * distribution. 19 * 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/)" 24 * 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 (at) OpenSSL.org. 29 * 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. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 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 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay (at) cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh (at) cryptsoft.com). 56 * 57 */ 58 59 /* Macros to code block cipher wrappers */ 60 61 /* Wrapper functions for each cipher mode */ 62 63 #define BLOCK_CIPHER_ecb_loop() \ 64 size_t i, bl; \ 65 bl = ctx->cipher->block_size;\ 66 if(inl < bl) return 1;\ 67 inl -= bl; \ 68 for(i=0; i <= inl; i+=bl) 69 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) \ 72 {\ 73 BLOCK_CIPHER_ecb_loop() \ 74 cprefix##_ecb_encrypt(in + i, out + i, &((kstruct *)ctx->cipher_data)->ksched, ctx->encrypt);\ 75 return 1;\ 76 } 77 78 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2)) 79 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) \ 82 {\ 83 while(inl>=EVP_MAXCHUNK)\ 84 {\ 85 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\ 86 inl-=EVP_MAXCHUNK;\ 87 in +=EVP_MAXCHUNK;\ 88 out+=EVP_MAXCHUNK;\ 89 }\ 90 if (inl)\ 91 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\ 92 return 1;\ 93 } 94 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) \ 97 {\ 98 while(inl>=EVP_MAXCHUNK) \ 99 {\ 100 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\ 101 inl-=EVP_MAXCHUNK;\ 102 in +=EVP_MAXCHUNK;\ 103 out+=EVP_MAXCHUNK;\ 104 }\ 105 if (inl)\ 106 cprefix##_cbc_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\ 107 return 1;\ 108 } 109 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) \ 112 {\ 113 size_t chunk=EVP_MAXCHUNK;\ 114 if (cbits==1) chunk>>=3;\ 115 if (inl<chunk) chunk=inl;\ 116 while(inl && inl>=chunk)\ 117 {\ 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);\ 119 inl-=chunk;\ 120 in +=chunk;\ 121 out+=chunk;\ 122 if(inl<chunk) chunk=inl;\ 123 }\ 124 return 1;\ 125 } 126 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) 132 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, \ 139 init_key, \ 140 cname##_##mode##_cipher, \ 141 cleanup, \ 142 sizeof(kstruct), \ 143 set_asn1, get_asn1,\ 144 ctrl, \ 145 NULL \ 146 }; \ 147 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; } 148 149 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \ 150 iv_len, flags, init_key, cleanup, set_asn1, \ 151 get_asn1, ctrl) \ 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) 154 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, \ 160 get_asn1, ctrl) 161 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, \ 167 get_asn1, ctrl) 168 169 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ 170 flags, init_key, cleanup, set_asn1, \ 171 get_asn1, ctrl) \ 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) 174 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) 186 187 188 /* 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,\ 195 init_key,\ 196 cname##_cbc_cipher,\ 197 cleanup,\ 198 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ 199 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ 200 set_asn1, get_asn1,\ 201 ctrl, \ 202 NULL \ 203 };\ 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,\ 208 init_key,\ 209 cname##_cfb_cipher,\ 210 cleanup,\ 211 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ 212 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ 213 set_asn1, get_asn1,\ 214 ctrl,\ 215 NULL \ 216 };\ 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,\ 221 init_key,\ 222 cname##_ofb_cipher,\ 223 cleanup,\ 224 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ 225 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ 226 set_asn1, get_asn1,\ 227 ctrl,\ 228 NULL \ 229 };\ 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,\ 234 init_key,\ 235 cname##_ecb_cipher,\ 236 cleanup,\ 237 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ 238 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ 239 set_asn1, get_asn1,\ 240 ctrl,\ 241 NULL \ 242 };\ 243 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } 244 */ 245 246 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \ 247 block_size, key_len, iv_len, cbits, \ 248 flags, init_key, \ 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, \ 253 get_asn1, ctrl) 254 255 #define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data) 256 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, \ 264 NULL) 265 266 struct evp_pkey_ctx_st 267 { 268 /* Method associated with this operation */ 269 const EVP_PKEY_METHOD *pmeth; 270 /* Engine that implements this method or NULL if builtin */ 271 ENGINE *engine; 272 /* Key: may be NULL */ 273 EVP_PKEY *pkey; 274 /* Peer key for key agreement, may be NULL */ 275 EVP_PKEY *peerkey; 276 /* Actual operation */ 277 int operation; 278 /* Algorithm specific data */ 279 void *data; 280 /* Application specific data */ 281 void *app_data; 282 /* Keygen callback */ 283 EVP_PKEY_gen_cb *pkey_gencb; 284 /* implementation specific keygen data */ 285 int *keygen_info; 286 int keygen_info_count; 287 } /* EVP_PKEY_CTX */; 288 289 #define EVP_PKEY_FLAG_DYNAMIC 1 290 291 struct evp_pkey_method_st 292 { 293 int pkey_id; 294 int flags; 295 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); 299 300 int (*paramgen_init)(EVP_PKEY_CTX *ctx); 301 int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); 302 303 int (*keygen_init)(EVP_PKEY_CTX *ctx); 304 int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); 305 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); 309 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); 314 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); 319 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, 322 EVP_MD_CTX *mctx); 323 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, 326 EVP_MD_CTX *mctx); 327 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); 331 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); 335 336 int (*derive_init)(EVP_PKEY_CTX *ctx); 337 int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); 338 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); 341 342 343 } /* EVP_PKEY_METHOD */; 344 345 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); 346