Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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