summary refs log tree commit diff
path: root/crypto/src/asn1/misc/IDEACBCPar.cs
blob: 21eabea527b42818a6d0ebef9c4c1c831de5c6e3 (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
using System;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.Misc
{
    public class IdeaCbcPar
        : Asn1Encodable
    {
		public static IdeaCbcPar GetInstance(object o)
        {
            if (o == null)
                return null;
            if (o is IdeaCbcPar ideaCbcPar)
                return ideaCbcPar;
            return new IdeaCbcPar(Asn1Sequence.GetInstance(o));
        }

        public static IdeaCbcPar GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return new IdeaCbcPar(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

        private readonly Asn1OctetString m_iv;

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

            m_iv = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional);

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

        public IdeaCbcPar()
            : this(iv: null)
        {
        }

        public IdeaCbcPar(byte[] iv)
        {
            m_iv = iv == null ? null : new DerOctetString(iv);
        }

        public Asn1OctetString IV => m_iv;

        public byte[] GetIV() => Arrays.Clone(m_iv?.GetOctets());

		/**
         * Produce an object suitable for an Asn1OutputStream.
         * <pre>
         * IDEA-CBCPar ::= Sequence {
         *                      iv    OCTET STRING OPTIONAL -- exactly 8 octets
         *                  }
         * </pre>
         */
        public override Asn1Object ToAsn1Object()
        {
            return m_iv == null
                ?  DerSequence.Empty
                :  new DerSequence(m_iv);
        }
    }
}