Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CryptoLib.Tests/src/Asn1/EnumeratedTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface
TestFramework,
{$ENDIF FPC}
ClpAsn1Core,
ClpAsn1Objects,
ClpIAsn1Objects,
ClpIAsn1Core,
ClpCryptoLibTypes,
Expand Down Expand Up @@ -75,6 +76,10 @@ TEnumeratedTest = class(TCryptoLibAlgorithmTestCase)
/// Makes sure multiple identically sized values are parsed correctly.
/// </summary>
procedure TestReadingMultipleTripleByteItems;
/// <summary>
/// An ENUMERATED is encoded like an INTEGER, so a negative value is valid and must round-trip.
/// </summary>
procedure TestNegativeValueRoundTrips;

end;

Expand Down Expand Up @@ -182,6 +187,23 @@ procedure TEnumeratedTest.TestReadingMultipleTripleByteItems;
CheckEquals('1.3.6.1', LObjectId.Id, 'Unexpected OBJECT IDENTIFIER value');
end;

procedure TEnumeratedTest.TestNegativeValueRoundTrips;
var
LEnum, LDecoded: IDerEnumerated;
LEncoded: TCryptoLibByteArray;
LValue: Int32;
begin
LEnum := TDerEnumerated.Create(Int32(-1)) as IDerEnumerated;
CheckEquals(-1, LEnum.IntValueExact, 'a negative ENUMERATED keeps its value');

LEncoded := LEnum.GetEncoded();
LDecoded := TDerEnumerated.GetInstance(LEncoded);
CheckEquals(-1, LDecoded.IntValueExact, 'a negative ENUMERATED round-trips through DER');

CheckTrue(LDecoded.TryGetIntValueExact(LValue), 'TryGetIntValueExact succeeds for an in-range value');
CheckEquals(-1, LValue, 'TryGetIntValueExact yields the value');
end;

initialization

{$IFDEF FPC}
Expand Down
80 changes: 80 additions & 0 deletions CryptoLib.Tests/src/Crypto/MlDsaTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface
ClpISecureRandom,
ClpSecureRandom,
ClpCryptoLibTypes,
ClpCryptoLibExceptions,
NistSecureRandom;

type
Expand Down Expand Up @@ -90,10 +91,15 @@ TTestMlDsa = class(TCryptoLibAlgorithmTestCase)
// Run a vector file through AImpl. AParameters nil => derive per record.
procedure RunParamVectors(const ARelativePath: string;
AImpl: TMlDsaVectorImpl; const AParameters: IMlDsaParameters);
procedure ImplComponentEncodingLengths(const AParameters: IMlDsaParameters);
// AKind: 0 = private-key encoding, 1 = public-key encoding, 2 = seed
procedure ExpectBadLengthRejected(const AParameters: IMlDsaParameters;
const AValid: TCryptoLibByteArray; AKind: Int32);
published
procedure TestConsistency44;
procedure TestConsistency65;
procedure TestConsistency87;
procedure TestComponentEncodingLengths;
procedure TestKeyGen;
procedure TestKeyGenAcvp44;
procedure TestKeyGenAcvp65;
Expand Down Expand Up @@ -248,6 +254,80 @@ procedure TTestMlDsa.TestConsistency87;
ImplConsistency(TMlDsaParameters.MlDsa87);
end;

procedure TTestMlDsa.ExpectBadLengthRejected(const AParameters: IMlDsaParameters;
const AValid: TCryptoLibByteArray; AKind: Int32);

procedure CheckOne(const ABuf: TCryptoLibByteArray);
var
LRaised: Boolean;
begin
LRaised := False;
try
case AKind of
0:
TMlDsaPrivateKeyParameters.FromEncoding(AParameters, ABuf);
1:
TMlDsaPublicKeyParameters.FromEncoding(AParameters, ABuf);
else
TMlDsaPrivateKeyParameters.FromSeed(AParameters, ABuf);
end;
except
on E: EArgumentCryptoLibException do
LRaised := True;
end;
CheckTrue(LRaised, 'a wrong-length ML-DSA component must be rejected');
end;

var
LBuf: TCryptoLibByteArray;
begin
LBuf := System.Copy(AValid);
System.SetLength(LBuf, System.Length(AValid) + 1);
CheckOne(LBuf);

LBuf := System.Copy(AValid);
System.SetLength(LBuf, System.Length(AValid) - 1);
CheckOne(LBuf);
end;

procedure TTestMlDsa.ImplComponentEncodingLengths(const AParameters: IMlDsaParameters);
var
LKpg: IAsymmetricCipherKeyPairGenerator;
LKp: IAsymmetricCipherKeyPair;
LPriv: IMlDsaPrivateKeyParameters;
LPub: IMlDsaPublicKeyParameters;
LPrivEnc, LPubEnc, LSeed: TCryptoLibByteArray;
begin
LKpg := TMlDsaKeyPairGenerator.Create;
LKpg.Init(TMlDsaKeyGenerationParameters.Create(FRandom, AParameters) as IKeyGenerationParameters);
LKp := LKpg.GenerateKeyPair();
LPriv := LKp.Private as IMlDsaPrivateKeyParameters;
LPub := LKp.Public as IMlDsaPublicKeyParameters;

LPrivEnc := LPriv.GetEncoded();
LPubEnc := LPub.GetEncoded();
LSeed := LPriv.GetSeed();

// correctly sized components round-trip
CheckNotNull(TMlDsaPrivateKeyParameters.FromEncoding(AParameters, LPrivEnc),
'the private key encoding round-trips');
CheckNotNull(TMlDsaPublicKeyParameters.FromEncoding(AParameters, LPubEnc),
'the public key encoding round-trips');
CheckNotNull(TMlDsaPrivateKeyParameters.FromSeed(AParameters, LSeed), 'the seed round-trips');

// a component one byte too long or too short is rejected
ExpectBadLengthRejected(AParameters, LPrivEnc, 0);
ExpectBadLengthRejected(AParameters, LPubEnc, 1);
ExpectBadLengthRejected(AParameters, LSeed, 2);
end;

procedure TTestMlDsa.TestComponentEncodingLengths;
begin
ImplComponentEncodingLengths(TMlDsaParameters.MlDsa44);
ImplComponentEncodingLengths(TMlDsaParameters.MlDsa65);
ImplComponentEncodingLengths(TMlDsaParameters.MlDsa87);
end;

procedure TTestMlDsa.ImplKeyGen(const AName: string; const AData: TRspTxtRecord;
const AParameters: IMlDsaParameters);
var
Expand Down
70 changes: 70 additions & 0 deletions CryptoLib.Tests/src/Crypto/SlhDsaTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface
FixedSecureRandom,
ClpSlhDsaParameters,
ClpISlhDsaParameters,
ClpCryptoLibExceptions,
ClpSlhDsaGenerators,
ClpSlhDsaSigner,
ClpHashSlhDsaSigner,
Expand Down Expand Up @@ -98,12 +99,16 @@ TTestSlhDsa = class(TCryptoLibAlgorithmTestCase)
procedure RunParamVectors(const ARelativePath: string;
AImpl: TSlhDsaVectorImpl; const AParameters: ISlhDsaParameters;
ASampleOnly: Boolean);
procedure ImplComponentEncodingLengths(const AParameters: ISlhDsaParameters);
procedure ExpectBadLengthRejected(const AParameters: ISlhDsaParameters;
const AValid: TCryptoLibByteArray; APrivate: Boolean);
public
procedure SetUp; override;
end;

TTestSlhDsaFast = class(TTestSlhDsa)
published
procedure TestComponentEncodingLengths;
procedure Test02KeyGenAcvp192f;
procedure Test04KeyGenAcvpShake256f;
procedure Test10ContextFastSha2128f;
Expand Down Expand Up @@ -302,6 +307,66 @@ procedure TTestSlhDsa.RunSigVer(const ARelativePath: string;
RunParamVectors(ARelativePath, ImplSigVer, AParameters, False);
end;

procedure TTestSlhDsa.ExpectBadLengthRejected(const AParameters: ISlhDsaParameters;
const AValid: TCryptoLibByteArray; APrivate: Boolean);

procedure CheckOne(const ABuf: TCryptoLibByteArray);
var
LRaised: Boolean;
begin
LRaised := False;
try
if APrivate then
TSlhDsaPrivateKeyParameters.FromEncoding(AParameters, ABuf)
else
TSlhDsaPublicKeyParameters.FromEncoding(AParameters, ABuf);
except
on E: EArgumentCryptoLibException do
LRaised := True;
end;
CheckTrue(LRaised, 'a wrong-length SLH-DSA component must be rejected');
end;

var
LBuf: TCryptoLibByteArray;
begin
LBuf := System.Copy(AValid);
System.SetLength(LBuf, System.Length(AValid) + 1);
CheckOne(LBuf);

LBuf := System.Copy(AValid);
System.SetLength(LBuf, System.Length(AValid) - 1);
CheckOne(LBuf);
end;

procedure TTestSlhDsa.ImplComponentEncodingLengths(const AParameters: ISlhDsaParameters);
var
LKpg: IAsymmetricCipherKeyPairGenerator;
LKp: IAsymmetricCipherKeyPair;
LPriv: ISlhDsaPrivateKeyParameters;
LPub: ISlhDsaPublicKeyParameters;
LPrivEnc, LPubEnc: TCryptoLibByteArray;
begin
LKpg := TSlhDsaKeyPairGenerator.Create;
LKpg.Init(TSlhDsaKeyGenerationParameters.Create(FRandom, AParameters) as IKeyGenerationParameters);
LKp := LKpg.GenerateKeyPair;
LPriv := LKp.Private as ISlhDsaPrivateKeyParameters;
LPub := LKp.Public as ISlhDsaPublicKeyParameters;

LPrivEnc := LPriv.GetEncoded();
LPubEnc := LPub.GetEncoded();

// correctly sized components round-trip
CheckNotNull(TSlhDsaPrivateKeyParameters.FromEncoding(AParameters, LPrivEnc),
'the private key encoding round-trips');
CheckNotNull(TSlhDsaPublicKeyParameters.FromEncoding(AParameters, LPubEnc),
'the public key encoding round-trips');

// a component one byte too long or too short is rejected
ExpectBadLengthRejected(AParameters, LPrivEnc, True);
ExpectBadLengthRejected(AParameters, LPubEnc, False);
end;

procedure TTestSlhDsa.ImplConsistency(const AParameters: ISlhDsaParameters);
var
LMsg: TCryptoLibByteArray;
Expand Down Expand Up @@ -629,6 +694,11 @@ procedure TTestSlhDsaSlow.Test41ContextSlowShake256sShake256;
RunContextVectors('shake-256s-shake256.rsp', TSlhDsaParameters.SlhDsaShake_256sWithShake256, True);
end;

procedure TTestSlhDsaFast.TestComponentEncodingLengths;
begin
ImplComponentEncodingLengths(TSlhDsaParameters.SlhDsaSha2_128s);
end;

procedure TTestSlhDsaFast.Test95ConsistencySha2_128s;
begin
ImplConsistency(TSlhDsaParameters.SlhDsaSha2_128s);
Expand Down
9 changes: 9 additions & 0 deletions CryptoLib.Tests/src/Pkcs/Pkcs12StoreTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ interface
CertTestUtilities,
CryptoTestKeys,
ClpEncoders,
ClpCryptoLibConfig,
CryptoLibTestBase,
PkcsVectors;

Expand Down Expand Up @@ -127,6 +128,7 @@ TTestPkcs12Store = class(TCryptoLibAlgorithmTestCase)
function GetExpectedPkcs12ChainSerial(AIndex: Int32): TBigInteger;
protected
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestCertsOnly;
procedure TestPkcs12Store_LoadAndVerifyKeyAndChain;
Expand Down Expand Up @@ -170,6 +172,7 @@ implementation
procedure TTestPkcs12Store.SetUp;
begin
inherited SetUp;
TCryptoLibConfig.Pkcs12.StoreIterationCount := 1024;
FPasswd := StringToCharArray(TPkcs12StoreVectors.GetPassword('MainKeyAndChain'));
FNoFriendlyPassword := StringToCharArray(TPkcs12StoreVectors.GetPassword('NoFriendly'));
FStoragePassword := StringToCharArray(TPkcs12StoreVectors.GetPassword('StorageIssue'));
Expand All @@ -196,6 +199,12 @@ procedure TTestPkcs12Store.SetUp;
FCertChainCycle := TPkcs12StoreVectors.LoadStoreBytes('CertChainCycle');
end;

procedure TTestPkcs12Store.TearDown;
begin
TCryptoLibConfig.Pkcs12.ResetToDefaults();
inherited TearDown;
end;

function TTestPkcs12Store.GetFirst(const AAliases: TCryptoLibStringArray): String;
begin
if (AAliases = nil) or (System.Length(AAliases) = 0) then
Expand Down
19 changes: 18 additions & 1 deletion CryptoLib/src/Asn1/ClpAsn1Dumper.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ TAsn1Dumper = class sealed(TObject)
const ABytes: TCryptoLibByteArray; AOff, ALen: Int32); static;

public
/// <summary>Append a formatted dump of a DER object to a caller-supplied buffer (non-verbose).</summary>
class procedure AppendAsString(const ABuf: TStringBuilder;
const AObj: IAsn1Encodable); overload; static;
/// <summary>Append a formatted dump of a DER object to a caller-supplied buffer.</summary>
class procedure AppendAsString(const ABuf: TStringBuilder; const AObj: IAsn1Encodable;
AVerbose: Boolean); overload; static;
/// <summary>
/// Dump out a DER object as a formatted string, in non-verbose mode.
/// </summary>
Expand Down Expand Up @@ -366,6 +372,17 @@ class procedure TAsn1Dumper.DumpBinaryDataAsString(const ABuf: TStringBuilder;
end;
end;

class procedure TAsn1Dumper.AppendAsString(const ABuf: TStringBuilder; const AObj: IAsn1Encodable);
begin
AppendAsString(ABuf, AObj, False);
end;

class procedure TAsn1Dumper.AppendAsString(const ABuf: TStringBuilder; const AObj: IAsn1Encodable;
AVerbose: Boolean);
begin
AsString('', AVerbose, AObj.ToAsn1Object(), ABuf);
end;

class function TAsn1Dumper.DumpAsString(const AObj: IAsn1Encodable): String;
begin
Result := DumpAsString(AObj, False);
Expand All @@ -377,7 +394,7 @@ class function TAsn1Dumper.DumpAsString(const AObj: IAsn1Encodable; AVerbose: Bo
begin
LBuf := TStringBuilder.Create();
try
AsString('', AVerbose, AObj.ToAsn1Object(), LBuf);
AppendAsString(LBuf, AObj, AVerbose);
Result := LBuf.ToString();
finally
LBuf.Free;
Expand Down
Loading
Loading