Skip to content

Commit 3491b9a

Browse files
committed
perf: refactor IBlock16Cipher and fix xts mode
1 parent 4ea73b3 commit 3491b9a

File tree

25 files changed

+734
-636
lines changed

25 files changed

+734
-636
lines changed

src/CryptoBase.Abstractions/SymmetricCryptos/BlockCipherHardwareAcceleration.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ public enum BlockCipherHardwareAcceleration
88
Block2 = 1 << 1,
99
Block4 = 1 << 2,
1010
Block8 = 1 << 3,
11-
Block16 = 1 << 4,
12-
Block32 = 1 << 5,
13-
Block64 = 1 << 6
11+
Block8V256 = 1 << 4,
12+
Block16V256 = 1 << 5,
13+
Block32V256 = 1 << 6,
14+
Block16V512 = 1 << 7,
15+
Block32V512 = 1 << 8,
16+
Block64V512 = 1 << 9
1417
}

src/CryptoBase.Abstractions/SymmetricCryptos/IBlock16Cipher.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ public interface IBlock16Cipher<out TSelf> : ISymmetricCrypto where TSelf : IBlo
2020
VectorBuffer128 Encrypt(scoped in VectorBuffer128 source);
2121
VectorBuffer128 Decrypt(scoped in VectorBuffer128 source);
2222

23-
VectorBuffer256 Encrypt(scoped in VectorBuffer256 source);
24-
VectorBuffer256 Decrypt(scoped in VectorBuffer256 source);
23+
VectorBuffer128 EncryptV256(scoped in VectorBuffer128 source);
24+
VectorBuffer128 DecryptV256(scoped in VectorBuffer128 source);
2525

26-
VectorBuffer512 Encrypt(scoped in VectorBuffer512 source);
27-
VectorBuffer512 Decrypt(scoped in VectorBuffer512 source);
26+
VectorBuffer256 EncryptV256(scoped in VectorBuffer256 source);
27+
VectorBuffer256 DecryptV256(scoped in VectorBuffer256 source);
28+
29+
VectorBuffer256 EncryptV512(scoped in VectorBuffer256 source);
30+
VectorBuffer256 DecryptV512(scoped in VectorBuffer256 source);
31+
32+
VectorBuffer512 EncryptV512(scoped in VectorBuffer512 source);
33+
VectorBuffer512 DecryptV512(scoped in VectorBuffer512 source);
2834
}

src/CryptoBase.Abstractions/Vectors/VectorBuffer128.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,4 @@ public static implicit operator Span<byte>(in VectorBuffer128 value)
2525
{
2626
return Unsafe.AsRef(in value).AsSpan();
2727
}
28-
29-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30-
public static VectorBuffer128 operator ^(scoped in VectorBuffer128 left, scoped in VectorBuffer128 right)
31-
{
32-
if (Vector256.IsHardwareAccelerated)
33-
{
34-
return new VectorBuffer128
35-
{
36-
V256_0 = left.V256_0 ^ right.V256_0,
37-
V256_1 = left.V256_1 ^ right.V256_1,
38-
V256_2 = left.V256_2 ^ right.V256_2,
39-
V256_3 = left.V256_3 ^ right.V256_3
40-
};
41-
}
42-
43-
return new VectorBuffer128
44-
{
45-
V128_0 = left.V128_0 ^ right.V128_0,
46-
V128_1 = left.V128_1 ^ right.V128_1,
47-
V128_2 = left.V128_2 ^ right.V128_2,
48-
V128_3 = left.V128_3 ^ right.V128_3,
49-
V128_4 = left.V128_4 ^ right.V128_4,
50-
V128_5 = left.V128_5 ^ right.V128_5,
51-
V128_6 = left.V128_6 ^ right.V128_6,
52-
V128_7 = left.V128_7 ^ right.V128_7
53-
};
54-
}
5528
}

src/CryptoBase.Abstractions/Vectors/VectorBuffer256.cs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,54 +42,4 @@ public static implicit operator Span<byte>(in VectorBuffer256 value)
4242
{
4343
return Unsafe.AsRef(in value).AsSpan();
4444
}
45-
46-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47-
public static VectorBuffer256 operator ^(scoped in VectorBuffer256 left, scoped in VectorBuffer256 right)
48-
{
49-
if (Vector512.IsHardwareAccelerated)
50-
{
51-
return new VectorBuffer256
52-
{
53-
V512_0 = left.V512_0 ^ right.V512_0,
54-
V512_1 = left.V512_1 ^ right.V512_1,
55-
V512_2 = left.V512_2 ^ right.V512_2,
56-
V512_3 = left.V512_3 ^ right.V512_3
57-
};
58-
}
59-
60-
if (Vector256.IsHardwareAccelerated)
61-
{
62-
return new VectorBuffer256
63-
{
64-
V256_0 = left.V256_0 ^ right.V256_0,
65-
V256_1 = left.V256_1 ^ right.V256_1,
66-
V256_2 = left.V256_2 ^ right.V256_2,
67-
V256_3 = left.V256_3 ^ right.V256_3,
68-
V256_4 = left.V256_4 ^ right.V256_4,
69-
V256_5 = left.V256_5 ^ right.V256_5,
70-
V256_6 = left.V256_6 ^ right.V256_6,
71-
V256_7 = left.V256_7 ^ right.V256_7
72-
};
73-
}
74-
75-
return new VectorBuffer256
76-
{
77-
V128_0 = left.V128_0 ^ right.V128_0,
78-
V128_1 = left.V128_1 ^ right.V128_1,
79-
V128_2 = left.V128_2 ^ right.V128_2,
80-
V128_3 = left.V128_3 ^ right.V128_3,
81-
V128_4 = left.V128_4 ^ right.V128_4,
82-
V128_5 = left.V128_5 ^ right.V128_5,
83-
V128_6 = left.V128_6 ^ right.V128_6,
84-
V128_7 = left.V128_7 ^ right.V128_7,
85-
V128_8 = left.V128_8 ^ right.V128_8,
86-
V128_9 = left.V128_9 ^ right.V128_9,
87-
V128_10 = left.V128_10 ^ right.V128_10,
88-
V128_11 = left.V128_11 ^ right.V128_11,
89-
V128_12 = left.V128_12 ^ right.V128_12,
90-
V128_13 = left.V128_13 ^ right.V128_13,
91-
V128_14 = left.V128_14 ^ right.V128_14,
92-
V128_15 = left.V128_15 ^ right.V128_15
93-
};
94-
}
9545
}

src/CryptoBase.Abstractions/Vectors/VectorBuffer32.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,4 @@ public static implicit operator Span<byte>(in VectorBuffer32 value)
1616
{
1717
return Unsafe.AsRef(in value).AsSpan();
1818
}
19-
20-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21-
public static VectorBuffer32 operator ^(scoped in VectorBuffer32 left, scoped in VectorBuffer32 right)
22-
{
23-
if (Vector256.IsHardwareAccelerated)
24-
{
25-
return new VectorBuffer32 { V256 = left.V256 ^ right.V256 };
26-
}
27-
28-
return new VectorBuffer32
29-
{
30-
V128_0 = left.V128_0 ^ right.V128_0,
31-
V128_1 = left.V128_1 ^ right.V128_1
32-
};
33-
}
3419
}

src/CryptoBase.Abstractions/Vectors/VectorBuffer512.cs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,4 @@ public static implicit operator Span<byte>(in VectorBuffer512 value)
3737
{
3838
return Unsafe.AsRef(in value).AsSpan();
3939
}
40-
41-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
42-
public static VectorBuffer512 operator ^(scoped in VectorBuffer512 left, scoped in VectorBuffer512 right)
43-
{
44-
if (Vector512.IsHardwareAccelerated)
45-
{
46-
return new VectorBuffer512
47-
{
48-
V512_0 = left.V512_0 ^ right.V512_0,
49-
V512_1 = left.V512_1 ^ right.V512_1,
50-
V512_2 = left.V512_2 ^ right.V512_2,
51-
V512_3 = left.V512_3 ^ right.V512_3,
52-
V512_4 = left.V512_4 ^ right.V512_4,
53-
V512_5 = left.V512_5 ^ right.V512_5,
54-
V512_6 = left.V512_6 ^ right.V512_6,
55-
V512_7 = left.V512_7 ^ right.V512_7
56-
};
57-
}
58-
59-
return new VectorBuffer512
60-
{
61-
V256_0 = left.V256_0 ^ right.V256_0,
62-
V256_1 = left.V256_1 ^ right.V256_1,
63-
V256_2 = left.V256_2 ^ right.V256_2,
64-
V256_3 = left.V256_3 ^ right.V256_3,
65-
V256_4 = left.V256_4 ^ right.V256_4,
66-
V256_5 = left.V256_5 ^ right.V256_5,
67-
V256_6 = left.V256_6 ^ right.V256_6,
68-
V256_7 = left.V256_7 ^ right.V256_7,
69-
V256_8 = left.V256_8 ^ right.V256_8,
70-
V256_9 = left.V256_9 ^ right.V256_9,
71-
V256_10 = left.V256_10 ^ right.V256_10,
72-
V256_11 = left.V256_11 ^ right.V256_11,
73-
V256_12 = left.V256_12 ^ right.V256_12,
74-
V256_13 = left.V256_13 ^ right.V256_13,
75-
V256_14 = left.V256_14 ^ right.V256_14,
76-
V256_15 = left.V256_15 ^ right.V256_15
77-
};
78-
}
7940
}

src/CryptoBase.Abstractions/Vectors/VectorBuffer64.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,4 @@ public static implicit operator Span<byte>(in VectorBuffer64 value)
1919
{
2020
return Unsafe.AsRef(in value).AsSpan();
2121
}
22-
23-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24-
public static VectorBuffer64 operator ^(scoped in VectorBuffer64 left, scoped in VectorBuffer64 right)
25-
{
26-
if (Vector256.IsHardwareAccelerated)
27-
{
28-
return new VectorBuffer64
29-
{
30-
V256_0 = left.V256_0 ^ right.V256_0,
31-
V256_1 = left.V256_1 ^ right.V256_1
32-
};
33-
}
34-
35-
return new VectorBuffer64
36-
{
37-
V128_0 = left.V128_0 ^ right.V128_0,
38-
V128_1 = left.V128_1 ^ right.V128_1,
39-
V128_2 = left.V128_2 ^ right.V128_2,
40-
V128_3 = left.V128_3 ^ right.V128_3
41-
};
42-
}
4322
}

src/CryptoBase.BouncyCastle/SymmetricCryptos/BlockCryptos/BcAesCipher.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,43 +136,43 @@ public VectorBuffer128 Decrypt(scoped in VectorBuffer128 source)
136136
return r;
137137
}
138138

139-
public VectorBuffer256 Encrypt(scoped in VectorBuffer256 source)
139+
public VectorBuffer128 EncryptV256(scoped in VectorBuffer128 source)
140140
{
141-
Unsafe.SkipInit(out VectorBuffer256 r);
142-
143-
r.Lower = Encrypt(source.Lower);
144-
r.Upper = Encrypt(source.Upper);
145-
146-
return r;
141+
throw new NotSupportedException();
147142
}
148143

149-
public VectorBuffer256 Decrypt(scoped in VectorBuffer256 source)
144+
public VectorBuffer128 DecryptV256(scoped in VectorBuffer128 source)
150145
{
151-
Unsafe.SkipInit(out VectorBuffer256 r);
152-
153-
r.Lower = Decrypt(source.Lower);
154-
r.Upper = Decrypt(source.Upper);
155-
156-
return r;
146+
throw new NotSupportedException();
157147
}
158148

159-
public VectorBuffer512 Encrypt(scoped in VectorBuffer512 source)
149+
public VectorBuffer256 EncryptV256(scoped in VectorBuffer256 source)
160150
{
161-
Unsafe.SkipInit(out VectorBuffer512 r);
151+
throw new NotSupportedException();
152+
}
162153

163-
r.Lower = Encrypt(source.Lower);
164-
r.Upper = Encrypt(source.Upper);
154+
public VectorBuffer256 DecryptV256(scoped in VectorBuffer256 source)
155+
{
156+
throw new NotSupportedException();
157+
}
165158

166-
return r;
159+
public VectorBuffer256 EncryptV512(scoped in VectorBuffer256 source)
160+
{
161+
throw new NotSupportedException();
167162
}
168163

169-
public VectorBuffer512 Decrypt(scoped in VectorBuffer512 source)
164+
public VectorBuffer256 DecryptV512(scoped in VectorBuffer256 source)
170165
{
171-
Unsafe.SkipInit(out VectorBuffer512 r);
166+
throw new NotSupportedException();
167+
}
172168

173-
r.Lower = Decrypt(source.Lower);
174-
r.Upper = Decrypt(source.Upper);
169+
public VectorBuffer512 EncryptV512(scoped in VectorBuffer512 source)
170+
{
171+
throw new NotSupportedException();
172+
}
175173

176-
return r;
174+
public VectorBuffer512 DecryptV512(scoped in VectorBuffer512 source)
175+
{
176+
throw new NotSupportedException();
177177
}
178178
}

src/CryptoBase.BouncyCastle/SymmetricCryptos/BlockCryptos/BcSm4Cipher.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,43 +113,43 @@ public VectorBuffer128 Decrypt(scoped in VectorBuffer128 source)
113113
return r;
114114
}
115115

116-
public VectorBuffer256 Encrypt(scoped in VectorBuffer256 source)
116+
public VectorBuffer128 EncryptV256(scoped in VectorBuffer128 source)
117117
{
118-
Unsafe.SkipInit(out VectorBuffer256 r);
119-
120-
r.Lower = Encrypt(source.Lower);
121-
r.Upper = Encrypt(source.Upper);
122-
123-
return r;
118+
throw new NotSupportedException();
124119
}
125120

126-
public VectorBuffer256 Decrypt(scoped in VectorBuffer256 source)
121+
public VectorBuffer128 DecryptV256(scoped in VectorBuffer128 source)
127122
{
128-
Unsafe.SkipInit(out VectorBuffer256 r);
129-
130-
r.Lower = Decrypt(source.Lower);
131-
r.Upper = Decrypt(source.Upper);
132-
133-
return r;
123+
throw new NotSupportedException();
134124
}
135125

136-
public VectorBuffer512 Encrypt(scoped in VectorBuffer512 source)
126+
public VectorBuffer256 EncryptV256(scoped in VectorBuffer256 source)
137127
{
138-
Unsafe.SkipInit(out VectorBuffer512 r);
128+
throw new NotSupportedException();
129+
}
139130

140-
r.Lower = Encrypt(source.Lower);
141-
r.Upper = Encrypt(source.Upper);
131+
public VectorBuffer256 DecryptV256(scoped in VectorBuffer256 source)
132+
{
133+
throw new NotSupportedException();
134+
}
142135

143-
return r;
136+
public VectorBuffer256 EncryptV512(scoped in VectorBuffer256 source)
137+
{
138+
throw new NotSupportedException();
144139
}
145140

146-
public VectorBuffer512 Decrypt(scoped in VectorBuffer512 source)
141+
public VectorBuffer256 DecryptV512(scoped in VectorBuffer256 source)
147142
{
148-
Unsafe.SkipInit(out VectorBuffer512 r);
143+
throw new NotSupportedException();
144+
}
149145

150-
r.Lower = Decrypt(source.Lower);
151-
r.Upper = Decrypt(source.Upper);
146+
public VectorBuffer512 EncryptV512(scoped in VectorBuffer512 source)
147+
{
148+
throw new NotSupportedException();
149+
}
152150

153-
return r;
151+
public VectorBuffer512 DecryptV512(scoped in VectorBuffer512 source)
152+
{
153+
throw new NotSupportedException();
154154
}
155155
}

0 commit comments

Comments
 (0)