Skip to content

Commit 4eec033

Browse files
Merged PR 11885097: Fix RSA encryption corner case test
## Description: Fix RSA encryption corner case test + Previously was making assumptions about the form of the KATs it was acting on which meant that it could spuriously fail 1 in every 2000 runs with the latest KATs Related work items: #55264809
1 parent 1d7e34b commit 4eec033

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

unittest/lib/testRsaEnc.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,23 @@ testRsaEncSingle(
559559

560560
if( cbMsg == cbKey || pcstrHashAlgName != NULL ) // Only for RsaRaw and OAEP
561561
{
562-
// Modify the ciphertext, not in the first byte to avoid values > modulus
562+
// Modify the ciphertext, avoiding values >= modulus
563+
// Count number of most significant bytes that modulus and ciphertext have in common;
564+
// Given ciphertext is valid, there is some i<cbKey, pbCiphertext[i] < abModulus[i]
565+
UINT32 cbToPreserve = 0;
566+
while( pcRsaKeyBlob->abModulus[cbToPreserve] == pbCiphertext[cbToPreserve] )
567+
{
568+
cbToPreserve++;
569+
}
570+
// Only modify the ciphertext after the most significant byte where the original
571+
// ciphertext < modulus
572+
// This ensures that the value of the modified ciphertext < modulus
573+
cbToPreserve++;
574+
CHECK( cbToPreserve < cbKey, "Unexpected KAT with ciphertext and modulus differing only in the least significant byte" );
575+
563576
memcpy( buf, pbCiphertext, cbKey );
564577
UINT32 t = g_rng.uint32();
565-
buf[ 1 + ((t/8) % (cbKey - 1)) ] ^= 1 << (t%8);
578+
buf[ cbToPreserve + ((t/8) % (cbKey - cbToPreserve)) ] ^= 1 << (t%8);
566579
ntStatus = pRsaEnc->decrypt( buf, cbKey, pcstrHashAlgName, pbLabel, cbLabel, buf, cbKey, &cbRes );
567580
if( cbMsg == cbKey )
568581
{

0 commit comments

Comments
 (0)