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 * feeCipherFile.c - general cipherfile support
16 * Added CFE_FEED and CFE_FEEDExp types.
21 #include "feeCipherFile.h"
23 #include "feeFEEDExp.h"
26 #include "CipherFileFEED.h"
27 #include "CipherFileDES.h"
31 * Create a cipherfile of specified cipherFileEncrType.
33 feeReturn
createCipherFile(feePubKey sendPrivKey
,
35 cipherFileEncrType encrType
,
36 const unsigned char *plainText
,
37 unsigned plainTextLen
,
38 int genSig
, // 1 ==> generate signature
39 int doEnc64
, // 1 ==> perform enc64
40 unsigned userData
, // for caller's convenience
41 unsigned char **cipherFileData
, // RETURNED
42 unsigned *cipherFileDataLen
) // RETURNED
44 feeReturn frtn
= FR_Success
;
45 feeCipherFile cipherFile
= NULL
;
46 unsigned char *cipherData
= NULL
;
47 unsigned cipherDataLen
;
50 * Dispatch to encrType-specific code.
54 frtn
= createRandDES(sendPrivKey
,
63 frtn
= createPubDES(sendPrivKey
,
72 frtn
= createFEED(sendPrivKey
,
81 frtn
= createFEEDExp(sendPrivKey
,
90 frtn
= FR_Unimplemented
;
99 * Common logic for all encrTypes
103 * Get the cipherfile's raw data
105 frtn
= feeCFileDataRepresentation(cipherFile
,
106 (const unsigned char **)&cipherData
,
113 * Optionally encode in 64-char ASCII
116 *cipherFileData
= enc64(cipherData
,
120 if(*cipherFileData
== NULL
) {
127 *cipherFileData
= cipherData
;
128 *cipherFileDataLen
= cipherDataLen
;
133 feeCFileFree(cipherFile
);
139 * Parse a cipherfile.
141 * sendPubKey only needed for cipherFileEncrType CFE_RandDES if signature
142 * is present. If sendPubKey is present, it will be used for signature
143 * validation rather than the embedded sender's public key.
145 feeReturn
parseCipherFile(feePubKey recvPrivKey
,
146 feePubKey sendPubKey
,
147 const unsigned char *cipherFileData
,
148 unsigned cipherFileDataLen
,
149 int doDec64
, // 1 ==> perform dec64
150 cipherFileEncrType
*encrType
, // RETURNED
151 unsigned char **plainText
, // RETURNED
152 unsigned *plainTextLen
, // RETURNED
153 feeSigStatus
*sigStatus
, // RETURNED
154 unsigned *userData
) // RETURNED
157 unsigned char *cipherData
= NULL
;
158 unsigned cipherDataLen
;
159 int freeCipherData
= 0;
160 feeCipherFile cipherFile
= NULL
;
165 if(recvPrivKey
== NULL
) { // always required
171 * First, optional dec64()
174 cipherData
= dec64(cipherFileData
,
177 if(cipherData
== NULL
) {
186 cipherData
= (unsigned char *)cipherFileData
;
187 cipherDataLen
= cipherFileDataLen
;
191 * Cons up a feeCipherFile object.
193 frtn
= feeCFileNewFromDataRep(cipherData
,
199 *encrType
= feeCFileEncrType(cipherFile
);
200 *userData
= feeCFileUserData(cipherFile
);
201 frtn
= decryptCipherFile(cipherFile
,
211 if(cipherData
&& freeCipherData
) {
215 feeCFileFree(cipherFile
);
221 * Decrypt a feeCipherFile obtained via feeCFileNewFromDataRep().
222 * recvPrivKey is required in all cases. If sendPubKey is present,
223 * sendPubKey - rather than the embedded sender's public key - will be
224 * used for signature validation.
226 feeReturn
decryptCipherFile(feeCipherFile cipherFile
,
227 feePubKey recvPrivKey
, // required
228 feePubKey sendPubKey
, // optional, for signature
229 unsigned char **plainText
, // malloc'd & RETURNED
230 unsigned *plainTextLen
, // RETURNED
231 feeSigStatus
*sigStatus
) // RETURNED
233 cipherFileEncrType encrType
= feeCFileEncrType(cipherFile
);
240 * Dispatch to encrType-specific code.
244 frtn
= decryptRandDES(cipherFile
,
252 frtn
= decryptPubDES(cipherFile
,
260 frtn
= decryptFEED(cipherFile
,
268 frtn
= decryptFEEDExp(cipherFile
,
276 frtn
= FR_Unimplemented
;