]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/e_sm4.c
Update copyright year
[thirdparty/openssl.git] / crypto / evp / e_sm4.c
CommitLineData
f19a5ff9 1/*
3c2bdd7d 2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
f19a5ff9
RT
3 * Copyright 2017 Ribose Inc. All Rights Reserved.
4 * Ported from Ribose contributions from Botan.
5 *
4a8b0c55 6 * Licensed under the Apache License 2.0 (the "License"). You may not use
f19a5ff9
RT
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/cryptlib.h"
13#ifndef OPENSSL_NO_SM4
14# include <openssl/evp.h>
15# include <openssl/modes.h>
25f2138b
DMSP
16# include "crypto/sm4.h"
17# include "crypto/evp.h"
1453d736 18# include "evp_local.h"
f19a5ff9
RT
19
20typedef struct {
21 SM4_KEY ks;
22} EVP_SM4_KEY;
23
24static int sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
25 const unsigned char *iv, int enc)
26{
8a6e9125 27 ossl_sm4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx));
f19a5ff9
RT
28 return 1;
29}
30
31static void sm4_cbc_encrypt(const unsigned char *in, unsigned char *out,
32 size_t len, const SM4_KEY *key,
33 unsigned char *ivec, const int enc)
34{
35 if (enc)
36 CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
8a6e9125 37 (block128_f)ossl_sm4_encrypt);
f19a5ff9
RT
38 else
39 CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
8a6e9125 40 (block128_f)ossl_sm4_decrypt);
f19a5ff9
RT
41}
42
43static void sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out,
44 size_t length, const SM4_KEY *key,
45 unsigned char *ivec, int *num, const int enc)
46{
47 CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
8a6e9125 48 (block128_f)ossl_sm4_encrypt);
f19a5ff9
RT
49}
50
51static void sm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
52 const SM4_KEY *key, const int enc)
53{
54 if (enc)
8a6e9125 55 ossl_sm4_encrypt(in, out, key);
f19a5ff9 56 else
8a6e9125 57 ossl_sm4_decrypt(in, out, key);
f19a5ff9
RT
58}
59
60static void sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out,
61 size_t length, const SM4_KEY *key,
62 unsigned char *ivec, int *num)
63{
64 CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
8a6e9125 65 (block128_f)ossl_sm4_encrypt);
f19a5ff9
RT
66}
67
68IMPLEMENT_BLOCK_CIPHER(sm4, ks, sm4, EVP_SM4_KEY, NID_sm4,
69 16, 16, 16, 128, EVP_CIPH_FLAG_DEFAULT_ASN1,
70 sm4_init_key, 0, 0, 0, 0)
71
72static int sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
73 const unsigned char *in, size_t len)
74{
75 unsigned int num = EVP_CIPHER_CTX_num(ctx);
76 EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY, ctx);
77
1453d736 78 CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv,
f19a5ff9 79 EVP_CIPHER_CTX_buf_noconst(ctx), &num,
8a6e9125 80 (block128_f)ossl_sm4_encrypt);
f19a5ff9
RT
81 EVP_CIPHER_CTX_set_num(ctx, num);
82 return 1;
83}
84
85static const EVP_CIPHER sm4_ctr_mode = {
86 NID_sm4_ctr, 1, 16, 16,
87 EVP_CIPH_CTR_MODE,
88 sm4_init_key,
89 sm4_ctr_cipher,
90 NULL,
91 sizeof(EVP_SM4_KEY),
92 NULL, NULL, NULL, NULL
93};
94
95const EVP_CIPHER *EVP_sm4_ctr(void)
96{
97 return &sm4_ctr_mode;
98}
99
100#endif