]>
Commit | Line | Data |
---|---|---|
c2ee608a TZ |
1 | /* |
2 | * Copyright 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 | /*- | |
11 | * Generic support for SM4 GCM. | |
12 | */ | |
13 | ||
14 | #include "cipher_sm4_gcm.h" | |
15b7175f | 15 | #include "crypto/sm4_platform.h" |
c2ee608a TZ |
16 | |
17 | static int sm4_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key, | |
18 | size_t keylen) | |
19 | { | |
20 | PROV_SM4_GCM_CTX *actx = (PROV_SM4_GCM_CTX *)ctx; | |
21 | SM4_KEY *ks = &actx->ks.ks; | |
22 | ||
23 | ctx->ks = ks; | |
15b7175f DH |
24 | # ifdef HWSM4_CAPABLE |
25 | if (HWSM4_CAPABLE) { | |
26 | HWSM4_set_encrypt_key(key, ks); | |
27 | CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f) HWSM4_encrypt); | |
28 | # ifdef HWSM4_ctr32_encrypt_blocks | |
29 | ctx->ctr = (ctr128_f) HWSM4_ctr32_encrypt_blocks; | |
30 | # else /* HWSM4_ctr32_encrypt_blocks */ | |
31 | ctx->ctr = (ctr128_f)NULL; | |
32 | # endif | |
33 | } else | |
34 | # endif /* HWSM4_CAPABLE */ | |
35 | { | |
36 | ossl_sm4_set_key(key, ks); | |
37 | CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)ossl_sm4_encrypt); | |
38 | ctx->ctr = (ctr128_f)NULL; | |
39 | } | |
c2ee608a TZ |
40 | ctx->key_set = 1; |
41 | ||
42 | return 1; | |
43 | } | |
44 | ||
26efd0b3 DH |
45 | static int hw_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in, |
46 | size_t len, unsigned char *out) | |
47 | { | |
48 | if (ctx->enc) { | |
49 | if (ctx->ctr != NULL) { | |
50 | if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr)) | |
51 | return 0; | |
52 | } else { | |
53 | if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len)) | |
54 | return 0; | |
55 | } | |
56 | } else { | |
57 | if (ctx->ctr != NULL) { | |
58 | if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr)) | |
59 | return 0; | |
60 | } else { | |
61 | if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len)) | |
62 | return 0; | |
63 | } | |
64 | } | |
65 | return 1; | |
66 | } | |
67 | ||
c2ee608a TZ |
68 | static const PROV_GCM_HW sm4_gcm = { |
69 | sm4_gcm_initkey, | |
70 | ossl_gcm_setiv, | |
71 | ossl_gcm_aad_update, | |
26efd0b3 | 72 | hw_gcm_cipher_update, |
c2ee608a TZ |
73 | ossl_gcm_cipher_final, |
74 | ossl_gcm_one_shot | |
75 | }; | |
76 | ||
77 | const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits) | |
78 | { | |
79 | return &sm4_gcm; | |
80 | } |