-
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.
* Changed PQ automatic configuration to use the implemented algorithms as default, moving the classical non-PQ algorithms as counter algorithms, or check the hybrid algorithms in strict mode, too + Added `RandomDataProvider` + Added `Extensions` which contains methods for getting/restoring a `VmpcRandomGenerator` internal state + Added `ChaCha20Rng` + Added `CipherRng` + Added `XSalsa20Rng` + Added `BouncyCastleRngWrapper` to merge the `wan24-Crypto` and Bouncy Castle RNG APIs by wrapping Bouncy Castle's `IRandomGenerator` with `ISeedableRng` + Added `DisposableRngWrapper` + Added `IBouncyCastleRng` + Added NTRUEncrypt asymmetric algorithm (but disabled 'cause of a bug in the Bouncy Castle library) + `BouncyCastleRandomGenerator` does forward seed to `RND` now - Fixed asymmetric key disposing
- Loading branch information
Showing
25 changed files
with
1,353 additions
and
31 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
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,71 @@ | ||
using Org.BouncyCastle.Pqc.Crypto.Ntru; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace wan24.Crypto.BC | ||
{ | ||
/// <summary> | ||
/// NTRUEncrypt asymmetric algorithm | ||
/// </summary> | ||
public sealed record class AsymmetricNtruEncryptAlgorithm | ||
: BouncyCastleAsymmetricAlgorithmBase< | ||
AsymmetricNtruEncryptPublicKey, | ||
AsymmetricNtruEncryptPrivateKey, | ||
NtruKeyPairGenerator, | ||
NtruKeyGenerationParameters, | ||
NtruParameters, | ||
NtruPublicKeyParameters, | ||
NtruPrivateKeyParameters, | ||
AsymmetricNtruEncryptAlgorithm | ||
> | ||
{ | ||
/// <summary> | ||
/// Algorithm name | ||
/// </summary> | ||
public const string ALGORITHM_NAME = "NTRUENCRYPT"; | ||
/// <summary> | ||
/// Algorithm value | ||
/// </summary> | ||
public const int ALGORITHM_VALUE = 7; | ||
/// <summary> | ||
/// Default key size in bits | ||
/// </summary> | ||
public const int DEFAULT_KEY_SIZE = 701; | ||
/// <summary> | ||
/// Algorithm usages | ||
/// </summary> | ||
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange; | ||
/// <summary> | ||
/// Display name | ||
/// </summary> | ||
public const string DISPLAY_NAME = "NTRUEncrypt"; | ||
|
||
/// <summary> | ||
/// Allowed key sizes in bits | ||
/// </summary> | ||
private static readonly ReadOnlyCollection<int> _AllowedKeySizes; | ||
|
||
/// <summary> | ||
/// Static constructor | ||
/// </summary> | ||
static AsymmetricNtruEncryptAlgorithm() => _AllowedKeySizes = new List<int>() | ||
{ | ||
509, | ||
677, | ||
701, | ||
821 | ||
}.AsReadOnly(); | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public AsymmetricNtruEncryptAlgorithm() | ||
: base(ALGORITHM_NAME, ALGORITHM_VALUE, USAGES, isEllipticCurveAlgorithm: false, _AllowedKeySizes, isPostQuantum: true, DEFAULT_KEY_SIZE) | ||
{ } | ||
|
||
/// <inheritdoc/> | ||
public override string DisplayName => DISPLAY_NAME; | ||
|
||
/// <inheritdoc/> | ||
protected override NtruParameters GetEngineParameters(CryptoOptions options) => AsymmetricNtruHelper.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,66 @@ | ||
using Org.BouncyCastle.Crypto; | ||
using Org.BouncyCastle.Pqc.Crypto.Ntru; | ||
using wan24.Core; | ||
|
||
namespace wan24.Crypto.BC | ||
{ | ||
/// <summary> | ||
/// NTRUEncrypt asymmetric private key | ||
/// </summary> | ||
public sealed record class AsymmetricNtruEncryptPrivateKey | ||
: BouncyCastleAsymmetricPrivateKeyExchangeKeyBase< | ||
AsymmetricNtruEncryptPublicKey, | ||
AsymmetricNtruEncryptAlgorithm, | ||
NtruPublicKeyParameters, | ||
NtruPrivateKeyParameters, | ||
NtruKemGenerator, | ||
NtruKemExtractor, | ||
AsymmetricNtruEncryptPrivateKey | ||
> | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public AsymmetricNtruEncryptPrivateKey() : base(AsymmetricNtruEncryptAlgorithm.ALGORITHM_NAME) { } | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="keyData">Key data</param> | ||
public AsymmetricNtruEncryptPrivateKey(byte[] keyData) : base(AsymmetricNtruEncryptAlgorithm.ALGORITHM_NAME, keyData) { } | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="keys">Keys</param> | ||
public AsymmetricNtruEncryptPrivateKey(AsymmetricCipherKeyPair keys) : base(AsymmetricNtruEncryptAlgorithm.ALGORITHM_NAME, keys) { } | ||
|
||
/// <inheritdoc/> | ||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
if (Keys == null) return; | ||
Keys.Private.ClearPrivateByteArrayFields();//TODO All parameter fields are private :( | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override async Task DisposeCore() | ||
{ | ||
await base.DisposeCore().DynamicContext(); | ||
if (Keys == null) return; | ||
Keys.Private.ClearPrivateByteArrayFields();//TODO All parameter fields are private :( | ||
} | ||
|
||
/// <summary> | ||
/// Cast to public key | ||
/// </summary> | ||
/// <param name="privateKey">Private key</param> | ||
public static implicit operator AsymmetricNtruEncryptPublicKey(AsymmetricNtruEncryptPrivateKey privateKey) => privateKey.PublicKey; | ||
|
||
/// <summary> | ||
/// Cast from serialized data | ||
/// </summary> | ||
/// <param name="data">Data</param> | ||
public static explicit operator AsymmetricNtruEncryptPrivateKey(byte[] data) => Import<AsymmetricNtruEncryptPrivateKey>(data); | ||
} | ||
} |
Oops, something went wrong.