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