File: Salsa20Cipher.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 (160 lines) | stat: -rw-r--r-- 5,478 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
/*
  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
*/

// Implementation of the Salsa20 cipher, based on the eSTREAM
// submission by D. J. Bernstein.

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

using KeePassLib.Utility;

namespace KeePassLib.Cryptography.Cipher
{
	public sealed class Salsa20Cipher : CtrBlockCipher
	{
		private readonly uint[] m_s = new uint[16]; // State
		private readonly uint[] m_x = new uint[16]; // Working buffer

		public override int BlockSize
		{
			get { return 64; }
		}

		public Salsa20Cipher(byte[] pbKey32, byte[] pbIV8) : base()
		{
			if(pbKey32 == null) throw new ArgumentNullException("pbKey32");
			if(pbKey32.Length != 32) throw new ArgumentOutOfRangeException("pbKey32");
			if(pbIV8 == null) throw new ArgumentNullException("pbIV8");
			if(pbIV8.Length != 8) throw new ArgumentOutOfRangeException("pbIV8");

			m_s[0] = 0x61707865;
			m_s[5] = 0x3320646E;
			m_s[10] = 0x79622D32;
			m_s[15] = 0x6B206574;

			m_s[1] = MemUtil.BytesToUInt32(pbKey32, 0);
			m_s[2] = MemUtil.BytesToUInt32(pbKey32, 4);
			m_s[3] = MemUtil.BytesToUInt32(pbKey32, 8);
			m_s[4] = MemUtil.BytesToUInt32(pbKey32, 12);
			m_s[11] = MemUtil.BytesToUInt32(pbKey32, 16);
			m_s[12] = MemUtil.BytesToUInt32(pbKey32, 20);
			m_s[13] = MemUtil.BytesToUInt32(pbKey32, 24);
			m_s[14] = MemUtil.BytesToUInt32(pbKey32, 28);

			m_s[6] = MemUtil.BytesToUInt32(pbIV8, 0);
			m_s[7] = MemUtil.BytesToUInt32(pbIV8, 4);
			m_s[8] = 0; // Counter, low
			m_s[9] = 0; // Counter, high
		}

		protected override void Dispose(bool bDisposing)
		{
			if(bDisposing)
			{
				MemUtil.ZeroArray<uint>(m_s);
				MemUtil.ZeroArray<uint>(m_x);
			}

			base.Dispose(bDisposing);
		}

		protected override void NextBlock(byte[] pbBlock)
		{
			if(pbBlock == null) throw new ArgumentNullException("pbBlock");
			if(pbBlock.Length != 64) throw new ArgumentOutOfRangeException("pbBlock");

			// x is a local alias for the working buffer; with this,
			// the compiler/runtime might remove some checks
			uint[] x = m_x;
			if(x == null) throw new InvalidOperationException();
			if(x.Length < 16) throw new InvalidOperationException();

			uint[] s = m_s;
			if(s == null) throw new InvalidOperationException();
			if(s.Length < 16) throw new InvalidOperationException();

			Array.Copy(s, x, 16);

			unchecked
			{
				// 10 * 8 quarter rounds = 20 rounds
				for(int i = 0; i < 10; ++i)
				{
					x[ 4] ^= MemUtil.RotateLeft32(x[ 0] + x[12],  7);
					x[ 8] ^= MemUtil.RotateLeft32(x[ 4] + x[ 0],  9);
					x[12] ^= MemUtil.RotateLeft32(x[ 8] + x[ 4], 13);
					x[ 0] ^= MemUtil.RotateLeft32(x[12] + x[ 8], 18);

					x[ 9] ^= MemUtil.RotateLeft32(x[ 5] + x[ 1],  7);
					x[13] ^= MemUtil.RotateLeft32(x[ 9] + x[ 5],  9);
					x[ 1] ^= MemUtil.RotateLeft32(x[13] + x[ 9], 13);
					x[ 5] ^= MemUtil.RotateLeft32(x[ 1] + x[13], 18);

					x[14] ^= MemUtil.RotateLeft32(x[10] + x[ 6],  7);
					x[ 2] ^= MemUtil.RotateLeft32(x[14] + x[10],  9);
					x[ 6] ^= MemUtil.RotateLeft32(x[ 2] + x[14], 13);
					x[10] ^= MemUtil.RotateLeft32(x[ 6] + x[ 2], 18);

					x[ 3] ^= MemUtil.RotateLeft32(x[15] + x[11],  7);
					x[ 7] ^= MemUtil.RotateLeft32(x[ 3] + x[15],  9);
					x[11] ^= MemUtil.RotateLeft32(x[ 7] + x[ 3], 13);
					x[15] ^= MemUtil.RotateLeft32(x[11] + x[ 7], 18);

					x[ 1] ^= MemUtil.RotateLeft32(x[ 0] + x[ 3],  7);
					x[ 2] ^= MemUtil.RotateLeft32(x[ 1] + x[ 0],  9);
					x[ 3] ^= MemUtil.RotateLeft32(x[ 2] + x[ 1], 13);
					x[ 0] ^= MemUtil.RotateLeft32(x[ 3] + x[ 2], 18);

					x[ 6] ^= MemUtil.RotateLeft32(x[ 5] + x[ 4],  7);
					x[ 7] ^= MemUtil.RotateLeft32(x[ 6] + x[ 5],  9);
					x[ 4] ^= MemUtil.RotateLeft32(x[ 7] + x[ 6], 13);
					x[ 5] ^= MemUtil.RotateLeft32(x[ 4] + x[ 7], 18);

					x[11] ^= MemUtil.RotateLeft32(x[10] + x[ 9],  7);
					x[ 8] ^= MemUtil.RotateLeft32(x[11] + x[10],  9);
					x[ 9] ^= MemUtil.RotateLeft32(x[ 8] + x[11], 13);
					x[10] ^= MemUtil.RotateLeft32(x[ 9] + x[ 8], 18);

					x[12] ^= MemUtil.RotateLeft32(x[15] + x[14],  7);
					x[13] ^= MemUtil.RotateLeft32(x[12] + x[15],  9);
					x[14] ^= MemUtil.RotateLeft32(x[13] + x[12], 13);
					x[15] ^= MemUtil.RotateLeft32(x[14] + x[13], 18);
				}

				for(int i = 0; i < 16; ++i) x[i] += s[i];

				for(int i = 0; i < 16; ++i)
				{
					int i4 = i << 2;
					uint xi = x[i];

					pbBlock[i4] = (byte)xi;
					pbBlock[i4 + 1] = (byte)(xi >> 8);
					pbBlock[i4 + 2] = (byte)(xi >> 16);
					pbBlock[i4 + 3] = (byte)(xi >> 24);
				}

				++s[8];
				if(s[8] == 0) ++s[9];
			}
		}
	}
}