-
Notifications
You must be signed in to change notification settings - Fork 3
/
AsciiXorCipher.cs
171 lines (155 loc) · 4.76 KB
/
AsciiXorCipher.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
167
168
169
170
171
using System;
using System.Composition;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
namespace Science.Cryptography.Ciphers.Specialized;
/// <summary>
/// Represents the XOR cipher.
/// </summary>
[Export("ASCII-XOR", typeof(IKeyedCipher<>))]
public class AsciiXorCipher : ReciprocalKeyedCipher<byte[]>
{
protected override void Crypt(ReadOnlySpan<char> input, Span<char> output, byte[] key, out int written)
{
if (key.Length == 0)
{
throw new ArgumentException("Key can't be zero-length.", nameof(key));
}
if (output.Length < input.Length)
{
throw new ArgumentException("Size of output buffer is insufficient.", nameof(output));
}
// prepare key
Span<ushort> ushortKey = stackalloc ushort[key.Length];
for (int i = 0; i < key.Length; i++)
{
ushortKey[i] = key[i];
}
// crypt
var inputBytes = MemoryMarshal.Cast<char, ushort>(input);
var outputBytes = MemoryMarshal.Cast<char, ushort>(output);
Xor(inputBytes, outputBytes, ushortKey);
// decode
written = input.Length;
}
public static void Xor(ReadOnlySpan<ushort> input, Span<ushort> output, ReadOnlySpan<ushort> key)
{
if (output.Length < input.Length)
{
throw new ArgumentException("Size of output buffer is insufficient.", nameof(output));
}
if (Avx2.IsSupported)
{
if (input.Length < 256 / 8)
{
SlowXor(input, output, key);
}
else
{
Avx2Xor256(input, output, key);
}
}
else
{
SlowXor(input, output, key);
}
}
public static unsafe void Avx2Xor256(ReadOnlySpan<ushort> input, Span<ushort> output, ReadOnlySpan<ushort> key)
{
// initialize
var length = input.Length;
int keyLength = key.Length;
int vectorLength = Vector256<ushort>.Count, maxVectorLength = length / vectorLength * vectorLength;
// prefill buffer with running instances of key,
// so the right slice can be instantly selected
//
// same length:
// vector (4): 01 02 03 04
// key (4): 01 02 03 04
//
// key is longer:
// vector (4): 01 02 03 04
// key (6): 01 02 03 04 05 06 01 02 03
//
// key is shorter:
// vector (4): 01 02 03 04
// key (3): 01 02 03 01 02 03
bool isVectorLengthMultipleOfKeyLength = (vectorLength % keyLength) == 0;
bool isKeyLongerThanVector = vectorLength < keyLength;
Span<ushort> keyVectorUnits = stackalloc ushort[isVectorLengthMultipleOfKeyLength switch
{
true => vectorLength,
false => keyLength + vectorLength - 1,
}];
if (isVectorLengthMultipleOfKeyLength)
{
for (int keyCopyIndex = 0; keyCopyIndex < keyVectorUnits.Length; keyCopyIndex += keyLength)
{
key.CopyTo(keyVectorUnits[keyCopyIndex..]);
}
}
else if (isKeyLongerThanVector)
{
key.CopyTo(keyVectorUnits);
key[..(vectorLength - 1)].CopyTo(keyVectorUnits[keyLength..]);
}
else
{
var remaining = keyVectorUnits.Length % keyLength;
var lastPosition = keyVectorUnits.Length - remaining;
int keyCopyIndex;
for (keyCopyIndex = 0; keyCopyIndex < lastPosition; keyCopyIndex += keyLength)
{
key.CopyTo(keyVectorUnits[keyCopyIndex..]);
}
key[..remaining].CopyTo(keyVectorUnits[keyCopyIndex..]);
}
// chunks of vector size
Vector256<ushort> keyVector;
Unsafe.SkipInit(out keyVector);
for (int chunkStartIndex = 0; chunkStartIndex < maxVectorLength; chunkStartIndex += vectorLength)
{
// input span as vector
ref ushort pointer = ref MemoryMarshal.GetReference(input[chunkStartIndex..(chunkStartIndex + vectorLength)]);
var vector = Unsafe.As<ushort, Vector256<ushort>>(ref pointer);
// select the matching slice of the key ushorts buffer
if (isVectorLengthMultipleOfKeyLength)
{
if (chunkStartIndex == 0)
{
ref ushort keyPointer = ref MemoryMarshal.GetReference(keyVectorUnits);
keyVector = Unsafe.As<ushort, Vector256<ushort>>(ref keyPointer);
}
}
else
{
ref ushort keyPointer = ref MemoryMarshal.GetReference(keyVectorUnits[(chunkStartIndex % keyLength)..]);
keyVector = Unsafe.As<ushort, Vector256<ushort>>(ref keyPointer);
}
// execute xor
var result = Avx2.Xor(vector, keyVector);
// read from vector
fixed (ushort* outputPointer = &MemoryMarshal.GetReference(output[chunkStartIndex..]))
{
Avx.Store(outputPointer, result);
}
}
// process final block
if (length != maxVectorLength)
{
var remaining = length - maxVectorLength;
SlowXor(input[^remaining..], output[^remaining..], key, keyStart: maxVectorLength);
}
}
public static void SlowXor(ReadOnlySpan<ushort> input, Span<ushort> output, ReadOnlySpan<ushort> key, int keyStart = 0)
{
int inputLength = input.Length;
int keyLength = key.Length;
for (int i = 0; i < inputLength; i++)
{
output[i] = (ushort)(input[i] ^ key[(keyStart + i) % keyLength]);
}
}
}