Skip to content

Commit 4d97fed

Browse files
committed
Added CreateCryptoGroup(securityLevel) to CurveGroupAlgebra and LibCrypto/EllipticCurveAlgebra.
1 parent 4483959 commit 4d97fed

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

CompactCryptoGroupAlgebra.LibCrypto.Tests/EllipticCurves/EllipticCurveAlgebraTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// CompactCryptoGroupAlgebra.LibCrypto - OpenSSL libcrypto implementation of CompactCryptoGroupAlgebra interfaces
22

3-
// SPDX-FileCopyrightText: 2021 Lukas Prediger <lumip@lumip.de>
3+
// SPDX-FileCopyrightText: 2022 Lukas Prediger <lumip@lumip.de>
44
// SPDX-License-Identifier: GPL-3.0-or-later WITH GPL-3.0-linking-exception
55
// SPDX-FileType: SOURCE
66

@@ -329,6 +329,27 @@ public void TestCreateCryptoGroup()
329329
Assert.That(group.Algebra.Equals(expectedGroupAlgebra));
330330
}
331331

332+
[Test]
333+
[TestCase(126)]
334+
[TestCase(128)]
335+
[TestCase(140)]
336+
[TestCase(190)]
337+
[TestCase(200)]
338+
[TestCase(254)]
339+
[TestCase(260)]
340+
public void TestCreateCryptoGroupWithSecurityLevel(int securityLevel)
341+
{
342+
var group = EllipticCurveAlgebra.CreateCryptoGroup(securityLevel);
343+
Assert.That(group.SecurityLevel >= securityLevel);
344+
}
345+
346+
[Test]
347+
public void TestCreateCryptoGroupWithTooHighSecurityLevel()
348+
{
349+
Assert.Throws<ArgumentOutOfRangeException>(() => EllipticCurveAlgebra.CreateCryptoGroup(261));
350+
}
351+
352+
332353
}
333354

334355
}

CompactCryptoGroupAlgebra.LibCrypto/EllipticCurves/EllipticCurveAlgebra.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// CompactCryptoGroupAlgebra.LibCrypto - OpenSSL libcrypto implementation of CompactCryptoGroupAlgebra interfaces
22

3-
// SPDX-FileCopyrightText: 2021 Lukas Prediger <lumip@lumip.de>
3+
// SPDX-FileCopyrightText: 2022 Lukas Prediger <lumip@lumip.de>
44
// SPDX-License-Identifier: GPL-3.0-or-later WITH GPL-3.0-linking-exception
55
// SPDX-FileType: SOURCE
66

@@ -336,6 +336,34 @@ public static CryptoGroup<SecureBigNumber, ECPoint> CreateCryptoGroup(EllipticCu
336336
return new CryptoGroup<SecureBigNumber, ECPoint>(new EllipticCurveAlgebra(curveId));
337337
}
338338

339+
/// <summary>
340+
/// Creates a <see cref="CryptoGroup{SecureBigNumber, ECPoint}" /> instance at least satisfying the given security level.
341+
/// </summary>
342+
/// <param name="securityLevel">The minimal security level for the curve to be created.</param>
343+
public static CryptoGroup<SecureBigNumber, ECPoint> CreateCryptoGroup(int securityLevel)
344+
{
345+
EllipticCurveID curveId;
346+
if (securityLevel <= 128)
347+
{
348+
curveId = EllipticCurveID.Prime256v1; // == Secp256r1
349+
}
350+
else if (securityLevel <= 192)
351+
{
352+
curveId = EllipticCurveID.Secp384r1;
353+
}
354+
else if (securityLevel <= 260)
355+
{
356+
curveId = EllipticCurveID.Secp521r1;
357+
}
358+
else
359+
{
360+
throw new ArgumentOutOfRangeException(
361+
$"There are no curves that satisfy a security level of {securityLevel}.", nameof(securityLevel)
362+
);
363+
}
364+
return CreateCryptoGroup(curveId);
365+
}
366+
339367
}
340368

341369
}

CompactCryptoGroupAlgebra.Tests/EllipticCurves/CurveGroupAlgebraTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// CompactCryptoGroupAlgebra - C# implementation of abelian group algebra for experimental cryptography
22

3-
// SPDX-FileCopyrightText: 2020-2021 Lukas Prediger <lumip@lumip.de>
3+
// SPDX-FileCopyrightText: 2022 Lukas Prediger <lumip@lumip.de>
44
// SPDX-License-Identifier: GPL-3.0-or-later
55
// SPDX-FileType: SOURCE
66

@@ -369,5 +369,26 @@ public void TestCreateCryptoGroup()
369369
var group = CurveGroupAlgebra.CreateCryptoGroup(curveParameters);
370370
Assert.AreEqual(expectedGroupAlgebra, group.Algebra);
371371
}
372+
373+
[Test]
374+
[TestCase(126)]
375+
[TestCase(128)]
376+
[TestCase(140)]
377+
[TestCase(190)]
378+
[TestCase(200)]
379+
[TestCase(254)]
380+
[TestCase(260)]
381+
public void TestCreateCryptoGroupWithSecurityLevel(int securityLevel)
382+
{
383+
var group = CurveGroupAlgebra.CreateCryptoGroup(securityLevel);
384+
Assert.That(group.SecurityLevel >= securityLevel);
385+
}
386+
387+
[Test]
388+
public void TestCreateCryptoGroupWithTooHighSecurityLevel()
389+
{
390+
Assert.Throws<ArgumentOutOfRangeException>(() => CurveGroupAlgebra.CreateCryptoGroup(261));
391+
}
392+
372393
}
373394
}

CompactCryptoGroupAlgebra/EllipticCurves/CurveGroupAlgebra.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// CompactCryptoGroupAlgebra - C# implementation of abelian group algebra for experimental cryptography
22

3-
// SPDX-FileCopyrightText: 2020-2021 Lukas Prediger <lumip@lumip.de>
3+
// SPDX-FileCopyrightText: 2022 Lukas Prediger <lumip@lumip.de>
44
// SPDX-License-Identifier: GPL-3.0-or-later
55
// SPDX-FileType: SOURCE
66

@@ -163,5 +163,43 @@ public static CryptoGroup<BigInteger, CurvePoint> CreateCryptoGroup(CurveParamet
163163
{
164164
return new CryptoGroup<BigInteger, CurvePoint>(new CurveGroupAlgebra(parameters));
165165
}
166+
167+
/// <summary>
168+
/// Creates a <see cref="CryptoGroup{BigInteger, CurvePoint}" /> instance at least satisfying a given security level.
169+
/// </summary>
170+
/// <param name="securityLevel">The minimal security level for the curve to be created.</param>
171+
public static CryptoGroup<BigInteger, CurvePoint> CreateCryptoGroup(int securityLevel)
172+
{
173+
CurveParameters parameters;
174+
if (securityLevel <= 126)
175+
{
176+
parameters = CurveParameters.Curve25519;
177+
}
178+
else if (securityLevel <= 128)
179+
{
180+
parameters = CurveParameters.NISTP256;
181+
}
182+
else if (securityLevel <= 190)
183+
{
184+
parameters = CurveParameters.M383;
185+
}
186+
else if (securityLevel <= 254)
187+
{
188+
parameters = CurveParameters.M511;
189+
}
190+
else if (securityLevel <= 260)
191+
{
192+
parameters = CurveParameters.NISTP521;
193+
}
194+
else
195+
{
196+
throw new ArgumentOutOfRangeException(
197+
$"There are no pre-configured curves that satisfy a security level of {securityLevel}.", nameof(securityLevel)
198+
);
199+
}
200+
201+
return CreateCryptoGroup(parameters);
202+
}
203+
166204
}
167205
}

0 commit comments

Comments
 (0)