Search in sources :

Example 1 with MacOutputStream

use of org.bouncycastle.crypto.io.MacOutputStream in project robovm by robovm.

the class BcKeyStoreSpi method engineStore.

public void engineStore(OutputStream stream, char[] password) throws IOException {
    DataOutputStream dOut = new DataOutputStream(stream);
    byte[] salt = new byte[STORE_SALT_SIZE];
    int iterationCount = MIN_ITERATIONS + (random.nextInt() & 0x3ff);
    random.nextBytes(salt);
    dOut.writeInt(version);
    dOut.writeInt(salt.length);
    dOut.write(salt);
    dOut.writeInt(iterationCount);
    HMac hMac = new HMac(new SHA1Digest());
    MacOutputStream mOut = new MacOutputStream(hMac);
    PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new SHA1Digest());
    byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
    pbeGen.init(passKey, salt, iterationCount);
    if (version < 2) {
        hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize()));
    } else {
        hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize() * 8));
    }
    for (int i = 0; i != passKey.length; i++) {
        passKey[i] = 0;
    }
    saveStore(new TeeOutputStream(dOut, mOut));
    byte[] mac = new byte[hMac.getMacSize()];
    hMac.doFinal(mac, 0);
    dOut.write(mac);
    dOut.close();
}
Also used : TeeOutputStream(org.bouncycastle.util.io.TeeOutputStream) PKCS12ParametersGenerator(org.bouncycastle.crypto.generators.PKCS12ParametersGenerator) DataOutputStream(java.io.DataOutputStream) HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) MacOutputStream(org.bouncycastle.crypto.io.MacOutputStream) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Example 2 with MacOutputStream

use of org.bouncycastle.crypto.io.MacOutputStream in project XobotOS by xamarin.

the class JDKKeyStore method engineStore.

public void engineStore(OutputStream stream, char[] password) throws IOException {
    DataOutputStream dOut = new DataOutputStream(stream);
    byte[] salt = new byte[STORE_SALT_SIZE];
    int iterationCount = MIN_ITERATIONS + (random.nextInt() & 0x3ff);
    random.nextBytes(salt);
    dOut.writeInt(STORE_VERSION);
    dOut.writeInt(salt.length);
    dOut.write(salt);
    dOut.writeInt(iterationCount);
    // BEGIN android-changed
    HMac hMac = new HMac(new OpenSSLDigest.SHA1());
    MacOutputStream mOut = new MacOutputStream(dOut, hMac);
    PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new OpenSSLDigest.SHA1());
    // END android-changed
    byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
    pbeGen.init(passKey, salt, iterationCount);
    hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize()));
    for (int i = 0; i != passKey.length; i++) {
        passKey[i] = 0;
    }
    saveStore(mOut);
    byte[] mac = new byte[hMac.getMacSize()];
    hMac.doFinal(mac, 0);
    dOut.write(mac);
    dOut.close();
}
Also used : PKCS12ParametersGenerator(org.bouncycastle.crypto.generators.PKCS12ParametersGenerator) DataOutputStream(java.io.DataOutputStream) HMac(org.bouncycastle.crypto.macs.HMac) MacOutputStream(org.bouncycastle.crypto.io.MacOutputStream) OpenSSLDigest(org.bouncycastle.crypto.digests.OpenSSLDigest) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Aggregations

DataOutputStream (java.io.DataOutputStream)2 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)2 PKCS12ParametersGenerator (org.bouncycastle.crypto.generators.PKCS12ParametersGenerator)2 MacOutputStream (org.bouncycastle.crypto.io.MacOutputStream)2 HMac (org.bouncycastle.crypto.macs.HMac)2 OpenSSLDigest (org.bouncycastle.crypto.digests.OpenSSLDigest)1 SHA1Digest (org.bouncycastle.crypto.digests.SHA1Digest)1 TeeOutputStream (org.bouncycastle.util.io.TeeOutputStream)1