static int camellia_set_key(struct crypto_tfm *tfm, const u8 *in_key, unsigned int key_len) { struct camellia_ctx *cctx = crypto_tfm_ctx(tfm); const unsigned char *key = (const unsigned char *)in_key; u32 *flags = &tfm->crt_flags; if (key_len != 16 && key_len != 24 && key_len != 32) { *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; } cctx->key_length = key_len; switch (key_len) { case 16: camellia_setup128(key, cctx->key_table); break; case 24: camellia_setup192(key, cctx->key_table); break; case 32: camellia_setup256(key, cctx->key_table); break; } return 0; }
int Camellia_set_key(const unsigned char *userKey, const int bits, CAMELLIA_KEY *key) { if (!userKey || !key) { return -1; } switch(bits) { case 128: camellia_setup128(userKey, (unsigned int *)key->rd_key); key->enc = camellia_encrypt128; key->dec = camellia_decrypt128; break; case 192: camellia_setup192(userKey, (unsigned int *)key->rd_key); key->enc = camellia_encrypt256; key->dec = camellia_decrypt256; break; case 256: camellia_setup256(userKey, (unsigned int *)key->rd_key); key->enc = camellia_encrypt256; key->dec = camellia_decrypt256; break; default: return -2; } key->bitLength = bits; return 0; }
static void camellia_setup192(const unsigned char *key, u32 *subkey) { unsigned char kk[32]; u32 krll, krlr, krrl, krrr; memcpy(kk, key, 24); memcpy((unsigned char *)&krll, key+16, 4); memcpy((unsigned char *)&krlr, key+20, 4); krrl = ~krll; krrr = ~krlr; memcpy(kk+24, (unsigned char *)&krrl, 4); memcpy(kk+28, (unsigned char *)&krrr, 4); camellia_setup256(kk, subkey); }
void Camellia_Ekeygen(const int keyBitLength, const unsigned char *rawKey, uint32_t *subkey) { KASSERT(keyBitLength == 128 || keyBitLength == 192 || keyBitLength == 256, ("Invalid key size (%d).", keyBitLength)); switch(keyBitLength) { case 128: camellia_setup128(rawKey, subkey); break; case 192: camellia_setup192(rawKey, subkey); break; case 256: camellia_setup256(rawKey, subkey); break; default: break; } }