summary refs log tree commit diff
path: root/Crypto/src/crypto/PbeParametersGenerator.cs
blob: 0e96abd0f7b8728ad5b54a1e95a9d1d822b0243d (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Text;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Crypto
{
	/**
	 * super class for all Password Based Encyrption (Pbe) parameter generator classes.
	 */
	public abstract class PbeParametersGenerator
	{
		protected byte[]	mPassword;
		protected byte[]	mSalt;
		protected int		mIterationCount;

		/**
		 * base constructor.
		 */
		protected PbeParametersGenerator()
		{
		}

		/**
		 * initialise the Pbe generator.
		 *
		 * @param password the password converted into bytes (see below).
		 * @param salt the salt to be mixed with the password.
		 * @param iterationCount the number of iterations the "mixing" function
		 * is to be applied for.
		 */
		public virtual void Init(
			byte[]  password,
			byte[]  salt,
			int     iterationCount)
		{
			if (password == null)
				throw new ArgumentNullException("password");
			if (salt == null)
				throw new ArgumentNullException("salt");

			this.mPassword = Arrays.Clone(password);
			this.mSalt = Arrays.Clone(salt);
			this.mIterationCount = iterationCount;
		}

		public virtual byte[] Password
		{
			get { return Arrays.Clone(mPassword); }
		}

		/**
		 * return the password byte array.
		 *
		 * @return the password byte array.
		 */
		[Obsolete("Use 'Password' property")]
		public byte[] GetPassword()
		{
			return Password;
		}

		public virtual byte[] Salt
		{
			get { return Arrays.Clone(mSalt); }
		}

		/**
		 * return the salt byte array.
		 *
		 * @return the salt byte array.
		 */
		[Obsolete("Use 'Salt' property")]
		public byte[] GetSalt()
		{
			return Salt;
		}

		/**
		 * return the iteration count.
		 *
		 * @return the iteration count.
		 */
		public virtual int IterationCount
		{
			get { return mIterationCount; }
		}

		/**
		 * Generate derived parameters for a key of length keySize.
		 *
		 * @param keySize the length, in bits, of the key required.
		 * @return a parameters object representing a key.
		 */
		[Obsolete("Use version with 'algorithm' parameter")]
		public abstract ICipherParameters GenerateDerivedParameters(int keySize);
		public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize);

		/**
		 * Generate derived parameters for a key of length keySize, and
		 * an initialisation vector (IV) of length ivSize.
		 *
		 * @param keySize the length, in bits, of the key required.
		 * @param ivSize the length, in bits, of the iv required.
		 * @return a parameters object representing a key and an IV.
		 */
		[Obsolete("Use version with 'algorithm' parameter")]
		public abstract ICipherParameters GenerateDerivedParameters(int keySize, int ivSize);
		public abstract ICipherParameters GenerateDerivedParameters(string algorithm, int keySize, int ivSize);

		/**
		 * Generate derived parameters for a key of length keySize, specifically
		 * for use with a MAC.
		 *
		 * @param keySize the length, in bits, of the key required.
		 * @return a parameters object representing a key.
		 */
		public abstract ICipherParameters GenerateDerivedMacParameters(int keySize);

		/**
		 * converts a password to a byte array according to the scheme in
		 * Pkcs5 (ascii, no padding)
		 *
		 * @param password a character array representing the password.
		 * @return a byte array representing the password.
		 */
		public static byte[] Pkcs5PasswordToBytes(
			char[] password)
		{
            return Strings.ToAsciiByteArray(password);
		}

		[Obsolete("Use version taking 'char[]' instead")]
		public static byte[] Pkcs5PasswordToBytes(
			string password)
		{
            return Strings.ToAsciiByteArray(password);
        }

		/**
		 * converts a password to a byte array according to the scheme in
		 * PKCS5 (UTF-8, no padding)
		 *
		 * @param password a character array representing the password.
		 * @return a byte array representing the password.
		 */
		public static byte[] Pkcs5PasswordToUtf8Bytes(
			char[] password)
		{
			return Encoding.UTF8.GetBytes(password);
		}

		[Obsolete("Use version taking 'char[]' instead")]
		public static byte[] Pkcs5PasswordToUtf8Bytes(
			string password)
		{
			return Encoding.UTF8.GetBytes(password);
		}

		/**
		 * converts a password to a byte array according to the scheme in
		 * Pkcs12 (unicode, big endian, 2 zero pad bytes at the end).
		 *
		 * @param password a character array representing the password.
		 * @return a byte array representing the password.
		 */
		public static byte[] Pkcs12PasswordToBytes(
			char[] password)
		{
			return Pkcs12PasswordToBytes(password, false);
		}

		public static byte[] Pkcs12PasswordToBytes(
			char[]	password,
			bool	wrongPkcs12Zero)
		{
			if (password.Length < 1)
			{
				return new byte[wrongPkcs12Zero ? 2 : 0];
			}

			// +1 for extra 2 pad bytes.
			byte[] bytes = new byte[(password.Length + 1) * 2];

			Encoding.BigEndianUnicode.GetBytes(password, 0, password.Length, bytes, 0);

			return bytes;
		}
	}
}