void reg_proto_encrypt_data(BufferObj *plainText, BufferObj *encrKey, BufferObj *authKey, BufferObj *cipherText, BufferObj *iv) { BufferObj *buf = buffobj_new(); uint8 ivBuf[SIZE_128_BITS]; /* 10 rounds for cbc 128 = (10+1) * 4 uint32 */ uint32 rk[44]; uint8 outBuf[1024]; int encrypt_len; if (!buf) return; if (plainText->m_dataLength == 0) { TUTRACE((TUTRACE_ERR, "Invalid parameters \n")); buffobj_del(buf); return; } /* Generate a random iv */ RAND_bytes(ivBuf, SIZE_128_BITS); buffobj_Reset(iv); buffobj_Append(iv, SIZE_128_BITS, ivBuf); /* Now encrypt the plaintext and mac using the encryption key and IV. */ buffobj_Append(buf, plainText->m_dataLength, plainText->pBase); TUTRACE((TUTRACE_ERR, "RPROTO: calling encryption of %d bytes\n", buf->m_dataLength)); rijndaelKeySetupEnc(rk, encrKey->pBase, 128); encrypt_len = aes_cbc_encrypt_pad(rk, 16, ivBuf, buf->m_dataLength, plainText->pBase, outBuf, PAD_LEN_PADDING); buffobj_Append(cipherText, encrypt_len, outBuf); buffobj_del(buf); }
int BCMROMFN(aes_cbc_encrypt)(uint32 *rk, const size_t key_len, const uint8 *nonce, const size_t data_len, const uint8 *ptxt, uint8 *ctxt) { if (data_len % AES_BLOCK_SZ) return (-1); if (data_len < AES_BLOCK_SZ) return (-1); return aes_cbc_encrypt_pad(rk, key_len, nonce, data_len, ptxt, ctxt, NO_PADDING); }