summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/sike/SIKEKEMGenerator.cs
blob: c9f68dcd089c22bf4260af0581bca4efc16839ac (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
40
41
using System;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pqc.Crypto.Utilities;
using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Pqc.Crypto.Sike
{
    [Obsolete("Will be removed")]
    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.GetEngine();

            return GenerateEncapsulated(recipientKey, (int)engine.GetDefaultSessionKeySize());
        }

        public ISecretWithEncapsulation GenerateEncapsulated(AsymmetricKeyParameter recipientKey,
            int sessionKeySizeInBits)
        {
            Console.Error.WriteLine("WARNING: the SIKE algorithm is only for research purposes, insecure");
            SikePublicKeyParameters key = (SikePublicKeyParameters)recipientKey;
            SikeEngine engine = key.Parameters.GetEngine();
            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);
        }
    }
}