org.spongycastle.crypto.engines.AESEngine Java Examples

The following examples show how to use org.spongycastle.crypto.engines.AESEngine. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SecureUtils.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static byte[] encryptCCM(@NonNull final byte[] data,
                                @NonNull final byte[] key,
                                @NonNull final byte[] nonce,
                                final int micSize) {
    final byte[] ccm = new byte[data.length + micSize];

    final CCMBlockCipher ccmBlockCipher = new CCMBlockCipher(new AESEngine());
    final AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key), micSize * 8, nonce);
    ccmBlockCipher.init(true, aeadParameters);
    ccmBlockCipher.processBytes(data, 0, data.length, ccm, data.length);
    try {
        ccmBlockCipher.doFinal(ccm, 0);
        return ccm;
    } catch (InvalidCipherTextException e) {
        Log.e(TAG, "Error wile encrypting: " + e.getMessage());
        return null;
    }
}
 
Example #2
Source File: SecureUtils.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static byte[] encryptCCM(@NonNull final byte[] data,
                                @NonNull final byte[] key,
                                @NonNull final byte[] nonce,
                                @NonNull final byte[] additionalData,
                                final int micSize) {
    final byte[] ccm = new byte[data.length + micSize];

    final CCMBlockCipher ccmBlockCipher = new CCMBlockCipher(new AESEngine());
    final AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key), micSize * 8, nonce, additionalData);
    ccmBlockCipher.init(true, aeadParameters);
    ccmBlockCipher.processBytes(data, 0, data.length, ccm, data.length);
    try {
        ccmBlockCipher.doFinal(ccm, 0);
        return ccm;
    } catch (InvalidCipherTextException e) {
        Log.e(TAG, "Error wile encrypting: " + e.getMessage());
        return null;
    }
}
 
Example #3
Source File: Encryption.java    From KeePassJava2 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a final key from the parameters passed
 */
public static byte[] getFinalKeyDigest(byte[] key, byte[] masterSeed, byte[] transformSeed, long transformRounds) {

    AESEngine engine = new AESEngine();
    engine.init(true, new KeyParameter(transformSeed));

    // copy input key
    byte[] transformedKey = new byte[key.length];
    System.arraycopy(key, 0, transformedKey, 0, transformedKey.length);

    // transform rounds times
    for (long rounds = 0; rounds < transformRounds; rounds++) {
        engine.processBlock(transformedKey, 0, transformedKey, 0);
        engine.processBlock(transformedKey, 16, transformedKey, 16);
    }

    MessageDigest md = getMessageDigestInstance();
    byte[] transformedKeyDigest = md.digest(transformedKey);

    md.update(masterSeed);
    return md.digest(transformedKeyDigest);
}
 
Example #4
Source File: SecureUtils.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static byte[] calculateCMAC(final byte[] data, final byte[] key) {
    final byte[] cmac = new byte[16];

    CipherParameters cipherParameters = new KeyParameter(key);
    BlockCipher blockCipher = new AESEngine();
    CMac mac = new CMac(blockCipher);

    mac.init(cipherParameters);
    mac.update(data, 0, data.length);
    mac.doFinal(cmac, 0);
    return cmac;
}
 
Example #5
Source File: SecureUtils.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static byte[] decryptCCM(@NonNull final byte[] data,
                                @NonNull final byte[] key,
                                @NonNull final byte[] nonce,
                                final int micSize) throws InvalidCipherTextException {
    final byte[] ccm = new byte[data.length - micSize];

    final CCMBlockCipher ccmBlockCipher = new CCMBlockCipher(new AESEngine());
    final AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key), micSize * 8, nonce);
    ccmBlockCipher.init(false, aeadParameters);
    ccmBlockCipher.processBytes(data, 0, data.length, ccm, 0);
    ccmBlockCipher.doFinal(ccm, 0);
    return ccm;
}
 
Example #6
Source File: SecureUtils.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static byte[] decryptCCM(@NonNull final byte[] data,
                                @NonNull final byte[] key,
                                @NonNull final byte[] nonce,
                                @NonNull final byte[] additionalData,
                                final int micSize) throws InvalidCipherTextException {
    final byte[] ccm = new byte[data.length - micSize];

    final CCMBlockCipher ccmBlockCipher = new CCMBlockCipher(new AESEngine());
    final AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key), micSize * 8, nonce, additionalData);
    ccmBlockCipher.init(false, aeadParameters);
    ccmBlockCipher.processBytes(data, 0, data.length, ccm, 0);
    ccmBlockCipher.doFinal(ccm, 0);
    return ccm;
}
 
Example #7
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {

    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (!(privKey instanceof BCECPrivateKey)) {
        throw new UnsupportedOperationException("Cannot use the private " +
                "key as an AES key");
    }

    AESEngine engine = new AESEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);

    KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray((
            (BCECPrivateKey) privKey).getD()));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

    ctrEngine.init(false, params);

    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize()) {
            break;
        }
    }

    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }

    return out;
}
 
Example #8
Source File: IoUtil.java    From OpenYOLO-Android with Apache License 2.0 5 votes vote down vote up
static PaddedBufferedBlockCipher createAes128CtrPkcs7PaddingCipher(
        boolean encrypting,
        byte[] iv,
        byte[] key) {
    AESEngine aes = new AESEngine();
    SICBlockCipher aesCtr = new SICBlockCipher(aes);
    PaddedBufferedBlockCipher aesCtrPkcs7 =
            new PaddedBufferedBlockCipher(aesCtr, new PKCS7Padding());
    aesCtrPkcs7.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv));

    return aesCtrPkcs7;
}