|
19 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | 20 | */ |
21 | 21 |
|
| 22 | +/* Generated by wolfSSL/scripts: |
| 23 | + * ruby sp/sp.rb ARM32 \ |
| 24 | + * ../wolfssl/wolfcrypt/src/sp_arm32 |
| 25 | + */ |
| 26 | + |
22 | 27 | /* Implementation by Sean Parkinson. */ |
23 | 28 |
|
24 | 29 | #define WC_FIPS_LL_CRYPTO |
|
166 | 171 | #if defined(WOLFSSL_HAVE_SP_ECC) && defined(WOLFSSL_SP_NONBLOCK) |
167 | 172 | /* Conditionally copy len bytes from a to r when copy is 1, in constant time. |
168 | 173 | * Reads r, so r must be initialized. Both candidates are always touched, so |
169 | | - * which one was selected is not observable. */ |
| 174 | + * which one was selected is not observable. All callers pass sp_digit aligned |
| 175 | + * buffers a whole number of sp_digit wide (sp_digit arrays or sp_point structs), |
| 176 | + * so the copy runs a word at a time rather than a byte at a time. The words are |
| 177 | + * loaded and stored with XMEMCPY through a byte pointer, so a struct member that |
| 178 | + * is not an sp_digit (such as sp_point's int infinity) is never accessed through |
| 179 | + * an sp_digit lvalue - that would break strict aliasing and could leave an |
| 180 | + * optimiser using a stale value. */ |
170 | 181 | WC_MAYBE_UNUSED static void sp_cond_memcpy(void* r, const void* a, int copy, |
171 | 182 | size_t len) |
172 | 183 | { |
173 | 184 | byte* rb = (byte*)r; |
174 | 185 | const byte* ab = (const byte*)a; |
175 | 186 | /* Read the mask back through a volatile so the compiler cannot tell it |
176 | | - * is only ever 0 or 0xff. Knowing that, it may turn the masking into a |
| 187 | + * is only ever 0 or all-ones. Knowing that, it may turn the masking into a |
177 | 188 | * branch on copy, e.g. by unswitching the loop, which leaks copy. */ |
178 | | - volatile byte vmask = (byte)(0U - (unsigned int)(copy != 0)); |
179 | | - byte mask = vmask; |
| 189 | + volatile sp_digit vmask = (sp_digit)0 - (sp_digit)(copy != 0); |
| 190 | + sp_digit mask = vmask; |
180 | 191 | size_t i; |
181 | 192 |
|
182 | | - for (i = 0; i < len; i++) { |
183 | | - rb[i] ^= (byte)((rb[i] ^ ab[i]) & mask); |
| 193 | + for (i = 0; i < len; i += sizeof(sp_digit)) { |
| 194 | + sp_digit rw; |
| 195 | + sp_digit aw; |
| 196 | + XMEMCPY(&rw, rb + i, sizeof(sp_digit)); |
| 197 | + XMEMCPY(&aw, ab + i, sizeof(sp_digit)); |
| 198 | + rw ^= (rw ^ aw) & mask; |
| 199 | + XMEMCPY(rb + i, &rw, sizeof(sp_digit)); |
184 | 200 | } |
185 | 201 | } |
186 | 202 |
|
187 | 203 | /* Set r to a when sel is 0 and to b when sel is 1, in constant time. |
188 | | - * Writes r without reading it, so r may be an uninitialized scratch buffer. */ |
| 204 | + * Writes r without reading it, so r may be an uninitialized scratch buffer. |
| 205 | + * As with sp_cond_memcpy, all callers pass sp_digit aligned buffers a whole |
| 206 | + * number of sp_digit wide, so the select runs a word at a time, loading and |
| 207 | + * storing through a byte pointer with XMEMCPY to avoid aliasing a non-sp_digit |
| 208 | + * struct member (such as sp_point's int infinity) through an sp_digit lvalue. */ |
189 | 209 | WC_MAYBE_UNUSED static void sp_cond_select(void* r, const void* a, |
190 | 210 | const void* b, int sel, size_t len) |
191 | 211 | { |
192 | 212 | byte* rb = (byte*)r; |
193 | 213 | const byte* ab = (const byte*)a; |
194 | 214 | const byte* bb = (const byte*)b; |
195 | 215 | /* Read the mask back through a volatile so the compiler cannot tell it |
196 | | - * is only ever 0 or 0xff. Knowing that, it may turn the masking into a |
| 216 | + * is only ever 0 or all-ones. Knowing that, it may turn the masking into a |
197 | 217 | * branch on sel, e.g. by unswitching the loop, which leaks sel. */ |
198 | | - volatile byte vmask = (byte)(0U - (unsigned int)(sel != 0)); |
199 | | - byte mask = vmask; |
| 218 | + volatile sp_digit vmask = (sp_digit)0 - (sp_digit)(sel != 0); |
| 219 | + sp_digit mask = vmask; |
200 | 220 | size_t i; |
201 | 221 |
|
202 | | - for (i = 0; i < len; i++) { |
203 | | - rb[i] = (byte)((ab[i] & (byte)~mask) | (bb[i] & mask)); |
| 222 | + for (i = 0; i < len; i += sizeof(sp_digit)) { |
| 223 | + sp_digit aw; |
| 224 | + sp_digit bw; |
| 225 | + sp_digit rw; |
| 226 | + XMEMCPY(&aw, ab + i, sizeof(sp_digit)); |
| 227 | + XMEMCPY(&bw, bb + i, sizeof(sp_digit)); |
| 228 | + rw = (aw & ~mask) | (bw & mask); |
| 229 | + XMEMCPY(rb + i, &rw, sizeof(sp_digit)); |
204 | 230 | } |
205 | 231 | } |
206 | 232 | #endif /* WOLFSSL_HAVE_SP_ECC && WOLFSSL_SP_NONBLOCK */ |
@@ -76887,8 +76913,14 @@ static int sp_256_ecc_mulmod_8_nb(sp_ecc_ctx_t* sp_ctx, sp_point_256* r, |
76887 | 76913 | ctx->state = 5; |
76888 | 76914 | break; |
76889 | 76915 | case 5: /* ADD */ |
76890 | | - sp_256_proj_point_add_8(&ctx->t[ctx->y^1], &ctx->t[0], &ctx->t[1], |
76891 | | - ctx->tmp); |
| 76916 | + /* Address-oblivious: sum -> scratch t[2], moved into t[y^1] by touching |
| 76917 | + * both t[0] and t[1]; t[y] copied into t[2] for the double. */ |
| 76918 | + sp_256_proj_point_add_8(&ctx->t[2], &ctx->t[0], |
| 76919 | + &ctx->t[1], ctx->tmp); |
| 76920 | + sp_cond_memcpy(&ctx->t[0], &ctx->t[2], (ctx->y), |
| 76921 | + sizeof(sp_point_256)); |
| 76922 | + sp_cond_memcpy(&ctx->t[1], &ctx->t[2], (ctx->y)^1, |
| 76923 | + sizeof(sp_point_256)); |
76892 | 76924 | sp_cond_select(&ctx->t[2], &ctx->t[0], &ctx->t[1], (ctx->y), |
76893 | 76925 | sizeof(sp_point_256)); |
76894 | 76926 | ctx->state = 6; |
@@ -95396,8 +95428,14 @@ static int sp_384_ecc_mulmod_12_nb(sp_ecc_ctx_t* sp_ctx, sp_point_384* r, |
95396 | 95428 | ctx->state = 5; |
95397 | 95429 | break; |
95398 | 95430 | case 5: /* ADD */ |
95399 | | - sp_384_proj_point_add_12(&ctx->t[ctx->y^1], &ctx->t[0], &ctx->t[1], |
95400 | | - ctx->tmp); |
| 95431 | + /* Address-oblivious: sum -> scratch t[2], moved into t[y^1] by touching |
| 95432 | + * both t[0] and t[1]; t[y] copied into t[2] for the double. */ |
| 95433 | + sp_384_proj_point_add_12(&ctx->t[2], &ctx->t[0], |
| 95434 | + &ctx->t[1], ctx->tmp); |
| 95435 | + sp_cond_memcpy(&ctx->t[0], &ctx->t[2], (ctx->y), |
| 95436 | + sizeof(sp_point_384)); |
| 95437 | + sp_cond_memcpy(&ctx->t[1], &ctx->t[2], (ctx->y)^1, |
| 95438 | + sizeof(sp_point_384)); |
95401 | 95439 | sp_cond_select(&ctx->t[2], &ctx->t[0], &ctx->t[1], (ctx->y), |
95402 | 95440 | sizeof(sp_point_384)); |
95403 | 95441 | ctx->state = 6; |
@@ -123055,8 +123093,14 @@ static int sp_521_ecc_mulmod_17_nb(sp_ecc_ctx_t* sp_ctx, sp_point_521* r, |
123055 | 123093 | ctx->state = 5; |
123056 | 123094 | break; |
123057 | 123095 | case 5: /* ADD */ |
123058 | | - sp_521_proj_point_add_17(&ctx->t[ctx->y^1], &ctx->t[0], &ctx->t[1], |
123059 | | - ctx->tmp); |
| 123096 | + /* Address-oblivious: sum -> scratch t[2], moved into t[y^1] by touching |
| 123097 | + * both t[0] and t[1]; t[y] copied into t[2] for the double. */ |
| 123098 | + sp_521_proj_point_add_17(&ctx->t[2], &ctx->t[0], |
| 123099 | + &ctx->t[1], ctx->tmp); |
| 123100 | + sp_cond_memcpy(&ctx->t[0], &ctx->t[2], (ctx->y), |
| 123101 | + sizeof(sp_point_521)); |
| 123102 | + sp_cond_memcpy(&ctx->t[1], &ctx->t[2], (ctx->y)^1, |
| 123103 | + sizeof(sp_point_521)); |
123060 | 123104 | sp_cond_select(&ctx->t[2], &ctx->t[0], &ctx->t[1], (ctx->y), |
123061 | 123105 | sizeof(sp_point_521)); |
123062 | 123106 | ctx->state = 6; |
|
0 commit comments