summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/verified/Hacl_Chacha20_Vec128.c
blob: 4eba49f47d4e0f6513a1100b9c9c2e54c381ed4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* Copyright 2016-2017 INRIA and Microsoft Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Hacl_Chacha20_Vec128.h"

inline static void
Hacl_Impl_Chacha20_Vec128_State_state_incr(vec *k)
{
    vec k3 = k[3U];
    k[3U] = vec_increment(k3);
}

inline static void
Hacl_Impl_Chacha20_Vec128_State_state_to_key_block(uint8_t *stream_block, vec *k)
{
    vec k0 = k[0U];
    vec k1 = k[1U];
    vec k2 = k[2U];
    vec k3 = k[3U];
    uint8_t *a = stream_block;
    uint8_t *b = stream_block + (uint32_t)16U;
    uint8_t *c = stream_block + (uint32_t)32U;
    uint8_t *d = stream_block + (uint32_t)48U;
    vec_store_le(a, k0);
    vec_store_le(b, k1);
    vec_store_le(c, k2);
    vec_store_le(d, k3);
}

inline static void
Hacl_Impl_Chacha20_Vec128_State_state_setup(vec *st, uint8_t *k, uint8_t *n1, uint32_t c)
{
    st[0U] =
        vec_load_32x4((uint32_t)0x61707865U,
                      (uint32_t)0x3320646eU,
                      (uint32_t)0x79622d32U,
                      (uint32_t)0x6b206574U);
    vec k0 = vec_load128_le(k);
    vec k1 = vec_load128_le(k + (uint32_t)16U);
    st[1U] = k0;
    st[2U] = k1;
    uint32_t n0 = load32_le(n1);
    uint8_t *x00 = n1 + (uint32_t)4U;
    uint32_t n10 = load32_le(x00);
    uint8_t *x0 = n1 + (uint32_t)8U;
    uint32_t n2 = load32_le(x0);
    vec v1 = vec_load_32x4(c, n0, n10, n2);
    st[3U] = v1;
}

inline static void
Hacl_Impl_Chacha20_Vec128_round(vec *st)
{
    vec sa = st[0U];
    vec sb0 = st[1U];
    vec sd0 = st[3U];
    vec sa10 = vec_add(sa, sb0);
    vec sd10 = vec_rotate_left(vec_xor(sd0, sa10), (uint32_t)16U);
    st[0U] = sa10;
    st[3U] = sd10;
    vec sa0 = st[2U];
    vec sb1 = st[3U];
    vec sd2 = st[1U];
    vec sa11 = vec_add(sa0, sb1);
    vec sd11 = vec_rotate_left(vec_xor(sd2, sa11), (uint32_t)12U);
    st[2U] = sa11;
    st[1U] = sd11;
    vec sa2 = st[0U];
    vec sb2 = st[1U];
    vec sd3 = st[3U];
    vec sa12 = vec_add(sa2, sb2);
    vec sd12 = vec_rotate_left(vec_xor(sd3, sa12), (uint32_t)8U);
    st[0U] = sa12;
    st[3U] = sd12;
    vec sa3 = st[2U];
    vec sb = st[3U];
    vec sd = st[1U];
    vec sa1 = vec_add(sa3, sb);
    vec sd1 = vec_rotate_left(vec_xor(sd, sa1), (uint32_t)7U);
    st[2U] = sa1;
    st[1U] = sd1;
}

inline static void
Hacl_Impl_Chacha20_Vec128_double_round(vec *st)
{
    Hacl_Impl_Chacha20_Vec128_round(st);
    vec r1 = st[1U];
    vec r20 = st[2U];
    vec r30 = st[3U];
    st[1U] = vec_shuffle_right(r1, (uint32_t)1U);
    st[2U] = vec_shuffle_right(r20, (uint32_t)2U);
    st[3U] = vec_shuffle_right(r30, (uint32_t)3U);
    Hacl_Impl_Chacha20_Vec128_round(st);
    vec r10 = st[1U];
    vec r2 = st[2U];
    vec r3 = st[3U];
    st[1U] = vec_shuffle_right(r10, (uint32_t)3U);
    st[2U] = vec_shuffle_right(r2, (uint32_t)2U);
    st[3U] = vec_shuffle_right(r3, (uint32_t)1U);
}

inline static void
Hacl_Impl_Chacha20_Vec128_double_round3(vec *st, vec *st_, vec *st__)
{
    Hacl_Impl_Chacha20_Vec128_double_round(st);
    Hacl_Impl_Chacha20_Vec128_double_round(st_);
    Hacl_Impl_Chacha20_Vec128_double_round(st__);
}

inline static void
Hacl_Impl_Chacha20_Vec128_sum_states(vec *st_, vec *st)
{
    vec s0 = st[0U];
    vec s1 = st[1U];
    vec s2 = st[2U];
    vec s3 = st[3U];
    vec s0_ = st_[0U];
    vec s1_ = st_[1U];
    vec s2_ = st_[2U];
    vec s3_ = st_[3U];
    st_[0U] = vec_add(s0_, s0);
    st_[1U] = vec_add(s1_, s1);
    st_[2U] = vec_add(s2_, s2);
    st_[3U] = vec_add(s3_, s3);
}

inline static void
Hacl_Impl_Chacha20_Vec128_copy_state(vec *st_, vec *st)
{
    vec st0 = st[0U];
    vec st1 = st[1U];
    vec st2 = st[2U];
    vec st3 = st[3U];
    st_[0U] = st0;
    st_[1U] = st1;
    st_[2U] = st2;
    st_[3U] = st3;
}

inline static void
Hacl_Impl_Chacha20_Vec128_chacha20_core(vec *k, vec *st)
{
    Hacl_Impl_Chacha20_Vec128_copy_state(k, st);
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)10U; i = i + (uint32_t)1U)
        Hacl_Impl_Chacha20_Vec128_double_round(k);
    Hacl_Impl_Chacha20_Vec128_sum_states(k, st);
}

static void
Hacl_Impl_Chacha20_Vec128_state_incr(vec *st)
{
    Hacl_Impl_Chacha20_Vec128_State_state_incr(st);
}

inline static void
Hacl_Impl_Chacha20_Vec128_chacha20_incr3(vec *k0, vec *k1, vec *k2, vec *st)
{
    Hacl_Impl_Chacha20_Vec128_copy_state(k0, st);
    Hacl_Impl_Chacha20_Vec128_copy_state(k1, st);
    Hacl_Impl_Chacha20_Vec128_state_incr(k1);
    Hacl_Impl_Chacha20_Vec128_copy_state(k2, k1);
    Hacl_Impl_Chacha20_Vec128_state_incr(k2);
}

inline static void
Hacl_Impl_Chacha20_Vec128_chacha20_sum3(vec *k0, vec *k1, vec *k2, vec *st)
{
    Hacl_Impl_Chacha20_Vec128_sum_states(k0, st);
    Hacl_Impl_Chacha20_Vec128_state_incr(st);
    Hacl_Impl_Chacha20_Vec128_sum_states(k1, st);
    Hacl_Impl_Chacha20_Vec128_state_incr(st);
    Hacl_Impl_Chacha20_Vec128_sum_states(k2, st);
}

inline static void
Hacl_Impl_Chacha20_Vec128_chacha20_core3(vec *k0, vec *k1, vec *k2, vec *st)
{
    Hacl_Impl_Chacha20_Vec128_chacha20_incr3(k0, k1, k2, st);
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)10U; i = i + (uint32_t)1U)
        Hacl_Impl_Chacha20_Vec128_double_round3(k0, k1, k2);
    Hacl_Impl_Chacha20_Vec128_chacha20_sum3(k0, k1, k2, st);
}

inline static void
Hacl_Impl_Chacha20_Vec128_chacha20_block(uint8_t *stream_block, vec *st)
{
    KRML_CHECK_SIZE(vec_zero(), (uint32_t)4U);
    vec k[4U];
    for (uint32_t _i = 0U; _i < (uint32_t)4U; ++_i)
        k[_i] = vec_zero();
    Hacl_Impl_Chacha20_Vec128_chacha20_core(k, st);
    Hacl_Impl_Chacha20_Vec128_State_state_to_key_block(stream_block, k);
}

inline static void
Hacl_Impl_Chacha20_Vec128_init(vec *st, uint8_t *k, uint8_t *n1, uint32_t ctr)
{
    Hacl_Impl_Chacha20_Vec128_State_state_setup(st, k, n1, ctr);
}

static void
Hacl_Impl_Chacha20_Vec128_update_last(uint8_t *output, uint8_t *plain, uint32_t len, vec *st)
{
    uint8_t block[64U] = { 0U };
    Hacl_Impl_Chacha20_Vec128_chacha20_block(block, st);
    uint8_t *mask = block;
    for (uint32_t i = (uint32_t)0U; i < len; i = i + (uint32_t)1U) {
        uint8_t xi = plain[i];
        uint8_t yi = mask[i];
        output[i] = xi ^ yi;
    }
}

static void
Hacl_Impl_Chacha20_Vec128_xor_block(uint8_t *output, uint8_t *plain, vec *st)
{
    vec p0 = vec_load_le(plain);
    vec p1 = vec_load_le(plain + (uint32_t)16U);
    vec p2 = vec_load_le(plain + (uint32_t)32U);
    vec p3 = vec_load_le(plain + (uint32_t)48U);
    vec k0 = st[0U];
    vec k1 = st[1U];
    vec k2 = st[2U];
    vec k3 = st[3U];
    vec o00 = vec_xor(p0, k0);
    vec o10 = vec_xor(p1, k1);
    vec o20 = vec_xor(p2, k2);
    vec o30 = vec_xor(p3, k3);
    uint8_t *o0 = output;
    uint8_t *o1 = output + (uint32_t)16U;
    uint8_t *o2 = output + (uint32_t)32U;
    uint8_t *o3 = output + (uint32_t)48U;
    vec_store_le(o0, o00);
    vec_store_le(o1, o10);
    vec_store_le(o2, o20);
    vec_store_le(o3, o30);
}

static void
Hacl_Impl_Chacha20_Vec128_update(uint8_t *output, uint8_t *plain, vec *st)
{
    KRML_CHECK_SIZE(vec_zero(), (uint32_t)4U);
    vec k[4U];
    for (uint32_t _i = 0U; _i < (uint32_t)4U; ++_i)
        k[_i] = vec_zero();
    Hacl_Impl_Chacha20_Vec128_chacha20_core(k, st);
    Hacl_Impl_Chacha20_Vec128_xor_block(output, plain, k);
}

static void
Hacl_Impl_Chacha20_Vec128_update3(uint8_t *output, uint8_t *plain, vec *st)
{
    KRML_CHECK_SIZE(vec_zero(), (uint32_t)4U);
    vec k0[4U];
    for (uint32_t _i = 0U; _i < (uint32_t)4U; ++_i)
        k0[_i] = vec_zero();
    KRML_CHECK_SIZE(vec_zero(), (uint32_t)4U);
    vec k1[4U];
    for (uint32_t _i = 0U; _i < (uint32_t)4U; ++_i)
        k1[_i] = vec_zero();
    KRML_CHECK_SIZE(vec_zero(), (uint32_t)4U);
    vec k2[4U];
    for (uint32_t _i = 0U; _i < (uint32_t)4U; ++_i)
        k2[_i] = vec_zero();
    Hacl_Impl_Chacha20_Vec128_chacha20_core3(k0, k1, k2, st);
    uint8_t *p0 = plain;
    uint8_t *p1 = plain + (uint32_t)64U;
    uint8_t *p2 = plain + (uint32_t)128U;
    uint8_t *o0 = output;
    uint8_t *o1 = output + (uint32_t)64U;
    uint8_t *o2 = output + (uint32_t)128U;
    Hacl_Impl_Chacha20_Vec128_xor_block(o0, p0, k0);
    Hacl_Impl_Chacha20_Vec128_xor_block(o1, p1, k1);
    Hacl_Impl_Chacha20_Vec128_xor_block(o2, p2, k2);
}

static void
Hacl_Impl_Chacha20_Vec128_update3_(
    uint8_t *output,
    uint8_t *plain,
    uint32_t len,
    vec *st,
    uint32_t i)
{
    uint8_t *out_block = output + (uint32_t)192U * i;
    uint8_t *plain_block = plain + (uint32_t)192U * i;
    Hacl_Impl_Chacha20_Vec128_update3(out_block, plain_block, st);
    Hacl_Impl_Chacha20_Vec128_state_incr(st);
}

static void
Hacl_Impl_Chacha20_Vec128_chacha20_counter_mode_blocks3(
    uint8_t *output,
    uint8_t *plain,
    uint32_t len,
    vec *st)
{
    for (uint32_t i = (uint32_t)0U; i < len; i = i + (uint32_t)1U)
        Hacl_Impl_Chacha20_Vec128_update3_(output, plain, len, st, i);
}

static void
Hacl_Impl_Chacha20_Vec128_chacha20_counter_mode_blocks(
    uint8_t *output,
    uint8_t *plain,
    uint32_t len,
    vec *st)
{
    uint32_t len3 = len / (uint32_t)3U;
    uint32_t rest3 = len % (uint32_t)3U;
    uint8_t *plain_ = plain;
    uint8_t *blocks1 = plain + (uint32_t)192U * len3;
    uint8_t *output_ = output;
    uint8_t *outs = output + (uint32_t)192U * len3;
    Hacl_Impl_Chacha20_Vec128_chacha20_counter_mode_blocks3(output_, plain_, len3, st);
    if (rest3 == (uint32_t)2U) {
        uint8_t *block0 = blocks1;
        uint8_t *block1 = blocks1 + (uint32_t)64U;
        uint8_t *out0 = outs;
        uint8_t *out1 = outs + (uint32_t)64U;
        Hacl_Impl_Chacha20_Vec128_update(out0, block0, st);
        Hacl_Impl_Chacha20_Vec128_state_incr(st);
        Hacl_Impl_Chacha20_Vec128_update(out1, block1, st);
        Hacl_Impl_Chacha20_Vec128_state_incr(st);
    } else if (rest3 == (uint32_t)1U) {
        Hacl_Impl_Chacha20_Vec128_update(outs, blocks1, st);
        Hacl_Impl_Chacha20_Vec128_state_incr(st);
    }
}

static void
Hacl_Impl_Chacha20_Vec128_chacha20_counter_mode(
    uint8_t *output,
    uint8_t *plain,
    uint32_t len,
    vec *st)
{
    uint32_t blocks_len = len >> (uint32_t)6U;
    uint32_t part_len = len & (uint32_t)0x3fU;
    uint8_t *output_ = output;
    uint8_t *plain_ = plain;
    uint8_t *output__ = output + (uint32_t)64U * blocks_len;
    uint8_t *plain__ = plain + (uint32_t)64U * blocks_len;
    Hacl_Impl_Chacha20_Vec128_chacha20_counter_mode_blocks(output_, plain_, blocks_len, st);
    if (part_len > (uint32_t)0U)
        Hacl_Impl_Chacha20_Vec128_update_last(output__, plain__, part_len, st);
}

static void
Hacl_Impl_Chacha20_Vec128_chacha20(
    uint8_t *output,
    uint8_t *plain,
    uint32_t len,
    uint8_t *k,
    uint8_t *n1,
    uint32_t ctr)
{
    KRML_CHECK_SIZE(vec_zero(), (uint32_t)4U);
    vec buf[4U];
    for (uint32_t _i = 0U; _i < (uint32_t)4U; ++_i)
        buf[_i] = vec_zero();
    vec *st = buf;
    Hacl_Impl_Chacha20_Vec128_init(st, k, n1, ctr);
    Hacl_Impl_Chacha20_Vec128_chacha20_counter_mode(output, plain, len, st);
}

void
Hacl_Chacha20_Vec128_chacha20(
    uint8_t *output,
    uint8_t *plain,
    uint32_t len,
    uint8_t *k,
    uint8_t *n1,
    uint32_t ctr)
{
    Hacl_Impl_Chacha20_Vec128_chacha20(output, plain, len, k, n1, ctr);
}