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