Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BasicSymmetricCipherCsp.cs
Go to the documentation of this file.
1using System;
4
6
7internal sealed class BasicSymmetricCipherCsp : Internal.Cryptography.BasicSymmetricCipher
8{
9 private readonly bool _encrypting;
10
12
14
15 public BasicSymmetricCipherCsp(int algId, CipherMode cipherMode, int blockSizeInBytes, byte[] key, int effectiveKeyLength, bool addNoSaltFlag, byte[] iv, bool encrypting, int feedbackSize, int paddingSizeInBytes)
16 : base(cipherMode.GetCipherIv(iv), blockSizeInBytes, paddingSizeInBytes)
17 {
18 _encrypting = encrypting;
20 _hKey = ImportCspBlob(_hProvider, algId, key, addNoSaltFlag);
22 if (cipherMode == CipherMode.CFB)
23 {
25 }
26 byte[] cipherIv = cipherMode.GetCipherIv(iv);
27 if (cipherIv != null)
28 {
30 }
31 if (effectiveKeyLength != 0)
32 {
33 CapiHelper.SetKeyParameter(_hKey, CapiHelper.CryptGetKeyParamQueryType.KP_EFFECTIVE_KEYLEN, effectiveKeyLength);
34 }
35 }
36
37 protected override void Dispose(bool disposing)
38 {
39 if (disposing)
40 {
41 SafeKeyHandle hKey = _hKey;
42 _hKey = null;
43 hKey?.Dispose();
44 SafeProvHandle hProvider = _hProvider;
45 _hProvider = null;
46 hProvider?.Dispose();
47 }
48 base.Dispose(disposing);
49 }
50
51 public override int Transform(ReadOnlySpan<byte> input, Span<byte> output)
52 {
53 return Transform(input, output, isFinal: false);
54 }
55
57 {
58 int result = 0;
59 if (input.Length != 0)
60 {
61 result = Transform(input, output, isFinal: true);
62 }
63 Reset();
64 return result;
65 }
66
67 private void Reset()
68 {
69 CapiHelper.EncryptData(_hKey, default(ReadOnlySpan<byte>), default(Span<byte>), isFinal: true);
70 }
71
72 private int Transform(ReadOnlySpan<byte> input, Span<byte> output, bool isFinal)
73 {
74 if (_encrypting)
75 {
76 return CapiHelper.EncryptData(_hKey, input, output, isFinal);
77 }
78 return CapiHelper.DecryptData(_hKey, input, output);
79 }
80
81 private static SafeKeyHandle ImportCspBlob(SafeProvHandle safeProvHandle, int algId, byte[] rawKey, bool addNoSaltFlag)
82 {
83 byte[] keyBlob = CapiHelper.ToPlainTextKeyBlob(algId, rawKey);
84 CapiHelper.ImportKeyBlob(safeProvHandle, CspProviderFlags.NoFlags, addNoSaltFlag, keyBlob, out var safeKeyHandle);
85 return safeKeyHandle;
86 }
87
89 {
90 CspParameters cspParameters = new CspParameters(1);
91 CapiHelper.AcquireCsp(cspParameters, out var safeProvHandle);
92 return safeProvHandle;
93 }
94}
override int TransformFinal(ReadOnlySpan< byte > input, Span< byte > output)
int Transform(ReadOnlySpan< byte > input, Span< byte > output, bool isFinal)
static SafeKeyHandle ImportCspBlob(SafeProvHandle safeProvHandle, int algId, byte[] rawKey, bool addNoSaltFlag)
override int Transform(ReadOnlySpan< byte > input, Span< byte > output)
BasicSymmetricCipherCsp(int algId, CipherMode cipherMode, int blockSizeInBytes, byte[] key, int effectiveKeyLength, bool addNoSaltFlag, byte[] iv, bool encrypting, int feedbackSize, int paddingSizeInBytes)
static void AcquireCsp(CspParameters cspParameters, out SafeProvHandle safeProvHandle)
static void SetKeyParameter(SafeKeyHandle safeKeyHandle, CryptGetKeyParamQueryType keyParam, byte[] value)
static int DecryptData(SafeKeyHandle hKey, ReadOnlySpan< byte > input, Span< byte > output)
static byte[] ToPlainTextKeyBlob(int algId, byte[] rawKey)
static void ImportKeyBlob(SafeProvHandle saveProvHandle, CspProviderFlags flags, bool addNoSaltFlag, byte[] keyBlob, out SafeKeyHandle safeKeyHandle)
static int EncryptData(SafeKeyHandle hKey, ReadOnlySpan< byte > input, Span< byte > output, bool isFinal)
override void Dispose(bool disposing)