1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-27 03:52:57 +02:00

move CbcBlockCipher into crypto methods instead of singleton instance to avoid multithreaded issues

This commit is contained in:
Kyle Spearrin 2016-07-11 20:14:24 -04:00
parent f0455aad74
commit 635b09de9b

View File

@ -22,12 +22,10 @@ namespace Bit.App.Services
private readonly Random _random = new Random();
private readonly ISecureStorageService _secureStorage;
private readonly CbcBlockCipher _aesBlockCipher;
private KeyParameter _keyParameter;
public CryptoService(ISecureStorageService secureStorage)
{
_aesBlockCipher = new CbcBlockCipher(new AesEngine());
_secureStorage = secureStorage;
}
@ -93,7 +91,8 @@ namespace Bit.App.Services
var iv = GenerateRandomInitializationVector();
var keyParamWithIV = new ParametersWithIV(_keyParameter, iv, 0, InitializationVectorSize);
var cipher = new PaddedBufferedBlockCipher(_aesBlockCipher);
var aesBlockCipher = new CbcBlockCipher(new AesEngine());
var cipher = new PaddedBufferedBlockCipher(aesBlockCipher);
cipher.Init(true, keyParamWithIV);
var encryptedBytes = new byte[cipher.GetOutputSize(plaintextBytes.Length)];
var length = cipher.ProcessBytes(plaintextBytes, encryptedBytes, 0);
@ -117,7 +116,8 @@ namespace Bit.App.Services
try
{
var keyParamWithIV = new ParametersWithIV(_keyParameter, encyptedValue.InitializationVectorBytes, 0, InitializationVectorSize);
var cipher = new PaddedBufferedBlockCipher(_aesBlockCipher);
var aesBlockCipher = new CbcBlockCipher(new AesEngine());
var cipher = new PaddedBufferedBlockCipher(aesBlockCipher);
cipher.Init(false, keyParamWithIV);
byte[] comparisonBytes = new byte[cipher.GetOutputSize(encyptedValue.CipherTextBytes.Length)];
var length = cipher.ProcessBytes(encyptedValue.CipherTextBytes, comparisonBytes, 0);