mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 18:41:59 2017 +0100
Revision:
1:9ebc941037d5
Parent:
0:cdf462088d13
Child:
2:bbdeda018a3c
Update to mbedtls 2.4.2, security fixes

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

Who changed what in which revision?

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