Skip to content

Commit 06f88e9

Browse files
committed
Merge remote-tracking branch 'public/pr/2007' into development-proposed
2 parents 76646a4 + 65593d2 commit 06f88e9

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Features
1717
operations. On CPUs where the extensions are available, they can accelerate
1818
MPI multiplications used in ECC and RSA cryptography. Contributed by
1919
Aurelien Jarno.
20+
* Extend RSASSA-PSS signature to allow slightly a smaller salt size.
21+
Previously, PSS signature always used a salt with the same length as the
22+
hash, and returned an error if this was not possible. Now the salt size
23+
may be up to two bytes shorter. This allows the library to support all
24+
hash and signature sizes that comply with FIPS 186-4, including SHA-512
25+
with a 1024-bit key.
2026

2127
Bugfix
2228
* Fix wrong order of freeing in programs/ssl/ssl_server2 example

include/mbedtls/rsa.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,16 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
894894
* Specifications</em> it is advised to keep both hashes the
895895
* same.
896896
*
897+
* \note This function always uses the maximum possible salt size,
898+
* up to the length of the payload hash. This choice of salt
899+
* size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
900+
* v2.2) §9.1.1 step 3. Furthermore this function enforces a
901+
* minimum salt size which is the hash size minus 2 bytes. If
902+
* this minimum size is too large given the key size (the salt
903+
* size, plus the hash size, plus 2 bytes must be no more than
904+
* the key size in bytes), this function returns
905+
* #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
906+
*
897907
* \deprecated It is deprecated and discouraged to call this function
898908
* in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
899909
* are likely to remove the \p mode argument and have it

library/rsa.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
15211521
size_t olen;
15221522
unsigned char *p = sig;
15231523
unsigned char salt[MBEDTLS_MD_MAX_SIZE];
1524-
unsigned int slen, hlen, offset = 0;
1524+
size_t slen, min_slen, hlen, offset = 0;
15251525
int ret;
15261526
size_t msb;
15271527
const mbedtls_md_info_t *md_info;
@@ -1550,10 +1550,20 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
15501550
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
15511551

15521552
hlen = mbedtls_md_get_size( md_info );
1553-
slen = hlen;
15541553

1555-
if( olen < hlen + slen + 2 )
1554+
/* Calculate the largest possible salt length. Normally this is the hash
1555+
* length, which is the maximum length the salt can have. If there is not
1556+
* enough room, use the maximum salt length that fits. The constraint is
1557+
* that the hash length plus the salt length plus 2 bytes must be at most
1558+
* the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1559+
* (PKCS#1 v2.2) §9.1.1 step 3. */
1560+
min_slen = hlen - 2;
1561+
if( olen < hlen + min_slen + 2 )
15561562
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1563+
else if( olen >= hlen + hlen + 2 )
1564+
slen = hlen;
1565+
else
1566+
slen = olen - hlen - 2;
15571567

15581568
memset( sig, 0, olen );
15591569

@@ -1563,7 +1573,7 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
15631573

15641574
/* Note: EMSA-PSS encoding is over the length of N - 1 bits */
15651575
msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
1566-
p += olen - hlen * 2 - 2;
1576+
p += olen - hlen - slen - 2;
15671577
*p++ = 0x01;
15681578
memcpy( p, salt, slen );
15691579
p += slen;

0 commit comments

Comments
 (0)