File: ChaCha20Engine.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 (190 lines) | stat: -rw-r--r-- 4,499 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
/*
  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
*/

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

using KeePassLib.Resources;

namespace KeePassLib.Cryptography.Cipher
{
	public sealed class ChaCha20Engine : ICipherEngine2
	{
		private static PwUuid g_uuid = null;
		internal static PwUuid ChaCha20Uuid
		{
			get
			{
				PwUuid pu = g_uuid;
				if(pu == null)
				{
					pu = new PwUuid(new byte[] {
						0xD6, 0x03, 0x8A, 0x2B, 0x8B, 0x6F, 0x4C, 0xB5,
						0xA5, 0x24, 0x33, 0x9A, 0x31, 0xDB, 0xB5, 0x9A });
					g_uuid = pu;
				}

				return pu;
			}
		}

		public PwUuid CipherUuid
		{
			get { return ChaCha20Engine.ChaCha20Uuid; }
		}

		public string DisplayName
		{
			get
			{
				return ("ChaCha20 (" + KLRes.KeyBits.Replace("{PARAM}",
					"256") + ", RFC 8439)");
			}
		}

		public int KeyLength
		{
			get { return 32; }
		}

		public int IVLength
		{
			get { return 12; } // 96 bits
		}

		public Stream EncryptStream(Stream s, byte[] pbKey, byte[] pbIV)
		{
			return new ChaCha20Stream(s, true, pbKey, pbIV);
		}

		public Stream DecryptStream(Stream s, byte[] pbKey, byte[] pbIV)
		{
			return new ChaCha20Stream(s, false, pbKey, pbIV);
		}
	}

	internal sealed class ChaCha20Stream : Stream
	{
		private Stream m_sBase;
		private readonly bool m_bWriting;
		private ChaCha20Cipher m_c;

		private byte[] m_pbBuffer = null;

		public override bool CanRead
		{
			get { return !m_bWriting; }
		}

		public override bool CanSeek
		{
			get { return false; }
		}

		public override bool CanWrite
		{
			get { return m_bWriting; }
		}

		public override long Length
		{
			get { Debug.Assert(false); throw new NotSupportedException(); }
		}

		public override long Position
		{
			get { Debug.Assert(false); throw new NotSupportedException(); }
			set { Debug.Assert(false); throw new NotSupportedException(); }
		}

		public ChaCha20Stream(Stream sBase, bool bWriting, byte[] pbKey32,
			byte[] pbIV12)
		{
			if(sBase == null) throw new ArgumentNullException("sBase");

			m_sBase = sBase;
			m_bWriting = bWriting;
			m_c = new ChaCha20Cipher(pbKey32, pbIV12);
		}

		protected override void Dispose(bool bDisposing)
		{
			if(bDisposing)
			{
				if(m_sBase != null)
				{
					m_c.Dispose();
					m_c = null;

					m_sBase.Close();
					m_sBase = null;
				}

				m_pbBuffer = null;
			}

			base.Dispose(bDisposing);
		}

		public override void Flush()
		{
			Debug.Assert(m_sBase != null);
			if(m_bWriting && (m_sBase != null)) m_sBase.Flush();
		}

		public override long Seek(long lOffset, SeekOrigin soOrigin)
		{
			Debug.Assert(false);
			throw new NotImplementedException();
		}

		public override void SetLength(long lValue)
		{
			Debug.Assert(false);
			throw new NotImplementedException();
		}

		public override int Read(byte[] pbBuffer, int iOffset, int nCount)
		{
			if(m_bWriting) throw new InvalidOperationException();

			int cbRead = m_sBase.Read(pbBuffer, iOffset, nCount);
			m_c.Decrypt(pbBuffer, iOffset, cbRead);
			return cbRead;
		}

		public override void Write(byte[] pbBuffer, int iOffset, int nCount)
		{
			if(nCount < 0) throw new ArgumentOutOfRangeException("nCount");
			if(nCount == 0) return;

			if(!m_bWriting) throw new InvalidOperationException();

			if((m_pbBuffer == null) || (m_pbBuffer.Length < nCount))
				m_pbBuffer = new byte[nCount];
			Array.Copy(pbBuffer, iOffset, m_pbBuffer, 0, nCount);

			m_c.Encrypt(m_pbBuffer, 0, nCount);
			m_sBase.Write(m_pbBuffer, 0, nCount);
		}
	}
}