1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-16 00:03:22 +00:00

async vault fetch and filter (search) tasks. Resolved singleton crypto issue around reuse of PaddedBufferedBlockCipher

This commit is contained in:
Kyle Spearrin
2016-07-11 19:36:39 -04:00
parent f9fd53c733
commit f0455aad74
2 changed files with 46 additions and 34 deletions

View File

@@ -21,16 +21,13 @@ namespace Bit.App.Services
private const int Iterations = 5000;
private readonly Random _random = new Random();
private readonly PaddedBufferedBlockCipher _cipher;
private readonly ISecureStorageService _secureStorage;
private readonly CbcBlockCipher _aesBlockCipher;
private KeyParameter _keyParameter;
public CryptoService(ISecureStorageService secureStorage)
{
var engine = new AesEngine();
var blockCipher = new CbcBlockCipher(engine);
_cipher = new PaddedBufferedBlockCipher(blockCipher);
_aesBlockCipher = new CbcBlockCipher(new AesEngine());
_secureStorage = secureStorage;
}
@@ -96,10 +93,11 @@ namespace Bit.App.Services
var iv = GenerateRandomInitializationVector();
var keyParamWithIV = new ParametersWithIV(_keyParameter, iv, 0, InitializationVectorSize);
_cipher.Init(true, keyParamWithIV);
var encryptedBytes = new byte[_cipher.GetOutputSize(plaintextBytes.Length)];
var length = _cipher.ProcessBytes(plaintextBytes, encryptedBytes, 0);
_cipher.DoFinal(encryptedBytes, length);
var cipher = new PaddedBufferedBlockCipher(_aesBlockCipher);
cipher.Init(true, keyParamWithIV);
var encryptedBytes = new byte[cipher.GetOutputSize(plaintextBytes.Length)];
var length = cipher.ProcessBytes(plaintextBytes, encryptedBytes, 0);
cipher.DoFinal(encryptedBytes, length);
return new CipherString(Convert.ToBase64String(iv), Convert.ToBase64String(encryptedBytes));
}
@@ -119,10 +117,11 @@ namespace Bit.App.Services
try
{
var keyParamWithIV = new ParametersWithIV(_keyParameter, encyptedValue.InitializationVectorBytes, 0, InitializationVectorSize);
_cipher.Init(false, keyParamWithIV);
byte[] comparisonBytes = new byte[_cipher.GetOutputSize(encyptedValue.CipherTextBytes.Length)];
var length = _cipher.ProcessBytes(encyptedValue.CipherTextBytes, comparisonBytes, 0);
_cipher.DoFinal(comparisonBytes, length);
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);
cipher.DoFinal(comparisonBytes, length);
return Encoding.UTF8.GetString(comparisonBytes, 0, comparisonBytes.Length).TrimEnd('\0');
}
catch(Exception e)