summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/saber/SABERKeyPairGenerator.cs
blob: 1b74a28333fe57b33974f0430dfdfb7d7444d6f1 (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
42
43
44
45
46
47
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Pqc.Crypto.Saber
{
    public class SABERKeyPairGenerator
        : IAsymmetricCipherKeyPairGenerator
    {
        private SABERKeyGenerationParameters saberParams;

        private int l;

        private SecureRandom random;

        private void Initialize(
            KeyGenerationParameters param)
        {
            this.saberParams = (SABERKeyGenerationParameters) param;
            this.random = param.Random;

            this.l = this.saberParams.GetParameters().GetL();
        }

        private AsymmetricCipherKeyPair GenKeyPair()
        {
            SABEREngine engine = saberParams.GetParameters().GetEngine();
            byte[] sk = new byte[engine.GetPrivateKeySize()];
            byte[] pk = new byte[engine.GetPublicKeySize()];
            engine.crypto_kem_keypair(pk, sk, random);

            SABERPublicKeyParameters pubKey = new SABERPublicKeyParameters(saberParams.GetParameters(), pk);
            SABERPrivateKeyParameters privKey = new SABERPrivateKeyParameters(saberParams.GetParameters(), sk);
            return new AsymmetricCipherKeyPair(pubKey, privKey);
        }

        public void Init(KeyGenerationParameters param)
        {
            this.Initialize(param);
        }

        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            return GenKeyPair();
        }
    }
}