remove silly workaround for aes-ctr encrypt/decrypt

This commit is contained in:
Sorunome 2020-05-25 17:55:49 +02:00
parent adc5591e54
commit 80c7125d1d
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
3 changed files with 5 additions and 34 deletions

View file

@ -24,7 +24,6 @@ const BASE58_ALPHABET =
const base58 = Base58Codec(BASE58_ALPHABET); const base58 = Base58Codec(BASE58_ALPHABET);
const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01]; const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01];
const OLM_PRIVATE_KEY_LENGTH = 32; // TODO: fetch from dart-olm const OLM_PRIVATE_KEY_LENGTH = 32; // TODO: fetch from dart-olm
const AES_BLOCKSIZE = 16;
class SSSS { class SSSS {
final Client client; final Client client;
@ -56,24 +55,10 @@ class SSSS {
final keys = deriveKeys(key, name); final keys = deriveKeys(key, name);
// workaround for https://github.com/leocavalcante/encrypt/issues/136 final plain = Uint8List.fromList(utf8.encode(data));
var plain = Uint8List.fromList(utf8.encode(data)); final ciphertext = AES(Key(keys.aesKey), mode: AESMode.ctr, padding: null)
final bytesMissing = AES_BLOCKSIZE - (plain.lengthInBytes % AES_BLOCKSIZE);
if (bytesMissing != AES_BLOCKSIZE) {
// we want to be able to modify it
final oldPlain = plain;
plain = Uint8List(plain.lengthInBytes + bytesMissing);
for (var i = 0; i < oldPlain.lengthInBytes; i++) {
plain[i] = oldPlain[i];
}
}
var ciphertext = AES(Key(keys.aesKey), mode: AESMode.ctr, padding: null)
.encrypt(plain, iv: IV(iv)) .encrypt(plain, iv: IV(iv))
.bytes; .bytes;
if (bytesMissing != AES_BLOCKSIZE) {
// chop off those extra bytes again
ciphertext = ciphertext.sublist(0, plain.length - bytesMissing);
}
final hmac = Hmac(sha256, keys.hmacKey).convert(ciphertext); final hmac = Hmac(sha256, keys.hmacKey).convert(ciphertext);
@ -94,23 +79,9 @@ class SSSS {
throw 'Bad MAC'; throw 'Bad MAC';
} }
// workaround for https://github.com/leocavalcante/encrypt/issues/136 // workaround for https://github.com/leocavalcante/encrypt/issues/136
var cipher = base64.decode(data.ciphertext); final cipher = base64.decode(data.ciphertext);
final bytesMissing = AES_BLOCKSIZE - (cipher.lengthInBytes % AES_BLOCKSIZE);
if (bytesMissing != AES_BLOCKSIZE) {
// we want to be able to modify it
final oldCipher = cipher;
cipher = Uint8List(cipher.lengthInBytes + bytesMissing);
for (var i = 0; i < oldCipher.lengthInBytes; i++) {
cipher[i] = oldCipher[i];
}
}
final decipher = AES(Key(keys.aesKey), mode: AESMode.ctr, padding: null) final decipher = AES(Key(keys.aesKey), mode: AESMode.ctr, padding: null)
.decrypt(Encrypted(cipher), iv: IV(base64.decode(data.iv))); .decrypt(Encrypted(cipher), iv: IV(base64.decode(data.iv)));
if (bytesMissing != AES_BLOCKSIZE) {
// chop off those extra bytes again
return String.fromCharCodes(
decipher.sublist(0, decipher.length - bytesMissing));
}
return String.fromCharCodes(decipher); return String.fromCharCodes(decipher);
} }

View file

@ -210,7 +210,7 @@ packages:
name: encrypt name: encrypt
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "4.0.1" version: "4.0.2"
ffi: ffi:
dependency: transitive dependency: transitive
description: description:

View file

@ -16,7 +16,7 @@ dependencies:
html_unescape: ^1.0.1+3 html_unescape: ^1.0.1+3
moor: ^3.0.2 moor: ^3.0.2
random_string: ^2.0.1 random_string: ^2.0.1
encrypt: ^4.0.1 encrypt: ^4.0.2
crypto: ^2.1.4 crypto: ^2.1.4
base58check: ^1.0.1 base58check: ^1.0.1
password_hash: ^2.0.0 password_hash: ^2.0.0