From 136bc45afd59256e29f593576a2bc8cd8426f180 Mon Sep 17 00:00:00 2001 From: Christian Forler Date: Mon, 2 Jun 2014 18:02:47 +0200 Subject: [PATCH] Replaced RSA Tests --- test/test-rsa.adb | 487 ++++++++++--------------- test/test-rsa.ads | 29 +- test/test-suite_asymmetric_ciphers.adb | 32 +- 3 files changed, 210 insertions(+), 338 deletions(-) diff --git a/test/test-rsa.adb b/test/test-rsa.adb index b3689de..415efa3 100644 --- a/test/test-rsa.adb +++ b/test/test-rsa.adb @@ -1,67 +1,90 @@ with AUnit.Assertions; with Crypto.Asymmetric.RSA; with Crypto.Types; +with Crypto.Types.Big_Numbers; + pragma Elaborate_All (Crypto.Asymmetric.RSA); package body Test.RSA is ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- --------------------------------- Type - Declaration -------------------------------- ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- - - package RSA is new Crypto.Asymmetric.RSA(512); + ---------------------------------------------------------------------------- + --------------------------- Type - Declaration ----------------------------- + ---------------------------------------------------------------------------- + + package RSA is new Crypto.Asymmetric.RSA(1024); use RSA; + use RSA.Big; + use RSA.Big.Utils; use Crypto.Types; + use AUnit.Assertions; + + N_RSA: constant RSA_Number := + (16#a8#, 16#b3#, 16#b2#, 16#84#, 16#af#, 16#8e#, 16#b5#, 16#0b#, 16#38#, + 16#70#, 16#34#, 16#a8#, 16#60#, 16#f1#, 16#46#, 16#c4#, 16#91#, 16#9f#, + 16#31#, 16#87#, 16#63#, 16#cd#, 16#6c#, 16#55#, 16#98#, 16#c8#, 16#ae#, + 16#48#, 16#11#, 16#a1#, 16#e0#, 16#ab#, 16#c4#, 16#c7#, 16#e0#, 16#b0#, + 16#82#, 16#d6#, 16#93#, 16#a5#, 16#e7#, 16#fc#, 16#ed#, 16#67#, 16#5c#, + 16#f4#, 16#66#, 16#85#, 16#12#, 16#77#, 16#2c#, 16#0c#, 16#bc#, 16#64#, + 16#a7#, 16#42#, 16#c6#, 16#c6#, 16#30#, 16#f5#, 16#33#, 16#c8#, 16#cc#, + 16#72#, 16#f6#, 16#2a#, 16#e8#, 16#33#, 16#c4#, 16#0b#, 16#f2#, 16#58#, + 16#42#, 16#e9#, 16#84#, 16#bb#, 16#78#, 16#bd#, 16#bf#, 16#97#, 16#c0#, + 16#10#, 16#7d#, 16#55#, 16#bd#, 16#b6#, 16#62#, 16#f5#, 16#c4#, 16#e0#, + 16#fa#, 16#b9#, 16#84#, 16#5c#, 16#b5#, 16#14#, 16#8e#, 16#f7#, 16#39#, + 16#2d#, 16#d3#, 16#aa#, 16#ff#, 16#93#, 16#ae#, 16#1e#, 16#6b#, 16#66#, + 16#7b#, 16#b3#, 16#d4#, 16#24#, 16#76#, 16#16#, 16#d4#, 16#f5#, 16#ba#, + 16#10#, 16#d4#, 16#cf#, 16#d2#, 16#26#, 16#de#, 16#88#, 16#d3#, 16#9f#, + 16#16#, 16#fb#); + + + E_RSA : RSA_Number := (RSA_Number'Last => 1, RSA_Number'Last-2 => 1, + Others =>0); + + D_RSA : RSA_Number := + (16#53#, 16#33#, 16#9c#, 16#fd#, 16#b7#, 16#9f#, 16#c8#, 16#46#, 16#6a#, + 16#65#, 16#5c#, 16#73#, 16#16#, 16#ac#, 16#a8#, 16#5c#, 16#55#, 16#fd#, + 16#8f#, 16#6d#, 16#d8#, 16#98#, 16#fd#, 16#af#, 16#11#, 16#95#, 16#17#, + 16#ef#, 16#4f#, 16#52#, 16#e8#, 16#fd#, 16#8e#, 16#25#, 16#8d#, 16#f9#, + 16#3f#, 16#ee#, 16#18#, 16#0f#, 16#a0#, 16#e4#, 16#ab#, 16#29#, 16#69#, + 16#3c#, 16#d8#, 16#3b#, 16#15#, 16#2a#, 16#55#, 16#3d#, 16#4a#, 16#c4#, + 16#d1#, 16#81#, 16#2b#, 16#8b#, 16#9f#, 16#a5#, 16#af#, 16#0e#, 16#7f#, + 16#55#, 16#fe#, 16#73#, 16#04#, 16#df#, 16#41#, 16#57#, 16#09#, 16#26#, + 16#f3#, 16#31#, 16#1f#, 16#15#, 16#c4#, 16#d6#, 16#5a#, 16#73#, 16#2c#, + 16#48#, 16#31#, 16#16#, 16#ee#, 16#3d#, 16#3d#, 16#2d#, 16#0a#, 16#f3#, + 16#54#, 16#9a#, 16#d9#, 16#bf#, 16#7c#, 16#bf#, 16#b7#, 16#8a#, 16#d8#, + 16#84#, 16#f8#, 16#4d#, 16#5b#, 16#eb#, 16#04#, 16#72#, 16#4d#, 16#c7#, + 16#36#, 16#9b#, 16#31#, 16#de#, 16#f3#, 16#7d#, 16#0c#, 16#f5#, 16#39#, + 16#e9#, 16#cf#, 16#cd#, 16#d3#, 16#de#, 16#65#, 16#37#, 16#29#, 16#ea#, + 16#d5#, 16#D1#); + + N : constant Big_Unsigned := To_Big_Unsigned(N_RSA); + + E : constant Big_Unsigned := To_Big_Unsigned("16#010001#"); + + D : constant Big_Unsigned := To_Big_Unsigned(D_RSA); + + P : constant Big_Unsigned := To_Big_Unsigned + ("16#d3_27_37_e7_26_7f_fe_13_41_b2_d5_c0_d1_50_a8_1b_58_6f_b3_13_2b_ed_2f" + & "8d_52_62_86_4a_9c_b9_f3_0a_f3_8b_e4_48_59_8d_41_3a_17_2e_fb_80_2c" + & "21_ac_f1_c1_1c_52_0c_2f_26_a4_71_dc_ad_21_2e_ac_7c_a3_9d#"); + + Q : constant Big_Unsigned := To_Big_Unsigned + ("16#cc_88_53_d1_d5_4d_a6_30_fa_c0_04_f4_71_f2_81_c7_b8_98_2d_82_24_a4_90" + & "ed_be_b3_3d_3e_3d_5c_c9_3c_47_65_70_3d_1d_d7_91_64_2f_1f_11_6a_0d" + & "d8_52_be_24_19_b2_af_72_bf_e9_a0_30_e8_60_b0_28_8b_5d_77#"); + + Phi: constant Big_Unsigned := (P-1) * (Q-1); - N: constant RSA_Number := - (16#ec#, 16#d1#, 16#54#, 16#36#, 16#1e#, 16#c4#, 16#bc#, 16#9b#, - 16#5d#, 16#d8#, 16#5b#, 16#4b#, 16#24#, 16#0e#, 16#85#, 16#c7#, - 16#8c#, 16#e0#, 16#34#, 16#8a#, 16#68#, 16#11#, 16#78#, 16#5b#, - 16#93#, 16#d5#, 16#21#, 16#74#, 16#fd#, 16#e2#, 16#54#, 16#44#, - 16#d5#, 16#52#, 16#6d#, 16#fc#, 16#6b#, 16#1e#, 16#ce#, 16#91#, - 16#c8#, 16#9d#, 16#89#, 16#ff#, 16#ed#, 16#dc#, 16#19#, 16#de#, - 16#b0#, 16#cc#, 16#cd#, 16#fb#, 16#bd#, 16#c6#, 16#c5#, 16#a7#, - 16#8a#, 16#2f#, 16#70#, 16#d2#, 16#4e#, 16#bc#, 16#5f#, 16#97#); - - - E: constant RSA_Number := - (16#35#, 16#d5#, 16#a4#, 16#4d#, 16#ea#, 16#ee#, 16#81#, 16#76#, - 16#25#, 16#28#, 16#3e#, 16#b8#, 16#77#, 16#bb#, 16#68#, 16#2c#, - 16#7b#, 16#0b#, 16#3d#, 16#75#, 16#0c#, 16#36#, 16#bb#, 16#c0#, - 16#15#, 16#62#, 16#9c#, 16#19#, 16#de#, 16#68#, 16#4e#, 16#3f#, - 16#76#, 16#d1#, 16#13#, 16#93#, 16#45#, 16#44#, 16#28#, 16#5e#, - 16#e7#, 16#e5#, 16#84#, 16#ae#, 16#6e#, 16#d2#, 16#9c#, 16#69#, - 16#e7#, 16#c7#, 16#7a#, 16#df#, 16#ef#, 16#29#, 16#bb#, 16#94#, - 16#ef#, 16#26#, 16#3c#, 16#13#, 16#26#, 16#c0#, 16#0b#, 16#11#); - - - - Phi: constant RSA_Number := - (16#ec#, 16#d1#, 16#54#, 16#36#, 16#1e#, 16#c4#, 16#bc#, 16#9b#, - 16#5d#, 16#d8#, 16#5b#, 16#4b#, 16#24#, 16#0e#, 16#85#, 16#c7#, - 16#8c#, 16#e0#, 16#34#, 16#8a#, 16#68#, 16#11#, 16#78#, 16#5b#, - 16#93#, 16#d5#, 16#21#, 16#74#, 16#fd#, 16#e2#, 16#54#, 16#42#, - 16#cc#, 16#ff#, 16#ed#, 16#5d#, 16#a6#, 16#f5#, 16#29#, 16#46#, - 16#5a#, 16#ff#, 16#de#, 16#8f#, 16#b3#, 16#37#, 16#c7#, 16#37#, - 16#67#, 16#6a#, 16#5c#, 16#7f#, 16#5c#, 16#98#, 16#01#, 16#6d#, - 16#ff#, 16#40#, 16#18#, 16#53#, 16#71#, 16#d1#, 16#c9#, 16#28#); - - - D: constant RSA_Number := - (16#43#, 16#79#, 16#cf#, 16#6a#, 16#3c#, 16#3b#, 16#74#, 16#0d#, + Mess: constant RSA_Number := + (16#43#, 16#79#, 16#cf#, 16#00#, 16#3c#, 16#3b#, 16#74#, 16#0d#, 16#d6#, 16#34#, 16#00#, 16#0c#, 16#4d#, 16#03#, 16#43#, 16#98#, 16#29#, 16#3c#, 16#39#, 16#3e#, 16#9c#, 16#98#, 16#5f#, 16#23#, - 16#f7#, 16#8e#, 16#8c#, 16#49#, 16#cd#, 16#f3#, 16#2f#, 16#ce#, + 16#f7#, 16#8e#, 16#00#, 16#49#, 16#cd#, 16#f3#, 16#2f#, 16#ce#, 16#24#, 16#9f#, 16#8e#, 16#c3#, 16#2d#, 16#6b#, 16#a3#, 16#f7#, - 16#16#, 16#4a#, 16#b6#, 16#4e#, 16#20#, 16#b1#, 16#33#, 16#4e#, + 16#16#, 16#4a#, 16#b6#, 16#4e#, 16#00#, 16#b1#, 16#33#, 16#4e#, 16#3c#, 16#81#, 16#c0#, 16#4c#, 16#2b#, 16#d1#, 16#29#, 16#2c#, - 16#52#, 16#7d#, 16#04#, 16#74#, 16#5b#, 16#e3#, 16#f0#, 16#c9#); - - Mess: constant RSA_Number := - (16#43#, 16#79#, 16#cf#, 16#00#, 16#3c#, 16#3b#, 16#74#, 16#0d#, + 16#00#, 16#7d#, 16#04#, 16#74#, 16#5b#, 16#e3#, 16#f0#, 16#c9#, + 16#43#, 16#79#, 16#cf#, 16#00#, 16#3c#, 16#3b#, 16#74#, 16#0d#, 16#d6#, 16#34#, 16#00#, 16#0c#, 16#4d#, 16#03#, 16#43#, 16#98#, 16#29#, 16#3c#, 16#39#, 16#3e#, 16#9c#, 16#98#, 16#5f#, 16#23#, 16#f7#, 16#8e#, 16#00#, 16#49#, 16#cd#, 16#f3#, 16#2f#, 16#ce#, @@ -70,276 +93,146 @@ package body Test.RSA is 16#3c#, 16#81#, 16#c0#, 16#4c#, 16#2b#, 16#d1#, 16#29#, 16#2c#, 16#00#, 16#7d#, 16#04#, 16#74#, 16#5b#, 16#e3#, 16#f0#, 16#c9#); - Comp: RSA_Number := - (16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, - 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, - 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, - 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, - 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, - 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, - 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, 16#00#, - 16#00#, 16#00#, 16#00#, 16#00#, 16#49#, 16#96#, 16#02#, 16#d2#); - - M: constant Bytes := (16#D4#, 16#36#, 16#e9#, 16#95#, 16#69#, 16#fd#, 16#32#, - 16#a7#, 16#c8#, 16#a0#, 16#5b#, 16#bc#, 16#90#, 16#d3#, - 16#2c#, 16#49#); - - A: Public_Key_RSA ; - B: Private_Key_RSA ; - - Public_Key: Public_Key_RSA; - Private_Key: Private_Key_RSA; - Private_Key_Comp: Private_Key_RSA; - - Plain_Text: constant RSA_Number :=(others => 0); - Cipher_Text, Plain_Text2 : RSA_Number; - ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- -------------------------------- Register RSA Test 1 -------------------------------- ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- - - procedure Register_Tests(T : in out RSA_Test) is - use Test_Cases.Registration; - begin - Register_Routine(T, RSA_Test1'Access,"RSA_Test1."); - Register_Routine(T, RSA_Test2'Access,"RSA_Test2."); - Register_Routine(T, RSA_Test3'Access,"RSA_Test3."); - Register_Routine(T, RSA_Test4'Access,"RSA_Test4."); - Register_Routine(T, RSA_Test5'Access,"RSA_Test5."); - Register_Routine(T, RSA_Test6'Access,"RSA_Test6."); - Register_Routine(T, RSA_Test7'Access,"RSA_Test7."); - Register_Routine(T, RSA_Test8'Access,"RSA_new To_Big_Unsigned Test."); - Register_Routine(T, RSA_Test9'Access,"RSA_new Encrypt/Decrypt Test."); - Register_Routine(T, RSA_Test10'Access,"RSA_new Get Public/Private Key Test."); - Register_Routine(T, RSA_Test11'Access,"RSA_new Set Private Key Test."); - end Register_Tests; - ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- ----------------------------------- Name RSA Test ----------------------------------- ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- - - function Name(T : RSA_Test) return Test_String is - begin - return new String'("RSA Test"); - end Name; - ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- ------------------------------------- Start Tests ----------------------------------- ------------------------------------------------------------------------------------- --------------------------------------- Test 1 -------------------------------------- ------------------------------------------------------------------------------------- - - procedure RSA_Test1(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + + ---------------------------------------------------------------------------- + --------------------------- Aux Methods ------------------------------------- + ---------------------------------------------------------------------------- + + procedure Set_Keypair(Pk : out Public_Key_RSA; Sk : out Private_Key_RSA) is begin - --- E(E'Last) := 16#11#; - Set_Public_Key(N, E, Public_Key); - Set_Private_Key(N, D, Phi, Private_Key); - Assert(Verify_Key_Pair(Private_Key, Public_Key), "RSA verifying Key failed."); - - end RSA_Test1; - ------------------------------------------------------------------------------------- --------------------------------------- Test 2 -------------------------------------- ------------------------------------------------------------------------------------- - - procedure RSA_Test2(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + Set_Public_Key(N, E, Pk); + Set_Private_Key(N, D, P, Q, Phi, Sk); + end Set_Keypair; + + ---------------------------------------------------------------------------- + + function RSA_Enc_Dec_Test(Msg : RSA_Number) return Boolean is + SK : Private_Key_RSA; + Pk : Public_Key_RSA; + Plaintext : RSA_Number; + Ciphertext : RSA_Number; begin - - declare - Ciphertext: constant Bytes := OAEP_Encrypt(Public_Key, M); - Plaintext: constant Bytes := OAEP_Decrypt(Private_Key, Ciphertext); - begin - Assert(M'Length = Plaintext'Length, "RSA failed."); - for I in 0 .. M'Length - 1 loop - Assert(Plaintext(Plaintext'First+I) = M(M'First+I), "RSA failed."); - end loop; - end; - - end RSA_Test2; - ------------------------------------------------------------------------------------- --------------------------------------- Test 3 -------------------------------------- ------------------------------------------------------------------------------------- - - procedure RSA_Test3(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + Set_Keypair(Pk, Sk); + Encrypt(pk ,Msg , Ciphertext); + Decrypt(Sk ,Ciphertext, Plaintext); + return Plaintext = Msg; + end RSA_Enc_Dec_Test; + + ---------------------------------------------------------------------------- + -------------------------- Register RSA Test 1 ----------------------------- + ---------------------------------------------------------------------------- + + procedure Register_Tests(T : in out RSA_Test) is + use Test_Cases.Registration; begin - - Gen_Key(A, B); - Assert(Verify_Key_Pair(B, A), "RSA verifying Key failed."); - - end RSA_Test3; - ------------------------------------------------------------------------------------- --------------------------------------- Test 4 -------------------------------------- ------------------------------------------------------------------------------------- - - procedure RSA_Test4(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + Register_Routine(T, Test_Verify_Key_Pair'Access, + "RSA: verify keypair test."); + Register_Routine(T, Test_OAEP'Access,"RSA: OAEP test."); +-- Register_Routine(T, Test_Gen_Keypair'Access, +-- "RSA: generate keypair test."); + Register_Routine(T, Test_Get_Public_key'Access, + "RSA: get public key test"); + Register_Routine(T, Test_Encryption'Access, + "RSA: encryption test."); + Register_Routine(T, Test_Zero_Encryption'Access, + "RSA: zero encryption test."); + Register_Routine(T, Test_Get_Private_Key'Access, + "RSA: Get Public/Private Key Test."); + end Register_Tests; + + ---------------------------------------------------------------------------- + ------------------------------ Name RSA Test ------------------------------- + ---------------------------------------------------------------------------- + + function Name(T : RSA_Test) return Test_String is begin - declare - Private_Key_New : Private_Key_RSA; - N_New, D_New, Phi_New : RSA_Number; - begin - Get_Private_Key(Private_Key, N_New, D_New, Phi_New); - Set_Private_Key(N_New, D_New, Phi_New, Private_Key_New); - - Assert(Verify_Key_Pair(Private_Key_New, Public_Key), "RSA getting private key failed."); - end; - end RSA_Test4; - ------------------------------------------------------------------------------------- --------------------------------------- Test 5 -------------------------------------- ------------------------------------------------------------------------------------- - - procedure RSA_Test5(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + return new String'("RSA Test"); + end Name; + +--------------------------------- Start Tests -------------------------------- + + procedure Test_Verify_Key_Pair(T : in out Test_Cases.Test_Case'Class) is + SK : Private_Key_RSA; + Pk : Public_Key_RSA; begin - declare - Public_Key_New : Public_Key_RSA; - N_New, E_New : RSA_Number; - begin - Get_Public_Key(A, N_New, E_New); - Set_Public_Key(N_New, E_New, Public_Key_New); - - Assert(Verify_Key_Pair(B, Public_Key_New), "RSA getting public key failed."); - end; - end RSA_Test5; - ------------------------------------------------------------------------------------- --------------------------------------- Test 6 -------------------------------------- ------------------------------------------------------------------------------------- - - procedure RSA_Test6(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + Set_Keypair(Pk,Sk); + Assert(Verify_Key_Pair(SK, PK), "RSA verifying Key failed."); + end Test_Verify_Key_Pair; + + --------------------------------------------------------------------------- + + procedure Test_OAEP(T : in out Test_Cases.Test_Case'Class) is + SK : Private_Key_RSA; + Pk : Public_Key_RSA; + Msg : constant Bytes := + (16#01#, 16#02#, 16#03#, 16#04#, 16#05#, 16#06#, 16#07#, 16#08#, + 16#09#, 16#0a#, 16#0b#, 16#0c#, 16#0d#, 16#0e#, 16#0f#, 16#00#, + 16#11#, 16#22#, 16#33#, 16#44#, 16#55#, 16#66#, 16#77#, 16#88#, + 16#99#, 16#aa#, 16#bb#, 16#cc#, 16#dd#, 16#ee#, 16#ff#, 16#de#, + 16#ad#, 16#be#, 16#ef#, 16#ff#); begin + Set_Keypair(Pk,Sk); declare - Private_Key_New : Private_Key_RSA; - N_Tem, D_Tem, Phi_Tem : RSA_Number; - N_New, D_New, Phi_New : RSA.Big.Big_Unsigned; + Ciphertext: constant Bytes := OAEP_Encrypt(Pk, Msg); + Plaintext: constant Bytes := OAEP_Decrypt(Sk, Ciphertext); begin - Get_Private_Key(Private_Key, N_Tem, D_Tem, Phi_Tem); - N_New := RSA.Big.Utils.To_Big_Unsigned(N_Tem); - D_New := RSA.Big.Utils.To_Big_Unsigned(D_Tem); - Phi_New := RSA.Big.Utils.To_Big_Unsigned(Phi_Tem); - Set_Private_Key(N_New, D_New, Phi_New, Private_Key_New); - - Assert(Verify_Key_Pair(Private_Key_New, Public_Key), "RSA getting private key failed."); + Assert(Msg = Plaintext, "OEAP RSA failed."); end; - end RSA_Test6; + end Test_OAEP; ------------------------------------------------------------------------------------- --------------------------------------- Test 7 -------------------------------------- ------------------------------------------------------------------------------------- + --------------------------------------------------------------------------- - procedure RSA_Test7(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + procedure Test_Gen_Keypair(T : in out Test_Cases.Test_Case'Class) is + SK : Private_Key_RSA; + Pk : Public_Key_RSA; begin - Encrypt(A ,Plain_Text ,Cipher_Text); - Decrypt(B ,Cipher_Text, Plain_Text2); - Assert(Plain_Text = Plain_Text2, "RSA encrypting and decrypting failed."); + Gen_Key(Pk, Sk); + Assert(Verify_Key_Pair(Sk, Pk), "RSA verifying Key failed."); + end Test_Gen_Keypair; + - end RSA_Test7; +------------------------------------------------------------------------------- - ------------------------------------------------------------------------------------ - -------------------------------------- Test 8 -------------------------------------- - ------------------------------------------------------------------------------------ - - procedure RSA_Test8(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; - use RSA.Big; - BigMess, BigCiph, BigDecMess: RSA.Big.Big_Unsigned; + procedure Test_Get_Public_Key(T : in out Test_Cases.Test_Case'Class) is + Pk : Public_Key_RSA; + N_New, E_New : RSA_Number; begin - BigMess := RSA.Big.Utils.To_Big_Unsigned(S => "1234567890"); - Set_Public_Key(N, E, Public_Key); - Set_Private_Key(N, D, Phi, Private_Key); - RSA.Encrypt(Public_Key => Public_Key, - Plaintext => BigMess, - Ciphertext => BigCiph); - RSA.Decrypt(Private_Key => Private_Key, - Ciphertext => BigCiph, - Plaintext => BigDecMess); - - Assert(BigMess = BigDecMess, "RSA_new Encrypt/Decrypt failed."); - - end RSA_Test8; - - ------------------------------------------------------------------------------------ - -------------------------------------- Test 9 -------------------------------------- - ------------------------------------------------------------------------------------ + Set_Public_Key(N, E, Pk); + Get_Public_Key(Pk, N_new, E_New); + + Assert(N_New = N_RSA and E_New = E_RSA, "RSA getting public key failed."); + end Test_Get_Public_Key; - procedure RSA_Test9(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; - - Plaintext:RSA_number; - Ciphertext:RSA_number; + --------------------------------------------------------------------------- + procedure Test_Encryption(T : in out Test_Cases.Test_Case'Class) is + Msg : constant RSA_Number := (others => 16#7f#); begin - Set_Public_Key(N, E, Public_Key); - Set_Private_Key(N, D, Phi, Private_Key); - RSA.Encrypt(Public_Key => Public_Key, - Plaintext => Mess, - Ciphertext => Ciphertext); - RSA.Decrypt(Private_Key => Private_Key, - Ciphertext => Ciphertext, - Plaintext => Plaintext); - - Assert(Mess = Plaintext, "RSA_new Encrypt/Decrypt failed."); - - - end RSA_Test9; + Assert(RSA_Enc_Dec_Test(Msg), "RSA encrypting and decrypting failed."); + end Test_Encryption; - ------------------------------------------------------------------------------------ - -------------------------------------- Test 10 ------------------------------------- - ------------------------------------------------------------------------------------ + -------------------------------------------------------------------------- - procedure RSA_Test10(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; - NComp, EComp, DComp, PhiComp: RSA_Number; + procedure Test_Zero_Encryption(T : in out Test_Cases.Test_Case'Class) is + Msg : constant RSA_Number := (others => 16#0#); begin + Assert(RSA_Enc_Dec_Test(Msg), + "RSA encryption/decryption of zero failed."); + end Test_Zero_Encryption; - RSA.Get_Public_Key(Public_Key => Public_Key, - N => NComp, - E => EComp); - RSA.Get_Private_Key(Private_Key => Private_Key, - N => NComp, - D => DComp, - Phi => PhiComp); - Assert(N=NComp AND E=EComp AND D= DComp AND Phi=PhiComp, "RSA_new Get Public/Private Key failed."); - - end RSA_Test10; - - ------------------------------------------------------------------------------------ - ------------------------------------------------------------------------------------ - -------------------------------------- Test 11 ------------------------------------- - ------------------------------------------------------------------------------------ - - procedure RSA_Test11(T : in out Test_Cases.Test_Case'Class) is - use AUnit.Assertions; + ------------------------------------------------------------------------- + procedure Test_Get_Private_Key(T : in out Test_Cases.Test_Case'Class) is + NComp, DComp, PhiComp, PComp, QComp : RSA_Number; + Sk : Private_Key_RSA; begin - - RSA.Set_Private_Key(N => N, - D => D, - Phi => Phi, - Private_Key => Private_Key_Comp); - Assert(Private_Key = Private_Key_Comp, "RSA_new Set Private Key(RSA_Number) failed."); - - RSA.Set_Private_Key(N => RSA.Big.Utils.To_Big_Unsigned(N), - D => RSA.Big.Utils.To_Big_Unsigned(D), - Phi => RSA.Big.Utils.To_Big_Unsigned(Phi), - Private_Key => Private_Key_Comp); - Assert(Private_Key = Private_Key_Comp, "RSA_new Set Private Key(Big_Number) failed."); - - - end RSA_Test11; - + Set_Private_Key(N, D, P, Q, Phi, Sk); + RSA.Get_Private_Key(Sk, NComp, DComp, PComp, QComp, PhiComp); + + Assert(N = To_Big_Unsigned(NComp) and D = To_Big_Unsigned(DComp) and + P = To_Big_Unsigned(PComp) and Q = To_Big_Unsigned(QComp) and + Phi = To_Big_Unsigned(PhiComp), + "RSA_new Get Public/Private Key failed."); + end Test_Get_Private_Key; + end Test.RSA; diff --git a/test/test-rsa.ads b/test/test-rsa.ads index 4a58c82..821dbb8 100644 --- a/test/test-rsa.ads +++ b/test/test-rsa.ads @@ -10,26 +10,11 @@ package Test.RSA is function Name(T: RSA_Test) return Test_String; - procedure RSA_Test1(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test2(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test3(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test4(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test5(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test6(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test7(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test8(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test9(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test10(T: in out Test_Cases.Test_Case'Class); - - procedure RSA_Test11(T: in out Test_Cases.Test_Case'Class); - + procedure Test_Verify_Key_Pair(T: in out Test_Cases.Test_Case'Class); + procedure Test_OAEP(T: in out Test_Cases.Test_Case'Class); + procedure Test_Gen_Keypair(T: in out Test_Cases.Test_Case'Class); + procedure Test_Get_Public_Key(T: in out Test_Cases.Test_Case'Class); + procedure Test_Encryption(T: in out Test_Cases.Test_Case'Class); + procedure Test_Zero_Encryption(T: in out Test_Cases.Test_Case'Class); + procedure Test_Get_Private_Key(T: in out Test_Cases.Test_Case'Class); end Test.RSA; diff --git a/test/test-suite_asymmetric_ciphers.adb b/test/test-suite_asymmetric_ciphers.adb index d1ffed2..1555dfa 100644 --- a/test/test-suite_asymmetric_ciphers.adb +++ b/test/test-suite_asymmetric_ciphers.adb @@ -3,23 +3,17 @@ with Test.RSA; with Test.ECDSA; package body Test.Suite_Asymmetric_Ciphers is - use Aunit.Test_Suites; - - Result: aliased Test_Suite; - Test_DSA: aliased Test.DSA.DSA_Test; - Test_RSA: aliased Test.RSA.RSA_Test; - Test_ECDSA: aliased Test.ECDSA.ECDSA_Test; - - function Suite return Aunit.Test_Suites.Access_Test_Suite is - - begin - - Add_Test(Result'Access, Test_DSA'Access); - Add_Test(Result'Access, Test_RSA'Access); - Add_Test(Result'Access, Test_ECDSA'Access); - - return Result'Access; - - end Suite; - + use Aunit.Test_Suites; + Result: aliased Test_Suite; + Test_DSA: aliased Test.DSA.DSA_Test; + Test_RSA: aliased Test.RSA.RSA_Test; + Test_ECDSA: aliased Test.ECDSA.ECDSA_Test; + + function Suite return Aunit.Test_Suites.Access_Test_Suite is + begin + Add_Test(Result'Access, Test_DSA'Access); + Add_Test(Result'Access, Test_RSA'Access); + Add_Test(Result'Access, Test_ECDSA'Access); + return Result'Access; + end Suite; end Test.Suite_Asymmetric_Ciphers;