diff options
Diffstat (limited to 'src/lib/libcrypto/evp/evp_locl.h')
-rw-r--r-- | src/lib/libcrypto/evp/evp_locl.h | 155 |
1 files changed, 124 insertions, 31 deletions
diff --git a/src/lib/libcrypto/evp/evp_locl.h b/src/lib/libcrypto/evp/evp_locl.h index eabcc96f30..292d74c188 100644 --- a/src/lib/libcrypto/evp/evp_locl.h +++ b/src/lib/libcrypto/evp/evp_locl.h | |||
@@ -61,38 +61,66 @@ | |||
61 | /* Wrapper functions for each cipher mode */ | 61 | /* Wrapper functions for each cipher mode */ |
62 | 62 | ||
63 | #define BLOCK_CIPHER_ecb_loop() \ | 63 | #define BLOCK_CIPHER_ecb_loop() \ |
64 | unsigned int i, bl; \ | 64 | size_t i, bl; \ |
65 | bl = ctx->cipher->block_size;\ | 65 | bl = ctx->cipher->block_size;\ |
66 | if(inl < bl) return 1;\ | 66 | if(inl < bl) return 1;\ |
67 | inl -= bl; \ | 67 | inl -= bl; \ |
68 | for(i=0; i <= inl; i+=bl) | 68 | for(i=0; i <= inl; i+=bl) |
69 | 69 | ||
70 | #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ | 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, unsigned int inl) \ | 71 | static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
72 | {\ | 72 | {\ |
73 | BLOCK_CIPHER_ecb_loop() \ | 73 | BLOCK_CIPHER_ecb_loop() \ |
74 | cprefix##_ecb_encrypt(in + i, out + i, &((kstruct *)ctx->cipher_data)->ksched, ctx->encrypt);\ | 74 | cprefix##_ecb_encrypt(in + i, out + i, &((kstruct *)ctx->cipher_data)->ksched, ctx->encrypt);\ |
75 | return 1;\ | 75 | return 1;\ |
76 | } | 76 | } |
77 | 77 | ||
78 | #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2)) | ||
79 | |||
78 | #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \ | 80 | #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \ |
79 | static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \ | 81 | static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
80 | {\ | 82 | {\ |
81 | cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, &ctx->num);\ | 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);\ | ||
82 | return 1;\ | 92 | return 1;\ |
83 | } | 93 | } |
84 | 94 | ||
85 | #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ | 95 | #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ |
86 | static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \ | 96 | static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
87 | {\ | 97 | {\ |
88 | cprefix##_cbc_encrypt(in, out, (long)inl, &((kstruct *)ctx->cipher_data)->ksched, ctx->iv, ctx->encrypt);\ | 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);\ | ||
89 | return 1;\ | 107 | return 1;\ |
90 | } | 108 | } |
91 | 109 | ||
92 | #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ | 110 | #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ |
93 | static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl) \ | 111 | static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ |
94 | {\ | 112 | {\ |
95 | 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);\ | 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 | }\ | ||
96 | return 1;\ | 124 | return 1;\ |
97 | } | 125 | } |
98 | 126 | ||
@@ -139,10 +167,10 @@ BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \ | |||
139 | get_asn1, ctrl) | 167 | get_asn1, ctrl) |
140 | 168 | ||
141 | #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ | 169 | #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ |
142 | iv_len, flags, init_key, cleanup, set_asn1, \ | 170 | flags, init_key, cleanup, set_asn1, \ |
143 | get_asn1, ctrl) \ | 171 | get_asn1, ctrl) \ |
144 | BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \ | 172 | BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \ |
145 | iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) | 173 | 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) |
146 | 174 | ||
147 | #define BLOCK_CIPHER_defs(cname, kstruct, \ | 175 | #define BLOCK_CIPHER_defs(cname, kstruct, \ |
148 | nid, block_size, key_len, iv_len, cbits, flags, \ | 176 | nid, block_size, key_len, iv_len, cbits, flags, \ |
@@ -153,7 +181,7 @@ BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \ | |||
153 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | 181 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ |
154 | BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \ | 182 | BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \ |
155 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | 183 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ |
156 | BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, iv_len, flags, \ | 184 | BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \ |
157 | init_key, cleanup, set_asn1, get_asn1, ctrl) | 185 | init_key, cleanup, set_asn1, get_asn1, ctrl) |
158 | 186 | ||
159 | 187 | ||
@@ -226,27 +254,92 @@ const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } | |||
226 | 254 | ||
227 | #define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data) | 255 | #define EVP_C_DATA(kstruct, ctx) ((kstruct *)(ctx)->cipher_data) |
228 | 256 | ||
229 | #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \ | 257 | #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len) \ |
230 | BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ | 258 | BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ |
231 | BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ | 259 | BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ |
232 | NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ | 260 | NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ |
233 | (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \ | 261 | 0, cipher##_init_key, NULL, \ |
234 | cipher##_init_key, NULL, NULL, NULL, NULL) | 262 | EVP_CIPHER_set_asn1_iv, \ |
235 | 263 | EVP_CIPHER_get_asn1_iv, \ | |
236 | #ifdef OPENSSL_FIPS | 264 | NULL) |
237 | #define RC2_set_key private_RC2_set_key | 265 | |
238 | #define RC4_set_key private_RC4_set_key | 266 | struct evp_pkey_ctx_st |
239 | #define CAST_set_key private_CAST_set_key | 267 | { |
240 | #define RC5_32_set_key private_RC5_32_set_key | 268 | /* Method associated with this operation */ |
241 | #define BF_set_key private_BF_set_key | 269 | const EVP_PKEY_METHOD *pmeth; |
242 | #define Camellia_set_key private_Camellia_set_key | 270 | /* Engine that implements this method or NULL if builtin */ |
243 | #define idea_set_encrypt_key private_idea_set_encrypt_key | 271 | ENGINE *engine; |
244 | 272 | /* Key: may be NULL */ | |
245 | #define MD5_Init private_MD5_Init | 273 | EVP_PKEY *pkey; |
246 | #define MD4_Init private_MD4_Init | 274 | /* Peer key for key agreement, may be NULL */ |
247 | #define MD2_Init private_MD2_Init | 275 | EVP_PKEY *peerkey; |
248 | #define MDC2_Init private_MDC2_Init | 276 | /* Actual operation */ |
249 | #define SHA_Init private_SHA_Init | 277 | int operation; |
250 | 278 | /* Algorithm specific data */ | |
251 | #endif | 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 */; | ||
252 | 344 | ||
345 | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); | ||