9 #define CMAC_CONSTANT_R_64 (0x1B)
10 #define CMAC_CONSTANT_R_128 (0x87)
16 if ((NULL == ctx) || (NULL == key)) {
17 return CX_INVALID_PARAMETER;
22 ctx->cmac_ctx = &
G_cx.cmac;
23 memset(ctx->cmac_ctx, 0,
sizeof(cx_cmac_context_t));
36 if ((NULL == ctx) || (NULL == ctx->
cipher_info) || (NULL == input) || (NULL == ctx->cmac_ctx)) {
37 return CX_INVALID_PARAMETER;
43 memcpy(ctx->cmac_ctx->unprocessed_block + *current_len,
60 for (i = 1; i < n; i++) {
72 memcpy(ctx->cmac_ctx->unprocessed_block + *current_len, input, in_len);
73 *current_len += in_len;
81 cx_err_t cx_cmac_shift_and_xor(
uint8_t *output,
uint8_t *input,
size_t block_size)
90 case CX_AES_BLOCK_SIZE:
91 constant = CMAC_CONSTANT_R_128;
95 return CX_INVALID_PARAMETER_VALUE;
98 for (i = 0; i < block_size; i++) {
99 output[block_size - 1 - i] = input[block_size - 1 - i] << 1 | msb;
100 msb = input[block_size - 1 - i] >> 7;
103 mask = -(input[0] >> 7);
104 output[block_size - 1] ^= constant & mask;
117 memset(
L, 0, CMAC_MAX_BLOCK_LENGTH);
123 memset(
L, 0, CMAC_MAX_BLOCK_LENGTH);
129 uint8_t sub_key1[CMAC_MAX_BLOCK_LENGTH];
130 uint8_t sub_key2[CMAC_MAX_BLOCK_LENGTH];
131 uint8_t last_block[CMAC_MAX_BLOCK_LENGTH];
135 if ((NULL == ctx) || (NULL == ctx->
cipher_info) || (NULL == ctx->cmac_ctx)
136 || (NULL == output)) {
137 return CX_INVALID_PARAMETER;
140 CX_CHECK(cx_cmac_generate_subkeys(ctx, sub_key1, sub_key2));
143 memcpy(last_block, ctx->cmac_ctx->unprocessed_block, ctx->cmac_ctx->
unprocessed_len);
160 memset(sub_key1, 0, CMAC_MAX_BLOCK_LENGTH);
161 memset(sub_key2, 0, CMAC_MAX_BLOCK_LENGTH);
162 explicit_bzero(ctx->cmac_ctx,
sizeof(cx_cmac_context_t));
178 if ((NULL == key) || (NULL == input) || (NULL == output)) {
179 return CX_INVALID_PARAMETER;
184 CX_CHECK(cx_cmac_start(&ctx, key, key_bitlen));
185 CX_CHECK(cx_cmac_update(&ctx, input, in_len));
186 CX_CHECK(cx_cmac_finish(&ctx, output));
void cx_memxor(uint8_t *buf1, const uint8_t *buf2, size_t len)
void add_one_and_zeros_padding(uint8_t *output, size_t out_len, size_t data_len)
WARN_UNUSED_RESULT cx_err_t cx_cipher_update(cx_cipher_context_t *ctx, const uint8_t *input, size_t in_len, uint8_t *output, size_t *out_len)
Encrypt or decrypt with the given context.
WARN_UNUSED_RESULT cx_err_t cx_cipher_setkey(cx_cipher_context_t *ctx, const uint8_t *key, uint32_t key_bitlen, uint32_t operation)
Set the key to use.
WARN_UNUSED_RESULT cx_err_t cx_cipher_init(cx_cipher_context_t *ctx)
Initialize a cipher context as NONE.
WARN_UNUSED_RESULT cx_err_t cx_cipher_setup(cx_cipher_context_t *ctx, const cx_cipher_id_t type, uint32_t mode)
Initialize and fill the context structure given the cipher info.
cx_err_t(* ctx_reset)(void)
Reset.
const cipher_key_t * cipher_key
Cipher-specific context.
const cx_cipher_info_t * cipher_info
Cipher information.
size_t unprocessed_len
Length of data to process.
const cx_cipher_base_t * base
uint32_t block_size
Block size.