blob: 76689496f159286bd7321d3858f876c00b765edb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pqc.Crypto.Utilities;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Pqc.Crypto.Sike
{
public sealed class SikeKemGenerator
: IEncapsulatedSecretGenerator
{
// the source of randomness
private readonly SecureRandom sr;
public SikeKemGenerator(SecureRandom random)
{
this.sr = CryptoServicesRegistrar.GetSecureRandom(random);
}
public ISecretWithEncapsulation GenerateEncapsulated(AsymmetricKeyParameter recipientKey)
{
SikePublicKeyParameters key = (SikePublicKeyParameters)recipientKey;
SikeEngine engine = key.Parameters.Engine;
return GenerateEncapsulated(recipientKey, engine.GetDefaultSessionKeySize());
}
public ISecretWithEncapsulation GenerateEncapsulated(AsymmetricKeyParameter recipientKey, uint sessionKeySizeInBits)
{
Console.Error.WriteLine("WARNING: the SIKE algorithm is only for research purposes, insecure");
SikePublicKeyParameters key = (SikePublicKeyParameters)recipientKey;
SikeEngine engine = key.Parameters.Engine;
byte[] cipher_text = new byte[engine.GetCipherTextSize()];
byte[] sessionKey = new byte[sessionKeySizeInBits / 8];
engine.crypto_kem_enc(cipher_text, sessionKey, key.GetPublicKey(), sr);
return new SecretWithEncapsulationImpl(sessionKey, cipher_text);
}
}
}
|