|
| 1 | +{ *********************************************************************************** } |
| 2 | +{ * CryptoLib Library * } |
| 3 | +{ * Author - 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 | +{ * the development of this library * } |
| 13 | +{ * ******************************************************************************* * } |
| 14 | + |
| 15 | +(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *) |
| 16 | + |
| 17 | +unit AlgorithmFinderTests; |
| 18 | + |
| 19 | +interface |
| 20 | + |
| 21 | +{$IFDEF FPC} |
| 22 | +{$MODE DELPHI} |
| 23 | +{$ENDIF FPC} |
| 24 | + |
| 25 | +uses |
| 26 | + SysUtils, |
| 27 | +{$IFDEF FPC} |
| 28 | + fpcunit, |
| 29 | + testregistry, |
| 30 | +{$ELSE} |
| 31 | + TestFramework, |
| 32 | +{$ENDIF FPC} |
| 33 | + ClpAsn1Objects, |
| 34 | + ClpIAsn1Objects, |
| 35 | + ClpIX509Asn1Objects, |
| 36 | + ClpX509Asn1Objects, |
| 37 | + ClpPkcsObjectIdentifiers, |
| 38 | + ClpNistObjectIdentifiers, |
| 39 | + ClpOiwObjectIdentifiers, |
| 40 | + ClpEdECObjectIdentifiers, |
| 41 | + ClpX9ObjectIdentifiers, |
| 42 | + ClpPkcsRsaAsn1Objects, |
| 43 | + ClpIPkcsRsaAsn1Objects, |
| 44 | + ClpDefaultDigestAlgorithmFinder, |
| 45 | + ClpIDigestAlgorithmFinder, |
| 46 | + ClpDefaultMacAlgorithmFinder, |
| 47 | + ClpIMacAlgorithmFinder, |
| 48 | + ClpDefaultSignatureAlgorithmFinder, |
| 49 | + ClpISignatureAlgorithmFinder, |
| 50 | + ClpX509Utilities, |
| 51 | + ClpCryptoLibTypes, |
| 52 | + CryptoLibTestBase; |
| 53 | + |
| 54 | +type |
| 55 | + TAlgorithmFinderTest = class(TCryptoLibAlgorithmTestCase) |
| 56 | + published |
| 57 | + procedure TestDigestFindByName; |
| 58 | + procedure TestDigestFindBySignatureAlgorithm; |
| 59 | + procedure TestDigestFindByOid; |
| 60 | + procedure TestDigestFindPssDelegation; |
| 61 | + procedure TestDigestFindUnknownName; |
| 62 | + procedure TestDigestFindNilOid; |
| 63 | + procedure TestMacFindKnown; |
| 64 | + procedure TestMacFindUnknown; |
| 65 | + procedure TestSignatureFindKnown; |
| 66 | + procedure TestSignatureFindPss; |
| 67 | + procedure TestSignatureFindEd25519; |
| 68 | + procedure TestSignatureFindEcdsa; |
| 69 | + procedure TestSignatureFindUnknown; |
| 70 | + end; |
| 71 | + |
| 72 | +implementation |
| 73 | + |
| 74 | +{ TAlgorithmFinderTest } |
| 75 | + |
| 76 | +procedure TAlgorithmFinderTest.TestDigestFindByName; |
| 77 | +var |
| 78 | + LDigestAlg: IAlgorithmIdentifier; |
| 79 | +begin |
| 80 | + LDigestAlg := TDefaultDigestAlgorithmFinder.Instance.Find('SHA-256'); |
| 81 | + CheckNotNull(LDigestAlg, 'digest algorithm should not be nil'); |
| 82 | + CheckTrue(LDigestAlg.Algorithm.Equals(TNistObjectIdentifiers.IdSha256), |
| 83 | + 'SHA-256 digest OID mismatch'); |
| 84 | +end; |
| 85 | + |
| 86 | +procedure TAlgorithmFinderTest.TestDigestFindBySignatureAlgorithm; |
| 87 | +var |
| 88 | + LSignatureAlg, LDigestAlg: IAlgorithmIdentifier; |
| 89 | +begin |
| 90 | + LSignatureAlg := TAlgorithmIdentifier.Create(TPkcsObjectIdentifiers.Sha256WithRsaEncryption, |
| 91 | + TDerNull.Instance); |
| 92 | + LDigestAlg := TDefaultDigestAlgorithmFinder.Instance.Find(LSignatureAlg); |
| 93 | + CheckNotNull(LDigestAlg, 'digest algorithm should not be nil'); |
| 94 | + CheckTrue(LDigestAlg.Algorithm.Equals(TNistObjectIdentifiers.IdSha256), |
| 95 | + 'signature-to-digest OID mismatch'); |
| 96 | +end; |
| 97 | + |
| 98 | +procedure TAlgorithmFinderTest.TestDigestFindByOid; |
| 99 | +var |
| 100 | + LDigestAlg: IAlgorithmIdentifier; |
| 101 | +begin |
| 102 | + LDigestAlg := TDefaultDigestAlgorithmFinder.Instance.Find(TNistObjectIdentifiers.IdSha384); |
| 103 | + CheckNotNull(LDigestAlg, 'digest algorithm should not be nil'); |
| 104 | + CheckTrue(LDigestAlg.Algorithm.Equals(TNistObjectIdentifiers.IdSha384), |
| 105 | + 'digest OID mismatch'); |
| 106 | +end; |
| 107 | + |
| 108 | +procedure TAlgorithmFinderTest.TestDigestFindPssDelegation; |
| 109 | +var |
| 110 | + LHashAlgId, LSignatureAlg, LDigestAlg: IAlgorithmIdentifier; |
| 111 | + LPssParams: IRsassaPssParameters; |
| 112 | +begin |
| 113 | + LHashAlgId := TAlgorithmIdentifier.Create(TNistObjectIdentifiers.IdSha256, TDerNull.Instance); |
| 114 | + LPssParams := TRsassaPssParameters.Create(LHashAlgId, |
| 115 | + TAlgorithmIdentifier.Create(TPkcsObjectIdentifiers.IdMgf1, LHashAlgId) as IAlgorithmIdentifier, |
| 116 | + TDerInteger.Create(32) as IDerInteger, TRsassaPssParameters.DefaultTrailerField); |
| 117 | + LSignatureAlg := TAlgorithmIdentifier.Create(TPkcsObjectIdentifiers.IdRsassaPss, LPssParams); |
| 118 | + LDigestAlg := TDefaultDigestAlgorithmFinder.Instance.Find(LSignatureAlg); |
| 119 | + CheckNotNull(LDigestAlg, 'PSS digest algorithm should not be nil'); |
| 120 | + CheckTrue(LDigestAlg.Algorithm.Equals(TNistObjectIdentifiers.IdSha256), |
| 121 | + 'PSS digest OID mismatch'); |
| 122 | +end; |
| 123 | + |
| 124 | +procedure TAlgorithmFinderTest.TestDigestFindUnknownName; |
| 125 | +var |
| 126 | + LDigestAlg: IAlgorithmIdentifier; |
| 127 | +begin |
| 128 | + LDigestAlg := TDefaultDigestAlgorithmFinder.Instance.Find('UNKNOWN-DIGEST-NAME'); |
| 129 | + CheckNull(LDigestAlg, 'unknown digest name should return nil'); |
| 130 | +end; |
| 131 | + |
| 132 | +procedure TAlgorithmFinderTest.TestDigestFindNilOid; |
| 133 | +var |
| 134 | + LNilOid: IDerObjectIdentifier; |
| 135 | +begin |
| 136 | + LNilOid := nil; |
| 137 | + try |
| 138 | + TDefaultDigestAlgorithmFinder.Instance.Find(LNilOid); |
| 139 | + Fail('expected EArgumentNilCryptoLibException'); |
| 140 | + except |
| 141 | + on E: EArgumentNilCryptoLibException do |
| 142 | + CheckEquals('digestOid', E.Message); |
| 143 | + end; |
| 144 | +end; |
| 145 | + |
| 146 | +procedure TAlgorithmFinderTest.TestMacFindKnown; |
| 147 | +var |
| 148 | + LMacAlg: IAlgorithmIdentifier; |
| 149 | +begin |
| 150 | + LMacAlg := TDefaultMacAlgorithmFinder.Instance.Find('HMACSHA256'); |
| 151 | + CheckNotNull(LMacAlg, 'HMACSHA256 should not be nil'); |
| 152 | + CheckTrue(LMacAlg.Algorithm.Equals(TPkcsObjectIdentifiers.IdHmacWithSha256), |
| 153 | + 'HMACSHA256 OID mismatch'); |
| 154 | + CheckTrue(LMacAlg.Parameters.Equals(TDerNull.Instance), 'HMACSHA256 params should be NULL'); |
| 155 | + |
| 156 | + LMacAlg := TDefaultMacAlgorithmFinder.Instance.Find('HMACSHA1'); |
| 157 | + CheckNotNull(LMacAlg, 'HMACSHA1 should not be nil'); |
| 158 | + CheckTrue(LMacAlg.Algorithm.Equals(TOiwObjectIdentifiers.IdSha1), |
| 159 | + 'HMACSHA1 OID mismatch'); |
| 160 | + CheckTrue(TX509Utilities.IsAbsentParameters(LMacAlg.Parameters), |
| 161 | + 'HMACSHA1 params should be absent'); |
| 162 | + |
| 163 | + LMacAlg := TDefaultMacAlgorithmFinder.Instance.Find('HMACSHA3-512'); |
| 164 | + CheckNotNull(LMacAlg, 'HMACSHA3-512 should not be nil'); |
| 165 | + CheckTrue(LMacAlg.Algorithm.Equals(TNistObjectIdentifiers.IdHMacWithSha3_512), |
| 166 | + 'HMACSHA3-512 OID mismatch'); |
| 167 | + CheckTrue(TX509Utilities.IsAbsentParameters(LMacAlg.Parameters), |
| 168 | + 'HMACSHA3-512 params should be absent'); |
| 169 | +end; |
| 170 | + |
| 171 | +procedure TAlgorithmFinderTest.TestMacFindUnknown; |
| 172 | +var |
| 173 | + LMacAlg: IAlgorithmIdentifier; |
| 174 | +begin |
| 175 | + LMacAlg := TDefaultMacAlgorithmFinder.Instance.Find('HMACUNKNOWN'); |
| 176 | + CheckNull(LMacAlg, 'unknown MAC name should return nil'); |
| 177 | +end; |
| 178 | + |
| 179 | +procedure TAlgorithmFinderTest.TestSignatureFindKnown; |
| 180 | +var |
| 181 | + LSignatureAlg: IAlgorithmIdentifier; |
| 182 | +begin |
| 183 | + LSignatureAlg := TDefaultSignatureAlgorithmFinder.Instance.Find('SHA256WITHRSA'); |
| 184 | + CheckNotNull(LSignatureAlg, 'SHA256WITHRSA should not be nil'); |
| 185 | + CheckTrue(LSignatureAlg.Algorithm.Equals(TPkcsObjectIdentifiers.Sha256WithRsaEncryption), |
| 186 | + 'SHA256WITHRSA OID mismatch'); |
| 187 | + CheckTrue(LSignatureAlg.Parameters.Equals(TDerNull.Instance), |
| 188 | + 'SHA256WITHRSA params should be NULL'); |
| 189 | +end; |
| 190 | + |
| 191 | +procedure TAlgorithmFinderTest.TestSignatureFindPss; |
| 192 | +var |
| 193 | + LSignatureAlg: IAlgorithmIdentifier; |
| 194 | + LPssParams: IRsassaPssParameters; |
| 195 | +begin |
| 196 | + LSignatureAlg := TDefaultSignatureAlgorithmFinder.Instance.Find('SHA256WITHRSAANDMGF1'); |
| 197 | + CheckNotNull(LSignatureAlg, 'SHA256WITHRSAANDMGF1 should not be nil'); |
| 198 | + CheckTrue(LSignatureAlg.Algorithm.Equals(TPkcsObjectIdentifiers.IdRsassaPss), |
| 199 | + 'SHA256WITHRSAANDMGF1 OID mismatch'); |
| 200 | + LPssParams := TRsassaPssParameters.GetInstance(LSignatureAlg.Parameters); |
| 201 | + CheckNotNull(LPssParams, 'PSS parameters should not be nil'); |
| 202 | + CheckTrue(LPssParams.HashAlgorithm.Algorithm.Equals(TNistObjectIdentifiers.IdSha256), |
| 203 | + 'PSS hash algorithm mismatch'); |
| 204 | + CheckEquals(32, LPssParams.SaltLength.IntValueExact, 'PSS salt length mismatch'); |
| 205 | +end; |
| 206 | + |
| 207 | +procedure TAlgorithmFinderTest.TestSignatureFindEd25519; |
| 208 | +var |
| 209 | + LSignatureAlg: IAlgorithmIdentifier; |
| 210 | +begin |
| 211 | + LSignatureAlg := TDefaultSignatureAlgorithmFinder.Instance.Find('Ed25519'); |
| 212 | + CheckNotNull(LSignatureAlg, 'Ed25519 should not be nil'); |
| 213 | + CheckTrue(LSignatureAlg.Algorithm.Equals(TEdECObjectIdentifiers.IdEd25519), |
| 214 | + 'Ed25519 OID mismatch'); |
| 215 | + CheckTrue(TX509Utilities.IsAbsentParameters(LSignatureAlg.Parameters), |
| 216 | + 'Ed25519 params should be absent'); |
| 217 | +end; |
| 218 | + |
| 219 | +procedure TAlgorithmFinderTest.TestSignatureFindEcdsa; |
| 220 | +var |
| 221 | + LSignatureAlg: IAlgorithmIdentifier; |
| 222 | +begin |
| 223 | + LSignatureAlg := TDefaultSignatureAlgorithmFinder.Instance.Find('SHA256WITHECDSA'); |
| 224 | + CheckNotNull(LSignatureAlg, 'SHA256WITHECDSA should not be nil'); |
| 225 | + CheckTrue(LSignatureAlg.Algorithm.Equals(TX9ObjectIdentifiers.ECDsaWithSha256), |
| 226 | + 'SHA256WITHECDSA OID mismatch'); |
| 227 | + CheckTrue(TX509Utilities.IsAbsentParameters(LSignatureAlg.Parameters), |
| 228 | + 'SHA256WITHECDSA params should be absent'); |
| 229 | +end; |
| 230 | + |
| 231 | +procedure TAlgorithmFinderTest.TestSignatureFindUnknown; |
| 232 | +begin |
| 233 | + try |
| 234 | + TDefaultSignatureAlgorithmFinder.Instance.Find('UNKNOWN-SIGNATURE-NAME'); |
| 235 | + Fail('expected EArgumentCryptoLibException'); |
| 236 | + except |
| 237 | + on E: EArgumentCryptoLibException do |
| 238 | + CheckTrue(Pos('Unknown signature name:', E.Message) > 0, |
| 239 | + 'Wrong exception message: ' + E.Message); |
| 240 | + end; |
| 241 | +end; |
| 242 | + |
| 243 | +initialization |
| 244 | + |
| 245 | +{$IFDEF FPC} |
| 246 | + RegisterTest(TAlgorithmFinderTest); |
| 247 | +{$ELSE} |
| 248 | + RegisterTest(TAlgorithmFinderTest.Suite); |
| 249 | +{$ENDIF FPC} |
| 250 | + |
| 251 | +end. |
0 commit comments