Skip to main content

Insecure encryption algorithm

Need

Secure encryption algorithm implementation

Context

  • Usage of Dart 2.12.0 for developing cross-platform applications
  • Usage of cipher for encryption and decryption operations

Description

Non compliant code

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:cipher/cipher.dart';
import 'package:cipher/impl/base.dart';
import 'package:shelf/shelf.dart';

void main() {
final server = HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
server.listen((HttpRequest request) async {
var key = utf8.encode("insecure");
var cipher = PaddedBlockCipherImpl(
PKCS7Padding(), ECBMode(BlockCipher("AES")));

cipher.init(true, PaddedBlockCipherParameters(key, null));

var data = utf8.encode("Sensitive information");
var encData = cipher.process(data);

var response = Response.ok(encData.toString());
await request.response.addStream(response.read());
});
}

The above Dart code snippet shows an example of an insecure encryption algorithm usage. The application is a simple server built with the shelf framework, and it uses the cipher package to encrypt sensitive information.

The server listens on localhost:8080 and, upon receiving a request, it encrypts a string "Sensitive information" using the AES encryption algorithm in ECB mode with a static key "insecure".

The problem here is twofold:

  1. Static encryption key: The key used for encryption is a static string "insecure". This is a poor practice as anyone who has access to the source code or can reverse engineer the application can easily decrypt any encrypted data as the key is not only static but also very weak.

  2. ECB mode: ECB (Electronic Codebook) mode is an insecure method of encryption. It does not use any Initialization Vector (IV) and encrypts each block of plaintext independently. This means that identical plaintext blocks are encrypted into identical ciphertext blocks, making it vulnerable to various attacks as patterns in the plaintext are preserved in the encrypted data.

To mitigate this vulnerability, it is advised to use a secure encryption algorithm with a strong key, and the key should be stored securely, not hardcoded into the application. Furthermore, a secure mode of operation, such as CBC (Cipher Block Chaining) or GCM (Galois/Counter Mode), should be used instead of ECB.

Steps

  • Replace the insecure encryption algorithm with a secure one. AES is a secure algorithm but the mode of operation (ECB) is insecure. Use a secure mode like CBC or GCM.
  • Use a secure method to generate the encryption key instead of hardcoding it in the code. You can use a key derivation function like PBKDF2, bcrypt or scrypt.
  • Use a random initialization vector (IV) for each encryption operation and include it with the encrypted data. The IV doesn't need to be secret, but it should be unpredictable and unique for each encryption operation.
  • Ensure that the encrypted data is properly encoded before sending it in the response. You can use base64 encoding to ensure that the data remains intact without any modification during transmission.

Compliant code

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:cipher/cipher.dart';
import 'package:cipher/impl/base.dart';
import 'package:shelf/shelf.dart';
import 'package:pointycastle/api.dart';
import 'package:pointycastle/block/aes_fast.dart';
import 'package:pointycastle/block/modes/cbc.dart';
import 'package:pointycastle/paddings/pkcs7.dart';
import 'package:pointycastle/key_derivators/pbkdf2.dart';
import 'package:pointycastle/random/fortuna_random.dart';

void main() {
final server = HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
server.listen((HttpRequest request) async {
var password = "SecurePassword";
var salt = createUint8ListFromString("Salt");
var pkcs = new KeyDerivator("SHA-1/HMAC/PBKDF2");
var params = new Pbkdf2Parameters(salt, 1000, 16);
pkcs.init(params);
var key = pkcs.process(createUint8ListFromString(password));

var iv = createUint8ListFromString("InitializationVector");
var aes = new AESFastEngine();
var cbc = new CBCBlockCipher(aes);
var cipher = new PaddedBlockCipherImpl(new PKCS7Padding(), cbc);
cipher.init(true, new ParametersWithIV<KeyParameter>(new KeyParameter(key), iv));

var data = utf8.encode("Sensitive information");
var encData = cipher.process(data);

var response = Response.ok(base64Encode(encData));
await request.response.addStream(response.read());
});
}

Uint8List createUint8ListFromString(String value) {
return new Uint8List.fromList(utf8.encode(value));
}

The updated code now uses a secure encryption algorithm and mode of operation. The AES algorithm is used with CBC mode, which is more secure than the previously used ECB mode.

The encryption key is no longer hardcoded in the code. Instead, a key derivation function (PBKDF2) is used to generate the key from a password and a salt. The salt is a random string that is used to ensure that the same password will result in different keys. The PBKDF2 function also takes a parameter for the number of iterations, which can be increased to make the key derivation process more computationally intensive and thus more secure against brute-force attacks.

An initialization vector (IV) is used for each encryption operation. The IV is a random string that is used to ensure that the same data will result in different encrypted output each time it is encrypted. This makes it more difficult for an attacker to find patterns in the encrypted data.

Finally, the encrypted data is encoded using base64 encoding before it is sent in the response. This ensures that the data remains intact without any modification during transmission.

References