Skip to content

Commit 3ac0fab

Browse files
committed
Revert refactor x86 aes
1 parent 3896364 commit 3ac0fab

File tree

6 files changed

+205
-298
lines changed

6 files changed

+205
-298
lines changed

src/CryptoBase/SymmetricCryptos/BlockCryptos/AES/AESCrypto.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ public abstract class AESCrypto : BlockCryptoBase
88

99
public sealed override int BlockSize => 16;
1010

11-
protected static ReadOnlySpan<byte> Rcon => [AESUtils.Rcon0, AESUtils.Rcon1, AESUtils.Rcon2, AESUtils.Rcon3, AESUtils.Rcon4, AESUtils.Rcon5, AESUtils.Rcon6, AESUtils.Rcon7, AESUtils.Rcon8, AESUtils.Rcon9, AESUtils.Rcon10];
11+
protected static ReadOnlySpan<byte> Rcon => [Rcon0, Rcon1, Rcon2, Rcon3, Rcon4, Rcon5, Rcon6, Rcon7, Rcon8, Rcon9, Rcon10];
12+
13+
protected const byte Rcon0 = 0x00;
14+
protected const byte Rcon1 = 0x01;
15+
protected const byte Rcon2 = 0x02;
16+
protected const byte Rcon3 = 0x04;
17+
protected const byte Rcon4 = 0x08;
18+
protected const byte Rcon5 = 0x10;
19+
protected const byte Rcon6 = 0x20;
20+
protected const byte Rcon7 = 0x40;
21+
protected const byte Rcon8 = 0x80;
22+
protected const byte Rcon9 = 0x1b;
23+
protected const byte Rcon10 = 0x36;
1224

1325
protected AESCrypto(ReadOnlySpan<byte> key)
1426
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace CryptoBase.SymmetricCryptos.BlockCryptos.AES;
2+
3+
public abstract class AESCryptoX86(ReadOnlySpan<byte> key) : AESCrypto(key);

src/CryptoBase/SymmetricCryptos/BlockCryptos/AES/AESUtils.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ namespace CryptoBase.SymmetricCryptos.BlockCryptos.AES;
77

88
public static class AESUtils
99
{
10-
public const byte Rcon0 = 0x00;
11-
public const byte Rcon1 = 0x01;
12-
public const byte Rcon2 = 0x02;
13-
public const byte Rcon3 = 0x04;
14-
public const byte Rcon4 = 0x08;
15-
public const byte Rcon5 = 0x10;
16-
public const byte Rcon6 = 0x20;
17-
public const byte Rcon7 = 0x40;
18-
public const byte Rcon8 = 0x80;
19-
public const byte Rcon9 = 0x1b;
20-
public const byte Rcon10 = 0x36;
21-
2210
public static readonly Aes AesEcb;
2311
public static readonly Aes AesCbc;
2412

@@ -34,7 +22,7 @@ static AESUtils()
3422
}
3523

3624
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37-
public static IBlockCrypto CreateECB(ReadOnlySpan<byte> key)
25+
public static AESCrypto CreateECB(ReadOnlySpan<byte> key)
3826
{
3927
if (System.Runtime.Intrinsics.X86.Aes.IsSupported && Sse2.IsSupported)
4028
{
Lines changed: 56 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
using CryptoBase.Abstractions.SymmetricCryptos;
2-
31
namespace CryptoBase.SymmetricCryptos.BlockCryptos.AES;
42

5-
[StructLayout(LayoutKind.Sequential, Size = RoundKeyCount * RoundKeySize)]
6-
public struct Aes128CryptoX86 : IBlockCrypto
3+
public class Aes128CryptoX86 : AESCryptoX86
74
{
8-
private const int RoundKeyCount = 20;
9-
private const int RoundKeySize = 0x10;
10-
11-
private Vector128<byte> _roundKeys;
12-
13-
private readonly ReadOnlySpan<Vector128<byte>> RoundKeys => MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _roundKeys), RoundKeyCount);
5+
private Vector128<byte> _k0, _k1, _k2, _k3, _k4, _k5, _k6, _k7, _k8, _k9, _k10,
6+
_k11, _k12, _k13, _k14, _k15, _k16, _k17, _k18, _k19;
147

15-
public Aes128CryptoX86(ReadOnlySpan<byte> key)
8+
public Aes128CryptoX86(ReadOnlySpan<byte> key) : base(key)
169
{
17-
ArgumentOutOfRangeException.ThrowIfNotEqual(key.Length, 16, nameof(key));
1810
Init(key);
1911
}
2012

@@ -30,96 +22,71 @@ private static Vector128<byte> KeyRound(Vector128<byte> key, [ConstantExpected]
3022
return key ^ t;
3123
}
3224

25+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3326
private void Init(ReadOnlySpan<byte> key)
3427
{
35-
Span<Vector128<byte>> roundKeys = MemoryMarshal.CreateSpan(ref _roundKeys, RoundKeyCount);
36-
37-
roundKeys[0] = Vector128.Create(key);
38-
roundKeys[1] = KeyRound(roundKeys[0], AESUtils.Rcon1);
39-
roundKeys[2] = KeyRound(roundKeys[1], AESUtils.Rcon2);
40-
roundKeys[3] = KeyRound(roundKeys[2], AESUtils.Rcon3);
41-
roundKeys[4] = KeyRound(roundKeys[3], AESUtils.Rcon4);
42-
roundKeys[5] = KeyRound(roundKeys[4], AESUtils.Rcon5);
43-
roundKeys[6] = KeyRound(roundKeys[5], AESUtils.Rcon6);
44-
roundKeys[7] = KeyRound(roundKeys[6], AESUtils.Rcon7);
45-
roundKeys[8] = KeyRound(roundKeys[7], AESUtils.Rcon8);
46-
roundKeys[9] = KeyRound(roundKeys[8], AESUtils.Rcon9);
47-
roundKeys[10] = KeyRound(roundKeys[9], AESUtils.Rcon10);
48-
49-
roundKeys[11] = Aes.InverseMixColumns(roundKeys[9]);
50-
roundKeys[12] = Aes.InverseMixColumns(roundKeys[8]);
51-
roundKeys[13] = Aes.InverseMixColumns(roundKeys[7]);
52-
roundKeys[14] = Aes.InverseMixColumns(roundKeys[6]);
53-
roundKeys[15] = Aes.InverseMixColumns(roundKeys[5]);
54-
roundKeys[16] = Aes.InverseMixColumns(roundKeys[4]);
55-
roundKeys[17] = Aes.InverseMixColumns(roundKeys[3]);
56-
roundKeys[18] = Aes.InverseMixColumns(roundKeys[2]);
57-
roundKeys[19] = Aes.InverseMixColumns(roundKeys[1]);
28+
_k0 = Vector128.Create(key);
29+
_k1 = KeyRound(_k0, Rcon1);
30+
_k2 = KeyRound(_k1, Rcon2);
31+
_k3 = KeyRound(_k2, Rcon3);
32+
_k4 = KeyRound(_k3, Rcon4);
33+
_k5 = KeyRound(_k4, Rcon5);
34+
_k6 = KeyRound(_k5, Rcon6);
35+
_k7 = KeyRound(_k6, Rcon7);
36+
_k8 = KeyRound(_k7, Rcon8);
37+
_k9 = KeyRound(_k8, Rcon9);
38+
_k10 = KeyRound(_k9, Rcon10);
39+
40+
_k11 = Aes.InverseMixColumns(_k9);
41+
_k12 = Aes.InverseMixColumns(_k8);
42+
_k13 = Aes.InverseMixColumns(_k7);
43+
_k14 = Aes.InverseMixColumns(_k6);
44+
_k15 = Aes.InverseMixColumns(_k5);
45+
_k16 = Aes.InverseMixColumns(_k4);
46+
_k17 = Aes.InverseMixColumns(_k3);
47+
_k18 = Aes.InverseMixColumns(_k2);
48+
_k19 = Aes.InverseMixColumns(_k1);
5849
}
5950

60-
public readonly void Encrypt(ReadOnlySpan<byte> source, Span<byte> destination)
51+
public override void Encrypt(ReadOnlySpan<byte> source, Span<byte> destination)
6152
{
62-
ArgumentOutOfRangeException.ThrowIfLessThan(source.Length, BlockSize, nameof(source));
63-
ArgumentOutOfRangeException.ThrowIfLessThan(destination.Length, BlockSize, nameof(destination));
53+
base.Encrypt(source, destination);
6454

6555
Vector128<byte> t = Vector128.Create(source);
66-
EncryptBlock(t).CopyTo(destination);
67-
}
6856

69-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
70-
private readonly Vector128<byte> EncryptBlock(Vector128<byte> input)
71-
{
72-
ReadOnlySpan<Vector128<byte>> keys = RoundKeys;
73-
74-
Vector128<byte> b = input ^ keys[0];
75-
b = Aes.Encrypt(b, keys[1]);
76-
b = Aes.Encrypt(b, keys[2]);
77-
b = Aes.Encrypt(b, keys[3]);
78-
b = Aes.Encrypt(b, keys[4]);
79-
b = Aes.Encrypt(b, keys[5]);
80-
b = Aes.Encrypt(b, keys[6]);
81-
b = Aes.Encrypt(b, keys[7]);
82-
b = Aes.Encrypt(b, keys[8]);
83-
b = Aes.Encrypt(b, keys[9]);
84-
return Aes.EncryptLast(b, keys[10]);
57+
t ^= _k0;
58+
t = Aes.Encrypt(t, _k1);
59+
t = Aes.Encrypt(t, _k2);
60+
t = Aes.Encrypt(t, _k3);
61+
t = Aes.Encrypt(t, _k4);
62+
t = Aes.Encrypt(t, _k5);
63+
t = Aes.Encrypt(t, _k6);
64+
t = Aes.Encrypt(t, _k7);
65+
t = Aes.Encrypt(t, _k8);
66+
t = Aes.Encrypt(t, _k9);
67+
t = Aes.EncryptLast(t, _k10);
68+
69+
t.CopyTo(destination);
8570
}
8671

87-
public readonly void Decrypt(ReadOnlySpan<byte> source, Span<byte> destination)
72+
public override void Decrypt(ReadOnlySpan<byte> source, Span<byte> destination)
8873
{
89-
ArgumentOutOfRangeException.ThrowIfLessThan(source.Length, BlockSize, nameof(source));
90-
ArgumentOutOfRangeException.ThrowIfLessThan(destination.Length, BlockSize, nameof(destination));
74+
base.Decrypt(source, destination);
9175

9276
Vector128<byte> t = Vector128.Create(source);
93-
DecryptBlock(t).CopyTo(destination);
94-
}
9577

96-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97-
private readonly Vector128<byte> DecryptBlock(Vector128<byte> input)
98-
{
99-
ReadOnlySpan<Vector128<byte>> keys = RoundKeys;
100-
101-
Vector128<byte> b = input ^ keys[10];
102-
b = Aes.Decrypt(b, keys[11]);
103-
b = Aes.Decrypt(b, keys[12]);
104-
b = Aes.Decrypt(b, keys[13]);
105-
b = Aes.Decrypt(b, keys[14]);
106-
b = Aes.Decrypt(b, keys[15]);
107-
b = Aes.Decrypt(b, keys[16]);
108-
b = Aes.Decrypt(b, keys[17]);
109-
b = Aes.Decrypt(b, keys[18]);
110-
b = Aes.Decrypt(b, keys[19]);
111-
return Aes.DecryptLast(b, keys[0]);
112-
}
113-
114-
public readonly int BlockSize => 16;
115-
116-
public readonly string Name => "AES";
117-
118-
public void Reset()
119-
{
120-
}
121-
122-
public void Dispose()
123-
{
78+
t ^= _k10;
79+
t = Aes.Decrypt(t, _k11);
80+
t = Aes.Decrypt(t, _k12);
81+
t = Aes.Decrypt(t, _k13);
82+
t = Aes.Decrypt(t, _k14);
83+
t = Aes.Decrypt(t, _k15);
84+
t = Aes.Decrypt(t, _k16);
85+
t = Aes.Decrypt(t, _k17);
86+
t = Aes.Decrypt(t, _k18);
87+
t = Aes.Decrypt(t, _k19);
88+
t = Aes.DecryptLast(t, _k0);
89+
90+
t.CopyTo(destination);
12491
}
12592
}

0 commit comments

Comments
 (0)