From eda393b11c795a0d4b71774ea4c1b49a3485e452 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 25 Jul 2014 16:57:59 -0700 Subject: [PATCH] Minor refactoring and renaming. --- .../libaxolotl/kdf/DerivedMessageSecrets.java | 24 +++++++------------ .../libaxolotl/ratchet/RootKey.java | 4 ++-- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/libaxolotl/src/main/java/org/whispersystems/libaxolotl/kdf/DerivedMessageSecrets.java b/libaxolotl/src/main/java/org/whispersystems/libaxolotl/kdf/DerivedMessageSecrets.java index f9d8a91918..f5f782d7ea 100644 --- a/libaxolotl/src/main/java/org/whispersystems/libaxolotl/kdf/DerivedMessageSecrets.java +++ b/libaxolotl/src/main/java/org/whispersystems/libaxolotl/kdf/DerivedMessageSecrets.java @@ -17,32 +17,24 @@ package org.whispersystems.libaxolotl.kdf; +import org.whispersystems.libaxolotl.util.ByteUtil; + import javax.crypto.spec.SecretKeySpec; public class DerivedMessageSecrets { - public static final int SIZE = 64; - private static final int CIPHER_KEYS_OFFSET = 0; - private static final int MAC_KEYS_OFFSET = 32; + public static final int SIZE = 64; + private static final int CIPHER_KEY_LENGTH = 32; + private static final int MAC_KEY_LENGTH = 32; private final SecretKeySpec cipherKey; private final SecretKeySpec macKey; public DerivedMessageSecrets(byte[] okm) { - this.cipherKey = deriveCipherKey(okm); - this.macKey = deriveMacKey(okm); - } - - private SecretKeySpec deriveCipherKey(byte[] okm) { - byte[] cipherKey = new byte[32]; - System.arraycopy(okm, CIPHER_KEYS_OFFSET, cipherKey, 0, cipherKey.length); - return new SecretKeySpec(cipherKey, "AES"); - } + byte[][] keys = ByteUtil.split(okm, CIPHER_KEY_LENGTH, MAC_KEY_LENGTH); - private SecretKeySpec deriveMacKey(byte[] okm) { - byte[] macKey = new byte[32]; - System.arraycopy(okm, MAC_KEYS_OFFSET, macKey, 0, macKey.length); - return new SecretKeySpec(macKey, "HmacSHA256"); + this.cipherKey = new SecretKeySpec(keys[0], "AES"); + this.macKey = new SecretKeySpec(keys[1], "HmacSHA256"); } public SecretKeySpec getCipherKey() { diff --git a/libaxolotl/src/main/java/org/whispersystems/libaxolotl/ratchet/RootKey.java b/libaxolotl/src/main/java/org/whispersystems/libaxolotl/ratchet/RootKey.java index 638acfb361..39f3a831ca 100644 --- a/libaxolotl/src/main/java/org/whispersystems/libaxolotl/ratchet/RootKey.java +++ b/libaxolotl/src/main/java/org/whispersystems/libaxolotl/ratchet/RootKey.java @@ -39,10 +39,10 @@ public class RootKey { return key; } - public Pair createChain(ECPublicKey theirEphemeral, ECKeyPair ourEphemeral) + public Pair createChain(ECPublicKey theirRatchetKey, ECKeyPair ourRatchetKey) throws InvalidKeyException { - byte[] sharedSecret = Curve.calculateAgreement(theirEphemeral, ourEphemeral.getPrivateKey()); + byte[] sharedSecret = Curve.calculateAgreement(theirRatchetKey, ourRatchetKey.getPrivateKey()); byte[] derivedSecretBytes = kdf.deriveSecrets(sharedSecret, key, "WhisperRatchet".getBytes(), DerivedRootSecrets.SIZE); DerivedRootSecrets derivedSecrets = new DerivedRootSecrets(derivedSecretBytes);