1 /* 2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <stdio.h> 11 #include "internal/cryptlib.h" 12 #include <openssl/evp.h> 13 #include <openssl/objects.h> 14 #include "internal/evp_int.h" 15 #include "evp_locl.h" 16 17 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 18 { 19 int ret; 20 21 if (c->cipher->set_asn1_parameters != NULL) 22 ret = c->cipher->set_asn1_parameters(c, type); 23 else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) { 24 switch (EVP_CIPHER_CTX_mode(c)) { 25 case EVP_CIPH_WRAP_MODE: 26 if (EVP_CIPHER_CTX_nid(c) == NID_id_smime_alg_CMS3DESwrap) 27 ASN1_TYPE_set(type, V_ASN1_NULL, NULL); 28 ret = 1; 29 break; 30 31 case EVP_CIPH_GCM_MODE: 32 case EVP_CIPH_CCM_MODE: 33 case EVP_CIPH_XTS_MODE: 34 case EVP_CIPH_OCB_MODE: 35 ret = -2; 36 break; 37 38 default: 39 ret = EVP_CIPHER_set_asn1_iv(c, type); 40 } 41 } else 42 ret = -1; 43 if (ret <= 0) 44 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ret == -2 ? 45 ASN1_R_UNSUPPORTED_CIPHER : 46 EVP_R_CIPHER_PARAMETER_ERROR); 47 if (ret < -1) 48 ret = -1; 49 return ret; 50 } 51 52 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 53 { 54 int ret; 55 56 if (c->cipher->get_asn1_parameters != NULL) 57 ret = c->cipher->get_asn1_parameters(c, type); 58 else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) { 59 switch (EVP_CIPHER_CTX_mode(c)) { 60 61 case EVP_CIPH_WRAP_MODE: 62 ret = 1; 63 break; 64 65 case EVP_CIPH_GCM_MODE: 66 case EVP_CIPH_CCM_MODE: 67 case EVP_CIPH_XTS_MODE: 68 case EVP_CIPH_OCB_MODE: 69 ret = -2; 70 break; 71 72 default: 73 ret = EVP_CIPHER_get_asn1_iv(c, type); 74 break; 75 } 76 } else 77 ret = -1; 78 if (ret <= 0) 79 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, ret == -2 ? 80 EVP_R_UNSUPPORTED_CIPHER : 81 EVP_R_CIPHER_PARAMETER_ERROR); 82 if (ret < -1) 83 ret = -1; 84 return ret; 85 } 86 87 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 88 { 89 int i = 0; 90 unsigned int l; 91 92 if (type != NULL) { 93 l = EVP_CIPHER_CTX_iv_length(c); 94 OPENSSL_assert(l <= sizeof(c->iv)); 95 i = ASN1_TYPE_get_octetstring(type, c->oiv, l); 96 if (i != (int)l) 97 return -1; 98 else if (i > 0) 99 memcpy(c->iv, c->oiv, l); 100 } 101 return i; 102 } 103 104 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 105 { 106 int i = 0; 107 unsigned int j; 108 109 if (type != NULL) { 110 j = EVP_CIPHER_CTX_iv_length(c); 111 OPENSSL_assert(j <= sizeof(c->iv)); 112 i = ASN1_TYPE_set_octetstring(type, c->oiv, j); 113 } 114 return i; 115 } 116 117 /* Convert the various cipher NIDs and dummies to a proper OID NID */ 118 int EVP_CIPHER_type(const EVP_CIPHER *ctx) 119 { 120 int nid; 121 ASN1_OBJECT *otmp; 122 nid = EVP_CIPHER_nid(ctx); 123 124 switch (nid) { 125 126 case NID_rc2_cbc: 127 case NID_rc2_64_cbc: 128 case NID_rc2_40_cbc: 129 130 return NID_rc2_cbc; 131 132 case NID_rc4: 133 case NID_rc4_40: 134 135 return NID_rc4; 136 137 case NID_aes_128_cfb128: 138 case NID_aes_128_cfb8: 139 case NID_aes_128_cfb1: 140 141 return NID_aes_128_cfb128; 142 143 case NID_aes_192_cfb128: 144 case NID_aes_192_cfb8: 145 case NID_aes_192_cfb1: 146 147 return NID_aes_192_cfb128; 148 149 case NID_aes_256_cfb128: 150 case NID_aes_256_cfb8: 151 case NID_aes_256_cfb1: 152 153 return NID_aes_256_cfb128; 154 155 case NID_des_cfb64: 156 case NID_des_cfb8: 157 case NID_des_cfb1: 158 159 return NID_des_cfb64; 160 161 case NID_des_ede3_cfb64: 162 case NID_des_ede3_cfb8: 163 case NID_des_ede3_cfb1: 164 165 return NID_des_cfb64; 166 167 default: 168 /* Check it has an OID and it is valid */ 169 otmp = OBJ_nid2obj(nid); 170 if (OBJ_get0_data(otmp) == NULL) 171 nid = NID_undef; 172 ASN1_OBJECT_free(otmp); 173 return nid; 174 } 175 } 176 177 int EVP_CIPHER_block_size(const EVP_CIPHER *e) 178 { 179 return e->block_size; 180 } 181 182 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) 183 { 184 return ctx->cipher->block_size; 185 } 186 187 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e) 188 { 189 return e->ctx_size; 190 } 191 192 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 193 const unsigned char *in, unsigned int inl) 194 { 195 return ctx->cipher->do_cipher(ctx, out, in, inl); 196 } 197 198 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) 199 { 200 return ctx->cipher; 201 } 202 203 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx) 204 { 205 return ctx->encrypt; 206 } 207 208 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher) 209 { 210 return cipher->flags; 211 } 212 213 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) 214 { 215 return ctx->app_data; 216 } 217 218 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) 219 { 220 ctx->app_data = data; 221 } 222 223 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx) 224 { 225 return ctx->cipher_data; 226 } 227 228 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data) 229 { 230 void *old_cipher_data; 231 232 old_cipher_data = ctx->cipher_data; 233 ctx->cipher_data = cipher_data; 234 235 return old_cipher_data; 236 } 237 238 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) 239 { 240 return cipher->iv_len; 241 } 242 243 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) 244 { 245 int i, rv; 246 247 if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) { 248 rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN, 249 0, &i); 250 return (rv == 1) ? i : -1; 251 } 252 return ctx->cipher->iv_len; 253 } 254 255 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx) 256 { 257 return ctx->oiv; 258 } 259 260 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx) 261 { 262 return ctx->iv; 263 } 264 265 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx) 266 { 267 return ctx->iv; 268 } 269 270 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx) 271 { 272 return ctx->buf; 273 } 274 275 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx) 276 { 277 return ctx->num; 278 } 279 280 void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num) 281 { 282 ctx->num = num; 283 } 284 285 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher) 286 { 287 return cipher->key_len; 288 } 289 290 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) 291 { 292 return ctx->key_len; 293 } 294 295 int EVP_CIPHER_nid(const EVP_CIPHER *cipher) 296 { 297 return cipher->nid; 298 } 299 300 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) 301 { 302 return ctx->cipher->nid; 303 } 304 305 int EVP_MD_block_size(const EVP_MD *md) 306 { 307 return md->block_size; 308 } 309 310 int EVP_MD_type(const EVP_MD *md) 311 { 312 return md->type; 313 } 314 315 int EVP_MD_pkey_type(const EVP_MD *md) 316 { 317 return md->pkey_type; 318 } 319 320 int EVP_MD_size(const EVP_MD *md) 321 { 322 if (!md) { 323 EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL); 324 return -1; 325 } 326 return md->md_size; 327 } 328 329 unsigned long EVP_MD_flags(const EVP_MD *md) 330 { 331 return md->flags; 332 } 333 334 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type) 335 { 336 EVP_MD *md = OPENSSL_zalloc(sizeof(*md)); 337 338 if (md != NULL) { 339 md->type = md_type; 340 md->pkey_type = pkey_type; 341 } 342 return md; 343 } 344 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md) 345 { 346 EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type); 347 348 if (to != NULL) 349 memcpy(to, md, sizeof(*to)); 350 return to; 351 } 352 void EVP_MD_meth_free(EVP_MD *md) 353 { 354 OPENSSL_free(md); 355 } 356 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize) 357 { 358 md->block_size = blocksize; 359 return 1; 360 } 361 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize) 362 { 363 md->md_size = resultsize; 364 return 1; 365 } 366 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize) 367 { 368 md->ctx_size = datasize; 369 return 1; 370 } 371 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags) 372 { 373 md->flags = flags; 374 return 1; 375 } 376 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx)) 377 { 378 md->init = init; 379 return 1; 380 } 381 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, 382 const void *data, 383 size_t count)) 384 { 385 md->update = update; 386 return 1; 387 } 388 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, 389 unsigned char *md)) 390 { 391 md->final = final; 392 return 1; 393 } 394 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, 395 const EVP_MD_CTX *from)) 396 { 397 md->copy = copy; 398 return 1; 399 } 400 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx)) 401 { 402 md->cleanup = cleanup; 403 return 1; 404 } 405 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, 406 int p1, void *p2)) 407 { 408 md->md_ctrl = ctrl; 409 return 1; 410 } 411 412 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md) 413 { 414 return md->block_size; 415 } 416 int EVP_MD_meth_get_result_size(const EVP_MD *md) 417 { 418 return md->md_size; 419 } 420 int EVP_MD_meth_get_app_datasize(const EVP_MD *md) 421 { 422 return md->ctx_size; 423 } 424 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md) 425 { 426 return md->flags; 427 } 428 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx) 429 { 430 return md->init; 431 } 432 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx, 433 const void *data, 434 size_t count) 435 { 436 return md->update; 437 } 438 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx, 439 unsigned char *md) 440 { 441 return md->final; 442 } 443 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to, 444 const EVP_MD_CTX *from) 445 { 446 return md->copy; 447 } 448 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx) 449 { 450 return md->cleanup; 451 } 452 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd, 453 int p1, void *p2) 454 { 455 return md->md_ctrl; 456 } 457 458 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) 459 { 460 if (!ctx) 461 return NULL; 462 return ctx->digest; 463 } 464 465 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) 466 { 467 return ctx->pctx; 468 } 469 470 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx) 471 { 472 /* 473 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so 474 * we have to deal with the cleanup job here. 475 */ 476 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) 477 EVP_PKEY_CTX_free(ctx->pctx); 478 479 ctx->pctx = pctx; 480 481 if (pctx != NULL) { 482 /* make sure pctx is not freed when destroying EVP_MD_CTX */ 483 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); 484 } else { 485 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); 486 } 487 } 488 489 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx) 490 { 491 return ctx->md_data; 492 } 493 494 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx, 495 const void *data, size_t count) 496 { 497 return ctx->update; 498 } 499 500 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx, 501 int (*update) (EVP_MD_CTX *ctx, 502 const void *data, size_t count)) 503 { 504 ctx->update = update; 505 } 506 507 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) 508 { 509 ctx->flags |= flags; 510 } 511 512 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags) 513 { 514 ctx->flags &= ~flags; 515 } 516 517 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags) 518 { 519 return (ctx->flags & flags); 520 } 521 522 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags) 523 { 524 ctx->flags |= flags; 525 } 526 527 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags) 528 { 529 ctx->flags &= ~flags; 530 } 531 532 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags) 533 { 534 return (ctx->flags & flags); 535 } 536