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 GCM.
12  */
13 
14 #include "cipher_sm4_gcm.h"
15 #include "crypto/sm4_platform.h"
16 
17 # define SM4_GCM_HW_SET_KEY_CTR_FN(ks, fn_set_enc_key, fn_block, fn_ctr)       \
18     fn_set_enc_key(key, ks);                                                   \
19     CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)fn_block);                   \
20     ctx->ctr = (ctr128_f)fn_ctr;                                               \
21     ctx->key_set = 1;
22 
sm4_gcm_initkey(PROV_GCM_CTX * ctx,const unsigned char * key,size_t keylen)23 static int sm4_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
24                            size_t keylen)
25 {
26     PROV_SM4_GCM_CTX *actx = (PROV_SM4_GCM_CTX *)ctx;
27     SM4_KEY *ks = &actx->ks.ks;
28 
29 # ifdef HWSM4_CAPABLE
30     if (HWSM4_CAPABLE) {
31 #  ifdef HWSM4_ctr32_encrypt_blocks
32         SM4_GCM_HW_SET_KEY_CTR_FN(ks, HWSM4_set_encrypt_key, HWSM4_encrypt,
33                                   HWSM4_ctr32_encrypt_blocks);
34 #  else /* HWSM4_ctr32_encrypt_blocks */
35         SM4_GCM_HW_SET_KEY_CTR_FN(ks, HWSM4_set_encrypt_key, HWSM4_encrypt, NULL);
36 #  endif
37     } else
38 # endif /* HWSM4_CAPABLE */
39 
40 #ifdef VPSM4_EX_CAPABLE
41     if (VPSM4_EX_CAPABLE) {
42         SM4_GCM_HW_SET_KEY_CTR_FN(ks, vpsm4_ex_set_encrypt_key, vpsm4_ex_encrypt,
43                                   vpsm4_ex_ctr32_encrypt_blocks);
44     } else
45 #endif /* VPSM4_EX_CAPABLE */
46 
47 # ifdef VPSM4_CAPABLE
48     if (VPSM4_CAPABLE) {
49         SM4_GCM_HW_SET_KEY_CTR_FN(ks, vpsm4_set_encrypt_key, vpsm4_encrypt,
50                                   vpsm4_ctr32_encrypt_blocks);
51     } else
52 # endif /* VPSM4_CAPABLE */
53     {
54         SM4_GCM_HW_SET_KEY_CTR_FN(ks, ossl_sm4_set_key, ossl_sm4_encrypt, NULL);
55     }
56 
57     return 1;
58 }
59 
hw_gcm_cipher_update(PROV_GCM_CTX * ctx,const unsigned char * in,size_t len,unsigned char * out)60 static int hw_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
61                                 size_t len, unsigned char *out)
62 {
63     if (ctx->enc) {
64         if (ctx->ctr != NULL) {
65             if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
66                 return 0;
67         } else {
68             if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
69                 return 0;
70         }
71     } else {
72         if (ctx->ctr != NULL) {
73             if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
74                 return 0;
75         } else {
76             if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
77                 return 0;
78         }
79     }
80     return 1;
81 }
82 
83 static const PROV_GCM_HW sm4_gcm = {
84     sm4_gcm_initkey,
85     ossl_gcm_setiv,
86     ossl_gcm_aad_update,
87     hw_gcm_cipher_update,
88     ossl_gcm_cipher_final,
89     ossl_gcm_one_shot
90 };
91 
92 #if defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
93 # include "cipher_sm4_gcm_hw_rv64i.inc"
94 #else
ossl_prov_sm4_hw_gcm(size_t keybits)95 const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits)
96 {
97     return &sm4_gcm;
98 }
99 #endif
100