Search in sources :

Example 1 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter 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 KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class RC2Engine method init.

/**
 * initialise a RC2 cipher.
 *
 * @param encrypting whether or not we are for encryption.
 * @param params     the parameters required to set up the cipher.
 * @throws IllegalArgumentException if the params argument is inappropriate.
 */
public void init(boolean encrypting, CipherParameters params) {
    this.encrypting = encrypting;
    if (params instanceof RC2Parameters) {
        RC2Parameters param = (RC2Parameters) params;
        workingKey = generateWorkingKey(param.getKey(), param.getEffectiveKeyBits());
    } else if (params instanceof KeyParameter) {
        byte[] key = ((KeyParameter) params).getKey();
        workingKey = generateWorkingKey(key, key.length * 8);
    } else {
        throw new IllegalArgumentException("invalid parameter passed to RC2 init - " + params.getClass().getName());
    }
}
Also used : RC2Parameters(com.github.zhenwei.core.crypto.params.RC2Parameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 3 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class RC6Engine method init.

/**
 * initialise a RC5-32 cipher.
 *
 * @param forEncryption whether or not we are for encryption.
 * @param params        the parameters required to set up the cipher.
 * @throws IllegalArgumentException if the params argument is inappropriate.
 */
public void init(boolean forEncryption, CipherParameters params) {
    if (!(params instanceof KeyParameter)) {
        throw new IllegalArgumentException("invalid parameter passed to RC6 init - " + params.getClass().getName());
    }
    KeyParameter p = (KeyParameter) params;
    this.forEncryption = forEncryption;
    setKey(p.getKey());
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 4 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class Grain128Engine method init.

/**
 * Initialize a Grain-128 cipher.
 *
 * @param forEncryption Whether or not we are for encryption.
 * @param params        The parameters required to set up the cipher.
 * @throws IllegalArgumentException If the params argument is inappropriate.
 */
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    /**
     * Grain encryption and decryption is completely symmetrical, so the
     * 'forEncryption' is irrelevant.
     */
    if (!(params instanceof ParametersWithIV)) {
        throw new IllegalArgumentException("Grain-128 Init parameters must include an IV");
    }
    ParametersWithIV ivParams = (ParametersWithIV) params;
    byte[] iv = ivParams.getIV();
    if (iv == null || iv.length != 12) {
        throw new IllegalArgumentException("Grain-128  requires exactly 12 bytes of IV");
    }
    if (!(ivParams.getParameters() instanceof KeyParameter)) {
        throw new IllegalArgumentException("Grain-128 Init parameters must include a key");
    }
    KeyParameter key = (KeyParameter) ivParams.getParameters();
    /**
     * Initialize variables.
     */
    workingIV = new byte[key.getKey().length];
    workingKey = new byte[key.getKey().length];
    lfsr = new int[STATE_SIZE];
    nfsr = new int[STATE_SIZE];
    out = new byte[4];
    System.arraycopy(iv, 0, workingIV, 0, iv.length);
    System.arraycopy(key.getKey(), 0, workingKey, 0, key.getKey().length);
    reset();
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 5 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class JPAKEUtil method calculateMacTag.

/**
 * Calculates the MacTag (to be used for key confirmation), as defined by
 * <a href="https://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf">NIST
 * SP 800-56A Revision 1</a>,
 * Section 8.2 Unilateral Key Confirmation for Key Agreement Schemes.
 * <pre>
 * MacTag = HMAC(MacKey, MacLen, MacData)
 *
 * MacKey = H(K || "JPAKE_KC")
 *
 * MacData = "KC_1_U" || participantId || partnerParticipantId || gx1 || gx2 || gx3 || gx4
 *
 * Note that both participants use "KC_1_U" because the sender of the round 3 message
 * is always the initiator for key confirmation.
 *
 * HMAC = {@link HMac} used with the given {@link Digest}
 * H = The given {@link Digest}
 * MacLen = length of MacTag
 * </pre>
 */
public static BigInteger calculateMacTag(String participantId, String partnerParticipantId, BigInteger gx1, BigInteger gx2, BigInteger gx3, BigInteger gx4, BigInteger keyingMaterial, Digest digest) {
    byte[] macKey = calculateMacKey(keyingMaterial, digest);
    HMac mac = new HMac(digest);
    byte[] macOutput = new byte[mac.getMacSize()];
    mac.init(new KeyParameter(macKey));
    /*
     * MacData = "KC_1_U" || participantId_Alice || participantId_Bob || gx1 || gx2 || gx3 || gx4.
     */
    updateMac(mac, "KC_1_U");
    updateMac(mac, participantId);
    updateMac(mac, partnerParticipantId);
    updateMac(mac, gx1);
    updateMac(mac, gx2);
    updateMac(mac, gx3);
    updateMac(mac, gx4);
    mac.doFinal(macOutput, 0);
    Arrays.fill(macKey, (byte) 0);
    return new BigInteger(macOutput);
}
Also used : HMac(com.github.zhenwei.core.crypto.macs.HMac) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) BigInteger(java.math.BigInteger)

Aggregations

KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)91 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)41 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)15 AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)10 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)6 ParametersWithSBox (com.github.zhenwei.core.crypto.params.ParametersWithSBox)6 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)6 BigInteger (java.math.BigInteger)6 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)4 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)4 PKCS5S2ParametersGenerator (com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator)4 HMac (com.github.zhenwei.core.crypto.macs.HMac)4 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)4 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)4 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IvParameterSpec (javax.crypto.spec.IvParameterSpec)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 RC5Parameters (com.github.zhenwei.core.crypto.params.RC5Parameters)3 CMSException (com.github.zhenwei.pkix.cms.CMSException)3