2022-08-09 12:45:47 +02:00
|
|
|
/*
|
2022-08-19 13:09:17 +02:00
|
|
|
* Core bignum functions
|
2022-08-09 12:45:47 +02:00
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* 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 "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
2022-09-12 16:25:24 +02:00
|
|
|
#include "constant_time_internal.h"
|
2022-08-09 12:45:47 +02:00
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
|
|
#include "bignum_core.h"
|
2022-08-24 12:08:51 +02:00
|
|
|
#include "bn_mul.h"
|
|
|
|
#include "constant_time_internal.h"
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-04-27 15:20:30 +02:00
|
|
|
size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
|
|
|
|
{
|
|
|
|
#if defined(__has_builtin)
|
2023-07-17 16:00:19 +02:00
|
|
|
#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz)
|
|
|
|
#define core_clz __builtin_clz
|
|
|
|
#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_clzl)
|
|
|
|
#define core_clz __builtin_clzl
|
|
|
|
#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_clzll)
|
|
|
|
#define core_clz __builtin_clzll
|
2023-04-27 15:20:30 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-07-17 16:00:19 +02:00
|
|
|
#if defined(core_clz)
|
|
|
|
return (size_t) core_clz(a);
|
2023-07-12 12:15:17 +02:00
|
|
|
#else
|
2023-04-27 15:20:30 +02:00
|
|
|
size_t j;
|
|
|
|
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
|
|
|
|
|
|
|
|
for (j = 0; j < biL; j++) {
|
|
|
|
if (a & mask) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return j;
|
2023-07-12 12:15:17 +02:00
|
|
|
#endif
|
2023-04-27 15:20:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2023-04-20 10:50:31 +02:00
|
|
|
int i;
|
|
|
|
size_t j;
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-04-25 18:34:59 +02:00
|
|
|
for (i = ((int) A_limbs) - 1; i >= 0; i--) {
|
|
|
|
if (A[i] != 0) {
|
|
|
|
j = biL - mbedtls_mpi_core_clz(A[i]);
|
|
|
|
return (i * biL) + j;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-04-20 10:50:31 +02:00
|
|
|
return 0;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
|
|
|
* into the storage form used by mbedtls_mpi. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
uint8_t i;
|
2022-08-19 13:24:40 +02:00
|
|
|
unsigned char *a_ptr;
|
2022-08-09 12:45:47 +02:00
|
|
|
mbedtls_mpi_uint tmp = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
|
2022-08-09 12:45:47 +02:00
|
|
|
tmp <<= CHAR_BIT;
|
2022-08-19 13:24:40 +02:00
|
|
|
tmp |= (mbedtls_mpi_uint) *a_ptr;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return tmp;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (MBEDTLS_IS_BIG_ENDIAN) {
|
2022-11-28 15:38:53 +01:00
|
|
|
/* Nothing to do on bigendian systems. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
switch (sizeof(mbedtls_mpi_uint)) {
|
2022-11-28 15:38:53 +01:00
|
|
|
case 4:
|
2023-01-11 14:50:10 +01:00
|
|
|
return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
|
2022-11-28 15:38:53 +01:00
|
|
|
case 8:
|
2023-01-11 14:50:10 +01:00
|
|
|
return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
|
2022-11-28 15:38:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall back to C-based reordering if we don't know the byte order
|
2023-01-11 14:50:10 +01:00
|
|
|
* or we couldn't use a compiler-specific builtin. */
|
|
|
|
return mpi_bigendian_to_host_c(a);
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
|
|
|
|
size_t A_limbs)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *cur_limb_left;
|
|
|
|
mbedtls_mpi_uint *cur_limb_right;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (A_limbs == 0) {
|
2022-08-09 12:45:47 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse limbs and
|
|
|
|
* - adapt byte-order in each limb
|
|
|
|
* - swap the limbs themselves.
|
|
|
|
* For that, simultaneously traverse the limbs from left to right
|
|
|
|
* and from right to left, as long as the left index is not bigger
|
|
|
|
* than the right index (it's not a problem if limbs is odd and the
|
|
|
|
* indices coincide in the last iteration).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
|
2022-08-09 12:45:47 +02:00
|
|
|
cur_limb_left <= cur_limb_right;
|
2023-01-11 14:50:10 +01:00
|
|
|
cur_limb_left++, cur_limb_right--) {
|
2022-08-09 12:45:47 +02:00
|
|
|
mbedtls_mpi_uint tmp;
|
|
|
|
/* Note that if cur_limb_left == cur_limb_right,
|
|
|
|
* this code effectively swaps the bytes only once. */
|
2023-01-11 14:50:10 +01:00
|
|
|
tmp = mpi_bigendian_to_host(*cur_limb_left);
|
|
|
|
*cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
|
2022-08-09 12:45:47 +02:00
|
|
|
*cur_limb_right = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:38:35 +02:00
|
|
|
/* Whether min <= A, in constant time.
|
|
|
|
* A_limbs must be at least 1. */
|
2023-05-17 15:00:39 +02:00
|
|
|
mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
size_t A_limbs)
|
2022-09-20 18:38:35 +02:00
|
|
|
{
|
|
|
|
/* min <= least significant limb? */
|
2023-08-10 12:58:18 +02:00
|
|
|
mbedtls_ct_condition_t min_le_lsl = mbedtls_ct_uint_ge(A[0], min);
|
2022-09-20 18:38:35 +02:00
|
|
|
|
2022-12-15 15:04:33 +01:00
|
|
|
/* limbs other than the least significant one are all zero? */
|
2023-05-17 15:00:39 +02:00
|
|
|
mbedtls_ct_condition_t msll_mask = MBEDTLS_CT_FALSE;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 1; i < A_limbs; i++) {
|
2023-05-17 15:00:39 +02:00
|
|
|
msll_mask = mbedtls_ct_bool_or(msll_mask, mbedtls_ct_bool(A[i]));
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-20 18:38:35 +02:00
|
|
|
|
|
|
|
/* min <= A iff the lowest limb of A is >= min or the other limbs
|
|
|
|
* are not all zero. */
|
2023-05-17 15:00:39 +02:00
|
|
|
return mbedtls_ct_bool_or(msll_mask, min_le_lsl);
|
2022-09-20 18:38:35 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 16:16:22 +02:00
|
|
|
mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs)
|
2023-05-09 15:01:05 +02:00
|
|
|
{
|
2023-05-17 16:16:22 +02:00
|
|
|
mbedtls_ct_condition_t ret = MBEDTLS_CT_FALSE, cond = MBEDTLS_CT_FALSE, done = MBEDTLS_CT_FALSE;
|
2023-05-09 15:01:05 +02:00
|
|
|
|
|
|
|
for (size_t i = limbs; i > 0; i--) {
|
|
|
|
/*
|
|
|
|
* If B[i - 1] < A[i - 1] then A < B is false and the result must
|
|
|
|
* remain 0.
|
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
2023-08-10 12:58:18 +02:00
|
|
|
cond = mbedtls_ct_uint_lt(B[i - 1], A[i - 1]);
|
2023-05-17 16:16:22 +02:00
|
|
|
done = mbedtls_ct_bool_or(done, cond);
|
2023-05-09 15:01:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If A[i - 1] < B[i - 1] then A < B is true.
|
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
2023-08-10 12:58:18 +02:00
|
|
|
cond = mbedtls_ct_uint_lt(A[i - 1], B[i - 1]);
|
2023-05-17 16:16:22 +02:00
|
|
|
ret = mbedtls_ct_bool_or(ret, mbedtls_ct_bool_and(cond, mbedtls_ct_bool_not(done)));
|
|
|
|
done = mbedtls_ct_bool_or(done, cond);
|
2023-05-09 15:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If all the limbs were equal, then the numbers are equal, A < B is false
|
|
|
|
* and leaving the result 0 is correct.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
2022-09-20 18:38:35 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
size_t limbs,
|
2023-05-17 14:31:55 +02:00
|
|
|
mbedtls_ct_condition_t assign)
|
2022-09-12 16:25:24 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X == A) {
|
2022-10-10 14:26:57 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-10-10 14:26:57 +02:00
|
|
|
|
2023-05-18 15:41:06 +02:00
|
|
|
/* This function is very performance-sensitive for RSA. For this reason
|
|
|
|
* we have the loop below, instead of calling mbedtls_ct_memcpy_if
|
|
|
|
* (this is more optimal since here we don't have to handle the case where
|
|
|
|
* we copy awkwardly sized data).
|
|
|
|
*/
|
|
|
|
for (size_t i = 0; i < limbs; i++) {
|
|
|
|
X[i] = mbedtls_ct_mpi_uint_if(assign, A[i], X[i]);
|
|
|
|
}
|
2022-09-12 16:25:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
|
|
|
|
mbedtls_mpi_uint *Y,
|
|
|
|
size_t limbs,
|
2023-05-17 14:31:55 +02:00
|
|
|
mbedtls_ct_condition_t swap)
|
2022-09-12 16:25:24 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X == Y) {
|
2022-10-10 14:26:57 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-10-10 14:26:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < limbs; i++) {
|
2022-09-30 13:54:02 +02:00
|
|
|
mbedtls_mpi_uint tmp = X[i];
|
2023-05-17 14:31:55 +02:00
|
|
|
X[i] = mbedtls_ct_mpi_uint_if(swap, Y[i], X[i]);
|
|
|
|
Y[i] = mbedtls_ct_mpi_uint_if(swap, tmp, Y[i]);
|
2022-09-12 16:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
|
|
|
|
size_t X_limbs,
|
|
|
|
const unsigned char *input,
|
|
|
|
size_t input_length)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS(input_length);
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X_limbs < limbs) {
|
|
|
|
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2022-08-11 17:13:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X != NULL) {
|
|
|
|
memset(X, 0, X_limbs * ciL);
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < input_length; i++) {
|
|
|
|
size_t offset = ((i % ciL) << 3);
|
|
|
|
X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
|
2022-08-19 13:05:28 +02:00
|
|
|
}
|
2022-08-12 14:11:56 +02:00
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
|
|
|
|
size_t X_limbs,
|
|
|
|
const unsigned char *input,
|
|
|
|
size_t input_length)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS(input_length);
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X_limbs < limbs) {
|
|
|
|
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2022-08-11 17:13:53 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
/* If X_limbs is 0, input_length must also be 0 (from previous test).
|
|
|
|
* Nothing to do. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X_limbs == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(X, 0, X_limbs * ciL);
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-12 17:47:39 +02:00
|
|
|
/* memcpy() with (NULL, 0) is undefined behaviour */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_length != 0) {
|
|
|
|
size_t overhead = (X_limbs * ciL) - input_length;
|
2022-08-12 17:47:39 +02:00
|
|
|
unsigned char *Xp = (unsigned char *) X;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(Xp + overhead, input, input_length);
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
|
2022-08-12 17:47:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
|
|
|
|
size_t A_limbs,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_length)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t stored_bytes = A_limbs * ciL;
|
2022-08-09 12:45:47 +02:00
|
|
|
size_t bytes_to_copy;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (stored_bytes < output_length) {
|
2022-08-09 12:45:47 +02:00
|
|
|
bytes_to_copy = stored_bytes;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-08-19 13:24:40 +02:00
|
|
|
bytes_to_copy = output_length;
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2022-08-22 10:06:32 +02:00
|
|
|
/* The output buffer is smaller than the allocated size of A.
|
2022-08-19 13:24:40 +02:00
|
|
|
* However A may fit if its leading bytes are zero. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
|
|
|
|
if (GET_BYTE(A, i) != 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < bytes_to_copy; i++) {
|
|
|
|
output[i] = GET_BYTE(A, i);
|
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (stored_bytes < output_length) {
|
2022-08-09 12:45:47 +02:00
|
|
|
/* Write trailing 0 bytes */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output + stored_bytes, 0, output_length - stored_bytes);
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
|
|
|
|
size_t X_limbs,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_length)
|
2022-08-09 12:45:47 +02:00
|
|
|
{
|
|
|
|
size_t stored_bytes;
|
|
|
|
size_t bytes_to_copy;
|
|
|
|
unsigned char *p;
|
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
stored_bytes = X_limbs * ciL;
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (stored_bytes < output_length) {
|
2022-08-22 10:06:32 +02:00
|
|
|
/* There is enough space in the output buffer. Write initial
|
2022-08-09 12:45:47 +02:00
|
|
|
* null bytes and record the position at which to start
|
|
|
|
* writing the significant bytes. In this case, the execution
|
|
|
|
* trace of this function does not depend on the value of the
|
|
|
|
* number. */
|
|
|
|
bytes_to_copy = stored_bytes;
|
2022-08-19 13:24:40 +02:00
|
|
|
p = output + output_length - stored_bytes;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, output_length - stored_bytes);
|
|
|
|
} else {
|
2022-08-22 10:06:32 +02:00
|
|
|
/* The output buffer is smaller than the allocated size of X.
|
2022-08-09 12:45:47 +02:00
|
|
|
* However X may fit if its leading bytes are zero. */
|
2022-08-19 13:24:40 +02:00
|
|
|
bytes_to_copy = output_length;
|
|
|
|
p = output;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
|
|
|
|
if (GET_BYTE(X, i) != 0) {
|
|
|
|
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < bytes_to_copy; i++) {
|
|
|
|
p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
|
|
|
|
}
|
2022-08-09 12:45:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-09 12:45:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
|
|
|
|
size_t count)
|
2022-09-21 15:38:38 +02:00
|
|
|
{
|
|
|
|
size_t i, v0, v1;
|
|
|
|
mbedtls_mpi_uint r0 = 0, r1;
|
|
|
|
|
|
|
|
v0 = count / biL;
|
|
|
|
v1 = count & (biL - 1);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (v0 > limbs || (v0 == limbs && v1 > 0)) {
|
|
|
|
memset(X, 0, limbs * ciL);
|
2022-09-21 15:38:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shift by count / limb_size
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (v0 > 0) {
|
|
|
|
for (i = 0; i < limbs - v0; i++) {
|
2022-09-21 15:38:38 +02:00
|
|
|
X[i] = X[i + v0];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-21 15:38:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; i < limbs; i++) {
|
2022-09-21 15:38:38 +02:00
|
|
|
X[i] = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-21 15:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shift by count % limb_size
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (v1 > 0) {
|
|
|
|
for (i = limbs; i > 0; i--) {
|
2022-09-21 15:38:38 +02:00
|
|
|
r1 = X[i - 1] << (biL - v1);
|
|
|
|
X[i - 1] >>= v1;
|
|
|
|
X[i - 1] |= r0;
|
|
|
|
r0 = r1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 15:22:16 +02:00
|
|
|
void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
|
|
|
|
size_t count)
|
2023-04-20 13:18:41 +02:00
|
|
|
{
|
2023-04-20 15:22:16 +02:00
|
|
|
size_t i, v0, v1;
|
2023-04-20 13:18:41 +02:00
|
|
|
mbedtls_mpi_uint r0 = 0, r1;
|
|
|
|
|
2023-04-20 15:22:16 +02:00
|
|
|
v0 = count / (biL);
|
|
|
|
v1 = count & (biL - 1);
|
2023-04-20 13:18:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* shift by count / limb_size
|
|
|
|
*/
|
2023-04-20 15:22:16 +02:00
|
|
|
if (v0 > 0) {
|
|
|
|
for (i = limbs; i > v0; i--) {
|
|
|
|
X[i - 1] = X[i - v0 - 1];
|
|
|
|
}
|
2023-04-20 13:18:41 +02:00
|
|
|
|
2023-04-20 15:22:16 +02:00
|
|
|
for (; i > 0; i--) {
|
|
|
|
X[i - 1] = 0;
|
|
|
|
}
|
2023-04-20 13:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shift by count % limb_size
|
|
|
|
*/
|
2023-04-20 15:22:16 +02:00
|
|
|
if (v1 > 0) {
|
|
|
|
for (i = v0; i < limbs; i++) {
|
|
|
|
r1 = X[i] >> (biL - v1);
|
|
|
|
X[i] <<= v1;
|
|
|
|
X[i] |= r0;
|
2023-04-20 13:18:41 +02:00
|
|
|
r0 = r1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs)
|
2022-08-24 13:54:36 +02:00
|
|
|
{
|
2022-08-24 15:05:26 +02:00
|
|
|
mbedtls_mpi_uint c = 0;
|
2022-09-21 15:38:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < limbs; i++) {
|
2022-08-24 15:05:26 +02:00
|
|
|
mbedtls_mpi_uint t = c + A[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
c = (t < A[i]);
|
2022-08-24 15:05:26 +02:00
|
|
|
t += B[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
c += (t < B[i]);
|
2022-08-24 15:05:26 +02:00
|
|
|
X[i] = t;
|
2022-08-24 13:54:36 +02:00
|
|
|
}
|
2022-08-24 15:05:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return c;
|
2022-08-24 13:54:36 +02:00
|
|
|
}
|
2022-09-21 15:38:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
size_t limbs,
|
|
|
|
unsigned cond)
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 16:15:02 +02:00
|
|
|
mbedtls_mpi_uint c = 0;
|
|
|
|
|
2023-05-17 16:17:12 +02:00
|
|
|
mbedtls_ct_condition_t do_add = mbedtls_ct_bool(cond);
|
2022-08-30 18:41:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < limbs; i++) {
|
2023-08-10 13:11:31 +02:00
|
|
|
mbedtls_mpi_uint add = mbedtls_ct_mpi_uint_if_else_0(do_add, A[i]);
|
2022-09-15 16:55:07 +02:00
|
|
|
mbedtls_mpi_uint t = c + X[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
c = (t < X[i]);
|
2022-08-30 16:15:02 +02:00
|
|
|
t += add;
|
2023-01-11 14:50:10 +01:00
|
|
|
c += (t < add);
|
2022-09-15 16:55:07 +02:00
|
|
|
X[i] = t;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
2022-08-30 16:15:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return c;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs)
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 12:57:22 +02:00
|
|
|
mbedtls_mpi_uint c = 0;
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < limbs; i++) {
|
|
|
|
mbedtls_mpi_uint z = (A[i] < c);
|
2022-08-30 12:57:22 +02:00
|
|
|
mbedtls_mpi_uint t = A[i] - c;
|
2023-01-11 14:50:10 +01:00
|
|
|
c = (t < B[i]) + z;
|
2022-08-30 12:57:22 +02:00
|
|
|
X[i] = t - B[i];
|
|
|
|
}
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return c;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
|
|
|
|
const mbedtls_mpi_uint *s, size_t s_len,
|
|
|
|
mbedtls_mpi_uint b)
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint c = 0; /* carry */
|
2022-08-30 15:31:49 +02:00
|
|
|
/*
|
|
|
|
* It is a documented precondition of this function that d_len >= s_len.
|
|
|
|
* If that's not the case, we swap these round: this turns what would be
|
|
|
|
* a buffer overflow into an incorrect result.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (d_len < s_len) {
|
2022-08-24 12:08:51 +02:00
|
|
|
s_len = d_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-24 12:08:51 +02:00
|
|
|
size_t excess_len = d_len - s_len;
|
|
|
|
size_t steps_x8 = s_len / 8;
|
|
|
|
size_t steps_x1 = s_len & 7;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (steps_x8--) {
|
2022-08-24 12:08:51 +02:00
|
|
|
MULADDC_X8_INIT
|
|
|
|
MULADDC_X8_CORE
|
2023-01-11 14:50:10 +01:00
|
|
|
MULADDC_X8_STOP
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (steps_x1--) {
|
2022-08-24 12:08:51 +02:00
|
|
|
MULADDC_X1_INIT
|
|
|
|
MULADDC_X1_CORE
|
2023-01-11 14:50:10 +01:00
|
|
|
MULADDC_X1_STOP
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (excess_len--) {
|
2022-08-30 16:15:02 +02:00
|
|
|
*d += c;
|
2023-01-11 14:50:10 +01:00
|
|
|
c = (*d < c);
|
2022-08-30 16:15:02 +02:00
|
|
|
d++;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return c;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 17:40:55 +02:00
|
|
|
void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A, size_t A_limbs,
|
|
|
|
const mbedtls_mpi_uint *B, size_t B_limbs)
|
2022-08-24 17:17:52 +02:00
|
|
|
{
|
2022-08-24 17:40:55 +02:00
|
|
|
memset(X, 0, (A_limbs + B_limbs) * ciL);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < B_limbs; i++) {
|
|
|
|
(void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
|
|
|
|
}
|
2022-08-24 17:17:52 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 12:57:22 +02:00
|
|
|
/*
|
|
|
|
* Fast Montgomery initialization (thanks to Tom St Denis).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2022-08-30 12:57:22 +02:00
|
|
|
mbedtls_mpi_uint x = N[0];
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
x += ((N[0] + 2) & 4) << 1;
|
2022-08-24 12:08:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (unsigned int i = biL; i >= 8; i /= 2) {
|
|
|
|
x *= (2 - (N[0] * x));
|
|
|
|
}
|
2022-08-30 12:57:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ~x + 1;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t B_limbs,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t AN_limbs,
|
|
|
|
mbedtls_mpi_uint mm,
|
|
|
|
mbedtls_mpi_uint *T)
|
2022-08-24 12:08:51 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(T, 0, (2 * AN_limbs + 1) * ciL);
|
2022-08-30 12:57:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < AN_limbs; i++) {
|
2022-08-30 12:57:22 +02:00
|
|
|
/* T = (T + u0*B + u1*N) / 2^biL */
|
2022-08-31 18:57:34 +02:00
|
|
|
mbedtls_mpi_uint u0 = A[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
|
2022-08-30 12:57:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
|
|
|
|
(void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
|
2022-09-02 14:28:59 +02:00
|
|
|
|
|
|
|
T++;
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
2022-08-30 12:57:22 +02:00
|
|
|
|
2022-08-31 18:57:34 +02:00
|
|
|
/*
|
|
|
|
* The result we want is (T >= N) ? T - N : T.
|
|
|
|
*
|
|
|
|
* For better constant-time properties in this function, we always do the
|
|
|
|
* subtraction, with the result in X.
|
|
|
|
*
|
|
|
|
* We also look to see if there was any carry in the final additions in the
|
|
|
|
* loop above.
|
|
|
|
*/
|
2022-08-30 12:57:22 +02:00
|
|
|
|
|
|
|
mbedtls_mpi_uint carry = T[AN_limbs];
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
|
2022-08-30 12:57:22 +02:00
|
|
|
|
|
|
|
/*
|
2022-08-31 18:57:34 +02:00
|
|
|
* Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-08-31 18:57:34 +02:00
|
|
|
* T can be in one of 3 ranges:
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-08-31 18:57:34 +02:00
|
|
|
* 1) T < N : (carry, borrow) = (0, 1): we want T
|
|
|
|
* 2) N <= T < R : (carry, borrow) = (0, 0): we want X
|
|
|
|
* 3) T >= R : (carry, borrow) = (1, 1): we want X
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-08-31 18:57:34 +02:00
|
|
|
* and (carry, borrow) = (1, 0) can't happen.
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
|
|
|
* So the correct return value is already in X if (carry ^ borrow) = 0,
|
|
|
|
* but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
|
|
|
|
*/
|
2023-05-17 16:13:14 +02:00
|
|
|
mbedtls_ct_memcpy_if(mbedtls_ct_bool(carry ^ borrow),
|
|
|
|
(unsigned char *) X,
|
|
|
|
(unsigned char *) T,
|
|
|
|
NULL,
|
|
|
|
AN_limbs * sizeof(mbedtls_mpi_uint));
|
2022-08-24 12:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *N)
|
2022-08-11 18:29:32 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
|
2022-08-11 18:29:32 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-08-11 18:29:32 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 19:13:43 +02:00
|
|
|
MBEDTLS_STATIC_TESTABLE
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
|
|
|
|
const mbedtls_mpi_uint *table,
|
|
|
|
size_t limbs,
|
|
|
|
size_t count,
|
|
|
|
size_t index)
|
2022-10-26 16:14:33 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < count; i++, table += limbs) {
|
2023-08-10 12:58:18 +02:00
|
|
|
mbedtls_ct_condition_t assign = mbedtls_ct_uint_eq(i, index);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
|
2022-10-26 16:14:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 21:00:00 +02:00
|
|
|
/* Fill X with n_bytes random bytes.
|
|
|
|
* X must already have room for those bytes.
|
|
|
|
* The ordering of the bytes returned from the RNG is suitable for
|
2022-10-27 20:15:13 +02:00
|
|
|
* deterministic ECDSA (see RFC 6979 §3.3 and the specification of
|
|
|
|
* mbedtls_mpi_core_random()).
|
2022-09-09 21:00:00 +02:00
|
|
|
*/
|
|
|
|
int mbedtls_mpi_core_fill_random(
|
|
|
|
mbedtls_mpi_uint *X, size_t X_limbs,
|
|
|
|
size_t n_bytes,
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2022-09-09 21:00:00 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t limbs = CHARS_TO_LIMBS(n_bytes);
|
|
|
|
const size_t overhead = (limbs * ciL) - n_bytes;
|
2022-09-09 21:00:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X_limbs < limbs) {
|
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-09-09 21:00:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(X, 0, overhead);
|
|
|
|
memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
|
|
|
|
MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
|
|
|
|
mbedtls_mpi_core_bigendian_to_host(X, limbs);
|
2022-09-09 21:00:00 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-09-09 21:00:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
|
|
|
|
mbedtls_mpi_uint min,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t limbs,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2022-09-21 15:47:23 +02:00
|
|
|
{
|
2023-05-17 16:17:29 +02:00
|
|
|
mbedtls_ct_condition_t ge_lower = MBEDTLS_CT_TRUE, lt_upper = MBEDTLS_CT_FALSE;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
|
|
|
|
size_t n_bytes = (n_bits + 7) / 8;
|
2022-09-21 15:47:23 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When min == 0, each try has at worst a probability 1/2 of failing
|
|
|
|
* (the msb has a probability 1/2 of being 0, and then the result will
|
|
|
|
* be < N), so after 30 tries failure probability is a most 2**(-30).
|
|
|
|
*
|
|
|
|
* When N is just below a power of 2, as is the case when generating
|
|
|
|
* a random scalar on most elliptic curves, 1 try is enough with
|
|
|
|
* overwhelming probability. When N is just above a power of 2,
|
|
|
|
* as when generating a random scalar on secp224k1, each try has
|
|
|
|
* a probability of failing that is almost 1/2.
|
|
|
|
*
|
|
|
|
* The probabilities are almost the same if min is nonzero but negligible
|
|
|
|
* compared to N. This is always the case when N is crypto-sized, but
|
|
|
|
* it's convenient to support small N for testing purposes. When N
|
|
|
|
* is small, use a higher repeat count, otherwise the probability of
|
|
|
|
* failure is macroscopic.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int count = (n_bytes > 4 ? 30 : 250);
|
2022-09-21 15:47:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
|
|
|
|
* when f_rng is a suitably parametrized instance of HMAC_DRBG:
|
|
|
|
* - use the same byte ordering;
|
|
|
|
* - keep the leftmost n_bits bits of the generated octet string;
|
|
|
|
* - try until result is in the desired range.
|
|
|
|
* This also avoids any bias, which is especially important for ECDSA.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
|
|
|
|
n_bytes,
|
|
|
|
f_rng, p_rng));
|
|
|
|
mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
|
|
|
|
|
|
|
|
if (--count == 0) {
|
2022-09-21 15:47:23 +02:00
|
|
|
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
|
|
|
|
lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
|
2023-05-17 16:17:29 +02:00
|
|
|
} while (mbedtls_ct_bool_and(ge_lower, lt_upper) == MBEDTLS_CT_FALSE);
|
2022-09-21 15:47:23 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-09-21 15:47:23 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static size_t exp_mod_get_window_size(size_t Ebits)
|
2022-09-30 15:13:14 +02:00
|
|
|
{
|
2023-08-09 21:17:40 +02:00
|
|
|
#if MBEDTLS_MPI_WINDOW_SIZE >= 6
|
|
|
|
return (Ebits > 671) ? 6 : (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
|
|
|
|
#elif MBEDTLS_MPI_WINDOW_SIZE == 5
|
|
|
|
return (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
|
|
|
|
#elif MBEDTLS_MPI_WINDOW_SIZE > 1
|
|
|
|
return (Ebits > 79) ? MBEDTLS_MPI_WINDOW_SIZE : 1;
|
|
|
|
#else
|
|
|
|
(void) Ebits;
|
|
|
|
return 1;
|
2022-09-30 15:13:14 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
|
2022-12-06 15:37:18 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
|
|
|
|
const size_t welem = ((size_t) 1) << wsize;
|
2022-12-06 15:37:18 +01:00
|
|
|
|
|
|
|
/* How big does each part of the working memory pool need to be? */
|
|
|
|
const size_t table_limbs = welem * AN_limbs;
|
|
|
|
const size_t select_limbs = AN_limbs;
|
|
|
|
const size_t temp_limbs = 2 * AN_limbs + 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return table_limbs + select_limbs + temp_limbs;
|
2022-12-06 15:37:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t AN_limbs,
|
|
|
|
mbedtls_mpi_uint mm,
|
|
|
|
const mbedtls_mpi_uint *RR,
|
|
|
|
size_t welem,
|
|
|
|
mbedtls_mpi_uint *Wtable,
|
|
|
|
mbedtls_mpi_uint *temp)
|
2022-11-16 20:12:49 +01:00
|
|
|
{
|
|
|
|
/* W[0] = 1 (in Montgomery presentation) */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(Wtable, 0, AN_limbs * ciL);
|
2022-11-16 20:12:49 +01:00
|
|
|
Wtable[0] = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
|
2022-11-16 20:26:14 +01:00
|
|
|
|
2022-12-06 11:46:30 +01:00
|
|
|
/* W[1] = A (already in Montgomery presentation) */
|
2022-11-16 20:26:14 +01:00
|
|
|
mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(W1, A, AN_limbs * ciL);
|
2022-11-16 20:26:14 +01:00
|
|
|
|
2022-11-16 20:12:49 +01:00
|
|
|
/* W[i+1] = W[i] * W[1], i >= 2 */
|
2022-11-16 20:26:14 +01:00
|
|
|
mbedtls_mpi_uint *Wprev = W1;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 2; i < welem; i++) {
|
2022-11-16 20:26:14 +01:00
|
|
|
mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
|
2022-11-16 20:26:14 +01:00
|
|
|
Wprev = Wcur;
|
|
|
|
}
|
2022-11-16 20:12:49 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 22:54:14 +01:00
|
|
|
/* Exponentiation: X := A^E mod N.
|
2022-12-06 11:46:30 +01:00
|
|
|
*
|
|
|
|
* A must already be in Montgomery form.
|
2022-11-16 22:54:14 +01:00
|
|
|
*
|
|
|
|
* As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
|
|
|
|
*
|
|
|
|
* RR must contain 2^{2*biL} mod N.
|
2022-11-22 22:08:33 +01:00
|
|
|
*
|
|
|
|
* The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
|
|
|
|
* (The difference is that the body in our loop processes a single bit instead
|
|
|
|
* of a full window.)
|
2022-11-16 22:54:14 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t AN_limbs,
|
|
|
|
const mbedtls_mpi_uint *E,
|
|
|
|
size_t E_limbs,
|
|
|
|
const mbedtls_mpi_uint *RR,
|
|
|
|
mbedtls_mpi_uint *T)
|
2022-09-30 15:13:14 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
|
|
|
|
const size_t welem = ((size_t) 1) << wsize;
|
2022-09-30 15:13:14 +02:00
|
|
|
|
2022-12-06 15:37:18 +01:00
|
|
|
/* This is how we will use the temporary storage T, which must have space
|
|
|
|
* for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
|
|
|
|
const size_t table_limbs = welem * AN_limbs;
|
|
|
|
const size_t select_limbs = AN_limbs;
|
2022-09-30 15:13:14 +02:00
|
|
|
|
2022-12-06 15:37:18 +01:00
|
|
|
/* Pointers to specific parts of the temporary working memory pool */
|
|
|
|
mbedtls_mpi_uint *const Wtable = T;
|
|
|
|
mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
|
|
|
|
mbedtls_mpi_uint *const temp = Wselect + select_limbs;
|
2022-09-30 15:13:14 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Window precomputation
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
|
2022-11-16 20:04:00 +01:00
|
|
|
|
2022-11-16 20:12:49 +01:00
|
|
|
/* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
|
2023-01-11 14:50:10 +01:00
|
|
|
exp_mod_precompute_window(A, N, AN_limbs,
|
|
|
|
mm, RR,
|
|
|
|
welem, Wtable, temp);
|
2022-09-30 15:13:14 +02:00
|
|
|
|
|
|
|
/*
|
2022-11-14 13:52:08 +01:00
|
|
|
* Fixed window exponentiation
|
2022-09-30 15:13:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* X = 1 (in Montgomery presentation) initially */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, Wtable, AN_limbs * ciL);
|
2022-09-30 15:13:14 +02:00
|
|
|
|
2022-11-16 20:42:09 +01:00
|
|
|
/* We'll process the bits of E from most significant
|
|
|
|
* (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
|
|
|
|
* (limb_index=0, E_bit_index=0). */
|
|
|
|
size_t E_limb_index = E_limbs;
|
|
|
|
size_t E_bit_index = 0;
|
2022-11-16 22:54:03 +01:00
|
|
|
/* At any given time, window contains window_bits bits from E.
|
|
|
|
* window_bits can go up to wsize. */
|
2022-11-09 15:30:44 +01:00
|
|
|
size_t window_bits = 0;
|
2022-11-16 22:54:03 +01:00
|
|
|
mbedtls_mpi_uint window = 0;
|
2022-11-16 20:04:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
2022-09-30 15:13:14 +02:00
|
|
|
/* Square */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
|
2022-09-30 15:13:14 +02:00
|
|
|
|
2022-11-22 22:08:33 +01:00
|
|
|
/* Move to the next bit of the exponent */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (E_bit_index == 0) {
|
2022-11-16 20:42:09 +01:00
|
|
|
--E_limb_index;
|
|
|
|
E_bit_index = biL - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-11-16 20:42:09 +01:00
|
|
|
--E_bit_index;
|
|
|
|
}
|
2022-11-22 22:08:33 +01:00
|
|
|
/* Insert next exponent bit into window */
|
2022-11-16 20:26:14 +01:00
|
|
|
++window_bits;
|
2022-11-16 20:42:09 +01:00
|
|
|
window <<= 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
window |= (E[E_limb_index] >> E_bit_index) & 1;
|
2022-11-16 22:06:18 +01:00
|
|
|
|
|
|
|
/* Clear window if it's full. Also clear the window at the end,
|
|
|
|
* when we've finished processing the exponent. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (window_bits == wsize ||
|
|
|
|
(E_bit_index == 0 && E_limb_index == 0)) {
|
2022-11-16 22:54:03 +01:00
|
|
|
/* Select Wtable[window] without leaking window through
|
|
|
|
* memory access patterns. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
|
|
|
|
AN_limbs, welem, window);
|
2022-11-16 22:54:03 +01:00
|
|
|
/* Multiply X by the selected element. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
|
|
|
|
temp);
|
2022-11-16 22:06:18 +01:00
|
|
|
window = 0;
|
|
|
|
window_bits = 0;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (!(E_bit_index == 0 && E_limb_index == 0));
|
2022-09-30 15:13:14 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
mbedtls_mpi_uint c, /* doubles as carry */
|
|
|
|
size_t limbs)
|
2022-08-25 09:25:19 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < limbs; i++) {
|
2022-08-25 09:39:07 +02:00
|
|
|
mbedtls_mpi_uint s = A[i];
|
|
|
|
mbedtls_mpi_uint t = s - c;
|
2023-01-11 14:50:10 +01:00
|
|
|
c = (t > s);
|
2022-08-25 09:39:07 +02:00
|
|
|
X[i] = t;
|
2022-08-25 09:25:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return c;
|
2022-08-25 09:25:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
|
|
|
|
size_t limbs)
|
2022-12-12 17:54:57 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint bits = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < limbs; i++) {
|
2022-12-12 17:54:57 +01:00
|
|
|
bits |= A[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-12 17:54:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return bits;
|
2022-12-12 17:54:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t AN_limbs,
|
|
|
|
mbedtls_mpi_uint mm,
|
|
|
|
const mbedtls_mpi_uint *rr,
|
|
|
|
mbedtls_mpi_uint *T)
|
2022-12-13 11:45:19 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
|
2022-12-13 11:45:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t AN_limbs,
|
|
|
|
mbedtls_mpi_uint mm,
|
|
|
|
mbedtls_mpi_uint *T)
|
2022-12-13 11:45:19 +01:00
|
|
|
{
|
|
|
|
const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
|
2022-12-13 11:45:19 +01:00
|
|
|
}
|
|
|
|
|
2022-08-09 12:45:47 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|