|
| 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 | +} |
0 commit comments