Lines Matching defs:ctx

70 void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) {
71 OPENSSL_memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
75 EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
76 if (ctx) {
77 EVP_CIPHER_CTX_init(ctx);
79 return ctx;
92 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) {
93 if (ctx) {
94 EVP_CIPHER_CTX_cleanup(ctx);
95 OPENSSL_free(ctx);
128 void EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) {
129 EVP_CIPHER_CTX_cleanup(ctx);
130 EVP_CIPHER_CTX_init(ctx);
133 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
137 enc = ctx->encrypt;
142 ctx->encrypt = enc;
149 if (ctx->cipher) {
150 EVP_CIPHER_CTX_cleanup(ctx);
152 ctx->encrypt = enc;
155 ctx->cipher = cipher;
156 if (ctx->cipher->ctx_size) {
157 ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
158 if (!ctx->cipher_data) {
159 ctx->cipher = NULL;
164 ctx->cipher_data = NULL;
167 ctx->key_len = cipher->key_len;
168 ctx->flags = 0;
170 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
171 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
172 ctx->cipher = NULL;
177 } else if (!ctx->cipher) {
183 assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 ||
184 ctx->cipher->block_size == 16);
186 if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
187 switch (EVP_CIPHER_CTX_mode(ctx)) {
193 ctx->num = 0;
197 assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
199 OPENSSL_memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
201 OPENSSL_memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
206 ctx->num = 0;
209 OPENSSL_memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
218 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
219 if (!ctx->cipher->init(ctx, key, iv, enc)) {
224 ctx->buf_len = 0;
225 ctx->final_used = 0;
226 ctx->block_mask = ctx->cipher->block_size - 1;
230 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
232 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
235 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
237 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
240 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
244 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
245 i = ctx->cipher->cipher(ctx, out, in, in_len);
259 if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
260 if (ctx->cipher->cipher(ctx, out, in, in_len)) {
269 i = ctx->buf_len;
270 bl = ctx->cipher->block_size;
271 assert(bl <= (int)sizeof(ctx->buf));
274 OPENSSL_memcpy(&ctx->buf[i], in, in_len);
275 ctx->buf_len += in_len;
280 OPENSSL_memcpy(&ctx->buf[i], in, j);
281 if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
293 i = in_len & ctx->block_mask;
296 if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
303 OPENSSL_memcpy(ctx->buf, &in[in_len], i);
305 ctx->buf_len = i;
309 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
313 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
314 ret = ctx->cipher->cipher(ctx, out, NULL, 0);
323 b = ctx->cipher->block_size;
324 assert(b <= sizeof(ctx->buf));
330 bl = ctx->buf_len;
331 if (ctx->flags & EVP_CIPH_NO_PADDING) {
342 ctx->buf[i] = n;
344 ret = ctx->cipher->cipher(ctx, out, ctx->buf, b);
353 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
358 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
359 int r = ctx->cipher->cipher(ctx, out, in, in_len);
374 if (ctx->flags & EVP_CIPH_NO_PADDING) {
375 return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
378 b = ctx->cipher->block_size;
379 assert(b <= sizeof(ctx->final));
381 if (ctx->final_used) {
382 OPENSSL_memcpy(out, ctx->final, b);
389 if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
395 if (b > 1 && !ctx->buf_len) {
397 ctx->final_used = 1;
398 OPENSSL_memcpy(ctx->final, &out[*out_len], b);
400 ctx->final_used = 0;
410 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) {
415 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
416 i = ctx->cipher->cipher(ctx, out, NULL, 0);
425 b = ctx->cipher->block_size;
426 if (ctx->flags & EVP_CIPH_NO_PADDING) {
427 if (ctx->buf_len) {
436 if (ctx->buf_len || !ctx->final_used) {
440 assert(b <= sizeof(ctx->final));
444 n = ctx->final[b - 1];
451 if (ctx->final[--b] != n) {
457 n = ctx->cipher->block_size - n;
459 out[i] = ctx->final[i];
469 int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
471 return ctx->cipher->cipher(ctx, out, in, in_len);
474 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
476 if (ctx->encrypt) {
477 return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
479 return EVP_DecryptUpdate(ctx, out, out_len, in, in_len);
483 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
484 if (ctx->encrypt) {
485 return EVP_EncryptFinal_ex(ctx, out, out_len);
487 return EVP_DecryptFinal_ex(ctx, out, out_len);
491 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) {
492 return ctx->cipher;
495 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
496 return ctx->cipher->nid;
499 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx) {
500 return ctx->encrypt;
503 unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
504 return ctx->cipher->block_size;
507 unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) {
508 return ctx->key_len;
511 unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) {
512 return ctx->cipher->iv_len;
515 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) {
516 return ctx->app_data;
519 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) {
520 ctx->app_data = data;
523 uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) {
524 return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK;
527 uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) {
528 return ctx->cipher->flags & EVP_CIPH_MODE_MASK;
531 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) {
533 if (!ctx->cipher) {
538 if (!ctx->cipher->ctrl) {
543 ret = ctx->cipher->ctrl(ctx, command, arg, ptr);
552 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) {
554 ctx->flags &= ~EVP_CIPH_NO_PADDING;
556 ctx->flags |= EVP_CIPH_NO_PADDING;
597 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
600 EVP_CIPHER_CTX_init(ctx);
602 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
605 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
607 return EVP_CipherInit(ctx, cipher, key, iv, 1);
610 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
612 return EVP_CipherInit(ctx, cipher, key, iv, 0);
619 void EVP_CIPHER_CTX_set_flags(const EVP_CIPHER_CTX *ctx, uint32_t flags) {}