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
|
/********************************************************************************/
/* */
/* TSS Library Dependent Crypto Support */
/* Written by Ken Goldman */
/* IBM Thomas J. Watson Research Center */
/* ECC Salt functions written by Bill Martin */
/* $Id: tsscrypto.c 1005 2017-05-05 16:18:34Z kgoldman $ */
/* */
/* (c) Copyright IBM Corporation 2015. */
/* */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions are */
/* met: */
/* */
/* Redistributions of source code must retain the above copyright notice, */
/* this list of conditions and the following disclaimer. */
/* */
/* 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. */
/* */
/* Neither the names of the IBM Corporation 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 COPYRIGHT HOLDERS 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 COPYRIGHT */
/* HOLDER 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. */
/********************************************************************************/
/* Interface to OpenSSL version 1.0 crypto library */
#include <string.h>
#include <stdio.h>
#ifdef TPM_POSIX
#include <netinet/in.h>
#endif
#ifdef TPM_WINDOWS
#include <winsock2.h>
#endif
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/aes.h>
#include <openssl/rsa.h>
#include <openssl/rand.h>
#include <openssl/engine.h>
#include <tss2/tssresponsecode.h>
#include <tss2/tssutils.h>
#include <tss2/tssprint.h>
#include <tss2/tsserror.h>
#include <tss2/tsscryptoh.h>
#include <tss2/tsscrypto.h>
extern int tssVverbose;
extern int tssVerbose;
/* local prototypes */
static TPM_RC TSS_Hash_GetMd(const EVP_MD **md,
TPMI_ALG_HASH hashAlg);
static TPM_RC TSS_ECC_GeneratePlatformEphemeralKey(CURVE_DATA *eCurveData,
EC_KEY *myecc);
static TPM_RC TSS_BN_new(BIGNUM **bn);
static TPM_RC TSS_BN_hex2bn(BIGNUM **bn, const char *str);
static TPM_RC TSS_bin2bn(BIGNUM **bn, const unsigned char *bin, unsigned int bytes);
/*
Initialization
*/
TPM_RC TSS_Crypto_Init(void)
{
TPM_RC rc = 0;
ERR_load_crypto_strings ();
OpenSSL_add_all_algorithms();
return rc;
}
/*
Digests
*/
static TPM_RC TSS_Hash_GetMd(const EVP_MD **md,
TPMI_ALG_HASH hashAlg)
{
TPM_RC rc = 0;
if (rc == 0) {
switch (hashAlg) {
#ifdef TPM_ALG_SHA1
case TPM_ALG_SHA1:
*md = EVP_get_digestbyname("sha1");
break;
#endif
#ifdef TPM_ALG_SHA256
case TPM_ALG_SHA256:
*md = EVP_get_digestbyname("sha256");
break;
#endif
#ifdef TPM_ALG_SHA384
case TPM_ALG_SHA384:
*md = EVP_get_digestbyname("sha384");
break;
#endif
#ifdef TPM_ALG_SHA512
case TPM_ALG_SHA512:
*md = EVP_get_digestbyname("sha512");
break;
#endif
default:
rc = TSS_RC_BAD_HASH_ALGORITHM;
}
}
return rc;
}
/* On call, digest->hashAlg is the desired hash algorithm
length 0 is ignored, buffer NULL terminates list.
*/
TPM_RC TSS_HMAC_Generate_valist(TPMT_HA *digest, /* largest size of a digest */
const TPM2B_KEY *hmacKey,
va_list ap)
{
TPM_RC rc = 0;
int irc = 0;
int done = FALSE;
const EVP_MD *md; /* message digest method */
#if OPENSSL_VERSION_NUMBER < 0x10100000
HMAC_CTX ctx;
#else
HMAC_CTX *ctx;
#endif
int length;
uint8_t *buffer;
#if OPENSSL_VERSION_NUMBER < 0x10100000
HMAC_CTX_init(&ctx);
#else
ctx = HMAC_CTX_new();
#endif
if (rc == 0) {
rc = TSS_Hash_GetMd(&md, digest->hashAlg);
}
if (rc == 0) {
#if OPENSSL_VERSION_NUMBER < 0x10100000
irc = HMAC_Init_ex(&ctx,
hmacKey->b.buffer, hmacKey->b.size, /* HMAC key */
md, /* message digest method */
NULL);
#else
irc = HMAC_Init_ex(ctx,
hmacKey->b.buffer, hmacKey->b.size, /* HMAC key */
md, /* message digest method */
NULL);
#endif
if (irc == 0) {
rc = TSS_RC_HMAC;
}
}
while ((rc == 0) && !done) {
length = va_arg(ap, int); /* first vararg is the length */
buffer = va_arg(ap, unsigned char *); /* second vararg is the array */
if (buffer != NULL) { /* loop until a NULL buffer terminates */
if (length < 0) {
if (tssVerbose) printf("TSS_HMAC_Generate: Length is negative\n");
rc = TSS_RC_HMAC;
}
else {
#if OPENSSL_VERSION_NUMBER < 0x10100000
irc = HMAC_Update(&ctx, buffer, length);
#else
irc = HMAC_Update(ctx, buffer, length);
#endif
if (irc == 0) {
if (tssVerbose) printf("TSS_HMAC_Generate: HMAC_Update failed\n");
rc = TSS_RC_HMAC;
}
}
}
else {
done = TRUE;
}
}
if (rc == 0) {
#if OPENSSL_VERSION_NUMBER < 0x10100000
irc = HMAC_Final(&ctx, (uint8_t *)&digest->digest, NULL);
#else
irc = HMAC_Final(ctx, (uint8_t *)&digest->digest, NULL);
#endif
if (irc == 0) {
rc = TSS_RC_HMAC;
}
}
#if OPENSSL_VERSION_NUMBER < 0x10100000
HMAC_CTX_cleanup(&ctx);
#else
HMAC_CTX_free(ctx);
#endif
return rc;
}
/*
valist is int length, unsigned char *buffer pairs
length 0 is ignored, buffer NULL terminates list.
*/
TPM_RC TSS_Hash_Generate_valist(TPMT_HA *digest, /* largest size of a digest */
va_list ap)
{
TPM_RC rc = 0;
int irc = 0;
int done = FALSE;
int length;
uint8_t *buffer;
EVP_MD_CTX *mdctx;
const EVP_MD *md;
if (rc == 0) {
mdctx = EVP_MD_CTX_create();
if (mdctx == NULL) {
if (tssVerbose) printf("TSS_Hash_Generate: malloc EVP_MD_CTX failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
rc = TSS_Hash_GetMd(&md, digest->hashAlg);
}
if (rc == 0) {
irc = EVP_DigestInit_ex(mdctx, md, NULL);
if (irc != 1) {
rc = TSS_RC_HASH;
}
}
while ((rc == 0) && !done) {
length = va_arg(ap, int); /* first vararg is the length */
buffer = va_arg(ap, unsigned char *); /* second vararg is the array */
if (buffer != NULL) { /* loop until a NULL buffer terminates */
if (length < 0) {
if (tssVerbose) printf("TSS_Hash_Generate: Length is negative\n");
rc = TSS_RC_HASH;
}
else {
if (length != 0) {
EVP_DigestUpdate(mdctx, buffer, length);
}
}
}
else {
done = TRUE;
}
}
if (rc == 0) {
EVP_DigestFinal_ex(mdctx, (uint8_t *)&digest->digest, NULL);
}
EVP_MD_CTX_destroy(mdctx);
return rc;
}
/* Random Numbers */
TPM_RC TSS_RandBytes(unsigned char *buffer, uint32_t size)
{
TPM_RC rc = 0;
int irc = 0;
irc = RAND_bytes(buffer, size);
if (irc != 1) {
if (tssVerbose) printf("TSS_RandBytes: Random number generation failed\n");
rc = TSS_RC_RNG_FAILURE;
}
return rc;
}
/*
RSA functions
*/
/* TSS_RSAGeneratePublicToken() generates an RSA key token from n and e
*/
TPM_RC TSS_RSAGeneratePublicToken(RSA **rsa_pub_key, /* freed by caller */
const unsigned char *narr, /* public modulus */
uint32_t nbytes,
const unsigned char *earr, /* public exponent */
uint32_t ebytes)
{
TPM_RC rc = 0;
BIGNUM * n = NULL;
BIGNUM * e = NULL;
/* sanity check for the free */
if (rc == 0) {
if (*rsa_pub_key != NULL) {
if (tssVerbose)
printf("TSS_RSAGeneratePublicToken: Error (fatal), token %p should be NULL\n",
*rsa_pub_key );
rc = TSS_RC_ALLOC_INPUT;
}
}
/* construct the OpenSSL private key object */
if (rc == 0) {
*rsa_pub_key = RSA_new(); /* freed by caller */
if (*rsa_pub_key == NULL) {
if (tssVerbose) printf("TSS_RSAGeneratePublicToken: Error in RSA_new()\n");
rc = TSS_RC_RSA_KEY_CONVERT;
}
}
if (rc == 0) {
rc = TSS_bin2bn(&n, narr, nbytes); /* freed by caller */
}
if (rc == 0) {
rc = TSS_bin2bn(&e, earr, ebytes); /* freed by caller */
}
if (rc == 0) {
#if OPENSSL_VERSION_NUMBER < 0x10100000
(*rsa_pub_key)->n = n;
(*rsa_pub_key)->e = e;
(*rsa_pub_key)->d = NULL;
#else
int irc = RSA_set0_key(*rsa_pub_key, n, e, NULL);
if (irc != 1) {
if (tssVerbose) printf("TSS_RSAGeneratePublicToken: Error in RSA_set0_key()\n");
rc = TSS_RC_RSA_KEY_CONVERT;
}
#endif
}
return rc;
}
/* TSS_RSAPublicEncrypt() pads 'decrypt_data' to 'encrypt_data_size' and encrypts using the public
key 'n, e'.
*/
TPM_RC TSS_RSAPublicEncrypt(unsigned char *encrypt_data, /* encrypted data */
size_t encrypt_data_size, /* size of encrypted data buffer */
const unsigned char *decrypt_data, /* decrypted data */
size_t decrypt_data_size,
unsigned char *narr, /* public modulus */
uint32_t nbytes,
unsigned char *earr, /* public exponent */
uint32_t ebytes,
unsigned char *p, /* encoding parameter */
int pl,
TPMI_ALG_HASH halg) /* OAEP hash algorithm */
{
TPM_RC rc = 0;
int irc;
RSA *rsa_pub_key = NULL;
unsigned char *padded_data = NULL;
if (tssVverbose) printf(" TSS_RSAPublicEncrypt: Input data size %lu\n",
(unsigned long)decrypt_data_size);
/* intermediate buffer for the decrypted but still padded data */
if (rc == 0) {
rc = TSS_Malloc(&padded_data, encrypt_data_size); /* freed @2 */
}
/* construct the OpenSSL public key object */
if (rc == 0) {
rc = TSS_RSAGeneratePublicToken(&rsa_pub_key, /* freed @1 */
narr, /* public modulus */
nbytes,
earr, /* public exponent */
ebytes);
}
if (rc == 0) {
padded_data[0] = 0x00;
rc = TSS_RSA_padding_add_PKCS1_OAEP(padded_data, /* to */
encrypt_data_size, /* to length */
decrypt_data, /* from */
decrypt_data_size, /* from length */
p, /* encoding parameter */
pl, /* encoding parameter length */
halg); /* OAEP hash algorithm */
}
if (rc == 0) {
if (tssVverbose)
printf(" TSS_RSAPublicEncrypt: Padded data size %lu\n",
(unsigned long)encrypt_data_size);
if (tssVverbose) TSS_PrintAll(" TPM_RSAPublicEncrypt: Padded data", padded_data,
encrypt_data_size);
/* encrypt with public key. Must pad first and then encrypt because the encrypt
call cannot specify an encoding parameter */
/* returns the size of the encrypted data. On error, -1 is returned */
irc = RSA_public_encrypt(encrypt_data_size, /* from length */
padded_data, /* from - the clear text data */
encrypt_data, /* the padded and encrypted data */
rsa_pub_key, /* key */
RSA_NO_PADDING); /* padding */
if (irc < 0) {
if (tssVerbose) printf("TSS_RSAPublicEncrypt: Error in RSA_public_encrypt()\n");
rc = TSS_RC_RSA_ENCRYPT;
}
}
if (rc == 0) {
if (tssVverbose) printf(" TSS_RSAPublicEncrypt: RSA_public_encrypt() success\n");
}
if (rsa_pub_key != NULL) {
RSA_free(rsa_pub_key); /* @1 */
}
free(padded_data); /* @2 */
return rc;
}
/* TSS_GeneratePlatformEphemeralKey sets the EC parameters to NIST P256 for generating the ephemeral
key. Some OpenSSL versions do not come with NIST p256. */
static TPM_RC TSS_ECC_GeneratePlatformEphemeralKey(CURVE_DATA *eCurveData, EC_KEY *myecc)
{
TPM_RC rc = 0;
BIGNUM *p = NULL;
BIGNUM *a = NULL;
BIGNUM *b = NULL;
BIGNUM *x = NULL;
BIGNUM *y = NULL;
BIGNUM *z = NULL;
EC_POINT *G = NULL; /* generator */
/* ---------------------------------------------------------- *
* Set the EC parameters to NISTp256. Openssl versions might *
* not have NISTP256 as a possible parameter so we make it *
* possible by setting the curve ourselves. *
* ---------------------------------------------------------- */
/* NIST P256 from FIPS 186-3 */
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Converting p\n");
rc = TSS_BN_hex2bn(&p, /* freed @1 */
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Converting a\n");
rc = TSS_BN_hex2bn(&a, /* freed @2 */
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Converting b\n");
rc = TSS_BN_hex2bn(&b, /* freed @3 */
"5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: New group\n");
eCurveData->G = EC_GROUP_new(EC_GFp_mont_method()); /* freed @4 */
if (eCurveData->G == NULL) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: "
"Error creating new group\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Set the curve prime\n");
if (EC_GROUP_set_curve_GFp(eCurveData->G, p, a, b, eCurveData->ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: "
"Error seting curve prime\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
G = EC_POINT_new(eCurveData->G); /* freed @5 */
if (G == NULL ){
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: EC_POINT_new failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
rc = TSS_BN_hex2bn(&x, /* freed @6 */
"6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
}
if (rc == 0) {
rc = TSS_BN_hex2bn(&y, /* freed @7 */
"4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5");
}
if (rc == 0) {
if (EC_POINT_set_affine_coordinates_GFp(eCurveData->G, G, x, y, eCurveData->ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Error, "
"Cannot create TPM public point from coordinates\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
/* sanity check to see if point is on the curve */
if (rc == 0) {
if (EC_POINT_is_on_curve(eCurveData->G, G, eCurveData->ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Error, "
"Point not on curve\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
rc = TSS_BN_hex2bn(&z, /* freed @8 */
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
}
if (rc == 0) {
if (EC_GROUP_set_generator(eCurveData->G, G, z, BN_value_one()) == 0) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Error, "
"EC_GROUP_set_generator()\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
if (EC_GROUP_check(eCurveData->G, eCurveData->ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Error, "
"EC_GROUP_check()\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
if (EC_KEY_set_group(myecc, eCurveData->G) == 0) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: Error, "
"EC_KEY_set_group()\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
#if 0
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: "
"Address of eCurveData->G is %p\n", eCurveData->G);
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: "
"Address of eCurveData->CTX is %p\n", eCurveData->ctx);
#endif
if (tssVverbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: "
"Set group for key\n");
}
/* Create the public/private EC key pair here */
if (rc == 0) {
if (EC_KEY_generate_key(myecc) == 0) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: "
"Error generating the ECC key.\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
if (!EC_KEY_check_key(myecc)) {
if (tssVerbose) printf("TSS_ECC_GeneratePlatformEphemeralKey: "
"Error on EC_KEY_check_key()\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (p != NULL) BN_clear_free(p); /* @1 */
if (a != NULL) BN_clear_free(a); /* @2 */
if (b != NULL) BN_clear_free(b); /* @3 */
if (rc != 0) {
EC_GROUP_free(eCurveData->G); /* @4 */
EC_POINT_free(G); /* @5 */
}
if (x != NULL) BN_clear_free(x); /* @6 */
if (y != NULL) BN_clear_free(y); /* @7 */
if (z != NULL) BN_clear_free(z); /* @8 */
/* don't free the key info. This curve was constructed out of parameters, not of the openssl
library */
/* EC_KEY_free(myecc) */
/* EC_POINT_free(G); */
return rc;
}
/* TSS_ECC_Salt() returns both the plaintext and excrypted salt, based on the salt key bPublic. */
TPM_RC TSS_ECC_Salt(TPM2B_DIGEST *salt,
TPM2B_ENCRYPTED_SECRET *encryptedSalt,
TPMT_PUBLIC *publicArea)
{
TPM_RC rc = 0;
EC_KEY *myecc = NULL; /* ephemeral key */
const BIGNUM *d_caller; /* ephemeral private key */
const EC_POINT *callerPointPub; /* ephemeral public key */
EC_POINT *tpmPointPub = NULL;
BIGNUM *p_tpmX = NULL;
BIGNUM *bigY = NULL;
BIGNUM *zBn = NULL;
EC_POINT *rPoint = NULL;
BIGNUM *thepoint = NULL;
BIGNUM *sharedX = NULL;
BIGNUM *yBn = NULL;
uint32_t sizeInBytes;
uint32_t sizeInBits;
uint8_t *sharedXBin = NULL;
unsigned int lengthSharedXBin;
BIGNUM *p_caller_Xbn = NULL;
BIGNUM *p_caller_Ybn = NULL;
uint8_t *p_caller_Xbin = NULL;
uint8_t *p_caller_Ybin = NULL;
uint8_t *p_tpmXbin = NULL;
unsigned int length_p_caller_Xbin;
unsigned int length_p_caller_Ybin;
unsigned int length_p_tpmXbin;
TPM2B_ECC_PARAMETER sharedX_For_KDFE;
TPM2B_ECC_PARAMETER p_caller_X_For_KDFE;
TPM2B_ECC_PARAMETER p_tpmX_For_KDFE;
CURVE_DATA eCurveData;
/* only NIST P256 is currently supported */
if (rc == 0) {
if ((publicArea->parameters.eccDetail.curveID != TPM_ECC_NIST_P256)) {
if (tssVerbose)
printf("TSS_ECC_Salt: ECC curve ID %04x not supported\n",
publicArea->parameters.eccDetail.curveID);
rc = TSS_RC_BAD_SALT_KEY;
}
}
if (rc == 0) {
myecc = EC_KEY_new(); /* freed @1 */
if (myecc == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: EC_KEY_new failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
eCurveData.ctx = BN_CTX_new(); /* freed @16 */
if (eCurveData.ctx == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: BN_CTX_new failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
/* Generate the TSS EC ephemeral key pair outside the TPM for the salt. The public part of this
key is actually the 'encrypted' salt. */
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Calling TSS_ECC_GeneratePlatformEphemeralKey\n");
rc = TSS_ECC_GeneratePlatformEphemeralKey(&eCurveData, myecc);
}
if (rc == 0) {
d_caller = EC_KEY_get0_private_key(myecc); /* ephemeral private key */
callerPointPub = EC_KEY_get0_public_key(myecc); /* ephemeral public key */
}
/* validate that the public point is on the NIST P-256 curve */
if (rc == 0) {
if (EC_POINT_is_on_curve(eCurveData.G, callerPointPub, eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"Generated point not on curve\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
/* let d_caller be private scalar and P_caller be public point */
/* p_tpm is public point. p_tpmX is to be X-coordinate and p_tpmY the
Y-coordinate */
/* Allocate the space for P_tpm */
tpmPointPub = EC_POINT_new(eCurveData.G); /* freed @2 */
if (tpmPointPub == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: EC_POINT_new failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
/* grab the public point x and y using the parameters passed in */
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Salt key sizes are X: %d and Y: %d\n",
publicArea->unique.ecc.x.t.size,
publicArea->unique.ecc.y.t.size);
p_tpmX = BN_bin2bn((const unsigned char *)&publicArea->unique.ecc.x.t.buffer,
publicArea->unique.ecc.x.t.size, NULL); /* freed @3 */
if (p_tpmX == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: BN_bin2bn p_tpmX failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
bigY = BN_bin2bn((const unsigned char*)&publicArea->unique.ecc.y.t.buffer,
publicArea->unique.ecc.y.t.size, bigY); /* freed @15 */
if (bigY == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: BN_bin2bn bigY failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Salt public key X %s\n", BN_bn2hex(p_tpmX));
if (tssVverbose) printf("TSS_ECC_Salt: "
"Salt public key Y %s\n", BN_bn2hex(bigY));
}
/* Create the openssl form of the TPM salt public key as EC_POINT using coordinates */
if (rc == 0) {
if (EC_POINT_set_affine_coordinates_GFp
(eCurveData.G, tpmPointPub, p_tpmX, bigY, eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"Cannot create TPM public point from coordinates\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
/* RFC 2440 Named curve prime256v1 */
if (rc == 0) {
rc = TSS_BN_hex2bn(&zBn, /* freed @4 */
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
}
/* add the generator z to the group we are constructing */
if (rc == 0) {
if (EC_GROUP_set_generator(eCurveData.G, tpmPointPub, zBn, BN_value_one()) == 0) {
if(tssVerbose) printf ("TSS_ECC_Salt: "
"Error EC_GROUP_set_generator()\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
/* Check for validity of our group */
if (rc == 0) {
if (EC_GROUP_check(eCurveData.G, eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"ec_group_check() failed\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
/* Check to see if what we think is the TPM point is on the curve */
if (rc == 0) {
if (EC_POINT_is_on_curve(eCurveData.G, tpmPointPub, eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: Error, "
"Point not on curve\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
else {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Validated that TPM EC point is on curve\n");
}
}
if (rc == 0) {
rPoint = EC_POINT_new(eCurveData.G);
if (rPoint == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"Cannot create rPoint\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
/* Point multiply the TPM public point by the ephemeral scalar. This will produce the
point from which we get the shared X coordinate, which we keep for use in KDFE. The
TPM will calculate the same X. */
if (rc == 0) {
if (EC_POINT_mul(eCurveData.G, rPoint, NULL, tpmPointPub,
d_caller, eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"EC_POINT_mul failed\n") ;
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
else {
if (tssVverbose) printf("TSS_ECC_Salt: "
"EC_POINT_mul() succeeded\n");
}
}
/* Check to see if calculated point is on the curve, just for extra sanity */
if (rc == 0) {
if (EC_POINT_is_on_curve(eCurveData.G, rPoint, eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: Error,"
"Point r is not on curve\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
else {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Point calculated by EC_POINT_mul() is on the curve\n");
}
}
if (rc == 0) {
thepoint = EC_POINT_point2bn(eCurveData.G, rPoint, POINT_CONVERSION_UNCOMPRESSED,
NULL, eCurveData.ctx); /* freed @6 */
if (thepoint == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"EC_POINT_point2bn thepoint failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
/* get sharedX */
if (rc == 0) {
rc = TSS_BN_new(&sharedX); /* freed @7 */
}
if (rc == 0) {
rc = TSS_BN_new(&yBn); /* freed @8 */
}
if (rc == 0) {
if (EC_POINT_get_affine_coordinates_GFp(eCurveData.G, rPoint,
sharedX, yBn, eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"EC_POINT_get_affine_coordinates_GFp() failed\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
sizeInBytes = TSS_GetDigestSize(publicArea->nameAlg);
sizeInBits = sizeInBytes * 8;
sharedXBin = malloc(BN_num_bytes(sharedX)); /* freed @9 */
if (sharedXBin == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"malloc sharedXBin failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
lengthSharedXBin = (unsigned int)BN_bn2bin(sharedX, sharedXBin);
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: sharedXBin",
sharedXBin,
lengthSharedXBin);
}
/* encrypted salt is just the ephemeral public key */
if (rc == 0) {
rc = TSS_BN_new(&p_caller_Xbn); /* freed 10 */
}
if (rc == 0) {
rc = TSS_BN_new(&p_caller_Ybn); /* freed @11 */
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Allocated space for ephemeral BIGNUM X, Y\n");
}
/* Get the X-coordinate and Y-Coordinate */
if (rc == 0) {
if (EC_POINT_get_affine_coordinates_GFp(eCurveData.G, callerPointPub,
p_caller_Xbn, p_caller_Ybn,
eCurveData.ctx) == 0) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"EC_POINT_get_affine_coordinates_GFp() failed\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
else {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Retrieved X and Y coordinates from ephemeral public\n");
}
}
if (rc == 0) {
p_caller_Xbin = malloc(BN_num_bytes(p_caller_Xbn)); /* freed @12 */
if (p_caller_Xbin == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"malloc p_caller_Xbin failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
p_caller_Ybin = malloc(BN_num_bytes(p_caller_Ybn)); /* freed @13 */
if (p_caller_Ybin == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"malloc p_caller_Ybin failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Allocated space for ephemeral binary X and y\n");
}
if (rc == 0) {
p_tpmXbin = malloc(BN_num_bytes(p_tpmX)); /* freed @14 */
if (p_tpmXbin == NULL) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"malloc p_tpmXbin failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
if (rc == 0) {
length_p_tpmXbin = (unsigned int)BN_bn2bin(p_tpmX, p_tpmXbin);
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: p_tpmXbin ",
p_tpmXbin,
length_p_tpmXbin);
length_p_caller_Xbin = (unsigned int)BN_bn2bin(p_caller_Xbn, p_caller_Xbin);
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: p_caller_Xbin",
p_caller_Xbin,
length_p_caller_Xbin);
length_p_caller_Ybin = (unsigned int)BN_bn2bin(p_caller_Ybn, p_caller_Ybin);
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: p_caller_Ybin",
p_caller_Ybin,
length_p_caller_Ybin);
}
/* in->encryptedSalt TPM2B_ENCRYPTED_SECRET is a size and TPMU_ENCRYPTED_SECRET secret.
TPMU_ENCRYPTED_SECRET is a TPMS_ECC_POINT
TPMS_ECC_POINT has two TPMB_ECC_PARAMETER, x and y
*/
if (rc == 0) {
/* TPMS_ECC_POINT 256/8 is a hard coded value for NIST P256, the only curve
currently supported */
uint8_t *secret = encryptedSalt->t.secret; /* TPMU_ENCRYPTED_SECRET pointer for
clarity */
/* TPM2B_ENCRYPTED_SECRET size */
encryptedSalt->t.size = sizeof(uint16_t) + (256/8) + sizeof(uint16_t) + (256/8);
/* leading zeros, because some points may be less than 32 bytes */
memset(secret, 0, sizeof(TPMU_ENCRYPTED_SECRET));
/* TPMB_ECC_PARAMETER X point */
*(uint16_t *)(secret) = htons(256/8);
memcpy(secret +
sizeof(uint16_t) + (256/8) - length_p_caller_Xbin,
p_caller_Xbin, length_p_caller_Xbin);
/* TPMB_ECC_PARAMETER Y point */
*(uint16_t *)(secret + sizeof(uint16_t) + (256/8)) = htons(256/8);
memcpy(secret +
sizeof(uint16_t) + (256/8) +
sizeof(uint16_t) + (256/8) - length_p_caller_Ybin,
p_caller_Ybin, length_p_caller_Ybin);
}
if (rc == 0) {
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: ECC encrypted salt",
encryptedSalt->t.secret,
encryptedSalt->t.size);
}
/* sharedX_For_KDFE */
if (rc == 0) {
if (lengthSharedXBin > sizeof(sharedX_For_KDFE.t.buffer)) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"lengthSharedXBin %u too large\n",
lengthSharedXBin);
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
sharedX_For_KDFE.t.size = 32;
memset(sharedX_For_KDFE.t.buffer, 0, sizeof(sharedX_For_KDFE.t.buffer));
memcpy(sharedX_For_KDFE.t.buffer + 32 - lengthSharedXBin,
sharedXBin, lengthSharedXBin);
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: sharedX_For_KDFE",
sharedX_For_KDFE.t.buffer,
sharedX_For_KDFE.t.size);
}
/* p_caller_X_For_KDFE */
if (rc == 0) {
if (length_p_caller_Xbin > sizeof(p_caller_X_For_KDFE.t.buffer)) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"length_p_caller_Xbin %u too large\n",
length_p_caller_Xbin);
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
p_caller_X_For_KDFE.t.size = 32;
memset(p_caller_X_For_KDFE.t.buffer, 0, sizeof(p_caller_X_For_KDFE.t.buffer));
memcpy(p_caller_X_For_KDFE.t.buffer + 32 - length_p_caller_Xbin,
p_caller_Xbin, length_p_caller_Xbin);
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: p_caller_X_For_KDFE",
p_caller_X_For_KDFE.t.buffer,
p_caller_X_For_KDFE.t.size);
}
/* p_tpmX_For_KDFE */
if (rc == 0) {
if (length_p_tpmXbin > sizeof(p_tpmX_For_KDFE.t.buffer)) {
if (tssVerbose) printf("TSS_ECC_Salt: "
"length_p_tpmXbin %u too large\n",
length_p_tpmXbin);
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
if (rc == 0) {
p_tpmX_For_KDFE .t.size = 32;
memset(p_tpmX_For_KDFE.t.buffer, 0, sizeof(p_tpmX_For_KDFE.t.buffer));
memcpy(p_tpmX_For_KDFE.t.buffer + 32 - length_p_tpmXbin,
p_tpmXbin, length_p_tpmXbin);
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: p_tpmX_For_KDFE",
p_tpmX_For_KDFE.t.buffer,
p_tpmX_For_KDFE.t.size);
}
if (rc == 0) {
if (tssVverbose) printf("TSS_ECC_Salt: "
"Calling TSS_KDFE\n");
/* TPM2B_DIGEST salt size is the largest supported digest algorithm.
This has already been validated when unmarshaling the Name hash algorithm.
*/
/* salt = KDFe(tpmKey_NameAlg, sharedX, "SECRET", P_caller, P_tpm,
tpmKey_NameAlgSizeBits) */
salt->t.size = sizeInBytes;
rc = TSS_KDFE((uint8_t *)&salt->t.buffer, /* KDFe output */
publicArea->nameAlg, /* hash algorithm */
&sharedX_For_KDFE.b, /* Z (key) */
"SECRET", /* KDFe label */
&p_caller_X_For_KDFE.b, /* context U */
&p_tpmX_For_KDFE.b, /* context V */
sizeInBits); /* required size of key in bits */
}
if (rc == 0) {
if (tssVverbose) TSS_PrintAll("TSS_ECC_Salt: salt",
(uint8_t *)&salt->t.buffer,
salt->t.size);
}
/* cleanup */
if (myecc != NULL) EC_KEY_free(myecc); /* @1 */
if (tpmPointPub != NULL) EC_POINT_free(tpmPointPub); /* @2 */
if (p_tpmX != NULL) BN_clear_free(p_tpmX); /* @3 */
if (zBn != NULL) BN_clear_free(zBn); /* @4 */
if (rPoint != NULL) EC_POINT_free(rPoint); /* @5 */
if (thepoint != NULL) BN_clear_free(thepoint); /* @6 */
if (sharedX != NULL) BN_clear_free(sharedX); /* @7 */
if (yBn != NULL) BN_clear_free(yBn); /* @8 */
free(sharedXBin); /* @9 */
if (p_caller_Xbn != NULL) BN_clear_free(p_caller_Xbn); /* @10 */
if (p_caller_Ybn != NULL) BN_clear_free(p_caller_Ybn); /* @11 */
free(p_caller_Xbin); /* @12 */
free(p_caller_Ybin); /* @13 */
free(p_tpmXbin); /* @14 */
if (bigY != NULL) BN_clear_free(bigY); /* @15 */
if (eCurveData.ctx != NULL) BN_CTX_free(eCurveData.ctx); /* @16 */
return rc;
}
/* TSS_BN_new() wraps the openSSL function in a TPM error handler
*/
static TPM_RC TSS_BN_new(BIGNUM **bn) /* freed by caller */
{
TPM_RC rc = 0;
if (rc == 0) {
if (*bn != NULL) {
if (tssVerbose)
printf("TSS_BN_new: Error (fatal), *bn %p should be NULL before BN_new()\n", *bn);
rc = TSS_RC_ALLOC_INPUT;
}
}
if (rc == 0) {
*bn = BN_new();
if (*bn == NULL) {
if (tssVerbose) printf("TSS_BN_new: BN_new() failed\n");
rc = TSS_RC_OUT_OF_MEMORY;
}
}
return rc;
}
/* TSS_BN_hex2bn() wraps the openSSL function in a TPM error handler
*/
static TPM_RC TSS_BN_hex2bn(BIGNUM **bn, const char *str) /* freed by caller */
{
TPM_RC rc = 0;
if (rc == 0) {
if (*bn != NULL) {
if (tssVerbose)
printf("TSS_BN_hex2bn: Error (fatal), *bn %p should be NULL before BN_new()\n", *bn);
rc = TSS_RC_ALLOC_INPUT;
}
}
if (rc == 0) {
int irc;
irc = BN_hex2bn(bn, str);
if (irc == 0) {
if (tssVerbose) printf("TSS_BN_hex2bn: BN_hex2bn() failed\n");
rc = TSS_RC_EC_EPHEMERAL_FAILURE;
}
}
return rc;
}
/* TSS_bin2bn() wraps the openSSL function in a TPM error handler
Converts a char array to bignum
bn must be freed by the caller.
*/
static TPM_RC TSS_bin2bn(BIGNUM **bn, const unsigned char *bin, unsigned int bytes)
{
TPM_RC rc = 0;
/* BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
BN_bin2bn() converts the positive integer in big-endian form of length len at s into a BIGNUM
and places it in ret. If ret is NULL, a new BIGNUM is created.
BN_bin2bn() returns the BIGNUM, NULL on error.
*/
if (rc == 0) {
*bn = BN_bin2bn(bin, bytes, *bn);
if (*bn == NULL) {
if (tssVerbose) printf("TSS_bin2bn: Error in BN_bin2bn\n");
rc = TSS_RC_BIGNUM;
}
}
return rc;
}
/*
AES
*/
TPM_RC TSS_AES_GetEncKeySize(size_t *tssSessionEncKeySize)
{
*tssSessionEncKeySize = sizeof(AES_KEY);
return 0;
}
TPM_RC TSS_AES_GetDecKeySize(size_t *tssSessionDecKeySize)
{
*tssSessionDecKeySize = sizeof(AES_KEY);
return 0;
}
#define TSS_AES_KEY_BITS 128
TPM_RC TSS_AES_KeyGenerate(void *tssSessionEncKey,
void *tssSessionDecKey)
{
TPM_RC rc = 0;
int irc;
unsigned char userKey[AES_128_BLOCK_SIZE_BYTES];
const char *envKeyString = NULL;
unsigned char *envKeyBin = NULL;
size_t envKeyBinLen;
if (rc == 0) {
envKeyString = getenv("TPM_SESSION_ENCKEY");
}
if (envKeyString == NULL) {
/* If the env variable TPM_SESSION_ENCKEY is not set, generate a random key for this
TSS_CONTEXT */
if (rc == 0) {
rc = TSS_RandBytes(userKey, AES_128_BLOCK_SIZE_BYTES);
}
}
/* The env variable TPM_SESSION_ENCKEY can set a (typically constant) encryption key. This is
useful for scripting, where the env variable is set to a random seed at the beginning of the
script. */
else {
/* hexascii to binary */
if (rc == 0) {
rc = TSS_Array_Scan(&envKeyBin, &envKeyBinLen, envKeyString);
}
/* range check */
if (rc == 0) {
if (envKeyBinLen != AES_128_BLOCK_SIZE_BYTES) {
if (tssVerbose)
printf("TSS_AES_KeyGenerate: Error, env variable length %lu not %lu\n",
(unsigned long)envKeyBinLen, (unsigned long)sizeof(userKey));
rc = TSS_RC_BAD_PROPERTY_VALUE;
}
}
/* copy the binary to the common userKey for use below */
if (rc == 0) {
memcpy(userKey, envKeyBin, envKeyBinLen);
}
}
/* translate to an openssl key token */
if (rc == 0) {
irc = AES_set_encrypt_key(userKey,
TSS_AES_KEY_BITS,
tssSessionEncKey);
/* should never occur, null pointers or bad bit size */
if (irc != 0) {
if (tssVerbose)
printf("TSS_AES_KeyGenerate: Error setting openssl AES encryption key\n");
rc = TSS_RC_AES_KEYGEN_FAILURE;
}
}
if (rc == 0) {
irc = AES_set_decrypt_key(userKey,
TSS_AES_KEY_BITS,
tssSessionDecKey);
/* should never occur, null pointers or bad bit size */
if (irc != 0) {
if (tssVerbose)
printf("TSS_AES_KeyGenerate: Error setting openssl AES decryption key\n");
rc = TSS_RC_AES_KEYGEN_FAILURE;
}
}
free(envKeyBin);
return rc;
}
/* TSS_AES_Encrypt() is AES non-portable code to encrypt 'decrypt_data' to 'encrypt_data' using CBC.
This function uses the session encryption key for encrypting session state.
The stream is padded as per PKCS#7 / RFC2630
'encrypt_data' must be free by the caller
*/
TPM_RC TSS_AES_Encrypt(void *tssSessionEncKey,
unsigned char **encrypt_data, /* output, caller frees */
uint32_t *encrypt_length, /* output */
const unsigned char *decrypt_data, /* input */
uint32_t decrypt_length) /* input */
{
TPM_RC rc = 0;
uint32_t pad_length;
unsigned char *decrypt_data_pad;
unsigned char ivec[AES_128_BLOCK_SIZE_BYTES]; /* initial chaining vector */
decrypt_data_pad = NULL; /* freed @1 */
if (rc == 0) {
/* calculate the pad length and padded data length */
pad_length = AES_128_BLOCK_SIZE_BYTES - (decrypt_length % AES_128_BLOCK_SIZE_BYTES);
*encrypt_length = decrypt_length + pad_length;
/* allocate memory for the encrypted response */
rc = TSS_Malloc(encrypt_data, *encrypt_length);
}
/* allocate memory for the padded decrypted data */
if (rc == 0) {
rc = TSS_Malloc(&decrypt_data_pad, *encrypt_length);
}
/* pad the decrypted clear text data */
if (rc == 0) {
/* unpadded original data */
memcpy(decrypt_data_pad, decrypt_data, decrypt_length);
/* last gets pad = pad length */
memset(decrypt_data_pad + decrypt_length, pad_length, pad_length);
/* set the IV */
memset(ivec, 0, sizeof(ivec));
/* encrypt the padded input to the output */
AES_cbc_encrypt(decrypt_data_pad,
*encrypt_data,
*encrypt_length,
tssSessionEncKey,
ivec,
AES_ENCRYPT);
}
free(decrypt_data_pad); /* @1 */
return rc;
}
/* TSS_AES_Decrypt() is AES non-portable code to decrypt 'encrypt_data' to 'decrypt_data' using CBC.
This function uses the session encryption key for decrypting session state.
The stream must be padded as per PKCS#7 / RFC2630
decrypt_data must be free by the caller
*/
TPM_RC TSS_AES_Decrypt(void *tssSessionDecKey,
unsigned char **decrypt_data, /* output, caller frees */
uint32_t *decrypt_length, /* output */
const unsigned char *encrypt_data, /* input */
uint32_t encrypt_length) /* input */
{
TPM_RC rc = 0;
uint32_t pad_length;
uint32_t i;
unsigned char *pad_data;
unsigned char ivec[AES_128_BLOCK_SIZE_BYTES]; /* initial chaining vector */
/* sanity check encrypted length */
if (rc == 0) {
if (encrypt_length < AES_128_BLOCK_SIZE_BYTES) {
if (tssVerbose) printf("TSS_AES_Decrypt: Error, bad length %u\n",
encrypt_length);
rc = TSS_RC_AES_DECRYPT_FAILURE;
}
}
/* allocate memory for the padded decrypted data */
if (rc == 0) {
rc = TSS_Malloc(decrypt_data, encrypt_length);
}
/* decrypt the input to the padded output */
if (rc == 0) {
/* set the IV */
memset(ivec, 0, sizeof(ivec));
/* decrypt the padded input to the output */
AES_cbc_encrypt(encrypt_data,
*decrypt_data,
encrypt_length,
tssSessionDecKey,
ivec,
AES_DECRYPT);
}
/* get the pad length */
if (rc == 0) {
/* get the pad length from the last byte */
pad_length = (uint32_t)*(*decrypt_data + encrypt_length - 1);
/* sanity check the pad length */
if ((pad_length == 0) ||
(pad_length > AES_128_BLOCK_SIZE_BYTES)) {
if (tssVerbose) printf("TSS_AES_Decrypt: Error, illegal pad length\n");
rc = TSS_RC_AES_DECRYPT_FAILURE;
}
}
if (rc == 0) {
/* get the unpadded length */
*decrypt_length = encrypt_length - pad_length;
/* pad starting point */
pad_data = *decrypt_data + *decrypt_length;
/* sanity check the pad */
for (i = 0 ; (rc == 0) && (i < pad_length) ; i++, pad_data++) {
if (*pad_data != pad_length) {
if (tssVerbose) printf("TSS_AES_Decrypt: Error, bad pad %02x at index %u\n",
*pad_data, i);
rc = TSS_RC_AES_DECRYPT_FAILURE;
}
}
}
return rc;
}
TPM_RC TSS_AES_EncryptCFB(uint8_t *dOut, /* OUT: the encrypted */
uint32_t keySizeInBits, /* IN: key size in bit */
uint8_t *key, /* IN: key buffer. The size of this buffer
in */
uint8_t *iv, /* IN/OUT: IV for decryption */
uint32_t dInSize, /* IN: data size */
uint8_t *dIn) /* IN: data buffer */
{
TPM_RC rc = 0;
int irc;
int blockSize;
AES_KEY aeskey;
int32_t dSize; /* signed version of dInSize */
/* Create AES encryption key token */
if (rc == 0) {
irc = AES_set_encrypt_key(key, keySizeInBits, &aeskey);
if (irc != 0) {
if (tssVerbose) printf("TSS_AES_EncryptCFB: Error setting openssl AES encryption key\n");
rc = TSS_RC_AES_KEYGEN_FAILURE; /* should never occur, null pointers or bad bit size */
}
}
if (rc == 0) {
/* Encrypt the current IV into the new IV, XOR in the data, and copy to output */
for(dSize = (INT32)dInSize ; dSize > 0 ; dSize -= 16, dOut += 16, dIn += 16) {
/* Encrypt the current value of the IV to the intermediate value. Store in old iv,
since it's not needed anymore. */
AES_encrypt(iv, iv, &aeskey);
blockSize = (dSize < 16) ? dSize : 16; /* last block can be < 16 */
TSS_XOR(dOut, dIn, iv, blockSize);
memcpy(iv, dOut, blockSize);
}
}
return rc;
}
TPM_RC TSS_AES_DecryptCFB(uint8_t *dOut, /* OUT: the decrypted data */
uint32_t keySizeInBits, /* IN: key size in bit */
uint8_t *key, /* IN: key buffer. The size of this buffer
in */
uint8_t *iv, /* IN/OUT: IV for decryption. */
uint32_t dInSize, /* IN: data size */
uint8_t *dIn) /* IN: data buffer */
{
TPM_RC rc = 0;
int irc;
uint8_t tmp[16];
int blockSize;
AES_KEY aesKey;
int32_t dSize;
/* Create AES encryption key token */
if (rc == 0) {
irc = AES_set_encrypt_key(key, keySizeInBits, &aesKey);
if (irc != 0) {
if (tssVerbose) printf("TSS_AES_DecryptCFB: Error setting openssl AES encryption key\n");
rc = TSS_RC_AES_KEYGEN_FAILURE; /* should never occur, null pointers or bad bit size */
}
}
if (rc == 0) {
for (dSize = (INT32)dInSize ; dSize > 0; dSize -= 16, dOut += 16, dIn += 16) {
/* Encrypt the IV into the temp buffer */
AES_encrypt(iv, tmp, &aesKey);
blockSize = (dSize < 16) ? dSize : 16; /* last block can be < 16 */
TSS_XOR(dOut, dIn, tmp, blockSize);
memcpy(iv, dIn, blockSize);
}
}
return rc;
}
|