Search in sources :

Example 1 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest in project LinLong-Java by zhenwei1108.

the class EthereumIESEngine method encryptBlock.

private byte[] encryptBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
    byte[] C = null, K = null, K1 = null, K2 = null;
    int len;
    if (cipher == null) {
        // Streaming mode.
        K1 = new byte[inLen];
        K2 = new byte[param.getMacKeySize() / 8];
        K = new byte[K1.length + K2.length];
        kdf.generateBytes(K, 0, K.length);
        if (V.length != 0) {
            System.arraycopy(K, 0, K2, 0, K2.length);
            System.arraycopy(K, K2.length, K1, 0, K1.length);
        } else {
            System.arraycopy(K, 0, K1, 0, K1.length);
            System.arraycopy(K, inLen, K2, 0, K2.length);
        }
        C = new byte[inLen];
        for (int i = 0; i != inLen; i++) {
            C[i] = (byte) (in[inOff + i] ^ K1[i]);
        }
        len = inLen;
    } else {
        // Block cipher mode.
        K1 = new byte[((IESWithCipherParameters) param).getCipherKeySize() / 8];
        K2 = new byte[param.getMacKeySize() / 8];
        K = new byte[K1.length + K2.length];
        kdf.generateBytes(K, 0, K.length);
        System.arraycopy(K, 0, K1, 0, K1.length);
        System.arraycopy(K, K1.length, K2, 0, K2.length);
        // If iv provided use it to initialise the cipher
        if (IV != null) {
            cipher.init(true, new ParametersWithIV(new KeyParameter(K1), IV));
        } else {
            cipher.init(true, new KeyParameter(K1));
        }
        C = new byte[cipher.getOutputSize(inLen)];
        len = cipher.processBytes(in, inOff, inLen, C, 0);
        len += cipher.doFinal(C, len);
    }
    // Convert the length of the encoding vector into a byte array.
    byte[] P2 = param.getEncodingV();
    byte[] L2 = null;
    if (V.length != 0) {
        L2 = getLengthTag(P2);
    }
    // Apply the MAC.
    byte[] T = new byte[mac.getMacSize()];
    // Ethereum change:
    // Instead of initializing the mac with the bytes, we initialize with the hash of the bytes.
    // Old code: mac.init(new KeyParameter(K2));
    Digest hash = new SHA256Digest();
    byte[] K2hash = new byte[hash.getDigestSize()];
    hash.reset();
    hash.update(K2, 0, K2.length);
    hash.doFinal(K2hash, 0);
    mac.init(new KeyParameter(K2hash));
    // we also update the mac with the IV:
    mac.update(IV, 0, IV.length);
    // end of Ethereum change.
    mac.update(C, 0, C.length);
    if (P2 != null) {
        mac.update(P2, 0, P2.length);
    }
    if (V.length != 0) {
        mac.update(L2, 0, L2.length);
    }
    // Ethereum change
    mac.update(commonMac, 0, commonMac.length);
    mac.doFinal(T, 0);
    // Output the triple (V,C,T).
    byte[] Output = new byte[V.length + len + T.length];
    System.arraycopy(V, 0, Output, 0, V.length);
    System.arraycopy(C, 0, Output, V.length, len);
    System.arraycopy(T, 0, Output, V.length + len, T.length);
    return Output;
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) Digest(com.github.zhenwei.core.crypto.Digest) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters)

Example 2 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest in project LinLong-Java by zhenwei1108.

the class AlgorithmParameterGeneratorSpi method engineGenerateParameters.

protected AlgorithmParameters engineGenerateParameters() {
    DSAParametersGenerator pGen;
    if (strength <= 1024) {
        pGen = new DSAParametersGenerator();
    } else {
        pGen = new DSAParametersGenerator(new SHA256Digest());
    }
    if (random == null) {
        random = CryptoServicesRegistrar.getSecureRandom();
    }
    int certainty = PrimeCertaintyCalculator.getDefaultCertainty(strength);
    if (strength == 1024) {
        params = new DSAParameterGenerationParameters(1024, 160, certainty, random);
        pGen.init(params);
    } else if (strength > 1024) {
        params = new DSAParameterGenerationParameters(strength, 256, certainty, random);
        pGen.init(params);
    } else {
        pGen.init(strength, certainty, random);
    }
    DSAParameters p = pGen.generateParameters();
    AlgorithmParameters params;
    try {
        params = createParametersInstance("DSA");
        params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return params;
}
Also used : DSAParametersGenerator(com.github.zhenwei.core.crypto.generators.DSAParametersGenerator) DSAParameterSpec(java.security.spec.DSAParameterSpec) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) DSAParameterGenerationParameters(com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterException(java.security.InvalidParameterException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 3 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest in project LinLong-Java by zhenwei1108.

the class JPAKEExample method main.

public static void main(String[] args) throws CryptoException {
    /*
     * Initialization
     *
     * Pick an appropriate prime order group to use throughout the exchange.
     * Note that both participants must use the same group.
     */
    JPAKEPrimeOrderGroup group = JPAKEPrimeOrderGroups.NIST_3072;
    BigInteger p = group.getP();
    BigInteger q = group.getQ();
    BigInteger g = group.getG();
    String alicePassword = "password";
    String bobPassword = "password";
    System.out.println("********* Initialization **********");
    System.out.println("Public parameters for the cyclic group:");
    System.out.println("p (" + p.bitLength() + " bits): " + p.toString(16));
    System.out.println("q (" + q.bitLength() + " bits): " + q.toString(16));
    System.out.println("g (" + p.bitLength() + " bits): " + g.toString(16));
    System.out.println("p mod q = " + p.mod(q).toString(16));
    System.out.println("g^{q} mod p = " + g.modPow(q, p).toString(16));
    System.out.println("");
    System.out.println("(Secret passwords used by Alice and Bob: " + "\"" + alicePassword + "\" and \"" + bobPassword + "\")\n");
    /*
     * Both participants must use the same hashing algorithm.
     */
    Digest digest = new SHA256Digest();
    SecureRandom random = new SecureRandom();
    JPAKEParticipant alice = new JPAKEParticipant("alice", alicePassword.toCharArray(), group, digest, random);
    JPAKEParticipant bob = new JPAKEParticipant("bob", bobPassword.toCharArray(), group, digest, random);
    /*
     * Round 1
     *
     * Alice and Bob each generate a round 1 payload, and send it to each other.
     */
    JPAKERound1Payload aliceRound1Payload = alice.createRound1PayloadToSend();
    JPAKERound1Payload bobRound1Payload = bob.createRound1PayloadToSend();
    System.out.println("************ Round 1 **************");
    System.out.println("Alice sends to Bob: ");
    System.out.println("g^{x1}=" + aliceRound1Payload.getGx1().toString(16));
    System.out.println("g^{x2}=" + aliceRound1Payload.getGx2().toString(16));
    System.out.println("KP{x1}={" + aliceRound1Payload.getKnowledgeProofForX1()[0].toString(16) + "};{" + aliceRound1Payload.getKnowledgeProofForX1()[1].toString(16) + "}");
    System.out.println("KP{x2}={" + aliceRound1Payload.getKnowledgeProofForX2()[0].toString(16) + "};{" + aliceRound1Payload.getKnowledgeProofForX2()[1].toString(16) + "}");
    System.out.println("");
    System.out.println("Bob sends to Alice: ");
    System.out.println("g^{x3}=" + bobRound1Payload.getGx1().toString(16));
    System.out.println("g^{x4}=" + bobRound1Payload.getGx2().toString(16));
    System.out.println("KP{x3}={" + bobRound1Payload.getKnowledgeProofForX1()[0].toString(16) + "};{" + bobRound1Payload.getKnowledgeProofForX1()[1].toString(16) + "}");
    System.out.println("KP{x4}={" + bobRound1Payload.getKnowledgeProofForX2()[0].toString(16) + "};{" + bobRound1Payload.getKnowledgeProofForX2()[1].toString(16) + "}");
    System.out.println("");
    /*
     * Each participant must then validate the received payload for round 1
     */
    alice.validateRound1PayloadReceived(bobRound1Payload);
    System.out.println("Alice checks g^{x4}!=1: OK");
    System.out.println("Alice checks KP{x3}: OK");
    System.out.println("Alice checks KP{x4}: OK");
    System.out.println("");
    bob.validateRound1PayloadReceived(aliceRound1Payload);
    System.out.println("Bob checks g^{x2}!=1: OK");
    System.out.println("Bob checks KP{x1},: OK");
    System.out.println("Bob checks KP{x2},: OK");
    System.out.println("");
    /*
     * Round 2
     *
     * Alice and Bob each generate a round 2 payload, and send it to each other.
     */
    JPAKERound2Payload aliceRound2Payload = alice.createRound2PayloadToSend();
    JPAKERound2Payload bobRound2Payload = bob.createRound2PayloadToSend();
    System.out.println("************ Round 2 **************");
    System.out.println("Alice sends to Bob: ");
    System.out.println("A=" + aliceRound2Payload.getA().toString(16));
    System.out.println("KP{x2*s}={" + aliceRound2Payload.getKnowledgeProofForX2s()[0].toString(16) + "},{" + aliceRound2Payload.getKnowledgeProofForX2s()[1].toString(16) + "}");
    System.out.println("");
    System.out.println("Bob sends to Alice");
    System.out.println("B=" + bobRound2Payload.getA().toString(16));
    System.out.println("KP{x4*s}={" + bobRound2Payload.getKnowledgeProofForX2s()[0].toString(16) + "},{" + bobRound2Payload.getKnowledgeProofForX2s()[1].toString(16) + "}");
    System.out.println("");
    /*
     * Each participant must then validate the received payload for round 2
     */
    alice.validateRound2PayloadReceived(bobRound2Payload);
    System.out.println("Alice checks KP{x4*s}: OK\n");
    bob.validateRound2PayloadReceived(aliceRound2Payload);
    System.out.println("Bob checks KP{x2*s}: OK\n");
    /*
     * After round 2, each participant computes the keying material.
     */
    BigInteger aliceKeyingMaterial = alice.calculateKeyingMaterial();
    BigInteger bobKeyingMaterial = bob.calculateKeyingMaterial();
    System.out.println("********* After round 2 ***********");
    System.out.println("Alice computes key material \t K=" + aliceKeyingMaterial.toString(16));
    System.out.println("Bob computes key material \t K=" + bobKeyingMaterial.toString(16));
    System.out.println();
    /*
     * You must derive a session key from the keying material applicable
     * to whatever encryption algorithm you want to use.
     */
    BigInteger aliceKey = deriveSessionKey(aliceKeyingMaterial);
    BigInteger bobKey = deriveSessionKey(bobKeyingMaterial);
    /*
     * At this point, you can stop and use the session keys if you want.
     * This is implicit key confirmation.
     *
     * If you want to explicitly confirm that the key material matches,
     * you can continue on and perform round 3.
     */
    /*
     * Round 3
     *
     * Alice and Bob each generate a round 3 payload, and send it to each other.
     */
    JPAKERound3Payload aliceRound3Payload = alice.createRound3PayloadToSend(aliceKeyingMaterial);
    JPAKERound3Payload bobRound3Payload = bob.createRound3PayloadToSend(bobKeyingMaterial);
    System.out.println("************ Round 3 **************");
    System.out.println("Alice sends to Bob: ");
    System.out.println("MacTag=" + aliceRound3Payload.getMacTag().toString(16));
    System.out.println("");
    System.out.println("Bob sends to Alice: ");
    System.out.println("MacTag=" + bobRound3Payload.getMacTag().toString(16));
    System.out.println("");
    /*
     * Each participant must then validate the received payload for round 3
     */
    alice.validateRound3PayloadReceived(bobRound3Payload, aliceKeyingMaterial);
    System.out.println("Alice checks MacTag: OK\n");
    bob.validateRound3PayloadReceived(aliceRound3Payload, bobKeyingMaterial);
    System.out.println("Bob checks MacTag: OK\n");
    System.out.println();
    System.out.println("MacTags validated, therefore the keying material matches.");
}
Also used : JPAKEPrimeOrderGroup(com.github.zhenwei.core.crypto.agreement.jpake.JPAKEPrimeOrderGroup) JPAKERound1Payload(com.github.zhenwei.core.crypto.agreement.jpake.JPAKERound1Payload) JPAKERound2Payload(com.github.zhenwei.core.crypto.agreement.jpake.JPAKERound2Payload) Digest(com.github.zhenwei.core.crypto.Digest) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom) JPAKEParticipant(com.github.zhenwei.core.crypto.agreement.jpake.JPAKEParticipant) JPAKERound3Payload(com.github.zhenwei.core.crypto.agreement.jpake.JPAKERound3Payload)

Example 4 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest in project LinLong-Java by zhenwei1108.

the class CramerShoupParametersGenerator method generateParameters.

/**
 * which generates the p and g values from the given parameters, returning the
 * CramerShoupParameters object.
 * <p>
 * Note: can take a while...
 * </p>
 *
 * @return a generated CramerShoupParameters object.
 */
public CramerShoupParameters generateParameters() {
    // 
    // find a safe prime p where p = 2*q + 1, where p and q are prime.
    // 
    BigInteger[] safePrimes = ParametersHelper.generateSafePrimes(size, certainty, random);
    // BigInteger p = safePrimes[0];
    BigInteger q = safePrimes[1];
    BigInteger g1 = ParametersHelper.selectGenerator(q, random);
    BigInteger g2 = ParametersHelper.selectGenerator(q, random);
    while (g1.equals(g2)) {
        g2 = ParametersHelper.selectGenerator(q, random);
    }
    return new CramerShoupParameters(q, g1, g2, new SHA256Digest());
}
Also used : CramerShoupParameters(com.github.zhenwei.core.crypto.params.CramerShoupParameters) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger)

Example 5 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest in project LinLong-Java by zhenwei1108.

the class SCrypt method SingleIterationPBKDF2.

private static byte[] SingleIterationPBKDF2(byte[] P, byte[] S, int dkLen) {
    PBEParametersGenerator pGen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    pGen.init(P, S, 1);
    KeyParameter key = (KeyParameter) pGen.generateDerivedMacParameters(dkLen * 8);
    return key.getKey();
}
Also used : SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) PBEParametersGenerator(com.github.zhenwei.core.crypto.PBEParametersGenerator)

Aggregations

SHA256Digest (com.github.zhenwei.core.crypto.digests.SHA256Digest)13 SHA512Digest (com.github.zhenwei.core.crypto.digests.SHA512Digest)4 BigInteger (java.math.BigInteger)4 Digest (com.github.zhenwei.core.crypto.Digest)3 SHAKEDigest (com.github.zhenwei.core.crypto.digests.SHAKEDigest)3 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 ExtendedDigest (com.github.zhenwei.core.crypto.ExtendedDigest)2 SHA1Digest (com.github.zhenwei.core.crypto.digests.SHA1Digest)2 SHA224Digest (com.github.zhenwei.core.crypto.digests.SHA224Digest)2 SHA384Digest (com.github.zhenwei.core.crypto.digests.SHA384Digest)2 DSAParametersGenerator (com.github.zhenwei.core.crypto.generators.DSAParametersGenerator)2 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)2 CramerShoupParameters (com.github.zhenwei.core.crypto.params.CramerShoupParameters)2 DSAParameterGenerationParameters (com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters)2 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)2 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)2 HashMap (java.util.HashMap)2 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1