Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/providers/implementations/ciphers/cipher_sm4_hw.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2022 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
#include "cipher_sm4.h"
11
12
static int cipher_hw_sm4_initkey(PROV_CIPHER_CTX *ctx,
13
                                 const unsigned char *key, size_t keylen)
14
2
{
15
2
    PROV_SM4_CTX *sctx =  (PROV_SM4_CTX *)ctx;
16
2
    SM4_KEY *ks = &sctx->ks.ks;
17
18
2
    ctx->ks = ks;
19
2
    if (ctx->enc
20
2
            || (ctx->mode != EVP_CIPH_ECB_MODE
21
2
                && ctx->mode != EVP_CIPH_CBC_MODE)) {
22
#ifdef HWSM4_CAPABLE
23
        if (HWSM4_CAPABLE) {
24
            HWSM4_set_encrypt_key(key, ks);
25
            ctx->block = (block128_f)HWSM4_encrypt;
26
            ctx->stream.cbc = NULL;
27
#ifdef HWSM4_cbc_encrypt
28
            if (ctx->mode == EVP_CIPH_CBC_MODE)
29
                ctx->stream.cbc = (cbc128_f)HWSM4_cbc_encrypt;
30
            else
31
#endif
32
#ifdef HWSM4_ecb_encrypt
33
            if (ctx->mode == EVP_CIPH_ECB_MODE)
34
                ctx->stream.ecb = (ecb128_f)HWSM4_ecb_encrypt;
35
            else
36
#endif
37
#ifdef HWSM4_ctr32_encrypt_blocks
38
            if (ctx->mode == EVP_CIPH_CTR_MODE)
39
                ctx->stream.ctr = (ctr128_f)HWSM4_ctr32_encrypt_blocks;
40
            else
41
#endif
42
            (void)0;            /* terminate potentially open 'else' */
43
        } else
44
#endif
45
#ifdef VPSM4_CAPABLE
46
        if (VPSM4_CAPABLE) {
47
            vpsm4_set_encrypt_key(key, ks);
48
            ctx->block = (block128_f)vpsm4_encrypt;
49
            ctx->stream.cbc = NULL;
50
            if (ctx->mode == EVP_CIPH_CBC_MODE)
51
                ctx->stream.cbc = (cbc128_f)vpsm4_cbc_encrypt;
52
            else if (ctx->mode == EVP_CIPH_ECB_MODE)
53
                ctx->stream.ecb = (ecb128_f)vpsm4_ecb_encrypt;
54
            else if (ctx->mode == EVP_CIPH_CTR_MODE)
55
                ctx->stream.ctr = (ctr128_f)vpsm4_ctr32_encrypt_blocks;
56
        } else
57
#endif
58
2
        {
59
2
            ossl_sm4_set_key(key, ks);
60
2
            ctx->block = (block128_f)ossl_sm4_encrypt;
61
2
        }
62
2
    } else {
63
#ifdef HWSM4_CAPABLE
64
        if (HWSM4_CAPABLE) {
65
            HWSM4_set_decrypt_key(key, ks);
66
            ctx->block = (block128_f)HWSM4_decrypt;
67
            ctx->stream.cbc = NULL;
68
#ifdef HWSM4_cbc_encrypt
69
            if (ctx->mode == EVP_CIPH_CBC_MODE)
70
                ctx->stream.cbc = (cbc128_f)HWSM4_cbc_encrypt;
71
#endif
72
#ifdef HWSM4_ecb_encrypt
73
            if (ctx->mode == EVP_CIPH_ECB_MODE)
74
                ctx->stream.ecb = (ecb128_f)HWSM4_ecb_encrypt;
75
#endif
76
        } else
77
#endif
78
#ifdef VPSM4_CAPABLE
79
        if (VPSM4_CAPABLE) {
80
            vpsm4_set_decrypt_key(key, ks);
81
            ctx->block = (block128_f)vpsm4_decrypt;
82
            ctx->stream.cbc = NULL;
83
            if (ctx->mode == EVP_CIPH_CBC_MODE)
84
                ctx->stream.cbc = (cbc128_f)vpsm4_cbc_encrypt;
85
        else if (ctx->mode == EVP_CIPH_ECB_MODE)
86
                ctx->stream.ecb = (ecb128_f)vpsm4_ecb_encrypt;
87
        } else
88
#endif
89
0
        {
90
0
            ossl_sm4_set_key(key, ks);
91
0
            ctx->block = (block128_f)ossl_sm4_decrypt;
92
0
        }
93
0
    }
94
95
2
    return 1;
96
2
}
97
98
IMPLEMENT_CIPHER_HW_COPYCTX(cipher_hw_sm4_copyctx, PROV_SM4_CTX)
99
100
# define PROV_CIPHER_HW_sm4_mode(mode)                                         \
101
static const PROV_CIPHER_HW sm4_##mode = {                                     \
102
    cipher_hw_sm4_initkey,                                                     \
103
    ossl_cipher_hw_generic_##mode,                                             \
104
    cipher_hw_sm4_copyctx                                                      \
105
};                                                                             \
106
0
const PROV_CIPHER_HW *ossl_prov_cipher_hw_sm4_##mode(size_t keybits)           \
107
0
{                                                                              \
108
0
    return &sm4_##mode;                                                        \
109
0
}
Unexecuted instantiation: ossl_prov_cipher_hw_sm4_ecb
Unexecuted instantiation: ossl_prov_cipher_hw_sm4_ofb128
Unexecuted instantiation: ossl_prov_cipher_hw_sm4_cfb128
Unexecuted instantiation: ossl_prov_cipher_hw_sm4_ctr
110
111
PROV_CIPHER_HW_sm4_mode(cbc)
112
PROV_CIPHER_HW_sm4_mode(ecb)
113
PROV_CIPHER_HW_sm4_mode(ofb128)
114
PROV_CIPHER_HW_sm4_mode(cfb128)
115
PROV_CIPHER_HW_sm4_mode(ctr)