summaryrefslogtreecommitdiff
path: root/lib/libcrypto/evp/e_sm4.c
blob: 0d37448109cab701d619b30dc67bdbb410246581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*	$OpenBSD: e_sm4.c,v 1.7 2022/11/26 16:08:52 tb Exp $	*/
/*
 * Copyright (c) 2017, 2019 Ribose Inc
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <openssl/opensslconf.h>

#ifndef OPENSSL_NO_SM4
#include <openssl/evp.h>
#include <openssl/modes.h>
#include <openssl/sm4.h>

#include "evp_local.h"

typedef struct {
	SM4_KEY ks;
} EVP_SM4_KEY;

static int
sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    const unsigned char *iv, int enc)
{
	SM4_set_key(key, ctx->cipher_data);
	return 1;
}

static void
sm4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len,
    const SM4_KEY *key, unsigned char *ivec, const int enc)
{
	if (enc)
		CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
		    (block128_f)SM4_encrypt);
	else
		CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
		    (block128_f)SM4_decrypt);
}

static void
sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t length,
    const SM4_KEY *key, unsigned char *ivec, int *num, const int enc)
{
	CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
	    (block128_f)SM4_encrypt);
}

static void
sm4_ecb_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key,
    const int enc)
{
	if (enc)
		SM4_encrypt(in, out, key);
	else
		SM4_decrypt(in, out, key);
}

static void
sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length,
    const SM4_KEY *key, unsigned char *ivec, int *num)
{
	CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
	    (block128_f)SM4_encrypt);
}

static int
sm4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
{
	while (inl >= EVP_MAXCHUNK) {
		sm4_cbc_encrypt(in, out, EVP_MAXCHUNK, &((EVP_SM4_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
		inl -= EVP_MAXCHUNK;
		in += EVP_MAXCHUNK;
		out += EVP_MAXCHUNK;
	}

	if (inl)
		sm4_cbc_encrypt(in, out, inl, &((EVP_SM4_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);

	return 1;
}

static int
sm4_cfb128_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
{
	size_t chunk = EVP_MAXCHUNK;

	if (inl < chunk)
		chunk = inl;

	while (inl && inl >= chunk) {
		sm4_cfb128_encrypt(in, out, chunk, &((EVP_SM4_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
		inl -= chunk;
		in += chunk;
		out += chunk;
		if (inl < chunk)
			chunk = inl;
	}

	return 1;
}

static int
sm4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
{
	size_t i, bl;

	bl = ctx->cipher->block_size;

	if (inl < bl)
		return 1;

	inl -= bl;

	for (i = 0; i <= inl; i += bl)
		sm4_ecb_encrypt(in + i, out + i, &((EVP_SM4_KEY *)ctx->cipher_data)->ks, ctx->encrypt);

	return 1;
}

static int
sm4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)
{
	while (inl >= EVP_MAXCHUNK) {
		sm4_ofb128_encrypt(in, out, EVP_MAXCHUNK, &((EVP_SM4_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);
		inl -= EVP_MAXCHUNK;
		in += EVP_MAXCHUNK;
		out += EVP_MAXCHUNK;
	}

	if (inl)
		sm4_ofb128_encrypt(in, out, inl, &((EVP_SM4_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num);

	return 1;
}

static const EVP_CIPHER sm4_cbc = {
	.nid = NID_sm4_cbc,
	.block_size = 16,
	.key_len = 16,
	.iv_len = 16,
	.flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CBC_MODE,
	.init = sm4_init_key,
	.do_cipher = sm4_cbc_cipher,
	.cleanup = NULL,
	.ctx_size = sizeof(EVP_SM4_KEY),
	.set_asn1_parameters = 0,
	.get_asn1_parameters = 0,
	.ctrl = 0,
	.app_data = NULL,
};

const EVP_CIPHER *
EVP_sm4_cbc(void)
{
	return &sm4_cbc;
}

static const EVP_CIPHER sm4_cfb128 = {
	.nid = NID_sm4_cfb128,
	.block_size = 1,
	.key_len = 16,
	.iv_len = 16,
	.flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CFB_MODE,
	.init = sm4_init_key,
	.do_cipher = sm4_cfb128_cipher,
	.cleanup = NULL,
	.ctx_size = sizeof(EVP_SM4_KEY),
	.set_asn1_parameters = 0,
	.get_asn1_parameters = 0,
	.ctrl = 0,
	.app_data = NULL,
};

const EVP_CIPHER *
EVP_sm4_cfb128(void)
{
	return &sm4_cfb128;
}

static const EVP_CIPHER sm4_ofb = {
	.nid = NID_sm4_ofb128,
	.block_size = 1,
	.key_len = 16,
	.iv_len = 16,
	.flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_OFB_MODE,
	.init = sm4_init_key,
	.do_cipher = sm4_ofb_cipher,
	.cleanup = NULL,
	.ctx_size = sizeof(EVP_SM4_KEY),
	.set_asn1_parameters = 0,
	.get_asn1_parameters = 0,
	.ctrl = 0,
	.app_data = NULL,
};

const EVP_CIPHER *
EVP_sm4_ofb(void)
{
	return &sm4_ofb;
}

static const EVP_CIPHER sm4_ecb = {
	.nid = NID_sm4_ecb,
	.block_size = 16,
	.key_len = 16,
	.iv_len = 0,
	.flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE,
	.init = sm4_init_key,
	.do_cipher = sm4_ecb_cipher,
	.cleanup = NULL,
	.ctx_size = sizeof(EVP_SM4_KEY),
	.set_asn1_parameters = 0,
	.get_asn1_parameters = 0,
	.ctrl = 0,
	.app_data = NULL,
};

const EVP_CIPHER *
EVP_sm4_ecb(void)
{
	return &sm4_ecb;
}

static int
sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in,
    size_t len)
{
	EVP_SM4_KEY *key = ((EVP_SM4_KEY *)(ctx)->cipher_data);

	CRYPTO_ctr128_encrypt(in, out, len, &key->ks, ctx->iv, ctx->buf,
	    &ctx->num, (block128_f)SM4_encrypt);
	return 1;
}

static const EVP_CIPHER sm4_ctr_mode = {
	.nid = NID_sm4_ctr,
	.block_size = 1,
	.key_len = 16,
	.iv_len = 16,
	.flags = EVP_CIPH_CTR_MODE,
	.init = sm4_init_key,
	.do_cipher = sm4_ctr_cipher,
	.cleanup = NULL,
	.ctx_size = sizeof(EVP_SM4_KEY),
	.set_asn1_parameters = NULL,
	.get_asn1_parameters = NULL,
	.ctrl = NULL,
	.app_data = NULL,
};

const EVP_CIPHER *
EVP_sm4_ctr(void)
{
	return &sm4_ctr_mode;
}
#endif