2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "cipher_locl.h"
12 #define AES_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec) \
13 fn_set_enc_key(key, keylen * 8, &actx->ccm.ks.ks); \
14 CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ccm.ks.ks, \
15 (block128_f)fn_blk); \
16 ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec; \
19 static int ccm_generic_aes_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
22 PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
26 AES_CCM_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_encrypt, NULL, NULL);
28 #endif /* HWAES_CAPABLE */
31 AES_CCM_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_encrypt, NULL, NULL);
35 AES_CCM_SET_KEY_FN(AES_set_encrypt_key, AES_encrypt, NULL, NULL)
40 static int ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce,
41 size_t nlen, size_t mlen)
43 return CRYPTO_ccm128_setiv(&ctx->ccm_ctx, nonce, nlen, mlen) == 0;
46 static int ccm_generic_setaad(PROV_CCM_CTX *ctx, const unsigned char *aad,
49 CRYPTO_ccm128_aad(&ctx->ccm_ctx, aad, alen);
53 static int ccm_generic_gettag(PROV_CCM_CTX *ctx, unsigned char *tag,
56 return CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, tlen) > 0;
59 static int ccm_generic_auth_encrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
60 unsigned char *out, size_t len,
61 unsigned char *tag, size_t taglen)
66 rv = CRYPTO_ccm128_encrypt_ccm64(&ctx->ccm_ctx, in,
67 out, len, ctx->str) == 0;
69 rv = CRYPTO_ccm128_encrypt(&ctx->ccm_ctx, in, out, len) == 0;
71 if (rv == 1 && tag != NULL)
72 rv = (CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, taglen) > 0);
76 static int ccm_generic_auth_decrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
77 unsigned char *out, size_t len,
78 unsigned char *expected_tag,
84 rv = CRYPTO_ccm128_decrypt_ccm64(&ctx->ccm_ctx, in, out, len,
87 rv = CRYPTO_ccm128_decrypt(&ctx->ccm_ctx, in, out, len) == 0;
89 unsigned char tag[16];
91 if (!CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, taglen)
92 || CRYPTO_memcmp(tag, expected_tag, taglen) != 0)
96 OPENSSL_cleanse(out, len);
100 static const PROV_CCM_HW aes_ccm = {
101 ccm_generic_aes_initkey,
104 ccm_generic_auth_encrypt,
105 ccm_generic_auth_decrypt,
108 #if defined(S390X_aes_128_CAPABLE)
109 # include "cipher_aes_ccm_hw_s390x.inc"
110 #elif defined(AESNI_CAPABLE)
111 # include "cipher_aes_ccm_hw_aesni.inc"
112 #elif defined(SPARC_AES_CAPABLE)
113 # include "cipher_aes_ccm_hw_t4.inc"
115 const PROV_CCM_HW *PROV_AES_HW_ccm(size_t keybits)
121 #include "cipher_aria_ccm_hw.inc"