1 /* 2 * Copyright 2021-2024 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 /*- 11 * Generic support for SM4 CCM. 12 */ 13 14 #include "cipher_sm4_ccm.h" 15 #include "crypto/sm4_platform.h" 16 17 #define SM4_HW_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec) \ 18 fn_set_enc_key(key, &actx->ks.ks); \ 19 CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ks.ks, \ 20 (block128_f)fn_blk); \ 21 ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec; \ 22 ctx->key_set = 1; 23 24 static int ccm_sm4_initkey(PROV_CCM_CTX *ctx, 25 const unsigned char *key, size_t keylen) 26 { 27 PROV_SM4_CCM_CTX *actx = (PROV_SM4_CCM_CTX *)ctx; 28 29 #ifdef HWSM4_CAPABLE 30 if (HWSM4_CAPABLE) { 31 SM4_HW_CCM_SET_KEY_FN(HWSM4_set_encrypt_key, HWSM4_encrypt, NULL, NULL); 32 } else 33 #endif /* HWSM4_CAPABLE */ 34 35 #ifdef VPSM4_EX_CAPABLE 36 if (VPSM4_EX_CAPABLE) { 37 SM4_HW_CCM_SET_KEY_FN(vpsm4_ex_set_encrypt_key, vpsm4_ex_encrypt, NULL, 38 NULL); 39 } else 40 #endif /* VPSM4_EX_CAPABLE */ 41 42 #ifdef VPSM4_CAPABLE 43 if (VPSM4_CAPABLE) { 44 SM4_HW_CCM_SET_KEY_FN(vpsm4_set_encrypt_key, vpsm4_encrypt, NULL, NULL); 45 } else 46 #endif /* VPSM4_CAPABLE */ 47 { 48 SM4_HW_CCM_SET_KEY_FN(ossl_sm4_set_key, ossl_sm4_encrypt, NULL, NULL); 49 } 50 return 1; 51 } 52 53 static const PROV_CCM_HW ccm_sm4 = { 54 ccm_sm4_initkey, 55 ossl_ccm_generic_setiv, 56 ossl_ccm_generic_setaad, 57 ossl_ccm_generic_auth_encrypt, 58 ossl_ccm_generic_auth_decrypt, 59 ossl_ccm_generic_gettag 60 }; 61 62 #if defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64 63 # include "cipher_sm4_ccm_hw_rv64i.inc" 64 #else 65 const PROV_CCM_HW *ossl_prov_sm4_hw_ccm(size_t keybits) 66 { 67 return &ccm_sm4; 68 } 69 #endif 70