|
| 1 | +{ *********************************************************************************** } |
| 2 | +{ * CryptoLib Library * } |
| 3 | +{ * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * } |
| 4 | +{ * Github Repository <https://github.com/Xor-el> * } |
| 5 | + |
| 6 | +{ * Distributed under the MIT software license, see the accompanying file LICENSE * } |
| 7 | +{ * or visit http://www.opensource.org/licenses/mit-license.php. * } |
| 8 | + |
| 9 | +{ * Acknowledgements: * } |
| 10 | +{ * * } |
| 11 | +{ * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * } |
| 12 | +{ * development of this library * } |
| 13 | + |
| 14 | +{ * ******************************************************************************* * } |
| 15 | + |
| 16 | +(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *) |
| 17 | + |
| 18 | +unit ISO9796Tests; |
| 19 | + |
| 20 | +interface |
| 21 | + |
| 22 | +{$IFDEF FPC} |
| 23 | +{$MODE DELPHI} |
| 24 | +{$ENDIF FPC} |
| 25 | + |
| 26 | +uses |
| 27 | + Classes, |
| 28 | + SysUtils, |
| 29 | +{$IFDEF FPC} |
| 30 | + fpcunit, |
| 31 | + testregistry, |
| 32 | +{$ELSE} |
| 33 | + TestFramework, |
| 34 | +{$ENDIF FPC} |
| 35 | + ClpBigInteger, |
| 36 | + ClpEncoders, |
| 37 | + ClpRsaEngine, |
| 38 | + ClpIRsaEngine, |
| 39 | + ClpISO9796d1Encoding, |
| 40 | + ClpIISO9796d1Encoding, |
| 41 | + ClpRsaKeyParameters, |
| 42 | + ClpIRsaKeyParameters, |
| 43 | + ClpIAsymmetricBlockCipher, |
| 44 | + ClpCryptoLibTypes, |
| 45 | + CryptoLibTestBase; |
| 46 | + |
| 47 | +type |
| 48 | + |
| 49 | + TTestISO9796 = class(TCryptoLibAlgorithmTestCase) |
| 50 | + private |
| 51 | + class var |
| 52 | + // Test 1 and 2 parameters |
| 53 | + FMod1, FPub1, FPri1: TBigInteger; |
| 54 | + FMsg1, FSig1, FMsg2, FSig2: TCryptoLibByteArray; |
| 55 | + |
| 56 | + // Test 3 parameters |
| 57 | + FMod2, FPub2, FPri2: TBigInteger; |
| 58 | + FMsg3, FSig3: TCryptoLibByteArray; |
| 59 | + |
| 60 | + class constructor Create; |
| 61 | + |
| 62 | + private |
| 63 | + function IsSameAs(const a: TCryptoLibByteArray; off: Int32; |
| 64 | + const b: TCryptoLibByteArray): Boolean; |
| 65 | + |
| 66 | + protected |
| 67 | + procedure SetUp; override; |
| 68 | + procedure TearDown; override; |
| 69 | + |
| 70 | + published |
| 71 | + procedure TestISO9796d1_Test1; |
| 72 | + procedure TestISO9796d1_Test2; |
| 73 | + procedure TestISO9796d1_Test3; |
| 74 | + |
| 75 | + end; |
| 76 | + |
| 77 | +implementation |
| 78 | + |
| 79 | +{ TTestISO9796 } |
| 80 | + |
| 81 | +class constructor TTestISO9796.Create; |
| 82 | +begin |
| 83 | + // ISO 9796-1 Test 1 and 2 parameters |
| 84 | + FMod1 := TBigInteger.Create('0100000000000000000000000000000000bba2d15dbb303c8a21c5ebbcbae52b7125087920dd7cdf358ea119fd66fb064012ec8ce692f0a0b8e8321b041acd40b7', 16); |
| 85 | + FPub1 := TBigInteger.Create('03', 16); |
| 86 | + FPri1 := TBigInteger.Create('2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac9f0783a49dd5f6c5af651f4c9d0dc9281c96a3f16a85f9572d7cc3f2d0f25a9dbf1149e4cdc32273faadd3fda5dcda7', 16); |
| 87 | + |
| 88 | + FMsg1 := THex.Decode('0cbbaa99887766554433221100'); |
| 89 | + // sig1 = mod1.Subtract(BigInteger("309f873d8ded8379490f6097eaafdabc137d3ebfd8f25ab5f138d56a719cdc526bdd022ea65dabab920a81013a85d092e04d3e421caab717c90d89ea45a8d23a", 16)).ToByteArray() |
| 90 | + FSig1 := FMod1.Subtract(TBigInteger.Create('309f873d8ded8379490f6097eaafdabc137d3ebfd8f25ab5f138d56a719cdc526bdd022ea65dabab920a81013a85d092e04d3e421caab717c90d89ea45a8d23a', 16)).ToByteArray(); |
| 91 | + |
| 92 | + FMsg2 := THex.Decode('fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210'); |
| 93 | + FSig2 := TBigInteger.Create('319bb9becb49f3ed1bca26d0fcf09b0b0a508e4d0bd43b350f959b72cd25b3af47d608fdcd248eada74fbe19990dbeb9bf0da4b4e1200243a14e5cab3f7e610c', 16).ToByteArray(); |
| 94 | + |
| 95 | + // ISO 9796-1 Test 3 parameters |
| 96 | + FMod2 := TBigInteger.Create('ffffff7fa27087c35ebead78412d2bdffe0301edd494df13458974ea89b364708f7d0f5a00a50779ddf9f7d4cb80b8891324da251a860c4ec9ef288104b3858d', 16); |
| 97 | + FPub2 := TBigInteger.Create('03', 16); |
| 98 | + FPri2 := TBigInteger.Create('2aaaaa9545bd6bf5e51fc7940adcdca5550080524e18cfd88b96e8d1c19de6121b13fac0eb0495d47928e047724d91d1740f6968457ce53ec8e24c9362ce84b5', 16); |
| 99 | + |
| 100 | + FMsg3 := THex.Decode('0112233445566778899aabbccd'); |
| 101 | + FSig3 := FMod2.Subtract(TBigInteger.Create('58e59ffb4b1fb1bcdbf8d1fe9afa3730c78a318a1134f5791b7313d480ff07ac319b068edf8f212945cb09cf33df30ace54f4a063fcca0b732f4b662dc4e2454', 16)).ToByteArray(); |
| 102 | +end; |
| 103 | + |
| 104 | +procedure TTestISO9796.SetUp; |
| 105 | +begin |
| 106 | + inherited; |
| 107 | +end; |
| 108 | + |
| 109 | +procedure TTestISO9796.TearDown; |
| 110 | +begin |
| 111 | + inherited; |
| 112 | +end; |
| 113 | + |
| 114 | +function TTestISO9796.IsSameAs(const a: TCryptoLibByteArray; off: Int32; |
| 115 | + const b: TCryptoLibByteArray): Boolean; |
| 116 | +var |
| 117 | + i: Int32; |
| 118 | +begin |
| 119 | + if (System.Length(a) - off) <> System.Length(b) then |
| 120 | + begin |
| 121 | + Result := False; |
| 122 | + Exit; |
| 123 | + end; |
| 124 | + |
| 125 | + for i := 0 to System.Length(b) - 1 do |
| 126 | + begin |
| 127 | + if a[i + off] <> b[i] then |
| 128 | + begin |
| 129 | + Result := False; |
| 130 | + Exit; |
| 131 | + end; |
| 132 | + end; |
| 133 | + |
| 134 | + Result := True; |
| 135 | +end; |
| 136 | + |
| 137 | +procedure TTestISO9796.TestISO9796d1_Test1; |
| 138 | +var |
| 139 | + pubParameters, privParameters: IRsaKeyParameters; |
| 140 | + rsa: IRsaEngine; |
| 141 | + eng: IISO9796d1Encoding; |
| 142 | + data: TCryptoLibByteArray; |
| 143 | +begin |
| 144 | + pubParameters := TRsaKeyParameters.Create(False, FMod1, FPub1); |
| 145 | + privParameters := TRsaKeyParameters.Create(True, FMod1, FPri1); |
| 146 | + rsa := TRsaEngine.Create(); |
| 147 | + |
| 148 | + // ISO 9796-1 - private sign, public verify |
| 149 | + eng := TISO9796d1Encoding.Create(rsa as IAsymmetricBlockCipher); |
| 150 | + |
| 151 | + eng.Init(True, privParameters); |
| 152 | + eng.SetPadBits(4); |
| 153 | + |
| 154 | + data := eng.ProcessBlock(FMsg1, 0, System.Length(FMsg1)); |
| 155 | + |
| 156 | + eng.Init(False, pubParameters); |
| 157 | + |
| 158 | + CheckTrue(AreEqual(FSig1, data), 'failed ISO9796-1 generation Test 1'); |
| 159 | + |
| 160 | + data := eng.ProcessBlock(data, 0, System.Length(data)); |
| 161 | + |
| 162 | + CheckTrue(AreEqual(FMsg1, data), 'failed ISO9796-1 retrieve Test 1'); |
| 163 | +end; |
| 164 | + |
| 165 | +procedure TTestISO9796.TestISO9796d1_Test2; |
| 166 | +var |
| 167 | + pubParameters, privParameters: IRsaKeyParameters; |
| 168 | + rsa: IRsaEngine; |
| 169 | + eng: IISO9796d1Encoding; |
| 170 | + data: TCryptoLibByteArray; |
| 171 | +begin |
| 172 | + pubParameters := TRsaKeyParameters.Create(False, FMod1, FPub1); |
| 173 | + privParameters := TRsaKeyParameters.Create(True, FMod1, FPri1); |
| 174 | + rsa := TRsaEngine.Create(); |
| 175 | + |
| 176 | + // ISO 9796-1 - private sign, public verify |
| 177 | + eng := TISO9796d1Encoding.Create(rsa as IAsymmetricBlockCipher); |
| 178 | + |
| 179 | + eng.Init(True, privParameters); |
| 180 | + |
| 181 | + data := eng.ProcessBlock(FMsg2, 0, System.Length(FMsg2)); |
| 182 | + |
| 183 | + eng.Init(False, pubParameters); |
| 184 | + |
| 185 | + CheckTrue(IsSameAs(data, 1, FSig2), 'failed ISO9796-1 generation Test 2'); |
| 186 | + |
| 187 | + data := eng.ProcessBlock(data, 0, System.Length(data)); |
| 188 | + |
| 189 | + CheckTrue(AreEqual(FMsg2, data), 'failed ISO9796-1 retrieve Test 2'); |
| 190 | +end; |
| 191 | + |
| 192 | +procedure TTestISO9796.TestISO9796d1_Test3; |
| 193 | +var |
| 194 | + pubParameters, privParameters: IRsaKeyParameters; |
| 195 | + rsa: IRsaEngine; |
| 196 | + eng: IISO9796d1Encoding; |
| 197 | + data: TCryptoLibByteArray; |
| 198 | +begin |
| 199 | + pubParameters := TRsaKeyParameters.Create(False, FMod2, FPub2); |
| 200 | + privParameters := TRsaKeyParameters.Create(True, FMod2, FPri2); |
| 201 | + rsa := TRsaEngine.Create(); |
| 202 | + |
| 203 | + // ISO 9796-1 - private sign, public verify |
| 204 | + eng := TISO9796d1Encoding.Create(rsa as IAsymmetricBlockCipher); |
| 205 | + |
| 206 | + eng.Init(True, privParameters); |
| 207 | + eng.SetPadBits(4); |
| 208 | + |
| 209 | + data := eng.ProcessBlock(FMsg3, 0, System.Length(FMsg3)); |
| 210 | + |
| 211 | + eng.Init(False, pubParameters); |
| 212 | + |
| 213 | + CheckTrue(IsSameAs(FSig3, 1, data), 'failed ISO9796-1 generation Test 3'); |
| 214 | + |
| 215 | + data := eng.ProcessBlock(data, 0, System.Length(data)); |
| 216 | + |
| 217 | + CheckTrue(IsSameAs(FMsg3, 0, data), 'failed ISO9796-1 retrieve Test 3'); |
| 218 | +end; |
| 219 | + |
| 220 | +initialization |
| 221 | + |
| 222 | +// Register any test cases with the test runner |
| 223 | + |
| 224 | +{$IFDEF FPC} |
| 225 | + RegisterTest(TTestISO9796); |
| 226 | +{$ELSE} |
| 227 | + RegisterTest(TTestISO9796.Suite); |
| 228 | +{$ENDIF FPC} |
| 229 | + |
| 230 | +end. |
0 commit comments