summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/lms/HSS.cs
blob: 317ee89f5e6dbcc5f3c8fbbab1ff3e52130ead17 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;

namespace Org.BouncyCastle.Pqc.Crypto.Lms
{
    public static class Hss
    {
        public static HssPrivateKeyParameters GenerateHssKeyPair(HssKeyGenerationParameters parameters)
        {
            //
            // LmsPrivateKey can derive and hold the public key so we just use an array of those.
            //
            LmsPrivateKeyParameters[] keys = new LmsPrivateKeyParameters[parameters.Depth];
            LmsSignature[] sig = new LmsSignature[parameters.Depth - 1];

            byte[] rootSeed = new byte[32];
            parameters.Random.NextBytes(rootSeed);

            byte[] I = new byte[16];
            parameters.Random.NextBytes(I);

            //
            // Set the HSS key up with a valid root LMSPrivateKeyParameters and placeholders for the remaining LMS keys.
            // The placeholders pass enough information to allow the HSSPrivateKeyParameters to be properly reset to an
            // index of zero. Rather than repeat the same reset-to-index logic in this static method.
            //

            byte[] zero = new byte[0];

            long hssKeyMaxIndex = 1;
            for (int t = 0; t < keys.Length; t++)
            {
                var lms = parameters.GetLmsParameters(t);
                if (t == 0)
                {
                    keys[t] = new LmsPrivateKeyParameters(
                        lms.LMSigParameters,
                        lms.LMOtsParameters,
                        0,
                        I,
                        1 << lms.LMSigParameters.H,
                        rootSeed,
                        isPlaceholder: false);
                }
                else
                {
                    keys[t] = new LmsPrivateKeyParameters(
                        lms.LMSigParameters,
                        lms.LMOtsParameters,
                        -1,
                        zero,
                        1 << lms.LMSigParameters.H,
                        zero,
                        isPlaceholder: true);
                }
                hssKeyMaxIndex <<= lms.LMSigParameters.H;
            }

            // if this has happened we're trying to generate a really large key
            // we'll use MAX_VALUE so that it's at least usable until someone upgrades the structure.
            if (hssKeyMaxIndex == 0)
            {
                hssKeyMaxIndex = long.MaxValue;
            }

            return new HssPrivateKeyParameters(
                parameters.Depth,
                new List<LmsPrivateKeyParameters>(keys),
                new List<LmsSignature>(sig),
                0, hssKeyMaxIndex);
        }

        /**
         * Increments an HSS private key without doing any work on it.
         * HSS private keys are automatically incremented when when used to create signatures.
         * <p/>
         * The HSS private key is ranged tested before this incrementation is applied.
         * LMS keys will be replaced as required.
         *
         * @param keyPair
         */
        public static void IncrementIndex(HssPrivateKeyParameters keyPair)
        {
            lock (keyPair)
            {
                RangeTestKeys(keyPair);
                keyPair.IncIndex();
                keyPair.GetKeys()[keyPair.L - 1].IncIndex();
            }
        }

        public static void RangeTestKeys(HssPrivateKeyParameters keyPair)
        {
            lock (keyPair)
            {
                if (keyPair.GetIndex() >= keyPair.IndexLimit)
                {
                    throw new Exception(
                        "hss private key" +
                            ((keyPair.IsShard()) ? " shard" : "") +
                            " is exhausted");
                }

                int L = keyPair.L;
                int d = L;
                var prv = keyPair.GetKeys();
                while (prv[d - 1].GetIndex() == 1 << prv[d - 1].GetSigParameters().H)
                {
                    if (--d == 0)
                        throw new Exception("hss private key" + (keyPair.IsShard() ? " shard" : "") +
                            " is exhausted the maximum limit for this HSS private key");
                }

                while (d < L)
                {
                    keyPair.ReplaceConsumedKey(d++);
                }
            }
        }


        public static HssSignature GenerateSignature(HssPrivateKeyParameters keyPair, byte[] message)
        {
            LmsSignedPubKey[] signed_pub_key;
            LmsPrivateKeyParameters nextKey;
            int L = keyPair.L;

            lock (keyPair)
            {
                RangeTestKeys(keyPair);
                
                var keys = keyPair.GetKeys();
                var sig = keyPair.GetSig();

                nextKey = keyPair.GetKeys()[L - 1];

                // Step 2. Stand in for sig[L-1]
                int i = 0;
                signed_pub_key = new LmsSignedPubKey[L - 1];
                while (i < L - 1)
                {
                    signed_pub_key[i] = new LmsSignedPubKey(sig[i], keys[i + 1].GetPublicKey());
                    ++i;
                }

                //
                // increment the index.
                //
                keyPair.IncIndex();
            }

            LmsContext context = nextKey.GenerateLmsContext().WithSignedPublicKeys(signed_pub_key);

            context.BlockUpdate(message, 0, message.Length);

            return GenerateSignature(L, context);
        }

        public static HssSignature GenerateSignature(int L, LmsContext context)
        {
            return new HssSignature(L - 1, context.SignedPubKeys, Lms.GenerateSign(context));
        }

        public static bool VerifySignature(HssPublicKeyParameters publicKey, HssSignature signature, byte[] message)
        {
            int Nspk = signature.GetlMinus1();
            if (Nspk + 1 != publicKey.L)
                return false;

            LmsSignature[] sigList = new LmsSignature[Nspk + 1];
            LmsPublicKeyParameters[] pubList = new LmsPublicKeyParameters[Nspk];

            for (int i = 0; i < Nspk; i++)
            {
                sigList[i] = signature.GetSignedPubKeys()[i].GetSignature();
                pubList[i] = signature.GetSignedPubKeys()[i].GetPublicKey();
            }
            sigList[Nspk] = signature.Signature;

            LmsPublicKeyParameters key = publicKey.LmsPublicKey;

            for (int i = 0; i < Nspk; i++)
            {
                LmsSignature sig = sigList[i];
                byte[] msg = pubList[i].ToByteArray();
                if (!Lms.VerifySignature(key, sig, msg))
                {
                    return false;
                }
                try
                {
                    key = pubList[i];
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex);
                }
            }
            return Lms.VerifySignature(key, sigList[Nspk], message);
        }
    }
}