Skip to content

Commit 3f9dfd8

Browse files
committed
Fix issues noted in review
Also fix the overhead calculaation for OAEP
1 parent f5b6ed9 commit 3f9dfd8

5 files changed

Lines changed: 30 additions & 8 deletions

File tree

RSA.xs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,19 @@ EVP_PKEY* _load_rsa_key(SV* p_keyStringSv,
392392

393393
void check_max_message_length(rsaData* p_rsa, int from_length) {
394394
int size;
395+
int max_len = -1;
396+
const char *pad_name = NULL;
397+
EVP_MD *md_obj;
398+
int digest_length = 0;
395399
/* Pre-validate plaintext length before calling OpenSSL.
396400
Only applies to encryption direction (encrypt, private_encrypt),
397401
not to decryption (decrypt, public_decrypt) where input is ciphertext. */
398402
size = EVP_PKEY_get_size(p_rsa->rsa);
399-
int max_len = -1;
400-
const char *pad_name = NULL;
403+
401404
if (p_rsa->padding == RSA_PKCS1_OAEP_PADDING) {
402-
max_len = size - 42; /* 2 * SHA1_DIGEST_LENGTH + 2 */
405+
md_obj = get_md_bynid(p_rsa->hashMode); /* croak()s on unknown NID */
406+
digest_length = EVP_MD_size(md_obj);
407+
max_len = size - (2 * digest_length) - 2; /* 2 * SHA1_DIGEST_LENGTH + 2 */
403408
pad_name = "OAEP";
404409
} else if (p_rsa->padding == RSA_PKCS1_PADDING) {
405410
max_len = size - 11; /* PKCS#1 v1.5 overhead */

t/crypto.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Crypt::OpenSSL::RSA->import_random_seed();
1515
my $rsa = Crypt::OpenSSL::RSA->generate_key(2048);
1616
my $rsa2 = Crypt::OpenSSL::RSA->generate_key(2048);
1717
my $key_size = $rsa->size(); # 256 bytes for 2048-bit key
18-
18+
my $hash_size = 32; # SHA256 (default is 32 bytes)
1919
# --- OAEP boundary tests ---
2020

2121
$rsa->use_pkcs1_oaep_padding();
22-
my $oaep_max = $key_size - 42; # SHA-1 OAEP overhead
22+
my $oaep_max = $key_size - (2 * $hash_size) - 2; # SHA256 OAEP overhead
2323

2424
# Max-length plaintext that fits OAEP
2525
{

t/error.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ ok($@, "decrypt croaks on empty ciphertext");
8686
# --- Plaintext too large for padding mode ---
8787

8888
$rsa->use_pkcs1_oaep_padding();
89-
my $max_oaep = $rsa->size() - 42;
89+
my $hash_size = 32; # SHA256 (default is 32 bytes)
90+
my $max_oaep = $rsa->size() - (2 * $hash_size) - 2; # SHA-1 OAEP overhead
9091
my $too_large = "x" x ($max_oaep + 1);
9192
eval { $rsa->encrypt($too_large) };
9293
ok($@, "encrypt croaks when plaintext exceeds OAEP max size");

t/padding.t

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ my %padding_methods = (
117117
#'sslv23' => {'sign' => 0, 'encrypt' => 0, 'pad' => 11},
118118
);
119119

120+
my %digest_lengths = (
121+
'md5' => 16,
122+
'sha1' => 20,
123+
'sha224' => 28,
124+
'sha256' => 32,
125+
'sha384' => 48,
126+
'sha512' => 64,
127+
'ripemd160' => 20,
128+
);
120129

121130
foreach my $padding (keys %padding_methods) {
122131
diag $padding;
@@ -157,7 +166,12 @@ foreach my $padding (keys %padding_methods) {
157166

158167
# Valid encryption methods with padding
159168
if ($encrypt) {
160-
_Test_Encrypt_And_Decrypt( $rsa->size() - $pad, $rsa, 0, $padding, $hash );
169+
my $size = $rsa->size();
170+
my $max_len = $size - $pad;
171+
if ( $padding eq 'pkcs1_oaep' && $hash =~ /sha/ ) {
172+
$max_len = $size - ($digest_lengths{$hash} * 2) - 2;
173+
}
174+
_Test_Encrypt_And_Decrypt( $max_len, $rsa, 0, $padding, $hash );
161175
}
162176

163177
}

t/rsa.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ _Test_Encrypt_And_Decrypt( $rsa->size(), $rsa, 1 );
8080
$rsa->use_pkcs1_oaep_padding();
8181

8282
# private_encrypt does not work with pkcs1_oaep_padding
83-
_Test_Encrypt_And_Decrypt( $rsa->size() - 42, $rsa, 0 );
83+
my $hash_size = 32; # SHA256 (default is 32 bytes)
84+
my $oaep_max = $rsa->size - (2 * $hash_size) - 2;
85+
_Test_Encrypt_And_Decrypt( $oaep_max, $rsa, 0 );
8486

8587
#FIXME - use_sslv23_padding seems to fail on decryption. openssl bug?
8688

0 commit comments

Comments
 (0)