summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/evp')
-rw-r--r--src/lib/libcrypto/evp/c_all.c12
-rw-r--r--src/lib/libcrypto/evp/e_sm4.c113
-rw-r--r--src/lib/libcrypto/evp/evp.h11
3 files changed, 134 insertions, 2 deletions
diff --git a/src/lib/libcrypto/evp/c_all.c b/src/lib/libcrypto/evp/c_all.c
index 5ed55f67f6..cce3640866 100644
--- a/src/lib/libcrypto/evp/c_all.c
+++ b/src/lib/libcrypto/evp/c_all.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: c_all.c,v 1.24 2018/12/26 15:11:04 tb Exp $ */ 1/* $OpenBSD: c_all.c,v 1.25 2019/03/17 17:42:37 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -227,6 +227,16 @@ OpenSSL_add_all_ciphers_internal(void)
227 EVP_add_cipher(EVP_gost2814789_cfb64()); 227 EVP_add_cipher(EVP_gost2814789_cfb64());
228 EVP_add_cipher(EVP_gost2814789_cnt()); 228 EVP_add_cipher(EVP_gost2814789_cnt());
229#endif 229#endif
230
231#ifndef OPENSSL_NO_SM4
232 EVP_add_cipher(EVP_sm4_ecb());
233 EVP_add_cipher(EVP_sm4_cbc());
234 EVP_add_cipher(EVP_sm4_cfb());
235 EVP_add_cipher(EVP_sm4_ofb());
236 EVP_add_cipher(EVP_sm4_ctr());
237 EVP_add_cipher_alias(SN_sm4_cbc, "SM4");
238 EVP_add_cipher_alias(SN_sm4_cbc, "sm4");
239#endif
230} 240}
231 241
232void 242void
diff --git a/src/lib/libcrypto/evp/e_sm4.c b/src/lib/libcrypto/evp/e_sm4.c
new file mode 100644
index 0000000000..554915b29c
--- /dev/null
+++ b/src/lib/libcrypto/evp/e_sm4.c
@@ -0,0 +1,113 @@
1/* $OpenBSD: e_sm4.c,v 1.1 2019/03/17 17:42:37 tb Exp $ */
2/*
3 * Copyright (c) 2017, 2019 Ribose Inc
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <openssl/opensslconf.h>
19
20#ifndef OPENSSL_NO_SM4
21#include <openssl/evp.h>
22#include <openssl/modes.h>
23#include <openssl/sm4.h>
24
25#include "evp_locl.h"
26
27typedef struct {
28 SM4_KEY ks;
29} EVP_SM4_KEY;
30
31static int
32sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
33 const unsigned char *iv, int enc)
34{
35 SM4_set_key(key, ctx->cipher_data);
36 return 1;
37}
38
39static void
40sm4_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len,
41 const SM4_KEY *key, unsigned char *ivec, const int enc)
42{
43 if (enc)
44 CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
45 (block128_f)SM4_encrypt);
46 else
47 CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
48 (block128_f)SM4_decrypt);
49}
50
51static void
52sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t length,
53 const SM4_KEY *key, unsigned char *ivec, int *num, const int enc)
54{
55 CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
56 (block128_f)SM4_encrypt);
57}
58
59static void
60sm4_ecb_encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key,
61 const int enc)
62{
63 if (enc)
64 SM4_encrypt(in, out, key);
65 else
66 SM4_decrypt(in, out, key);
67}
68
69static void
70sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length,
71 const SM4_KEY *key, unsigned char *ivec, int *num)
72{
73 CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
74 (block128_f)SM4_encrypt);
75}
76
77IMPLEMENT_BLOCK_CIPHER(sm4, ks, sm4, EVP_SM4_KEY, NID_sm4, 16, 16, 16, 128,
78 EVP_CIPH_FLAG_DEFAULT_ASN1, sm4_init_key, NULL, 0, 0, 0)
79
80static int
81sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in,
82 size_t len)
83{
84 EVP_SM4_KEY *key = EVP_C_DATA(EVP_SM4_KEY, ctx);
85
86 CRYPTO_ctr128_encrypt(in, out, len, &key->ks, ctx->iv, ctx->buf,
87 &ctx->num, (block128_f)SM4_encrypt);
88 return 1;
89}
90
91static const EVP_CIPHER sm4_ctr_mode = {
92 .nid = NID_sm4_ctr,
93 .block_size = 1,
94 .key_len = 16,
95 .iv_len = 16,
96 .flags = EVP_CIPH_CTR_MODE,
97 .init = sm4_init_key,
98 .do_cipher = sm4_ctr_cipher,
99 .cleanup = NULL,
100 .ctx_size = sizeof(EVP_SM4_KEY),
101 .set_asn1_parameters = NULL,
102 .get_asn1_parameters = NULL,
103 .ctrl = NULL,
104 .app_data = NULL,
105};
106
107const EVP_CIPHER *
108EVP_sm4_ctr(void)
109{
110 return &sm4_ctr_mode;
111}
112
113#endif
diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h
index 0645303686..cd9b33c9b8 100644
--- a/src/lib/libcrypto/evp/evp.h
+++ b/src/lib/libcrypto/evp/evp.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp.h,v 1.72 2019/01/22 00:59:21 dlg Exp $ */ 1/* $OpenBSD: evp.h,v 1.73 2019/03/17 17:42:37 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -837,6 +837,15 @@ const EVP_CIPHER *EVP_gost2814789_cfb64(void);
837const EVP_CIPHER *EVP_gost2814789_cnt(void); 837const EVP_CIPHER *EVP_gost2814789_cnt(void);
838#endif 838#endif
839 839
840#ifndef OPENSSL_NO_SM4
841const EVP_CIPHER *EVP_sm4_ecb(void);
842const EVP_CIPHER *EVP_sm4_cbc(void);
843const EVP_CIPHER *EVP_sm4_cfb128(void);
844#define EVP_sm4_cfb EVP_sm4_cfb128
845const EVP_CIPHER *EVP_sm4_ofb(void);
846const EVP_CIPHER *EVP_sm4_ctr(void);
847#endif
848
840void OPENSSL_add_all_algorithms_noconf(void); 849void OPENSSL_add_all_algorithms_noconf(void);
841void OPENSSL_add_all_algorithms_conf(void); 850void OPENSSL_add_all_algorithms_conf(void);
842 851