Search in sources :

Example 1 with HKDFParameters

use of org.bouncycastle.crypto.params.HKDFParameters in project habot by ghys.

the class HttpEce method hkdfExpand.

/**
 * Convenience method for computing the HMAC Key Derivation Function. The
 * real work is offloaded to BouncyCastle.
 */
protected static byte[] hkdfExpand(byte[] ikm, byte[] salt, byte[] info, int length) throws InvalidKeyException, NoSuchAlgorithmException {
    HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest());
    hkdf.init(new HKDFParameters(ikm, salt, info));
    byte[] okm = new byte[length];
    hkdf.generateBytes(okm, 0, length);
    return okm;
}
Also used : HKDFBytesGenerator(org.bouncycastle.crypto.generators.HKDFBytesGenerator) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters)

Example 2 with HKDFParameters

use of org.bouncycastle.crypto.params.HKDFParameters in project vsDiaryWriter by shilongdai.

the class StreamCipherTransformer method decryptData.

@Override
protected byte[] decryptData(String cData) {
    ByteParameterPair pair = ByteParameterPair.valueOf(cData);
    int keysize = StreamCipherEncryptors.INSTANCE.getKeySize(algName) / 8;
    int ivsize = StreamCipherEncryptors.INSTANCE.getIVSize(algName) / 8;
    byte[] keyCombo = new byte[keysize + ivsize];
    hkdf.init(new HKDFParameters(masterKey, pair.getFirst(), "Encryption Key".getBytes()));
    hkdf.generateBytes(keyCombo, 0, keyCombo.length);
    byte[] key = new byte[keysize];
    byte[] iv = new byte[ivsize];
    System.arraycopy(keyCombo, 0, key, 0, keysize);
    System.arraycopy(keyCombo, keysize, iv, 0, ivsize);
    StreamCipherEncryptor enc = StreamCipherEncryptors.INSTANCE.getEncryptor(algName);
    return enc.decrypt(pair.getSecond(), key, iv);
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) StreamCipherEncryptor(net.viperfish.journal.streamCipher.StreamCipherEncryptor)

Example 3 with HKDFParameters

use of org.bouncycastle.crypto.params.HKDFParameters in project vsDiaryWriter by shilongdai.

the class StreamCipherTransformer method setPassword.

@Override
public void setPassword(String pass) throws FailToSyncCipherDataException {
    if (salts == null) {
        byte[] macSalt = new byte[8];
        // left blank intentionally
        byte[] encSalt = new byte[8];
        rand.nextBytes(macSalt);
        salts = new ByteParameterPair(encSalt, macSalt);
        try {
            Files.write(additSalt.toPath(), salts.toString().getBytes(StandardCharsets.US_ASCII), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        } catch (IOException e) {
            FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot write salt to " + additSalt.getAbsolutePath() + " message:" + e.getMessage());
            fc.initCause(e);
            throw fc;
        }
    }
    masterKey = this.generateKey(pass);
    hkdf.init(new HKDFParameters(masterKey, salts.getSecond(), "Mac Key".getBytes()));
    byte[] macKey = new byte[this.getMacKeySize() / 8];
    hkdf.generateBytes(macKey, 0, macKey.length);
    this.initMac(macKey);
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) FailToSyncCipherDataException(net.viperfish.journal.framework.errors.FailToSyncCipherDataException) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) IOException(java.io.IOException)

Example 4 with HKDFParameters

use of org.bouncycastle.crypto.params.HKDFParameters in project vsDiaryWriter by shilongdai.

the class StreamCipherTransformer method encryptData.

@Override
protected String encryptData(byte[] bytes) {
    byte[] randomSource = new byte[16];
    rand.nextBytes(randomSource);
    int keysize = StreamCipherEncryptors.INSTANCE.getKeySize(algName) / 8;
    int ivsize = StreamCipherEncryptors.INSTANCE.getIVSize(algName) / 8;
    byte[] keyCombo = new byte[keysize + ivsize];
    hkdf.init(new HKDFParameters(masterKey, randomSource, "Encryption Key".getBytes()));
    hkdf.generateBytes(keyCombo, 0, keyCombo.length);
    byte[] key = new byte[keysize];
    byte[] iv = new byte[ivsize];
    System.arraycopy(keyCombo, 0, key, 0, keysize);
    System.arraycopy(keyCombo, keysize, iv, 0, ivsize);
    StreamCipherEncryptor enc = StreamCipherEncryptors.INSTANCE.getEncryptor(algName);
    byte[] ecrypted = enc.encrypt(bytes, key, iv);
    ByteParameterPair pair = new ByteParameterPair(randomSource, ecrypted);
    return pair.toString();
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) StreamCipherEncryptor(net.viperfish.journal.streamCipher.StreamCipherEncryptor)

Example 5 with HKDFParameters

use of org.bouncycastle.crypto.params.HKDFParameters in project vsDiaryWriter by shilongdai.

the class BlockCipherMacTransformer method setPassword.

/**
 * derive the key from the password
 *
 * @throws FailToSyncCipherDataException
 */
@Override
public void setPassword(String string) throws FailToSyncCipherDataException {
    try {
        String saltRaw = salts.read(StandardCharsets.US_ASCII);
        if (saltRaw.length() > 0) {
            saltPair = ByteParameterPair.valueOf(saltRaw);
        } else {
            byte[] encSalt = new byte[8];
            byte[] macSalt = new byte[8];
            rand.nextBytes(encSalt);
            rand.nextBytes(macSalt);
            saltPair = new ByteParameterPair(encSalt, macSalt);
            salts.write(saltPair.toString(), StandardCharsets.US_ASCII);
        }
    } catch (IOException e) {
        FailToSyncCipherDataException fc = new FailToSyncCipherDataException("Cannot load encryption and mac salt from file");
        fc.initCause(e);
        throw fc;
    }
    this.key = generateKey(string);
    hkdf.init(new HKDFParameters(key, saltPair.getFirst(), "Encryption Key".getBytes()));
    cryptKey = new byte[enc.getKeySize() / 8];
    hkdf.generateBytes(cryptKey, 0, cryptKey.length);
    byte[] macKey = new byte[getMacKeySize() / 8];
    hkdf.init(new HKDFParameters(key, saltPair.getSecond(), "Mac Key".getBytes()));
    hkdf.generateBytes(macKey, 0, macKey.length);
    initMac(macKey);
}
Also used : ByteParameterPair(net.viperfish.framework.ByteParameterPair) FailToSyncCipherDataException(net.viperfish.journal.framework.errors.FailToSyncCipherDataException) HKDFParameters(org.bouncycastle.crypto.params.HKDFParameters) IOException(java.io.IOException)

Aggregations

HKDFParameters (org.bouncycastle.crypto.params.HKDFParameters)7 ByteParameterPair (net.viperfish.framework.ByteParameterPair)4 HKDFBytesGenerator (org.bouncycastle.crypto.generators.HKDFBytesGenerator)3 IOException (java.io.IOException)2 FailToSyncCipherDataException (net.viperfish.journal.framework.errors.FailToSyncCipherDataException)2 StreamCipherEncryptor (net.viperfish.journal.streamCipher.StreamCipherEncryptor)2 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)2 MessageDigest (java.security.MessageDigest)1 Digest (org.bouncycastle.crypto.Digest)1