summary refs log tree commit diff
path: root/crypto/src/crypto/parameters/ParametersWithIV.cs
blob: c5f04aab1ff3e3f1fcad645f3ca8cdefdf7cb055 (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
using System;
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
using System.Buffers;
#endif

namespace Org.BouncyCastle.Crypto.Parameters
{
    public class ParametersWithIV
        : ICipherParameters
    {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public static ParametersWithIV Create<TState>(ICipherParameters parameter, int ivLength, TState state,
            SpanAction<byte, TState> action)
        {
            if (action == null)
                throw new ArgumentNullException(nameof(action));
            if (ivLength < 0)
                throw new ArgumentOutOfRangeException(nameof(ivLength));

            ParametersWithIV result = new ParametersWithIV(parameter, ivLength);
            action(result.m_iv, state);
            return result;
        }
#endif

        internal static ICipherParameters ApplyOptionalIV(ICipherParameters parameters, byte[] iv)
        {
            return iv == null ? parameters : new ParametersWithIV(parameters, iv);
        }

        private readonly ICipherParameters m_parameters;
        private readonly byte[] m_iv;

        public ParametersWithIV(ICipherParameters parameters, byte[] iv)
            : this(parameters, iv, 0, iv.Length)
        {
            // NOTE: 'parameters' may be null to imply key re-use
            if (iv == null)
                throw new ArgumentNullException(nameof(iv));

            m_parameters = parameters;
            m_iv = (byte[])iv.Clone();
        }

        public ParametersWithIV(ICipherParameters parameters, byte[] iv, int ivOff, int ivLen)
        {
            // NOTE: 'parameters' may be null to imply key re-use
            if (iv == null)
                throw new ArgumentNullException(nameof(iv));

            m_parameters = parameters;
            m_iv = new byte[ivLen];
            Array.Copy(iv, ivOff, m_iv, 0, ivLen);
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public ParametersWithIV(ICipherParameters parameters, ReadOnlySpan<byte> iv)
        {
            // NOTE: 'parameters' may be null to imply key re-use
            m_parameters = parameters;
            m_iv = iv.ToArray();
        }
#endif

        private ParametersWithIV(ICipherParameters parameters, int ivLength)
        {
            if (ivLength < 0)
                throw new ArgumentOutOfRangeException(nameof(ivLength));

            // NOTE: 'parameters' may be null to imply key re-use
            m_parameters = parameters;
            m_iv = new byte[ivLength];
        }

        public byte[] GetIV()
        {
            return (byte[])m_iv.Clone();
        }

        public int IVLength => m_iv.Length;

        public ICipherParameters Parameters => m_parameters;

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        internal ReadOnlySpan<byte> IV => m_iv;
#endif
    }
}