use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class IESEngine 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 BaseKDFBytesGenerator method init.
public void init(DerivationParameters param) {
if (param instanceof KDFParameters) {
KDFParameters p = (KDFParameters) param;
shared = p.getSharedSecret();
iv = p.getIV();
} else if (param instanceof ISO18033KDFParameters) {
ISO18033KDFParameters p = (ISO18033KDFParameters) param;
shared = p.getSeed();
iv = null;
} else {
throw new IllegalArgumentException("KDF parameters required for generator");
}
}
use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class ConcatenationKDFGenerator method init.
public void init(DerivationParameters param) {
if (param instanceof KDFParameters) {
KDFParameters p = (KDFParameters) param;
shared = p.getSharedSecret();
otherInfo = p.getIV();
} else {
throw new IllegalArgumentException("KDF parameters required for generator");
}
}
use of com.github.zhenwei.core.crypto.params.KDFParameters in project LinLong-Java by zhenwei1108.
the class ECDHKEKGenerator method generateBytes.
public int generateBytes(byte[] out, int outOff, int len) throws DataLengthException, IllegalArgumentException {
if (outOff + len > out.length) {
throw new DataLengthException("output buffer too small");
}
// TODO Create an ASN.1 class for this (RFC3278)
// ECC-CMS-SharedInfo
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(new AlgorithmIdentifier(algorithm, DERNull.INSTANCE));
v.add(new DERTaggedObject(true, 2, new DEROctetString(Pack.intToBigEndian(keySize))));
try {
kdf.init(new KDFParameters(z, new DERSequence(v).getEncoded(ASN1Encoding.DER)));
} catch (IOException e) {
throw new IllegalArgumentException("unable to initialise kdf: " + e.getMessage());
}
return kdf.generateBytes(out, outOff, len);
}
Aggregations