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

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

namespace Org.BouncyCastle.Pqc.Crypto.Lms
{
    // TODO[api] Make internal
    public sealed class LMOtsPublicKey
    {
        private readonly LMOtsParameters m_parameters;
        private readonly byte[] m_I;
        private readonly int m_q;
        private readonly byte[] m_K;

        public LMOtsPublicKey(LMOtsParameters parameters, byte[] i, int q, byte[] k)
        {
            m_parameters = parameters;
            m_I = i;
            m_q = q;
            m_K = k;
        }

        public static LMOtsPublicKey GetInstance(object src)
        {
            if (src is LMOtsPublicKey lmOtsPublicKey)
                return lmOtsPublicKey;

            if (src is BinaryReader binaryReader)
                return Parse(binaryReader);

            if (src is Stream stream)
                return BinaryReaders.Parse(Parse, stream, leaveOpen: true);

            if (src is byte[] bytes)
                return BinaryReaders.Parse(Parse, new MemoryStream(bytes, false), leaveOpen: false);

            throw new ArgumentException($"cannot parse {src}");
        }

        internal static LMOtsPublicKey Parse(BinaryReader binaryReader)
        {
            int index = BinaryReaders.ReadInt32BigEndian(binaryReader);
            LMOtsParameters parameter = LMOtsParameters.GetParametersByID(index);

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

            int q = BinaryReaders.ReadInt32BigEndian(binaryReader);

            byte[] K = BinaryReaders.ReadBytesFully(binaryReader, parameter.N);

            return new LMOtsPublicKey(parameter, I, q, K);
        }

        public byte[] GetI() => Arrays.Clone(m_I);

        public byte[] GetK() => Arrays.Clone(m_K);

        public LMOtsParameters Parameters => m_parameters;

        public int Q => m_q;

        public override bool Equals(object obj)
        {
            if (this == obj)
                return true;

            return obj is LMOtsPublicKey that
                && m_q == that.m_q
                && Objects.Equals(m_parameters, that.m_parameters)
                && Arrays.AreEqual(m_I, that.m_I)
                && Arrays.AreEqual(m_K, that.m_K);
        }

        public override int GetHashCode()
        {
            int result = m_q;
            result = 31 * result + Objects.GetHashCode(m_parameters);
            result = 31 * result + Arrays.GetHashCode(m_I);
            result = 31 * result + Arrays.GetHashCode(m_K);
            return result;
        }

        public byte[] GetEncoded()
        {
            return Composer.Compose()
                .U32Str(m_parameters.ID)
                .Bytes(m_I)
                .U32Str(m_q)
                .Bytes(m_K)
                .Build();
        }

        internal LmsContext CreateOtsContext(LMOtsSignature signature)
        {
            IDigest ctx = LmsUtilities.GetDigest(m_parameters);

            LmsUtilities.ByteArray(m_I, ctx);
            LmsUtilities.U32Str(m_q, ctx);
            LmsUtilities.U16Str((short)LMOts.D_MESG, ctx);
#pragma warning disable CS0618 // Type or member is obsolete
            LmsUtilities.ByteArray(signature.C, ctx);
#pragma warning restore CS0618 // Type or member is obsolete

            return new LmsContext(this, signature, ctx);
        }

        internal LmsContext CreateOtsContext(LmsSignature signature)
        {
            IDigest ctx = LmsUtilities.GetDigest(m_parameters);

            LmsUtilities.ByteArray(m_I, ctx);
            LmsUtilities.U32Str(m_q, ctx);
            LmsUtilities.U16Str((short)LMOts.D_MESG, ctx);
#pragma warning disable CS0618 // Type or member is obsolete
            LmsUtilities.ByteArray(signature.OtsSignature.C, ctx);
#pragma warning restore CS0618 // Type or member is obsolete

            return new LmsContext(this, signature, ctx);
        }

        [Obsolete("Use 'GetI' instead")]
        public byte[] I => m_I;

        [Obsolete("Use 'GetK' instead")]
        public byte[] K => m_K;
    }
}