• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2023, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/base.h>
16 
17 #include <assert.h>
18 #include <stdlib.h>
19 
20 #include "../internal.h"
21 #include "./internal.h"
22 
23 
24 // keccak_f implements the Keccak-1600 permutation as described at
25 // https://keccak.team/keccak_specs_summary.html. Each lane is represented as a
26 // 64-bit value and the 5×5 lanes are stored as an array in row-major order.
keccak_f(uint64_t state[25])27 static void keccak_f(uint64_t state[25]) {
28   static const int kNumRounds = 24;
29   for (int round = 0; round < kNumRounds; round++) {
30     // θ step
31     uint64_t c[5];
32     for (int x = 0; x < 5; x++) {
33       c[x] = state[x] ^ state[x + 5] ^ state[x + 10] ^ state[x + 15] ^
34              state[x + 20];
35     }
36 
37     for (int x = 0; x < 5; x++) {
38       const uint64_t d = c[(x + 4) % 5] ^ CRYPTO_rotl_u64(c[(x + 1) % 5], 1);
39       for (int y = 0; y < 5; y++) {
40         state[y * 5 + x] ^= d;
41       }
42     }
43 
44     // ρ and π steps.
45     //
46     // These steps involve a mapping of the state matrix. Each input point,
47     // (x,y), is rotated and written to the point (y, 2x + 3y). In the Keccak
48     // pseudo-code a separate array is used because an in-place operation would
49     // overwrite some values that are subsequently needed. However, the mapping
50     // forms a trail through 24 of the 25 values so we can do it in place with
51     // only a single temporary variable.
52     //
53     // Start with (1, 0). The value here will be mapped and end up at (0, 2).
54     // That value will end up at (2, 1), then (1, 2), and so on. After 24
55     // steps, 24 of the 25 values have been hit (as this mapping is injective)
56     // and the sequence will repeat. All that remains is to handle the element
57     // at (0, 0), but the rotation for that element is zero, and it goes to (0,
58     // 0), so we can ignore it.
59     static const uint8_t kIndexes[24] = {10, 7,  11, 17, 18, 3,  5,  16,
60                                          8,  21, 24, 4,  15, 23, 19, 13,
61                                          12, 2,  20, 14, 22, 9,  6,  1};
62     static const uint8_t kRotations[24] = {1,  3,  6,  10, 15, 21, 28, 36,
63                                            45, 55, 2,  14, 27, 41, 56, 8,
64                                            25, 43, 62, 18, 39, 61, 20, 44};
65     uint64_t prev_value = state[1];
66     for (int i = 0; i < 24; i++) {
67       const uint64_t value = CRYPTO_rotl_u64(prev_value, kRotations[i]);
68       const size_t index = kIndexes[i];
69       prev_value = state[index];
70       state[index] = value;
71     }
72 
73     // χ step
74     for (int y = 0; y < 5; y++) {
75       const int row_index = 5 * y;
76       const uint64_t orig_x0 = state[row_index];
77       const uint64_t orig_x1 = state[row_index + 1];
78       state[row_index] ^= ~orig_x1 & state[row_index + 2];
79       state[row_index + 1] ^= ~state[row_index + 2] & state[row_index + 3];
80       state[row_index + 2] ^= ~state[row_index + 3] & state[row_index + 4];
81       state[row_index + 3] ^= ~state[row_index + 4] & orig_x0;
82       state[row_index + 4] ^= ~orig_x0 & orig_x1;
83     }
84 
85     // ι step
86     //
87     // From https://keccak.team/files/Keccak-reference-3.0.pdf, section
88     // 1.2, the round constants are based on the output of a LFSR. Thus, as
89     // suggested in the appendix of of
90     // https://keccak.team/keccak_specs_summary.html, the values are
91     // simply encoded here.
92     static const uint64_t kRoundConstants[24] = {
93         0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
94         0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
95         0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
96         0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
97         0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
98         0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
99         0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
100         0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
101     };
102 
103     state[0] ^= kRoundConstants[round];
104   }
105 }
106 
keccak_init(struct BORINGSSL_keccak_st * ctx,size_t * out_required_out_len,const uint8_t * in,size_t in_len,enum boringssl_keccak_config_t config)107 static void keccak_init(struct BORINGSSL_keccak_st *ctx,
108                         size_t *out_required_out_len, const uint8_t *in,
109                         size_t in_len, enum boringssl_keccak_config_t config) {
110   size_t capacity_bytes;
111   uint8_t terminator;
112   switch (config) {
113     case boringssl_sha3_256:
114       capacity_bytes = 512 / 8;
115       *out_required_out_len = 32;
116       terminator = 0x06;
117       break;
118     case boringssl_sha3_512:
119       capacity_bytes = 1024 / 8;
120       *out_required_out_len = 64;
121       terminator = 0x06;
122       break;
123     case boringssl_shake128:
124       capacity_bytes = 256 / 8;
125       *out_required_out_len = 0;
126       terminator = 0x1f;
127       break;
128     case boringssl_shake256:
129       capacity_bytes = 512 / 8;
130       *out_required_out_len = 0;
131       terminator = 0x1f;
132       break;
133     default:
134       abort();
135   }
136 
137   OPENSSL_memset(ctx, 0, sizeof(*ctx));
138   ctx->rate_bytes = 200 - capacity_bytes;
139   assert(ctx->rate_bytes % 8 == 0);
140   const size_t rate_words = ctx->rate_bytes / 8;
141 
142   while (in_len >= ctx->rate_bytes) {
143     for (size_t i = 0; i < rate_words; i++) {
144       ctx->state[i] ^= CRYPTO_load_u64_le(in + 8 * i);
145     }
146     keccak_f(ctx->state);
147     in += ctx->rate_bytes;
148     in_len -= ctx->rate_bytes;
149   }
150 
151   // XOR the final block. Accessing |ctx->state| as a |uint8_t*| is allowed by
152   // strict aliasing because we require |uint8_t| to be a character type.
153   uint8_t *state_bytes = (uint8_t *)ctx->state;
154   assert(in_len < ctx->rate_bytes);
155   for (size_t i = 0; i < in_len; i++) {
156     state_bytes[i] ^= in[i];
157   }
158   state_bytes[in_len] ^= terminator;
159   state_bytes[ctx->rate_bytes - 1] ^= 0x80;
160   keccak_f(ctx->state);
161 }
162 
BORINGSSL_keccak(uint8_t * out,size_t out_len,const uint8_t * in,size_t in_len,enum boringssl_keccak_config_t config)163 void BORINGSSL_keccak(uint8_t *out, size_t out_len, const uint8_t *in,
164                       size_t in_len, enum boringssl_keccak_config_t config) {
165   struct BORINGSSL_keccak_st ctx;
166   size_t required_out_len;
167   keccak_init(&ctx, &required_out_len, in, in_len, config);
168   if (required_out_len != 0 && out_len != required_out_len) {
169     abort();
170   }
171   BORINGSSL_keccak_squeeze(&ctx, out, out_len);
172 }
173 
BORINGSSL_keccak_init(struct BORINGSSL_keccak_st * ctx,const uint8_t * in,size_t in_len,enum boringssl_keccak_config_t config)174 void BORINGSSL_keccak_init(struct BORINGSSL_keccak_st *ctx, const uint8_t *in,
175                            size_t in_len,
176                            enum boringssl_keccak_config_t config) {
177   size_t required_out_len;
178   keccak_init(ctx, &required_out_len, in, in_len, config);
179   if (required_out_len != 0) {
180     abort();
181   }
182 }
183 
BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st * ctx,uint8_t * out,size_t out_len)184 void BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st *ctx, uint8_t *out,
185                               size_t out_len) {
186   // Accessing |ctx->state| as a |uint8_t*| is allowed by strict aliasing
187   // because we require |uint8_t| to be a character type.
188   const uint8_t *state_bytes = (const uint8_t *)ctx->state;
189   while (out_len) {
190     size_t remaining = ctx->rate_bytes - ctx->offset;
191     size_t todo = out_len;
192     if (todo > remaining) {
193       todo = remaining;
194     }
195     OPENSSL_memcpy(out, &state_bytes[ctx->offset], todo);
196     out += todo;
197     out_len -= todo;
198     ctx->offset += todo;
199     if (ctx->offset == ctx->rate_bytes) {
200       keccak_f(ctx->state);
201       ctx->offset = 0;
202     }
203   }
204 }
205