void bcal_ctr_encNext(void* block, bcal_ctr_ctx_t* ctx){ uint8_t tmp[ctx->blocksize_B]; memcpy(tmp, ctx->in_block, ctx->blocksize_B); bcal_cipher_enc(tmp, &(ctx->cctx)); memxor(block, tmp, ctx->blocksize_B); ctx->inc_func(ctx->in_block, ctx->blocksize_B); }
void bcal_cfb_B_encNext(void* block, bcal_cfb_B_ctx_t* ctx){ uint8_t tmp[ctx->blocksize_B]; memcpy(tmp, ctx->in_block, ctx->blocksize_B); bcal_cipher_enc(tmp, &(ctx->cctx)); memxor(block, tmp, ctx->size_B); memmove(ctx->in_block, ctx->in_block+ctx->size_B, ctx->blocksize_B - ctx->size_B); memcpy(ctx->in_block+ctx->blocksize_B-ctx->size_B, block, ctx->size_B); }
void bcal_cmac_nextBlock (bcal_cmac_ctx_t* ctx, const void* block){ if(ctx->last_set){ memxor(ctx->accu, ctx->lastblock, ctx->blocksize_B); bcal_cipher_enc(ctx->accu, &(ctx->cctx)); } memcpy(ctx->lastblock, block, ctx->blocksize_B); ctx->last_set=1; }
void bcal_ofb_encMsg(const void* iv, void* msg, uint32_t msg_len_b, bcal_ofb_ctx_t* ctx){ uint16_t block_len_b; block_len_b = ctx->blocksize_B*8; bcal_ofb_loadIV(iv, ctx); while(msg_len_b>block_len_b){ bcal_ofb_encNext(msg, ctx); msg_len_b -= block_len_b; msg = (uint8_t*)msg + ctx->blocksize_B; } bcal_cipher_enc(ctx->in_block, &(ctx->cctx)); ctx->in_block[msg_len_b/8] = 0xff00>>(msg_len_b&7); memxor(msg, ctx->in_block, (msg_len_b+7)/8); }
uint8_t bcal_cmac_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b, bcal_cmac_ctx_t *ctx) { uint8_t r; ctx->desc = (bcdesc_t*) desc; ctx->blocksize_B = bcal_cipher_getBlocksize_b(desc) / 8; if (ctx->blocksize_B != 128 / 8 && ctx->blocksize_B != 64 / 8) { return 0x13; } ctx->accu = malloc(ctx->blocksize_B); if (ctx->accu == NULL) { return 0x14; } ctx->k1 = malloc(ctx->blocksize_B); if (ctx->k1 == NULL) { return 0x15; } ctx->k2 = malloc(ctx->blocksize_B); if (ctx->k2 == NULL) { return 0x16; } ctx->lastblock = malloc(ctx->blocksize_B); if (ctx->lastblock == NULL) { return 0x17; } r = bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx)); if (r) { return r; } if (ctx->blocksize_B == 128 / 8) { r = const_128; } else { r = const_64; } /* subkey computation */ memset(ctx->accu, 0x00, ctx->blocksize_B); memset(ctx->k1, 0x00, ctx->blocksize_B); bcal_cipher_enc(ctx->k1, &(ctx->cctx)); if (left_shift_be_block(ctx->k1, ctx->blocksize_B)) { ctx->k1[ctx->blocksize_B - 1] ^= r; } memcpy(ctx->k2, ctx->k1, ctx->blocksize_B); if (left_shift_be_block(ctx->k2, ctx->blocksize_B)) { ctx->k2[ctx->blocksize_B - 1] ^= r; } ctx->last_set = 0; return 0; }
void bcal_ctr_encMsg(const void* iv, void* msg, uint32_t msg_len_b, bcal_ctr_ctx_t* ctx){ bcal_ctr_loadIV(iv, ctx); uint16_t blocksize_b; blocksize_b = ctx->blocksize_B*8; while(msg_len_b>blocksize_b){ bcal_ctr_encNext(msg, ctx); msg_len_b -= blocksize_b; msg = (uint8_t*)msg + ctx->blocksize_B; } uint8_t tmp[ctx->blocksize_B]; memcpy(tmp, ctx->in_block, ctx->blocksize_B); bcal_cipher_enc(tmp, &(ctx->cctx)); ctx->inc_func(ctx->in_block, ctx->blocksize_B); tmp[msg_len_b/8] = 0xff00>>(msg_len_b&7); memxor(msg, tmp, (msg_len_b+7)/8); }
void testrun_test_aes(void){ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 }; aes128_ctx_t ctx; aes128_init(key, &ctx); cli_putstr("\r\n\r\n cipher test (FIPS 197):\r\n key: "); cli_hexdump(key, 16); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); aes128_enc(data, &ctx); cli_putstr("\r\n ciphertext: "); cli_hexdump(data, 16); aes128_dec(data, &ctx); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); cli_putstr("\r\n testing bcal:"); bcgen_ctx_t bcal_ctx; uint8_t r; r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx); cli_putstr("\r\n init = 0x"); cli_hexdump(&r, 1); bcal_cipher_enc(data, &bcal_ctx); cli_putstr("\r\n ciphertext: "); cli_hexdump(data, 16); bcal_cipher_dec(data, &bcal_ctx); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); bcal_cipher_free(&bcal_ctx); }
void bcal_cbc_encNext(void *block, bcal_cbc_ctx_t *ctx){ memxor(block, ctx->prev_block, ctx->blocksize_B); bcal_cipher_enc(block, &(ctx->cctx)); memcpy(ctx->prev_block, block, ctx->blocksize_B); }
void bcal_ofb_decNext(void *block, bcal_ofb_ctx_t *ctx) { bcal_cipher_enc(ctx->in_block, &(ctx->cctx)); memxor(block, ctx->in_block, ctx->blocksize_B); }