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 * CipherFileFEED.c - FEED and FEEDExp related cipherfile support
16 * Fixed memory leaks via sigData
18 * Split off from feeCipherFile.c
23 #if CRYPTKIT_CIPHERFILE_ENABLE
26 #include "CipherFileFEED.h"
30 feeReturn
createFEED(feePubKey sendPrivKey
, // required
32 const unsigned char *plainText
,
33 unsigned plainTextLen
,
34 int genSig
, // 1 ==> generate signature
35 unsigned userData
, // for caller's convenience
36 feeCipherFile
*cipherFile
) // RETURNED if successful
40 unsigned char *cipherText
= NULL
;
41 unsigned cipherTextLen
;
42 unsigned char *sigData
= NULL
;
43 unsigned sigDataLen
= 0;
44 feeCipherFile cfile
= NULL
;
45 unsigned char *pubKeyString
= NULL
; // of sendPrivKey
46 unsigned pubKeyStringLen
= 0;
48 if((sendPrivKey
== NULL
) || (recvPubKey
== NULL
)) {
53 * FEED encrypt plaintext
55 feed
= feeFEEDNewWithPubKey(sendPrivKey
, recvPubKey
, FF_ENCRYPT
, NULL
, NULL
);
60 frtn
= feeFEEDEncrypt(feed
,
70 * Sender's public key string
72 frtn
= feePubKeyCreateKeyString(sendPrivKey
,
73 (char **)&pubKeyString
,
85 * We generate signature on ciphertext by convention.
87 frtn
= feePubKeyCreateSignature(sendPrivKey
,
98 * Cons up a cipherfile
100 cfile
= feeCFileNewFromCipherText(CFE_FEED
,
116 /* free alloc'd stuff */
135 feeReturn
decryptFEED(feeCipherFile cipherFile
,
136 feePubKey recvPrivKey
,
137 feePubKey sendPubKey
, // optional
138 unsigned char **plainText
, // RETURNED
139 unsigned *plainTextLen
, // RETURNED
140 feeSigStatus
*sigStatus
) // RETURNED
142 feeReturn frtn
= FR_Success
;
143 unsigned char *cipherText
= NULL
;
144 unsigned cipherTextLen
;
146 unsigned char *sigData
= NULL
;
148 unsigned char *sendPubKeyStr
= NULL
;
149 unsigned sendPubKeyStrLen
= 0;
150 feePubKey parsedSendPubKey
= NULL
;
152 if(feeCFileEncrType(cipherFile
) != CFE_FEED
) {
156 //printf("decryptFEED\n");
157 //printf("privKey:\n"); printPubKey(recvPrivKey);
158 //printf("pubKey:\n"); printPubKey(sendPubKey);
160 * Get ciphertext and sender's public key from cipherFile
162 cipherText
= feeCFileCipherText(cipherFile
, &cipherTextLen
);
163 if(cipherText
== NULL
) {
164 frtn
= FR_BadCipherFile
;
167 sendPubKeyStr
= feeCFileSendPubKeyData(cipherFile
, &sendPubKeyStrLen
);
168 if(sendPubKeyStr
== NULL
) {
169 frtn
= FR_BadCipherFile
;
172 parsedSendPubKey
= feePubKeyAlloc();
173 frtn
= feePubKeyInitFromKeyString(parsedSendPubKey
,
174 (char *)sendPubKeyStr
,
177 frtn
= FR_BadCipherFile
;
180 //printf("parsedSendPubKey:\n"); printPubKey(parsedSendPubKey);
185 feed
= feeFEEDNewWithPubKey(recvPrivKey
, parsedSendPubKey
, FF_DECRYPT
, NULL
, NULL
);
190 frtn
= feeFEEDDecrypt(feed
,
199 sigData
= feeCFileSigData(cipherFile
, &sigDataLen
);
203 if(sendPubKey
== NULL
) {
205 * use embedded sender's public key
207 sendPubKey
= parsedSendPubKey
;
209 sigFrtn
= feePubKeyVerifySignature(sendPubKey
,
216 *sigStatus
= SS_PresentValid
;
219 *sigStatus
= SS_PresentInvalid
;
224 *sigStatus
= SS_NotPresent
;
236 if(parsedSendPubKey
) {
237 feePubKeyFree(parsedSendPubKey
);
240 ffree(sendPubKeyStr
);
245 feeReturn
createFEEDExp(feePubKey sendPrivKey
, // for sig only
246 feePubKey recvPubKey
,
247 const unsigned char *plainText
,
248 unsigned plainTextLen
,
249 int genSig
, // 1 ==> generate signature
250 unsigned userData
, // for caller's convenience
251 feeCipherFile
*cipherFile
) // RETURNED if successful
254 feeFEEDExp feed
= NULL
;
255 unsigned char *cipherText
= NULL
;
256 unsigned cipherTextLen
;
257 unsigned char *sigData
= NULL
;
258 unsigned sigDataLen
= 0;
259 feeCipherFile cfile
= NULL
;
260 unsigned char *pubKeyString
= NULL
; // of sendPrivKey, for sig
261 unsigned pubKeyStringLen
= 0;
263 if(recvPubKey
== NULL
) {
268 * FEEDExp encrypt plaintext
270 feed
= feeFEEDExpNewWithPubKey(recvPubKey
, NULL
, NULL
);
275 frtn
= feeFEEDExpEncrypt(feed
,
285 if(sendPrivKey
== NULL
) {
286 frtn
= FR_IllegalArg
;
290 * We generate signature on ciphertext by convention.
292 frtn
= feePubKeyCreateSignature(sendPrivKey
,
301 * Sender's public key string
303 frtn
= feePubKeyCreateKeyString(sendPrivKey
,
304 (char **)&pubKeyString
,
316 * Cons up a cipherfile
318 cfile
= feeCFileNewFromCipherText(CFE_FEEDExp
,
334 /* free alloc'd stuff */
340 feeFEEDExpFree(feed
);
353 feeReturn
decryptFEEDExp(feeCipherFile cipherFile
,
354 feePubKey recvPrivKey
,
355 feePubKey sendPubKey
, // optional
356 unsigned char **plainText
, // RETURNED
357 unsigned *plainTextLen
, // RETURNED
358 feeSigStatus
*sigStatus
) // RETURNED
360 feeReturn frtn
= FR_Success
;
361 unsigned char *cipherText
= NULL
;
362 unsigned cipherTextLen
;
363 feeFEEDExp feed
= NULL
;
364 unsigned char *sigData
= NULL
;
366 unsigned char *sendPubKeyStr
= NULL
;
367 unsigned sendPubKeyStrLen
= 0;
368 feePubKey parsedSendPubKey
= NULL
;
370 if(feeCFileEncrType(cipherFile
) != CFE_FEEDExp
) {
376 * Get ciphertext from cipherFile
378 cipherText
= feeCFileCipherText(cipherFile
, &cipherTextLen
);
379 if(cipherText
== NULL
) {
380 frtn
= FR_BadCipherFile
;
387 feed
= feeFEEDExpNewWithPubKey(recvPrivKey
, NULL
, NULL
);
392 frtn
= feeFEEDExpDecrypt(feed
,
401 sigData
= feeCFileSigData(cipherFile
, &sigDataLen
);
405 if(sendPubKey
== NULL
) {
407 * use embedded sender's public key
409 sendPubKeyStr
= feeCFileSendPubKeyData(cipherFile
,
411 if(sendPubKeyStr
== NULL
) {
412 frtn
= FR_BadCipherFile
;
415 parsedSendPubKey
= feePubKeyAlloc();
416 frtn
= feePubKeyInitFromKeyString(parsedSendPubKey
,
417 (char *)sendPubKeyStr
, sendPubKeyStrLen
);
419 frtn
= FR_BadCipherFile
;
422 sendPubKey
= parsedSendPubKey
;
424 sigFrtn
= feePubKeyVerifySignature(sendPubKey
,
431 *sigStatus
= SS_PresentValid
;
434 *sigStatus
= SS_PresentInvalid
;
439 *sigStatus
= SS_NotPresent
;
446 feeFEEDExpFree(feed
);
451 if(parsedSendPubKey
) {
452 feePubKeyFree(parsedSendPubKey
);
455 ffree(sendPubKeyStr
);
460 #endif /* CRYPTKIT_CIPHERFILE_ENABLE */