19 #if defined(HAVE_AEAD)
25 #if defined(HAVE_AES_GCM)
28 #if defined(HAVE_CHACHA_POLY) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
32 const cx_aead_info_t *cx_aead_get_info(cx_aead_type_t type)
35 #if defined(HAVE_AES_GCM)
36 case CX_AEAD_AES128_GCM:
37 return &cx_aes128_gcm_info;
38 case CX_AEAD_AES192_GCM:
39 return &cx_aes192_gcm_info;
40 case CX_AEAD_AES256_GCM:
41 return &cx_aes256_gcm_info;
43 #if defined(HAVE_CHACHA_POLY) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
44 case CX_AEAD_CHACHA20_POLY1305:
45 return &cx_chacha20_poly1305_info;
52 cx_err_t cx_aead_init(cx_aead_context_t *ctx)
55 return CX_INVALID_PARAMETER;
57 memset(ctx, 0,
sizeof(cx_aead_context_t));
61 cx_err_t cx_aead_setup(cx_aead_context_t *ctx, cx_aead_type_t type)
63 const cx_aead_info_t *info = cx_aead_get_info(type);
65 return CX_INVALID_PARAMETER;
68 ctx->info->func->init(ctx->base_ctx);
72 cx_err_t cx_aead_set_key(cx_aead_context_t *ctx,
const uint8_t *key,
size_t key_len, uint32_t mode)
74 if (ctx->info->key_bitlen != key_len * 8) {
75 return CX_INVALID_PARAMETER_SIZE;
78 return CX_INVALID_PARAMETER_VALUE;
81 return ctx->info->func->set_key(ctx->base_ctx, key, key_len);
84 cx_err_t cx_aead_set_iv(cx_aead_context_t *ctx,
const uint8_t *iv,
size_t iv_len)
86 if ((NULL == iv) || (iv_len < 1)) {
87 return CX_INVALID_PARAMETER;
89 if (NULL == ctx->info) {
90 return CX_INVALID_PARAMETER;
92 return ctx->info->func->start(ctx->base_ctx, ctx->mode, iv, iv_len);
95 cx_err_t cx_aead_update_ad(cx_aead_context_t *ctx,
const uint8_t *ad,
size_t ad_len)
97 if (NULL == ctx->info) {
98 return CX_INVALID_PARAMETER;
100 return ctx->info->func->update_aad(ctx->base_ctx, ad, ad_len);
103 cx_err_t cx_aead_update(cx_aead_context_t *ctx,
109 if ((NULL == ctx->info) || (NULL == out_len)) {
110 return CX_INVALID_PARAMETER;
113 return ctx->info->func->update(ctx->base_ctx, in, out, in_len);
116 cx_err_t cx_aead_write_tag(cx_aead_context_t *ctx,
uint8_t *tag,
size_t tag_len)
118 if ((NULL == ctx->info) || (NULL == tag)) {
119 return CX_INVALID_PARAMETER;
121 return ctx->info->func->finish(ctx->base_ctx, tag, tag_len);
124 cx_err_t cx_aead_check_tag(cx_aead_context_t *ctx,
const uint8_t *tag,
size_t tag_len)
126 if ((NULL == ctx->info) || (ctx->mode !=
CX_DECRYPT)) {
127 return CX_INVALID_PARAMETER;
130 return ctx->info->func->check_tag(ctx->base_ctx, tag, tag_len);
133 cx_err_t cx_aead_encrypt(cx_aead_context_t *ctx,
145 if (NULL == ctx->info) {
146 return CX_INVALID_PARAMETER;
149 return ctx->info->func->encrypt_and_tag(
150 ctx->base_ctx, in, in_len, iv, iv_len, ad, ad_len, out, tag, tag_len);
153 cx_err_t cx_aead_decrypt(cx_aead_context_t *ctx,
165 if (NULL == ctx->info) {
166 return CX_INVALID_PARAMETER;
169 return ctx->info->func->auth_decrypt(
170 ctx->base_ctx, in, in_len, iv, iv_len, ad, ad_len, out, tag, tag_len);
Authenticated Encryption with Associated Data (AEAD)