Search in sources :

Example 1 with HKDFv3

use of org.whispersystems.libsignal.kdf.HKDFv3 in project Signal-Android by WhisperSystems.

the class AttachmentCipherInputStream method createForStickerData.

public static InputStream createForStickerData(byte[] data, byte[] packKey) throws InvalidMessageException, IOException {
    try {
        byte[] combinedKeyMaterial = new HKDFv3().deriveSecrets(packKey, "Sticker Pack".getBytes(), 64);
        byte[][] parts = Util.split(combinedKeyMaterial, CIPHER_KEY_SIZE, MAC_KEY_SIZE);
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(parts[1], "HmacSHA256"));
        if (data.length <= BLOCK_SIZE + mac.getMacLength()) {
            throw new InvalidMessageException("Message shorter than crypto overhead!");
        }
        try (InputStream inputStream = new ByteArrayInputStream(data)) {
            verifyMac(inputStream, data.length, mac, null);
        }
        return new AttachmentCipherInputStream(new ByteArrayInputStream(data), parts[0], data.length - BLOCK_SIZE - mac.getMacLength());
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new AssertionError(e);
    } catch (InvalidMacException e) {
        throw new InvalidMessageException(e);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ContentLengthInputStream(org.whispersystems.signalservice.internal.util.ContentLengthInputStream) FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) ByteArrayInputStream(java.io.ByteArrayInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) InvalidMacException(org.whispersystems.libsignal.InvalidMacException) HKDFv3(org.whispersystems.libsignal.kdf.HKDFv3)

Example 2 with HKDFv3

use of org.whispersystems.libsignal.kdf.HKDFv3 in project libsignal-service-java by signalapp.

the class ProvisioningCipher method encrypt.

public byte[] encrypt(ProvisionMessage message) throws InvalidKeyException {
    ECKeyPair ourKeyPair = Curve.generateKeyPair();
    byte[] sharedSecret = Curve.calculateAgreement(theirPublicKey, ourKeyPair.getPrivateKey());
    byte[] derivedSecret = new HKDFv3().deriveSecrets(sharedSecret, "TextSecure Provisioning Message".getBytes(), 64);
    byte[][] parts = Util.split(derivedSecret, 32, 32);
    byte[] version = { 0x01 };
    byte[] ciphertext = getCiphertext(parts[0], message.toByteArray());
    byte[] mac = getMac(parts[1], Util.join(version, ciphertext));
    byte[] body = Util.join(version, ciphertext, mac);
    return ProvisionEnvelope.newBuilder().setPublicKey(ByteString.copyFrom(ourKeyPair.getPublicKey().serialize())).setBody(ByteString.copyFrom(body)).build().toByteArray();
}
Also used : ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) HKDFv3(org.whispersystems.libsignal.kdf.HKDFv3)

Aggregations

HKDFv3 (org.whispersystems.libsignal.kdf.HKDFv3)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FilterInputStream (java.io.FilterInputStream)1 InputStream (java.io.InputStream)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 InvalidMacException (org.whispersystems.libsignal.InvalidMacException)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)1 ContentLengthInputStream (org.whispersystems.signalservice.internal.util.ContentLengthInputStream)1