#include "desContext.h"
#include <security_utilities/debugging.h>
#include <security_utilities/globalizer.h>
#include <security_utilities/threading.h>
#define DESDebug(args...) secinfo("desContext", ## args)
DESContext::DESContext(AppleCSPSession &session) : BlockCryptor(session), DesInst(NULL)
{
}
DESContext::~DESContext()
{
if (DesInst != NULL) {
CCCryptorRelease(DesInst);
}
DesInst = NULL;
}
void DESContext::init(
const Context &context,
bool encrypting)
{
CSSM_SIZE keyLen;
uint8 *keyData = NULL;
symmetricKeyBits(context, session(), 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);
}
if (DesInst != NULL)
{
CCCryptorRelease(DesInst);
}
(void) CCCryptorCreateWithMode(0, kCCModeECB, kCCAlgorithmDES, ccDefaultPadding, NULL, keyData, kCCKeySizeDES, NULL, 0, 0, 0, &DesInst);
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);
}
(void) CCCryptorEncryptDataBlock(DesInst, NULL, plainText, DES_BLOCK_SIZE_BYTES, cipherText);
cipherTextLen = DES_BLOCK_SIZE_BYTES;
}
void DESContext::decryptBlock(
const void *cipherText, size_t cipherTextLen,
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);
}
(void) CCCryptorDecryptDataBlock(DesInst, NULL, cipherText, DES_BLOCK_SIZE_BYTES, plainText);
plainTextLen = DES_BLOCK_SIZE_BYTES;
}
DES3Context::DES3Context(AppleCSPSession &session) : BlockCryptor(session), DesInst(NULL)
{
}
DES3Context::~DES3Context()
{
if (DesInst != NULL) {
CCCryptorRelease(DesInst);
}
DesInst = NULL;
}
void DES3Context::init(
const Context &context,
bool encrypting)
{
CSSM_SIZE keyLen;
uint8 *keyData = NULL;
symmetricKeyBits(context, session(), 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);
}
if (DesInst != NULL) {
CCCryptorRelease(DesInst);
}
(void) CCCryptorCreateWithMode(0, kCCModeECB, kCCAlgorithm3DES, ccDefaultPadding, NULL, keyData, kCCKeySize3DES, NULL, 0, 0, 0, &DesInst);
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);
}
(void) CCCryptorEncryptDataBlock(DesInst, NULL, plainText, DES3_BLOCK_SIZE_BYTES, cipherText);
cipherTextLen = DES3_BLOCK_SIZE_BYTES;
}
void DES3Context::decryptBlock(
const void *cipherText, size_t cipherTextLen,
void *plainText,
size_t &plainTextLen, bool final) {
if(plainTextLen < DES3_BLOCK_SIZE_BYTES) {
CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
}
(void) CCCryptorDecryptDataBlock(DesInst, NULL, cipherText, DES3_BLOCK_SIZE_BYTES, plainText);
plainTextLen = DES3_BLOCK_SIZE_BYTES;
}