#include "desContext.h"
#include <Security/debugging.h>
#include <Security/globalizer.h>
#include <Security/threading.h>
ModuleNexus<Mutex> desInitMutex;
#define DESDebug(args...) debug("desContext", ## args)
DESContext::~DESContext()
{
desdone(&DesInst);
memset(&DesInst, 0, sizeof(struct _desInst));
}
void DESContext::init(
const Context &context,
bool encrypting)
{
UInt32 keyLen;
UInt8 *keyData = NULL;
symmetricKeyBits(context, CSSM_ALGID_DES,
encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
keyData, keyLen);
if(keyLen != (DES_KEY_SIZE_BITS_EXTERNAL / 8)) {
CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
}
{
StLock<Mutex> _(desInitMutex());
if(int irtn = desinit(&DesInst, DES_MODE_STD)) {
DESDebug("desinit returned %d\n", irtn);
CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR);
}
}
dessetkey(&DesInst, (char *)keyData);
setup(DES_BLOCK_SIZE_BYTES, context);
}
void DESContext::encryptBlock(
const void *plainText, size_t plainTextLen,
void *cipherText,
size_t &cipherTextLen, bool final) {
if(plainTextLen != DES_BLOCK_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR);
}
if(cipherTextLen < DES_BLOCK_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
}
if(plainText != cipherText) {
memmove(cipherText, plainText, DES_BLOCK_SIZE_BYTES);
}
endes(&DesInst, (char *)cipherText);
cipherTextLen = DES_BLOCK_SIZE_BYTES;
}
void DESContext::decryptBlock(
const void *cipherText, void *plainText,
size_t &plainTextLen, bool final) {
if(plainTextLen < DES_BLOCK_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
}
if(plainText != cipherText) {
memmove(plainText, cipherText, DES_BLOCK_SIZE_BYTES);
}
dedes(&DesInst, (char *)plainText);
plainTextLen = DES_BLOCK_SIZE_BYTES;
}
DES3Context::~DES3Context()
{
for(int i =0; i<3; i++) {
desdone(&DesInst[i]);
memset(&DesInst[i], 0, sizeof(struct _desInst));
}
}
void DES3Context::init(
const Context &context,
bool encrypting)
{
UInt32 keyLen;
UInt8 *keyData = NULL;
symmetricKeyBits(context, CSSM_ALGID_3DES_3KEY_EDE,
encrypting ? CSSM_KEYUSE_ENCRYPT : CSSM_KEYUSE_DECRYPT,
keyData, keyLen);
if(keyLen != DES3_KEY_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_KEY);
}
int irtn;
unsigned i;
{
StLock<Mutex> _(desInitMutex());
for(i=0; i<3; i++) {
if((irtn = desinit(&DesInst[i], DES_MODE_STD))) {
DESDebug("desinit returned %d\n", irtn);
CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR);
}
dessetkey(&DesInst[i], (char *)keyData + (8 * i));
}
}
setup(DES3_BLOCK_SIZE_BYTES, context);
}
void DES3Context::encryptBlock(
const void *plainText, size_t plainTextLen,
void *cipherText,
size_t &cipherTextLen, bool final) {
if(plainTextLen != DES3_BLOCK_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_INPUT_LENGTH_ERROR);
}
if(cipherTextLen < DES3_BLOCK_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
}
if(plainText != cipherText) {
memmove(cipherText, plainText, DES3_BLOCK_SIZE_BYTES);
}
endes(&DesInst[0], (char *)cipherText);
dedes(&DesInst[1], (char *)cipherText);
endes(&DesInst[2], (char *)cipherText);
cipherTextLen = DES3_BLOCK_SIZE_BYTES;
}
void DES3Context::decryptBlock(
const void *cipherText, void *plainText,
size_t &plainTextLen, bool final) {
if(plainTextLen < DES3_BLOCK_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
}
if(plainText != cipherText) {
memmove(plainText, cipherText, DES3_BLOCK_SIZE_BYTES);
}
dedes(&DesInst[2], (char *)plainText);
endes(&DesInst[1], (char *)plainText);
dedes(&DesInst[0], (char *)plainText);
plainTextLen = DES3_BLOCK_SIZE_BYTES;
}