summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/hqc/HqcKeyPairGenerator.cs
blob: 50882f432c041f0e5d2976a0cf9f905119cd1f60 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Pqc.Crypto.Hqc

{
    public class HqcKeyPairGenerator : IAsymmetricCipherKeyPairGenerator
    {
        private int n;

        private int k;

        private int delta;

        private int w;

        private int wr;

        private int we;
        private int N_BYTE;
        private HqcKeyGenerationParameters hqcKeyGenerationParameters;

        private SecureRandom random;

        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            byte[] seed = new byte[48];
            random.NextBytes(seed);
            return GenKeyPair(seed);
        }

        public AsymmetricCipherKeyPair GenerateKeyPairWithSeed(byte[] seed)
        {
            return GenKeyPair(seed);
        }

        public void Init(KeyGenerationParameters parameters)
        {
            hqcKeyGenerationParameters = (HqcKeyGenerationParameters)parameters;
            random = parameters.Random;

            // get parameters
            n = hqcKeyGenerationParameters.Parameters.N;
            k = hqcKeyGenerationParameters.Parameters.K;
            delta = hqcKeyGenerationParameters.Parameters.Delta;
            w = hqcKeyGenerationParameters.Parameters.W;
            wr = hqcKeyGenerationParameters.Parameters.Wr;
            we = hqcKeyGenerationParameters.Parameters.We;
            N_BYTE = (n + 7) / 8;
        }
        private AsymmetricCipherKeyPair GenKeyPair(byte[] seed)
        {
            HqcEngine engine = hqcKeyGenerationParameters.Parameters.Engine;
            byte[] pk = new byte[40 + N_BYTE];
            byte[] sk = new byte[40 + 40 + N_BYTE];

            engine.GenKeyPair(pk, sk, seed);

            // form keys
            HqcPublicKeyParameters publicKey = new HqcPublicKeyParameters(hqcKeyGenerationParameters.Parameters, pk);
            HqcPrivateKeyParameters privateKey = new HqcPrivateKeyParameters(hqcKeyGenerationParameters.Parameters, sk);

            return new AsymmetricCipherKeyPair(publicKey, privateKey);
        }

       

    }
}