1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "sm4_core.h"
5
rk_crypto_cbc128_encrypt(void * ctx,const unsigned char * in,unsigned char * out,unsigned int len,unsigned char * ivec,block128_f block)6 static void rk_crypto_cbc128_encrypt(void *ctx, const unsigned char *in, unsigned char *out,
7 unsigned int len, unsigned char *ivec, block128_f block)
8 {
9 unsigned int n;
10 const unsigned char *iv = ivec;
11
12 while (len) {
13 for(n=0; n<SM4_BLOCK_SIZE && n<len; ++n)
14 out[n] = in[n] ^ iv[n];
15 for(; n<SM4_BLOCK_SIZE; ++n)
16 out[n] = iv[n];
17 (*block)((const unsigned char*)out, out, ctx);
18 iv = out;
19 if (len<=SM4_BLOCK_SIZE) break;
20 len -= SM4_BLOCK_SIZE;
21 in += SM4_BLOCK_SIZE;
22 out += SM4_BLOCK_SIZE;
23 }
24 memcpy(ivec,iv,SM4_BLOCK_SIZE);
25 }
26
rk_crypto_cbc128_decrypt(void * ctx,const unsigned char * in,unsigned char * out,unsigned int len,unsigned char * ivec,block128_f block)27 static void rk_crypto_cbc128_decrypt(void *ctx, const unsigned char *in, unsigned char *out,
28 unsigned int len, unsigned char *ivec, block128_f block)
29 {
30 unsigned int n;
31 unsigned char c;
32 unsigned char tmp_buf[SM4_BLOCK_SIZE];
33
34 memset(tmp_buf, 0x00, sizeof(tmp_buf));
35
36 while (len) {
37 (*block)(in, tmp_buf, ctx);
38 for(n=0; n<SM4_BLOCK_SIZE && n<len; ++n) {
39 c = in[n];
40 out[n] = tmp_buf[n] ^ ivec[n];
41 ivec[n] = c;
42 }
43 if (len<=SM4_BLOCK_SIZE) {
44 for (; n<SM4_BLOCK_SIZE; ++n)
45 ivec[n] = in[n];
46 break;
47 }
48 len -= SM4_BLOCK_SIZE;
49 in += SM4_BLOCK_SIZE;
50 out += SM4_BLOCK_SIZE;
51 }
52 }
53
54
rk_crypto_cts_encrypt(void * ctx,const unsigned char * in,unsigned char * out,unsigned int length,unsigned char * ivec,const int enc,block128_f block)55 static int rk_crypto_cts_encrypt(void *ctx, const unsigned char *in, unsigned char *out,
56 unsigned int length, unsigned char *ivec, const int enc, block128_f block)
57 {
58 int k = 0, r = 0;
59
60 r = length % SM4_BLOCK_SIZE;
61 if (r) {
62 k = length - r - SM4_BLOCK_SIZE;
63 } else {
64 k = length;
65 }
66
67 if (enc){
68 unsigned char peniv[SM4_BLOCK_SIZE] = {0};
69 memset(peniv, 0x00, sizeof(peniv));
70
71 rk_crypto_cbc128_encrypt(ctx, in, out, k, ivec,block);
72
73 if (r) {
74 memcpy(peniv, in + k + SM4_BLOCK_SIZE, r);
75
76 rk_crypto_cbc128_encrypt(ctx, in + k, out + k, SM4_BLOCK_SIZE, ivec, block);
77 memcpy(out + length - r, out + k, r);
78 rk_crypto_cbc128_encrypt(ctx, peniv, out + k, SM4_BLOCK_SIZE, ivec, block);
79 }else{
80 //swap last two block
81 memcpy(peniv, out + length - SM4_BLOCK_SIZE, SM4_BLOCK_SIZE);
82 memcpy(out + length - SM4_BLOCK_SIZE, out + length - 2*SM4_BLOCK_SIZE, SM4_BLOCK_SIZE);
83 memcpy(out + length - 2*SM4_BLOCK_SIZE, peniv, SM4_BLOCK_SIZE);
84 }
85 }else{
86 unsigned int i;
87 unsigned char *pout_tmp = NULL;
88 unsigned char tmp1[SM4_BLOCK_SIZE], tmp2[SM4_BLOCK_SIZE];
89
90 memset(tmp1, 0x00, sizeof(tmp1));
91 memset(tmp2, 0x00, sizeof(tmp2));
92
93 if(r == 0){
94 rk_crypto_cbc128_decrypt(ctx, in, out, k-2*SM4_BLOCK_SIZE, ivec, block);
95
96 //swap last two block
97 rk_crypto_cbc128_decrypt(ctx, in + length - SM4_BLOCK_SIZE, out + length - 2*SM4_BLOCK_SIZE,
98 SM4_BLOCK_SIZE, ivec, block);
99 rk_crypto_cbc128_decrypt(ctx, in + length - 2*SM4_BLOCK_SIZE, out + length - SM4_BLOCK_SIZE,
100 SM4_BLOCK_SIZE, ivec, block);
101 }else{
102 rk_crypto_cbc128_decrypt(ctx, in, out, k, ivec, block);
103 (*block)(in + k, tmp1, ctx);
104
105 memcpy(tmp2, in + k + SM4_BLOCK_SIZE, r);
106 memcpy(tmp2+r, tmp1+r, SM4_BLOCK_SIZE-r);
107
108 //get last one plain text
109 pout_tmp = out + k + SM4_BLOCK_SIZE;
110 for(i=0; i<SM4_BLOCK_SIZE; i++,pout_tmp++)
111 *pout_tmp = tmp1[i] ^ tmp2[i];
112
113 (*block)(tmp2, tmp2, ctx);
114
115 //get sencond to last plain text
116 pout_tmp = out + k;
117 for(i=0; i<SM4_BLOCK_SIZE; i++,pout_tmp++)
118 *pout_tmp = tmp2[i] ^ ivec[i];
119
120 }
121 }
122
123 return 0;
124 }
125
126
rk_sm4_cts_encrypt(const unsigned char * in,unsigned char * out,unsigned long length,const unsigned char * key,const int key_len,unsigned char * ivec,const int enc)127 int rk_sm4_cts_encrypt(const unsigned char *in, unsigned char *out,
128 unsigned long length, const unsigned char *key, const int key_len,
129 unsigned char *ivec, const int enc)
130 {
131 sm4_context ctx;
132
133 if (in == NULL || out ==NULL || key == NULL)
134 return -1;
135
136 if (key_len != 16)
137 return -2;
138
139 if(length <= SM4_BLOCK_SIZE)
140 return -3;
141
142 if (enc) {
143 rk_sm4_setkey_enc(&ctx, key);
144 } else {
145 rk_sm4_setkey_dec(&ctx, key);
146 }
147
148 rk_crypto_cts_encrypt((void*)(&ctx), in, out, length, ivec, enc, rk_rk_sm4_crypt_ecb);
149
150 return 0;
151 }
152
153
154