#include <tls_ciphersuites.h>
#include "sslBuildFlags.h"
#include "sslDebug.h"
#include "sslMemory.h"
#include "symCipher.h"
#include "tls_types_private.h"
#include <corecrypto/ccrc4.h>
#include <string.h>
#include <AssertMacros.h>
#include <stddef.h>
#if ENABLE_RC4
struct SymCipherContext {
const struct ccrc4_info *rc4;
ccrc4_ctx key[];
};
static int RC4Init(
const SSLSymmetricCipherParams *params,
int encrypting,
uint8_t *key,
uint8_t *iv,
struct ccrng_state *rng,
SymCipherContext *cipherCtx)
{
SymCipherContext ctx = *cipherCtx;
if (ctx) {
sslFree(ctx);
ctx = NULL;
}
const struct ccrc4_info *rc4 = ccrc4();
ctx = sslMalloc(offsetof(struct SymCipherContext, key) + rc4->size);
if (ctx==NULL) {
sslErrorLog("RC4Init: Can't allocate context\n");
return errSSLRecordInternal;
}
rc4->init(ctx->key, params->keySize, key);
ctx->rc4 = rc4;
*cipherCtx = ctx;
return 0;
}
static int RC4Crypt(
const uint8_t *src,
uint8_t *dest,
size_t len,
SymCipherContext cipherCtx)
{
cipherCtx->rc4->crypt(cipherCtx->key, len, src, dest);
return 0;
}
static int RC4Finish(
SymCipherContext cipherCtx)
{
if (cipherCtx) {
ccrc4_ctx_clear(cipherCtx->rc4->size, cipherCtx->key);
sslFree(cipherCtx);
}
return 0;
}
const SSLSymmetricCipher SSLCipherRC4_128 = {
.params = &SSLCipherRC4_128Params,
.c.cipher = {
.initialize = RC4Init,
.encrypt = RC4Crypt,
.decrypt = RC4Crypt
},
.finish = RC4Finish
};
#endif