Lines Matching refs:ctx
17 static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
21 static int ccm_tls_init(PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen) in ccm_tls_init() argument
29 memcpy(ctx->buf, aad, alen); in ccm_tls_init()
30 ctx->tls_aad_len = alen; in ccm_tls_init()
32 len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1]; in ccm_tls_init()
39 if (!ctx->enc) { in ccm_tls_init()
40 if (len < ctx->m) in ccm_tls_init()
43 len -= ctx->m; in ccm_tls_init()
45 ctx->buf[alen - 2] = (unsigned char)(len >> 8); in ccm_tls_init()
46 ctx->buf[alen - 1] = (unsigned char)(len & 0xff); in ccm_tls_init()
49 return ctx->m; in ccm_tls_init()
52 static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed, in ccm_tls_iv_set_fixed() argument
59 memcpy(ctx->iv, fixed, flen); in ccm_tls_iv_set_fixed()
63 static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx) in ccm_get_ivlen() argument
65 return 15 - ctx->l; in ccm_get_ivlen()
70 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; in ossl_ccm_set_ctx_params() local
89 if (ctx->enc) { in ossl_ccm_set_ctx_params()
93 memcpy(ctx->buf, p->data, p->data_size); in ossl_ccm_set_ctx_params()
94 ctx->tag_set = 1; in ossl_ccm_set_ctx_params()
96 ctx->m = p->data_size; in ossl_ccm_set_ctx_params()
112 ctx->l = ivlen; in ossl_ccm_set_ctx_params()
121 sz = ccm_tls_init(ctx, p->data, p->data_size); in ossl_ccm_set_ctx_params()
126 ctx->tls_aad_pad_sz = sz; in ossl_ccm_set_ctx_params()
135 if (ccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) { in ossl_ccm_set_ctx_params()
146 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; in ossl_ccm_get_ctx_params() local
150 if (p != NULL && !OSSL_PARAM_set_size_t(p, ccm_get_ivlen(ctx))) { in ossl_ccm_get_ctx_params()
157 size_t m = ctx->m; in ossl_ccm_get_ctx_params()
167 if (ccm_get_ivlen(ctx) > p->data_size) { in ossl_ccm_get_ctx_params()
171 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size) in ossl_ccm_get_ctx_params()
172 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) { in ossl_ccm_get_ctx_params()
180 if (ccm_get_ivlen(ctx) > p->data_size) { in ossl_ccm_get_ctx_params()
184 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size) in ossl_ccm_get_ctx_params()
185 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) { in ossl_ccm_get_ctx_params()
192 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) { in ossl_ccm_get_ctx_params()
198 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) { in ossl_ccm_get_ctx_params()
205 if (!ctx->enc || !ctx->tag_set) { in ossl_ccm_get_ctx_params()
213 if (!ctx->hw->gettag(ctx, p->data, p->data_size)) in ossl_ccm_get_ctx_params()
215 ctx->tag_set = 0; in ossl_ccm_get_ctx_params()
216 ctx->iv_set = 0; in ossl_ccm_get_ctx_params()
217 ctx->len_set = 0; in ossl_ccm_get_ctx_params()
226 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; in ccm_init() local
231 ctx->enc = enc; in ccm_init()
234 if (ivlen != ccm_get_ivlen(ctx)) { in ccm_init()
238 memcpy(ctx->iv, iv, ivlen); in ccm_init()
239 ctx->iv_set = 1; in ccm_init()
242 if (keylen != ctx->keylen) { in ccm_init()
246 if (!ctx->hw->setkey(ctx, key, keylen)) in ccm_init()
249 return ossl_ccm_set_ctx_params(ctx, params); in ccm_init()
270 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; in ossl_ccm_stream_update() local
277 if (!ccm_cipher_internal(ctx, out, outl, in, inl)) { in ossl_ccm_stream_update()
287 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; in ossl_ccm_stream_final() local
293 i = ccm_cipher_internal(ctx, out, outl, NULL, 0); in ossl_ccm_stream_final()
304 PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx; in ossl_ccm_cipher() local
314 if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0) in ossl_ccm_cipher()
322 static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen) in ccm_set_iv() argument
324 const PROV_CCM_HW *hw = ctx->hw; in ccm_set_iv()
326 if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen)) in ccm_set_iv()
328 ctx->len_set = 1; in ccm_set_iv()
332 static int ccm_tls_cipher(PROV_CCM_CTX *ctx, in ccm_tls_cipher() argument
343 if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m) in ccm_tls_cipher()
347 if (ctx->enc) in ccm_tls_cipher()
348 memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN); in ccm_tls_cipher()
350 memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN); in ccm_tls_cipher()
352 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m; in ccm_tls_cipher()
353 if (!ccm_set_iv(ctx, len)) in ccm_tls_cipher()
357 if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len)) in ccm_tls_cipher()
363 if (ctx->enc) { in ccm_tls_cipher()
364 if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m)) in ccm_tls_cipher()
366 olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m; in ccm_tls_cipher()
368 if (!ctx->hw->auth_decrypt(ctx, in, out, len, in ccm_tls_cipher()
369 (unsigned char *)in + len, ctx->m)) in ccm_tls_cipher()
379 static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out, in ccm_cipher_internal() argument
385 const PROV_CCM_HW *hw = ctx->hw; in ccm_cipher_internal()
388 if (!ctx->key_set) in ccm_cipher_internal()
391 if (ctx->tls_aad_len != UNINITIALISED_SIZET) in ccm_cipher_internal()
392 return ccm_tls_cipher(ctx, out, padlen, in, len); in ccm_cipher_internal()
398 if (!ctx->iv_set) in ccm_cipher_internal()
403 if (!ccm_set_iv(ctx, len)) in ccm_cipher_internal()
407 if (!ctx->len_set && len) in ccm_cipher_internal()
409 if (!hw->setaad(ctx, in, len)) in ccm_cipher_internal()
414 if (!ctx->len_set && !ccm_set_iv(ctx, len)) in ccm_cipher_internal()
417 if (ctx->enc) { in ccm_cipher_internal()
418 if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0)) in ccm_cipher_internal()
420 ctx->tag_set = 1; in ccm_cipher_internal()
423 if (!ctx->tag_set) in ccm_cipher_internal()
426 if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m)) in ccm_cipher_internal()
429 ctx->iv_set = 0; in ccm_cipher_internal()
430 ctx->tag_set = 0; in ccm_cipher_internal()
431 ctx->len_set = 0; in ccm_cipher_internal()
442 void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw) in ossl_ccm_initctx() argument
444 ctx->keylen = keybits / 8; in ossl_ccm_initctx()
445 ctx->key_set = 0; in ossl_ccm_initctx()
446 ctx->iv_set = 0; in ossl_ccm_initctx()
447 ctx->tag_set = 0; in ossl_ccm_initctx()
448 ctx->len_set = 0; in ossl_ccm_initctx()
449 ctx->l = 8; in ossl_ccm_initctx()
450 ctx->m = 12; in ossl_ccm_initctx()
451 ctx->tls_aad_len = UNINITIALISED_SIZET; in ossl_ccm_initctx()
452 ctx->hw = hw; in ossl_ccm_initctx()