#include "ckconfig.h"
#if CRYPTKIT_CIPHERFILE_ENABLE
#include "Crypt.h"
#include "CipherFileFEED.h"
#include "falloc.h"
#include "feeDebug.h"
feeReturn createFEED(feePubKey sendPrivKey, feePubKey recvPubKey,
const unsigned char *plainText,
unsigned plainTextLen,
int genSig, unsigned userData, feeCipherFile *cipherFile) {
feeReturn frtn;
feeFEED feed = NULL;
unsigned char *cipherText = NULL;
unsigned cipherTextLen;
unsigned char *sigData = NULL;
unsigned sigDataLen = 0;
feeCipherFile cfile = NULL;
unsigned char *pubKeyString = NULL; unsigned pubKeyStringLen = 0;
if((sendPrivKey == NULL) || (recvPubKey == NULL)) {
return FR_BadPubKey;
}
feed = feeFEEDNewWithPubKey(sendPrivKey, recvPubKey, FF_ENCRYPT, NULL, NULL);
if(feed == NULL) {
frtn = FR_BadPubKey;
goto out;
}
frtn = feeFEEDEncrypt(feed,
plainText,
plainTextLen,
&cipherText,
&cipherTextLen);
if(frtn) {
goto out;
}
frtn = feePubKeyCreateKeyString(sendPrivKey,
(char **)&pubKeyString,
&pubKeyStringLen);
if(frtn) {
frtn = FR_BadPubKey;
goto out;
}
if(genSig) {
frtn = feePubKeyCreateSignature(sendPrivKey,
cipherText,
cipherTextLen,
&sigData,
&sigDataLen);
if(frtn) {
goto out;
}
}
cfile = feeCFileNewFromCipherText(CFE_FEED,
cipherText,
cipherTextLen,
pubKeyString,
pubKeyStringLen,
NULL,
0,
sigData,
sigDataLen,
userData);
if(cfile == NULL) {
frtn = FR_Internal;
goto out;
}
out:
if(cipherText) {
ffree(cipherText);
}
if(feed) {
feeFEEDFree(feed);
}
if(pubKeyString) {
ffree(pubKeyString);
}
if(sigData) {
ffree(sigData);
}
*cipherFile = cfile;
return frtn;
}
feeReturn decryptFEED(feeCipherFile cipherFile,
feePubKey recvPrivKey,
feePubKey sendPubKey, unsigned char **plainText, unsigned *plainTextLen, feeSigStatus *sigStatus) {
feeReturn frtn = FR_Success;
unsigned char *cipherText = NULL;
unsigned cipherTextLen;
feeFEED feed = NULL;
unsigned char *sigData = NULL;
unsigned sigDataLen;
unsigned char *sendPubKeyStr = NULL;
unsigned sendPubKeyStrLen = 0;
feePubKey parsedSendPubKey = NULL;
if(feeCFileEncrType(cipherFile) != CFE_FEED) {
frtn = FR_Internal;
goto out;
}
cipherText = feeCFileCipherText(cipherFile, &cipherTextLen);
if(cipherText == NULL) {
frtn = FR_BadCipherFile;
goto out;
}
sendPubKeyStr = feeCFileSendPubKeyData(cipherFile, &sendPubKeyStrLen);
if(sendPubKeyStr == NULL) {
frtn = FR_BadCipherFile;
goto out;
}
parsedSendPubKey = feePubKeyAlloc();
frtn = feePubKeyInitFromKeyString(parsedSendPubKey,
(char *)sendPubKeyStr,
sendPubKeyStrLen);
if(frtn) {
frtn = FR_BadCipherFile;
goto out;
}
feed = feeFEEDNewWithPubKey(recvPrivKey, parsedSendPubKey, FF_DECRYPT, NULL, NULL);
if(feed == NULL) {
frtn = FR_BadPubKey;
goto out;
}
frtn = feeFEEDDecrypt(feed,
cipherText,
cipherTextLen,
plainText,
plainTextLen);
if(frtn) {
goto out;
}
sigData = feeCFileSigData(cipherFile, &sigDataLen);
if(sigData) {
feeReturn sigFrtn;
if(sendPubKey == NULL) {
sendPubKey = parsedSendPubKey;
}
sigFrtn = feePubKeyVerifySignature(sendPubKey,
cipherText,
cipherTextLen,
sigData,
sigDataLen);
switch(sigFrtn) {
case FR_Success:
*sigStatus = SS_PresentValid;
break;
default:
*sigStatus = SS_PresentInvalid;
break;
}
}
else {
*sigStatus = SS_NotPresent;
}
out:
if(cipherText) {
ffree(cipherText);
}
if(feed) {
feeFEEDFree(feed);
}
if(sigData) {
ffree(sigData);
}
if(parsedSendPubKey) {
feePubKeyFree(parsedSendPubKey);
}
if(sendPubKeyStr) {
ffree(sendPubKeyStr);
}
return frtn;
}
feeReturn createFEEDExp(feePubKey sendPrivKey, feePubKey recvPubKey,
const unsigned char *plainText,
unsigned plainTextLen,
int genSig, unsigned userData, feeCipherFile *cipherFile) {
feeReturn frtn;
feeFEEDExp feed = NULL;
unsigned char *cipherText = NULL;
unsigned cipherTextLen;
unsigned char *sigData = NULL;
unsigned sigDataLen = 0;
feeCipherFile cfile = NULL;
unsigned char *pubKeyString = NULL; unsigned pubKeyStringLen = 0;
if(recvPubKey == NULL) {
return FR_BadPubKey;
}
feed = feeFEEDExpNewWithPubKey(recvPubKey, NULL, NULL);
if(feed == NULL) {
frtn = FR_BadPubKey;
goto out;
}
frtn = feeFEEDExpEncrypt(feed,
plainText,
plainTextLen,
&cipherText,
&cipherTextLen);
if(frtn) {
goto out;
}
if(genSig) {
if(sendPrivKey == NULL) {
frtn = FR_IllegalArg;
goto out;
}
frtn = feePubKeyCreateSignature(sendPrivKey,
cipherText,
cipherTextLen,
&sigData,
&sigDataLen);
if(frtn) {
goto out;
}
frtn = feePubKeyCreateKeyString(sendPrivKey,
(char **)&pubKeyString,
&pubKeyStringLen);
if(frtn) {
frtn = FR_BadPubKey;
goto out;
}
}
cfile = feeCFileNewFromCipherText(CFE_FEEDExp,
cipherText,
cipherTextLen,
pubKeyString,
pubKeyStringLen,
NULL,
0,
sigData,
sigDataLen,
userData);
if(cfile == NULL) {
frtn = FR_Internal;
goto out;
}
out:
if(cipherText) {
ffree(cipherText);
}
if(feed) {
feeFEEDExpFree(feed);
}
if(sigData) {
ffree(sigData);
}
if(pubKeyString) {
ffree(pubKeyString);
}
*cipherFile = cfile;
return frtn;
}
feeReturn decryptFEEDExp(feeCipherFile cipherFile,
feePubKey recvPrivKey,
feePubKey sendPubKey, unsigned char **plainText, unsigned *plainTextLen, feeSigStatus *sigStatus) {
feeReturn frtn = FR_Success;
unsigned char *cipherText = NULL;
unsigned cipherTextLen;
feeFEEDExp feed = NULL;
unsigned char *sigData = NULL;
unsigned sigDataLen;
unsigned char *sendPubKeyStr = NULL;
unsigned sendPubKeyStrLen = 0;
feePubKey parsedSendPubKey = NULL;
if(feeCFileEncrType(cipherFile) != CFE_FEEDExp) {
frtn = FR_Internal;
goto out;
}
cipherText = feeCFileCipherText(cipherFile, &cipherTextLen);
if(cipherText == NULL) {
frtn = FR_BadCipherFile;
goto out;
}
feed = feeFEEDExpNewWithPubKey(recvPrivKey, NULL, NULL);
if(feed == NULL) {
frtn = FR_BadPubKey;
goto out;
}
frtn = feeFEEDExpDecrypt(feed,
cipherText,
cipherTextLen,
plainText,
plainTextLen);
if(frtn) {
goto out;
}
sigData = feeCFileSigData(cipherFile, &sigDataLen);
if(sigData) {
feeReturn sigFrtn;
if(sendPubKey == NULL) {
sendPubKeyStr = feeCFileSendPubKeyData(cipherFile,
&sendPubKeyStrLen);
if(sendPubKeyStr == NULL) {
frtn = FR_BadCipherFile;
goto out;
}
parsedSendPubKey = feePubKeyAlloc();
frtn = feePubKeyInitFromKeyString(parsedSendPubKey,
(char *)sendPubKeyStr, sendPubKeyStrLen);
if(frtn) {
frtn = FR_BadCipherFile;
goto out;
}
sendPubKey = parsedSendPubKey;
}
sigFrtn = feePubKeyVerifySignature(sendPubKey,
cipherText,
cipherTextLen,
sigData,
sigDataLen);
switch(sigFrtn) {
case FR_Success:
*sigStatus = SS_PresentValid;
break;
default:
*sigStatus = SS_PresentInvalid;
break;
}
}
else {
*sigStatus = SS_NotPresent;
}
out:
if(cipherText) {
ffree(cipherText);
}
if(feed) {
feeFEEDExpFree(feed);
}
if(sigData) {
ffree(sigData);
}
if(parsedSendPubKey) {
feePubKeyFree(parsedSendPubKey);
}
if(sendPubKeyStr) {
ffree(sendPubKeyStr);
}
return frtn;
}
#endif