summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs
blob: 80cf158278214af893e5ec942955afe007a9bf86 (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
using System;
using System.IO;

using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Pqc.Crypto.Lms
{
    public class LMSPublicKeyParameters
        : LMSKeyParameters, ILMSContextBasedVerifier
    {
        private LMSigParameters parameterSet;
        private LMOtsParameters lmOtsType;
        private byte[] I;
        private byte[] T1;

        public LMSPublicKeyParameters(LMSigParameters parameterSet, LMOtsParameters lmOtsType, byte[] T1, byte[] I)
            : base(false)
        {
            this.parameterSet = parameterSet;
            this.lmOtsType = lmOtsType;
            this.I = Arrays.Clone(I);
            this.T1 = Arrays.Clone(T1);
        }

        public static LMSPublicKeyParameters GetInstance(object src)
        {
            if (src is LMSPublicKeyParameters lmsPublicKeyParameters)
            {
                return lmsPublicKeyParameters;
            }
            else if (src is BinaryReader binaryReader)
            {
                int pubType = BinaryReaders.ReadInt32BigEndian(binaryReader);
                LMSigParameters lmsParameter = LMSigParameters.GetParametersByID(pubType);

                int index = BinaryReaders.ReadInt32BigEndian(binaryReader);
                LMOtsParameters ostTypeCode = LMOtsParameters.GetParametersByID(index);

                byte[] I = BinaryReaders.ReadBytesFully(binaryReader, 16);

                byte[] T1 = BinaryReaders.ReadBytesFully(binaryReader, lmsParameter.M);

                return new LMSPublicKeyParameters(lmsParameter, ostTypeCode, T1, I);
            }
            else if (src is byte[] bytes)
             {
                 BinaryReader input = null;
                 try // 1.5 / 1.6 compatibility
                 {
                     input = new BinaryReader(new MemoryStream(bytes, false));
                     return GetInstance(input);
                 }
                 finally
                 {
                     if (input != null)
                     {
                         input.Close();
                     }
                 }
             }
            else if (src is MemoryStream memoryStream)
            {
                return GetInstance(Streams.ReadAll(memoryStream));
            }
            throw new Exception ($"cannot parse {src}");
        }

        public override byte[] GetEncoded()
        {
            return this.ToByteArray();
        }

        public LMSigParameters GetSigParameters()
        {
            return parameterSet;
        }

        public LMOtsParameters GetOtsParameters()
        {
            return lmOtsType;
        }

        public LMSParameters GetLmsParameters()
        {
            return new LMSParameters(this.GetSigParameters(), this.GetOtsParameters());
        }

        public byte[] GetT1()
        {
            return Arrays.Clone(T1);
        }

        internal bool MatchesT1(byte[] sig)
        {
            return Arrays.ConstantTimeAreEqual(T1, sig);
        }

        public byte[] GetI()
        {
            return Arrays.Clone(I);
        }

        byte[] RefI()
        {
            return I;
        }

        public override bool Equals(Object o)
        {
            if (this == o)
            {
                return true;
            }
            if (o == null || GetType() != o.GetType())
            {
                return false;
            }

            LMSPublicKeyParameters publicKey = (LMSPublicKeyParameters)o;

            if (!parameterSet.Equals(publicKey.parameterSet))
            {
                return false;
            }
            if (!lmOtsType.Equals(publicKey.lmOtsType))
            {
                return false;
            }
            if (!Arrays.AreEqual(I, publicKey.I))
            {
                return false;
            }
            return Arrays.AreEqual(T1, publicKey.T1);
        }

        public override int GetHashCode()
        {
            int result = parameterSet.GetHashCode();
            result = 31 * result + lmOtsType.GetHashCode();
            result = 31 * result + Arrays.GetHashCode(I);
            result = 31 * result + Arrays.GetHashCode(T1);
            return result;
        }

        internal byte[] ToByteArray()
        {
            return Composer.Compose()
                .U32Str(parameterSet.ID)
                .U32Str(lmOtsType.ID)
                .Bytes(I)
                .Bytes(T1)
                .Build();
        }

        public LMSContext GenerateLmsContext(byte[] signature)
        {
            try
            {
                return GenerateOtsContext(LMSSignature.GetInstance(signature));
            }
            catch (IOException e)
            {
                throw new IOException($"cannot parse signature: {e.Message}");
            }
        }

        internal LMSContext GenerateOtsContext(LMSSignature S)
        {
            int ots_typecode = GetOtsParameters().ID;
            if (S.OtsSignature.ParamType.ID != ots_typecode)
            {
                throw new ArgumentException("ots type from lsm signature does not match ots" +
                    " signature type from embedded ots signature");
            }

            return new LMOtsPublicKey(LMOtsParameters.GetParametersByID(ots_typecode), I,  S.Q, null)
                .CreateOtsContext(S);
        }

        public bool Verify(LMSContext context)
        {
            return LMS.VerifySignature(this, context);
        }
    }
}