1 /* Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
11 * CipherFileDES.c - DES-related cipherfile support
16 * Fixed memory leaks via sigData
18 * Split off from feeCipherFile.c
23 #if CRYPTKIT_CIPHERFILE_ENABLE
26 #include "CipherFileDES.h"
32 * These functions are only called from feeCipherFile.c.
34 feeReturn
createRandDES(feePubKey sendPrivKey
, // for sig only
36 const unsigned char *plainText
,
37 unsigned plainTextLen
,
38 int genSig
, // 1 ==> generate signature
39 unsigned userData
, // for caller's convenience
40 feeCipherFile
*cipherFile
) // RETURNED if successful
44 unsigned char desKey
[FEE_DES_MIN_STATE_SIZE
];
45 unsigned char *encrDesKey
= NULL
; // FEED encrypted desKey
46 unsigned encrDesKeyLen
;
48 feeFEEDExp feed
= NULL
;
49 unsigned char *cipherText
= NULL
;
50 unsigned cipherTextLen
;
51 unsigned char *sigData
= NULL
;
52 unsigned sigDataLen
= 0;
53 feeCipherFile cfile
= NULL
;
54 unsigned char *pubKeyString
= NULL
; // of sendPrivKey
55 unsigned pubKeyStringLen
= 0;
57 if(recvPubKey
== NULL
) {
62 * Cons up random DES key and a feeDES object with it
64 frand
= feeRandAlloc();
69 feeRandBytes(frand
, desKey
, FEE_DES_MIN_STATE_SIZE
);
70 des
= feeDESNewWithState(desKey
, FEE_DES_MIN_STATE_SIZE
);
77 * Encrypt the DES key via FEEDExp
79 feed
= feeFEEDExpNewWithPubKey(recvPubKey
, NULL
, NULL
);
84 frtn
= feeFEEDExpEncrypt(feed
,
86 FEE_DES_MIN_STATE_SIZE
,
94 * Encrypt the plaintext via DES
96 frtn
= feeDESEncrypt(des
,
107 * We generate signature on ciphertext by convention.
109 if(sendPrivKey
== NULL
) {
113 frtn
= feePubKeyCreateSignature(sendPrivKey
,
122 * Sender's public key string
124 frtn
= feePubKeyCreateKeyString(sendPrivKey
,
125 (char **)&pubKeyString
,
137 * Cons up a cipherfile
139 cfile
= feeCFileNewFromCipherText(CFE_RandDES
,
155 /* free alloc'd stuff */
161 feeFEEDExpFree(feed
);
178 memset(desKey
, 0, FEE_DES_MIN_STATE_SIZE
);
184 feeReturn
decryptRandDES(feeCipherFile cipherFile
,
185 feePubKey recvPrivKey
,
186 feePubKey sendPubKey
, // optional
187 unsigned char **plainText
, // RETURNED
188 unsigned *plainTextLen
, // RETURNED
189 feeSigStatus
*sigStatus
) // RETURNED
191 feeReturn frtn
= FR_Success
;
192 unsigned char *cipherText
= NULL
;
193 unsigned cipherTextLen
;
194 feeFEEDExp feed
= NULL
; // to decrypt desKey
195 feeDES des
= NULL
; // to decrypt cipherText
196 unsigned char *desKey
;
198 unsigned char *encrDesKey
= NULL
; // FEED encrypted desKey
199 unsigned encrDesKeyLen
;
200 unsigned char *sigData
= NULL
;
202 unsigned char *sendPubKeyStr
= NULL
;
203 unsigned sendPubKeyStrLen
= 0;
204 feePubKey parsedSendPubKey
= NULL
;
206 if(feeCFileEncrType(cipherFile
) != CFE_RandDES
) {
212 * Get ciphertext and encrypted DES key from cipherFile
214 cipherText
= feeCFileCipherText(cipherFile
, &cipherTextLen
);
215 if(cipherText
== NULL
) {
216 frtn
= FR_BadCipherFile
;
219 encrDesKey
= feeCFileOtherKeyData(cipherFile
, &encrDesKeyLen
);
220 if(encrDesKey
== NULL
) {
221 frtn
= FR_BadCipherFile
;
226 * FEED decrypt to get DES key
228 feed
= feeFEEDExpNewWithPubKey(recvPrivKey
, NULL
, NULL
);
233 frtn
= feeFEEDExpDecrypt(feed
,
243 * Now DES decrypt the ciphertext
245 if(desKeyLen
!= FEE_DES_MIN_STATE_SIZE
) {
246 frtn
= FR_BadCipherFile
;
249 des
= feeDESNewWithState(desKey
, desKeyLen
);
254 frtn
= feeDESDecrypt(des
,
263 sigData
= feeCFileSigData(cipherFile
, &sigDataLen
);
267 if(sendPubKey
== NULL
) {
269 * Obtain sender's public key from cipherfile
271 sendPubKeyStr
= feeCFileSendPubKeyData(cipherFile
,
273 if(sendPubKeyStr
== NULL
) {
275 * Hmm..shouldn't really happen, but let's
278 *sigStatus
= SS_PresentNoKey
;
281 parsedSendPubKey
= feePubKeyAlloc();
282 frtn
= feePubKeyInitFromKeyString(parsedSendPubKey
,
283 (char *)sendPubKeyStr
, sendPubKeyStrLen
);
285 dbgLog(("parseRandDES: bad sendPubKeyStr\n"));
286 *sigStatus
= SS_PresentNoKey
;
289 sendPubKey
= parsedSendPubKey
;
291 sigFrtn
= feePubKeyVerifySignature(sendPubKey
,
298 *sigStatus
= SS_PresentValid
;
301 *sigStatus
= SS_PresentInvalid
;
306 *sigStatus
= SS_NotPresent
;
313 feeFEEDExpFree(feed
);
319 memset(desKey
, 0, desKeyLen
);
328 if(parsedSendPubKey
) {
329 feePubKeyFree(parsedSendPubKey
);
332 ffree(sendPubKeyStr
);
337 feeReturn
createPubDES(feePubKey sendPrivKey
, // required
338 feePubKey recvPubKey
,
339 const unsigned char *plainText
,
340 unsigned plainTextLen
,
341 int genSig
, // 1 ==> generate signature
342 unsigned userData
, // for caller's convenience
343 feeCipherFile
*cipherFile
) // RETURNED if successful
345 feeRand frand
= NULL
;
347 unsigned char *desKey
;
350 unsigned char *cipherText
= NULL
;
351 unsigned cipherTextLen
;
352 unsigned char *sigData
= NULL
;
353 unsigned sigDataLen
= 0;
354 feeCipherFile cfile
= NULL
;
355 unsigned char *pubKeyString
= NULL
;
356 unsigned pubKeyStringLen
;
358 if((sendPrivKey
== NULL
) || (recvPubKey
== NULL
)) {
363 * Get the public string version of sendPrivKey for embedding in
366 frtn
= feePubKeyCreateKeyString(sendPrivKey
,
367 (char **)&pubKeyString
,
374 * Obtain DES key via key exchange and get a feeDES object with it
376 frtn
= feePubKeyCreatePad(sendPrivKey
,
383 des
= feeDESNewWithState(desKey
, desKeyLen
);
390 * Encrypt the plaintext via DES
392 frtn
= feeDESEncrypt(des
,
403 * We generate signature on ciphertext by convention.
405 frtn
= feePubKeyCreateSignature(sendPrivKey
,
416 * Cons up a cipherfile
418 cfile
= feeCFileNewFromCipherText(CFE_PublicDES
,
434 /* free alloc'd stuff */
459 feeReturn
decryptPubDES(feeCipherFile cipherFile
,
460 feePubKey recvPrivKey
,
461 feePubKey sendPubKey
,
462 unsigned char **plainText
, // RETURNED
463 unsigned *plainTextLen
, // RETURNED
464 feeSigStatus
*sigStatus
) // RETURNED
466 feeReturn frtn
= FR_Success
;
467 unsigned char *cipherText
= NULL
;
468 unsigned cipherTextLen
;
469 feeDES des
= NULL
; // to decrypt cipherText
470 unsigned char *desKey
;
472 unsigned char *sigData
= NULL
;
474 unsigned char *pubKeyString
= NULL
;
475 unsigned pubKeyStringLen
;
476 feePubKey decryptPubKey
= NULL
; // from cipherfile
478 if(feeCFileEncrType(cipherFile
) != CFE_PublicDES
) {
484 * Get ciphertext and sender's public key from cipherFile
486 cipherText
= feeCFileCipherText(cipherFile
, &cipherTextLen
);
487 if(cipherText
== NULL
) {
488 frtn
= FR_BadCipherFile
;
491 pubKeyString
= feeCFileSendPubKeyData(cipherFile
, &pubKeyStringLen
);
492 if(pubKeyString
== NULL
) {
493 frtn
= FR_BadCipherFile
;
496 decryptPubKey
= feePubKeyAlloc();
497 frtn
= feePubKeyInitFromKeyString(decryptPubKey
,
498 (char *)pubKeyString
,
505 * key exchange to get DES key
507 frtn
= feePubKeyCreatePad(recvPrivKey
,
516 * Now DES decrypt the ciphertext
518 if(desKeyLen
< FEE_DES_MIN_STATE_SIZE
) {
519 frtn
= FR_BadCipherFile
;
522 des
= feeDESNewWithState(desKey
, desKeyLen
);
527 frtn
= feeDESDecrypt(des
,
536 sigData
= feeCFileSigData(cipherFile
, &sigDataLen
);
540 if(sendPubKey
== NULL
) {
542 * Use key embedded in cipherfile
544 sendPubKey
= decryptPubKey
;
546 sigFrtn
= feePubKeyVerifySignature(sendPubKey
,
553 *sigStatus
= SS_PresentValid
;
556 *sigStatus
= SS_PresentInvalid
;
561 *sigStatus
= SS_NotPresent
;
580 feePubKeyFree(decryptPubKey
);
585 #endif /* CRYPTKIT_CIPHERFILE_ENABLE */