summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/lms/LMOtsSignature.cs
blob: f5452f45a6d7f929d4f9ef96132038422df7242c (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
using System;
using System.IO;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Pqc.Crypto.Lms
{
    public class LMOtsSignature
        : IEncodable
    {
        private LMOtsParameters ParamType;
        private byte[] C;
        private byte[] y;

        public LMOtsSignature(LMOtsParameters ParamType, byte[] c, byte[] y)
        {
            this.ParamType = ParamType;
            C = c;
            this.y = y;
        }

        public static LMOtsSignature GetInstance(Object src)
        {
            if (src is LMOtsSignature)
            {
                return (LMOtsSignature)src;
            }
            //TODO replace inputstreams with something
            
            else if (src is BinaryReader)
            {
                byte[] data = ((BinaryReader) src).ReadBytes(4);
                Array.Reverse(data);
                int index = BitConverter.ToInt32(data, 0);
                LMOtsParameters type = LMOtsParameters.GetParametersForType(index);
                byte[] C = new byte[type.GetN()];
            
                ((BinaryReader)src).Read(C, 0, C.Length);
            
                byte[] sig = new byte[type.GetP()*type.GetN()];
                ((BinaryReader)src).Read(sig, 0, sig.Length);
            
            
                return new LMOtsSignature(type, C, sig);
            }
            else if (src is byte[])
            {
                BinaryReader input = null;
                try // 1.5 / 1.4 compatibility
                {
                    input = new BinaryReader(new MemoryStream((byte[])src, false));
                    return GetInstance(input);
                }
                finally
                {
                    if (input != null) input.Close();
                }
            }
            else if (src is MemoryStream)
            {
                return GetInstance(Streams.ReadAll((Stream)src));
            }
            throw new Exception ($"cannot parse {src}");
        }
        public LMOtsParameters GetParamType()
        {
            return ParamType;
        }

        public byte[] GetC()
        {
            return C;
        }

        public byte[] GetY()
        {
            return y;
        }
        
        public bool Equals(Object o)
        {
            if (this == o)
            {
                return true;
            }
            if (o == null || GetType() != o.GetType())
            {
                return false;
            }

            LMOtsSignature that = (LMOtsSignature)o;

            if (ParamType != null ? !ParamType.Equals(that.ParamType) : that.ParamType != null)
            {
                return false;
            }
            if (!Arrays.AreEqual(C, that.C))
            {
                return false;
            }
            return Arrays.AreEqual(y, that.y);
        }

        public override int GetHashCode()
        {
            int result = ParamType != null ? ParamType.GetHashCode() : 0;
            result = 31 * result + Arrays.GetHashCode(C);
            result = 31 * result + Arrays.GetHashCode(y);
            return result;
        }

        public byte[] GetEncoded()
        {
            return Composer.Compose()
                .U32Str(ParamType.GetType())
                .Bytes(C)
                .Bytes(y)
                .Build();
        }
    }
}