xref: /freebsd/crypto/openssl/providers/implementations/ciphers/cipher_aes_ccm_hw.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*
2  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 /* AES CCM mode */
11 
12 /*
13  * This file uses the low level AES functions (which are deprecated for
14  * non-internal use) in order to implement provider AES ciphers.
15  */
16 #include "internal/deprecated.h"
17 
18 #include "cipher_aes_ccm.h"
19 
20 #define AES_HW_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec)  \
21     fn_set_enc_key(key, keylen * 8, &actx->ccm.ks.ks);                         \
22     CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ccm.ks.ks,        \
23                        (block128_f)fn_blk);                                    \
24     ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec;         \
25     ctx->key_set = 1;
26 
27 static int ccm_generic_aes_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
28                                    size_t keylen)
29 {
30     PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
31 
32 #ifdef HWAES_CAPABLE
33     if (HWAES_CAPABLE) {
34         AES_HW_CCM_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_encrypt, NULL, NULL);
35     } else
36 #endif /* HWAES_CAPABLE */
37 
38 #ifdef VPAES_CAPABLE
39     if (VPAES_CAPABLE) {
40         AES_HW_CCM_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_encrypt, NULL, NULL);
41     } else
42 #endif
43     {
44         AES_HW_CCM_SET_KEY_FN(AES_set_encrypt_key, AES_encrypt, NULL, NULL)
45     }
46     return 1;
47 }
48 
49 static const PROV_CCM_HW aes_ccm = {
50     ccm_generic_aes_initkey,
51     ossl_ccm_generic_setiv,
52     ossl_ccm_generic_setaad,
53     ossl_ccm_generic_auth_encrypt,
54     ossl_ccm_generic_auth_decrypt,
55     ossl_ccm_generic_gettag
56 };
57 
58 #if defined(S390X_aes_128_CAPABLE)
59 # include "cipher_aes_ccm_hw_s390x.inc"
60 #elif defined(AESNI_CAPABLE)
61 # include "cipher_aes_ccm_hw_aesni.inc"
62 #elif defined(SPARC_AES_CAPABLE)
63 # include "cipher_aes_ccm_hw_t4.inc"
64 #else
65 const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
66 {
67     return &aes_ccm;
68 }
69 #endif
70