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
|
.. _mozilla_projects_nss_encrypt_decrypt_mac_using_token:
Encrypt and decrypt MAC using token
===================================
.. _nss_sample_code_3_encryptiondecryption_and_mac_using_token_object.:
`NSS sample code 3: encryption/decryption and MAC using token object. <#nss_sample_code_3_encryptiondecryption_and_mac_using_token_object.>`__
----------------------------------------------------------------------------------------------------------------------------------------------
.. container::
Generates encryption/mac keys and uses token for storing.
.. code:: c
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* NSPR Headers */
#include
#include
#include
#include
#include
#include
#include
/* NSS headers */
#include
#include
/* our samples utilities */
#include "util.h"
#define BUFFERSIZE 80
#define DIGESTSIZE 16
#define PTEXT_MAC_BUFFER_SIZE 96
#define CIPHERSIZE 96
#define BLOCKSIZE 32
#define CIPHER_HEADER "-----BEGIN CIPHER-----"
#define CIPHER_TRAILER "-----END CIPHER-----"
#define ENCKEY_HEADER "-----BEGIN AESKEY CKAID-----"
#define ENCKEY_TRAILER "-----END AESKEY CKAID-----"
#define MACKEY_HEADER "-----BEGIN MACKEY CKAID-----"
#define MACKEY_TRAILER "-----END MACKEY CKAID-----"
#define IV_HEADER "-----BEGIN IV-----"
#define IV_TRAILER "-----END IV-----"
#define MAC_HEADER "-----BEGIN MAC-----"
#define MAC_TRAILER "-----END MAC-----"
#define PAD_HEADER "-----BEGIN PAD-----"
#define PAD_TRAILER "-----END PAD-----"
typedef enum {
ENCRYPT,
DECRYPT,
UNKNOWN
} CommandType;
typedef enum {
SYMKEY = 0,
MACKEY = 1,
IV = 2,
MAC = 3,
PAD = 4
} HeaderType;
/*
* Print usage message and exit
*/
static void
Usage(const char *progName)
{
fprintf(stderr, "\nUsage: %s -c -d [-z ] "
"[-p | -f ] -i -o \n\n",
progName);
fprintf(stderr, "%-20s Specify 'a' for encrypt operation\n\n",
"-c ");
fprintf(stderr, "%-20s Specify 'b' for decrypt operation\n\n",
" ");
fprintf(stderr, "%-20s Specify db directory path\n\n",
"-d ");
fprintf(stderr, "%-20s Specify db password [optional]\n\n",
"-p ");
fprintf(stderr, "%-20s Specify db password file [optional]\n\n",
"-f ");
fprintf(stderr, "%-20s Specify noise file name [optional]\n\n",
"-z ");
fprintf(stderr, "%-21s Specify an input file name\n\n",
"-i ");
fprintf(stderr, "%-21s Specify an output file name\n\n",
"-o ");
fprintf(stderr, "%-7s For encrypt, it takes as an input file and produces\n",
"Note :");
fprintf(stderr, "%-7s .enc and .header as intermediate output files.\n\n",
"");
fprintf(stderr, "%-7s For decrypt, it takes .enc and .header\n",
"");
fprintf(stderr, "%-7s as input files and produces as a final output file.\n\n",
"");
exit(-1);
}
/*
* Gather a CKA_ID
*/
SECStatus
GatherCKA_ID(PK11SymKey* key, SECItem* buf)
{
SECStatus rv = PK11_ReadRawAttribute(PK11_TypeSymKey, key, CKA_ID, buf);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "PK11_ReadRawAttribute returned (%d)\n", rv);
PR_fprintf(PR_STDERR, "Could not read SymKey CKA_ID attribute\n");
return rv;
}
return rv;
}
/*
* Generate a Symmetric Key
*/
PK11SymKey *
GenerateSYMKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE mechanism,
int keySize, SECItem *keyID, secuPWData *pwdata)
{
SECStatus rv;
PK11SymKey *key;
if (PK11_NeedLogin(slot)) {
rv = PK11_Authenticate(slot, PR_TRUE, pwdata);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Could not authenticate to token %s.\n",
PK11_GetTokenName(slot));
return NULL;
}
}
/* Generate the symmetric key */
key = PK11_TokenKeyGen(slot, mechanism,
NULL, keySize, keyID, PR_TRUE, pwdata);
if (!key) {
PR_fprintf(PR_STDERR, "Symmetric Key Generation Failed \n");
}
return key;
}
/*
* MacInit
*/
SECStatus
MacInit(PK11Context *ctx)
{
SECStatus rv = PK11_DigestBegin(ctx);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Compute MAC Failed : PK11_DigestBegin()\n");
}
return rv;
}
/*
* MacUpdate
*/
SECStatus
MacUpdate(PK11Context *ctx,
unsigned char *msg, unsigned int msgLen)
{
SECStatus rv = PK11_DigestOp(ctx, msg, msgLen);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Compute MAC Failed : DigestOp()\n");
}
return rv;
}
/*
* Finalize MACing
*/
SECStatus
MacFinal(PK11Context *ctx,
unsigned char *mac, unsigned int *macLen, unsigned int maxLen)
{
SECStatus rv = PK11_DigestFinal(ctx, mac, macLen, maxLen);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Compute MAC Failed : PK11_DigestFinal()\n");
}
return SECSuccess;
}
/*
* Compute Mac
*/
SECStatus
ComputeMac(PK11Context *ctxmac,
unsigned char *ptext, unsigned int ptextLen,
unsigned char *mac, unsigned int *macLen,
unsigned int maxLen)
{
SECStatus rv = MacInit(ctxmac);
if (rv != SECSuccess) return rv;
rv = MacUpdate(ctxmac, ptext, ptextLen);
if (rv != SECSuccess) return rv;
rv = MacFinal(ctxmac, mac, macLen, maxLen);
return rv;
}
/*
* WriteToHeaderFile
*/
SECStatus
WriteToHeaderFile(const char *buf, unsigned int len, HeaderType type,
PRFileDesc *outFile)
{
SECStatus rv;
char header[40];
char trailer[40];
char *outString = NULL;
switch (type) {
case SYMKEY:
strcpy(header, ENCKEY_HEADER);
strcpy(trailer, ENCKEY_TRAILER);
break;
case MACKEY:
strcpy(header, MACKEY_HEADER);
strcpy(trailer, MACKEY_TRAILER);
break;
case IV:
strcpy(header, IV_HEADER);
strcpy(trailer, IV_TRAILER);
break;
case MAC:
strcpy(header, MAC_HEADER);
strcpy(trailer, MAC_TRAILER);
break;
case PAD:
strcpy(header, PAD_HEADER);
strcpy(trailer, PAD_TRAILER);
break;
}
PR_fprintf(outFile, "%s\n", header);
PrintAsHex(outFile, buf, len);
PR_fprintf(outFile, "%s\n\n", trailer);
return SECSuccess;
}
/*
* Initialize for encryption or decryption - common code
*/
PK11Context *
CryptInit(PK11SymKey *key,
unsigned char *iv, unsigned int ivLen,
CK_MECHANISM_TYPE type, CK_ATTRIBUTE_TYPE operation)
{
SECItem ivItem = { siBuffer, iv, ivLen };
PK11Context *ctx = NULL;
SECItem *secParam = PK11_ParamFromIV(CKM_AES_CBC, &ivItem);
if (secParam == NULL) {
PR_fprintf(PR_STDERR, "Crypt Failed : secParam NULL\n");
return NULL;
}
ctx = PK11_CreateContextBySymKey(CKM_AES_CBC, operation, key, secParam);
if (ctx == NULL) {
PR_fprintf(PR_STDERR, "Crypt Failed : can't create a context\n");
goto cleanup;
}
cleanup:
if (secParam) {
SECITEM_FreeItem(secParam, PR_TRUE);
}
return ctx;
}
/*
* Common encryption and decryption code
*/
SECStatus
Crypt(PK11Context *ctx,
unsigned char *out, unsigned int *outLen, unsigned int maxOut,
unsigned char *in, unsigned int inLen)
{
SECStatus rv;
rv = PK11_CipherOp(ctx, out, outLen, maxOut, in, inLen);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Crypt Failed : PK11_CipherOp returned %d\n", rv);
goto cleanup;
}
cleanup:
if (rv != SECSuccess) {
return rv;
}
return SECSuccess;
}
/*
* Decrypt
*/
SECStatus
Decrypt(PK11Context *ctx,
unsigned char *out, unsigned int *outLen, unsigned int maxout,
unsigned char *in, unsigned int inLen)
{
return Crypt(ctx, out, outLen, maxout, in, inLen);
}
/*
* Encrypt
*/
SECStatus
Encrypt(PK11Context* ctx,
unsigned char *out, unsigned int *outLen, unsigned int maxout,
unsigned char *in, unsigned int inLen)
{
return Crypt(ctx, out, outLen, maxout, in, inLen);
}
/*
* EncryptInit
*/
PK11Context *
EncryptInit(PK11SymKey *ek, unsigned char *iv, unsigned int ivLen,
CK_MECHANISM_TYPE type)
{
return CryptInit(ek, iv, ivLen, type, CKA_ENCRYPT);
}
/*
* DecryptInit
*/
PK11Context *
DecryptInit(PK11SymKey *dk, unsigned char *iv, unsigned int ivLen,
CK_MECHANISM_TYPE type)
{
return CryptInit(dk, iv, ivLen, type, CKA_DECRYPT);
}
/*
* Read cryptographic parameters from the header file
*/
SECStatus
ReadFromHeaderFile(const char *fileName, HeaderType type,
SECItem *item, PRBool isHexData)
{
SECStatus rv;
PRFileDesc* file;
SECItem filedata;
SECItem outbuf;
unsigned char *nonbody;
unsigned char *body;
char header[40];
char trailer[40];
outbuf.type = siBuffer;
file = PR_Open(fileName, PR_RDONLY, 0);
if (!file) {
PR_fprintf(PR_STDERR, "Failed to open %s\n", fileName);
return SECFailure;
}
switch (type) {
case SYMKEY:
strcpy(header, ENCKEY_HEADER);
strcpy(trailer, ENCKEY_TRAILER);
break;
case MACKEY:
strcpy(header, MACKEY_HEADER);
strcpy(trailer, MACKEY_TRAILER);
break;
case IV:
strcpy(header, IV_HEADER);
strcpy(trailer, IV_TRAILER);
break;
case MAC:
strcpy(header, MAC_HEADER);
strcpy(trailer, MAC_TRAILER);
break;
case PAD:
strcpy(header, PAD_HEADER);
strcpy(trailer, PAD_TRAILER);
break;
}
rv = FileToItem(&filedata, file);
nonbody = (char *)filedata.data;
if (!nonbody) {
PR_fprintf(PR_STDERR, "unable to read data from input file\n");
rv = SECFailure;
goto cleanup;
}
/* check for headers and trailers and remove them */
if ((body = strstr(nonbody, header)) != NULL) {
char *trail = NULL;
nonbody = body;
body = PORT_Strchr(body, '\n');
if (!body)
body = PORT_Strchr(nonbody, '\r'); /* maybe this is a MAC file */
if (body)
trail = strstr(++body, trailer);
if (trail != NULL) {
*trail = '\0';
} else {
PR_fprintf(PR_STDERR, "input has header but no trailer\n");
PORT_Free(filedata.data);
return SECFailure;
}
} else {
body = nonbody;
}
cleanup:
PR_Close(file);
HexToBuf(body, item, isHexData);
return SECSuccess;
}
/*
* EncryptAndMac
*/
SECStatus
EncryptAndMac(PRFileDesc *inFile,
PRFileDesc *headerFile,
PRFileDesc *encFile,
PK11SymKey *ek,
PK11SymKey *mk,
unsigned char *iv, unsigned int ivLen,
PRBool ascii)
{
SECStatus rv;
unsigned char ptext[BLOCKSIZE];
unsigned int ptextLen;
unsigned char mac[DIGESTSIZE];
unsigned int macLen;
unsigned int nwritten;
unsigned char encbuf[BLOCKSIZE];
unsigned int encbufLen;
SECItem noParams = { siBuffer, NULL, 0 };
PK11Context *ctxmac = NULL;
PK11Context *ctxenc = NULL;
unsigned int pad[1];
SECItem padItem;
unsigned int paddingLength;
static unsigned int firstTime = 1;
int j;
ctxmac = PK11_CreateContextBySymKey(CKM_MD5_HMAC, CKA_SIGN, mk, &noParams);
if (ctxmac == NULL) {
PR_fprintf(PR_STDERR, "Can't create MAC context\n");
rv = SECFailure;
goto cleanup;
}
rv = MacInit(ctxmac);
if (rv != SECSuccess) {
goto cleanup;
}
ctxenc = EncryptInit(ek, iv, ivLen, CKM_AES_CBC);
/* read a buffer of plaintext from input file */
while ((ptextLen = PR_Read(inFile, ptext, sizeof(ptext))) > 0) {
/* Encrypt using it using CBC, using previously created IV */
if (ptextLen != BLOCKSIZE) {
paddingLength = BLOCKSIZE - ptextLen;
for ( j=0; j < paddingLength; j++) {
ptext[ptextLen+j] = (unsigned char)paddingLength;
}
ptextLen = BLOCKSIZE;
}
rv = Encrypt(ctxenc,
encbuf, &encbufLen, sizeof(encbuf),
ptext, ptextLen);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Encrypt Failure\n");
goto cleanup;
}
/* save the last block of ciphertext as the next IV */
iv = encbuf;
ivLen = encbufLen;
/* write the cipher text to intermediate file */
nwritten = PR_Write(encFile, encbuf, encbufLen);
/*PR_Assert(nwritten == encbufLen);*/
rv = MacUpdate(ctxmac, ptext, ptextLen);
}
rv = MacFinal(ctxmac, mac, &macLen, DIGESTSIZE);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "MacFinal Failure\n");
goto cleanup;
}
if (macLen == 0) {
PR_fprintf(PR_STDERR, "Bad MAC length\n");
rv = SECFailure;
goto cleanup;
}
WriteToHeaderFile(mac, macLen, MAC, headerFile);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Write MAC Failure\n");
goto cleanup;
}
pad[0] = paddingLength;
padItem.type = siBuffer;
padItem.data = (unsigned char *)pad;
padItem.len = sizeof(pad[0]);
WriteToHeaderFile(padItem.data, padItem.len, PAD, headerFile);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Write PAD Failure\n");
goto cleanup;
}
rv = SECSuccess;
cleanup:
if (ctxmac != NULL) {
PK11_DestroyContext(ctxmac, PR_TRUE);
}
if (ctxenc != NULL) {
PK11_DestroyContext(ctxenc, PR_TRUE);
}
return rv;
}
/*
* Find the Key for the given mechanism
*/
PK11SymKey*
FindKey(PK11SlotInfo *slot,
CK_MECHANISM_TYPE mechanism,
SECItem *keyBuf, secuPWData *pwdata)
{
SECStatus rv;
PK11SymKey *key;
if (PK11_NeedLogin(slot)) {
rv = PK11_Authenticate(slot, PR_TRUE, pwdata);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR,
"Could not authenticate to token %s.\n",
PK11_GetTokenName(slot));
if (slot) {
PK11_FreeSlot(slot);
}
return NULL;
}
}
key = PK11_FindFixedKey(slot, mechanism, keyBuf, 0);
if (!key) {
PR_fprintf(PR_STDERR,
"PK11_FindFixedKey failed (err %d)\n",
PR_GetError());
PK11_FreeSlot(slot);
return NULL;
}
return key;
}
/*
* Decrypt and Verify MAC
*/
SECStatus
DecryptAndVerifyMac(const char* outFileName,
char *encryptedFileName,
SECItem *cItem, SECItem *macItem,
PK11SymKey* ek, PK11SymKey* mk, SECItem *ivItem, SECItem *padItem)
{
SECStatus rv;
PRFileDesc* inFile;
PRFileDesc* outFile;
unsigned char decbuf[64];
unsigned int decbufLen;
unsigned char ptext[BLOCKSIZE];
unsigned int ptextLen = 0;
unsigned char ctext[64];
unsigned int ctextLen;
unsigned char newmac[DIGESTSIZE];
unsigned int newmacLen = 0;
unsigned int newptextLen = 0;
unsigned int count = 0;
unsigned int temp = 0;
unsigned int blockNumber = 0;
SECItem noParams = { siBuffer, NULL, 0 };
PK11Context *ctxmac = NULL;
PK11Context *ctxenc = NULL;
unsigned char iv[BLOCKSIZE];
unsigned int ivLen = ivItem->len;
unsigned int fileLength;
unsigned int paddingLength;
int j;
memcpy(iv, ivItem->data, ivItem->len);
paddingLength = (unsigned int)padItem->data[0];
ctxmac = PK11_CreateContextBySymKey(CKM_MD5_HMAC, CKA_SIGN, mk, &noParams);
if (ctxmac == NULL) {
PR_fprintf(PR_STDERR, "Can't create MAC context\n");
rv = SECFailure;
goto cleanup;
}
/* Open the input file. */
inFile = PR_Open(encryptedFileName, PR_RDONLY , 0);
if (!inFile) {
PR_fprintf(PR_STDERR,
"Unable to open \"%s\" for writing.\n",
encryptedFileName);
return SECFailure;
}
/* Open the output file. */
outFile = PR_Open(outFileName,
PR_CREATE_FILE | PR_TRUNCATE | PR_RDWR , 00660);
if (!outFile) {
PR_fprintf(PR_STDERR,
"Unable to open \"%s\" for writing.\n",
outFileName);
return SECFailure;
}
rv = MacInit(ctxmac);
if (rv != SECSuccess) goto cleanup;
ctxenc = DecryptInit(ek, iv, ivLen, CKM_AES_CBC);
fileLength = FileSize(encryptedFileName);
while ((ctextLen = PR_Read(inFile, ctext, sizeof(ctext))) > 0) {
count += ctextLen;
/* decrypt cipher text buffer using CBC and IV */
rv = Decrypt(ctxenc, decbuf, &decbufLen, sizeof(decbuf),
ctext, ctextLen);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Decrypt Failure\n");
goto cleanup;
}
if (decbufLen == 0) break;
rv = MacUpdate(ctxmac, decbuf, decbufLen);
if (rv != SECSuccess) { goto cleanup; }
if (count == fileLength) {
decbufLen = decbufLen-paddingLength;
}
/* write the plain text to out file */
temp = PR_Write(outFile, decbuf, decbufLen);
if (temp != decbufLen) {
PR_fprintf(PR_STDERR, "write error\n");
rv = SECFailure;
break;
}
/* save last block of ciphertext */
memcpy(iv, decbuf, decbufLen);
ivLen = decbufLen;
blockNumber++;
}
if (rv != SECSuccess) { goto cleanup; }
rv = MacFinal(ctxmac, newmac, &newmacLen, sizeof(newmac));
if (rv != SECSuccess) { goto cleanup; }
if (PORT_Memcmp(macItem->data, newmac, newmacLen) == 0) {
rv = SECSuccess;
} else {
PR_fprintf(PR_STDERR, "Check MAC : Failure\n");
PR_fprintf(PR_STDERR, "Extracted : ");
PrintAsHex(PR_STDERR, macItem->data, macItem->len);
PR_fprintf(PR_STDERR, "Computed : ");
PrintAsHex(PR_STDERR, newmac, newmacLen);
rv = SECFailure;
}
cleanup:
if (ctxmac) {
PK11_DestroyContext(ctxmac, PR_TRUE);
}
if (ctxenc) {
PK11_DestroyContext(ctxenc, PR_TRUE);
}
if (outFile) {
PR_Close(outFile);
}
return rv;
}
/*
* Gets IV and CKAIDS From Header File
*/
SECStatus
GetIVandCKAIDSFromHeader(const char *cipherFileName,
SECItem *ivItem, SECItem *encKeyItem, SECItem *macKeyItem)
{
SECStatus rv;
/* open intermediate file, read in header, get IV and CKA_IDs of two keys
* from it
*/
rv = ReadFromHeaderFile(cipherFileName, IV, ivItem, PR_TRUE);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Could not retrieve IV from cipher file\n");
goto cleanup;
}
rv = ReadFromHeaderFile(cipherFileName, SYMKEY, encKeyItem, PR_TRUE);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR,
"Could not retrieve AES CKA_ID from cipher file\n");
goto cleanup;
}
rv = ReadFromHeaderFile(cipherFileName, MACKEY, macKeyItem, PR_TRUE);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR,
"Could not retrieve MAC CKA_ID from cipher file\n");
goto cleanup;
}
cleanup:
return rv;
}
/*
* DecryptFile
*/
SECStatus
DecryptFile(PK11SlotInfo *slot,
const char *dbdir,
const char *outFileName,
const char *headerFileName,
char *encryptedFileName,
secuPWData *pwdata,
PRBool ascii)
{
/*
* The DB is open read only and we have authenticated to it
* open input file, read in header, get IV and CKA_IDs of two keys from it
* find those keys in the DB token
* Open output file
* loop until EOF(input):
* read a buffer of ciphertext from input file,
* Save last block of ciphertext
* decrypt ciphertext buffer using CBC and IV,
* compute and check MAC, then remove MAC from plaintext
* replace IV with saved last block of ciphertext
* write the plain text to output file
* close files
* report success
*/
SECStatus rv;
SECItem ivItem;
SECItem encKeyItem;
SECItem macKeyItem;
SECItem cipherItem;
SECItem macItem;
SECItem padItem;
PK11SymKey *encKey = NULL;
PK11SymKey *macKey = NULL;
/* open intermediate file, read in header, get IV and CKA_IDs of two keys
* from it
*/
rv = GetIVandCKAIDSFromHeader(headerFileName,
&ivItem, &encKeyItem, &macKeyItem);
if (rv != SECSuccess) {
goto cleanup;
}
/* find those keys in the DB token */
encKey = FindKey(slot, CKM_AES_CBC, &encKeyItem, pwdata);
if (encKey == NULL) {
PR_fprintf(PR_STDERR, "Can't find the encryption key\n");
rv = SECFailure;
goto cleanup;
}
/* CKM_MD5_HMAC or CKM_EXTRACT_KEY_FROM_KEY */
macKey = FindKey(slot, CKM_MD5_HMAC, &macKeyItem, pwdata);
if (macKey == NULL) {
rv = SECFailure;
goto cleanup;
}
/* Read in the Mac into item from the intermediate file */
rv = ReadFromHeaderFile(headerFileName, MAC, &macItem, PR_TRUE);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR,
"Could not retrieve MAC from cipher file\n");
goto cleanup;
}
if (macItem.data == NULL) {
PR_fprintf(PR_STDERR, "MAC has NULL data\n");
rv = SECFailure;
goto cleanup;
}
if (macItem.len == 0) {
PR_fprintf(PR_STDERR, "MAC has data has 0 length\n");
/*rv = SECFailure;
goto cleanup;*/
}
rv = ReadFromHeaderFile(headerFileName, PAD, &padItem, PR_TRUE);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR,
"Could not retrieve PAD detail from header file\n");
goto cleanup;
}
if (rv == SECSuccess) {
/* Decrypt and Remove Mac */
rv = DecryptAndVerifyMac(outFileName, encryptedFileName,
&cipherItem, &macItem, encKey, macKey, &ivItem, &padItem);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Failed while decrypting and removing MAC\n");
}
}
cleanup:
if (slot) {
PK11_FreeSlot(slot);
}
if (encKey) {
PK11_FreeSymKey(encKey);
}
if (macKey) {
PK11_FreeSymKey(macKey);
}
return rv;
}
/*
* EncryptFile
*/
SECStatus
EncryptFile(PK11SlotInfo *slot,
const char *dbdir,
const char *inFileName,
const char *headerFileName,
const char *encryptedFileName,
const char *noiseFileName,
secuPWData *pwdata,
PRBool ascii)
{
/*
* The DB is open for read/write and we have authenticated to it.
* generate a symmetric AES key as a token object.
* generate a second key to use for MACing, also a token object.
* get their CKA_IDs
* generate a random value to use as IV for AES CBC
* open an input file and an output file,
* write a header to the output that identifies the two keys by
* their CKA_IDs, May include original file name and length.
* loop until EOF(input)
* read a buffer of plaintext from input file,
* MAC it, append the MAC to the plaintext
* encrypt it using CBC, using previously created IV,
* store the last block of ciphertext as the new IV,
* write the cipher text to intermediate file
* close files
* report success
*/
SECStatus rv;
PRFileDesc *inFile;
PRFileDesc *headerFile;
PRFileDesc *encFile;
unsigned char *encKeyId = (unsigned char *) "Encrypt Key";
unsigned char *macKeyId = (unsigned char *) "MAC Key";
SECItem encKeyID = { siAsciiString, encKeyId, PL_strlen(encKeyId) };
SECItem macKeyID = { siAsciiString, macKeyId, PL_strlen(macKeyId) };
SECItem encCKAID;
SECItem macCKAID;
unsigned char iv[BLOCKSIZE];
SECItem ivItem;
PK11SymKey *encKey = NULL;
PK11SymKey *macKey = NULL;
SECItem temp;
unsigned char c;
/* generate a symmetric AES key as a token object. */
encKey = GenerateSYMKey(slot, CKM_AES_KEY_GEN, 128/8, &encKeyID, pwdata);
if (encKey == NULL) {
PR_fprintf(PR_STDERR, "GenerateSYMKey for AES returned NULL.\n");
rv = SECFailure;
goto cleanup;
}
/* generate a second key to use for MACing, also a token object. */
macKey = GenerateSYMKey(slot, CKM_GENERIC_SECRET_KEY_GEN, 160/8,
&macKeyID, pwdata);
if (macKey == NULL) {
PR_fprintf(PR_STDERR, "GenerateSYMKey for MACing returned NULL.\n");
rv = SECFailure;
goto cleanup;
}
/* get the encrypt key CKA_ID */
rv = GatherCKA_ID(encKey, &encCKAID);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Error while wrapping encrypt key\n");
goto cleanup;
}
/* get the MAC key CKA_ID */
rv = GatherCKA_ID(macKey, &macCKAID);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Can't get the MAC key CKA_ID.\n");
goto cleanup;
}
if (noiseFileName) {
rv = SeedFromNoiseFile(noiseFileName);
if (rv != SECSuccess) {
PORT_SetError(PR_END_OF_FILE_ERROR);
return SECFailure;
}
rv = PK11_GenerateRandom(iv, BLOCKSIZE);
if (rv != SECSuccess) {
goto cleanup;
}
} else {
/* generate a random value to use as IV for AES CBC */
GenerateRandom(iv, BLOCKSIZE);
}
headerFile = PR_Open(headerFileName,
PR_CREATE_FILE | PR_TRUNCATE | PR_RDWR, 00660);
if (!headerFile) {
PR_fprintf(PR_STDERR,
"Unable to open \"%s\" for writing.\n",
headerFileName);
return SECFailure;
}
encFile = PR_Open(encryptedFileName,
PR_CREATE_FILE | PR_TRUNCATE | PR_RDWR, 00660);
if (!encFile) {
PR_fprintf(PR_STDERR,
"Unable to open \"%s\" for writing.\n",
encryptedFileName);
return SECFailure;
}
/* write to a header file the IV and the CKA_IDs
* identifying the two keys
*/
ivItem.type = siBuffer;
ivItem.data = iv;
ivItem.len = BLOCKSIZE;
rv = WriteToHeaderFile(iv, BLOCKSIZE, IV, headerFile);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Error writing IV to cipher file - %s\n",
headerFileName);
goto cleanup;
}
rv = WriteToHeaderFile(encCKAID.data, encCKAID.len, SYMKEY, headerFile);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Error writing AES CKA_ID to cipher file - %s\n",
encryptedFileName);
goto cleanup;
}
rv = WriteToHeaderFile(macCKAID.data, macCKAID.len, MACKEY, headerFile);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Error writing MAC CKA_ID to cipher file - %s\n",
headerFileName);
goto cleanup;
}
/* Open the input file. */
inFile = PR_Open(inFileName, PR_RDONLY, 0);
if (!inFile) {
PR_fprintf(PR_STDERR, "Unable to open \"%s\" for reading.\n",
inFileName);
return SECFailure;
}
/* Macing and Encryption */
if (rv == SECSuccess) {
rv = EncryptAndMac(inFile, headerFile, encFile,
encKey, macKey, ivItem.data, ivItem.len, ascii);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Failed : Macing and Encryption\n");
goto cleanup;
}
}
cleanup:
if (inFile) {
PR_Close(inFile);
}
if (headerFile) {
PR_Close(headerFile);
}
if (encFile) {
PR_Close(encFile);
}
if (slot) {
PK11_FreeSlot(slot);
}
if (encKey) {
PK11_FreeSymKey(encKey);
}
if (macKey) {
PK11_FreeSymKey(macKey);
}
return rv;
}
/*
* This example illustrates basic encryption/decryption and MACing
* Generates the encryption/mac keys and uses token for storing.
* Encrypts the input file and appends MAC before storing in intermediate
* header file.
* Writes the CKA_IDs of the encryption keys into intermediate header file.
* Reads the intermediate headerfile for CKA_IDs and encrypted
* contents and decrypts into output file.
*/
int
main(int argc, char **argv)
{
SECStatus rv;
SECStatus rvShutdown;
PK11SlotInfo *slot = NULL;
PLOptState *optstate;
PLOptStatus status;
char headerFileName[50];
char encryptedFileName[50];
PRFileDesc *inFile;
PRFileDesc *outFile;
PRBool ascii = PR_FALSE;
CommandType cmd = UNKNOWN;
const char *command = NULL;
const char *dbdir = NULL;
const char *inFileName = NULL;
const char *outFileName = NULL;
const char *noiseFileName = NULL;
secuPWData pwdata = { PW_NONE, 0 };
char * progName = strrchr(argv[0], '/');
progName = progName ? progName + 1 : argv[0];
/* Parse command line arguments */
optstate = PL_CreateOptState(argc, argv, "c:d:i:o:f:p:z:a");
while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
switch (optstate->option) {
case 'a':
ascii = PR_TRUE;
break;
case 'c':
command = strdup(optstate->value);
break;
case 'd':
dbdir = strdup(optstate->value);
break;
case 'f':
pwdata.source = PW_FROMFILE;
pwdata.data = strdup(optstate->value);
break;
case 'p':
pwdata.source = PW_PLAINTEXT;
pwdata.data = strdup(optstate->value);
break;
case 'i':
inFileName = strdup(optstate->value);
break;
case 'o':
outFileName = strdup(optstate->value);
break;
case 'z':
noiseFileName = strdup(optstate->value);
break;
default:
Usage(progName);
break;
}
}
PL_DestroyOptState(optstate);
if (!command || !dbdir || !inFileName || !outFileName)
Usage(progName);
if (PL_strlen(command)==0)
Usage(progName);
cmd = command[0] == 'a' ? ENCRYPT : command[0] == 'b' ? DECRYPT : UNKNOWN;
/* Open the input file. */
inFile = PR_Open(inFileName, PR_RDONLY, 0);
if (!inFile) {
PR_fprintf(PR_STDERR, "Unable to open \"%s\" for reading.\n",
inFileName);
return SECFailure;
}
PR_Close(inFile);
/* For intermediate header file, choose filename as inputfile name
with extension ".header" */
strcpy(headerFileName, inFileName);
strcat(headerFileName, ".header");
/* For intermediate encrypted file, choose filename as inputfile name
with extension ".enc" */
strcpy(encryptedFileName, inFileName);
strcat(encryptedFileName, ".enc");
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
switch (cmd) {
case ENCRYPT:
/* If the intermediate header file already exists, delete it */
if (PR_Access(headerFileName, PR_ACCESS_EXISTS) == PR_SUCCESS) {
PR_Delete(headerFileName);
}
/* If the intermediate encrypted already exists, delete it */
if (PR_Access(encryptedFileName, PR_ACCESS_EXISTS) == PR_SUCCESS) {
PR_Delete(encryptedFileName);
}
/* Open DB for read/write and authenticate to it. */
rv = NSS_InitReadWrite(dbdir);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "NSS_InitReadWrite Failed\n");
goto cleanup;
}
PK11_SetPasswordFunc(GetModulePassword);
slot = PK11_GetInternalKeySlot();
if (PK11_NeedLogin(slot)) {
rv = PK11_Authenticate(slot, PR_TRUE, &pwdata);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Could not authenticate to token %s.\n",
PK11_GetTokenName(slot));
goto cleanup;
}
}
rv = EncryptFile(slot, dbdir,
inFileName, headerFileName, encryptedFileName,
noiseFileName, &pwdata, ascii);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "EncryptFile : Failed\n");
return SECFailure;
}
break;
case DECRYPT:
/* Open DB read only, authenticate to it */
PK11_SetPasswordFunc(GetModulePassword);
rv = NSS_Init(dbdir);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "NSS_Init Failed\n");
return SECFailure;
}
slot = PK11_GetInternalKeySlot();
if (PK11_NeedLogin(slot)) {
rv = PK11_Authenticate(slot, PR_TRUE, &pwdata);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "Could not authenticate to token %s.\n",
PK11_GetTokenName(slot));
goto cleanup;
}
}
rv = DecryptFile(slot, dbdir,
outFileName, headerFileName,
encryptedFileName, &pwdata, ascii);
if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "DecryptFile : Failed\n");
return SECFailure;
}
break;
}
cleanup:
rvShutdown = NSS_Shutdown();
if (rvShutdown != SECSuccess) {
PR_fprintf(PR_STDERR, "Failed : NSS_Shutdown()\n");
rv = SECFailure;
}
PR_Cleanup();
return rv;
}
|