blob: ff4b823feb36f49830158d52d7853478419bc1b9 [file] [log] [blame]
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001/*
2 * AES SIV (RFC 5297)
3 * Copyright (c) 2013 Cozybit, Inc.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "aes.h"
13#include "aes_wrap.h"
14
15
16static const u8 zero[AES_BLOCK_SIZE];
17
18
19static void dbl(u8 *pad)
20{
21 int i, carry;
22
23 carry = pad[0] & 0x80;
24 for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
25 pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
26 pad[AES_BLOCK_SIZE - 1] <<= 1;
27 if (carry)
28 pad[AES_BLOCK_SIZE - 1] ^= 0x87;
29}
30
31
32static void xor(u8 *a, const u8 *b)
33{
34 int i;
35
36 for (i = 0; i < AES_BLOCK_SIZE; i++)
37 *a++ ^= *b++;
38}
39
40
41static void xorend(u8 *a, int alen, const u8 *b, int blen)
42{
43 int i;
44
45 if (alen < blen)
46 return;
47
48 for (i = 0; i < blen; i++)
49 a[alen - blen + i] ^= b[i];
50}
51
52
53static void pad_block(u8 *pad, const u8 *addr, size_t len)
54{
55 os_memset(pad, 0, AES_BLOCK_SIZE);
56 os_memcpy(pad, addr, len);
57
58 if (len < AES_BLOCK_SIZE)
59 pad[len] = 0x80;
60}
61
62
63int aes_s2v(const u8 *key, size_t num_elem, const u8 *addr[],
64 size_t *len, u8 *mac)
65{
66 u8 tmp[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
67 u8 *buf = NULL;
68 int ret;
69 size_t i;
70
71 if (!num_elem) {
72 os_memcpy(tmp, zero, sizeof(zero));
73 tmp[AES_BLOCK_SIZE - 1] = 1;
74 return omac1_aes_128(key, tmp, sizeof(tmp), mac);
75 }
76
77 ret = omac1_aes_128(key, zero, sizeof(zero), tmp);
78 if (ret)
79 return ret;
80
81 for (i = 0; i < num_elem - 1; i++) {
82 ret = omac1_aes_128(key, addr[i], len[i], tmp2);
83 if (ret)
84 return ret;
85
86 dbl(tmp);
87 xor(tmp, tmp2);
88 }
89 if (len[i] >= AES_BLOCK_SIZE) {
90 buf = os_malloc(len[i]);
91 if (!buf)
92 return -ENOMEM;
93
94 os_memcpy(buf, addr[i], len[i]);
95 xorend(buf, len[i], tmp, AES_BLOCK_SIZE);
96 ret = omac1_aes_128(key, buf, len[i], mac);
97 os_free(buf);
98 return ret;
99 }
100
101 dbl(tmp);
102 pad_block(tmp2, addr[i], len[i]);
103 xor(tmp, tmp2);
104
105 return omac1_aes_128(key, tmp, sizeof(tmp), mac);
106}
107
108
109int aes_siv_encrypt(const u8 *key, const u8 *pw,
110 size_t pwlen, size_t num_elem,
111 const u8 *addr[], const size_t *len, u8 *out)
112{
113 const u8 *_addr[6];
114 size_t _len[6];
115 const u8 *k1 = key, *k2 = key + 16;
116 u8 v[AES_BLOCK_SIZE];
117 size_t i;
118 u8 *iv, *crypt_pw;
119
120 if (num_elem > ARRAY_SIZE(_addr) - 1)
121 return -1;
122
123 for (i = 0; i < num_elem; i++) {
124 _addr[i] = addr[i];
125 _len[i] = len[i];
126 }
127 _addr[num_elem] = pw;
128 _len[num_elem] = pwlen;
129
130 if (aes_s2v(k1, num_elem + 1, _addr, _len, v))
131 return -1;
132
133 iv = out;
134 crypt_pw = out + AES_BLOCK_SIZE;
135
136 os_memcpy(iv, v, AES_BLOCK_SIZE);
137 os_memcpy(crypt_pw, pw, pwlen);
138
139 /* zero out 63rd and 31st bits of ctr (from right) */
140 v[8] &= 0x7f;
141 v[12] &= 0x7f;
142 return aes_128_ctr_encrypt(k2, v, crypt_pw, pwlen);
143}
144
145
146int aes_siv_decrypt(const u8 *key, const u8 *iv_crypt, size_t iv_c_len,
147 size_t num_elem, const u8 *addr[], const size_t *len,
148 u8 *out)
149{
150 const u8 *_addr[6];
151 size_t _len[6];
152 const u8 *k1 = key, *k2 = key + 16;
153 size_t crypt_len;
154 size_t i;
155 int ret;
156 u8 iv[AES_BLOCK_SIZE];
157 u8 check[AES_BLOCK_SIZE];
158
159 if (iv_c_len < AES_BLOCK_SIZE || num_elem > ARRAY_SIZE(_addr) - 1)
160 return -1;
161 crypt_len = iv_c_len - AES_BLOCK_SIZE;
162
163 for (i = 0; i < num_elem; i++) {
164 _addr[i] = addr[i];
165 _len[i] = len[i];
166 }
167 _addr[num_elem] = out;
168 _len[num_elem] = crypt_len;
169
170 os_memcpy(iv, iv_crypt, AES_BLOCK_SIZE);
171 os_memcpy(out, iv_crypt + AES_BLOCK_SIZE, crypt_len);
172
173 iv[8] &= 0x7f;
174 iv[12] &= 0x7f;
175
176 ret = aes_128_ctr_encrypt(k2, iv, out, crypt_len);
177 if (ret)
178 return ret;
179
180 ret = aes_s2v(k1, num_elem + 1, _addr, _len, check);
181 if (ret)
182 return ret;
183 if (os_memcmp(check, iv_crypt, AES_BLOCK_SIZE) == 0)
184 return 0;
185
186 return -1;
187}