#include "sslContext.h"
#include "cryptType.h"
#include "sslDebug.h"
#include "sslMemory.h"
#include <CommonCrypto/CommonCryptor.h>
#include "symCipher.h"
OSStatus CCSymmInit(
uint8_t *key,
uint8_t* iv,
CipherContext *cipherCtx,
SSLContext *ctx)
{
CCCryptorStatus ccrtn;
CCOperation op = cipherCtx->encrypting ? kCCEncrypt : kCCDecrypt;
if(cipherCtx->cryptorRef) {
CCCryptorRelease(cipherCtx->cryptorRef);
cipherCtx->cryptorRef = NULL;
}
ccrtn = CCCryptorCreate(op, cipherCtx->symCipher->keyAlg,
0,
key, cipherCtx->symCipher->keySize,
iv,
&cipherCtx->cryptorRef);
if(ccrtn) {
sslErrorLog("CCCryptorCreate returned %d\n", (int)ccrtn);
return internalComponentErr;
}
return noErr;
}
OSStatus CCSymmEncryptDecrypt(
const uint8_t *src,
uint8_t *dest,
size_t len,
CipherContext *cipherCtx,
SSLContext *ctx)
{
CCCryptorStatus ccrtn;
ASSERT(cipherCtx != NULL);
ASSERT(cipherCtx->cryptorRef != NULL);
if(cipherCtx->cryptorRef == NULL) {
sslErrorLog("CCSymmEncryptDecrypt: NULL cryptorRef\n");
return internalComponentErr;
}
size_t data_moved;
ccrtn = CCCryptorUpdate(cipherCtx->cryptorRef, src, len,
dest, len, &data_moved);
assert(data_moved == len);
#if SSL_DEBUG
if(ccrtn) {
sslErrorLog("CCSymmEncryptDecrypt: returned %d\n", (int)ccrtn);
return internalComponentErr;
}
#endif
return noErr;
}
OSStatus CCSymmFinish(
CipherContext *cipherCtx,
SSLContext *ctx)
{
if(cipherCtx->cryptorRef) {
CCCryptorRelease(cipherCtx->cryptorRef);
cipherCtx->cryptorRef = NULL;
}
return noErr;
}