/
Aes128CtrStream.cs
166 lines (142 loc) · 5.77 KB
/
Aes128CtrStream.cs
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
using System;
using System.IO;
using LibHac.Streams;
namespace LibHac
{
public class Aes128CtrStream : SectorStream
{
private const int CryptChunkSize = 0x4000;
private const int BlockSize = 0x10;
private readonly long _counterOffset;
private readonly byte[] _tempBuffer;
private readonly Aes128CtrTransform _decryptor;
protected readonly byte[] Counter;
/// <summary>
/// Creates a new stream
/// </summary>
/// <param name="baseStream">The base stream</param>
/// <param name="key">The decryption key</param>
/// <param name="counter">The initial counter</param>
public Aes128CtrStream(Stream baseStream, byte[] key, byte[] counter)
: this(baseStream, key, 0, baseStream.Length, counter) { }
/// <summary>
/// Creates a new stream
/// </summary>
/// <param name="baseStream">The base stream</param>
/// <param name="key">The decryption key</param>
/// <param name="counterOffset">Offset to add to the counter</param>
/// <param name="ctrHi">The value of the upper 64 bits of the counter</param>
public Aes128CtrStream(Stream baseStream, byte[] key, long counterOffset = 0, byte[] ctrHi = null)
: this(baseStream, key, 0, baseStream.Length, counterOffset, ctrHi) { }
/// <summary>
/// Creates a new stream
/// </summary>
/// <param name="baseStream">The base stream</param>
/// <param name="key">The decryption key</param>
/// <param name="offset">Offset to start at in the input stream</param>
/// <param name="length">The length of the created stream</param>
/// <param name="counter">The initial counter</param>
public Aes128CtrStream(Stream baseStream, byte[] key, long offset, long length, byte[] counter)
: base(baseStream, BlockSize, 1, offset)
{
_counterOffset = 0;
// Make the stream seekable by remembering the initial counter value
if (counter != null)
{
for (int i = 0; i < 8; i++)
{
_counterOffset |= (long)counter[0xF - i] << (4 + i * 8);
}
}
Length = length;
_tempBuffer = new byte[CryptChunkSize];
_decryptor = new Aes128CtrTransform(key, counter ?? new byte[0x10], CryptChunkSize);
Counter = _decryptor.Counter;
}
/// <summary>
/// Creates a new stream
/// </summary>
/// <param name="baseStream">The base stream</param>
/// <param name="key">The decryption key</param>
/// <param name="offset">Offset to start at in the input stream</param>
/// <param name="length">The length of the created stream</param>
/// <param name="counterOffset">Offset to add to the counter</param>
/// <param name="ctrHi">The value of the upper 64 bits of the counter</param>
public Aes128CtrStream(Stream baseStream, byte[] key, long offset, long length, long counterOffset, byte[] ctrHi = null)
: base(baseStream, BlockSize, CryptChunkSize / BlockSize, offset)
{
var initialCounter = new byte[BlockSize];
if (ctrHi != null)
{
Array.Copy(ctrHi, initialCounter, 8);
}
_counterOffset = counterOffset;
Length = length;
_tempBuffer = new byte[CryptChunkSize];
_decryptor = new Aes128CtrTransform(key, initialCounter, CryptChunkSize);
Counter = _decryptor.Counter;
UpdateCounter(_counterOffset + base.Position);
baseStream.Position = offset;
}
private void UpdateCounter(long offset)
{
ulong off = (ulong)offset >> 4;
for (uint j = 0; j < 0x7; j++)
{
Counter[0x10 - j - 1] = (byte)(off & 0xFF);
off >>= 8;
}
// Because the value stored in the counter is offset >> 4, the top 4 bits
// of byte 8 need to have their original value preserved
Counter[8] = (byte)((Counter[8] & 0xF0) | (int)(off & 0x0F));
}
public override void Flush()
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
Position = offset;
break;
case SeekOrigin.Current:
Position += offset;
break;
case SeekOrigin.End:
Position = Length - offset;
break;
}
return Position;
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length { get; }
public override long Position
{
get => base.Position;
set
{
base.Position = value;
UpdateCounter(_counterOffset + base.Position);
}
}
public override int Read(byte[] buffer, int offset, int count)
{
ValidateSize(count);
int bytesRead = base.Read(_tempBuffer, 0, count);
if (bytesRead == 0) return 0;
return _decryptor.TransformBlock(_tempBuffer, 0, bytesRead, buffer, offset);
}
}
}