1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "sm4_core.h"
5
6 #define DEBUG(format,...) printf("[%s]:%d: "format"\n", __func__,__LINE__, ##__VA_ARGS__)
7
aes_xts128_encrypt(void * ctx1,void * ctx2,const unsigned char iv[16],const unsigned char * inp,unsigned char * out,unsigned long len,int enc)8 static int aes_xts128_encrypt(void *ctx1, void *ctx2,
9 const unsigned char iv[16], const unsigned char *inp,
10 unsigned char *out, unsigned long len, int enc)
11 {
12 const union { long one; char little; } is_endian = {1};
13 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch;
14 unsigned int i;
15
16 if (len < 16)
17 return -1;
18
19 memcpy(tweak.c, iv, 16);
20
21 rk_sm4_crypt_ecb(ctx2, (const unsigned char*)tweak.c, tweak.c);
22
23 if (!enc && (len % 16))
24 len -= 16;
25
26 while (len >= 16) {
27 memcpy(scratch.c, inp, 16);
28 scratch.u[0] ^= tweak.u[0];
29 scratch.u[1] ^= tweak.u[1];
30
31 if (enc)
32 rk_sm4_crypt_ecb(ctx1, (const unsigned char*)scratch.c, scratch.c);
33 else
34 rk_sm4_crypt_ecb(ctx1, (const unsigned char*)scratch.c, scratch.c);
35
36 scratch.u[0] ^= tweak.u[0];
37 scratch.u[1] ^= tweak.u[1];
38 memcpy(out, scratch.c, 16);
39
40 inp += 16;
41 out += 16;
42 len -= 16;
43
44 if (len == 0)
45 return 0;
46
47 if (is_endian.little) {
48 unsigned int carry,res;
49
50 res = 0x87 & (((int)tweak.d[3]) >> 31);
51 carry = (unsigned int)(tweak.u[0] >> 63);
52 tweak.u[0] = (tweak.u[0] << 1) ^ res;
53 tweak.u[1] = (tweak.u[1] << 1) | carry;
54 }
55 else {
56 size_t c;
57
58 for (c = 0, i = 0; i < 16; ++i) {
59 /*+ substitutes for |, because c is 1 bit */
60 c += ((size_t)tweak.c[i]) << 1;
61 tweak.c[i] = (u8)c;
62 c = c >> 8;
63 }
64 tweak.c[0] ^= (u8)(0x87 & (0-c));
65 }
66 }
67 if (enc) {
68 for (i = 0;i < len; ++i) {
69 u8 c = inp[i];
70 out[i] = scratch.c[i];
71 scratch.c[i] = c;
72 }
73 scratch.u[0] ^= tweak.u[0];
74 scratch.u[1] ^= tweak.u[1];
75 rk_sm4_crypt_ecb(ctx1, (const unsigned char*)scratch.c, scratch.c);
76 scratch.u[0] ^= tweak.u[0];
77 scratch.u[1] ^= tweak.u[1];
78 memcpy(out-16, scratch.c, 16);
79 }
80 else {
81 union { u64 u[2]; u8 c[16]; } tweak1;
82
83 if (is_endian.little) {
84 unsigned int carry,res;
85
86 res = 0x87 & (((int)tweak.d[3]) >> 31);
87 carry = (unsigned int)(tweak.u[0] >> 63);
88 tweak1.u[0] = (tweak.u[0] << 1) ^ res;
89 tweak1.u[1] = (tweak.u[1] << 1) | carry;
90 }
91 else {
92 size_t c;
93
94 for (c = 0, i = 0;i < 16;++i) {
95 /*+ substitutes for |, because c is 1 bit */
96 c += ((size_t)tweak.c[i]) << 1;
97 tweak1.c[i] = (u8)c;
98 c = c >> 8;
99 }
100 tweak1.c[0] ^= (u8)(0x87 & (0-c));
101 }
102
103 memcpy(scratch.c, inp, 16);
104 scratch.u[0] ^= tweak1.u[0];
105 scratch.u[1] ^= tweak1.u[1];
106
107 rk_sm4_crypt_ecb(ctx1, (const unsigned char*)scratch.c, scratch.c);
108 scratch.u[0] ^= tweak1.u[0];
109 scratch.u[1] ^= tweak1.u[1];
110
111 for (i = 0;i < len;++i) {
112 u8 c = inp[16+i];
113 out[16+i] = scratch.c[i];
114 scratch.c[i] = c;
115 }
116
117 scratch.u[0] ^= tweak.u[0];
118 scratch.u[1] ^= tweak.u[1];
119 rk_sm4_crypt_ecb(ctx1, (const unsigned char*)scratch.c, scratch.c);
120
121 scratch.u[0] ^= tweak.u[0];
122 scratch.u[1] ^= tweak.u[1];
123 memcpy (out, scratch.c, 16);
124 }
125
126 return 0;
127 }
128
129 /* XTS makes use of two different keys, usually generated by splitting
130 * the supplied block cipher's key in half.
131 * Because of the splitting, users wanting AES 256 and AES 128 encryption
132 * will need to choose key sizes of 512 bits and 256 bits respectively.
133 */
rk_sm4_xts_encrypt(const unsigned char * in,unsigned char * out,unsigned int length,const unsigned char * key,const int key_len,unsigned char * ivec,const int enc)134 int rk_sm4_xts_encrypt(const unsigned char *in, unsigned char *out,
135 unsigned int length, const unsigned char *key, const int key_len,
136 unsigned char *ivec, const int enc)
137 {
138 sm4_context ctx1, ctx2;
139
140 if(key_len != 32)
141 return -1;
142
143 if (enc) {
144 rk_sm4_setkey_enc(&ctx1, key);
145 } else {
146 rk_sm4_setkey_dec(&ctx1, key);
147 }
148
149 rk_sm4_setkey_enc(&ctx2, key + key_len / 2);
150
151 return aes_xts128_encrypt(&ctx1, &ctx2, ivec, in, out, length, enc);
152 }
153
154
155