Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/features/mbedtls/src/bignum.c@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* |
marcozecchini | 0:9fca2b23d0ba | 2 | * Multi-precision integer library |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
marcozecchini | 0:9fca2b23d0ba | 5 | * SPDX-License-Identifier: Apache-2.0 |
marcozecchini | 0:9fca2b23d0ba | 6 | * |
marcozecchini | 0:9fca2b23d0ba | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
marcozecchini | 0:9fca2b23d0ba | 8 | * not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 9 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 12 | * |
marcozecchini | 0:9fca2b23d0ba | 13 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
marcozecchini | 0:9fca2b23d0ba | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 16 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 17 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 18 | * |
marcozecchini | 0:9fca2b23d0ba | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
marcozecchini | 0:9fca2b23d0ba | 20 | */ |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | /* |
marcozecchini | 0:9fca2b23d0ba | 23 | * The following sources were referenced in the design of this Multi-precision |
marcozecchini | 0:9fca2b23d0ba | 24 | * Integer library: |
marcozecchini | 0:9fca2b23d0ba | 25 | * |
marcozecchini | 0:9fca2b23d0ba | 26 | * [1] Handbook of Applied Cryptography - 1997 |
marcozecchini | 0:9fca2b23d0ba | 27 | * Menezes, van Oorschot and Vanstone |
marcozecchini | 0:9fca2b23d0ba | 28 | * |
marcozecchini | 0:9fca2b23d0ba | 29 | * [2] Multi-Precision Math |
marcozecchini | 0:9fca2b23d0ba | 30 | * Tom St Denis |
marcozecchini | 0:9fca2b23d0ba | 31 | * https://github.com/libtom/libtommath/blob/develop/tommath.pdf |
marcozecchini | 0:9fca2b23d0ba | 32 | * |
marcozecchini | 0:9fca2b23d0ba | 33 | * [3] GNU Multi-Precision Arithmetic Library |
marcozecchini | 0:9fca2b23d0ba | 34 | * https://gmplib.org/manual/index.html |
marcozecchini | 0:9fca2b23d0ba | 35 | * |
marcozecchini | 0:9fca2b23d0ba | 36 | */ |
marcozecchini | 0:9fca2b23d0ba | 37 | |
marcozecchini | 0:9fca2b23d0ba | 38 | #if !defined(MBEDTLS_CONFIG_FILE) |
marcozecchini | 0:9fca2b23d0ba | 39 | #include "mbedtls/config.h" |
marcozecchini | 0:9fca2b23d0ba | 40 | #else |
marcozecchini | 0:9fca2b23d0ba | 41 | #include MBEDTLS_CONFIG_FILE |
marcozecchini | 0:9fca2b23d0ba | 42 | #endif |
marcozecchini | 0:9fca2b23d0ba | 43 | |
marcozecchini | 0:9fca2b23d0ba | 44 | #if defined(MBEDTLS_BIGNUM_C) |
marcozecchini | 0:9fca2b23d0ba | 45 | |
marcozecchini | 0:9fca2b23d0ba | 46 | #include "mbedtls/bignum.h" |
marcozecchini | 0:9fca2b23d0ba | 47 | #include "mbedtls/bn_mul.h" |
marcozecchini | 0:9fca2b23d0ba | 48 | |
marcozecchini | 0:9fca2b23d0ba | 49 | #include <string.h> |
marcozecchini | 0:9fca2b23d0ba | 50 | |
marcozecchini | 0:9fca2b23d0ba | 51 | #if defined(MBEDTLS_PLATFORM_C) |
marcozecchini | 0:9fca2b23d0ba | 52 | #include "mbedtls/platform.h" |
marcozecchini | 0:9fca2b23d0ba | 53 | #else |
marcozecchini | 0:9fca2b23d0ba | 54 | #include <stdio.h> |
marcozecchini | 0:9fca2b23d0ba | 55 | #include <stdlib.h> |
marcozecchini | 0:9fca2b23d0ba | 56 | #define mbedtls_printf printf |
marcozecchini | 0:9fca2b23d0ba | 57 | #define mbedtls_calloc calloc |
marcozecchini | 0:9fca2b23d0ba | 58 | #define mbedtls_free free |
marcozecchini | 0:9fca2b23d0ba | 59 | #endif |
marcozecchini | 0:9fca2b23d0ba | 60 | |
marcozecchini | 0:9fca2b23d0ba | 61 | /* Implementation that should never be optimized out by the compiler */ |
marcozecchini | 0:9fca2b23d0ba | 62 | static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n ) { |
marcozecchini | 0:9fca2b23d0ba | 63 | volatile mbedtls_mpi_uint *p = v; while( n-- ) *p++ = 0; |
marcozecchini | 0:9fca2b23d0ba | 64 | } |
marcozecchini | 0:9fca2b23d0ba | 65 | |
marcozecchini | 0:9fca2b23d0ba | 66 | #define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ |
marcozecchini | 0:9fca2b23d0ba | 67 | #define biL (ciL << 3) /* bits in limb */ |
marcozecchini | 0:9fca2b23d0ba | 68 | #define biH (ciL << 2) /* half limb size */ |
marcozecchini | 0:9fca2b23d0ba | 69 | |
marcozecchini | 0:9fca2b23d0ba | 70 | #define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */ |
marcozecchini | 0:9fca2b23d0ba | 71 | |
marcozecchini | 0:9fca2b23d0ba | 72 | /* |
marcozecchini | 0:9fca2b23d0ba | 73 | * Convert between bits/chars and number of limbs |
marcozecchini | 0:9fca2b23d0ba | 74 | * Divide first in order to avoid potential overflows |
marcozecchini | 0:9fca2b23d0ba | 75 | */ |
marcozecchini | 0:9fca2b23d0ba | 76 | #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) ) |
marcozecchini | 0:9fca2b23d0ba | 77 | #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) ) |
marcozecchini | 0:9fca2b23d0ba | 78 | |
marcozecchini | 0:9fca2b23d0ba | 79 | /* |
marcozecchini | 0:9fca2b23d0ba | 80 | * Initialize one MPI |
marcozecchini | 0:9fca2b23d0ba | 81 | */ |
marcozecchini | 0:9fca2b23d0ba | 82 | void mbedtls_mpi_init( mbedtls_mpi *X ) |
marcozecchini | 0:9fca2b23d0ba | 83 | { |
marcozecchini | 0:9fca2b23d0ba | 84 | if( X == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 85 | return; |
marcozecchini | 0:9fca2b23d0ba | 86 | |
marcozecchini | 0:9fca2b23d0ba | 87 | X->s = 1; |
marcozecchini | 0:9fca2b23d0ba | 88 | X->n = 0; |
marcozecchini | 0:9fca2b23d0ba | 89 | X->p = NULL; |
marcozecchini | 0:9fca2b23d0ba | 90 | } |
marcozecchini | 0:9fca2b23d0ba | 91 | |
marcozecchini | 0:9fca2b23d0ba | 92 | /* |
marcozecchini | 0:9fca2b23d0ba | 93 | * Unallocate one MPI |
marcozecchini | 0:9fca2b23d0ba | 94 | */ |
marcozecchini | 0:9fca2b23d0ba | 95 | void mbedtls_mpi_free( mbedtls_mpi *X ) |
marcozecchini | 0:9fca2b23d0ba | 96 | { |
marcozecchini | 0:9fca2b23d0ba | 97 | if( X == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 98 | return; |
marcozecchini | 0:9fca2b23d0ba | 99 | |
marcozecchini | 0:9fca2b23d0ba | 100 | if( X->p != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 101 | { |
marcozecchini | 0:9fca2b23d0ba | 102 | mbedtls_mpi_zeroize( X->p, X->n ); |
marcozecchini | 0:9fca2b23d0ba | 103 | mbedtls_free( X->p ); |
marcozecchini | 0:9fca2b23d0ba | 104 | } |
marcozecchini | 0:9fca2b23d0ba | 105 | |
marcozecchini | 0:9fca2b23d0ba | 106 | X->s = 1; |
marcozecchini | 0:9fca2b23d0ba | 107 | X->n = 0; |
marcozecchini | 0:9fca2b23d0ba | 108 | X->p = NULL; |
marcozecchini | 0:9fca2b23d0ba | 109 | } |
marcozecchini | 0:9fca2b23d0ba | 110 | |
marcozecchini | 0:9fca2b23d0ba | 111 | /* |
marcozecchini | 0:9fca2b23d0ba | 112 | * Enlarge to the specified number of limbs |
marcozecchini | 0:9fca2b23d0ba | 113 | */ |
marcozecchini | 0:9fca2b23d0ba | 114 | int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ) |
marcozecchini | 0:9fca2b23d0ba | 115 | { |
marcozecchini | 0:9fca2b23d0ba | 116 | mbedtls_mpi_uint *p; |
marcozecchini | 0:9fca2b23d0ba | 117 | |
marcozecchini | 0:9fca2b23d0ba | 118 | if( nblimbs > MBEDTLS_MPI_MAX_LIMBS ) |
marcozecchini | 0:9fca2b23d0ba | 119 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
marcozecchini | 0:9fca2b23d0ba | 120 | |
marcozecchini | 0:9fca2b23d0ba | 121 | if( X->n < nblimbs ) |
marcozecchini | 0:9fca2b23d0ba | 122 | { |
marcozecchini | 0:9fca2b23d0ba | 123 | if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 124 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
marcozecchini | 0:9fca2b23d0ba | 125 | |
marcozecchini | 0:9fca2b23d0ba | 126 | if( X->p != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 127 | { |
marcozecchini | 0:9fca2b23d0ba | 128 | memcpy( p, X->p, X->n * ciL ); |
marcozecchini | 0:9fca2b23d0ba | 129 | mbedtls_mpi_zeroize( X->p, X->n ); |
marcozecchini | 0:9fca2b23d0ba | 130 | mbedtls_free( X->p ); |
marcozecchini | 0:9fca2b23d0ba | 131 | } |
marcozecchini | 0:9fca2b23d0ba | 132 | |
marcozecchini | 0:9fca2b23d0ba | 133 | X->n = nblimbs; |
marcozecchini | 0:9fca2b23d0ba | 134 | X->p = p; |
marcozecchini | 0:9fca2b23d0ba | 135 | } |
marcozecchini | 0:9fca2b23d0ba | 136 | |
marcozecchini | 0:9fca2b23d0ba | 137 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 138 | } |
marcozecchini | 0:9fca2b23d0ba | 139 | |
marcozecchini | 0:9fca2b23d0ba | 140 | /* |
marcozecchini | 0:9fca2b23d0ba | 141 | * Resize down as much as possible, |
marcozecchini | 0:9fca2b23d0ba | 142 | * while keeping at least the specified number of limbs |
marcozecchini | 0:9fca2b23d0ba | 143 | */ |
marcozecchini | 0:9fca2b23d0ba | 144 | int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ) |
marcozecchini | 0:9fca2b23d0ba | 145 | { |
marcozecchini | 0:9fca2b23d0ba | 146 | mbedtls_mpi_uint *p; |
marcozecchini | 0:9fca2b23d0ba | 147 | size_t i; |
marcozecchini | 0:9fca2b23d0ba | 148 | |
marcozecchini | 0:9fca2b23d0ba | 149 | /* Actually resize up in this case */ |
marcozecchini | 0:9fca2b23d0ba | 150 | if( X->n <= nblimbs ) |
marcozecchini | 0:9fca2b23d0ba | 151 | return( mbedtls_mpi_grow( X, nblimbs ) ); |
marcozecchini | 0:9fca2b23d0ba | 152 | |
marcozecchini | 0:9fca2b23d0ba | 153 | for( i = X->n - 1; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 154 | if( X->p[i] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 155 | break; |
marcozecchini | 0:9fca2b23d0ba | 156 | i++; |
marcozecchini | 0:9fca2b23d0ba | 157 | |
marcozecchini | 0:9fca2b23d0ba | 158 | if( i < nblimbs ) |
marcozecchini | 0:9fca2b23d0ba | 159 | i = nblimbs; |
marcozecchini | 0:9fca2b23d0ba | 160 | |
marcozecchini | 0:9fca2b23d0ba | 161 | if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 162 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
marcozecchini | 0:9fca2b23d0ba | 163 | |
marcozecchini | 0:9fca2b23d0ba | 164 | if( X->p != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 165 | { |
marcozecchini | 0:9fca2b23d0ba | 166 | memcpy( p, X->p, i * ciL ); |
marcozecchini | 0:9fca2b23d0ba | 167 | mbedtls_mpi_zeroize( X->p, X->n ); |
marcozecchini | 0:9fca2b23d0ba | 168 | mbedtls_free( X->p ); |
marcozecchini | 0:9fca2b23d0ba | 169 | } |
marcozecchini | 0:9fca2b23d0ba | 170 | |
marcozecchini | 0:9fca2b23d0ba | 171 | X->n = i; |
marcozecchini | 0:9fca2b23d0ba | 172 | X->p = p; |
marcozecchini | 0:9fca2b23d0ba | 173 | |
marcozecchini | 0:9fca2b23d0ba | 174 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 175 | } |
marcozecchini | 0:9fca2b23d0ba | 176 | |
marcozecchini | 0:9fca2b23d0ba | 177 | /* |
marcozecchini | 0:9fca2b23d0ba | 178 | * Copy the contents of Y into X |
marcozecchini | 0:9fca2b23d0ba | 179 | */ |
marcozecchini | 0:9fca2b23d0ba | 180 | int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ) |
marcozecchini | 0:9fca2b23d0ba | 181 | { |
marcozecchini | 0:9fca2b23d0ba | 182 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 183 | size_t i; |
marcozecchini | 0:9fca2b23d0ba | 184 | |
marcozecchini | 0:9fca2b23d0ba | 185 | if( X == Y ) |
marcozecchini | 0:9fca2b23d0ba | 186 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 187 | |
marcozecchini | 0:9fca2b23d0ba | 188 | if( Y->p == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 189 | { |
marcozecchini | 0:9fca2b23d0ba | 190 | mbedtls_mpi_free( X ); |
marcozecchini | 0:9fca2b23d0ba | 191 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 192 | } |
marcozecchini | 0:9fca2b23d0ba | 193 | |
marcozecchini | 0:9fca2b23d0ba | 194 | for( i = Y->n - 1; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 195 | if( Y->p[i] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 196 | break; |
marcozecchini | 0:9fca2b23d0ba | 197 | i++; |
marcozecchini | 0:9fca2b23d0ba | 198 | |
marcozecchini | 0:9fca2b23d0ba | 199 | X->s = Y->s; |
marcozecchini | 0:9fca2b23d0ba | 200 | |
marcozecchini | 0:9fca2b23d0ba | 201 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) ); |
marcozecchini | 0:9fca2b23d0ba | 202 | |
marcozecchini | 0:9fca2b23d0ba | 203 | memset( X->p, 0, X->n * ciL ); |
marcozecchini | 0:9fca2b23d0ba | 204 | memcpy( X->p, Y->p, i * ciL ); |
marcozecchini | 0:9fca2b23d0ba | 205 | |
marcozecchini | 0:9fca2b23d0ba | 206 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 207 | |
marcozecchini | 0:9fca2b23d0ba | 208 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 209 | } |
marcozecchini | 0:9fca2b23d0ba | 210 | |
marcozecchini | 0:9fca2b23d0ba | 211 | /* |
marcozecchini | 0:9fca2b23d0ba | 212 | * Swap the contents of X and Y |
marcozecchini | 0:9fca2b23d0ba | 213 | */ |
marcozecchini | 0:9fca2b23d0ba | 214 | void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ) |
marcozecchini | 0:9fca2b23d0ba | 215 | { |
marcozecchini | 0:9fca2b23d0ba | 216 | mbedtls_mpi T; |
marcozecchini | 0:9fca2b23d0ba | 217 | |
marcozecchini | 0:9fca2b23d0ba | 218 | memcpy( &T, X, sizeof( mbedtls_mpi ) ); |
marcozecchini | 0:9fca2b23d0ba | 219 | memcpy( X, Y, sizeof( mbedtls_mpi ) ); |
marcozecchini | 0:9fca2b23d0ba | 220 | memcpy( Y, &T, sizeof( mbedtls_mpi ) ); |
marcozecchini | 0:9fca2b23d0ba | 221 | } |
marcozecchini | 0:9fca2b23d0ba | 222 | |
marcozecchini | 0:9fca2b23d0ba | 223 | /* |
marcozecchini | 0:9fca2b23d0ba | 224 | * Conditionally assign X = Y, without leaking information |
marcozecchini | 0:9fca2b23d0ba | 225 | * about whether the assignment was made or not. |
marcozecchini | 0:9fca2b23d0ba | 226 | * (Leaking information about the respective sizes of X and Y is ok however.) |
marcozecchini | 0:9fca2b23d0ba | 227 | */ |
marcozecchini | 0:9fca2b23d0ba | 228 | int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign ) |
marcozecchini | 0:9fca2b23d0ba | 229 | { |
marcozecchini | 0:9fca2b23d0ba | 230 | int ret = 0; |
marcozecchini | 0:9fca2b23d0ba | 231 | size_t i; |
marcozecchini | 0:9fca2b23d0ba | 232 | |
marcozecchini | 0:9fca2b23d0ba | 233 | /* make sure assign is 0 or 1 in a time-constant manner */ |
marcozecchini | 0:9fca2b23d0ba | 234 | assign = (assign | (unsigned char)-assign) >> 7; |
marcozecchini | 0:9fca2b23d0ba | 235 | |
marcozecchini | 0:9fca2b23d0ba | 236 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); |
marcozecchini | 0:9fca2b23d0ba | 237 | |
marcozecchini | 0:9fca2b23d0ba | 238 | X->s = X->s * ( 1 - assign ) + Y->s * assign; |
marcozecchini | 0:9fca2b23d0ba | 239 | |
marcozecchini | 0:9fca2b23d0ba | 240 | for( i = 0; i < Y->n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 241 | X->p[i] = X->p[i] * ( 1 - assign ) + Y->p[i] * assign; |
marcozecchini | 0:9fca2b23d0ba | 242 | |
marcozecchini | 0:9fca2b23d0ba | 243 | for( ; i < X->n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 244 | X->p[i] *= ( 1 - assign ); |
marcozecchini | 0:9fca2b23d0ba | 245 | |
marcozecchini | 0:9fca2b23d0ba | 246 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 247 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 248 | } |
marcozecchini | 0:9fca2b23d0ba | 249 | |
marcozecchini | 0:9fca2b23d0ba | 250 | /* |
marcozecchini | 0:9fca2b23d0ba | 251 | * Conditionally swap X and Y, without leaking information |
marcozecchini | 0:9fca2b23d0ba | 252 | * about whether the swap was made or not. |
marcozecchini | 0:9fca2b23d0ba | 253 | * Here it is not ok to simply swap the pointers, which whould lead to |
marcozecchini | 0:9fca2b23d0ba | 254 | * different memory access patterns when X and Y are used afterwards. |
marcozecchini | 0:9fca2b23d0ba | 255 | */ |
marcozecchini | 0:9fca2b23d0ba | 256 | int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap ) |
marcozecchini | 0:9fca2b23d0ba | 257 | { |
marcozecchini | 0:9fca2b23d0ba | 258 | int ret, s; |
marcozecchini | 0:9fca2b23d0ba | 259 | size_t i; |
marcozecchini | 0:9fca2b23d0ba | 260 | mbedtls_mpi_uint tmp; |
marcozecchini | 0:9fca2b23d0ba | 261 | |
marcozecchini | 0:9fca2b23d0ba | 262 | if( X == Y ) |
marcozecchini | 0:9fca2b23d0ba | 263 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 264 | |
marcozecchini | 0:9fca2b23d0ba | 265 | /* make sure swap is 0 or 1 in a time-constant manner */ |
marcozecchini | 0:9fca2b23d0ba | 266 | swap = (swap | (unsigned char)-swap) >> 7; |
marcozecchini | 0:9fca2b23d0ba | 267 | |
marcozecchini | 0:9fca2b23d0ba | 268 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); |
marcozecchini | 0:9fca2b23d0ba | 269 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) ); |
marcozecchini | 0:9fca2b23d0ba | 270 | |
marcozecchini | 0:9fca2b23d0ba | 271 | s = X->s; |
marcozecchini | 0:9fca2b23d0ba | 272 | X->s = X->s * ( 1 - swap ) + Y->s * swap; |
marcozecchini | 0:9fca2b23d0ba | 273 | Y->s = Y->s * ( 1 - swap ) + s * swap; |
marcozecchini | 0:9fca2b23d0ba | 274 | |
marcozecchini | 0:9fca2b23d0ba | 275 | |
marcozecchini | 0:9fca2b23d0ba | 276 | for( i = 0; i < X->n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 277 | { |
marcozecchini | 0:9fca2b23d0ba | 278 | tmp = X->p[i]; |
marcozecchini | 0:9fca2b23d0ba | 279 | X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap; |
marcozecchini | 0:9fca2b23d0ba | 280 | Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap; |
marcozecchini | 0:9fca2b23d0ba | 281 | } |
marcozecchini | 0:9fca2b23d0ba | 282 | |
marcozecchini | 0:9fca2b23d0ba | 283 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 284 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 285 | } |
marcozecchini | 0:9fca2b23d0ba | 286 | |
marcozecchini | 0:9fca2b23d0ba | 287 | /* |
marcozecchini | 0:9fca2b23d0ba | 288 | * Set value from integer |
marcozecchini | 0:9fca2b23d0ba | 289 | */ |
marcozecchini | 0:9fca2b23d0ba | 290 | int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ) |
marcozecchini | 0:9fca2b23d0ba | 291 | { |
marcozecchini | 0:9fca2b23d0ba | 292 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 293 | |
marcozecchini | 0:9fca2b23d0ba | 294 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 295 | memset( X->p, 0, X->n * ciL ); |
marcozecchini | 0:9fca2b23d0ba | 296 | |
marcozecchini | 0:9fca2b23d0ba | 297 | X->p[0] = ( z < 0 ) ? -z : z; |
marcozecchini | 0:9fca2b23d0ba | 298 | X->s = ( z < 0 ) ? -1 : 1; |
marcozecchini | 0:9fca2b23d0ba | 299 | |
marcozecchini | 0:9fca2b23d0ba | 300 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 301 | |
marcozecchini | 0:9fca2b23d0ba | 302 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 303 | } |
marcozecchini | 0:9fca2b23d0ba | 304 | |
marcozecchini | 0:9fca2b23d0ba | 305 | /* |
marcozecchini | 0:9fca2b23d0ba | 306 | * Get a specific bit |
marcozecchini | 0:9fca2b23d0ba | 307 | */ |
marcozecchini | 0:9fca2b23d0ba | 308 | int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ) |
marcozecchini | 0:9fca2b23d0ba | 309 | { |
marcozecchini | 0:9fca2b23d0ba | 310 | if( X->n * biL <= pos ) |
marcozecchini | 0:9fca2b23d0ba | 311 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 312 | |
marcozecchini | 0:9fca2b23d0ba | 313 | return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 ); |
marcozecchini | 0:9fca2b23d0ba | 314 | } |
marcozecchini | 0:9fca2b23d0ba | 315 | |
marcozecchini | 0:9fca2b23d0ba | 316 | /* |
marcozecchini | 0:9fca2b23d0ba | 317 | * Set a bit to a specific value of 0 or 1 |
marcozecchini | 0:9fca2b23d0ba | 318 | */ |
marcozecchini | 0:9fca2b23d0ba | 319 | int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ) |
marcozecchini | 0:9fca2b23d0ba | 320 | { |
marcozecchini | 0:9fca2b23d0ba | 321 | int ret = 0; |
marcozecchini | 0:9fca2b23d0ba | 322 | size_t off = pos / biL; |
marcozecchini | 0:9fca2b23d0ba | 323 | size_t idx = pos % biL; |
marcozecchini | 0:9fca2b23d0ba | 324 | |
marcozecchini | 0:9fca2b23d0ba | 325 | if( val != 0 && val != 1 ) |
marcozecchini | 0:9fca2b23d0ba | 326 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 327 | |
marcozecchini | 0:9fca2b23d0ba | 328 | if( X->n * biL <= pos ) |
marcozecchini | 0:9fca2b23d0ba | 329 | { |
marcozecchini | 0:9fca2b23d0ba | 330 | if( val == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 331 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 332 | |
marcozecchini | 0:9fca2b23d0ba | 333 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 334 | } |
marcozecchini | 0:9fca2b23d0ba | 335 | |
marcozecchini | 0:9fca2b23d0ba | 336 | X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx ); |
marcozecchini | 0:9fca2b23d0ba | 337 | X->p[off] |= (mbedtls_mpi_uint) val << idx; |
marcozecchini | 0:9fca2b23d0ba | 338 | |
marcozecchini | 0:9fca2b23d0ba | 339 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 340 | |
marcozecchini | 0:9fca2b23d0ba | 341 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 342 | } |
marcozecchini | 0:9fca2b23d0ba | 343 | |
marcozecchini | 0:9fca2b23d0ba | 344 | /* |
marcozecchini | 0:9fca2b23d0ba | 345 | * Return the number of less significant zero-bits |
marcozecchini | 0:9fca2b23d0ba | 346 | */ |
marcozecchini | 0:9fca2b23d0ba | 347 | size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ) |
marcozecchini | 0:9fca2b23d0ba | 348 | { |
marcozecchini | 0:9fca2b23d0ba | 349 | size_t i, j, count = 0; |
marcozecchini | 0:9fca2b23d0ba | 350 | |
marcozecchini | 0:9fca2b23d0ba | 351 | for( i = 0; i < X->n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 352 | for( j = 0; j < biL; j++, count++ ) |
marcozecchini | 0:9fca2b23d0ba | 353 | if( ( ( X->p[i] >> j ) & 1 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 354 | return( count ); |
marcozecchini | 0:9fca2b23d0ba | 355 | |
marcozecchini | 0:9fca2b23d0ba | 356 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 357 | } |
marcozecchini | 0:9fca2b23d0ba | 358 | |
marcozecchini | 0:9fca2b23d0ba | 359 | /* |
marcozecchini | 0:9fca2b23d0ba | 360 | * Count leading zero bits in a given integer |
marcozecchini | 0:9fca2b23d0ba | 361 | */ |
marcozecchini | 0:9fca2b23d0ba | 362 | static size_t mbedtls_clz( const mbedtls_mpi_uint x ) |
marcozecchini | 0:9fca2b23d0ba | 363 | { |
marcozecchini | 0:9fca2b23d0ba | 364 | size_t j; |
marcozecchini | 0:9fca2b23d0ba | 365 | mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); |
marcozecchini | 0:9fca2b23d0ba | 366 | |
marcozecchini | 0:9fca2b23d0ba | 367 | for( j = 0; j < biL; j++ ) |
marcozecchini | 0:9fca2b23d0ba | 368 | { |
marcozecchini | 0:9fca2b23d0ba | 369 | if( x & mask ) break; |
marcozecchini | 0:9fca2b23d0ba | 370 | |
marcozecchini | 0:9fca2b23d0ba | 371 | mask >>= 1; |
marcozecchini | 0:9fca2b23d0ba | 372 | } |
marcozecchini | 0:9fca2b23d0ba | 373 | |
marcozecchini | 0:9fca2b23d0ba | 374 | return j; |
marcozecchini | 0:9fca2b23d0ba | 375 | } |
marcozecchini | 0:9fca2b23d0ba | 376 | |
marcozecchini | 0:9fca2b23d0ba | 377 | /* |
marcozecchini | 0:9fca2b23d0ba | 378 | * Return the number of bits |
marcozecchini | 0:9fca2b23d0ba | 379 | */ |
marcozecchini | 0:9fca2b23d0ba | 380 | size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ) |
marcozecchini | 0:9fca2b23d0ba | 381 | { |
marcozecchini | 0:9fca2b23d0ba | 382 | size_t i, j; |
marcozecchini | 0:9fca2b23d0ba | 383 | |
marcozecchini | 0:9fca2b23d0ba | 384 | if( X->n == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 385 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 386 | |
marcozecchini | 0:9fca2b23d0ba | 387 | for( i = X->n - 1; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 388 | if( X->p[i] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 389 | break; |
marcozecchini | 0:9fca2b23d0ba | 390 | |
marcozecchini | 0:9fca2b23d0ba | 391 | j = biL - mbedtls_clz( X->p[i] ); |
marcozecchini | 0:9fca2b23d0ba | 392 | |
marcozecchini | 0:9fca2b23d0ba | 393 | return( ( i * biL ) + j ); |
marcozecchini | 0:9fca2b23d0ba | 394 | } |
marcozecchini | 0:9fca2b23d0ba | 395 | |
marcozecchini | 0:9fca2b23d0ba | 396 | /* |
marcozecchini | 0:9fca2b23d0ba | 397 | * Return the total size in bytes |
marcozecchini | 0:9fca2b23d0ba | 398 | */ |
marcozecchini | 0:9fca2b23d0ba | 399 | size_t mbedtls_mpi_size( const mbedtls_mpi *X ) |
marcozecchini | 0:9fca2b23d0ba | 400 | { |
marcozecchini | 0:9fca2b23d0ba | 401 | return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 ); |
marcozecchini | 0:9fca2b23d0ba | 402 | } |
marcozecchini | 0:9fca2b23d0ba | 403 | |
marcozecchini | 0:9fca2b23d0ba | 404 | /* |
marcozecchini | 0:9fca2b23d0ba | 405 | * Convert an ASCII character to digit value |
marcozecchini | 0:9fca2b23d0ba | 406 | */ |
marcozecchini | 0:9fca2b23d0ba | 407 | static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c ) |
marcozecchini | 0:9fca2b23d0ba | 408 | { |
marcozecchini | 0:9fca2b23d0ba | 409 | *d = 255; |
marcozecchini | 0:9fca2b23d0ba | 410 | |
marcozecchini | 0:9fca2b23d0ba | 411 | if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30; |
marcozecchini | 0:9fca2b23d0ba | 412 | if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37; |
marcozecchini | 0:9fca2b23d0ba | 413 | if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57; |
marcozecchini | 0:9fca2b23d0ba | 414 | |
marcozecchini | 0:9fca2b23d0ba | 415 | if( *d >= (mbedtls_mpi_uint) radix ) |
marcozecchini | 0:9fca2b23d0ba | 416 | return( MBEDTLS_ERR_MPI_INVALID_CHARACTER ); |
marcozecchini | 0:9fca2b23d0ba | 417 | |
marcozecchini | 0:9fca2b23d0ba | 418 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 419 | } |
marcozecchini | 0:9fca2b23d0ba | 420 | |
marcozecchini | 0:9fca2b23d0ba | 421 | /* |
marcozecchini | 0:9fca2b23d0ba | 422 | * Import from an ASCII string |
marcozecchini | 0:9fca2b23d0ba | 423 | */ |
marcozecchini | 0:9fca2b23d0ba | 424 | int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ) |
marcozecchini | 0:9fca2b23d0ba | 425 | { |
marcozecchini | 0:9fca2b23d0ba | 426 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 427 | size_t i, j, slen, n; |
marcozecchini | 0:9fca2b23d0ba | 428 | mbedtls_mpi_uint d; |
marcozecchini | 0:9fca2b23d0ba | 429 | mbedtls_mpi T; |
marcozecchini | 0:9fca2b23d0ba | 430 | |
marcozecchini | 0:9fca2b23d0ba | 431 | if( radix < 2 || radix > 16 ) |
marcozecchini | 0:9fca2b23d0ba | 432 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 433 | |
marcozecchini | 0:9fca2b23d0ba | 434 | mbedtls_mpi_init( &T ); |
marcozecchini | 0:9fca2b23d0ba | 435 | |
marcozecchini | 0:9fca2b23d0ba | 436 | slen = strlen( s ); |
marcozecchini | 0:9fca2b23d0ba | 437 | |
marcozecchini | 0:9fca2b23d0ba | 438 | if( radix == 16 ) |
marcozecchini | 0:9fca2b23d0ba | 439 | { |
marcozecchini | 0:9fca2b23d0ba | 440 | if( slen > MPI_SIZE_T_MAX >> 2 ) |
marcozecchini | 0:9fca2b23d0ba | 441 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 442 | |
marcozecchini | 0:9fca2b23d0ba | 443 | n = BITS_TO_LIMBS( slen << 2 ); |
marcozecchini | 0:9fca2b23d0ba | 444 | |
marcozecchini | 0:9fca2b23d0ba | 445 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) ); |
marcozecchini | 0:9fca2b23d0ba | 446 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 447 | |
marcozecchini | 0:9fca2b23d0ba | 448 | for( i = slen, j = 0; i > 0; i--, j++ ) |
marcozecchini | 0:9fca2b23d0ba | 449 | { |
marcozecchini | 0:9fca2b23d0ba | 450 | if( i == 1 && s[i - 1] == '-' ) |
marcozecchini | 0:9fca2b23d0ba | 451 | { |
marcozecchini | 0:9fca2b23d0ba | 452 | X->s = -1; |
marcozecchini | 0:9fca2b23d0ba | 453 | break; |
marcozecchini | 0:9fca2b23d0ba | 454 | } |
marcozecchini | 0:9fca2b23d0ba | 455 | |
marcozecchini | 0:9fca2b23d0ba | 456 | MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) ); |
marcozecchini | 0:9fca2b23d0ba | 457 | X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 ); |
marcozecchini | 0:9fca2b23d0ba | 458 | } |
marcozecchini | 0:9fca2b23d0ba | 459 | } |
marcozecchini | 0:9fca2b23d0ba | 460 | else |
marcozecchini | 0:9fca2b23d0ba | 461 | { |
marcozecchini | 0:9fca2b23d0ba | 462 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 463 | |
marcozecchini | 0:9fca2b23d0ba | 464 | for( i = 0; i < slen; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 465 | { |
marcozecchini | 0:9fca2b23d0ba | 466 | if( i == 0 && s[i] == '-' ) |
marcozecchini | 0:9fca2b23d0ba | 467 | { |
marcozecchini | 0:9fca2b23d0ba | 468 | X->s = -1; |
marcozecchini | 0:9fca2b23d0ba | 469 | continue; |
marcozecchini | 0:9fca2b23d0ba | 470 | } |
marcozecchini | 0:9fca2b23d0ba | 471 | |
marcozecchini | 0:9fca2b23d0ba | 472 | MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) ); |
marcozecchini | 0:9fca2b23d0ba | 473 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) ); |
marcozecchini | 0:9fca2b23d0ba | 474 | |
marcozecchini | 0:9fca2b23d0ba | 475 | if( X->s == 1 ) |
marcozecchini | 0:9fca2b23d0ba | 476 | { |
marcozecchini | 0:9fca2b23d0ba | 477 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) ); |
marcozecchini | 0:9fca2b23d0ba | 478 | } |
marcozecchini | 0:9fca2b23d0ba | 479 | else |
marcozecchini | 0:9fca2b23d0ba | 480 | { |
marcozecchini | 0:9fca2b23d0ba | 481 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( X, &T, d ) ); |
marcozecchini | 0:9fca2b23d0ba | 482 | } |
marcozecchini | 0:9fca2b23d0ba | 483 | } |
marcozecchini | 0:9fca2b23d0ba | 484 | } |
marcozecchini | 0:9fca2b23d0ba | 485 | |
marcozecchini | 0:9fca2b23d0ba | 486 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 487 | |
marcozecchini | 0:9fca2b23d0ba | 488 | mbedtls_mpi_free( &T ); |
marcozecchini | 0:9fca2b23d0ba | 489 | |
marcozecchini | 0:9fca2b23d0ba | 490 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 491 | } |
marcozecchini | 0:9fca2b23d0ba | 492 | |
marcozecchini | 0:9fca2b23d0ba | 493 | /* |
marcozecchini | 0:9fca2b23d0ba | 494 | * Helper to write the digits high-order first |
marcozecchini | 0:9fca2b23d0ba | 495 | */ |
marcozecchini | 0:9fca2b23d0ba | 496 | static int mpi_write_hlp( mbedtls_mpi *X, int radix, char **p ) |
marcozecchini | 0:9fca2b23d0ba | 497 | { |
marcozecchini | 0:9fca2b23d0ba | 498 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 499 | mbedtls_mpi_uint r; |
marcozecchini | 0:9fca2b23d0ba | 500 | |
marcozecchini | 0:9fca2b23d0ba | 501 | if( radix < 2 || radix > 16 ) |
marcozecchini | 0:9fca2b23d0ba | 502 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 503 | |
marcozecchini | 0:9fca2b23d0ba | 504 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) ); |
marcozecchini | 0:9fca2b23d0ba | 505 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) ); |
marcozecchini | 0:9fca2b23d0ba | 506 | |
marcozecchini | 0:9fca2b23d0ba | 507 | if( mbedtls_mpi_cmp_int( X, 0 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 508 | MBEDTLS_MPI_CHK( mpi_write_hlp( X, radix, p ) ); |
marcozecchini | 0:9fca2b23d0ba | 509 | |
marcozecchini | 0:9fca2b23d0ba | 510 | if( r < 10 ) |
marcozecchini | 0:9fca2b23d0ba | 511 | *(*p)++ = (char)( r + 0x30 ); |
marcozecchini | 0:9fca2b23d0ba | 512 | else |
marcozecchini | 0:9fca2b23d0ba | 513 | *(*p)++ = (char)( r + 0x37 ); |
marcozecchini | 0:9fca2b23d0ba | 514 | |
marcozecchini | 0:9fca2b23d0ba | 515 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 516 | |
marcozecchini | 0:9fca2b23d0ba | 517 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 518 | } |
marcozecchini | 0:9fca2b23d0ba | 519 | |
marcozecchini | 0:9fca2b23d0ba | 520 | /* |
marcozecchini | 0:9fca2b23d0ba | 521 | * Export into an ASCII string |
marcozecchini | 0:9fca2b23d0ba | 522 | */ |
marcozecchini | 0:9fca2b23d0ba | 523 | int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, |
marcozecchini | 0:9fca2b23d0ba | 524 | char *buf, size_t buflen, size_t *olen ) |
marcozecchini | 0:9fca2b23d0ba | 525 | { |
marcozecchini | 0:9fca2b23d0ba | 526 | int ret = 0; |
marcozecchini | 0:9fca2b23d0ba | 527 | size_t n; |
marcozecchini | 0:9fca2b23d0ba | 528 | char *p; |
marcozecchini | 0:9fca2b23d0ba | 529 | mbedtls_mpi T; |
marcozecchini | 0:9fca2b23d0ba | 530 | |
marcozecchini | 0:9fca2b23d0ba | 531 | if( radix < 2 || radix > 16 ) |
marcozecchini | 0:9fca2b23d0ba | 532 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 533 | |
marcozecchini | 0:9fca2b23d0ba | 534 | n = mbedtls_mpi_bitlen( X ); |
marcozecchini | 0:9fca2b23d0ba | 535 | if( radix >= 4 ) n >>= 1; |
marcozecchini | 0:9fca2b23d0ba | 536 | if( radix >= 16 ) n >>= 1; |
marcozecchini | 0:9fca2b23d0ba | 537 | /* |
marcozecchini | 0:9fca2b23d0ba | 538 | * Round up the buffer length to an even value to ensure that there is |
marcozecchini | 0:9fca2b23d0ba | 539 | * enough room for hexadecimal values that can be represented in an odd |
marcozecchini | 0:9fca2b23d0ba | 540 | * number of digits. |
marcozecchini | 0:9fca2b23d0ba | 541 | */ |
marcozecchini | 0:9fca2b23d0ba | 542 | n += 3 + ( ( n + 1 ) & 1 ); |
marcozecchini | 0:9fca2b23d0ba | 543 | |
marcozecchini | 0:9fca2b23d0ba | 544 | if( buflen < n ) |
marcozecchini | 0:9fca2b23d0ba | 545 | { |
marcozecchini | 0:9fca2b23d0ba | 546 | *olen = n; |
marcozecchini | 0:9fca2b23d0ba | 547 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
marcozecchini | 0:9fca2b23d0ba | 548 | } |
marcozecchini | 0:9fca2b23d0ba | 549 | |
marcozecchini | 0:9fca2b23d0ba | 550 | p = buf; |
marcozecchini | 0:9fca2b23d0ba | 551 | mbedtls_mpi_init( &T ); |
marcozecchini | 0:9fca2b23d0ba | 552 | |
marcozecchini | 0:9fca2b23d0ba | 553 | if( X->s == -1 ) |
marcozecchini | 0:9fca2b23d0ba | 554 | *p++ = '-'; |
marcozecchini | 0:9fca2b23d0ba | 555 | |
marcozecchini | 0:9fca2b23d0ba | 556 | if( radix == 16 ) |
marcozecchini | 0:9fca2b23d0ba | 557 | { |
marcozecchini | 0:9fca2b23d0ba | 558 | int c; |
marcozecchini | 0:9fca2b23d0ba | 559 | size_t i, j, k; |
marcozecchini | 0:9fca2b23d0ba | 560 | |
marcozecchini | 0:9fca2b23d0ba | 561 | for( i = X->n, k = 0; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 562 | { |
marcozecchini | 0:9fca2b23d0ba | 563 | for( j = ciL; j > 0; j-- ) |
marcozecchini | 0:9fca2b23d0ba | 564 | { |
marcozecchini | 0:9fca2b23d0ba | 565 | c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF; |
marcozecchini | 0:9fca2b23d0ba | 566 | |
marcozecchini | 0:9fca2b23d0ba | 567 | if( c == 0 && k == 0 && ( i + j ) != 2 ) |
marcozecchini | 0:9fca2b23d0ba | 568 | continue; |
marcozecchini | 0:9fca2b23d0ba | 569 | |
marcozecchini | 0:9fca2b23d0ba | 570 | *(p++) = "0123456789ABCDEF" [c / 16]; |
marcozecchini | 0:9fca2b23d0ba | 571 | *(p++) = "0123456789ABCDEF" [c % 16]; |
marcozecchini | 0:9fca2b23d0ba | 572 | k = 1; |
marcozecchini | 0:9fca2b23d0ba | 573 | } |
marcozecchini | 0:9fca2b23d0ba | 574 | } |
marcozecchini | 0:9fca2b23d0ba | 575 | } |
marcozecchini | 0:9fca2b23d0ba | 576 | else |
marcozecchini | 0:9fca2b23d0ba | 577 | { |
marcozecchini | 0:9fca2b23d0ba | 578 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) ); |
marcozecchini | 0:9fca2b23d0ba | 579 | |
marcozecchini | 0:9fca2b23d0ba | 580 | if( T.s == -1 ) |
marcozecchini | 0:9fca2b23d0ba | 581 | T.s = 1; |
marcozecchini | 0:9fca2b23d0ba | 582 | |
marcozecchini | 0:9fca2b23d0ba | 583 | MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p ) ); |
marcozecchini | 0:9fca2b23d0ba | 584 | } |
marcozecchini | 0:9fca2b23d0ba | 585 | |
marcozecchini | 0:9fca2b23d0ba | 586 | *p++ = '\0'; |
marcozecchini | 0:9fca2b23d0ba | 587 | *olen = p - buf; |
marcozecchini | 0:9fca2b23d0ba | 588 | |
marcozecchini | 0:9fca2b23d0ba | 589 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 590 | |
marcozecchini | 0:9fca2b23d0ba | 591 | mbedtls_mpi_free( &T ); |
marcozecchini | 0:9fca2b23d0ba | 592 | |
marcozecchini | 0:9fca2b23d0ba | 593 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 594 | } |
marcozecchini | 0:9fca2b23d0ba | 595 | |
marcozecchini | 0:9fca2b23d0ba | 596 | #if defined(MBEDTLS_FS_IO) |
marcozecchini | 0:9fca2b23d0ba | 597 | /* |
marcozecchini | 0:9fca2b23d0ba | 598 | * Read X from an opened file |
marcozecchini | 0:9fca2b23d0ba | 599 | */ |
marcozecchini | 0:9fca2b23d0ba | 600 | int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ) |
marcozecchini | 0:9fca2b23d0ba | 601 | { |
marcozecchini | 0:9fca2b23d0ba | 602 | mbedtls_mpi_uint d; |
marcozecchini | 0:9fca2b23d0ba | 603 | size_t slen; |
marcozecchini | 0:9fca2b23d0ba | 604 | char *p; |
marcozecchini | 0:9fca2b23d0ba | 605 | /* |
marcozecchini | 0:9fca2b23d0ba | 606 | * Buffer should have space for (short) label and decimal formatted MPI, |
marcozecchini | 0:9fca2b23d0ba | 607 | * newline characters and '\0' |
marcozecchini | 0:9fca2b23d0ba | 608 | */ |
marcozecchini | 0:9fca2b23d0ba | 609 | char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ]; |
marcozecchini | 0:9fca2b23d0ba | 610 | |
marcozecchini | 0:9fca2b23d0ba | 611 | memset( s, 0, sizeof( s ) ); |
marcozecchini | 0:9fca2b23d0ba | 612 | if( fgets( s, sizeof( s ) - 1, fin ) == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 613 | return( MBEDTLS_ERR_MPI_FILE_IO_ERROR ); |
marcozecchini | 0:9fca2b23d0ba | 614 | |
marcozecchini | 0:9fca2b23d0ba | 615 | slen = strlen( s ); |
marcozecchini | 0:9fca2b23d0ba | 616 | if( slen == sizeof( s ) - 2 ) |
marcozecchini | 0:9fca2b23d0ba | 617 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
marcozecchini | 0:9fca2b23d0ba | 618 | |
marcozecchini | 0:9fca2b23d0ba | 619 | if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; } |
marcozecchini | 0:9fca2b23d0ba | 620 | if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; } |
marcozecchini | 0:9fca2b23d0ba | 621 | |
marcozecchini | 0:9fca2b23d0ba | 622 | p = s + slen; |
marcozecchini | 0:9fca2b23d0ba | 623 | while( p-- > s ) |
marcozecchini | 0:9fca2b23d0ba | 624 | if( mpi_get_digit( &d, radix, *p ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 625 | break; |
marcozecchini | 0:9fca2b23d0ba | 626 | |
marcozecchini | 0:9fca2b23d0ba | 627 | return( mbedtls_mpi_read_string( X, radix, p + 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 628 | } |
marcozecchini | 0:9fca2b23d0ba | 629 | |
marcozecchini | 0:9fca2b23d0ba | 630 | /* |
marcozecchini | 0:9fca2b23d0ba | 631 | * Write X into an opened file (or stdout if fout == NULL) |
marcozecchini | 0:9fca2b23d0ba | 632 | */ |
marcozecchini | 0:9fca2b23d0ba | 633 | int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout ) |
marcozecchini | 0:9fca2b23d0ba | 634 | { |
marcozecchini | 0:9fca2b23d0ba | 635 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 636 | size_t n, slen, plen; |
marcozecchini | 0:9fca2b23d0ba | 637 | /* |
marcozecchini | 0:9fca2b23d0ba | 638 | * Buffer should have space for (short) label and decimal formatted MPI, |
marcozecchini | 0:9fca2b23d0ba | 639 | * newline characters and '\0' |
marcozecchini | 0:9fca2b23d0ba | 640 | */ |
marcozecchini | 0:9fca2b23d0ba | 641 | char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ]; |
marcozecchini | 0:9fca2b23d0ba | 642 | |
marcozecchini | 0:9fca2b23d0ba | 643 | memset( s, 0, sizeof( s ) ); |
marcozecchini | 0:9fca2b23d0ba | 644 | |
marcozecchini | 0:9fca2b23d0ba | 645 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) ); |
marcozecchini | 0:9fca2b23d0ba | 646 | |
marcozecchini | 0:9fca2b23d0ba | 647 | if( p == NULL ) p = ""; |
marcozecchini | 0:9fca2b23d0ba | 648 | |
marcozecchini | 0:9fca2b23d0ba | 649 | plen = strlen( p ); |
marcozecchini | 0:9fca2b23d0ba | 650 | slen = strlen( s ); |
marcozecchini | 0:9fca2b23d0ba | 651 | s[slen++] = '\r'; |
marcozecchini | 0:9fca2b23d0ba | 652 | s[slen++] = '\n'; |
marcozecchini | 0:9fca2b23d0ba | 653 | |
marcozecchini | 0:9fca2b23d0ba | 654 | if( fout != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 655 | { |
marcozecchini | 0:9fca2b23d0ba | 656 | if( fwrite( p, 1, plen, fout ) != plen || |
marcozecchini | 0:9fca2b23d0ba | 657 | fwrite( s, 1, slen, fout ) != slen ) |
marcozecchini | 0:9fca2b23d0ba | 658 | return( MBEDTLS_ERR_MPI_FILE_IO_ERROR ); |
marcozecchini | 0:9fca2b23d0ba | 659 | } |
marcozecchini | 0:9fca2b23d0ba | 660 | else |
marcozecchini | 0:9fca2b23d0ba | 661 | mbedtls_printf( "%s%s", p, s ); |
marcozecchini | 0:9fca2b23d0ba | 662 | |
marcozecchini | 0:9fca2b23d0ba | 663 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 664 | |
marcozecchini | 0:9fca2b23d0ba | 665 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 666 | } |
marcozecchini | 0:9fca2b23d0ba | 667 | #endif /* MBEDTLS_FS_IO */ |
marcozecchini | 0:9fca2b23d0ba | 668 | |
marcozecchini | 0:9fca2b23d0ba | 669 | /* |
marcozecchini | 0:9fca2b23d0ba | 670 | * Import X from unsigned binary data, big endian |
marcozecchini | 0:9fca2b23d0ba | 671 | */ |
marcozecchini | 0:9fca2b23d0ba | 672 | int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen ) |
marcozecchini | 0:9fca2b23d0ba | 673 | { |
marcozecchini | 0:9fca2b23d0ba | 674 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 675 | size_t i, j, n; |
marcozecchini | 0:9fca2b23d0ba | 676 | |
marcozecchini | 0:9fca2b23d0ba | 677 | for( n = 0; n < buflen; n++ ) |
marcozecchini | 0:9fca2b23d0ba | 678 | if( buf[n] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 679 | break; |
marcozecchini | 0:9fca2b23d0ba | 680 | |
marcozecchini | 0:9fca2b23d0ba | 681 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 682 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 683 | |
marcozecchini | 0:9fca2b23d0ba | 684 | for( i = buflen, j = 0; i > n; i--, j++ ) |
marcozecchini | 0:9fca2b23d0ba | 685 | X->p[j / ciL] |= ((mbedtls_mpi_uint) buf[i - 1]) << ((j % ciL) << 3); |
marcozecchini | 0:9fca2b23d0ba | 686 | |
marcozecchini | 0:9fca2b23d0ba | 687 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 688 | |
marcozecchini | 0:9fca2b23d0ba | 689 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 690 | } |
marcozecchini | 0:9fca2b23d0ba | 691 | |
marcozecchini | 0:9fca2b23d0ba | 692 | /* |
marcozecchini | 0:9fca2b23d0ba | 693 | * Export X into unsigned binary data, big endian |
marcozecchini | 0:9fca2b23d0ba | 694 | */ |
marcozecchini | 0:9fca2b23d0ba | 695 | int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen ) |
marcozecchini | 0:9fca2b23d0ba | 696 | { |
marcozecchini | 0:9fca2b23d0ba | 697 | size_t i, j, n; |
marcozecchini | 0:9fca2b23d0ba | 698 | |
marcozecchini | 0:9fca2b23d0ba | 699 | n = mbedtls_mpi_size( X ); |
marcozecchini | 0:9fca2b23d0ba | 700 | |
marcozecchini | 0:9fca2b23d0ba | 701 | if( buflen < n ) |
marcozecchini | 0:9fca2b23d0ba | 702 | return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); |
marcozecchini | 0:9fca2b23d0ba | 703 | |
marcozecchini | 0:9fca2b23d0ba | 704 | memset( buf, 0, buflen ); |
marcozecchini | 0:9fca2b23d0ba | 705 | |
marcozecchini | 0:9fca2b23d0ba | 706 | for( i = buflen - 1, j = 0; n > 0; i--, j++, n-- ) |
marcozecchini | 0:9fca2b23d0ba | 707 | buf[i] = (unsigned char)( X->p[j / ciL] >> ((j % ciL) << 3) ); |
marcozecchini | 0:9fca2b23d0ba | 708 | |
marcozecchini | 0:9fca2b23d0ba | 709 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 710 | } |
marcozecchini | 0:9fca2b23d0ba | 711 | |
marcozecchini | 0:9fca2b23d0ba | 712 | /* |
marcozecchini | 0:9fca2b23d0ba | 713 | * Left-shift: X <<= count |
marcozecchini | 0:9fca2b23d0ba | 714 | */ |
marcozecchini | 0:9fca2b23d0ba | 715 | int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ) |
marcozecchini | 0:9fca2b23d0ba | 716 | { |
marcozecchini | 0:9fca2b23d0ba | 717 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 718 | size_t i, v0, t1; |
marcozecchini | 0:9fca2b23d0ba | 719 | mbedtls_mpi_uint r0 = 0, r1; |
marcozecchini | 0:9fca2b23d0ba | 720 | |
marcozecchini | 0:9fca2b23d0ba | 721 | v0 = count / (biL ); |
marcozecchini | 0:9fca2b23d0ba | 722 | t1 = count & (biL - 1); |
marcozecchini | 0:9fca2b23d0ba | 723 | |
marcozecchini | 0:9fca2b23d0ba | 724 | i = mbedtls_mpi_bitlen( X ) + count; |
marcozecchini | 0:9fca2b23d0ba | 725 | |
marcozecchini | 0:9fca2b23d0ba | 726 | if( X->n * biL < i ) |
marcozecchini | 0:9fca2b23d0ba | 727 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 728 | |
marcozecchini | 0:9fca2b23d0ba | 729 | ret = 0; |
marcozecchini | 0:9fca2b23d0ba | 730 | |
marcozecchini | 0:9fca2b23d0ba | 731 | /* |
marcozecchini | 0:9fca2b23d0ba | 732 | * shift by count / limb_size |
marcozecchini | 0:9fca2b23d0ba | 733 | */ |
marcozecchini | 0:9fca2b23d0ba | 734 | if( v0 > 0 ) |
marcozecchini | 0:9fca2b23d0ba | 735 | { |
marcozecchini | 0:9fca2b23d0ba | 736 | for( i = X->n; i > v0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 737 | X->p[i - 1] = X->p[i - v0 - 1]; |
marcozecchini | 0:9fca2b23d0ba | 738 | |
marcozecchini | 0:9fca2b23d0ba | 739 | for( ; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 740 | X->p[i - 1] = 0; |
marcozecchini | 0:9fca2b23d0ba | 741 | } |
marcozecchini | 0:9fca2b23d0ba | 742 | |
marcozecchini | 0:9fca2b23d0ba | 743 | /* |
marcozecchini | 0:9fca2b23d0ba | 744 | * shift by count % limb_size |
marcozecchini | 0:9fca2b23d0ba | 745 | */ |
marcozecchini | 0:9fca2b23d0ba | 746 | if( t1 > 0 ) |
marcozecchini | 0:9fca2b23d0ba | 747 | { |
marcozecchini | 0:9fca2b23d0ba | 748 | for( i = v0; i < X->n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 749 | { |
marcozecchini | 0:9fca2b23d0ba | 750 | r1 = X->p[i] >> (biL - t1); |
marcozecchini | 0:9fca2b23d0ba | 751 | X->p[i] <<= t1; |
marcozecchini | 0:9fca2b23d0ba | 752 | X->p[i] |= r0; |
marcozecchini | 0:9fca2b23d0ba | 753 | r0 = r1; |
marcozecchini | 0:9fca2b23d0ba | 754 | } |
marcozecchini | 0:9fca2b23d0ba | 755 | } |
marcozecchini | 0:9fca2b23d0ba | 756 | |
marcozecchini | 0:9fca2b23d0ba | 757 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 758 | |
marcozecchini | 0:9fca2b23d0ba | 759 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 760 | } |
marcozecchini | 0:9fca2b23d0ba | 761 | |
marcozecchini | 0:9fca2b23d0ba | 762 | /* |
marcozecchini | 0:9fca2b23d0ba | 763 | * Right-shift: X >>= count |
marcozecchini | 0:9fca2b23d0ba | 764 | */ |
marcozecchini | 0:9fca2b23d0ba | 765 | int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ) |
marcozecchini | 0:9fca2b23d0ba | 766 | { |
marcozecchini | 0:9fca2b23d0ba | 767 | size_t i, v0, v1; |
marcozecchini | 0:9fca2b23d0ba | 768 | mbedtls_mpi_uint r0 = 0, r1; |
marcozecchini | 0:9fca2b23d0ba | 769 | |
marcozecchini | 0:9fca2b23d0ba | 770 | v0 = count / biL; |
marcozecchini | 0:9fca2b23d0ba | 771 | v1 = count & (biL - 1); |
marcozecchini | 0:9fca2b23d0ba | 772 | |
marcozecchini | 0:9fca2b23d0ba | 773 | if( v0 > X->n || ( v0 == X->n && v1 > 0 ) ) |
marcozecchini | 0:9fca2b23d0ba | 774 | return mbedtls_mpi_lset( X, 0 ); |
marcozecchini | 0:9fca2b23d0ba | 775 | |
marcozecchini | 0:9fca2b23d0ba | 776 | /* |
marcozecchini | 0:9fca2b23d0ba | 777 | * shift by count / limb_size |
marcozecchini | 0:9fca2b23d0ba | 778 | */ |
marcozecchini | 0:9fca2b23d0ba | 779 | if( v0 > 0 ) |
marcozecchini | 0:9fca2b23d0ba | 780 | { |
marcozecchini | 0:9fca2b23d0ba | 781 | for( i = 0; i < X->n - v0; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 782 | X->p[i] = X->p[i + v0]; |
marcozecchini | 0:9fca2b23d0ba | 783 | |
marcozecchini | 0:9fca2b23d0ba | 784 | for( ; i < X->n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 785 | X->p[i] = 0; |
marcozecchini | 0:9fca2b23d0ba | 786 | } |
marcozecchini | 0:9fca2b23d0ba | 787 | |
marcozecchini | 0:9fca2b23d0ba | 788 | /* |
marcozecchini | 0:9fca2b23d0ba | 789 | * shift by count % limb_size |
marcozecchini | 0:9fca2b23d0ba | 790 | */ |
marcozecchini | 0:9fca2b23d0ba | 791 | if( v1 > 0 ) |
marcozecchini | 0:9fca2b23d0ba | 792 | { |
marcozecchini | 0:9fca2b23d0ba | 793 | for( i = X->n; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 794 | { |
marcozecchini | 0:9fca2b23d0ba | 795 | r1 = X->p[i - 1] << (biL - v1); |
marcozecchini | 0:9fca2b23d0ba | 796 | X->p[i - 1] >>= v1; |
marcozecchini | 0:9fca2b23d0ba | 797 | X->p[i - 1] |= r0; |
marcozecchini | 0:9fca2b23d0ba | 798 | r0 = r1; |
marcozecchini | 0:9fca2b23d0ba | 799 | } |
marcozecchini | 0:9fca2b23d0ba | 800 | } |
marcozecchini | 0:9fca2b23d0ba | 801 | |
marcozecchini | 0:9fca2b23d0ba | 802 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 803 | } |
marcozecchini | 0:9fca2b23d0ba | 804 | |
marcozecchini | 0:9fca2b23d0ba | 805 | /* |
marcozecchini | 0:9fca2b23d0ba | 806 | * Compare unsigned values |
marcozecchini | 0:9fca2b23d0ba | 807 | */ |
marcozecchini | 0:9fca2b23d0ba | 808 | int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ) |
marcozecchini | 0:9fca2b23d0ba | 809 | { |
marcozecchini | 0:9fca2b23d0ba | 810 | size_t i, j; |
marcozecchini | 0:9fca2b23d0ba | 811 | |
marcozecchini | 0:9fca2b23d0ba | 812 | for( i = X->n; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 813 | if( X->p[i - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 814 | break; |
marcozecchini | 0:9fca2b23d0ba | 815 | |
marcozecchini | 0:9fca2b23d0ba | 816 | for( j = Y->n; j > 0; j-- ) |
marcozecchini | 0:9fca2b23d0ba | 817 | if( Y->p[j - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 818 | break; |
marcozecchini | 0:9fca2b23d0ba | 819 | |
marcozecchini | 0:9fca2b23d0ba | 820 | if( i == 0 && j == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 821 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 822 | |
marcozecchini | 0:9fca2b23d0ba | 823 | if( i > j ) return( 1 ); |
marcozecchini | 0:9fca2b23d0ba | 824 | if( j > i ) return( -1 ); |
marcozecchini | 0:9fca2b23d0ba | 825 | |
marcozecchini | 0:9fca2b23d0ba | 826 | for( ; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 827 | { |
marcozecchini | 0:9fca2b23d0ba | 828 | if( X->p[i - 1] > Y->p[i - 1] ) return( 1 ); |
marcozecchini | 0:9fca2b23d0ba | 829 | if( X->p[i - 1] < Y->p[i - 1] ) return( -1 ); |
marcozecchini | 0:9fca2b23d0ba | 830 | } |
marcozecchini | 0:9fca2b23d0ba | 831 | |
marcozecchini | 0:9fca2b23d0ba | 832 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 833 | } |
marcozecchini | 0:9fca2b23d0ba | 834 | |
marcozecchini | 0:9fca2b23d0ba | 835 | /* |
marcozecchini | 0:9fca2b23d0ba | 836 | * Compare signed values |
marcozecchini | 0:9fca2b23d0ba | 837 | */ |
marcozecchini | 0:9fca2b23d0ba | 838 | int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ) |
marcozecchini | 0:9fca2b23d0ba | 839 | { |
marcozecchini | 0:9fca2b23d0ba | 840 | size_t i, j; |
marcozecchini | 0:9fca2b23d0ba | 841 | |
marcozecchini | 0:9fca2b23d0ba | 842 | for( i = X->n; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 843 | if( X->p[i - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 844 | break; |
marcozecchini | 0:9fca2b23d0ba | 845 | |
marcozecchini | 0:9fca2b23d0ba | 846 | for( j = Y->n; j > 0; j-- ) |
marcozecchini | 0:9fca2b23d0ba | 847 | if( Y->p[j - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 848 | break; |
marcozecchini | 0:9fca2b23d0ba | 849 | |
marcozecchini | 0:9fca2b23d0ba | 850 | if( i == 0 && j == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 851 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 852 | |
marcozecchini | 0:9fca2b23d0ba | 853 | if( i > j ) return( X->s ); |
marcozecchini | 0:9fca2b23d0ba | 854 | if( j > i ) return( -Y->s ); |
marcozecchini | 0:9fca2b23d0ba | 855 | |
marcozecchini | 0:9fca2b23d0ba | 856 | if( X->s > 0 && Y->s < 0 ) return( 1 ); |
marcozecchini | 0:9fca2b23d0ba | 857 | if( Y->s > 0 && X->s < 0 ) return( -1 ); |
marcozecchini | 0:9fca2b23d0ba | 858 | |
marcozecchini | 0:9fca2b23d0ba | 859 | for( ; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 860 | { |
marcozecchini | 0:9fca2b23d0ba | 861 | if( X->p[i - 1] > Y->p[i - 1] ) return( X->s ); |
marcozecchini | 0:9fca2b23d0ba | 862 | if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s ); |
marcozecchini | 0:9fca2b23d0ba | 863 | } |
marcozecchini | 0:9fca2b23d0ba | 864 | |
marcozecchini | 0:9fca2b23d0ba | 865 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 866 | } |
marcozecchini | 0:9fca2b23d0ba | 867 | |
marcozecchini | 0:9fca2b23d0ba | 868 | /* |
marcozecchini | 0:9fca2b23d0ba | 869 | * Compare signed values |
marcozecchini | 0:9fca2b23d0ba | 870 | */ |
marcozecchini | 0:9fca2b23d0ba | 871 | int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ) |
marcozecchini | 0:9fca2b23d0ba | 872 | { |
marcozecchini | 0:9fca2b23d0ba | 873 | mbedtls_mpi Y; |
marcozecchini | 0:9fca2b23d0ba | 874 | mbedtls_mpi_uint p[1]; |
marcozecchini | 0:9fca2b23d0ba | 875 | |
marcozecchini | 0:9fca2b23d0ba | 876 | *p = ( z < 0 ) ? -z : z; |
marcozecchini | 0:9fca2b23d0ba | 877 | Y.s = ( z < 0 ) ? -1 : 1; |
marcozecchini | 0:9fca2b23d0ba | 878 | Y.n = 1; |
marcozecchini | 0:9fca2b23d0ba | 879 | Y.p = p; |
marcozecchini | 0:9fca2b23d0ba | 880 | |
marcozecchini | 0:9fca2b23d0ba | 881 | return( mbedtls_mpi_cmp_mpi( X, &Y ) ); |
marcozecchini | 0:9fca2b23d0ba | 882 | } |
marcozecchini | 0:9fca2b23d0ba | 883 | |
marcozecchini | 0:9fca2b23d0ba | 884 | /* |
marcozecchini | 0:9fca2b23d0ba | 885 | * Unsigned addition: X = |A| + |B| (HAC 14.7) |
marcozecchini | 0:9fca2b23d0ba | 886 | */ |
marcozecchini | 0:9fca2b23d0ba | 887 | int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 888 | { |
marcozecchini | 0:9fca2b23d0ba | 889 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 890 | size_t i, j; |
marcozecchini | 0:9fca2b23d0ba | 891 | mbedtls_mpi_uint *o, *p, c, tmp; |
marcozecchini | 0:9fca2b23d0ba | 892 | |
marcozecchini | 0:9fca2b23d0ba | 893 | if( X == B ) |
marcozecchini | 0:9fca2b23d0ba | 894 | { |
marcozecchini | 0:9fca2b23d0ba | 895 | const mbedtls_mpi *T = A; A = X; B = T; |
marcozecchini | 0:9fca2b23d0ba | 896 | } |
marcozecchini | 0:9fca2b23d0ba | 897 | |
marcozecchini | 0:9fca2b23d0ba | 898 | if( X != A ) |
marcozecchini | 0:9fca2b23d0ba | 899 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 900 | |
marcozecchini | 0:9fca2b23d0ba | 901 | /* |
marcozecchini | 0:9fca2b23d0ba | 902 | * X should always be positive as a result of unsigned additions. |
marcozecchini | 0:9fca2b23d0ba | 903 | */ |
marcozecchini | 0:9fca2b23d0ba | 904 | X->s = 1; |
marcozecchini | 0:9fca2b23d0ba | 905 | |
marcozecchini | 0:9fca2b23d0ba | 906 | for( j = B->n; j > 0; j-- ) |
marcozecchini | 0:9fca2b23d0ba | 907 | if( B->p[j - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 908 | break; |
marcozecchini | 0:9fca2b23d0ba | 909 | |
marcozecchini | 0:9fca2b23d0ba | 910 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) ); |
marcozecchini | 0:9fca2b23d0ba | 911 | |
marcozecchini | 0:9fca2b23d0ba | 912 | o = B->p; p = X->p; c = 0; |
marcozecchini | 0:9fca2b23d0ba | 913 | |
marcozecchini | 0:9fca2b23d0ba | 914 | /* |
marcozecchini | 0:9fca2b23d0ba | 915 | * tmp is used because it might happen that p == o |
marcozecchini | 0:9fca2b23d0ba | 916 | */ |
marcozecchini | 0:9fca2b23d0ba | 917 | for( i = 0; i < j; i++, o++, p++ ) |
marcozecchini | 0:9fca2b23d0ba | 918 | { |
marcozecchini | 0:9fca2b23d0ba | 919 | tmp= *o; |
marcozecchini | 0:9fca2b23d0ba | 920 | *p += c; c = ( *p < c ); |
marcozecchini | 0:9fca2b23d0ba | 921 | *p += tmp; c += ( *p < tmp ); |
marcozecchini | 0:9fca2b23d0ba | 922 | } |
marcozecchini | 0:9fca2b23d0ba | 923 | |
marcozecchini | 0:9fca2b23d0ba | 924 | while( c != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 925 | { |
marcozecchini | 0:9fca2b23d0ba | 926 | if( i >= X->n ) |
marcozecchini | 0:9fca2b23d0ba | 927 | { |
marcozecchini | 0:9fca2b23d0ba | 928 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 929 | p = X->p + i; |
marcozecchini | 0:9fca2b23d0ba | 930 | } |
marcozecchini | 0:9fca2b23d0ba | 931 | |
marcozecchini | 0:9fca2b23d0ba | 932 | *p += c; c = ( *p < c ); i++; p++; |
marcozecchini | 0:9fca2b23d0ba | 933 | } |
marcozecchini | 0:9fca2b23d0ba | 934 | |
marcozecchini | 0:9fca2b23d0ba | 935 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 936 | |
marcozecchini | 0:9fca2b23d0ba | 937 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 938 | } |
marcozecchini | 0:9fca2b23d0ba | 939 | |
marcozecchini | 0:9fca2b23d0ba | 940 | /* |
marcozecchini | 0:9fca2b23d0ba | 941 | * Helper for mbedtls_mpi subtraction |
marcozecchini | 0:9fca2b23d0ba | 942 | */ |
marcozecchini | 0:9fca2b23d0ba | 943 | static void mpi_sub_hlp( size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d ) |
marcozecchini | 0:9fca2b23d0ba | 944 | { |
marcozecchini | 0:9fca2b23d0ba | 945 | size_t i; |
marcozecchini | 0:9fca2b23d0ba | 946 | mbedtls_mpi_uint c, z; |
marcozecchini | 0:9fca2b23d0ba | 947 | |
marcozecchini | 0:9fca2b23d0ba | 948 | for( i = c = 0; i < n; i++, s++, d++ ) |
marcozecchini | 0:9fca2b23d0ba | 949 | { |
marcozecchini | 0:9fca2b23d0ba | 950 | z = ( *d < c ); *d -= c; |
marcozecchini | 0:9fca2b23d0ba | 951 | c = ( *d < *s ) + z; *d -= *s; |
marcozecchini | 0:9fca2b23d0ba | 952 | } |
marcozecchini | 0:9fca2b23d0ba | 953 | |
marcozecchini | 0:9fca2b23d0ba | 954 | while( c != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 955 | { |
marcozecchini | 0:9fca2b23d0ba | 956 | z = ( *d < c ); *d -= c; |
marcozecchini | 0:9fca2b23d0ba | 957 | c = z; i++; d++; |
marcozecchini | 0:9fca2b23d0ba | 958 | } |
marcozecchini | 0:9fca2b23d0ba | 959 | } |
marcozecchini | 0:9fca2b23d0ba | 960 | |
marcozecchini | 0:9fca2b23d0ba | 961 | /* |
marcozecchini | 0:9fca2b23d0ba | 962 | * Unsigned subtraction: X = |A| - |B| (HAC 14.9) |
marcozecchini | 0:9fca2b23d0ba | 963 | */ |
marcozecchini | 0:9fca2b23d0ba | 964 | int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 965 | { |
marcozecchini | 0:9fca2b23d0ba | 966 | mbedtls_mpi TB; |
marcozecchini | 0:9fca2b23d0ba | 967 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 968 | size_t n; |
marcozecchini | 0:9fca2b23d0ba | 969 | |
marcozecchini | 0:9fca2b23d0ba | 970 | if( mbedtls_mpi_cmp_abs( A, B ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 971 | return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE ); |
marcozecchini | 0:9fca2b23d0ba | 972 | |
marcozecchini | 0:9fca2b23d0ba | 973 | mbedtls_mpi_init( &TB ); |
marcozecchini | 0:9fca2b23d0ba | 974 | |
marcozecchini | 0:9fca2b23d0ba | 975 | if( X == B ) |
marcozecchini | 0:9fca2b23d0ba | 976 | { |
marcozecchini | 0:9fca2b23d0ba | 977 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 978 | B = &TB; |
marcozecchini | 0:9fca2b23d0ba | 979 | } |
marcozecchini | 0:9fca2b23d0ba | 980 | |
marcozecchini | 0:9fca2b23d0ba | 981 | if( X != A ) |
marcozecchini | 0:9fca2b23d0ba | 982 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 983 | |
marcozecchini | 0:9fca2b23d0ba | 984 | /* |
marcozecchini | 0:9fca2b23d0ba | 985 | * X should always be positive as a result of unsigned subtractions. |
marcozecchini | 0:9fca2b23d0ba | 986 | */ |
marcozecchini | 0:9fca2b23d0ba | 987 | X->s = 1; |
marcozecchini | 0:9fca2b23d0ba | 988 | |
marcozecchini | 0:9fca2b23d0ba | 989 | ret = 0; |
marcozecchini | 0:9fca2b23d0ba | 990 | |
marcozecchini | 0:9fca2b23d0ba | 991 | for( n = B->n; n > 0; n-- ) |
marcozecchini | 0:9fca2b23d0ba | 992 | if( B->p[n - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 993 | break; |
marcozecchini | 0:9fca2b23d0ba | 994 | |
marcozecchini | 0:9fca2b23d0ba | 995 | mpi_sub_hlp( n, B->p, X->p ); |
marcozecchini | 0:9fca2b23d0ba | 996 | |
marcozecchini | 0:9fca2b23d0ba | 997 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 998 | |
marcozecchini | 0:9fca2b23d0ba | 999 | mbedtls_mpi_free( &TB ); |
marcozecchini | 0:9fca2b23d0ba | 1000 | |
marcozecchini | 0:9fca2b23d0ba | 1001 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1002 | } |
marcozecchini | 0:9fca2b23d0ba | 1003 | |
marcozecchini | 0:9fca2b23d0ba | 1004 | /* |
marcozecchini | 0:9fca2b23d0ba | 1005 | * Signed addition: X = A + B |
marcozecchini | 0:9fca2b23d0ba | 1006 | */ |
marcozecchini | 0:9fca2b23d0ba | 1007 | int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 1008 | { |
marcozecchini | 0:9fca2b23d0ba | 1009 | int ret, s = A->s; |
marcozecchini | 0:9fca2b23d0ba | 1010 | |
marcozecchini | 0:9fca2b23d0ba | 1011 | if( A->s * B->s < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1012 | { |
marcozecchini | 0:9fca2b23d0ba | 1013 | if( mbedtls_mpi_cmp_abs( A, B ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1014 | { |
marcozecchini | 0:9fca2b23d0ba | 1015 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1016 | X->s = s; |
marcozecchini | 0:9fca2b23d0ba | 1017 | } |
marcozecchini | 0:9fca2b23d0ba | 1018 | else |
marcozecchini | 0:9fca2b23d0ba | 1019 | { |
marcozecchini | 0:9fca2b23d0ba | 1020 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 1021 | X->s = -s; |
marcozecchini | 0:9fca2b23d0ba | 1022 | } |
marcozecchini | 0:9fca2b23d0ba | 1023 | } |
marcozecchini | 0:9fca2b23d0ba | 1024 | else |
marcozecchini | 0:9fca2b23d0ba | 1025 | { |
marcozecchini | 0:9fca2b23d0ba | 1026 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1027 | X->s = s; |
marcozecchini | 0:9fca2b23d0ba | 1028 | } |
marcozecchini | 0:9fca2b23d0ba | 1029 | |
marcozecchini | 0:9fca2b23d0ba | 1030 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1031 | |
marcozecchini | 0:9fca2b23d0ba | 1032 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1033 | } |
marcozecchini | 0:9fca2b23d0ba | 1034 | |
marcozecchini | 0:9fca2b23d0ba | 1035 | /* |
marcozecchini | 0:9fca2b23d0ba | 1036 | * Signed subtraction: X = A - B |
marcozecchini | 0:9fca2b23d0ba | 1037 | */ |
marcozecchini | 0:9fca2b23d0ba | 1038 | int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 1039 | { |
marcozecchini | 0:9fca2b23d0ba | 1040 | int ret, s = A->s; |
marcozecchini | 0:9fca2b23d0ba | 1041 | |
marcozecchini | 0:9fca2b23d0ba | 1042 | if( A->s * B->s > 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1043 | { |
marcozecchini | 0:9fca2b23d0ba | 1044 | if( mbedtls_mpi_cmp_abs( A, B ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1045 | { |
marcozecchini | 0:9fca2b23d0ba | 1046 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1047 | X->s = s; |
marcozecchini | 0:9fca2b23d0ba | 1048 | } |
marcozecchini | 0:9fca2b23d0ba | 1049 | else |
marcozecchini | 0:9fca2b23d0ba | 1050 | { |
marcozecchini | 0:9fca2b23d0ba | 1051 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 1052 | X->s = -s; |
marcozecchini | 0:9fca2b23d0ba | 1053 | } |
marcozecchini | 0:9fca2b23d0ba | 1054 | } |
marcozecchini | 0:9fca2b23d0ba | 1055 | else |
marcozecchini | 0:9fca2b23d0ba | 1056 | { |
marcozecchini | 0:9fca2b23d0ba | 1057 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1058 | X->s = s; |
marcozecchini | 0:9fca2b23d0ba | 1059 | } |
marcozecchini | 0:9fca2b23d0ba | 1060 | |
marcozecchini | 0:9fca2b23d0ba | 1061 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1062 | |
marcozecchini | 0:9fca2b23d0ba | 1063 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1064 | } |
marcozecchini | 0:9fca2b23d0ba | 1065 | |
marcozecchini | 0:9fca2b23d0ba | 1066 | /* |
marcozecchini | 0:9fca2b23d0ba | 1067 | * Signed addition: X = A + b |
marcozecchini | 0:9fca2b23d0ba | 1068 | */ |
marcozecchini | 0:9fca2b23d0ba | 1069 | int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ) |
marcozecchini | 0:9fca2b23d0ba | 1070 | { |
marcozecchini | 0:9fca2b23d0ba | 1071 | mbedtls_mpi _B; |
marcozecchini | 0:9fca2b23d0ba | 1072 | mbedtls_mpi_uint p[1]; |
marcozecchini | 0:9fca2b23d0ba | 1073 | |
marcozecchini | 0:9fca2b23d0ba | 1074 | p[0] = ( b < 0 ) ? -b : b; |
marcozecchini | 0:9fca2b23d0ba | 1075 | _B.s = ( b < 0 ) ? -1 : 1; |
marcozecchini | 0:9fca2b23d0ba | 1076 | _B.n = 1; |
marcozecchini | 0:9fca2b23d0ba | 1077 | _B.p = p; |
marcozecchini | 0:9fca2b23d0ba | 1078 | |
marcozecchini | 0:9fca2b23d0ba | 1079 | return( mbedtls_mpi_add_mpi( X, A, &_B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1080 | } |
marcozecchini | 0:9fca2b23d0ba | 1081 | |
marcozecchini | 0:9fca2b23d0ba | 1082 | /* |
marcozecchini | 0:9fca2b23d0ba | 1083 | * Signed subtraction: X = A - b |
marcozecchini | 0:9fca2b23d0ba | 1084 | */ |
marcozecchini | 0:9fca2b23d0ba | 1085 | int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ) |
marcozecchini | 0:9fca2b23d0ba | 1086 | { |
marcozecchini | 0:9fca2b23d0ba | 1087 | mbedtls_mpi _B; |
marcozecchini | 0:9fca2b23d0ba | 1088 | mbedtls_mpi_uint p[1]; |
marcozecchini | 0:9fca2b23d0ba | 1089 | |
marcozecchini | 0:9fca2b23d0ba | 1090 | p[0] = ( b < 0 ) ? -b : b; |
marcozecchini | 0:9fca2b23d0ba | 1091 | _B.s = ( b < 0 ) ? -1 : 1; |
marcozecchini | 0:9fca2b23d0ba | 1092 | _B.n = 1; |
marcozecchini | 0:9fca2b23d0ba | 1093 | _B.p = p; |
marcozecchini | 0:9fca2b23d0ba | 1094 | |
marcozecchini | 0:9fca2b23d0ba | 1095 | return( mbedtls_mpi_sub_mpi( X, A, &_B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1096 | } |
marcozecchini | 0:9fca2b23d0ba | 1097 | |
marcozecchini | 0:9fca2b23d0ba | 1098 | /* |
marcozecchini | 0:9fca2b23d0ba | 1099 | * Helper for mbedtls_mpi multiplication |
marcozecchini | 0:9fca2b23d0ba | 1100 | */ |
marcozecchini | 0:9fca2b23d0ba | 1101 | static |
marcozecchini | 0:9fca2b23d0ba | 1102 | #if defined(__APPLE__) && defined(__arm__) |
marcozecchini | 0:9fca2b23d0ba | 1103 | /* |
marcozecchini | 0:9fca2b23d0ba | 1104 | * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn) |
marcozecchini | 0:9fca2b23d0ba | 1105 | * appears to need this to prevent bad ARM code generation at -O3. |
marcozecchini | 0:9fca2b23d0ba | 1106 | */ |
marcozecchini | 0:9fca2b23d0ba | 1107 | __attribute__ ((noinline)) |
marcozecchini | 0:9fca2b23d0ba | 1108 | #endif |
marcozecchini | 0:9fca2b23d0ba | 1109 | void mpi_mul_hlp( size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b ) |
marcozecchini | 0:9fca2b23d0ba | 1110 | { |
marcozecchini | 0:9fca2b23d0ba | 1111 | mbedtls_mpi_uint c = 0, t = 0; |
marcozecchini | 0:9fca2b23d0ba | 1112 | |
marcozecchini | 0:9fca2b23d0ba | 1113 | #if defined(MULADDC_HUIT) |
marcozecchini | 0:9fca2b23d0ba | 1114 | for( ; i >= 8; i -= 8 ) |
marcozecchini | 0:9fca2b23d0ba | 1115 | { |
marcozecchini | 0:9fca2b23d0ba | 1116 | MULADDC_INIT |
marcozecchini | 0:9fca2b23d0ba | 1117 | MULADDC_HUIT |
marcozecchini | 0:9fca2b23d0ba | 1118 | MULADDC_STOP |
marcozecchini | 0:9fca2b23d0ba | 1119 | } |
marcozecchini | 0:9fca2b23d0ba | 1120 | |
marcozecchini | 0:9fca2b23d0ba | 1121 | for( ; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 1122 | { |
marcozecchini | 0:9fca2b23d0ba | 1123 | MULADDC_INIT |
marcozecchini | 0:9fca2b23d0ba | 1124 | MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1125 | MULADDC_STOP |
marcozecchini | 0:9fca2b23d0ba | 1126 | } |
marcozecchini | 0:9fca2b23d0ba | 1127 | #else /* MULADDC_HUIT */ |
marcozecchini | 0:9fca2b23d0ba | 1128 | for( ; i >= 16; i -= 16 ) |
marcozecchini | 0:9fca2b23d0ba | 1129 | { |
marcozecchini | 0:9fca2b23d0ba | 1130 | MULADDC_INIT |
marcozecchini | 0:9fca2b23d0ba | 1131 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1132 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1133 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1134 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1135 | |
marcozecchini | 0:9fca2b23d0ba | 1136 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1137 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1138 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1139 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1140 | MULADDC_STOP |
marcozecchini | 0:9fca2b23d0ba | 1141 | } |
marcozecchini | 0:9fca2b23d0ba | 1142 | |
marcozecchini | 0:9fca2b23d0ba | 1143 | for( ; i >= 8; i -= 8 ) |
marcozecchini | 0:9fca2b23d0ba | 1144 | { |
marcozecchini | 0:9fca2b23d0ba | 1145 | MULADDC_INIT |
marcozecchini | 0:9fca2b23d0ba | 1146 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1147 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1148 | |
marcozecchini | 0:9fca2b23d0ba | 1149 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1150 | MULADDC_CORE MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1151 | MULADDC_STOP |
marcozecchini | 0:9fca2b23d0ba | 1152 | } |
marcozecchini | 0:9fca2b23d0ba | 1153 | |
marcozecchini | 0:9fca2b23d0ba | 1154 | for( ; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 1155 | { |
marcozecchini | 0:9fca2b23d0ba | 1156 | MULADDC_INIT |
marcozecchini | 0:9fca2b23d0ba | 1157 | MULADDC_CORE |
marcozecchini | 0:9fca2b23d0ba | 1158 | MULADDC_STOP |
marcozecchini | 0:9fca2b23d0ba | 1159 | } |
marcozecchini | 0:9fca2b23d0ba | 1160 | #endif /* MULADDC_HUIT */ |
marcozecchini | 0:9fca2b23d0ba | 1161 | |
marcozecchini | 0:9fca2b23d0ba | 1162 | t++; |
marcozecchini | 0:9fca2b23d0ba | 1163 | |
marcozecchini | 0:9fca2b23d0ba | 1164 | do { |
marcozecchini | 0:9fca2b23d0ba | 1165 | *d += c; c = ( *d < c ); d++; |
marcozecchini | 0:9fca2b23d0ba | 1166 | } |
marcozecchini | 0:9fca2b23d0ba | 1167 | while( c != 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1168 | } |
marcozecchini | 0:9fca2b23d0ba | 1169 | |
marcozecchini | 0:9fca2b23d0ba | 1170 | /* |
marcozecchini | 0:9fca2b23d0ba | 1171 | * Baseline multiplication: X = A * B (HAC 14.12) |
marcozecchini | 0:9fca2b23d0ba | 1172 | */ |
marcozecchini | 0:9fca2b23d0ba | 1173 | int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 1174 | { |
marcozecchini | 0:9fca2b23d0ba | 1175 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 1176 | size_t i, j; |
marcozecchini | 0:9fca2b23d0ba | 1177 | mbedtls_mpi TA, TB; |
marcozecchini | 0:9fca2b23d0ba | 1178 | |
marcozecchini | 0:9fca2b23d0ba | 1179 | mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB ); |
marcozecchini | 0:9fca2b23d0ba | 1180 | |
marcozecchini | 0:9fca2b23d0ba | 1181 | if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; } |
marcozecchini | 0:9fca2b23d0ba | 1182 | if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; } |
marcozecchini | 0:9fca2b23d0ba | 1183 | |
marcozecchini | 0:9fca2b23d0ba | 1184 | for( i = A->n; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 1185 | if( A->p[i - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1186 | break; |
marcozecchini | 0:9fca2b23d0ba | 1187 | |
marcozecchini | 0:9fca2b23d0ba | 1188 | for( j = B->n; j > 0; j-- ) |
marcozecchini | 0:9fca2b23d0ba | 1189 | if( B->p[j - 1] != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1190 | break; |
marcozecchini | 0:9fca2b23d0ba | 1191 | |
marcozecchini | 0:9fca2b23d0ba | 1192 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) ); |
marcozecchini | 0:9fca2b23d0ba | 1193 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1194 | |
marcozecchini | 0:9fca2b23d0ba | 1195 | for( i++; j > 0; j-- ) |
marcozecchini | 0:9fca2b23d0ba | 1196 | mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] ); |
marcozecchini | 0:9fca2b23d0ba | 1197 | |
marcozecchini | 0:9fca2b23d0ba | 1198 | X->s = A->s * B->s; |
marcozecchini | 0:9fca2b23d0ba | 1199 | |
marcozecchini | 0:9fca2b23d0ba | 1200 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1201 | |
marcozecchini | 0:9fca2b23d0ba | 1202 | mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA ); |
marcozecchini | 0:9fca2b23d0ba | 1203 | |
marcozecchini | 0:9fca2b23d0ba | 1204 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1205 | } |
marcozecchini | 0:9fca2b23d0ba | 1206 | |
marcozecchini | 0:9fca2b23d0ba | 1207 | /* |
marcozecchini | 0:9fca2b23d0ba | 1208 | * Baseline multiplication: X = A * b |
marcozecchini | 0:9fca2b23d0ba | 1209 | */ |
marcozecchini | 0:9fca2b23d0ba | 1210 | int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b ) |
marcozecchini | 0:9fca2b23d0ba | 1211 | { |
marcozecchini | 0:9fca2b23d0ba | 1212 | mbedtls_mpi _B; |
marcozecchini | 0:9fca2b23d0ba | 1213 | mbedtls_mpi_uint p[1]; |
marcozecchini | 0:9fca2b23d0ba | 1214 | |
marcozecchini | 0:9fca2b23d0ba | 1215 | _B.s = 1; |
marcozecchini | 0:9fca2b23d0ba | 1216 | _B.n = 1; |
marcozecchini | 0:9fca2b23d0ba | 1217 | _B.p = p; |
marcozecchini | 0:9fca2b23d0ba | 1218 | p[0] = b; |
marcozecchini | 0:9fca2b23d0ba | 1219 | |
marcozecchini | 0:9fca2b23d0ba | 1220 | return( mbedtls_mpi_mul_mpi( X, A, &_B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1221 | } |
marcozecchini | 0:9fca2b23d0ba | 1222 | |
marcozecchini | 0:9fca2b23d0ba | 1223 | /* |
marcozecchini | 0:9fca2b23d0ba | 1224 | * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and |
marcozecchini | 0:9fca2b23d0ba | 1225 | * mbedtls_mpi_uint divisor, d |
marcozecchini | 0:9fca2b23d0ba | 1226 | */ |
marcozecchini | 0:9fca2b23d0ba | 1227 | static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1, |
marcozecchini | 0:9fca2b23d0ba | 1228 | mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r ) |
marcozecchini | 0:9fca2b23d0ba | 1229 | { |
marcozecchini | 0:9fca2b23d0ba | 1230 | #if defined(MBEDTLS_HAVE_UDBL) |
marcozecchini | 0:9fca2b23d0ba | 1231 | mbedtls_t_udbl dividend, quotient; |
marcozecchini | 0:9fca2b23d0ba | 1232 | #else |
marcozecchini | 0:9fca2b23d0ba | 1233 | const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH; |
marcozecchini | 0:9fca2b23d0ba | 1234 | const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1; |
marcozecchini | 0:9fca2b23d0ba | 1235 | mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient; |
marcozecchini | 0:9fca2b23d0ba | 1236 | mbedtls_mpi_uint u0_msw, u0_lsw; |
marcozecchini | 0:9fca2b23d0ba | 1237 | size_t s; |
marcozecchini | 0:9fca2b23d0ba | 1238 | #endif |
marcozecchini | 0:9fca2b23d0ba | 1239 | |
marcozecchini | 0:9fca2b23d0ba | 1240 | /* |
marcozecchini | 0:9fca2b23d0ba | 1241 | * Check for overflow |
marcozecchini | 0:9fca2b23d0ba | 1242 | */ |
marcozecchini | 0:9fca2b23d0ba | 1243 | if( 0 == d || u1 >= d ) |
marcozecchini | 0:9fca2b23d0ba | 1244 | { |
marcozecchini | 0:9fca2b23d0ba | 1245 | if (r != NULL) *r = ~0; |
marcozecchini | 0:9fca2b23d0ba | 1246 | |
marcozecchini | 0:9fca2b23d0ba | 1247 | return ( ~0 ); |
marcozecchini | 0:9fca2b23d0ba | 1248 | } |
marcozecchini | 0:9fca2b23d0ba | 1249 | |
marcozecchini | 0:9fca2b23d0ba | 1250 | #if defined(MBEDTLS_HAVE_UDBL) |
marcozecchini | 0:9fca2b23d0ba | 1251 | dividend = (mbedtls_t_udbl) u1 << biL; |
marcozecchini | 0:9fca2b23d0ba | 1252 | dividend |= (mbedtls_t_udbl) u0; |
marcozecchini | 0:9fca2b23d0ba | 1253 | quotient = dividend / d; |
marcozecchini | 0:9fca2b23d0ba | 1254 | if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 ) |
marcozecchini | 0:9fca2b23d0ba | 1255 | quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1; |
marcozecchini | 0:9fca2b23d0ba | 1256 | |
marcozecchini | 0:9fca2b23d0ba | 1257 | if( r != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 1258 | *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) ); |
marcozecchini | 0:9fca2b23d0ba | 1259 | |
marcozecchini | 0:9fca2b23d0ba | 1260 | return (mbedtls_mpi_uint) quotient; |
marcozecchini | 0:9fca2b23d0ba | 1261 | #else |
marcozecchini | 0:9fca2b23d0ba | 1262 | |
marcozecchini | 0:9fca2b23d0ba | 1263 | /* |
marcozecchini | 0:9fca2b23d0ba | 1264 | * Algorithm D, Section 4.3.1 - The Art of Computer Programming |
marcozecchini | 0:9fca2b23d0ba | 1265 | * Vol. 2 - Seminumerical Algorithms, Knuth |
marcozecchini | 0:9fca2b23d0ba | 1266 | */ |
marcozecchini | 0:9fca2b23d0ba | 1267 | |
marcozecchini | 0:9fca2b23d0ba | 1268 | /* |
marcozecchini | 0:9fca2b23d0ba | 1269 | * Normalize the divisor, d, and dividend, u0, u1 |
marcozecchini | 0:9fca2b23d0ba | 1270 | */ |
marcozecchini | 0:9fca2b23d0ba | 1271 | s = mbedtls_clz( d ); |
marcozecchini | 0:9fca2b23d0ba | 1272 | d = d << s; |
marcozecchini | 0:9fca2b23d0ba | 1273 | |
marcozecchini | 0:9fca2b23d0ba | 1274 | u1 = u1 << s; |
marcozecchini | 0:9fca2b23d0ba | 1275 | u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1276 | u0 = u0 << s; |
marcozecchini | 0:9fca2b23d0ba | 1277 | |
marcozecchini | 0:9fca2b23d0ba | 1278 | d1 = d >> biH; |
marcozecchini | 0:9fca2b23d0ba | 1279 | d0 = d & uint_halfword_mask; |
marcozecchini | 0:9fca2b23d0ba | 1280 | |
marcozecchini | 0:9fca2b23d0ba | 1281 | u0_msw = u0 >> biH; |
marcozecchini | 0:9fca2b23d0ba | 1282 | u0_lsw = u0 & uint_halfword_mask; |
marcozecchini | 0:9fca2b23d0ba | 1283 | |
marcozecchini | 0:9fca2b23d0ba | 1284 | /* |
marcozecchini | 0:9fca2b23d0ba | 1285 | * Find the first quotient and remainder |
marcozecchini | 0:9fca2b23d0ba | 1286 | */ |
marcozecchini | 0:9fca2b23d0ba | 1287 | q1 = u1 / d1; |
marcozecchini | 0:9fca2b23d0ba | 1288 | r0 = u1 - d1 * q1; |
marcozecchini | 0:9fca2b23d0ba | 1289 | |
marcozecchini | 0:9fca2b23d0ba | 1290 | while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) ) |
marcozecchini | 0:9fca2b23d0ba | 1291 | { |
marcozecchini | 0:9fca2b23d0ba | 1292 | q1 -= 1; |
marcozecchini | 0:9fca2b23d0ba | 1293 | r0 += d1; |
marcozecchini | 0:9fca2b23d0ba | 1294 | |
marcozecchini | 0:9fca2b23d0ba | 1295 | if ( r0 >= radix ) break; |
marcozecchini | 0:9fca2b23d0ba | 1296 | } |
marcozecchini | 0:9fca2b23d0ba | 1297 | |
marcozecchini | 0:9fca2b23d0ba | 1298 | rAX = ( u1 * radix ) + ( u0_msw - q1 * d ); |
marcozecchini | 0:9fca2b23d0ba | 1299 | q0 = rAX / d1; |
marcozecchini | 0:9fca2b23d0ba | 1300 | r0 = rAX - q0 * d1; |
marcozecchini | 0:9fca2b23d0ba | 1301 | |
marcozecchini | 0:9fca2b23d0ba | 1302 | while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) ) |
marcozecchini | 0:9fca2b23d0ba | 1303 | { |
marcozecchini | 0:9fca2b23d0ba | 1304 | q0 -= 1; |
marcozecchini | 0:9fca2b23d0ba | 1305 | r0 += d1; |
marcozecchini | 0:9fca2b23d0ba | 1306 | |
marcozecchini | 0:9fca2b23d0ba | 1307 | if ( r0 >= radix ) break; |
marcozecchini | 0:9fca2b23d0ba | 1308 | } |
marcozecchini | 0:9fca2b23d0ba | 1309 | |
marcozecchini | 0:9fca2b23d0ba | 1310 | if (r != NULL) |
marcozecchini | 0:9fca2b23d0ba | 1311 | *r = ( rAX * radix + u0_lsw - q0 * d ) >> s; |
marcozecchini | 0:9fca2b23d0ba | 1312 | |
marcozecchini | 0:9fca2b23d0ba | 1313 | quotient = q1 * radix + q0; |
marcozecchini | 0:9fca2b23d0ba | 1314 | |
marcozecchini | 0:9fca2b23d0ba | 1315 | return quotient; |
marcozecchini | 0:9fca2b23d0ba | 1316 | #endif |
marcozecchini | 0:9fca2b23d0ba | 1317 | } |
marcozecchini | 0:9fca2b23d0ba | 1318 | |
marcozecchini | 0:9fca2b23d0ba | 1319 | /* |
marcozecchini | 0:9fca2b23d0ba | 1320 | * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20) |
marcozecchini | 0:9fca2b23d0ba | 1321 | */ |
marcozecchini | 0:9fca2b23d0ba | 1322 | int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 1323 | { |
marcozecchini | 0:9fca2b23d0ba | 1324 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 1325 | size_t i, n, t, k; |
marcozecchini | 0:9fca2b23d0ba | 1326 | mbedtls_mpi X, Y, Z, T1, T2; |
marcozecchini | 0:9fca2b23d0ba | 1327 | |
marcozecchini | 0:9fca2b23d0ba | 1328 | if( mbedtls_mpi_cmp_int( B, 0 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1329 | return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO ); |
marcozecchini | 0:9fca2b23d0ba | 1330 | |
marcozecchini | 0:9fca2b23d0ba | 1331 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); |
marcozecchini | 0:9fca2b23d0ba | 1332 | mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); |
marcozecchini | 0:9fca2b23d0ba | 1333 | |
marcozecchini | 0:9fca2b23d0ba | 1334 | if( mbedtls_mpi_cmp_abs( A, B ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1335 | { |
marcozecchini | 0:9fca2b23d0ba | 1336 | if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1337 | if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 1338 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1339 | } |
marcozecchini | 0:9fca2b23d0ba | 1340 | |
marcozecchini | 0:9fca2b23d0ba | 1341 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 1342 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1343 | X.s = Y.s = 1; |
marcozecchini | 0:9fca2b23d0ba | 1344 | |
marcozecchini | 0:9fca2b23d0ba | 1345 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1346 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1347 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1348 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1349 | |
marcozecchini | 0:9fca2b23d0ba | 1350 | k = mbedtls_mpi_bitlen( &Y ) % biL; |
marcozecchini | 0:9fca2b23d0ba | 1351 | if( k < biL - 1 ) |
marcozecchini | 0:9fca2b23d0ba | 1352 | { |
marcozecchini | 0:9fca2b23d0ba | 1353 | k = biL - 1 - k; |
marcozecchini | 0:9fca2b23d0ba | 1354 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) ); |
marcozecchini | 0:9fca2b23d0ba | 1355 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) ); |
marcozecchini | 0:9fca2b23d0ba | 1356 | } |
marcozecchini | 0:9fca2b23d0ba | 1357 | else k = 0; |
marcozecchini | 0:9fca2b23d0ba | 1358 | |
marcozecchini | 0:9fca2b23d0ba | 1359 | n = X.n - 1; |
marcozecchini | 0:9fca2b23d0ba | 1360 | t = Y.n - 1; |
marcozecchini | 0:9fca2b23d0ba | 1361 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 1362 | |
marcozecchini | 0:9fca2b23d0ba | 1363 | while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1364 | { |
marcozecchini | 0:9fca2b23d0ba | 1365 | Z.p[n - t]++; |
marcozecchini | 0:9fca2b23d0ba | 1366 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) ); |
marcozecchini | 0:9fca2b23d0ba | 1367 | } |
marcozecchini | 0:9fca2b23d0ba | 1368 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 1369 | |
marcozecchini | 0:9fca2b23d0ba | 1370 | for( i = n; i > t ; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 1371 | { |
marcozecchini | 0:9fca2b23d0ba | 1372 | if( X.p[i] >= Y.p[t] ) |
marcozecchini | 0:9fca2b23d0ba | 1373 | Z.p[i - t - 1] = ~0; |
marcozecchini | 0:9fca2b23d0ba | 1374 | else |
marcozecchini | 0:9fca2b23d0ba | 1375 | { |
marcozecchini | 0:9fca2b23d0ba | 1376 | Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1], |
marcozecchini | 0:9fca2b23d0ba | 1377 | Y.p[t], NULL); |
marcozecchini | 0:9fca2b23d0ba | 1378 | } |
marcozecchini | 0:9fca2b23d0ba | 1379 | |
marcozecchini | 0:9fca2b23d0ba | 1380 | Z.p[i - t - 1]++; |
marcozecchini | 0:9fca2b23d0ba | 1381 | do |
marcozecchini | 0:9fca2b23d0ba | 1382 | { |
marcozecchini | 0:9fca2b23d0ba | 1383 | Z.p[i - t - 1]--; |
marcozecchini | 0:9fca2b23d0ba | 1384 | |
marcozecchini | 0:9fca2b23d0ba | 1385 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1386 | T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1]; |
marcozecchini | 0:9fca2b23d0ba | 1387 | T1.p[1] = Y.p[t]; |
marcozecchini | 0:9fca2b23d0ba | 1388 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) ); |
marcozecchini | 0:9fca2b23d0ba | 1389 | |
marcozecchini | 0:9fca2b23d0ba | 1390 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1391 | T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2]; |
marcozecchini | 0:9fca2b23d0ba | 1392 | T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1]; |
marcozecchini | 0:9fca2b23d0ba | 1393 | T2.p[2] = X.p[i]; |
marcozecchini | 0:9fca2b23d0ba | 1394 | } |
marcozecchini | 0:9fca2b23d0ba | 1395 | while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1396 | |
marcozecchini | 0:9fca2b23d0ba | 1397 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) ); |
marcozecchini | 0:9fca2b23d0ba | 1398 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 1399 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1400 | |
marcozecchini | 0:9fca2b23d0ba | 1401 | if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1402 | { |
marcozecchini | 0:9fca2b23d0ba | 1403 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) ); |
marcozecchini | 0:9fca2b23d0ba | 1404 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 1405 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1406 | Z.p[i - t - 1]--; |
marcozecchini | 0:9fca2b23d0ba | 1407 | } |
marcozecchini | 0:9fca2b23d0ba | 1408 | } |
marcozecchini | 0:9fca2b23d0ba | 1409 | |
marcozecchini | 0:9fca2b23d0ba | 1410 | if( Q != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 1411 | { |
marcozecchini | 0:9fca2b23d0ba | 1412 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) ); |
marcozecchini | 0:9fca2b23d0ba | 1413 | Q->s = A->s * B->s; |
marcozecchini | 0:9fca2b23d0ba | 1414 | } |
marcozecchini | 0:9fca2b23d0ba | 1415 | |
marcozecchini | 0:9fca2b23d0ba | 1416 | if( R != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 1417 | { |
marcozecchini | 0:9fca2b23d0ba | 1418 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) ); |
marcozecchini | 0:9fca2b23d0ba | 1419 | X.s = A->s; |
marcozecchini | 0:9fca2b23d0ba | 1420 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) ); |
marcozecchini | 0:9fca2b23d0ba | 1421 | |
marcozecchini | 0:9fca2b23d0ba | 1422 | if( mbedtls_mpi_cmp_int( R, 0 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1423 | R->s = 1; |
marcozecchini | 0:9fca2b23d0ba | 1424 | } |
marcozecchini | 0:9fca2b23d0ba | 1425 | |
marcozecchini | 0:9fca2b23d0ba | 1426 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1427 | |
marcozecchini | 0:9fca2b23d0ba | 1428 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); |
marcozecchini | 0:9fca2b23d0ba | 1429 | mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); |
marcozecchini | 0:9fca2b23d0ba | 1430 | |
marcozecchini | 0:9fca2b23d0ba | 1431 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1432 | } |
marcozecchini | 0:9fca2b23d0ba | 1433 | |
marcozecchini | 0:9fca2b23d0ba | 1434 | /* |
marcozecchini | 0:9fca2b23d0ba | 1435 | * Division by int: A = Q * b + R |
marcozecchini | 0:9fca2b23d0ba | 1436 | */ |
marcozecchini | 0:9fca2b23d0ba | 1437 | int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b ) |
marcozecchini | 0:9fca2b23d0ba | 1438 | { |
marcozecchini | 0:9fca2b23d0ba | 1439 | mbedtls_mpi _B; |
marcozecchini | 0:9fca2b23d0ba | 1440 | mbedtls_mpi_uint p[1]; |
marcozecchini | 0:9fca2b23d0ba | 1441 | |
marcozecchini | 0:9fca2b23d0ba | 1442 | p[0] = ( b < 0 ) ? -b : b; |
marcozecchini | 0:9fca2b23d0ba | 1443 | _B.s = ( b < 0 ) ? -1 : 1; |
marcozecchini | 0:9fca2b23d0ba | 1444 | _B.n = 1; |
marcozecchini | 0:9fca2b23d0ba | 1445 | _B.p = p; |
marcozecchini | 0:9fca2b23d0ba | 1446 | |
marcozecchini | 0:9fca2b23d0ba | 1447 | return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1448 | } |
marcozecchini | 0:9fca2b23d0ba | 1449 | |
marcozecchini | 0:9fca2b23d0ba | 1450 | /* |
marcozecchini | 0:9fca2b23d0ba | 1451 | * Modulo: R = A mod B |
marcozecchini | 0:9fca2b23d0ba | 1452 | */ |
marcozecchini | 0:9fca2b23d0ba | 1453 | int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 1454 | { |
marcozecchini | 0:9fca2b23d0ba | 1455 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 1456 | |
marcozecchini | 0:9fca2b23d0ba | 1457 | if( mbedtls_mpi_cmp_int( B, 0 ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1458 | return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE ); |
marcozecchini | 0:9fca2b23d0ba | 1459 | |
marcozecchini | 0:9fca2b23d0ba | 1460 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1461 | |
marcozecchini | 0:9fca2b23d0ba | 1462 | while( mbedtls_mpi_cmp_int( R, 0 ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1463 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1464 | |
marcozecchini | 0:9fca2b23d0ba | 1465 | while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1466 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1467 | |
marcozecchini | 0:9fca2b23d0ba | 1468 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1469 | |
marcozecchini | 0:9fca2b23d0ba | 1470 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1471 | } |
marcozecchini | 0:9fca2b23d0ba | 1472 | |
marcozecchini | 0:9fca2b23d0ba | 1473 | /* |
marcozecchini | 0:9fca2b23d0ba | 1474 | * Modulo: r = A mod b |
marcozecchini | 0:9fca2b23d0ba | 1475 | */ |
marcozecchini | 0:9fca2b23d0ba | 1476 | int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b ) |
marcozecchini | 0:9fca2b23d0ba | 1477 | { |
marcozecchini | 0:9fca2b23d0ba | 1478 | size_t i; |
marcozecchini | 0:9fca2b23d0ba | 1479 | mbedtls_mpi_uint x, y, z; |
marcozecchini | 0:9fca2b23d0ba | 1480 | |
marcozecchini | 0:9fca2b23d0ba | 1481 | if( b == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1482 | return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO ); |
marcozecchini | 0:9fca2b23d0ba | 1483 | |
marcozecchini | 0:9fca2b23d0ba | 1484 | if( b < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1485 | return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE ); |
marcozecchini | 0:9fca2b23d0ba | 1486 | |
marcozecchini | 0:9fca2b23d0ba | 1487 | /* |
marcozecchini | 0:9fca2b23d0ba | 1488 | * handle trivial cases |
marcozecchini | 0:9fca2b23d0ba | 1489 | */ |
marcozecchini | 0:9fca2b23d0ba | 1490 | if( b == 1 ) |
marcozecchini | 0:9fca2b23d0ba | 1491 | { |
marcozecchini | 0:9fca2b23d0ba | 1492 | *r = 0; |
marcozecchini | 0:9fca2b23d0ba | 1493 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1494 | } |
marcozecchini | 0:9fca2b23d0ba | 1495 | |
marcozecchini | 0:9fca2b23d0ba | 1496 | if( b == 2 ) |
marcozecchini | 0:9fca2b23d0ba | 1497 | { |
marcozecchini | 0:9fca2b23d0ba | 1498 | *r = A->p[0] & 1; |
marcozecchini | 0:9fca2b23d0ba | 1499 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1500 | } |
marcozecchini | 0:9fca2b23d0ba | 1501 | |
marcozecchini | 0:9fca2b23d0ba | 1502 | /* |
marcozecchini | 0:9fca2b23d0ba | 1503 | * general case |
marcozecchini | 0:9fca2b23d0ba | 1504 | */ |
marcozecchini | 0:9fca2b23d0ba | 1505 | for( i = A->n, y = 0; i > 0; i-- ) |
marcozecchini | 0:9fca2b23d0ba | 1506 | { |
marcozecchini | 0:9fca2b23d0ba | 1507 | x = A->p[i - 1]; |
marcozecchini | 0:9fca2b23d0ba | 1508 | y = ( y << biH ) | ( x >> biH ); |
marcozecchini | 0:9fca2b23d0ba | 1509 | z = y / b; |
marcozecchini | 0:9fca2b23d0ba | 1510 | y -= z * b; |
marcozecchini | 0:9fca2b23d0ba | 1511 | |
marcozecchini | 0:9fca2b23d0ba | 1512 | x <<= biH; |
marcozecchini | 0:9fca2b23d0ba | 1513 | y = ( y << biH ) | ( x >> biH ); |
marcozecchini | 0:9fca2b23d0ba | 1514 | z = y / b; |
marcozecchini | 0:9fca2b23d0ba | 1515 | y -= z * b; |
marcozecchini | 0:9fca2b23d0ba | 1516 | } |
marcozecchini | 0:9fca2b23d0ba | 1517 | |
marcozecchini | 0:9fca2b23d0ba | 1518 | /* |
marcozecchini | 0:9fca2b23d0ba | 1519 | * If A is negative, then the current y represents a negative value. |
marcozecchini | 0:9fca2b23d0ba | 1520 | * Flipping it to the positive side. |
marcozecchini | 0:9fca2b23d0ba | 1521 | */ |
marcozecchini | 0:9fca2b23d0ba | 1522 | if( A->s < 0 && y != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1523 | y = b - y; |
marcozecchini | 0:9fca2b23d0ba | 1524 | |
marcozecchini | 0:9fca2b23d0ba | 1525 | *r = y; |
marcozecchini | 0:9fca2b23d0ba | 1526 | |
marcozecchini | 0:9fca2b23d0ba | 1527 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1528 | } |
marcozecchini | 0:9fca2b23d0ba | 1529 | |
marcozecchini | 0:9fca2b23d0ba | 1530 | /* |
marcozecchini | 0:9fca2b23d0ba | 1531 | * Fast Montgomery initialization (thanks to Tom St Denis) |
marcozecchini | 0:9fca2b23d0ba | 1532 | */ |
marcozecchini | 0:9fca2b23d0ba | 1533 | static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N ) |
marcozecchini | 0:9fca2b23d0ba | 1534 | { |
marcozecchini | 0:9fca2b23d0ba | 1535 | mbedtls_mpi_uint x, m0 = N->p[0]; |
marcozecchini | 0:9fca2b23d0ba | 1536 | unsigned int i; |
marcozecchini | 0:9fca2b23d0ba | 1537 | |
marcozecchini | 0:9fca2b23d0ba | 1538 | x = m0; |
marcozecchini | 0:9fca2b23d0ba | 1539 | x += ( ( m0 + 2 ) & 4 ) << 1; |
marcozecchini | 0:9fca2b23d0ba | 1540 | |
marcozecchini | 0:9fca2b23d0ba | 1541 | for( i = biL; i >= 8; i /= 2 ) |
marcozecchini | 0:9fca2b23d0ba | 1542 | x *= ( 2 - ( m0 * x ) ); |
marcozecchini | 0:9fca2b23d0ba | 1543 | |
marcozecchini | 0:9fca2b23d0ba | 1544 | *mm = ~x + 1; |
marcozecchini | 0:9fca2b23d0ba | 1545 | } |
marcozecchini | 0:9fca2b23d0ba | 1546 | |
marcozecchini | 0:9fca2b23d0ba | 1547 | /* |
marcozecchini | 0:9fca2b23d0ba | 1548 | * Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36) |
marcozecchini | 0:9fca2b23d0ba | 1549 | */ |
marcozecchini | 0:9fca2b23d0ba | 1550 | static int mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm, |
marcozecchini | 0:9fca2b23d0ba | 1551 | const mbedtls_mpi *T ) |
marcozecchini | 0:9fca2b23d0ba | 1552 | { |
marcozecchini | 0:9fca2b23d0ba | 1553 | size_t i, n, m; |
marcozecchini | 0:9fca2b23d0ba | 1554 | mbedtls_mpi_uint u0, u1, *d; |
marcozecchini | 0:9fca2b23d0ba | 1555 | |
marcozecchini | 0:9fca2b23d0ba | 1556 | if( T->n < N->n + 1 || T->p == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 1557 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 1558 | |
marcozecchini | 0:9fca2b23d0ba | 1559 | memset( T->p, 0, T->n * ciL ); |
marcozecchini | 0:9fca2b23d0ba | 1560 | |
marcozecchini | 0:9fca2b23d0ba | 1561 | d = T->p; |
marcozecchini | 0:9fca2b23d0ba | 1562 | n = N->n; |
marcozecchini | 0:9fca2b23d0ba | 1563 | m = ( B->n < n ) ? B->n : n; |
marcozecchini | 0:9fca2b23d0ba | 1564 | |
marcozecchini | 0:9fca2b23d0ba | 1565 | for( i = 0; i < n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 1566 | { |
marcozecchini | 0:9fca2b23d0ba | 1567 | /* |
marcozecchini | 0:9fca2b23d0ba | 1568 | * T = (T + u0*B + u1*N) / 2^biL |
marcozecchini | 0:9fca2b23d0ba | 1569 | */ |
marcozecchini | 0:9fca2b23d0ba | 1570 | u0 = A->p[i]; |
marcozecchini | 0:9fca2b23d0ba | 1571 | u1 = ( d[0] + u0 * B->p[0] ) * mm; |
marcozecchini | 0:9fca2b23d0ba | 1572 | |
marcozecchini | 0:9fca2b23d0ba | 1573 | mpi_mul_hlp( m, B->p, d, u0 ); |
marcozecchini | 0:9fca2b23d0ba | 1574 | mpi_mul_hlp( n, N->p, d, u1 ); |
marcozecchini | 0:9fca2b23d0ba | 1575 | |
marcozecchini | 0:9fca2b23d0ba | 1576 | *d++ = u0; d[n + 1] = 0; |
marcozecchini | 0:9fca2b23d0ba | 1577 | } |
marcozecchini | 0:9fca2b23d0ba | 1578 | |
marcozecchini | 0:9fca2b23d0ba | 1579 | memcpy( A->p, d, ( n + 1 ) * ciL ); |
marcozecchini | 0:9fca2b23d0ba | 1580 | |
marcozecchini | 0:9fca2b23d0ba | 1581 | if( mbedtls_mpi_cmp_abs( A, N ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1582 | mpi_sub_hlp( n, N->p, A->p ); |
marcozecchini | 0:9fca2b23d0ba | 1583 | else |
marcozecchini | 0:9fca2b23d0ba | 1584 | /* prevent timing attacks */ |
marcozecchini | 0:9fca2b23d0ba | 1585 | mpi_sub_hlp( n, A->p, T->p ); |
marcozecchini | 0:9fca2b23d0ba | 1586 | |
marcozecchini | 0:9fca2b23d0ba | 1587 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1588 | } |
marcozecchini | 0:9fca2b23d0ba | 1589 | |
marcozecchini | 0:9fca2b23d0ba | 1590 | /* |
marcozecchini | 0:9fca2b23d0ba | 1591 | * Montgomery reduction: A = A * R^-1 mod N |
marcozecchini | 0:9fca2b23d0ba | 1592 | */ |
marcozecchini | 0:9fca2b23d0ba | 1593 | static int mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T ) |
marcozecchini | 0:9fca2b23d0ba | 1594 | { |
marcozecchini | 0:9fca2b23d0ba | 1595 | mbedtls_mpi_uint z = 1; |
marcozecchini | 0:9fca2b23d0ba | 1596 | mbedtls_mpi U; |
marcozecchini | 0:9fca2b23d0ba | 1597 | |
marcozecchini | 0:9fca2b23d0ba | 1598 | U.n = U.s = (int) z; |
marcozecchini | 0:9fca2b23d0ba | 1599 | U.p = &z; |
marcozecchini | 0:9fca2b23d0ba | 1600 | |
marcozecchini | 0:9fca2b23d0ba | 1601 | return( mpi_montmul( A, &U, N, mm, T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1602 | } |
marcozecchini | 0:9fca2b23d0ba | 1603 | |
marcozecchini | 0:9fca2b23d0ba | 1604 | /* |
marcozecchini | 0:9fca2b23d0ba | 1605 | * Sliding-window exponentiation: X = A^E mod N (HAC 14.85) |
marcozecchini | 0:9fca2b23d0ba | 1606 | */ |
marcozecchini | 0:9fca2b23d0ba | 1607 | int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR ) |
marcozecchini | 0:9fca2b23d0ba | 1608 | { |
marcozecchini | 0:9fca2b23d0ba | 1609 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 1610 | size_t wbits, wsize, one = 1; |
marcozecchini | 0:9fca2b23d0ba | 1611 | size_t i, j, nblimbs; |
marcozecchini | 0:9fca2b23d0ba | 1612 | size_t bufsize, nbits; |
marcozecchini | 0:9fca2b23d0ba | 1613 | mbedtls_mpi_uint ei, mm, state; |
marcozecchini | 0:9fca2b23d0ba | 1614 | mbedtls_mpi RR, T, W[ 2 << MBEDTLS_MPI_WINDOW_SIZE ], Apos; |
marcozecchini | 0:9fca2b23d0ba | 1615 | int neg; |
marcozecchini | 0:9fca2b23d0ba | 1616 | |
marcozecchini | 0:9fca2b23d0ba | 1617 | if( mbedtls_mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1618 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 1619 | |
marcozecchini | 0:9fca2b23d0ba | 1620 | if( mbedtls_mpi_cmp_int( E, 0 ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1621 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 1622 | |
marcozecchini | 0:9fca2b23d0ba | 1623 | /* |
marcozecchini | 0:9fca2b23d0ba | 1624 | * Init temps and window size |
marcozecchini | 0:9fca2b23d0ba | 1625 | */ |
marcozecchini | 0:9fca2b23d0ba | 1626 | mpi_montg_init( &mm, N ); |
marcozecchini | 0:9fca2b23d0ba | 1627 | mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T ); |
marcozecchini | 0:9fca2b23d0ba | 1628 | mbedtls_mpi_init( &Apos ); |
marcozecchini | 0:9fca2b23d0ba | 1629 | memset( W, 0, sizeof( W ) ); |
marcozecchini | 0:9fca2b23d0ba | 1630 | |
marcozecchini | 0:9fca2b23d0ba | 1631 | i = mbedtls_mpi_bitlen( E ); |
marcozecchini | 0:9fca2b23d0ba | 1632 | |
marcozecchini | 0:9fca2b23d0ba | 1633 | wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 : |
marcozecchini | 0:9fca2b23d0ba | 1634 | ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1; |
marcozecchini | 0:9fca2b23d0ba | 1635 | |
marcozecchini | 0:9fca2b23d0ba | 1636 | if( wsize > MBEDTLS_MPI_WINDOW_SIZE ) |
marcozecchini | 0:9fca2b23d0ba | 1637 | wsize = MBEDTLS_MPI_WINDOW_SIZE; |
marcozecchini | 0:9fca2b23d0ba | 1638 | |
marcozecchini | 0:9fca2b23d0ba | 1639 | j = N->n + 1; |
marcozecchini | 0:9fca2b23d0ba | 1640 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) ); |
marcozecchini | 0:9fca2b23d0ba | 1641 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) ); |
marcozecchini | 0:9fca2b23d0ba | 1642 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1643 | |
marcozecchini | 0:9fca2b23d0ba | 1644 | /* |
marcozecchini | 0:9fca2b23d0ba | 1645 | * Compensate for negative A (and correct at the end) |
marcozecchini | 0:9fca2b23d0ba | 1646 | */ |
marcozecchini | 0:9fca2b23d0ba | 1647 | neg = ( A->s == -1 ); |
marcozecchini | 0:9fca2b23d0ba | 1648 | if( neg ) |
marcozecchini | 0:9fca2b23d0ba | 1649 | { |
marcozecchini | 0:9fca2b23d0ba | 1650 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 1651 | Apos.s = 1; |
marcozecchini | 0:9fca2b23d0ba | 1652 | A = &Apos; |
marcozecchini | 0:9fca2b23d0ba | 1653 | } |
marcozecchini | 0:9fca2b23d0ba | 1654 | |
marcozecchini | 0:9fca2b23d0ba | 1655 | /* |
marcozecchini | 0:9fca2b23d0ba | 1656 | * If 1st call, pre-compute R^2 mod N |
marcozecchini | 0:9fca2b23d0ba | 1657 | */ |
marcozecchini | 0:9fca2b23d0ba | 1658 | if( _RR == NULL || _RR->p == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 1659 | { |
marcozecchini | 0:9fca2b23d0ba | 1660 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1661 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) ); |
marcozecchini | 0:9fca2b23d0ba | 1662 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1663 | |
marcozecchini | 0:9fca2b23d0ba | 1664 | if( _RR != NULL ) |
marcozecchini | 0:9fca2b23d0ba | 1665 | memcpy( _RR, &RR, sizeof( mbedtls_mpi ) ); |
marcozecchini | 0:9fca2b23d0ba | 1666 | } |
marcozecchini | 0:9fca2b23d0ba | 1667 | else |
marcozecchini | 0:9fca2b23d0ba | 1668 | memcpy( &RR, _RR, sizeof( mbedtls_mpi ) ); |
marcozecchini | 0:9fca2b23d0ba | 1669 | |
marcozecchini | 0:9fca2b23d0ba | 1670 | /* |
marcozecchini | 0:9fca2b23d0ba | 1671 | * W[1] = A * R^2 * R^-1 mod N = A * R mod N |
marcozecchini | 0:9fca2b23d0ba | 1672 | */ |
marcozecchini | 0:9fca2b23d0ba | 1673 | if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1674 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1675 | else |
marcozecchini | 0:9fca2b23d0ba | 1676 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) ); |
marcozecchini | 0:9fca2b23d0ba | 1677 | |
marcozecchini | 0:9fca2b23d0ba | 1678 | MBEDTLS_MPI_CHK( mpi_montmul( &W[1], &RR, N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1679 | |
marcozecchini | 0:9fca2b23d0ba | 1680 | /* |
marcozecchini | 0:9fca2b23d0ba | 1681 | * X = R^2 * R^-1 mod N = R mod N |
marcozecchini | 0:9fca2b23d0ba | 1682 | */ |
marcozecchini | 0:9fca2b23d0ba | 1683 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) ); |
marcozecchini | 0:9fca2b23d0ba | 1684 | MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1685 | |
marcozecchini | 0:9fca2b23d0ba | 1686 | if( wsize > 1 ) |
marcozecchini | 0:9fca2b23d0ba | 1687 | { |
marcozecchini | 0:9fca2b23d0ba | 1688 | /* |
marcozecchini | 0:9fca2b23d0ba | 1689 | * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1) |
marcozecchini | 0:9fca2b23d0ba | 1690 | */ |
marcozecchini | 0:9fca2b23d0ba | 1691 | j = one << ( wsize - 1 ); |
marcozecchini | 0:9fca2b23d0ba | 1692 | |
marcozecchini | 0:9fca2b23d0ba | 1693 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1694 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) ); |
marcozecchini | 0:9fca2b23d0ba | 1695 | |
marcozecchini | 0:9fca2b23d0ba | 1696 | for( i = 0; i < wsize - 1; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 1697 | MBEDTLS_MPI_CHK( mpi_montmul( &W[j], &W[j], N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1698 | |
marcozecchini | 0:9fca2b23d0ba | 1699 | /* |
marcozecchini | 0:9fca2b23d0ba | 1700 | * W[i] = W[i - 1] * W[1] |
marcozecchini | 0:9fca2b23d0ba | 1701 | */ |
marcozecchini | 0:9fca2b23d0ba | 1702 | for( i = j + 1; i < ( one << wsize ); i++ ) |
marcozecchini | 0:9fca2b23d0ba | 1703 | { |
marcozecchini | 0:9fca2b23d0ba | 1704 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1705 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) ); |
marcozecchini | 0:9fca2b23d0ba | 1706 | |
marcozecchini | 0:9fca2b23d0ba | 1707 | MBEDTLS_MPI_CHK( mpi_montmul( &W[i], &W[1], N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1708 | } |
marcozecchini | 0:9fca2b23d0ba | 1709 | } |
marcozecchini | 0:9fca2b23d0ba | 1710 | |
marcozecchini | 0:9fca2b23d0ba | 1711 | nblimbs = E->n; |
marcozecchini | 0:9fca2b23d0ba | 1712 | bufsize = 0; |
marcozecchini | 0:9fca2b23d0ba | 1713 | nbits = 0; |
marcozecchini | 0:9fca2b23d0ba | 1714 | wbits = 0; |
marcozecchini | 0:9fca2b23d0ba | 1715 | state = 0; |
marcozecchini | 0:9fca2b23d0ba | 1716 | |
marcozecchini | 0:9fca2b23d0ba | 1717 | while( 1 ) |
marcozecchini | 0:9fca2b23d0ba | 1718 | { |
marcozecchini | 0:9fca2b23d0ba | 1719 | if( bufsize == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1720 | { |
marcozecchini | 0:9fca2b23d0ba | 1721 | if( nblimbs == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1722 | break; |
marcozecchini | 0:9fca2b23d0ba | 1723 | |
marcozecchini | 0:9fca2b23d0ba | 1724 | nblimbs--; |
marcozecchini | 0:9fca2b23d0ba | 1725 | |
marcozecchini | 0:9fca2b23d0ba | 1726 | bufsize = sizeof( mbedtls_mpi_uint ) << 3; |
marcozecchini | 0:9fca2b23d0ba | 1727 | } |
marcozecchini | 0:9fca2b23d0ba | 1728 | |
marcozecchini | 0:9fca2b23d0ba | 1729 | bufsize--; |
marcozecchini | 0:9fca2b23d0ba | 1730 | |
marcozecchini | 0:9fca2b23d0ba | 1731 | ei = (E->p[nblimbs] >> bufsize) & 1; |
marcozecchini | 0:9fca2b23d0ba | 1732 | |
marcozecchini | 0:9fca2b23d0ba | 1733 | /* |
marcozecchini | 0:9fca2b23d0ba | 1734 | * skip leading 0s |
marcozecchini | 0:9fca2b23d0ba | 1735 | */ |
marcozecchini | 0:9fca2b23d0ba | 1736 | if( ei == 0 && state == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1737 | continue; |
marcozecchini | 0:9fca2b23d0ba | 1738 | |
marcozecchini | 0:9fca2b23d0ba | 1739 | if( ei == 0 && state == 1 ) |
marcozecchini | 0:9fca2b23d0ba | 1740 | { |
marcozecchini | 0:9fca2b23d0ba | 1741 | /* |
marcozecchini | 0:9fca2b23d0ba | 1742 | * out of window, square X |
marcozecchini | 0:9fca2b23d0ba | 1743 | */ |
marcozecchini | 0:9fca2b23d0ba | 1744 | MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1745 | continue; |
marcozecchini | 0:9fca2b23d0ba | 1746 | } |
marcozecchini | 0:9fca2b23d0ba | 1747 | |
marcozecchini | 0:9fca2b23d0ba | 1748 | /* |
marcozecchini | 0:9fca2b23d0ba | 1749 | * add ei to current window |
marcozecchini | 0:9fca2b23d0ba | 1750 | */ |
marcozecchini | 0:9fca2b23d0ba | 1751 | state = 2; |
marcozecchini | 0:9fca2b23d0ba | 1752 | |
marcozecchini | 0:9fca2b23d0ba | 1753 | nbits++; |
marcozecchini | 0:9fca2b23d0ba | 1754 | wbits |= ( ei << ( wsize - nbits ) ); |
marcozecchini | 0:9fca2b23d0ba | 1755 | |
marcozecchini | 0:9fca2b23d0ba | 1756 | if( nbits == wsize ) |
marcozecchini | 0:9fca2b23d0ba | 1757 | { |
marcozecchini | 0:9fca2b23d0ba | 1758 | /* |
marcozecchini | 0:9fca2b23d0ba | 1759 | * X = X^wsize R^-1 mod N |
marcozecchini | 0:9fca2b23d0ba | 1760 | */ |
marcozecchini | 0:9fca2b23d0ba | 1761 | for( i = 0; i < wsize; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 1762 | MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1763 | |
marcozecchini | 0:9fca2b23d0ba | 1764 | /* |
marcozecchini | 0:9fca2b23d0ba | 1765 | * X = X * W[wbits] R^-1 mod N |
marcozecchini | 0:9fca2b23d0ba | 1766 | */ |
marcozecchini | 0:9fca2b23d0ba | 1767 | MBEDTLS_MPI_CHK( mpi_montmul( X, &W[wbits], N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1768 | |
marcozecchini | 0:9fca2b23d0ba | 1769 | state--; |
marcozecchini | 0:9fca2b23d0ba | 1770 | nbits = 0; |
marcozecchini | 0:9fca2b23d0ba | 1771 | wbits = 0; |
marcozecchini | 0:9fca2b23d0ba | 1772 | } |
marcozecchini | 0:9fca2b23d0ba | 1773 | } |
marcozecchini | 0:9fca2b23d0ba | 1774 | |
marcozecchini | 0:9fca2b23d0ba | 1775 | /* |
marcozecchini | 0:9fca2b23d0ba | 1776 | * process the remaining bits |
marcozecchini | 0:9fca2b23d0ba | 1777 | */ |
marcozecchini | 0:9fca2b23d0ba | 1778 | for( i = 0; i < nbits; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 1779 | { |
marcozecchini | 0:9fca2b23d0ba | 1780 | MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1781 | |
marcozecchini | 0:9fca2b23d0ba | 1782 | wbits <<= 1; |
marcozecchini | 0:9fca2b23d0ba | 1783 | |
marcozecchini | 0:9fca2b23d0ba | 1784 | if( ( wbits & ( one << wsize ) ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1785 | MBEDTLS_MPI_CHK( mpi_montmul( X, &W[1], N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1786 | } |
marcozecchini | 0:9fca2b23d0ba | 1787 | |
marcozecchini | 0:9fca2b23d0ba | 1788 | /* |
marcozecchini | 0:9fca2b23d0ba | 1789 | * X = A^E * R * R^-1 mod N = A^E mod N |
marcozecchini | 0:9fca2b23d0ba | 1790 | */ |
marcozecchini | 0:9fca2b23d0ba | 1791 | MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) ); |
marcozecchini | 0:9fca2b23d0ba | 1792 | |
marcozecchini | 0:9fca2b23d0ba | 1793 | if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1794 | { |
marcozecchini | 0:9fca2b23d0ba | 1795 | X->s = -1; |
marcozecchini | 0:9fca2b23d0ba | 1796 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) ); |
marcozecchini | 0:9fca2b23d0ba | 1797 | } |
marcozecchini | 0:9fca2b23d0ba | 1798 | |
marcozecchini | 0:9fca2b23d0ba | 1799 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1800 | |
marcozecchini | 0:9fca2b23d0ba | 1801 | for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ ) |
marcozecchini | 0:9fca2b23d0ba | 1802 | mbedtls_mpi_free( &W[i] ); |
marcozecchini | 0:9fca2b23d0ba | 1803 | |
marcozecchini | 0:9fca2b23d0ba | 1804 | mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos ); |
marcozecchini | 0:9fca2b23d0ba | 1805 | |
marcozecchini | 0:9fca2b23d0ba | 1806 | if( _RR == NULL || _RR->p == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 1807 | mbedtls_mpi_free( &RR ); |
marcozecchini | 0:9fca2b23d0ba | 1808 | |
marcozecchini | 0:9fca2b23d0ba | 1809 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1810 | } |
marcozecchini | 0:9fca2b23d0ba | 1811 | |
marcozecchini | 0:9fca2b23d0ba | 1812 | /* |
marcozecchini | 0:9fca2b23d0ba | 1813 | * Greatest common divisor: G = gcd(A, B) (HAC 14.54) |
marcozecchini | 0:9fca2b23d0ba | 1814 | */ |
marcozecchini | 0:9fca2b23d0ba | 1815 | int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B ) |
marcozecchini | 0:9fca2b23d0ba | 1816 | { |
marcozecchini | 0:9fca2b23d0ba | 1817 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 1818 | size_t lz, lzt; |
marcozecchini | 0:9fca2b23d0ba | 1819 | mbedtls_mpi TG, TA, TB; |
marcozecchini | 0:9fca2b23d0ba | 1820 | |
marcozecchini | 0:9fca2b23d0ba | 1821 | mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB ); |
marcozecchini | 0:9fca2b23d0ba | 1822 | |
marcozecchini | 0:9fca2b23d0ba | 1823 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); |
marcozecchini | 0:9fca2b23d0ba | 1824 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); |
marcozecchini | 0:9fca2b23d0ba | 1825 | |
marcozecchini | 0:9fca2b23d0ba | 1826 | lz = mbedtls_mpi_lsb( &TA ); |
marcozecchini | 0:9fca2b23d0ba | 1827 | lzt = mbedtls_mpi_lsb( &TB ); |
marcozecchini | 0:9fca2b23d0ba | 1828 | |
marcozecchini | 0:9fca2b23d0ba | 1829 | if( lzt < lz ) |
marcozecchini | 0:9fca2b23d0ba | 1830 | lz = lzt; |
marcozecchini | 0:9fca2b23d0ba | 1831 | |
marcozecchini | 0:9fca2b23d0ba | 1832 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) ); |
marcozecchini | 0:9fca2b23d0ba | 1833 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) ); |
marcozecchini | 0:9fca2b23d0ba | 1834 | |
marcozecchini | 0:9fca2b23d0ba | 1835 | TA.s = TB.s = 1; |
marcozecchini | 0:9fca2b23d0ba | 1836 | |
marcozecchini | 0:9fca2b23d0ba | 1837 | while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1838 | { |
marcozecchini | 0:9fca2b23d0ba | 1839 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 1840 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) ); |
marcozecchini | 0:9fca2b23d0ba | 1841 | |
marcozecchini | 0:9fca2b23d0ba | 1842 | if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1843 | { |
marcozecchini | 0:9fca2b23d0ba | 1844 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) ); |
marcozecchini | 0:9fca2b23d0ba | 1845 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1846 | } |
marcozecchini | 0:9fca2b23d0ba | 1847 | else |
marcozecchini | 0:9fca2b23d0ba | 1848 | { |
marcozecchini | 0:9fca2b23d0ba | 1849 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) ); |
marcozecchini | 0:9fca2b23d0ba | 1850 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1851 | } |
marcozecchini | 0:9fca2b23d0ba | 1852 | } |
marcozecchini | 0:9fca2b23d0ba | 1853 | |
marcozecchini | 0:9fca2b23d0ba | 1854 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) ); |
marcozecchini | 0:9fca2b23d0ba | 1855 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) ); |
marcozecchini | 0:9fca2b23d0ba | 1856 | |
marcozecchini | 0:9fca2b23d0ba | 1857 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1858 | |
marcozecchini | 0:9fca2b23d0ba | 1859 | mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB ); |
marcozecchini | 0:9fca2b23d0ba | 1860 | |
marcozecchini | 0:9fca2b23d0ba | 1861 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1862 | } |
marcozecchini | 0:9fca2b23d0ba | 1863 | |
marcozecchini | 0:9fca2b23d0ba | 1864 | /* |
marcozecchini | 0:9fca2b23d0ba | 1865 | * Fill X with size bytes of random. |
marcozecchini | 0:9fca2b23d0ba | 1866 | * |
marcozecchini | 0:9fca2b23d0ba | 1867 | * Use a temporary bytes representation to make sure the result is the same |
marcozecchini | 0:9fca2b23d0ba | 1868 | * regardless of the platform endianness (useful when f_rng is actually |
marcozecchini | 0:9fca2b23d0ba | 1869 | * deterministic, eg for tests). |
marcozecchini | 0:9fca2b23d0ba | 1870 | */ |
marcozecchini | 0:9fca2b23d0ba | 1871 | int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, |
marcozecchini | 0:9fca2b23d0ba | 1872 | int (*f_rng)(void *, unsigned char *, size_t), |
marcozecchini | 0:9fca2b23d0ba | 1873 | void *p_rng ) |
marcozecchini | 0:9fca2b23d0ba | 1874 | { |
marcozecchini | 0:9fca2b23d0ba | 1875 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 1876 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
marcozecchini | 0:9fca2b23d0ba | 1877 | |
marcozecchini | 0:9fca2b23d0ba | 1878 | if( size > MBEDTLS_MPI_MAX_SIZE ) |
marcozecchini | 0:9fca2b23d0ba | 1879 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 1880 | |
marcozecchini | 0:9fca2b23d0ba | 1881 | MBEDTLS_MPI_CHK( f_rng( p_rng, buf, size ) ); |
marcozecchini | 0:9fca2b23d0ba | 1882 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( X, buf, size ) ); |
marcozecchini | 0:9fca2b23d0ba | 1883 | |
marcozecchini | 0:9fca2b23d0ba | 1884 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1885 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1886 | } |
marcozecchini | 0:9fca2b23d0ba | 1887 | |
marcozecchini | 0:9fca2b23d0ba | 1888 | /* |
marcozecchini | 0:9fca2b23d0ba | 1889 | * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64) |
marcozecchini | 0:9fca2b23d0ba | 1890 | */ |
marcozecchini | 0:9fca2b23d0ba | 1891 | int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N ) |
marcozecchini | 0:9fca2b23d0ba | 1892 | { |
marcozecchini | 0:9fca2b23d0ba | 1893 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 1894 | mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2; |
marcozecchini | 0:9fca2b23d0ba | 1895 | |
marcozecchini | 0:9fca2b23d0ba | 1896 | if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1897 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 1898 | |
marcozecchini | 0:9fca2b23d0ba | 1899 | mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 ); |
marcozecchini | 0:9fca2b23d0ba | 1900 | mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV ); |
marcozecchini | 0:9fca2b23d0ba | 1901 | mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 ); |
marcozecchini | 0:9fca2b23d0ba | 1902 | |
marcozecchini | 0:9fca2b23d0ba | 1903 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1904 | |
marcozecchini | 0:9fca2b23d0ba | 1905 | if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1906 | { |
marcozecchini | 0:9fca2b23d0ba | 1907 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
marcozecchini | 0:9fca2b23d0ba | 1908 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 1909 | } |
marcozecchini | 0:9fca2b23d0ba | 1910 | |
marcozecchini | 0:9fca2b23d0ba | 1911 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1912 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) ); |
marcozecchini | 0:9fca2b23d0ba | 1913 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1914 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1915 | |
marcozecchini | 0:9fca2b23d0ba | 1916 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1917 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1918 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1919 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1920 | |
marcozecchini | 0:9fca2b23d0ba | 1921 | do |
marcozecchini | 0:9fca2b23d0ba | 1922 | { |
marcozecchini | 0:9fca2b23d0ba | 1923 | while( ( TU.p[0] & 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1924 | { |
marcozecchini | 0:9fca2b23d0ba | 1925 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1926 | |
marcozecchini | 0:9fca2b23d0ba | 1927 | if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1928 | { |
marcozecchini | 0:9fca2b23d0ba | 1929 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) ); |
marcozecchini | 0:9fca2b23d0ba | 1930 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) ); |
marcozecchini | 0:9fca2b23d0ba | 1931 | } |
marcozecchini | 0:9fca2b23d0ba | 1932 | |
marcozecchini | 0:9fca2b23d0ba | 1933 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1934 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1935 | } |
marcozecchini | 0:9fca2b23d0ba | 1936 | |
marcozecchini | 0:9fca2b23d0ba | 1937 | while( ( TV.p[0] & 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1938 | { |
marcozecchini | 0:9fca2b23d0ba | 1939 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1940 | |
marcozecchini | 0:9fca2b23d0ba | 1941 | if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1942 | { |
marcozecchini | 0:9fca2b23d0ba | 1943 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) ); |
marcozecchini | 0:9fca2b23d0ba | 1944 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) ); |
marcozecchini | 0:9fca2b23d0ba | 1945 | } |
marcozecchini | 0:9fca2b23d0ba | 1946 | |
marcozecchini | 0:9fca2b23d0ba | 1947 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1948 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1949 | } |
marcozecchini | 0:9fca2b23d0ba | 1950 | |
marcozecchini | 0:9fca2b23d0ba | 1951 | if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1952 | { |
marcozecchini | 0:9fca2b23d0ba | 1953 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) ); |
marcozecchini | 0:9fca2b23d0ba | 1954 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1955 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1956 | } |
marcozecchini | 0:9fca2b23d0ba | 1957 | else |
marcozecchini | 0:9fca2b23d0ba | 1958 | { |
marcozecchini | 0:9fca2b23d0ba | 1959 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) ); |
marcozecchini | 0:9fca2b23d0ba | 1960 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1961 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1962 | } |
marcozecchini | 0:9fca2b23d0ba | 1963 | } |
marcozecchini | 0:9fca2b23d0ba | 1964 | while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 ); |
marcozecchini | 0:9fca2b23d0ba | 1965 | |
marcozecchini | 0:9fca2b23d0ba | 1966 | while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1967 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1968 | |
marcozecchini | 0:9fca2b23d0ba | 1969 | while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 1970 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) ); |
marcozecchini | 0:9fca2b23d0ba | 1971 | |
marcozecchini | 0:9fca2b23d0ba | 1972 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 1973 | |
marcozecchini | 0:9fca2b23d0ba | 1974 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 1975 | |
marcozecchini | 0:9fca2b23d0ba | 1976 | mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 ); |
marcozecchini | 0:9fca2b23d0ba | 1977 | mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV ); |
marcozecchini | 0:9fca2b23d0ba | 1978 | mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 ); |
marcozecchini | 0:9fca2b23d0ba | 1979 | |
marcozecchini | 0:9fca2b23d0ba | 1980 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 1981 | } |
marcozecchini | 0:9fca2b23d0ba | 1982 | |
marcozecchini | 0:9fca2b23d0ba | 1983 | #if defined(MBEDTLS_GENPRIME) |
marcozecchini | 0:9fca2b23d0ba | 1984 | |
marcozecchini | 0:9fca2b23d0ba | 1985 | static const int small_prime[] = |
marcozecchini | 0:9fca2b23d0ba | 1986 | { |
marcozecchini | 0:9fca2b23d0ba | 1987 | 3, 5, 7, 11, 13, 17, 19, 23, |
marcozecchini | 0:9fca2b23d0ba | 1988 | 29, 31, 37, 41, 43, 47, 53, 59, |
marcozecchini | 0:9fca2b23d0ba | 1989 | 61, 67, 71, 73, 79, 83, 89, 97, |
marcozecchini | 0:9fca2b23d0ba | 1990 | 101, 103, 107, 109, 113, 127, 131, 137, |
marcozecchini | 0:9fca2b23d0ba | 1991 | 139, 149, 151, 157, 163, 167, 173, 179, |
marcozecchini | 0:9fca2b23d0ba | 1992 | 181, 191, 193, 197, 199, 211, 223, 227, |
marcozecchini | 0:9fca2b23d0ba | 1993 | 229, 233, 239, 241, 251, 257, 263, 269, |
marcozecchini | 0:9fca2b23d0ba | 1994 | 271, 277, 281, 283, 293, 307, 311, 313, |
marcozecchini | 0:9fca2b23d0ba | 1995 | 317, 331, 337, 347, 349, 353, 359, 367, |
marcozecchini | 0:9fca2b23d0ba | 1996 | 373, 379, 383, 389, 397, 401, 409, 419, |
marcozecchini | 0:9fca2b23d0ba | 1997 | 421, 431, 433, 439, 443, 449, 457, 461, |
marcozecchini | 0:9fca2b23d0ba | 1998 | 463, 467, 479, 487, 491, 499, 503, 509, |
marcozecchini | 0:9fca2b23d0ba | 1999 | 521, 523, 541, 547, 557, 563, 569, 571, |
marcozecchini | 0:9fca2b23d0ba | 2000 | 577, 587, 593, 599, 601, 607, 613, 617, |
marcozecchini | 0:9fca2b23d0ba | 2001 | 619, 631, 641, 643, 647, 653, 659, 661, |
marcozecchini | 0:9fca2b23d0ba | 2002 | 673, 677, 683, 691, 701, 709, 719, 727, |
marcozecchini | 0:9fca2b23d0ba | 2003 | 733, 739, 743, 751, 757, 761, 769, 773, |
marcozecchini | 0:9fca2b23d0ba | 2004 | 787, 797, 809, 811, 821, 823, 827, 829, |
marcozecchini | 0:9fca2b23d0ba | 2005 | 839, 853, 857, 859, 863, 877, 881, 883, |
marcozecchini | 0:9fca2b23d0ba | 2006 | 887, 907, 911, 919, 929, 937, 941, 947, |
marcozecchini | 0:9fca2b23d0ba | 2007 | 953, 967, 971, 977, 983, 991, 997, -103 |
marcozecchini | 0:9fca2b23d0ba | 2008 | }; |
marcozecchini | 0:9fca2b23d0ba | 2009 | |
marcozecchini | 0:9fca2b23d0ba | 2010 | /* |
marcozecchini | 0:9fca2b23d0ba | 2011 | * Small divisors test (X must be positive) |
marcozecchini | 0:9fca2b23d0ba | 2012 | * |
marcozecchini | 0:9fca2b23d0ba | 2013 | * Return values: |
marcozecchini | 0:9fca2b23d0ba | 2014 | * 0: no small factor (possible prime, more tests needed) |
marcozecchini | 0:9fca2b23d0ba | 2015 | * 1: certain prime |
marcozecchini | 0:9fca2b23d0ba | 2016 | * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime |
marcozecchini | 0:9fca2b23d0ba | 2017 | * other negative: error |
marcozecchini | 0:9fca2b23d0ba | 2018 | */ |
marcozecchini | 0:9fca2b23d0ba | 2019 | static int mpi_check_small_factors( const mbedtls_mpi *X ) |
marcozecchini | 0:9fca2b23d0ba | 2020 | { |
marcozecchini | 0:9fca2b23d0ba | 2021 | int ret = 0; |
marcozecchini | 0:9fca2b23d0ba | 2022 | size_t i; |
marcozecchini | 0:9fca2b23d0ba | 2023 | mbedtls_mpi_uint r; |
marcozecchini | 0:9fca2b23d0ba | 2024 | |
marcozecchini | 0:9fca2b23d0ba | 2025 | if( ( X->p[0] & 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2026 | return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); |
marcozecchini | 0:9fca2b23d0ba | 2027 | |
marcozecchini | 0:9fca2b23d0ba | 2028 | for( i = 0; small_prime[i] > 0; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 2029 | { |
marcozecchini | 0:9fca2b23d0ba | 2030 | if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2031 | return( 1 ); |
marcozecchini | 0:9fca2b23d0ba | 2032 | |
marcozecchini | 0:9fca2b23d0ba | 2033 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) ); |
marcozecchini | 0:9fca2b23d0ba | 2034 | |
marcozecchini | 0:9fca2b23d0ba | 2035 | if( r == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2036 | return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); |
marcozecchini | 0:9fca2b23d0ba | 2037 | } |
marcozecchini | 0:9fca2b23d0ba | 2038 | |
marcozecchini | 0:9fca2b23d0ba | 2039 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 2040 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 2041 | } |
marcozecchini | 0:9fca2b23d0ba | 2042 | |
marcozecchini | 0:9fca2b23d0ba | 2043 | /* |
marcozecchini | 0:9fca2b23d0ba | 2044 | * Miller-Rabin pseudo-primality test (HAC 4.24) |
marcozecchini | 0:9fca2b23d0ba | 2045 | */ |
marcozecchini | 0:9fca2b23d0ba | 2046 | static int mpi_miller_rabin( const mbedtls_mpi *X, |
marcozecchini | 0:9fca2b23d0ba | 2047 | int (*f_rng)(void *, unsigned char *, size_t), |
marcozecchini | 0:9fca2b23d0ba | 2048 | void *p_rng ) |
marcozecchini | 0:9fca2b23d0ba | 2049 | { |
marcozecchini | 0:9fca2b23d0ba | 2050 | int ret, count; |
marcozecchini | 0:9fca2b23d0ba | 2051 | size_t i, j, k, n, s; |
marcozecchini | 0:9fca2b23d0ba | 2052 | mbedtls_mpi W, R, T, A, RR; |
marcozecchini | 0:9fca2b23d0ba | 2053 | |
marcozecchini | 0:9fca2b23d0ba | 2054 | mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A ); |
marcozecchini | 0:9fca2b23d0ba | 2055 | mbedtls_mpi_init( &RR ); |
marcozecchini | 0:9fca2b23d0ba | 2056 | |
marcozecchini | 0:9fca2b23d0ba | 2057 | /* |
marcozecchini | 0:9fca2b23d0ba | 2058 | * W = |X| - 1 |
marcozecchini | 0:9fca2b23d0ba | 2059 | * R = W >> lsb( W ) |
marcozecchini | 0:9fca2b23d0ba | 2060 | */ |
marcozecchini | 0:9fca2b23d0ba | 2061 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2062 | s = mbedtls_mpi_lsb( &W ); |
marcozecchini | 0:9fca2b23d0ba | 2063 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) ); |
marcozecchini | 0:9fca2b23d0ba | 2064 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) ); |
marcozecchini | 0:9fca2b23d0ba | 2065 | |
marcozecchini | 0:9fca2b23d0ba | 2066 | i = mbedtls_mpi_bitlen( X ); |
marcozecchini | 0:9fca2b23d0ba | 2067 | /* |
marcozecchini | 0:9fca2b23d0ba | 2068 | * HAC, table 4.4 |
marcozecchini | 0:9fca2b23d0ba | 2069 | */ |
marcozecchini | 0:9fca2b23d0ba | 2070 | n = ( ( i >= 1300 ) ? 2 : ( i >= 850 ) ? 3 : |
marcozecchini | 0:9fca2b23d0ba | 2071 | ( i >= 650 ) ? 4 : ( i >= 350 ) ? 8 : |
marcozecchini | 0:9fca2b23d0ba | 2072 | ( i >= 250 ) ? 12 : ( i >= 150 ) ? 18 : 27 ); |
marcozecchini | 0:9fca2b23d0ba | 2073 | |
marcozecchini | 0:9fca2b23d0ba | 2074 | for( i = 0; i < n; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 2075 | { |
marcozecchini | 0:9fca2b23d0ba | 2076 | /* |
marcozecchini | 0:9fca2b23d0ba | 2077 | * pick a random A, 1 < A < |X| - 1 |
marcozecchini | 0:9fca2b23d0ba | 2078 | */ |
marcozecchini | 0:9fca2b23d0ba | 2079 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) ); |
marcozecchini | 0:9fca2b23d0ba | 2080 | |
marcozecchini | 0:9fca2b23d0ba | 2081 | if( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2082 | { |
marcozecchini | 0:9fca2b23d0ba | 2083 | j = mbedtls_mpi_bitlen( &A ) - mbedtls_mpi_bitlen( &W ); |
marcozecchini | 0:9fca2b23d0ba | 2084 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j + 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2085 | } |
marcozecchini | 0:9fca2b23d0ba | 2086 | A.p[0] |= 3; |
marcozecchini | 0:9fca2b23d0ba | 2087 | |
marcozecchini | 0:9fca2b23d0ba | 2088 | count = 0; |
marcozecchini | 0:9fca2b23d0ba | 2089 | do { |
marcozecchini | 0:9fca2b23d0ba | 2090 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) ); |
marcozecchini | 0:9fca2b23d0ba | 2091 | |
marcozecchini | 0:9fca2b23d0ba | 2092 | j = mbedtls_mpi_bitlen( &A ); |
marcozecchini | 0:9fca2b23d0ba | 2093 | k = mbedtls_mpi_bitlen( &W ); |
marcozecchini | 0:9fca2b23d0ba | 2094 | if (j > k) { |
marcozecchini | 0:9fca2b23d0ba | 2095 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j - k ) ); |
marcozecchini | 0:9fca2b23d0ba | 2096 | } |
marcozecchini | 0:9fca2b23d0ba | 2097 | |
marcozecchini | 0:9fca2b23d0ba | 2098 | if (count++ > 30) { |
marcozecchini | 0:9fca2b23d0ba | 2099 | return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
marcozecchini | 0:9fca2b23d0ba | 2100 | } |
marcozecchini | 0:9fca2b23d0ba | 2101 | |
marcozecchini | 0:9fca2b23d0ba | 2102 | } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 || |
marcozecchini | 0:9fca2b23d0ba | 2103 | mbedtls_mpi_cmp_int( &A, 1 ) <= 0 ); |
marcozecchini | 0:9fca2b23d0ba | 2104 | |
marcozecchini | 0:9fca2b23d0ba | 2105 | /* |
marcozecchini | 0:9fca2b23d0ba | 2106 | * A = A^R mod |X| |
marcozecchini | 0:9fca2b23d0ba | 2107 | */ |
marcozecchini | 0:9fca2b23d0ba | 2108 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) ); |
marcozecchini | 0:9fca2b23d0ba | 2109 | |
marcozecchini | 0:9fca2b23d0ba | 2110 | if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 || |
marcozecchini | 0:9fca2b23d0ba | 2111 | mbedtls_mpi_cmp_int( &A, 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2112 | continue; |
marcozecchini | 0:9fca2b23d0ba | 2113 | |
marcozecchini | 0:9fca2b23d0ba | 2114 | j = 1; |
marcozecchini | 0:9fca2b23d0ba | 2115 | while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2116 | { |
marcozecchini | 0:9fca2b23d0ba | 2117 | /* |
marcozecchini | 0:9fca2b23d0ba | 2118 | * A = A * A mod |X| |
marcozecchini | 0:9fca2b23d0ba | 2119 | */ |
marcozecchini | 0:9fca2b23d0ba | 2120 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) ); |
marcozecchini | 0:9fca2b23d0ba | 2121 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) ); |
marcozecchini | 0:9fca2b23d0ba | 2122 | |
marcozecchini | 0:9fca2b23d0ba | 2123 | if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2124 | break; |
marcozecchini | 0:9fca2b23d0ba | 2125 | |
marcozecchini | 0:9fca2b23d0ba | 2126 | j++; |
marcozecchini | 0:9fca2b23d0ba | 2127 | } |
marcozecchini | 0:9fca2b23d0ba | 2128 | |
marcozecchini | 0:9fca2b23d0ba | 2129 | /* |
marcozecchini | 0:9fca2b23d0ba | 2130 | * not prime if A != |X| - 1 or A == 1 |
marcozecchini | 0:9fca2b23d0ba | 2131 | */ |
marcozecchini | 0:9fca2b23d0ba | 2132 | if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 || |
marcozecchini | 0:9fca2b23d0ba | 2133 | mbedtls_mpi_cmp_int( &A, 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2134 | { |
marcozecchini | 0:9fca2b23d0ba | 2135 | ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; |
marcozecchini | 0:9fca2b23d0ba | 2136 | break; |
marcozecchini | 0:9fca2b23d0ba | 2137 | } |
marcozecchini | 0:9fca2b23d0ba | 2138 | } |
marcozecchini | 0:9fca2b23d0ba | 2139 | |
marcozecchini | 0:9fca2b23d0ba | 2140 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 2141 | mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A ); |
marcozecchini | 0:9fca2b23d0ba | 2142 | mbedtls_mpi_free( &RR ); |
marcozecchini | 0:9fca2b23d0ba | 2143 | |
marcozecchini | 0:9fca2b23d0ba | 2144 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 2145 | } |
marcozecchini | 0:9fca2b23d0ba | 2146 | |
marcozecchini | 0:9fca2b23d0ba | 2147 | /* |
marcozecchini | 0:9fca2b23d0ba | 2148 | * Pseudo-primality test: small factors, then Miller-Rabin |
marcozecchini | 0:9fca2b23d0ba | 2149 | */ |
marcozecchini | 0:9fca2b23d0ba | 2150 | int mbedtls_mpi_is_prime( const mbedtls_mpi *X, |
marcozecchini | 0:9fca2b23d0ba | 2151 | int (*f_rng)(void *, unsigned char *, size_t), |
marcozecchini | 0:9fca2b23d0ba | 2152 | void *p_rng ) |
marcozecchini | 0:9fca2b23d0ba | 2153 | { |
marcozecchini | 0:9fca2b23d0ba | 2154 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 2155 | mbedtls_mpi XX; |
marcozecchini | 0:9fca2b23d0ba | 2156 | |
marcozecchini | 0:9fca2b23d0ba | 2157 | XX.s = 1; |
marcozecchini | 0:9fca2b23d0ba | 2158 | XX.n = X->n; |
marcozecchini | 0:9fca2b23d0ba | 2159 | XX.p = X->p; |
marcozecchini | 0:9fca2b23d0ba | 2160 | |
marcozecchini | 0:9fca2b23d0ba | 2161 | if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 || |
marcozecchini | 0:9fca2b23d0ba | 2162 | mbedtls_mpi_cmp_int( &XX, 1 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2163 | return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); |
marcozecchini | 0:9fca2b23d0ba | 2164 | |
marcozecchini | 0:9fca2b23d0ba | 2165 | if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2166 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 2167 | |
marcozecchini | 0:9fca2b23d0ba | 2168 | if( ( ret = mpi_check_small_factors( &XX ) ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2169 | { |
marcozecchini | 0:9fca2b23d0ba | 2170 | if( ret == 1 ) |
marcozecchini | 0:9fca2b23d0ba | 2171 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 2172 | |
marcozecchini | 0:9fca2b23d0ba | 2173 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 2174 | } |
marcozecchini | 0:9fca2b23d0ba | 2175 | |
marcozecchini | 0:9fca2b23d0ba | 2176 | return( mpi_miller_rabin( &XX, f_rng, p_rng ) ); |
marcozecchini | 0:9fca2b23d0ba | 2177 | } |
marcozecchini | 0:9fca2b23d0ba | 2178 | |
marcozecchini | 0:9fca2b23d0ba | 2179 | /* |
marcozecchini | 0:9fca2b23d0ba | 2180 | * Prime number generation |
marcozecchini | 0:9fca2b23d0ba | 2181 | */ |
marcozecchini | 0:9fca2b23d0ba | 2182 | int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag, |
marcozecchini | 0:9fca2b23d0ba | 2183 | int (*f_rng)(void *, unsigned char *, size_t), |
marcozecchini | 0:9fca2b23d0ba | 2184 | void *p_rng ) |
marcozecchini | 0:9fca2b23d0ba | 2185 | { |
marcozecchini | 0:9fca2b23d0ba | 2186 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 2187 | size_t k, n; |
marcozecchini | 0:9fca2b23d0ba | 2188 | mbedtls_mpi_uint r; |
marcozecchini | 0:9fca2b23d0ba | 2189 | mbedtls_mpi Y; |
marcozecchini | 0:9fca2b23d0ba | 2190 | |
marcozecchini | 0:9fca2b23d0ba | 2191 | if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS ) |
marcozecchini | 0:9fca2b23d0ba | 2192 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 2193 | |
marcozecchini | 0:9fca2b23d0ba | 2194 | mbedtls_mpi_init( &Y ); |
marcozecchini | 0:9fca2b23d0ba | 2195 | |
marcozecchini | 0:9fca2b23d0ba | 2196 | n = BITS_TO_LIMBS( nbits ); |
marcozecchini | 0:9fca2b23d0ba | 2197 | |
marcozecchini | 0:9fca2b23d0ba | 2198 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) ); |
marcozecchini | 0:9fca2b23d0ba | 2199 | |
marcozecchini | 0:9fca2b23d0ba | 2200 | k = mbedtls_mpi_bitlen( X ); |
marcozecchini | 0:9fca2b23d0ba | 2201 | if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits + 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2202 | |
marcozecchini | 0:9fca2b23d0ba | 2203 | mbedtls_mpi_set_bit( X, nbits-1, 1 ); |
marcozecchini | 0:9fca2b23d0ba | 2204 | |
marcozecchini | 0:9fca2b23d0ba | 2205 | X->p[0] |= 1; |
marcozecchini | 0:9fca2b23d0ba | 2206 | |
marcozecchini | 0:9fca2b23d0ba | 2207 | if( dh_flag == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2208 | { |
marcozecchini | 0:9fca2b23d0ba | 2209 | while( ( ret = mbedtls_mpi_is_prime( X, f_rng, p_rng ) ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2210 | { |
marcozecchini | 0:9fca2b23d0ba | 2211 | if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ) |
marcozecchini | 0:9fca2b23d0ba | 2212 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 2213 | |
marcozecchini | 0:9fca2b23d0ba | 2214 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 2 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2215 | } |
marcozecchini | 0:9fca2b23d0ba | 2216 | } |
marcozecchini | 0:9fca2b23d0ba | 2217 | else |
marcozecchini | 0:9fca2b23d0ba | 2218 | { |
marcozecchini | 0:9fca2b23d0ba | 2219 | /* |
marcozecchini | 0:9fca2b23d0ba | 2220 | * An necessary condition for Y and X = 2Y + 1 to be prime |
marcozecchini | 0:9fca2b23d0ba | 2221 | * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3). |
marcozecchini | 0:9fca2b23d0ba | 2222 | * Make sure it is satisfied, while keeping X = 3 mod 4 |
marcozecchini | 0:9fca2b23d0ba | 2223 | */ |
marcozecchini | 0:9fca2b23d0ba | 2224 | |
marcozecchini | 0:9fca2b23d0ba | 2225 | X->p[0] |= 2; |
marcozecchini | 0:9fca2b23d0ba | 2226 | |
marcozecchini | 0:9fca2b23d0ba | 2227 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2228 | if( r == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2229 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2230 | else if( r == 1 ) |
marcozecchini | 0:9fca2b23d0ba | 2231 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2232 | |
marcozecchini | 0:9fca2b23d0ba | 2233 | /* Set Y = (X-1) / 2, which is X / 2 because X is odd */ |
marcozecchini | 0:9fca2b23d0ba | 2234 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) ); |
marcozecchini | 0:9fca2b23d0ba | 2235 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2236 | |
marcozecchini | 0:9fca2b23d0ba | 2237 | while( 1 ) |
marcozecchini | 0:9fca2b23d0ba | 2238 | { |
marcozecchini | 0:9fca2b23d0ba | 2239 | /* |
marcozecchini | 0:9fca2b23d0ba | 2240 | * First, check small factors for X and Y |
marcozecchini | 0:9fca2b23d0ba | 2241 | * before doing Miller-Rabin on any of them |
marcozecchini | 0:9fca2b23d0ba | 2242 | */ |
marcozecchini | 0:9fca2b23d0ba | 2243 | if( ( ret = mpi_check_small_factors( X ) ) == 0 && |
marcozecchini | 0:9fca2b23d0ba | 2244 | ( ret = mpi_check_small_factors( &Y ) ) == 0 && |
marcozecchini | 0:9fca2b23d0ba | 2245 | ( ret = mpi_miller_rabin( X, f_rng, p_rng ) ) == 0 && |
marcozecchini | 0:9fca2b23d0ba | 2246 | ( ret = mpi_miller_rabin( &Y, f_rng, p_rng ) ) == 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2247 | { |
marcozecchini | 0:9fca2b23d0ba | 2248 | break; |
marcozecchini | 0:9fca2b23d0ba | 2249 | } |
marcozecchini | 0:9fca2b23d0ba | 2250 | |
marcozecchini | 0:9fca2b23d0ba | 2251 | if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ) |
marcozecchini | 0:9fca2b23d0ba | 2252 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 2253 | |
marcozecchini | 0:9fca2b23d0ba | 2254 | /* |
marcozecchini | 0:9fca2b23d0ba | 2255 | * Next candidates. We want to preserve Y = (X-1) / 2 and |
marcozecchini | 0:9fca2b23d0ba | 2256 | * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3) |
marcozecchini | 0:9fca2b23d0ba | 2257 | * so up Y by 6 and X by 12. |
marcozecchini | 0:9fca2b23d0ba | 2258 | */ |
marcozecchini | 0:9fca2b23d0ba | 2259 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2260 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) ); |
marcozecchini | 0:9fca2b23d0ba | 2261 | } |
marcozecchini | 0:9fca2b23d0ba | 2262 | } |
marcozecchini | 0:9fca2b23d0ba | 2263 | |
marcozecchini | 0:9fca2b23d0ba | 2264 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 2265 | |
marcozecchini | 0:9fca2b23d0ba | 2266 | mbedtls_mpi_free( &Y ); |
marcozecchini | 0:9fca2b23d0ba | 2267 | |
marcozecchini | 0:9fca2b23d0ba | 2268 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 2269 | } |
marcozecchini | 0:9fca2b23d0ba | 2270 | |
marcozecchini | 0:9fca2b23d0ba | 2271 | #endif /* MBEDTLS_GENPRIME */ |
marcozecchini | 0:9fca2b23d0ba | 2272 | |
marcozecchini | 0:9fca2b23d0ba | 2273 | #if defined(MBEDTLS_SELF_TEST) |
marcozecchini | 0:9fca2b23d0ba | 2274 | |
marcozecchini | 0:9fca2b23d0ba | 2275 | #define GCD_PAIR_COUNT 3 |
marcozecchini | 0:9fca2b23d0ba | 2276 | |
marcozecchini | 0:9fca2b23d0ba | 2277 | static const int gcd_pairs[GCD_PAIR_COUNT][3] = |
marcozecchini | 0:9fca2b23d0ba | 2278 | { |
marcozecchini | 0:9fca2b23d0ba | 2279 | { 693, 609, 21 }, |
marcozecchini | 0:9fca2b23d0ba | 2280 | { 1764, 868, 28 }, |
marcozecchini | 0:9fca2b23d0ba | 2281 | { 768454923, 542167814, 1 } |
marcozecchini | 0:9fca2b23d0ba | 2282 | }; |
marcozecchini | 0:9fca2b23d0ba | 2283 | |
marcozecchini | 0:9fca2b23d0ba | 2284 | /* |
marcozecchini | 0:9fca2b23d0ba | 2285 | * Checkup routine |
marcozecchini | 0:9fca2b23d0ba | 2286 | */ |
marcozecchini | 0:9fca2b23d0ba | 2287 | int mbedtls_mpi_self_test( int verbose ) |
marcozecchini | 0:9fca2b23d0ba | 2288 | { |
marcozecchini | 0:9fca2b23d0ba | 2289 | int ret, i; |
marcozecchini | 0:9fca2b23d0ba | 2290 | mbedtls_mpi A, E, N, X, Y, U, V; |
marcozecchini | 0:9fca2b23d0ba | 2291 | |
marcozecchini | 0:9fca2b23d0ba | 2292 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X ); |
marcozecchini | 0:9fca2b23d0ba | 2293 | mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V ); |
marcozecchini | 0:9fca2b23d0ba | 2294 | |
marcozecchini | 0:9fca2b23d0ba | 2295 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16, |
marcozecchini | 0:9fca2b23d0ba | 2296 | "EFE021C2645FD1DC586E69184AF4A31E" \ |
marcozecchini | 0:9fca2b23d0ba | 2297 | "D5F53E93B5F123FA41680867BA110131" \ |
marcozecchini | 0:9fca2b23d0ba | 2298 | "944FE7952E2517337780CB0DB80E61AA" \ |
marcozecchini | 0:9fca2b23d0ba | 2299 | "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2300 | |
marcozecchini | 0:9fca2b23d0ba | 2301 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16, |
marcozecchini | 0:9fca2b23d0ba | 2302 | "B2E7EFD37075B9F03FF989C7C5051C20" \ |
marcozecchini | 0:9fca2b23d0ba | 2303 | "34D2A323810251127E7BF8625A4F49A5" \ |
marcozecchini | 0:9fca2b23d0ba | 2304 | "F3E27F4DA8BD59C47D6DAABA4C8127BD" \ |
marcozecchini | 0:9fca2b23d0ba | 2305 | "5B5C25763222FEFCCFC38B832366C29E" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2306 | |
marcozecchini | 0:9fca2b23d0ba | 2307 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16, |
marcozecchini | 0:9fca2b23d0ba | 2308 | "0066A198186C18C10B2F5ED9B522752A" \ |
marcozecchini | 0:9fca2b23d0ba | 2309 | "9830B69916E535C8F047518A889A43A5" \ |
marcozecchini | 0:9fca2b23d0ba | 2310 | "94B6BED27A168D31D4A52F88925AA8F5" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2311 | |
marcozecchini | 0:9fca2b23d0ba | 2312 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) ); |
marcozecchini | 0:9fca2b23d0ba | 2313 | |
marcozecchini | 0:9fca2b23d0ba | 2314 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16, |
marcozecchini | 0:9fca2b23d0ba | 2315 | "602AB7ECA597A3D6B56FF9829A5E8B85" \ |
marcozecchini | 0:9fca2b23d0ba | 2316 | "9E857EA95A03512E2BAE7391688D264A" \ |
marcozecchini | 0:9fca2b23d0ba | 2317 | "A5663B0341DB9CCFD2C4C5F421FEC814" \ |
marcozecchini | 0:9fca2b23d0ba | 2318 | "8001B72E848A38CAE1C65F78E56ABDEF" \ |
marcozecchini | 0:9fca2b23d0ba | 2319 | "E12D3C039B8A02D6BE593F0BBBDA56F1" \ |
marcozecchini | 0:9fca2b23d0ba | 2320 | "ECF677152EF804370C1A305CAF3B5BF1" \ |
marcozecchini | 0:9fca2b23d0ba | 2321 | "30879B56C61DE584A0F53A2447A51E" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2322 | |
marcozecchini | 0:9fca2b23d0ba | 2323 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2324 | mbedtls_printf( " MPI test #1 (mul_mpi): " ); |
marcozecchini | 0:9fca2b23d0ba | 2325 | |
marcozecchini | 0:9fca2b23d0ba | 2326 | if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2327 | { |
marcozecchini | 0:9fca2b23d0ba | 2328 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2329 | mbedtls_printf( "failed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2330 | |
marcozecchini | 0:9fca2b23d0ba | 2331 | ret = 1; |
marcozecchini | 0:9fca2b23d0ba | 2332 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 2333 | } |
marcozecchini | 0:9fca2b23d0ba | 2334 | |
marcozecchini | 0:9fca2b23d0ba | 2335 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2336 | mbedtls_printf( "passed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2337 | |
marcozecchini | 0:9fca2b23d0ba | 2338 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) ); |
marcozecchini | 0:9fca2b23d0ba | 2339 | |
marcozecchini | 0:9fca2b23d0ba | 2340 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16, |
marcozecchini | 0:9fca2b23d0ba | 2341 | "256567336059E52CAE22925474705F39A94" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2342 | |
marcozecchini | 0:9fca2b23d0ba | 2343 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16, |
marcozecchini | 0:9fca2b23d0ba | 2344 | "6613F26162223DF488E9CD48CC132C7A" \ |
marcozecchini | 0:9fca2b23d0ba | 2345 | "0AC93C701B001B092E4E5B9F73BCD27B" \ |
marcozecchini | 0:9fca2b23d0ba | 2346 | "9EE50D0657C77F374E903CDFA4C642" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2347 | |
marcozecchini | 0:9fca2b23d0ba | 2348 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2349 | mbedtls_printf( " MPI test #2 (div_mpi): " ); |
marcozecchini | 0:9fca2b23d0ba | 2350 | |
marcozecchini | 0:9fca2b23d0ba | 2351 | if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 || |
marcozecchini | 0:9fca2b23d0ba | 2352 | mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2353 | { |
marcozecchini | 0:9fca2b23d0ba | 2354 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2355 | mbedtls_printf( "failed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2356 | |
marcozecchini | 0:9fca2b23d0ba | 2357 | ret = 1; |
marcozecchini | 0:9fca2b23d0ba | 2358 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 2359 | } |
marcozecchini | 0:9fca2b23d0ba | 2360 | |
marcozecchini | 0:9fca2b23d0ba | 2361 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2362 | mbedtls_printf( "passed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2363 | |
marcozecchini | 0:9fca2b23d0ba | 2364 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) ); |
marcozecchini | 0:9fca2b23d0ba | 2365 | |
marcozecchini | 0:9fca2b23d0ba | 2366 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16, |
marcozecchini | 0:9fca2b23d0ba | 2367 | "36E139AEA55215609D2816998ED020BB" \ |
marcozecchini | 0:9fca2b23d0ba | 2368 | "BD96C37890F65171D948E9BC7CBAA4D9" \ |
marcozecchini | 0:9fca2b23d0ba | 2369 | "325D24D6A3C12710F10A09FA08AB87" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2370 | |
marcozecchini | 0:9fca2b23d0ba | 2371 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2372 | mbedtls_printf( " MPI test #3 (exp_mod): " ); |
marcozecchini | 0:9fca2b23d0ba | 2373 | |
marcozecchini | 0:9fca2b23d0ba | 2374 | if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2375 | { |
marcozecchini | 0:9fca2b23d0ba | 2376 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2377 | mbedtls_printf( "failed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2378 | |
marcozecchini | 0:9fca2b23d0ba | 2379 | ret = 1; |
marcozecchini | 0:9fca2b23d0ba | 2380 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 2381 | } |
marcozecchini | 0:9fca2b23d0ba | 2382 | |
marcozecchini | 0:9fca2b23d0ba | 2383 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2384 | mbedtls_printf( "passed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2385 | |
marcozecchini | 0:9fca2b23d0ba | 2386 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) ); |
marcozecchini | 0:9fca2b23d0ba | 2387 | |
marcozecchini | 0:9fca2b23d0ba | 2388 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16, |
marcozecchini | 0:9fca2b23d0ba | 2389 | "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \ |
marcozecchini | 0:9fca2b23d0ba | 2390 | "C3DBA76456363A10869622EAC2DD84EC" \ |
marcozecchini | 0:9fca2b23d0ba | 2391 | "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) ); |
marcozecchini | 0:9fca2b23d0ba | 2392 | |
marcozecchini | 0:9fca2b23d0ba | 2393 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2394 | mbedtls_printf( " MPI test #4 (inv_mod): " ); |
marcozecchini | 0:9fca2b23d0ba | 2395 | |
marcozecchini | 0:9fca2b23d0ba | 2396 | if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2397 | { |
marcozecchini | 0:9fca2b23d0ba | 2398 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2399 | mbedtls_printf( "failed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2400 | |
marcozecchini | 0:9fca2b23d0ba | 2401 | ret = 1; |
marcozecchini | 0:9fca2b23d0ba | 2402 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 2403 | } |
marcozecchini | 0:9fca2b23d0ba | 2404 | |
marcozecchini | 0:9fca2b23d0ba | 2405 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2406 | mbedtls_printf( "passed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2407 | |
marcozecchini | 0:9fca2b23d0ba | 2408 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2409 | mbedtls_printf( " MPI test #5 (simple gcd): " ); |
marcozecchini | 0:9fca2b23d0ba | 2410 | |
marcozecchini | 0:9fca2b23d0ba | 2411 | for( i = 0; i < GCD_PAIR_COUNT; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 2412 | { |
marcozecchini | 0:9fca2b23d0ba | 2413 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) ); |
marcozecchini | 0:9fca2b23d0ba | 2414 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) ); |
marcozecchini | 0:9fca2b23d0ba | 2415 | |
marcozecchini | 0:9fca2b23d0ba | 2416 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) ); |
marcozecchini | 0:9fca2b23d0ba | 2417 | |
marcozecchini | 0:9fca2b23d0ba | 2418 | if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2419 | { |
marcozecchini | 0:9fca2b23d0ba | 2420 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2421 | mbedtls_printf( "failed at %d\n", i ); |
marcozecchini | 0:9fca2b23d0ba | 2422 | |
marcozecchini | 0:9fca2b23d0ba | 2423 | ret = 1; |
marcozecchini | 0:9fca2b23d0ba | 2424 | goto cleanup; |
marcozecchini | 0:9fca2b23d0ba | 2425 | } |
marcozecchini | 0:9fca2b23d0ba | 2426 | } |
marcozecchini | 0:9fca2b23d0ba | 2427 | |
marcozecchini | 0:9fca2b23d0ba | 2428 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2429 | mbedtls_printf( "passed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2430 | |
marcozecchini | 0:9fca2b23d0ba | 2431 | cleanup: |
marcozecchini | 0:9fca2b23d0ba | 2432 | |
marcozecchini | 0:9fca2b23d0ba | 2433 | if( ret != 0 && verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2434 | mbedtls_printf( "Unexpected error, return code = %08X\n", ret ); |
marcozecchini | 0:9fca2b23d0ba | 2435 | |
marcozecchini | 0:9fca2b23d0ba | 2436 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X ); |
marcozecchini | 0:9fca2b23d0ba | 2437 | mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V ); |
marcozecchini | 0:9fca2b23d0ba | 2438 | |
marcozecchini | 0:9fca2b23d0ba | 2439 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 2440 | mbedtls_printf( "\n" ); |
marcozecchini | 0:9fca2b23d0ba | 2441 | |
marcozecchini | 0:9fca2b23d0ba | 2442 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 2443 | } |
marcozecchini | 0:9fca2b23d0ba | 2444 | |
marcozecchini | 0:9fca2b23d0ba | 2445 | #endif /* MBEDTLS_SELF_TEST */ |
marcozecchini | 0:9fca2b23d0ba | 2446 | |
marcozecchini | 0:9fca2b23d0ba | 2447 | #endif /* MBEDTLS_BIGNUM_C */ |