1*e0c4386eSCy Schubert#! /usr/bin/env perl 2*e0c4386eSCy Schubert# Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. 3*e0c4386eSCy Schubert# 4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License"). You may not use 5*e0c4386eSCy Schubert# this file except in compliance with the License. You can obtain a copy 6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at 7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html 8*e0c4386eSCy Schubert 9*e0c4386eSCy Schubertuse strict; 10*e0c4386eSCy Schubertuse warnings; 11*e0c4386eSCy Schubert 12*e0c4386eSCy Schubertuse OpenSSL::Test qw(:DEFAULT data_file bldtop_dir srctop_file srctop_dir bldtop_file); 13*e0c4386eSCy Schubertuse OpenSSL::Test::Utils; 14*e0c4386eSCy Schubertuse File::Compare qw/compare_text/; 15*e0c4386eSCy Schubert 16*e0c4386eSCy SchubertBEGIN { 17*e0c4386eSCy Schubert setup("test_rsaoaep"); 18*e0c4386eSCy Schubert} 19*e0c4386eSCy Schubertuse lib srctop_dir('Configurations'); 20*e0c4386eSCy Schubertuse lib bldtop_dir('.'); 21*e0c4386eSCy Schubert 22*e0c4386eSCy Schubertmy $no_check = disabled('fips-securitychecks'); 23*e0c4386eSCy Schubert 24*e0c4386eSCy Schubertplan tests => 25*e0c4386eSCy Schubert ($no_check ? 0 : 1) # FIPS security check 26*e0c4386eSCy Schubert + 9; 27*e0c4386eSCy Schubert 28*e0c4386eSCy Schubertmy @prov = ( ); 29*e0c4386eSCy Schubertmy $provconf = srctop_file("test", "fips-and-base.cnf"); 30*e0c4386eSCy Schubertmy $provpath = bldtop_dir("providers"); 31*e0c4386eSCy Schubertmy $msg_file = data_file("plain_text"); 32*e0c4386eSCy Schubertmy $enc1_file = "enc1.bin"; 33*e0c4386eSCy Schubertmy $enc2_file = "enc2.bin"; 34*e0c4386eSCy Schubertmy $enc3_file = "enc3.bin"; 35*e0c4386eSCy Schubertmy $dec1_file = "dec1.txt"; 36*e0c4386eSCy Schubertmy $dec2_file = "dec2.txt"; 37*e0c4386eSCy Schubertmy $dec3_file = "dec3.txt"; 38*e0c4386eSCy Schubertmy $key_file = srctop_file("test", "testrsa2048.pem"); 39*e0c4386eSCy Schubertmy $small_key_file = srctop_file("test", "testrsa.pem"); 40*e0c4386eSCy Schubert 41*e0c4386eSCy Schubert$ENV{OPENSSL_TEST_LIBCTX} = "1"; 42*e0c4386eSCy Schubert 43*e0c4386eSCy Schubertunless ($no_check) { 44*e0c4386eSCy Schubert @prov = ( "-provider-path", $provpath, "-config", $provconf ); 45*e0c4386eSCy Schubert ok(!run(app(['openssl', 'pkeyutl', 46*e0c4386eSCy Schubert @prov, 47*e0c4386eSCy Schubert '-encrypt', 48*e0c4386eSCy Schubert '-in', $msg_file, 49*e0c4386eSCy Schubert '-inkey', $small_key_file, 50*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 51*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 52*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha1', 53*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha1', 54*e0c4386eSCy Schubert '-out', $enc1_file])), 55*e0c4386eSCy Schubert "RSA OAEP Encryption with a key smaller than 2048 in fips mode should fail"); 56*e0c4386eSCy Schubert} 57*e0c4386eSCy Schubert 58*e0c4386eSCy Schubertok(run(app(['openssl', 'pkeyutl', 59*e0c4386eSCy Schubert @prov, 60*e0c4386eSCy Schubert '-encrypt', 61*e0c4386eSCy Schubert '-in', $msg_file, 62*e0c4386eSCy Schubert '-inkey', $key_file, 63*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 64*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 65*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha1', 66*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha1', 67*e0c4386eSCy Schubert '-out', $enc1_file])), 68*e0c4386eSCy Schubert "RSA OAEP Encryption"); 69*e0c4386eSCy Schubert 70*e0c4386eSCy Schubertok(!run(app(['openssl', 'pkeyutl', 71*e0c4386eSCy Schubert @prov, 72*e0c4386eSCy Schubert '-encrypt', 73*e0c4386eSCy Schubert '-in', $key_file, 74*e0c4386eSCy Schubert '-inkey', $key_file, 75*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 76*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 77*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha256', 78*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha1'])), 79*e0c4386eSCy Schubert "RSA OAEP Encryption should fail if the message is larger than the rsa modulus"); 80*e0c4386eSCy Schubert 81*e0c4386eSCy Schubertok(run(app(['openssl', 'pkeyutl', 82*e0c4386eSCy Schubert @prov, 83*e0c4386eSCy Schubert '-decrypt', 84*e0c4386eSCy Schubert '-inkey', $key_file, 85*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 86*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 87*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha1', 88*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha1', 89*e0c4386eSCy Schubert '-in', $enc1_file, 90*e0c4386eSCy Schubert '-out', $dec1_file])) 91*e0c4386eSCy Schubert && compare_text($dec1_file, $msg_file) == 0, 92*e0c4386eSCy Schubert "RSA OAEP Decryption"); 93*e0c4386eSCy Schubert 94*e0c4386eSCy Schubertok(!run(app(['openssl', 'pkeyutl', 95*e0c4386eSCy Schubert @prov, 96*e0c4386eSCy Schubert '-decrypt', 97*e0c4386eSCy Schubert '-inkey', $key_file, 98*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 99*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 100*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha256', 101*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha224', 102*e0c4386eSCy Schubert '-in', $enc1_file])), 103*e0c4386eSCy Schubert "Incorrect digest for RSA OAEP Decryption"); 104*e0c4386eSCy Schubert 105*e0c4386eSCy Schubertok(!run(app(['openssl', 'pkeyutl', 106*e0c4386eSCy Schubert @prov, 107*e0c4386eSCy Schubert '-decrypt', 108*e0c4386eSCy Schubert '-inkey', $key_file, 109*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 110*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 111*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha1', 112*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha224', 113*e0c4386eSCy Schubert '-in', $enc1_file])), 114*e0c4386eSCy Schubert "Incorrect mgf1-digest for RSA OAEP Decryption"); 115*e0c4386eSCy Schubert 116*e0c4386eSCy Schubertok(run(app(['openssl', 'pkeyutl', 117*e0c4386eSCy Schubert @prov, 118*e0c4386eSCy Schubert '-encrypt', 119*e0c4386eSCy Schubert '-in', $msg_file, 120*e0c4386eSCy Schubert '-inkey', $key_file, 121*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 122*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 123*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha1', 124*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha1', 125*e0c4386eSCy Schubert '-out', $enc2_file])) 126*e0c4386eSCy Schubert && compare_text($enc2_file, $enc1_file) != 0, 127*e0c4386eSCy Schubert "RSA OAEP Encryption should generate different encrypted data"); 128*e0c4386eSCy Schubert 129*e0c4386eSCy Schubertok(run(app(['openssl', 'pkeyutl', 130*e0c4386eSCy Schubert @prov, 131*e0c4386eSCy Schubert '-decrypt', 132*e0c4386eSCy Schubert '-inkey', $key_file, 133*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 134*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 135*e0c4386eSCy Schubert '-in', $enc2_file, 136*e0c4386eSCy Schubert '-out', $dec2_file])) 137*e0c4386eSCy Schubert && compare_text($dec2_file, $msg_file) == 0, 138*e0c4386eSCy Schubert "RSA OAEP Decryption with default digests"); 139*e0c4386eSCy Schubert 140*e0c4386eSCy Schubertok(run(app(['openssl', 'pkeyutl', 141*e0c4386eSCy Schubert @prov, 142*e0c4386eSCy Schubert '-encrypt', 143*e0c4386eSCy Schubert '-in', $msg_file, 144*e0c4386eSCy Schubert '-inkey', $key_file, 145*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 146*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 147*e0c4386eSCy Schubert '-out', $enc3_file])), 148*e0c4386eSCy Schubert "RSA OAEP Encryption with default digests"); 149*e0c4386eSCy Schubert 150*e0c4386eSCy Schubertok(run(app(['openssl', 'pkeyutl', 151*e0c4386eSCy Schubert @prov, 152*e0c4386eSCy Schubert '-decrypt', 153*e0c4386eSCy Schubert '-inkey', $key_file, 154*e0c4386eSCy Schubert '-pkeyopt', 'pad-mode:oaep', 155*e0c4386eSCy Schubert '-pkeyopt', 'oaep-label:123', 156*e0c4386eSCy Schubert '-pkeyopt', 'digest:sha1', 157*e0c4386eSCy Schubert '-pkeyopt', 'mgf1-digest:sha1', 158*e0c4386eSCy Schubert '-in', $enc3_file, 159*e0c4386eSCy Schubert '-out', $dec3_file])) 160*e0c4386eSCy Schubert && compare_text($dec3_file, $msg_file) == 0, 161*e0c4386eSCy Schubert "RSA OAEP Decryption with explicit default digests"); 162