-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Added `ExportBc` and `ImportBc` methods and `IsBcImportExportImplemented` to asymmetric keys which support ex-/import using a key info object + Added constructor to asymmetric private keys which accepts a Bouncy Castle private key, if the public key can be generated from that private key + Added `BouncyCastleAsymmetricAlgorithmBase` which supports any key generator type + Added Streamlined NTRU Prime asymmetric PQC key exchange algorithm + Added BIKE asymmetric PQC key exchange algorithm + Added HQC asymmetric PQC key exchange algorithm + Added Picnic asymmetric PQC signature algorithm - Fixed Falcon, FrodoKEM, CRYSTALS-Kyber, NTRUEncrypt and SPHINCS+ asymmetric public key `Bits` property
- Loading branch information
Showing
69 changed files
with
1,672 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Org.BouncyCastle.Pqc.Crypto.Bike; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace wan24.Crypto.BC | ||
{ | ||
/// <summary> | ||
/// BIKE asymmetric algorithm | ||
/// </summary> | ||
public sealed record class AsymmetricBikeAlgorithm | ||
: BouncyCastleAsymmetricAlgorithmBase< | ||
AsymmetricBikePublicKey, | ||
AsymmetricBikePrivateKey, | ||
BikeKeyPairGenerator, | ||
BikeKeyGenerationParameters, | ||
BikeParameters, | ||
BikePublicKeyParameters, | ||
BikePrivateKeyParameters, | ||
AsymmetricBikeAlgorithm | ||
> | ||
{ | ||
/// <summary> | ||
/// Algorithm name | ||
/// </summary> | ||
public const string ALGORITHM_NAME = "BIKE"; | ||
/// <summary> | ||
/// Algorithm value | ||
/// </summary> | ||
public const int ALGORITHM_VALUE = 15; | ||
/// <summary> | ||
/// Default key size in bits | ||
/// </summary> | ||
public const int DEFAULT_KEY_SIZE = 256; | ||
/// <summary> | ||
/// Algorithm usages | ||
/// </summary> | ||
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange; | ||
/// <summary> | ||
/// Display name | ||
/// </summary> | ||
public const string DISPLAY_NAME = "BIKE"; | ||
|
||
/// <summary> | ||
/// Allowed key sizes in bits | ||
/// </summary> | ||
private static readonly ReadOnlyCollection<int> _AllowedKeySizes; | ||
|
||
/// <summary> | ||
/// Static constructor | ||
/// </summary> | ||
static AsymmetricBikeAlgorithm() => _AllowedKeySizes = new List<int>() | ||
{ | ||
128,// 128 bit security | ||
192,// 192 bit security | ||
256// 256 bit security | ||
}.AsReadOnly(); | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public AsymmetricBikeAlgorithm() | ||
: base(ALGORITHM_NAME, ALGORITHM_VALUE, USAGES, isEllipticCurveAlgorithm: false, _AllowedKeySizes, isPostQuantum: true, DEFAULT_KEY_SIZE) | ||
{ } | ||
|
||
/// <inheritdoc/> | ||
public override string DisplayName => DISPLAY_NAME; | ||
|
||
/// <inheritdoc/> | ||
protected override BikeParameters GetEngineParameters(CryptoOptions options) => AsymmetricBikeHelper.GetParameters(options.AsymmetricKeyBits); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Org.BouncyCastle.Pqc.Crypto.Bike; | ||
|
||
namespace wan24.Crypto.BC | ||
{ | ||
/// <summary> | ||
/// BIKE asymmetric algorithm helper | ||
/// </summary> | ||
public static class AsymmetricBikeHelper | ||
{ | ||
/// <summary> | ||
/// Get the key size in bits | ||
/// </summary> | ||
/// <param name="param">Parameters</param> | ||
/// <returns>Key size in bits</returns> | ||
public static int GetKeySize(this BikeParameters param) | ||
{ | ||
if (param == BikeParameters.bike128) return 128; | ||
if (param == BikeParameters.bike192) return 192; | ||
if (param == BikeParameters.bike256) return 256; | ||
throw new ArgumentException("Invalid BIKE parameters", nameof(param)); | ||
} | ||
|
||
/// <summary> | ||
/// Get the BIKE parameters | ||
/// </summary> | ||
/// <param name="keySize">Key size in bits</param> | ||
/// <returns>Parameters</returns> | ||
public static BikeParameters GetParameters(int keySize) => keySize switch | ||
{ | ||
128 => BikeParameters.bike128, | ||
192 => BikeParameters.bike192, | ||
256 => BikeParameters.bike256, | ||
_ => throw new ArgumentException("Invalid key size", nameof(keySize)) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Org.BouncyCastle.Crypto; | ||
using Org.BouncyCastle.Pqc.Crypto.Bike; | ||
using wan24.Core; | ||
|
||
namespace wan24.Crypto.BC | ||
{ | ||
/// <summary> | ||
/// BIKE asymmetric private key | ||
/// </summary> | ||
public sealed record class AsymmetricBikePrivateKey | ||
: BouncyCastleAsymmetricPqcPrivateKeyExchangeKeyBase< | ||
AsymmetricBikePublicKey, | ||
AsymmetricBikeAlgorithm, | ||
BikePublicKeyParameters, | ||
BikePrivateKeyParameters, | ||
BikeKemGenerator, | ||
BikeKemExtractor, | ||
AsymmetricBikePrivateKey | ||
> | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public AsymmetricBikePrivateKey() : base(AsymmetricBikeAlgorithm.ALGORITHM_NAME) { } | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="keyData">Key data</param> | ||
public AsymmetricBikePrivateKey(byte[] keyData) : base(AsymmetricBikeAlgorithm.ALGORITHM_NAME, keyData) { } | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="keys">Keys</param> | ||
public AsymmetricBikePrivateKey(AsymmetricCipherKeyPair keys) : base(AsymmetricBikeAlgorithm.ALGORITHM_NAME, keys) { } | ||
|
||
/// <inheritdoc/> | ||
new public static bool IsBcImportExportImplemented => false; | ||
|
||
/// <inheritdoc/> | ||
protected override byte[] SerializeKeyData() => SerializeFullKeyData(); | ||
|
||
/// <inheritdoc/> | ||
protected override void DeserializeKeyData() => DeserializeFullKeyData(); | ||
|
||
/// <inheritdoc/> | ||
protected override BikePublicKeyParameters GetPublicKey(BikePrivateKeyParameters privateKey) => throw new NotSupportedException(); | ||
|
||
/// <inheritdoc/> | ||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
if (Keys?.Private is not BikePrivateKeyParameters privateKey) return; | ||
privateKey.GetH0().Clear(); | ||
privateKey.GetH1().Clear(); | ||
privateKey.GetSigma().Clear(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override async Task DisposeCore() | ||
{ | ||
await base.DisposeCore().DynamicContext(); | ||
if (Keys?.Private is not BikePrivateKeyParameters privateKey) return; | ||
privateKey.GetH0().Clear(); | ||
privateKey.GetH1().Clear(); | ||
privateKey.GetSigma().Clear(); | ||
} | ||
|
||
/// <summary> | ||
/// Cast to public key | ||
/// </summary> | ||
/// <param name="privateKey">Private key</param> | ||
public static implicit operator AsymmetricBikePublicKey(AsymmetricBikePrivateKey privateKey) => privateKey.PublicKey; | ||
|
||
/// <summary> | ||
/// Cast from serialized data | ||
/// </summary> | ||
/// <param name="data">Data</param> | ||
public static explicit operator AsymmetricBikePrivateKey(byte[] data) => Import<AsymmetricBikePrivateKey>(data); | ||
} | ||
} |
Oops, something went wrong.