1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
|
/*
* Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define HC_DEPRECATED
#define HC_DEPRECATED_CRYPTO
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <evp.h>
#include <evp-hcrypto.h>
#include <evp-cc.h>
#include <krb5-types.h>
#include <roken.h>
#ifndef HCRYPTO_DEF_PROVIDER
#define HCRYPTO_DEF_PROVIDER hcrypto
#endif
#define HC_CONCAT4(x,y,z,aa) x ## y ## z ## aa
#define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
/**
* @page page_evp EVP - generic crypto interface
*
* See the library functions here: @ref hcrypto_evp
*
* @section evp_cipher EVP Cipher
*
* The use of EVP_CipherInit_ex() and EVP_Cipher() is pretty easy to
* understand forward, then EVP_CipherUpdate() and
* EVP_CipherFinal_ex() really needs an example to explain @ref
* example_evp_cipher.c .
*
* @example example_evp_cipher.c
*
* This is an example how to use EVP_CipherInit_ex(),
* EVP_CipherUpdate() and EVP_CipherFinal_ex().
*/
struct hc_EVP_MD_CTX {
const EVP_MD *md;
ENGINE *engine;
void *ptr;
};
/**
* Return the output size of the message digest function.
*
* @param md the evp message
*
* @return size output size of the message digest function.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_MD_size(const EVP_MD *md)
{
return md->hash_size;
}
/**
* Return the blocksize of the message digest function.
*
* @param md the evp message
*
* @return size size of the message digest block size
*
* @ingroup hcrypto_evp
*/
size_t
EVP_MD_block_size(const EVP_MD *md)
{
return md->block_size;
}
/**
* Allocate a messsage digest context object. Free with
* EVP_MD_CTX_destroy().
*
* @return a newly allocated message digest context object.
*
* @ingroup hcrypto_evp
*/
EVP_MD_CTX *
EVP_MD_CTX_create(void)
{
return calloc(1, sizeof(EVP_MD_CTX));
}
/**
* Initiate a messsage digest context object. Deallocate with
* EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
*
* @param ctx variable to initiate.
*
* @ingroup hcrypto_evp
*/
void
EVP_MD_CTX_init(EVP_MD_CTX *ctx) HC_DEPRECATED
{
memset(ctx, 0, sizeof(*ctx));
}
/**
* Free a messsage digest context object.
*
* @param ctx context to free.
*
* @ingroup hcrypto_evp
*/
void
EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
{
EVP_MD_CTX_cleanup(ctx);
free(ctx);
}
/**
* Free the resources used by the EVP_MD context.
*
* @param ctx the context to free the resources from.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) HC_DEPRECATED
{
if (ctx->md && ctx->md->cleanup)
(ctx->md->cleanup)(ctx);
else if (ctx->md)
memset(ctx->ptr, 0, ctx->md->ctx_size);
ctx->md = NULL;
ctx->engine = NULL;
free(ctx->ptr);
memset(ctx, 0, sizeof(*ctx));
return 1;
}
/**
* Get the EVP_MD use for a specified context.
*
* @param ctx the EVP_MD context to get the EVP_MD for.
*
* @return the EVP_MD used for the context.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_MD_CTX_md(EVP_MD_CTX *ctx)
{
return ctx->md;
}
/**
* Return the output size of the message digest function.
*
* @param ctx the evp message digest context
*
* @return size output size of the message digest function.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_MD_CTX_size(EVP_MD_CTX *ctx)
{
return EVP_MD_size(ctx->md);
}
/**
* Return the blocksize of the message digest function.
*
* @param ctx the evp message digest context
*
* @return size size of the message digest block size
*
* @ingroup hcrypto_evp
*/
size_t
EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
{
return EVP_MD_block_size(ctx->md);
}
/**
* Init a EVP_MD_CTX for use a specific message digest and engine.
*
* @param ctx the message digest context to init.
* @param md the message digest to use.
* @param engine the engine to use, NULL to use the default engine.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
{
if (ctx->md != md || ctx->engine != engine) {
EVP_MD_CTX_cleanup(ctx);
ctx->md = md;
ctx->engine = engine;
ctx->ptr = calloc(1, md->ctx_size);
if (ctx->ptr == NULL)
return 0;
}
(ctx->md->init)(ctx->ptr);
return 1;
}
/**
* Update the digest with some data.
*
* @param ctx the context to update
* @param data the data to update the context with
* @param size length of data
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
{
(ctx->md->update)(ctx->ptr, data, size);
return 1;
}
/**
* Complete the message digest.
*
* @param ctx the context to complete.
* @param hash the output of the message digest function. At least
* EVP_MD_size().
* @param size the output size of hash.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
{
(ctx->md->final)(hash, ctx->ptr);
if (size)
*size = ctx->md->hash_size;
return 1;
}
/**
* Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
* EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
* dance in one call.
*
* @param data the data to update the context with
* @param dsize length of data
* @param hash output data of at least EVP_MD_size() length.
* @param hsize output length of hash.
* @param md message digest to use
* @param engine engine to use, NULL for default engine.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
const EVP_MD *md, ENGINE *engine)
{
EVP_MD_CTX *ctx;
int ret;
ctx = EVP_MD_CTX_create();
if (ctx == NULL)
return 0;
ret = EVP_DigestInit_ex(ctx, md, engine);
if (ret != 1) {
EVP_MD_CTX_destroy(ctx);
return ret;
}
ret = EVP_DigestUpdate(ctx, data, dsize);
if (ret != 1) {
EVP_MD_CTX_destroy(ctx);
return ret;
}
ret = EVP_DigestFinal_ex(ctx, hash, hsize);
EVP_MD_CTX_destroy(ctx);
return ret;
}
/**
* The message digest SHA256
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_sha256(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha256);
}
/**
* The message digest SHA384
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_sha384(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha384);
}
/**
* The message digest SHA512
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_sha512(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha512);
}
/**
* The message digest SHA1
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_sha1(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha1);
}
/**
* The message digest SHA1
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_sha(void) HC_DEPRECATED
{
hcrypto_validate();
return EVP_sha1();
}
/**
* The message digest MD5
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_md5(void) HC_DEPRECATED_CRYPTO
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md5);
}
/**
* The message digest MD4
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_md4(void) HC_DEPRECATED_CRYPTO
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md4);
}
/**
* The message digest MD2
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_md2(void) HC_DEPRECATED_CRYPTO
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md2);
}
/*
*
*/
static void
null_Init (void *m)
{
}
static void
null_Update (void *m, const void * data, size_t size)
{
}
static void
null_Final(void *res, void *m)
{
}
/**
* The null message digest
*
* @return the message digest type.
*
* @ingroup hcrypto_evp
*/
const EVP_MD *
EVP_md_null(void)
{
static const struct hc_evp_md null = {
0,
0,
0,
(hc_evp_md_init)null_Init,
(hc_evp_md_update)null_Update,
(hc_evp_md_final)null_Final,
NULL
};
return &null;
}
/**
* Return the block size of the cipher.
*
* @param c cipher to get the block size from.
*
* @return the block size of the cipher.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_CIPHER_block_size(const EVP_CIPHER *c)
{
return c->block_size;
}
/**
* Return the key size of the cipher.
*
* @param c cipher to get the key size from.
*
* @return the key size of the cipher.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_CIPHER_key_length(const EVP_CIPHER *c)
{
return c->key_len;
}
/**
* Return the IV size of the cipher.
*
* @param c cipher to get the IV size from.
*
* @return the IV size of the cipher.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_CIPHER_iv_length(const EVP_CIPHER *c)
{
return c->iv_len;
}
/**
* Initiate a EVP_CIPHER_CTX context. Clean up with
* EVP_CIPHER_CTX_cleanup().
*
* @param c the cipher initiate.
*
* @ingroup hcrypto_evp
*/
void
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
{
memset(c, 0, sizeof(*c));
}
/**
* Clean up the EVP_CIPHER_CTX context.
*
* @param c the cipher to clean up.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
{
if (c->cipher && c->cipher->cleanup)
c->cipher->cleanup(c);
if (c->cipher_data) {
memset(c->cipher_data, 0, c->cipher->ctx_size);
free(c->cipher_data);
c->cipher_data = NULL;
}
return 1;
}
/**
* If the cipher type supports it, change the key length
*
* @param c the cipher context to change the key length for
* @param length new key length
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
{
if ((c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH) && length > 0) {
c->key_len = length;
return 1;
}
return 0;
}
#if 0
int
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
{
return 0;
}
#endif
/**
* Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
*
* @param ctx the context to get the cipher type from.
*
* @return the EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
{
return ctx->cipher;
}
/**
* Return the block size of the cipher context.
*
* @param ctx cipher context to get the block size from.
*
* @return the block size of the cipher context.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_block_size(ctx->cipher);
}
/**
* Return the key size of the cipher context.
*
* @param ctx cipher context to get the key size from.
*
* @return the key size of the cipher context.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_key_length(ctx->cipher);
}
/**
* Return the IV size of the cipher context.
*
* @param ctx cipher context to get the IV size from.
*
* @return the IV size of the cipher context.
*
* @ingroup hcrypto_evp
*/
size_t
EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_iv_length(ctx->cipher);
}
/**
* Get the flags for an EVP_CIPHER_CTX context.
*
* @param ctx the EVP_CIPHER_CTX to get the flags from
*
* @return the flags for an EVP_CIPHER_CTX.
*
* @ingroup hcrypto_evp
*/
unsigned long
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
{
return ctx->cipher->flags;
}
/**
* Get the mode for an EVP_CIPHER_CTX context.
*
* @param ctx the EVP_CIPHER_CTX to get the mode from
*
* @return the mode for an EVP_CIPHER_CTX.
*
* @ingroup hcrypto_evp
*/
int
EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
}
/**
* Get the app data for an EVP_CIPHER_CTX context.
*
* @param ctx the EVP_CIPHER_CTX to get the app data from
*
* @return the app data for an EVP_CIPHER_CTX.
*
* @ingroup hcrypto_evp
*/
void *
EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
{
return ctx->app_data;
}
/**
* Set the app data for an EVP_CIPHER_CTX context.
*
* @param ctx the EVP_CIPHER_CTX to set the app data for
* @param data the app data to set for an EVP_CIPHER_CTX.
*
* @ingroup hcrypto_evp
*/
void
EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
{
ctx->app_data = data;
}
/**
* Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
* Clean up with EVP_CIPHER_CTX_cleanup().
*
* @param ctx context to initiate
* @param c cipher to use.
* @param engine crypto engine to use, NULL to select default.
* @param key the crypto key to use, NULL will use the previous value.
* @param iv the IV to use, NULL will use the previous value.
* @param encp non zero will encrypt, -1 use the previous value.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
const void *key, const void *iv, int encp)
{
ctx->buf_len = 0;
if (encp == -1)
encp = ctx->encrypt;
else
ctx->encrypt = (encp ? 1 : 0);
if (c && (c != ctx->cipher)) {
EVP_CIPHER_CTX_cleanup(ctx);
ctx->cipher = c;
ctx->key_len = c->key_len;
ctx->cipher_data = calloc(1, c->ctx_size);
if (ctx->cipher_data == NULL && c->ctx_size != 0)
return 0;
/* assume block size is a multiple of 2 */
ctx->block_mask = EVP_CIPHER_block_size(c) - 1;
} else if (ctx->cipher == NULL) {
/* reuse of cipher, but not any cipher ever set! */
return 0;
}
switch (EVP_CIPHER_CTX_mode(ctx)) {
case EVP_CIPH_CBC_MODE:
assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
if (iv)
memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
break;
case EVP_CIPH_STREAM_CIPHER:
break;
case EVP_CIPH_CFB8_MODE:
if (iv)
memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
break;
default:
return 0;
}
if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
ctx->cipher->init(ctx, key, iv, encp);
return 1;
}
/**
* Encipher/decipher partial data
*
* @param ctx the cipher context.
* @param out output data from the operation.
* @param outlen output length
* @param in input data to the operation.
* @param inlen length of data.
*
* The output buffer length should at least be EVP_CIPHER_block_size()
* byte longer then the input length.
*
* See @ref evp_cipher for an example how to use this function.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
void *in, size_t inlen)
{
int ret, left, blocksize;
*outlen = 0;
/**
* If there in no spare bytes in the left from last Update and the
* input length is on the block boundery, the EVP_CipherUpdate()
* function can take a shortcut (and preformance gain) and
* directly encrypt the data, otherwise we hav to fix it up and
* store extra it the EVP_CIPHER_CTX.
*/
if (ctx->buf_len == 0 && (inlen & ctx->block_mask) == 0) {
ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
if (ret == 1)
*outlen = inlen;
else
*outlen = 0;
return ret;
}
blocksize = EVP_CIPHER_CTX_block_size(ctx);
left = blocksize - ctx->buf_len;
assert(left > 0);
if (ctx->buf_len) {
/* if total buffer is smaller then input, store locally */
if (inlen < left) {
memcpy(ctx->buf + ctx->buf_len, in, inlen);
ctx->buf_len += inlen;
return 1;
}
/* fill in local buffer and encrypt */
memcpy(ctx->buf + ctx->buf_len, in, left);
ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
memset(ctx->buf, 0, blocksize);
if (ret != 1)
return ret;
*outlen += blocksize;
inlen -= left;
in = ((unsigned char *)in) + left;
out = ((unsigned char *)out) + blocksize;
ctx->buf_len = 0;
}
if (inlen) {
ctx->buf_len = (inlen & ctx->block_mask);
inlen &= ~ctx->block_mask;
ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
if (ret != 1)
return ret;
*outlen += inlen;
in = ((unsigned char *)in) + inlen;
memcpy(ctx->buf, in, ctx->buf_len);
}
return 1;
}
/**
* Encipher/decipher final data
*
* @param ctx the cipher context.
* @param out output data from the operation.
* @param outlen output length
*
* The input length needs to be at least EVP_CIPHER_block_size() bytes
* long.
*
* See @ref evp_cipher for an example how to use this function.
*
* @return 1 on success.
*
* @ingroup hcrypto_evp
*/
int
EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen)
{
*outlen = 0;
if (ctx->buf_len) {
int ret, left, blocksize;
blocksize = EVP_CIPHER_CTX_block_size(ctx);
left = blocksize - ctx->buf_len;
assert(left > 0);
/* zero fill local buffer */
memset(ctx->buf + ctx->buf_len, 0, left);
ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
memset(ctx->buf, 0, blocksize);
if (ret != 1)
return ret;
*outlen += blocksize;
}
return 1;
}
/**
* Encipher/decipher data
*
* @param ctx the cipher context.
* @param out out data from the operation.
* @param in in data to the operation.
* @param size length of data.
*
* @return 1 on success.
*/
int
EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
{
return ctx->cipher->do_cipher(ctx, out, in, size);
}
/*
*
*/
static int
enc_null_init(EVP_CIPHER_CTX *ctx,
const unsigned char * key,
const unsigned char * iv,
int encp)
{
return 1;
}
static int
enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int size)
{
memmove(out, in, size);
return 1;
}
static int
enc_null_cleanup(EVP_CIPHER_CTX *ctx)
{
return 1;
}
/**
* The NULL cipher type, does no encryption/decryption.
*
* @return the null EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_enc_null(void)
{
static const EVP_CIPHER enc_null = {
0,
0,
0,
0,
EVP_CIPH_CBC_MODE,
enc_null_init,
enc_null_do_cipher,
enc_null_cleanup,
0,
NULL,
NULL,
NULL,
NULL
};
return &enc_null;
}
/**
* The RC2 cipher type
*
* @return the RC2 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_rc2_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_cbc);
}
/**
* The RC2 cipher type
*
* @return the RC2 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_rc2_40_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_40_cbc);
}
/**
* The RC2 cipher type
*
* @return the RC2 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_rc2_64_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_64_cbc);
}
/**
* The RC4 cipher type
*
* @return the RC4 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_rc4(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4);
}
/**
* The RC4-40 cipher type
*
* @return the RC4-40 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_rc4_40(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4_40);
}
/**
* The DES cipher type
*
* @return the DES-CBC EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_des_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_cbc);
}
/**
* The tripple DES cipher type
*
* @return the DES-EDE3-CBC EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_des_ede3_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_ede3_cbc);
}
/**
* The AES-128 cipher type
*
* @return the AES-128 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_128_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cbc);
}
/**
* The AES-192 cipher type
*
* @return the AES-192 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_192_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cbc);
}
/**
* The AES-256 cipher type
*
* @return the AES-256 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_256_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc);
}
/**
* The AES-128 cipher type
*
* @return the AES-128 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_128_cfb8(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8);
}
/**
* The AES-192 cipher type
*
* @return the AES-192 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_192_cfb8(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8);
}
/**
* The AES-256 cipher type
*
* @return the AES-256 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_256_cfb8(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8);
}
/**
* The Camellia-128 cipher type
*
* @return the Camellia-128 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_camellia_128_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_128_cbc);
}
/**
* The Camellia-198 cipher type
*
* @return the Camellia-198 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_camellia_192_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_192_cbc);
}
/**
* The Camellia-256 cipher type
*
* @return the Camellia-256 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_camellia_256_cbc(void)
{
hcrypto_validate();
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_256_cbc);
}
/*
*
*/
static const struct cipher_name {
const char *name;
const EVP_CIPHER *(*func)(void);
} cipher_name[] = {
{ "des-ede3-cbc", EVP_des_ede3_cbc },
{ "aes-128-cbc", EVP_aes_128_cbc },
{ "aes-192-cbc", EVP_aes_192_cbc },
{ "aes-256-cbc", EVP_aes_256_cbc },
{ "aes-128-cfb8", EVP_aes_128_cfb8 },
{ "aes-192-cfb8", EVP_aes_192_cfb8 },
{ "aes-256-cfb8", EVP_aes_256_cfb8 },
{ "camellia-128-cbc", EVP_camellia_128_cbc },
{ "camellia-192-cbc", EVP_camellia_192_cbc },
{ "camellia-256-cbc", EVP_camellia_256_cbc }
};
/**
* Get the cipher type using their name.
*
* @param name the name of the cipher.
*
* @return the selected EVP_CIPHER pointer or NULL if not found.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_get_cipherbyname(const char *name)
{
int i;
for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
if (strcasecmp(cipher_name[i].name, name) == 0)
return (*cipher_name[i].func)();
}
return NULL;
}
/*
*
*/
#ifndef min
#define min(a,b) (((a)>(b))?(b):(a))
#endif
/**
* Provides a legancy string to key function, used in PEM files.
*
* New protocols should use new string to key functions like NIST
* SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
*
* @param type type of cipher to use
* @param md message digest to use
* @param salt salt salt string, should be an binary 8 byte buffer.
* @param data the password/input key string.
* @param datalen length of data parameter.
* @param count iteration counter.
* @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
* @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
*
* @return the size of derived key.
*
* @ingroup hcrypto_evp
*/
int
EVP_BytesToKey(const EVP_CIPHER *type,
const EVP_MD *md,
const void *salt,
const void *data, size_t datalen,
unsigned int count,
void *keydata,
void *ivdata)
{
unsigned int ivlen, keylen;
int first = 0;
unsigned int mds = 0, i;
unsigned char *key = keydata;
unsigned char *iv = ivdata;
unsigned char *buf;
EVP_MD_CTX c;
keylen = EVP_CIPHER_key_length(type);
ivlen = EVP_CIPHER_iv_length(type);
if (data == NULL)
return keylen;
buf = malloc(EVP_MD_size(md));
if (buf == NULL)
return -1;
EVP_MD_CTX_init(&c);
first = 1;
while (1) {
EVP_DigestInit_ex(&c, md, NULL);
if (!first)
EVP_DigestUpdate(&c, buf, mds);
first = 0;
EVP_DigestUpdate(&c,data,datalen);
#define PKCS5_SALT_LEN 8
if (salt)
EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
EVP_DigestFinal_ex(&c, buf, &mds);
assert(mds == EVP_MD_size(md));
for (i = 1; i < count; i++) {
EVP_DigestInit_ex(&c, md, NULL);
EVP_DigestUpdate(&c, buf, mds);
EVP_DigestFinal_ex(&c, buf, &mds);
assert(mds == EVP_MD_size(md));
}
i = 0;
if (keylen) {
size_t sz = min(keylen, mds);
if (key) {
memcpy(key, buf, sz);
key += sz;
}
keylen -= sz;
i += sz;
}
if (ivlen && mds > i) {
size_t sz = min(ivlen, (mds - i));
if (iv) {
memcpy(iv, &buf[i], sz);
iv += sz;
}
ivlen -= sz;
}
if (keylen == 0 && ivlen == 0)
break;
}
EVP_MD_CTX_cleanup(&c);
free(buf);
return EVP_CIPHER_key_length(type);
}
/**
* Generate a random key for the specificed EVP_CIPHER.
*
* @param ctx EVP_CIPHER_CTX type to build the key for.
* @param key return key, must be at least EVP_CIPHER_key_length() byte long.
*
* @return 1 for success, 0 for failure.
*
* @ingroup hcrypto_core
*/
int
EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key)
{
if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
if (RAND_bytes(key, ctx->key_len) != 1)
return 0;
return 1;
}
/**
* Perform a operation on a ctx
*
* @param ctx context to perform operation on.
* @param type type of operation.
* @param arg argument to operation.
* @param data addition data to operation.
* @return 1 for success, 0 for failure.
*
* @ingroup hcrypto_core
*/
int
EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data)
{
if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL)
return 0;
return (*ctx->cipher->ctrl)(ctx, type, arg, data);
}
/**
* Add all algorithms to the crypto core.
*
* @ingroup hcrypto_core
*/
void
OpenSSL_add_all_algorithms(void)
{
return;
}
/**
* Add all algorithms to the crypto core using configuration file.
*
* @ingroup hcrypto_core
*/
void
OpenSSL_add_all_algorithms_conf(void)
{
return;
}
/**
* Add all algorithms to the crypto core, but don't use the
* configuration file.
*
* @ingroup hcrypto_core
*/
void
OpenSSL_add_all_algorithms_noconf(void)
{
return;
}
|