| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "common.h" |
| 26 |
#include "constant_time_internal.h" |
| 27 |
#include "mbedtls/constant_time.h" |
| 28 |
#include "mbedtls/error.h" |
| 29 |
#include "mbedtls/platform_util.h" |
| 30 |
|
| 31 |
#if defined(MBEDTLS_BIGNUM_C) |
| 32 |
#include "mbedtls/bignum.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
#if defined(MBEDTLS_SSL_TLS_C) |
| 36 |
#include "mbedtls/ssl_internal.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#if defined(MBEDTLS_RSA_C) |
| 40 |
#include "mbedtls/rsa.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(MBEDTLS_BASE64_C) |
| 44 |
#include "constant_time_invasive.h" |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <string.h> |
| 48 |
|
| 49 |
int mbedtls_ct_memcmp( const void *a, |
| 50 |
const void *b, |
| 51 |
size_t n ) |
| 52 |
{ |
| 53 |
size_t i; |
| 54 |
volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 55 |
volatile const unsigned char *B = (volatile const unsigned char *) b; |
| 56 |
volatile unsigned char diff = 0; |
| 57 |
|
| 58 |
for( i = 0; i < n; i++ ) |
| 59 |
{ |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
unsigned char x = A[i], y = B[i]; |
| 64 |
diff |= x ^ y; |
| 65 |
} |
| 66 |
|
| 67 |
return( (int)diff ); |
| 68 |
} |
| 69 |
|
| 70 |
unsigned mbedtls_ct_uint_mask( unsigned value ) |
| 71 |
{ |
| 72 |
|
| 73 |
|
| 74 |
#if defined(_MSC_VER) |
| 75 |
#pragma warning( push ) |
| 76 |
#pragma warning( disable : 4146 ) |
| 77 |
#endif |
| 78 |
return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) ); |
| 79 |
#if defined(_MSC_VER) |
| 80 |
#pragma warning( pop ) |
| 81 |
#endif |
| 82 |
} |
| 83 |
|
| 84 |
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
| 85 |
|
| 86 |
size_t mbedtls_ct_size_mask( size_t value ) |
| 87 |
{ |
| 88 |
|
| 89 |
|
| 90 |
#if defined(_MSC_VER) |
| 91 |
#pragma warning( push ) |
| 92 |
#pragma warning( disable : 4146 ) |
| 93 |
#endif |
| 94 |
return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) ); |
| 95 |
#if defined(_MSC_VER) |
| 96 |
#pragma warning( pop ) |
| 97 |
#endif |
| 98 |
} |
| 99 |
|
| 100 |
#endif |
| 101 |
|
| 102 |
#if defined(MBEDTLS_BIGNUM_C) |
| 103 |
|
| 104 |
mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask( mbedtls_mpi_uint value ) |
| 105 |
{ |
| 106 |
|
| 107 |
|
| 108 |
#if defined(_MSC_VER) |
| 109 |
#pragma warning( push ) |
| 110 |
#pragma warning( disable : 4146 ) |
| 111 |
#endif |
| 112 |
return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) ); |
| 113 |
#if defined(_MSC_VER) |
| 114 |
#pragma warning( pop ) |
| 115 |
#endif |
| 116 |
} |
| 117 |
|
| 118 |
#endif |
| 119 |
|
| 120 |
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static size_t mbedtls_ct_size_mask_lt( size_t x, |
| 135 |
size_t y ) |
| 136 |
{ |
| 137 |
|
| 138 |
const size_t sub = x - y; |
| 139 |
|
| 140 |
|
| 141 |
const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 ); |
| 142 |
|
| 143 |
|
| 144 |
const size_t mask = mbedtls_ct_size_mask( sub1 ); |
| 145 |
|
| 146 |
return( mask ); |
| 147 |
} |
| 148 |
|
| 149 |
size_t mbedtls_ct_size_mask_ge( size_t x, |
| 150 |
size_t y ) |
| 151 |
{ |
| 152 |
return( ~mbedtls_ct_size_mask_lt( x, y ) ); |
| 153 |
} |
| 154 |
|
| 155 |
#endif |
| 156 |
|
| 157 |
#if defined(MBEDTLS_BASE64_C) |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
MBEDTLS_STATIC_TESTABLE |
| 164 |
unsigned char mbedtls_ct_uchar_mask_of_range( unsigned char low, |
| 165 |
unsigned char high, |
| 166 |
unsigned char c ) |
| 167 |
{ |
| 168 |
|
| 169 |
unsigned low_mask = ( (unsigned) c - low ) >> 8; |
| 170 |
|
| 171 |
unsigned high_mask = ( (unsigned) high - c ) >> 8; |
| 172 |
return( ~( low_mask | high_mask ) & 0xff ); |
| 173 |
} |
| 174 |
|
| 175 |
#endif |
| 176 |
|
| 177 |
unsigned mbedtls_ct_size_bool_eq( size_t x, |
| 178 |
size_t y ) |
| 179 |
{ |
| 180 |
|
| 181 |
const size_t diff = x ^ y; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#if defined(_MSC_VER) |
| 186 |
#pragma warning( push ) |
| 187 |
#pragma warning( disable : 4146 ) |
| 188 |
#endif |
| 189 |
|
| 190 |
|
| 191 |
const size_t diff_msb = ( diff | (size_t) -diff ); |
| 192 |
|
| 193 |
#if defined(_MSC_VER) |
| 194 |
#pragma warning( pop ) |
| 195 |
#endif |
| 196 |
|
| 197 |
|
| 198 |
const unsigned diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 ); |
| 199 |
|
| 200 |
return( 1 ^ diff1 ); |
| 201 |
} |
| 202 |
|
| 203 |
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
static unsigned mbedtls_ct_size_gt( size_t x, |
| 217 |
size_t y ) |
| 218 |
{ |
| 219 |
|
| 220 |
return( ( y - x ) >> ( sizeof( size_t ) * 8 - 1 ) ); |
| 221 |
} |
| 222 |
|
| 223 |
#endif |
| 224 |
|
| 225 |
#if defined(MBEDTLS_BIGNUM_C) |
| 226 |
|
| 227 |
unsigned mbedtls_ct_mpi_uint_lt( const mbedtls_mpi_uint x, |
| 228 |
const mbedtls_mpi_uint y ) |
| 229 |
{ |
| 230 |
mbedtls_mpi_uint ret; |
| 231 |
mbedtls_mpi_uint cond; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
cond = ( x ^ y ); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
ret = ( x - y ) & ~cond; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
ret |= y & cond; |
| 248 |
|
| 249 |
|
| 250 |
ret = ret >> ( sizeof( mbedtls_mpi_uint ) * 8 - 1 ); |
| 251 |
|
| 252 |
return (unsigned) ret; |
| 253 |
} |
| 254 |
|
| 255 |
#endif |
| 256 |
|
| 257 |
unsigned mbedtls_ct_uint_if( unsigned condition, |
| 258 |
unsigned if1, |
| 259 |
unsigned if0 ) |
| 260 |
{ |
| 261 |
unsigned mask = mbedtls_ct_uint_mask( condition ); |
| 262 |
return( ( mask & if1 ) | (~mask & if0 ) ); |
| 263 |
} |
| 264 |
|
| 265 |
#if defined(MBEDTLS_BIGNUM_C) |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
static int mbedtls_ct_cond_select_sign( unsigned char condition, |
| 282 |
int if1, |
| 283 |
int if0 ) |
| 284 |
{ |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
unsigned uif1 = if1 + 1; |
| 289 |
unsigned uif0 = if0 + 1; |
| 290 |
|
| 291 |
|
| 292 |
const unsigned mask = condition << 1; |
| 293 |
|
| 294 |
|
| 295 |
unsigned ur = ( uif0 & ~mask ) | ( uif1 & mask ); |
| 296 |
|
| 297 |
|
| 298 |
return( (int) ur - 1 ); |
| 299 |
} |
| 300 |
|
| 301 |
void mbedtls_ct_mpi_uint_cond_assign( size_t n, |
| 302 |
mbedtls_mpi_uint *dest, |
| 303 |
const mbedtls_mpi_uint *src, |
| 304 |
unsigned char condition ) |
| 305 |
{ |
| 306 |
size_t i; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
#if defined(_MSC_VER) |
| 311 |
#pragma warning( push ) |
| 312 |
#pragma warning( disable : 4146 ) |
| 313 |
#endif |
| 314 |
|
| 315 |
|
| 316 |
const mbedtls_mpi_uint mask = -condition; |
| 317 |
|
| 318 |
#if defined(_MSC_VER) |
| 319 |
#pragma warning( pop ) |
| 320 |
#endif |
| 321 |
|
| 322 |
for( i = 0; i < n; i++ ) |
| 323 |
dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask ); |
| 324 |
} |
| 325 |
|
| 326 |
#endif |
| 327 |
|
| 328 |
#if defined(MBEDTLS_BASE64_C) |
| 329 |
|
| 330 |
unsigned char mbedtls_ct_base64_enc_char( unsigned char value ) |
| 331 |
{ |
| 332 |
unsigned char digit = 0; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
digit |= mbedtls_ct_uchar_mask_of_range( 0, 25, value ) & ( 'A' + value ); |
| 337 |
digit |= mbedtls_ct_uchar_mask_of_range( 26, 51, value ) & ( 'a' + value - 26 ); |
| 338 |
digit |= mbedtls_ct_uchar_mask_of_range( 52, 61, value ) & ( '0' + value - 52 ); |
| 339 |
digit |= mbedtls_ct_uchar_mask_of_range( 62, 62, value ) & '+'; |
| 340 |
digit |= mbedtls_ct_uchar_mask_of_range( 63, 63, value ) & '/'; |
| 341 |
return( digit ); |
| 342 |
} |
| 343 |
|
| 344 |
signed char mbedtls_ct_base64_dec_value( unsigned char c ) |
| 345 |
{ |
| 346 |
unsigned char val = 0; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
val |= mbedtls_ct_uchar_mask_of_range( 'A', 'Z', c ) & ( c - 'A' + 0 + 1 ); |
| 352 |
val |= mbedtls_ct_uchar_mask_of_range( 'a', 'z', c ) & ( c - 'a' + 26 + 1 ); |
| 353 |
val |= mbedtls_ct_uchar_mask_of_range( '0', '9', c ) & ( c - '0' + 52 + 1 ); |
| 354 |
val |= mbedtls_ct_uchar_mask_of_range( '+', '+', c ) & ( c - '+' + 62 + 1 ); |
| 355 |
val |= mbedtls_ct_uchar_mask_of_range( '/', '/', c ) & ( c - '/' + 63 + 1 ); |
| 356 |
|
| 357 |
|
| 358 |
return( val - 1 ); |
| 359 |
} |
| 360 |
|
| 361 |
#endif |
| 362 |
|
| 363 |
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
static void mbedtls_ct_mem_move_to_left( void *start, |
| 382 |
size_t total, |
| 383 |
size_t offset ) |
| 384 |
{ |
| 385 |
volatile unsigned char *buf = start; |
| 386 |
size_t i, n; |
| 387 |
if( total == 0 ) |
| 388 |
return; |
| 389 |
for( i = 0; i < total; i++ ) |
| 390 |
{ |
| 391 |
unsigned no_op = mbedtls_ct_size_gt( total - offset, i ); |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
for( n = 0; n < total - 1; n++ ) |
| 396 |
{ |
| 397 |
unsigned char current = buf[n]; |
| 398 |
unsigned char next = buf[n+1]; |
| 399 |
buf[n] = mbedtls_ct_uint_if( no_op, current, next ); |
| 400 |
} |
| 401 |
buf[total-1] = mbedtls_ct_uint_if( no_op, buf[total-1], 0 ); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
#endif |
| 406 |
|
| 407 |
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
| 408 |
|
| 409 |
void mbedtls_ct_memcpy_if_eq( unsigned char *dest, |
| 410 |
const unsigned char *src, |
| 411 |
size_t len, |
| 412 |
size_t c1, |
| 413 |
size_t c2 ) |
| 414 |
{ |
| 415 |
|
| 416 |
const size_t equal = mbedtls_ct_size_bool_eq( c1, c2 ); |
| 417 |
const unsigned char mask = (unsigned char) mbedtls_ct_size_mask( equal ); |
| 418 |
|
| 419 |
|
| 420 |
for( size_t i = 0; i < len; i++ ) |
| 421 |
dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask ); |
| 422 |
} |
| 423 |
|
| 424 |
void mbedtls_ct_memcpy_offset( unsigned char *dest, |
| 425 |
const unsigned char *src, |
| 426 |
size_t offset, |
| 427 |
size_t offset_min, |
| 428 |
size_t offset_max, |
| 429 |
size_t len ) |
| 430 |
{ |
| 431 |
size_t offsetval; |
| 432 |
|
| 433 |
for( offsetval = offset_min; offsetval <= offset_max; offsetval++ ) |
| 434 |
{ |
| 435 |
mbedtls_ct_memcpy_if_eq( dest, src + offsetval, len, |
| 436 |
offsetval, offset ); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
int mbedtls_ct_hmac( mbedtls_md_context_t *ctx, |
| 441 |
const unsigned char *add_data, |
| 442 |
size_t add_data_len, |
| 443 |
const unsigned char *data, |
| 444 |
size_t data_len_secret, |
| 445 |
size_t min_data_len, |
| 446 |
size_t max_data_len, |
| 447 |
unsigned char *output ) |
| 448 |
{ |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info ); |
| 464 |
|
| 465 |
|
| 466 |
const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64; |
| 467 |
const unsigned char * const ikey = ctx->hmac_ctx; |
| 468 |
const unsigned char * const okey = ikey + block_size; |
| 469 |
const size_t hash_size = mbedtls_md_get_size( ctx->md_info ); |
| 470 |
|
| 471 |
unsigned char aux_out[MBEDTLS_MD_MAX_SIZE]; |
| 472 |
mbedtls_md_context_t aux; |
| 473 |
size_t offset; |
| 474 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 475 |
|
| 476 |
mbedtls_md_init( &aux ); |
| 477 |
|
| 478 |
#define MD_CHK( func_call ) \ |
| 479 |
do { \ |
| 480 |
ret = (func_call); \ |
| 481 |
if( ret != 0 ) \ |
| 482 |
goto cleanup; \ |
| 483 |
} while( 0 ) |
| 484 |
|
| 485 |
MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) ); |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) ); |
| 490 |
MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) ); |
| 491 |
|
| 492 |
|
| 493 |
for( offset = min_data_len; offset <= max_data_len; offset++ ) |
| 494 |
{ |
| 495 |
MD_CHK( mbedtls_md_clone( &aux, ctx ) ); |
| 496 |
MD_CHK( mbedtls_md_finish( &aux, aux_out ) ); |
| 497 |
|
| 498 |
mbedtls_ct_memcpy_if_eq( output, aux_out, hash_size, |
| 499 |
offset, data_len_secret ); |
| 500 |
|
| 501 |
if( offset < max_data_len ) |
| 502 |
MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) ); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
MD_CHK( mbedtls_md_finish( ctx, aux_out ) ); |
| 507 |
|
| 508 |
|
| 509 |
MD_CHK( mbedtls_md_starts( ctx ) ); |
| 510 |
MD_CHK( mbedtls_md_update( ctx, okey, block_size ) ); |
| 511 |
MD_CHK( mbedtls_md_update( ctx, output, hash_size ) ); |
| 512 |
MD_CHK( mbedtls_md_finish( ctx, output ) ); |
| 513 |
|
| 514 |
|
| 515 |
MD_CHK( mbedtls_md_hmac_reset( ctx ) ); |
| 516 |
|
| 517 |
#undef MD_CHK |
| 518 |
|
| 519 |
cleanup: |
| 520 |
mbedtls_md_free( &aux ); |
| 521 |
return( ret ); |
| 522 |
} |
| 523 |
|
| 524 |
#endif |
| 525 |
|
| 526 |
#if defined(MBEDTLS_BIGNUM_C) |
| 527 |
|
| 528 |
#define MPI_VALIDATE_RET( cond ) \ |
| 529 |
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA ) |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, |
| 537 |
const mbedtls_mpi *Y, |
| 538 |
unsigned char assign ) |
| 539 |
{ |
| 540 |
int ret = 0; |
| 541 |
size_t i; |
| 542 |
mbedtls_mpi_uint limb_mask; |
| 543 |
MPI_VALIDATE_RET( X != NULL ); |
| 544 |
MPI_VALIDATE_RET( Y != NULL ); |
| 545 |
|
| 546 |
|
| 547 |
limb_mask = mbedtls_ct_mpi_uint_mask( assign );; |
| 548 |
|
| 549 |
MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); |
| 550 |
|
| 551 |
X->s = mbedtls_ct_cond_select_sign( assign, Y->s, X->s ); |
| 552 |
|
| 553 |
mbedtls_ct_mpi_uint_cond_assign( Y->n, X->p, Y->p, assign ); |
| 554 |
|
| 555 |
for( i = Y->n; i < X->n; i++ ) |
| 556 |
X->p[i] &= ~limb_mask; |
| 557 |
|
| 558 |
cleanup: |
| 559 |
return( ret ); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, |
| 569 |
mbedtls_mpi *Y, |
| 570 |
unsigned char swap ) |
| 571 |
{ |
| 572 |
int ret, s; |
| 573 |
size_t i; |
| 574 |
mbedtls_mpi_uint limb_mask; |
| 575 |
mbedtls_mpi_uint tmp; |
| 576 |
MPI_VALIDATE_RET( X != NULL ); |
| 577 |
MPI_VALIDATE_RET( Y != NULL ); |
| 578 |
|
| 579 |
if( X == Y ) |
| 580 |
return( 0 ); |
| 581 |
|
| 582 |
|
| 583 |
limb_mask = mbedtls_ct_mpi_uint_mask( swap ); |
| 584 |
|
| 585 |
MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); |
| 586 |
MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) ); |
| 587 |
|
| 588 |
s = X->s; |
| 589 |
X->s = mbedtls_ct_cond_select_sign( swap, Y->s, X->s ); |
| 590 |
Y->s = mbedtls_ct_cond_select_sign( swap, s, Y->s ); |
| 591 |
|
| 592 |
|
| 593 |
for( i = 0; i < X->n; i++ ) |
| 594 |
{ |
| 595 |
tmp = X->p[i]; |
| 596 |
X->p[i] = ( X->p[i] & ~limb_mask ) | ( Y->p[i] & limb_mask ); |
| 597 |
Y->p[i] = ( Y->p[i] & ~limb_mask ) | ( tmp & limb_mask ); |
| 598 |
} |
| 599 |
|
| 600 |
cleanup: |
| 601 |
return( ret ); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, |
| 608 |
const mbedtls_mpi *Y, |
| 609 |
unsigned *ret ) |
| 610 |
{ |
| 611 |
size_t i; |
| 612 |
|
| 613 |
unsigned cond, done, X_is_negative, Y_is_negative; |
| 614 |
|
| 615 |
MPI_VALIDATE_RET( X != NULL ); |
| 616 |
MPI_VALIDATE_RET( Y != NULL ); |
| 617 |
MPI_VALIDATE_RET( ret != NULL ); |
| 618 |
|
| 619 |
if( X->n != Y->n ) |
| 620 |
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
X_is_negative = ( X->s & 2 ) >> 1; |
| 627 |
Y_is_negative = ( Y->s & 2 ) >> 1; |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
cond = ( X_is_negative ^ Y_is_negative ); |
| 635 |
*ret = cond & X_is_negative; |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
done = cond; |
| 642 |
|
| 643 |
for( i = X->n; i > 0; i-- ) |
| 644 |
{ |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
cond = mbedtls_ct_mpi_uint_lt( Y->p[i - 1], X->p[i - 1] ); |
| 653 |
*ret |= cond & ( 1 - done ) & X_is_negative; |
| 654 |
done |= cond; |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
cond = mbedtls_ct_mpi_uint_lt( X->p[i - 1], Y->p[i - 1] ); |
| 664 |
*ret |= cond & ( 1 - done ) & ( 1 - X_is_negative ); |
| 665 |
done |= cond; |
| 666 |
} |
| 667 |
|
| 668 |
return( 0 ); |
| 669 |
} |
| 670 |
|
| 671 |
#endif |
| 672 |
|
| 673 |
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 674 |
|
| 675 |
int mbedtls_ct_rsaes_pkcs1_v15_unpadding( int mode, |
| 676 |
unsigned char *input, |
| 677 |
size_t ilen, |
| 678 |
unsigned char *output, |
| 679 |
size_t output_max_len, |
| 680 |
size_t *olen ) |
| 681 |
{ |
| 682 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 683 |
size_t i, plaintext_max_size; |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
size_t pad_count = 0; |
| 696 |
unsigned bad = 0; |
| 697 |
unsigned char pad_done = 0; |
| 698 |
size_t plaintext_size = 0; |
| 699 |
unsigned output_too_large; |
| 700 |
|
| 701 |
plaintext_max_size = ( output_max_len > ilen - 11 ) ? ilen - 11 |
| 702 |
: output_max_len; |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
bad |= input[0]; |
| 707 |
|
| 708 |
if( mode == MBEDTLS_RSA_PRIVATE ) |
| 709 |
{ |
| 710 |
|
| 711 |
|
| 712 |
bad |= input[1] ^ MBEDTLS_RSA_CRYPT; |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
for( i = 2; i < ilen; i++ ) |
| 717 |
{ |
| 718 |
pad_done |= ((input[i] | (unsigned char)-input[i]) >> 7) ^ 1; |
| 719 |
pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
| 720 |
} |
| 721 |
} |
| 722 |
else |
| 723 |
{ |
| 724 |
|
| 725 |
|
| 726 |
bad |= input[1] ^ MBEDTLS_RSA_SIGN; |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
for( i = 2; i < ilen; i++ ) |
| 732 |
{ |
| 733 |
pad_done |= mbedtls_ct_uint_if( input[i], 0, 1 ); |
| 734 |
pad_count += mbedtls_ct_uint_if( pad_done, 0, 1 ); |
| 735 |
bad |= mbedtls_ct_uint_if( pad_done, 0, input[i] ^ 0xFF ); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
bad |= mbedtls_ct_uint_if( pad_done, 0, 1 ); |
| 741 |
|
| 742 |
|
| 743 |
bad |= mbedtls_ct_size_gt( 8, pad_count ); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
plaintext_size = mbedtls_ct_uint_if( |
| 753 |
bad, (unsigned) plaintext_max_size, |
| 754 |
(unsigned) ( ilen - pad_count - 3 ) ); |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
output_too_large = mbedtls_ct_size_gt( plaintext_size, |
| 759 |
plaintext_max_size ); |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
ret = - (int) mbedtls_ct_uint_if( |
| 767 |
bad, - MBEDTLS_ERR_RSA_INVALID_PADDING, |
| 768 |
mbedtls_ct_uint_if( output_too_large, |
| 769 |
- MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE, |
| 770 |
0 ) ); |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
bad = mbedtls_ct_uint_mask( bad | output_too_large ); |
| 779 |
for( i = 11; i < ilen; i++ ) |
| 780 |
input[i] &= ~bad; |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
plaintext_size = mbedtls_ct_uint_if( output_too_large, |
| 787 |
(unsigned) plaintext_max_size, |
| 788 |
(unsigned) plaintext_size ); |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
mbedtls_ct_mem_move_to_left( input + ilen - plaintext_max_size, |
| 797 |
plaintext_max_size, |
| 798 |
plaintext_max_size - plaintext_size ); |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
if( output_max_len != 0 ) |
| 808 |
memcpy( output, input + ilen - plaintext_max_size, plaintext_max_size ); |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
*olen = plaintext_size; |
| 815 |
|
| 816 |
return( ret ); |
| 817 |
} |
| 818 |
|
| 819 |
#endif |