Home | History | Annotate | Download | only in evp

Lines Matching refs:ctx

73 #define M_do_cipher(ctx, out, in, inl) FIPS_cipher(ctx, out, in, inl)
75 #define M_do_cipher(ctx, out, in, inl) ctx->cipher->do_cipher(ctx, out, in, inl)
81 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
83 memset(ctx,0,sizeof(EVP_CIPHER_CTX));
84 /* ctx->cipher=NULL; */
89 EVP_CIPHER_CTX *ctx=OPENSSL_malloc(sizeof *ctx);
90 if (ctx)
91 EVP_CIPHER_CTX_init(ctx);
92 return ctx;
95 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
99 EVP_CIPHER_CTX_init(ctx);
100 return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc);
103 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
107 enc = ctx->encrypt;
112 ctx->encrypt = enc;
119 if (ctx->engine && ctx->cipher && (!cipher ||
120 (cipher && (cipher->nid == ctx->cipher->nid))))
128 if (ctx->cipher)
130 unsigned long flags = ctx->flags;
131 EVP_CIPHER_CTX_cleanup(ctx);
133 ctx->encrypt = enc;
134 ctx->flags = flags;
166 ctx->engine = impl;
169 ctx->engine = NULL;
174 return FIPS_cipherinit(ctx, cipher, key, iv, enc);
176 ctx->cipher=cipher;
177 if (ctx->cipher->ctx_size)
179 ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
180 if (!ctx->cipher_data)
188 ctx->cipher_data = NULL;
190 ctx->key_len = cipher->key_len;
191 ctx->flags = 0;
192 if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT)
194 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL))
201 else if(!ctx->cipher)
211 return FIPS_cipherinit(ctx, cipher, key, iv, enc);
214 OPENSSL_assert(ctx->cipher->block_size == 1
215 || ctx->cipher->block_size == 8
216 || ctx->cipher->block_size == 16);
218 if(!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
219 switch(EVP_CIPHER_CTX_mode(ctx)) {
228 ctx->num = 0;
233 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
234 (int)sizeof(ctx->iv));
235 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
236 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
240 ctx->num = 0;
243 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
252 if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
253 if(!ctx->cipher->init(ctx,key,iv,enc)) return 0;
255 ctx->buf_len=0;
256 ctx->final_used=0;
257 ctx->block_mask=ctx->cipher->block_size-1;
261 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
264 if (ctx->encrypt)
265 return EVP_EncryptUpdate(ctx,out,outl,in,inl);
266 else return EVP_DecryptUpdate(ctx,out,outl,in,inl);
269 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
271 if (ctx->encrypt)
272 return EVP_EncryptFinal_ex(ctx,out,outl);
273 else return EVP_DecryptFinal_ex(ctx,out,outl);
276 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
278 if (ctx->encrypt)
279 return EVP_EncryptFinal(ctx,out,outl);
280 else return EVP_DecryptFinal(ctx,out,outl);
283 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
286 return EVP_CipherInit(ctx, cipher, key, iv, 1);
289 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl,
292 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
295 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
298 return EVP_CipherInit(ctx, cipher, key, iv, 0);
301 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
304 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
307 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
312 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)
314 i = M_do_cipher(ctx, out, in, inl);
328 if(ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0)
330 if(M_do_cipher(ctx,out,in,inl))
341 i=ctx->buf_len;
342 bl=ctx->cipher->block_size;
343 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
348 memcpy(&(ctx->buf[i]),in,inl);
349 ctx->buf_len+=inl;
356 memcpy(&(ctx->buf[i]),in,j);
357 if(!M_do_cipher(ctx,out,ctx->buf,bl)) return 0;
370 if(!M_do_cipher(ctx,out,in,inl)) return 0;
375 memcpy(ctx->buf,&(in[inl]),i);
376 ctx->buf_len=i;
380 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
383 ret = EVP_EncryptFinal_ex(ctx, out, outl);
387 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
392 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)
394 ret = M_do_cipher(ctx, out, NULL, 0);
402 b=ctx->cipher->block_size;
403 OPENSSL_assert(b <= sizeof ctx->buf);
409 bl=ctx->buf_len;
410 if (ctx->flags & EVP_CIPH_NO_PADDING)
423 ctx->buf[i]=n;
424 ret=M_do_cipher(ctx,out,ctx->buf,b);
433 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
439 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)
441 fix_len = M_do_cipher(ctx, out, in, inl);
458 if (ctx->flags & EVP_CIPH_NO_PADDING)
459 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
461 b=ctx->cipher->block_size;
462 OPENSSL_assert(b <= sizeof ctx->final);
464 if(ctx->final_used)
466 memcpy(out,ctx->final,b);
474 if(!EVP_EncryptUpdate(ctx,out,outl,in,inl))
479 if (b > 1 && !ctx->buf_len)
482 ctx->final_used=1;
483 memcpy(ctx->final,&out[*outl],b);
486 ctx->final_used = 0;
494 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
497 ret = EVP_DecryptFinal_ex(ctx, out, outl);
501 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
507 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER)
509 i = M_do_cipher(ctx, out, NULL, 0);
517 b=ctx->cipher->block_size;
518 if (ctx->flags & EVP_CIPH_NO_PADDING)
520 if(ctx->buf_len)
530 if (ctx->buf_len || !ctx->final_used)
535 OPENSSL_assert(b <= sizeof ctx->final);
536 n=ctx->final[b-1];
544 if (ctx->final[--b] != n)
550 n=ctx->cipher->block_size-n;
552 out[i]=ctx->final[i];
560 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
562 if (ctx)
564 EVP_CIPHER_CTX_cleanup(ctx);
565 OPENSSL_free(ctx);
610 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
612 if (pad) ctx->flags &= ~EVP_CIPH_NO_PADDING;
613 else ctx->flags |= EVP_CIPH_NO_PADDING;
617 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
620 if(!ctx->cipher) {
625 if(!ctx->cipher->ctrl) {
630 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
638 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
640 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
641 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
642 if (RAND_bytes(key, ctx->key_len) <= 0)