OSDP.Net 5.3.0-beta.3
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
BouncyCastleAes.cs
1using System;
2using System.Security.Cryptography;
3using Org.BouncyCastle.Crypto;
4using Org.BouncyCastle.Crypto.Engines;
5using Org.BouncyCastle.Crypto.Modes;
6using Org.BouncyCastle.Crypto.Parameters;
7
9
15internal sealed class BouncyCastleAes : Aes
16{
17 private const int BlockSizeInBytes = 16;
18
19 private static readonly bool NativeSupported = IsNativeSupported();
20
24 internal static bool ForceBouncyCastle { get; set; }
25
29 internal static Aes CreateAes() =>
30 NativeSupported && !ForceBouncyCastle ? Aes.Create() : new BouncyCastleAes();
31
32 // In browser WebAssembly Aes.Create() succeeds; the first transform throws.
33 private static bool IsNativeSupported()
34 {
35 try
36 {
37 using var aes = Aes.Create();
38 using var transform = aes.CreateEncryptor(new byte[BlockSizeInBytes], new byte[BlockSizeInBytes]);
39 return true;
40 }
41 catch (Exception exception) when (exception is PlatformNotSupportedException or CryptographicException)
42 {
43 return false;
44 }
45 }
46
47 // Parameter names match SymmetricAlgorithm's declarations.
48 // ReSharper disable InconsistentNaming
49
51 public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) =>
52 CreateTransform(true, rgbKey, rgbIV);
53
55 public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) =>
56 CreateTransform(false, rgbKey, rgbIV);
57
58 // ReSharper restore InconsistentNaming
59
61 public override void GenerateKey() => KeyValue = RandomBytes(KeySizeValue / 8);
62
64 public override void GenerateIV() => IVValue = RandomBytes(BlockSizeInBytes);
65
66 private static byte[] RandomBytes(int count)
67 {
68 var bytes = new byte[count];
69 using var random = RandomNumberGenerator.Create();
70 random.GetBytes(bytes);
71 return bytes;
72 }
73
74 private ICryptoTransform CreateTransform(bool encrypt, byte[] key, byte[] iv)
75 {
76 if (Padding != PaddingMode.None && Padding != PaddingMode.Zeros)
77 {
78 throw new NotSupportedException($"Padding mode {Padding} is not supported");
79 }
80
81 IBlockCipher cipher;
82 ICipherParameters parameters = new KeyParameter(key);
83 switch (Mode)
84 {
85 case CipherMode.ECB:
86 cipher = new AesEngine();
87 break;
88 case CipherMode.CBC:
89 cipher = new CbcBlockCipher(new AesEngine());
90 parameters = new ParametersWithIV(parameters, iv ?? new byte[BlockSizeInBytes]);
91 break;
92 default:
93 throw new NotSupportedException($"Cipher mode {Mode} is not supported");
94 }
95
96 cipher.Init(encrypt, parameters);
97 return new BlockTransform(cipher);
98 }
99
104 private sealed class BlockTransform(IBlockCipher cipher) : ICryptoTransform
105 {
106 public int InputBlockSize => BlockSizeInBytes;
107
108 public int OutputBlockSize => BlockSizeInBytes;
109
110 public bool CanTransformMultipleBlocks => true;
111
112 public bool CanReuseTransform => false;
113
114 public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer,
115 int outputOffset)
116 {
117 if (inputCount % BlockSizeInBytes != 0)
118 {
119 throw new CryptographicException("The input must be a whole number of blocks");
120 }
121
122 for (var offset = 0; offset < inputCount; offset += BlockSizeInBytes)
123 {
124 cipher.ProcessBlock(inputBuffer, inputOffset + offset, outputBuffer, outputOffset + offset);
125 }
126
127 return inputCount;
128 }
129
130 public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
131 {
132 var output = new byte[inputCount];
133 TransformBlock(inputBuffer, inputOffset, inputCount, output, 0);
134 return output;
135 }
136
137 public void Dispose()
138 {
139 }
140 }
141}