File: Blake2b.cs

package info (click to toggle)
keepass2 2.57%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 14,520 kB
  • sloc: cs: 120,930; xml: 6,271; cpp: 322; sh: 53; makefile: 42
file content (232 lines) | stat: -rw-r--r-- 6,849 bytes parent folder | download | duplicates (3)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// This implementation is based on the official reference C
// implementation by Samuel Neves (CC0 1.0 Universal).

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

#if !KeePassUAP
using System.Security.Cryptography;
#endif

using KeePassLib.Utility;

namespace KeePassLib.Cryptography.Hash
{
	public sealed class Blake2b : HashAlgorithm
	{
		private const int NbRounds = 12;
		private const int NbBlockBytes = 128;
		private const int NbMaxOutBytes = 64;

		private static readonly ulong[] g_vIV = new ulong[8] {
			0x6A09E667F3BCC908UL, 0xBB67AE8584CAA73BUL,
			0x3C6EF372FE94F82BUL, 0xA54FF53A5F1D36F1UL,
			0x510E527FADE682D1UL, 0x9B05688C2B3E6C1FUL,
			0x1F83D9ABFB41BD6BUL, 0x5BE0CD19137E2179UL
		};

		private static readonly int[] g_vSigma = new int[NbRounds * 16] {
			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
			14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
			11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
			7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
			9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
			2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
			12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
			13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
			6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
			10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0,
			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
			14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3
		};

		private readonly int m_cbHashLength;

		private readonly ulong[] m_h = new ulong[8];
		private readonly ulong[] m_t = new ulong[2];
		private readonly ulong[] m_f = new ulong[2];
		private readonly byte[] m_buf = new byte[NbBlockBytes];
		private int m_cbBuf = 0;

		private readonly ulong[] m_m = new ulong[16];
		private readonly ulong[] m_v = new ulong[16];

		public Blake2b()
		{
			m_cbHashLength = NbMaxOutBytes;
			this.HashSizeValue = NbMaxOutBytes * 8; // Bits

			Initialize();
		}

		public Blake2b(int cbHashLength)
		{
			if((cbHashLength < 0) || (cbHashLength > NbMaxOutBytes))
				throw new ArgumentOutOfRangeException("cbHashLength");

			m_cbHashLength = cbHashLength;
			this.HashSizeValue = cbHashLength * 8; // Bits

			Initialize();
		}

		public override void Initialize()
		{
			Debug.Assert(m_h.Length == g_vIV.Length);
			Array.Copy(g_vIV, m_h, m_h.Length);

			// Fan-out = 1, depth = 1
			m_h[0] ^= 0x0000000001010000UL ^ (ulong)m_cbHashLength;

			Array.Clear(m_t, 0, m_t.Length);
			Array.Clear(m_f, 0, m_f.Length);
			Array.Clear(m_buf, 0, m_buf.Length);
			m_cbBuf = 0;

			Array.Clear(m_m, 0, m_m.Length);
			Array.Clear(m_v, 0, m_v.Length);
		}

		private static void G(ulong[] v, ulong[] m, int r16, int i,
			int a, int b, int c, int d)
		{
			int p = r16 + i;

			v[a] += v[b] + m[g_vSigma[p]];
			v[d] = MemUtil.RotateRight64(v[d] ^ v[a], 32);
			v[c] += v[d];
			v[b] = MemUtil.RotateRight64(v[b] ^ v[c], 24);
			v[a] += v[b] + m[g_vSigma[p + 1]];
			v[d] = MemUtil.RotateRight64(v[d] ^ v[a], 16);
			v[c] += v[d];
			v[b] = MemUtil.RotateRight64(v[b] ^ v[c], 63);
		}

		private void Compress(byte[] pb, int iOffset)
		{
			ulong[] v = m_v;
			ulong[] m = m_m;
			ulong[] h = m_h;

			for(int i = 0; i < 16; ++i)
				m[i] = MemUtil.BytesToUInt64(pb, iOffset + (i << 3));

			Array.Copy(h, v, 8);
			v[8] = g_vIV[0];
			v[9] = g_vIV[1];
			v[10] = g_vIV[2];
			v[11] = g_vIV[3];
			v[12] = g_vIV[4] ^ m_t[0];
			v[13] = g_vIV[5] ^ m_t[1];
			v[14] = g_vIV[6] ^ m_f[0];
			v[15] = g_vIV[7] ^ m_f[1];

			for(int r = 0; r < NbRounds; ++r)
			{
				int r16 = r << 4;

				G(v, m, r16, 0, 0, 4, 8, 12);
				G(v, m, r16, 2, 1, 5, 9, 13);
				G(v, m, r16, 4, 2, 6, 10, 14);
				G(v, m, r16, 6, 3, 7, 11, 15);
				G(v, m, r16, 8, 0, 5, 10, 15);
				G(v, m, r16, 10, 1, 6, 11, 12);
				G(v, m, r16, 12, 2, 7, 8, 13);
				G(v, m, r16, 14, 3, 4, 9, 14);
			}

			for(int i = 0; i < 8; ++i)
				h[i] ^= v[i] ^ v[i + 8];
		}

		private void IncrementCounter(ulong cb)
		{
			m_t[0] += cb;
			if(m_t[0] < cb) ++m_t[1];
		}

		protected override void HashCore(byte[] array, int ibStart, int cbSize)
		{
			Debug.Assert(m_f[0] == 0);

			if((m_cbBuf + cbSize) > NbBlockBytes) // Not '>=' (buffer must not be empty)
			{
				int cbFill = NbBlockBytes - m_cbBuf;
				if(cbFill > 0) Array.Copy(array, ibStart, m_buf, m_cbBuf, cbFill);

				IncrementCounter((ulong)NbBlockBytes);
				Compress(m_buf, 0);

				m_cbBuf = 0;
				cbSize -= cbFill;
				ibStart += cbFill;

				while(cbSize > NbBlockBytes) // Not '>=' (buffer must not be empty)
				{
					IncrementCounter((ulong)NbBlockBytes);
					Compress(array, ibStart);

					cbSize -= NbBlockBytes;
					ibStart += NbBlockBytes;
				}
			}

			if(cbSize > 0)
			{
				Debug.Assert((m_cbBuf + cbSize) <= NbBlockBytes);

				Array.Copy(array, ibStart, m_buf, m_cbBuf, cbSize);
				m_cbBuf += cbSize;
			}
		}

		protected override byte[] HashFinal()
		{
			if(m_f[0] != 0) { Debug.Assert(false); throw new InvalidOperationException(); }
			Debug.Assert(((m_t[1] == 0) && (m_t[0] == 0)) ||
				(m_cbBuf > 0)); // Buffer must not be empty for last block processing

			m_f[0] = ulong.MaxValue; // Indicate last block

			int cbFill = NbBlockBytes - m_cbBuf;
			if(cbFill > 0) Array.Clear(m_buf, m_cbBuf, cbFill);

			IncrementCounter((ulong)m_cbBuf);
			Compress(m_buf, 0);

			byte[] pbHash = new byte[NbMaxOutBytes];
			for(int i = 0; i < m_h.Length; ++i)
				MemUtil.UInt64ToBytesEx(m_h[i], pbHash, i << 3);

			if(m_cbHashLength == NbMaxOutBytes) return pbHash;
			Debug.Assert(m_cbHashLength < NbMaxOutBytes);

			byte[] pbShort = new byte[m_cbHashLength];
			if(m_cbHashLength > 0)
				Array.Copy(pbHash, pbShort, m_cbHashLength);
			MemUtil.ZeroByteArray(pbHash);
			return pbShort;
		}
	}
}