Skip to content

Commit c51ee8a

Browse files
committed
refactor: add Sm4Cipher
1 parent 2594b58 commit c51ee8a

File tree

12 files changed

+1088
-504
lines changed

12 files changed

+1088
-504
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void Dispose()
1818

1919
public static bool IsSupported => true;
2020

21-
private BcAesCipher(ReadOnlySpan<byte> key)
21+
private BcAesCipher(in ReadOnlySpan<byte> key)
2222
{
2323
KeyParameter keyParameter = new(key);
2424

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using CryptoBase.Abstractions.SymmetricCryptos;
2+
using CryptoBase.Abstractions.Vectors;
3+
using Org.BouncyCastle.Crypto.Engines;
4+
using Org.BouncyCastle.Crypto.Parameters;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace CryptoBase.BouncyCastle.SymmetricCryptos.BlockCryptos;
8+
9+
public sealed class BcSm4Cipher : IBlock16Cipher<BcSm4Cipher>
10+
{
11+
private readonly SM4Engine _encryptionEngine;
12+
13+
private readonly SM4Engine _decryptionEngine;
14+
15+
public void Dispose()
16+
{
17+
}
18+
19+
public static bool IsSupported => true;
20+
21+
private BcSm4Cipher(in ReadOnlySpan<byte> key)
22+
{
23+
KeyParameter keyParameter = new(key);
24+
25+
_encryptionEngine = new SM4Engine();
26+
_decryptionEngine = new SM4Engine();
27+
_encryptionEngine.Init(true, keyParameter);
28+
_decryptionEngine.Init(false, keyParameter);
29+
}
30+
31+
public static BcSm4Cipher Create(in ReadOnlySpan<byte> key)
32+
{
33+
return new BcSm4Cipher(key);
34+
}
35+
36+
public VectorBuffer16 Encrypt(scoped in VectorBuffer16 source)
37+
{
38+
Unsafe.SkipInit(out VectorBuffer16 r);
39+
_encryptionEngine.ProcessBlock(source, r);
40+
41+
return r;
42+
}
43+
44+
public VectorBuffer16 Decrypt(scoped in VectorBuffer16 source)
45+
{
46+
Unsafe.SkipInit(out VectorBuffer16 r);
47+
_decryptionEngine.ProcessBlock(source, r);
48+
49+
return r;
50+
}
51+
52+
public VectorBuffer32 Encrypt(scoped in VectorBuffer32 source)
53+
{
54+
Unsafe.SkipInit(out VectorBuffer32 r);
55+
56+
r.Lower = Encrypt(source.Lower);
57+
r.Upper = Encrypt(source.Upper);
58+
59+
return r;
60+
}
61+
62+
public VectorBuffer32 Decrypt(scoped in VectorBuffer32 source)
63+
{
64+
Unsafe.SkipInit(out VectorBuffer32 r);
65+
66+
r.Lower = Decrypt(source.Lower);
67+
r.Upper = Decrypt(source.Upper);
68+
69+
return r;
70+
}
71+
72+
public VectorBuffer64 Encrypt(scoped in VectorBuffer64 source)
73+
{
74+
Unsafe.SkipInit(out VectorBuffer64 r);
75+
76+
r.Lower = Encrypt(source.Lower);
77+
r.Upper = Encrypt(source.Upper);
78+
79+
return r;
80+
}
81+
82+
public VectorBuffer64 Decrypt(scoped in VectorBuffer64 source)
83+
{
84+
Unsafe.SkipInit(out VectorBuffer64 r);
85+
86+
r.Lower = Decrypt(source.Lower);
87+
r.Upper = Decrypt(source.Upper);
88+
89+
return r;
90+
}
91+
92+
public VectorBuffer128 Encrypt(scoped in VectorBuffer128 source)
93+
{
94+
Unsafe.SkipInit(out VectorBuffer128 r);
95+
96+
r.Lower = Encrypt(source.Lower);
97+
r.Upper = Encrypt(source.Upper);
98+
99+
return r;
100+
}
101+
102+
public VectorBuffer128 Decrypt(scoped in VectorBuffer128 source)
103+
{
104+
Unsafe.SkipInit(out VectorBuffer128 r);
105+
106+
r.Lower = Decrypt(source.Lower);
107+
r.Upper = Decrypt(source.Upper);
108+
109+
return r;
110+
}
111+
112+
public VectorBuffer256 Encrypt(scoped in VectorBuffer256 source)
113+
{
114+
Unsafe.SkipInit(out VectorBuffer256 r);
115+
116+
r.Lower = Encrypt(source.Lower);
117+
r.Upper = Encrypt(source.Upper);
118+
119+
return r;
120+
}
121+
122+
public VectorBuffer256 Decrypt(scoped in VectorBuffer256 source)
123+
{
124+
Unsafe.SkipInit(out VectorBuffer256 r);
125+
126+
r.Lower = Decrypt(source.Lower);
127+
r.Upper = Decrypt(source.Upper);
128+
129+
return r;
130+
}
131+
132+
public VectorBuffer512 Encrypt(scoped in VectorBuffer512 source)
133+
{
134+
Unsafe.SkipInit(out VectorBuffer512 r);
135+
136+
r.Lower = Encrypt(source.Lower);
137+
r.Upper = Encrypt(source.Upper);
138+
139+
return r;
140+
}
141+
142+
public VectorBuffer512 Decrypt(scoped in VectorBuffer512 source)
143+
{
144+
Unsafe.SkipInit(out VectorBuffer512 r);
145+
146+
r.Lower = Decrypt(source.Lower);
147+
r.Upper = Decrypt(source.Upper);
148+
149+
return r;
150+
}
151+
}

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/CryptoBase/SymmetricCryptos/BlockCryptos/SM4/SM4Crypto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override void Encrypt(ReadOnlySpan<byte> source, Span<byte> destination)
6060

6161
Span<uint> rk = _roundKeys.Span;
6262

63-
Unsafe.WriteUnaligned(ref destination.GetReference(), SM4Utils.Encrypt(rk, source.AsVectorBuffer16()));
63+
Unsafe.WriteUnaligned(ref destination.GetReference(), SM4Utils.ProcessBlock(rk, source.AsVectorBuffer16()));
6464
}
6565

6666
public override void Decrypt(ReadOnlySpan<byte> source, Span<byte> destination)
@@ -69,7 +69,7 @@ public override void Decrypt(ReadOnlySpan<byte> source, Span<byte> destination)
6969

7070
Span<uint> rk = _reverseRoundKeys.Span;
7171

72-
Unsafe.WriteUnaligned(ref destination.GetReference(), SM4Utils.Encrypt(rk, source.AsVectorBuffer16()));
72+
Unsafe.WriteUnaligned(ref destination.GetReference(), SM4Utils.ProcessBlock(rk, source.AsVectorBuffer16()));
7373
}
7474

7575
public override void Encrypt4(ReadOnlySpan<byte> source, Span<byte> destination)

0 commit comments

Comments
 (0)