For Non-Cryptographers
Patrick Favre-Bulle
Lead Dev, Senacor 2019, Wien
- NFC Payment App (EMVCo)
- E2E encrypted Health Data Provider
Security Best Practices, ...
bcrypt, armadillo, hkdf, id-mask, dice, ...
Patrick Favre-Bulle
Symmetric Encryption
From Block Cipher to Authenticated Encryption
Cryptoanalysis
128 vs 256 bit security, How Secure is AES?
Crypto APIs
The unfortunate Story
| A | B | Out |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
0110 ^ 1100 = 1010 1010 ^ 1100 = 0110
128 bit = 16 byte = 3.4x10^38
256 bit = 32 byte = 1.15x10^77
16 byte = UUID = IPv6 Address
0x41 0x20 0x53 0x65 0x63 0x72 0x65 0x74 0x20 0x4d 0x65 0x73 0x73 0x61 0x67 0x65
A Secret Message
16 random bytes
| 0x41 | 0x20 | 0x53 | 0x65 |
| 0x63 | 0x72 | 0x65 | 0x74 |
| 0x20 | 0x4d | 0x65 | 0x73 |
| 0x73 | 0x61 | 0x67 | 0x65 |
9, 11 or 13 Rounds
AES Round
Block cipher mode of operation!
Block Cipher
Encryption
Ciphertext 1
Plaintext 1
Key
Block 1
Block Cipher
Encryption
Ciphertext 2
Plaintext 2
Key
Block 2
...
Padding!
Practically no security implication in the choice of padding in AES*
... 6F 1A 02 __ __ __ __
04 04 04 04
... 6F 1A 02 E5 __ __ __
03 03 03
Block Cipher
Encryption
Ciphertext 1
Plaintext 1
Key
Block 1
Initialization Vector (IV)
Block Cipher
Encryption
Ciphertext 2
Plaintext 2
Key
Block 2
+
+
* This property is depending on the block mode a recommentation or integral for security
CBC: data looks random
Block Cipher
Encryption
Ciphertext 1
Nonce
Key
Block 1
Plaintext 1
+
Counter
0000
Block Cipher
Encryption
Ciphertext 2
Nonce
Key
Block 2
Plaintext 2
+
Counter
0001
Unfortunately: Encryption does not protect against modification!
Known Attacks
Ciphertext
Auth Tag
Why then 128 bit, 192 bit and 256 bit Key Option?
AES is Broken!
In Cryptography broken does not necessarily means insecure.
Attacks have been published that are computationally faster than a full brute-force attack, though none as of 2013 are computationally feasible. For AES-128, the key can be recovered with a computational complexity of 2^126.1 using the biclique attack. (...) Related-key attacks can break (...) AES-256 with complexities 2^176 in both time and data, respectively.
Primitives
Protocol Developer
Opinionated Protocol User
e.g. Implementation of Block Cipher
e.g. Secure Channel implementation
e.g. changing TLS Certificate
most Crypto APIs
Higher Level
SecureRandom secureRandom = new SecureRandom();
byte[] key = new byte[16];
secureRandom.nextBytes(key);
SecretKey secretKey = new SecretKeySpec(key, "AES");byte[] iv = new byte[12]; //NEVER REUSE THIS IV WITH SAME KEY
secureRandom.nextBytes(iv);
final Cipher cipher =
Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec parameterSpec =
new GCMParameterSpec(128, iv); //128 bit auth tag length
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
if (associatedData != null) {
cipher.updateAAD(associatedData);
}byte[] cipherText = cipher.doFinal(plainText);
ByteBuffer byteBuffer =
ByteBuffer.allocate(iv.length + cipherText.length);
byteBuffer.put(iv);
byteBuffer.put(cipherText);
byte[] cipherMessage = byteBuffer.array();
//overwrite the content of key with zeros
Arrays.fill(key, (byte) 0); final Cipher cipher =
Cipher.getInstance("AES/GCM/NoPadding");
//use first 12 bytes for iv
AlgorithmParameterSpec gcmIv = new GCMParameterSpec(128, cipherMessage, 0, 12);
cipher.init(Cipher.DECRYPT_MODE, secretKey, gcmIv);
if (associatedData != null) {
cipher.updateAAD(associatedData);
}
//use everything from 12 bytes on as ciphertext
byte[] plainText
= cipher.doFinal(cipherMessage, 12, cipherMessage.length - 12);//THIS EXAMPLE IS UNSECURE - DO NOT USE
byte[] iv = new byte[12];
secureRandom.nextBytes(iv);
final Cipher cipher =
Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec parameterSpec =
new GCMParameterSpec(16, iv); //16 byte auth tag length
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte[] calculatedMac = ...
byte[] providedMac = ...
for(int i=0; i < calculatedMac.length; i++) {
if(calculatedMac[i] != providedMac[i]) {
return false;
}
}
return true;MessageDigest.isEqual(calculatedMac, providedMac); //JDK 6u10Hello Side Channel Attack!
Post your Qs in Slack