summary refs log tree commit diff
path: root/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs
blob: 3a03b9cb074b099b6be267a3e373f1ae7075a0b2 (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
using System;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.Nist
{
    /// <summary>
    /// KMACwithSHAKE128-params ::= SEQUENCE {
    ///     kMACOutputLength     INTEGER DEFAULT 256, -- Output length in bits
    ///     customizationString  OCTET STRING DEFAULT ''H
    /// } 
    /// </summary>
    public class KMacWithShake128Params
        : Asn1Encodable
    {
        private const int _DefaultOutputLength = 256;

        public static readonly DerInteger DefaultOutputLength = new DerInteger(_DefaultOutputLength);
        public static readonly Asn1OctetString DefaultCustomizationString = DerOctetString.Empty;

        public static KMacWithShake128Params GetInstance(object o)
        {
            if (o == null)
                return null;
            if (o is KMacWithShake128Params kMacWithShake128Params)
                return kMacWithShake128Params;
            return new KMacWithShake128Params(Asn1Sequence.GetInstance(o));
        }

        public static KMacWithShake128Params GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new KMacWithShake128Params(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));

        public static KMacWithShake128Params GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            new KMacWithShake128Params(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));

        private readonly DerInteger m_outputLength;
        private readonly Asn1OctetString m_customizationString;

        private KMacWithShake128Params(Asn1Sequence seq)
        {
            int count = seq.Count, pos = 0;
            if (count < 0 || count > 2)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

            m_outputLength = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional)
                ?? DefaultOutputLength;
            m_customizationString = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional)
                ?? DefaultCustomizationString;

            if (pos != count)
                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
        }

        public KMacWithShake128Params(int outputLength)
        {
            m_outputLength = new DerInteger(outputLength);
            m_customizationString = DefaultCustomizationString;
        }

        public KMacWithShake128Params(int outputLength, byte[] customizationString)
        {
            m_outputLength = new DerInteger(outputLength);
            m_customizationString = new DerOctetString(customizationString);
        }

        public int OutputLength => m_outputLength.IntValueExact;

        public byte[] CustomizationString => Arrays.Clone(m_customizationString.GetOctets());

        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(2);
            if (!m_outputLength.HasValue(_DefaultOutputLength))
            {
                v.Add(m_outputLength);
            }
            if (m_customizationString.GetOctetsLength() != 0)
            {
                v.Add(m_customizationString);
            }
            return new DerSequence(v);
        }
    }
}