use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class EthereumIESEngine method processBlock.
public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
if (forEncryption) {
if (keyPairGenerator != null) {
EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();
this.privParam = ephKeyPair.getKeyPair().getPrivate();
this.V = ephKeyPair.getEncodedPublicKey();
}
} else {
if (keyParser != null) {
ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);
try {
this.pubParam = keyParser.readKey(bIn);
} catch (IOException e) {
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
}
int encLength = (inLen - bIn.available());
this.V = Arrays.copyOfRange(in, inOff, inOff + encLength);
}
}
// Compute the common value and convert to byte array.
agree.init(privParam);
BigInteger z = agree.calculateAgreement(pubParam);
byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);
// Create input to KDF.
if (V.length != 0) {
byte[] VZ = Arrays.concatenate(V, Z);
Arrays.fill(Z, (byte) 0);
Z = VZ;
}
try {
// Initialise the KDF.
KDFParameters kdfParam = new KDFParameters(Z, param.getDerivationV());
kdf.init(kdfParam);
return forEncryption ? encryptBlock(in, inOff, inLen) : decryptBlock(in, inOff, inLen);
} finally {
Arrays.fill(Z, (byte) 0);
}
}
use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class ECIESKeyEncapsulation method deriveKey.
protected KeyParameter deriveKey(int keyLen, byte[] C, byte[] PEH) {
byte[] kdfInput = PEH;
if (!SingleHashMode) {
kdfInput = Arrays.concatenate(C, PEH);
Arrays.fill(PEH, (byte) 0);
}
try {
// Initialise the KDF
kdf.init(new KDFParameters(kdfInput, null));
// Generate the secret key
byte[] K = new byte[keyLen];
kdf.generateBytes(K, 0, K.length);
// Return the ciphertext
return new KeyParameter(K);
} finally {
Arrays.fill(kdfInput, (byte) 0);
}
}
use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class RSAKeyEncapsulation method generateKey.
protected KeyParameter generateKey(BigInteger n, BigInteger r, int keyLen) {
byte[] R = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, r);
// Initialise the KDF
kdf.init(new KDFParameters(R, null));
// Generate the secret key
byte[] K = new byte[keyLen];
kdf.generateBytes(K, 0, K.length);
return new KeyParameter(K);
}
use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class BrokenKDF2BytesGenerator method init.
public void init(DerivationParameters param) {
if (!(param instanceof KDFParameters)) {
throw new IllegalArgumentException("KDF parameters required for generator");
}
KDFParameters p = (KDFParameters) param;
shared = p.getSharedSecret();
iv = p.getIV();
}
use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class BaseAgreementSpi method getSharedSecretBytes.
private byte[] getSharedSecretBytes(byte[] secret, String oidAlgorithm, int keySize) throws NoSuchAlgorithmException {
if (kdf != null) {
if (keySize < 0) {
throw new NoSuchAlgorithmException("unknown algorithm encountered: " + oidAlgorithm);
}
byte[] keyBytes = new byte[keySize / 8];
if (kdf instanceof DHKEKGenerator) {
if (oidAlgorithm == null) {
throw new NoSuchAlgorithmException("algorithm OID is null");
}
ASN1ObjectIdentifier oid;
try {
oid = new ASN1ObjectIdentifier(oidAlgorithm);
} catch (IllegalArgumentException e) {
throw new NoSuchAlgorithmException("no OID for algorithm: " + oidAlgorithm);
}
DHKDFParameters params = new DHKDFParameters(oid, keySize, secret, ukmParameters);
kdf.init(params);
} else {
KDFParameters params = new KDFParameters(secret, ukmParameters);
kdf.init(params);
}
kdf.generateBytes(keyBytes, 0, keyBytes.length);
Arrays.clear(secret);
return keyBytes;
} else {
if (keySize > 0) {
byte[] keyBytes = new byte[keySize / 8];
System.arraycopy(secret, 0, keyBytes, 0, keyBytes.length);
Arrays.clear(secret);
return keyBytes;
}
return secret;
}
}
Aggregations