uint8_t bcal_eax_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b, bcal_eax_ctx_t *ctx){ uint8_t r; ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8; ctx->nonce = malloc(ctx->blocksize_B); if(ctx->nonce==NULL){ return 0x81; } r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ctag)); if(r){ return r; } r = bcal_cmac_init(desc, key, keysize_b, &(ctx->htag)); if(r){ return (r|0x10); } r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ntag)); if(r){ return (r|0x20); } r = bcal_ctr_init(desc, key, keysize_b, NULL, &(ctx->cipher)); if(r){ return (r|0x30); } ctx->header_set=0; uint8_t tmp[ctx->blocksize_B]; memset(tmp, 0, ctx->blocksize_B); bcal_cmac_nextBlock(&(ctx->ntag), tmp); tmp[ctx->blocksize_B-1]=1; bcal_cmac_nextBlock(&(ctx->htag), tmp); tmp[ctx->blocksize_B-1]=2; bcal_cmac_nextBlock(&(ctx->ctag), tmp); return 0; }
uint8_t bcal_ofb_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_ofb_ctx_t* ctx){ ctx->desc = (bcdesc_t*)desc; ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8; ctx->in_block=malloc(ctx->blocksize_B); if(ctx->in_block==NULL){ return 0x11; } return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx)); }
uint8_t bcal_ctr_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, inc_fp_t inc_func, bcal_ctr_ctx_t* ctx){ ctx->desc = (bcdesc_t*)desc; if(inc_func){ ctx->inc_func = inc_func; }else{ ctx->inc_func = increment_be; } ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8; ctx->in_block=malloc(ctx->blocksize_B); if(ctx->in_block==NULL){ return 0x11; } return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx)); }
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; }