/src/openssl30/crypto/evp/e_sm4.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright 2017 Ribose Inc. All Rights Reserved. |
4 | | * Ported from Ribose contributions from Botan. |
5 | | * |
6 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
7 | | * this file except in compliance with the License. You can obtain a copy |
8 | | * in the file LICENSE in the source distribution or at |
9 | | * https://www.openssl.org/source/license.html |
10 | | */ |
11 | | |
12 | | #include "internal/deprecated.h" |
13 | | |
14 | | #include "internal/cryptlib.h" |
15 | | #ifndef OPENSSL_NO_SM4 |
16 | | # include <openssl/evp.h> |
17 | | # include <openssl/modes.h> |
18 | | # include "crypto/sm4.h" |
19 | | # include "crypto/evp.h" |
20 | | # include "evp_local.h" |
21 | | |
22 | | typedef struct { |
23 | | SM4_KEY ks; |
24 | | } EVP_SM4_KEY; |
25 | | |
26 | | static int sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
27 | | const unsigned char *iv, int enc) |
28 | 0 | { |
29 | 0 | ossl_sm4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx)); |
30 | 0 | return 1; |
31 | 0 | } |
32 | | |
33 | | static void sm4_cbc_encrypt(const unsigned char *in, unsigned char *out, |
34 | | size_t len, const SM4_KEY *key, |
35 | | unsigned char *ivec, const int enc) |
36 | 0 | { |
37 | 0 | if (enc) |
38 | 0 | CRYPTO_cbc128_encrypt(in, out, len, key, ivec, |
39 | 0 | (block128_f)ossl_sm4_encrypt); |
40 | 0 | else |
41 | 0 | CRYPTO_cbc128_decrypt(in, out, len, key, ivec, |
42 | 0 | (block128_f)ossl_sm4_decrypt); |
43 | 0 | } |
44 | | |
45 | | static void sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out, |
46 | | size_t length, const SM4_KEY *key, |
47 | | unsigned char *ivec, int *num, const int enc) |
48 | 0 | { |
49 | 0 | CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, |
50 | 0 | (block128_f)ossl_sm4_encrypt); |
51 | 0 | } |
52 | | |
53 | | static void sm4_ecb_encrypt(const unsigned char *in, unsigned char *out, |
54 | | const SM4_KEY *key, const int enc) |
55 | 0 | { |
56 | 0 | if (enc) |
57 | 0 | ossl_sm4_encrypt(in, out, key); |
58 | 0 | else |
59 | 0 | ossl_sm4_decrypt(in, out, key); |
60 | 0 | } |
61 | | |
62 | | static void sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out, |
63 | | size_t length, const SM4_KEY *key, |
64 | | unsigned char *ivec, int *num) |
65 | 0 | { |
66 | 0 | CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num, |
67 | 0 | (block128_f)ossl_sm4_encrypt); |
68 | 0 | } |
69 | | |
70 | | IMPLEMENT_BLOCK_CIPHER(sm4, ks, sm4, EVP_SM4_KEY, NID_sm4, |
71 | | 16, 16, 16, 128, EVP_CIPH_FLAG_DEFAULT_ASN1, |
72 | | sm4_init_key, 0, 0, 0, 0) |
73 | | |
74 | | static int sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
75 | | const unsigned char *in, size_t len) |
76 | 0 | { |
77 | 0 | int n = EVP_CIPHER_CTX_get_num(ctx); |
78 | 0 | unsigned int num; |
79 | 0 | EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY, ctx); |
80 | |
|
81 | 0 | if (n < 0) |
82 | 0 | return 0; |
83 | 0 | num = (unsigned int)n; |
84 | |
|
85 | 0 | CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, |
86 | 0 | EVP_CIPHER_CTX_buf_noconst(ctx), &num, |
87 | 0 | (block128_f)ossl_sm4_encrypt); |
88 | 0 | EVP_CIPHER_CTX_set_num(ctx, num); |
89 | 0 | return 1; |
90 | 0 | } |
91 | | |
92 | | static const EVP_CIPHER sm4_ctr_mode = { |
93 | | NID_sm4_ctr, 1, 16, 16, |
94 | | EVP_CIPH_CTR_MODE, |
95 | | EVP_ORIG_GLOBAL, |
96 | | sm4_init_key, |
97 | | sm4_ctr_cipher, |
98 | | NULL, |
99 | | sizeof(EVP_SM4_KEY), |
100 | | NULL, NULL, NULL, NULL |
101 | | }; |
102 | | |
103 | | const EVP_CIPHER *EVP_sm4_ctr(void) |
104 | 71 | { |
105 | 71 | return &sm4_ctr_mode; |
106 | 71 | } |
107 | | |
108 | | #endif |