1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
10*b077aed3SPierre Pronchery #include "prov/ciphercommon.h"
11*b077aed3SPierre Pronchery #include "prov/ciphercommon_ccm.h"
12*b077aed3SPierre Pronchery
ossl_ccm_generic_setiv(PROV_CCM_CTX * ctx,const unsigned char * nonce,size_t nlen,size_t mlen)13*b077aed3SPierre Pronchery int ossl_ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce,
14*b077aed3SPierre Pronchery size_t nlen, size_t mlen)
15*b077aed3SPierre Pronchery {
16*b077aed3SPierre Pronchery return CRYPTO_ccm128_setiv(&ctx->ccm_ctx, nonce, nlen, mlen) == 0;
17*b077aed3SPierre Pronchery }
18*b077aed3SPierre Pronchery
ossl_ccm_generic_setaad(PROV_CCM_CTX * ctx,const unsigned char * aad,size_t alen)19*b077aed3SPierre Pronchery int ossl_ccm_generic_setaad(PROV_CCM_CTX *ctx, const unsigned char *aad,
20*b077aed3SPierre Pronchery size_t alen)
21*b077aed3SPierre Pronchery {
22*b077aed3SPierre Pronchery CRYPTO_ccm128_aad(&ctx->ccm_ctx, aad, alen);
23*b077aed3SPierre Pronchery return 1;
24*b077aed3SPierre Pronchery }
25*b077aed3SPierre Pronchery
ossl_ccm_generic_gettag(PROV_CCM_CTX * ctx,unsigned char * tag,size_t tlen)26*b077aed3SPierre Pronchery int ossl_ccm_generic_gettag(PROV_CCM_CTX *ctx, unsigned char *tag, size_t tlen)
27*b077aed3SPierre Pronchery {
28*b077aed3SPierre Pronchery return CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, tlen) > 0;
29*b077aed3SPierre Pronchery }
30*b077aed3SPierre Pronchery
ossl_ccm_generic_auth_encrypt(PROV_CCM_CTX * ctx,const unsigned char * in,unsigned char * out,size_t len,unsigned char * tag,size_t taglen)31*b077aed3SPierre Pronchery int ossl_ccm_generic_auth_encrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
32*b077aed3SPierre Pronchery unsigned char *out, size_t len,
33*b077aed3SPierre Pronchery unsigned char *tag, size_t taglen)
34*b077aed3SPierre Pronchery {
35*b077aed3SPierre Pronchery int rv;
36*b077aed3SPierre Pronchery
37*b077aed3SPierre Pronchery if (ctx->str != NULL)
38*b077aed3SPierre Pronchery rv = CRYPTO_ccm128_encrypt_ccm64(&ctx->ccm_ctx, in,
39*b077aed3SPierre Pronchery out, len, ctx->str) == 0;
40*b077aed3SPierre Pronchery else
41*b077aed3SPierre Pronchery rv = CRYPTO_ccm128_encrypt(&ctx->ccm_ctx, in, out, len) == 0;
42*b077aed3SPierre Pronchery
43*b077aed3SPierre Pronchery if (rv == 1 && tag != NULL)
44*b077aed3SPierre Pronchery rv = (CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, taglen) > 0);
45*b077aed3SPierre Pronchery return rv;
46*b077aed3SPierre Pronchery }
47*b077aed3SPierre Pronchery
ossl_ccm_generic_auth_decrypt(PROV_CCM_CTX * ctx,const unsigned char * in,unsigned char * out,size_t len,unsigned char * expected_tag,size_t taglen)48*b077aed3SPierre Pronchery int ossl_ccm_generic_auth_decrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
49*b077aed3SPierre Pronchery unsigned char *out, size_t len,
50*b077aed3SPierre Pronchery unsigned char *expected_tag, size_t taglen)
51*b077aed3SPierre Pronchery {
52*b077aed3SPierre Pronchery int rv = 0;
53*b077aed3SPierre Pronchery
54*b077aed3SPierre Pronchery if (ctx->str != NULL)
55*b077aed3SPierre Pronchery rv = CRYPTO_ccm128_decrypt_ccm64(&ctx->ccm_ctx, in, out, len,
56*b077aed3SPierre Pronchery ctx->str) == 0;
57*b077aed3SPierre Pronchery else
58*b077aed3SPierre Pronchery rv = CRYPTO_ccm128_decrypt(&ctx->ccm_ctx, in, out, len) == 0;
59*b077aed3SPierre Pronchery if (rv) {
60*b077aed3SPierre Pronchery unsigned char tag[16];
61*b077aed3SPierre Pronchery
62*b077aed3SPierre Pronchery if (!CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, taglen)
63*b077aed3SPierre Pronchery || CRYPTO_memcmp(tag, expected_tag, taglen) != 0)
64*b077aed3SPierre Pronchery rv = 0;
65*b077aed3SPierre Pronchery }
66*b077aed3SPierre Pronchery if (rv == 0)
67*b077aed3SPierre Pronchery OPENSSL_cleanse(out, len);
68*b077aed3SPierre Pronchery return rv;
69*b077aed3SPierre Pronchery }
70