use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class GOST3411Digest method E.
// Encrypt function, ECB mode
private void E(byte[] key, byte[] s, int sOff, byte[] in, int inOff) {
cipher.init(true, new KeyParameter(key));
cipher.processBlock(in, inOff, s, sOff);
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class HMacDSAKCalculator method init.
public void init(BigInteger n, BigInteger d, byte[] message) {
this.n = n;
Arrays.fill(V, (byte) 0x01);
Arrays.fill(K, (byte) 0);
int size = BigIntegers.getUnsignedByteLength(n);
byte[] x = new byte[size];
byte[] dVal = BigIntegers.asUnsignedByteArray(d);
System.arraycopy(dVal, 0, x, x.length - dVal.length, dVal.length);
byte[] m = new byte[size];
BigInteger mInt = bitsToInt(message);
if (mInt.compareTo(n) >= 0) {
mInt = mInt.subtract(n);
}
byte[] mVal = BigIntegers.asUnsignedByteArray(mInt);
System.arraycopy(mVal, 0, m, m.length - mVal.length, mVal.length);
hMac.init(new KeyParameter(K));
hMac.update(V, 0, V.length);
hMac.update((byte) 0x00);
hMac.update(x, 0, x.length);
hMac.update(m, 0, m.length);
hMac.doFinal(K, 0);
hMac.init(new KeyParameter(K));
hMac.update(V, 0, V.length);
hMac.doFinal(V, 0);
hMac.update(V, 0, V.length);
hMac.update((byte) 0x01);
hMac.update(x, 0, x.length);
hMac.update(m, 0, m.length);
hMac.doFinal(K, 0);
hMac.init(new KeyParameter(K));
hMac.update(V, 0, V.length);
hMac.doFinal(V, 0);
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class CTRSP800DRBG method Block_Cipher_df.
// 1. If (number_of_bits_to_return > max_number_of_bits), then return an
// ERROR_FLAG.
// 2. L = len (input_string)/8.
// 3. N = number_of_bits_to_return/8.
// Comment: L is the bitstring represention of
// the integer resulting from len (input_string)/8.
// L shall be represented as a 32-bit integer.
//
// Comment : N is the bitstring represention of
// the integer resulting from
// number_of_bits_to_return/8. N shall be
// represented as a 32-bit integer.
//
// 4. S = L || N || input_string || 0x80.
// 5. While (len (S) mod outlen)
// Comment : Pad S with zeros, if necessary.
// 0, S = S || 0x00.
//
// Comment : Compute the starting value.
// 6. temp = the Null string.
// 7. i = 0.
// 8. K = Leftmost keylen bits of 0x00010203...1D1E1F.
// 9. While len (temp) < keylen + outlen, do
//
// IV = i || 0outlen - len (i).
//
// 9.1
//
// temp = temp || BCC (K, (IV || S)).
//
// 9.2
//
// i = i + 1.
//
// 9.3
//
// Comment : i shall be represented as a 32-bit
// integer, i.e., len (i) = 32.
//
// Comment: The 32-bit integer represenation of
// i is padded with zeros to outlen bits.
//
// Comment: Compute the requested number of
// bits.
//
// 10. K = Leftmost keylen bits of temp.
//
// 11. X = Next outlen bits of temp.
//
// 12. temp = the Null string.
//
// 13. While len (temp) < number_of_bits_to_return, do
//
// 13.1 X = Block_Encrypt (K, X).
//
// 13.2 temp = temp || X.
//
// 14. requested_bits = Leftmost number_of_bits_to_return of temp.
//
// 15. Return SUCCESS and requested_bits.
private byte[] Block_Cipher_df(byte[] inputString, int bitLength) {
int outLen = _engine.getBlockSize();
// already in bytes
int L = inputString.length;
int N = bitLength / 8;
// 4 S = L || N || inputstring || 0x80
int sLen = 4 + 4 + L + 1;
int blockLen = ((sLen + outLen - 1) / outLen) * outLen;
byte[] S = new byte[blockLen];
copyIntToByteArray(S, L, 0);
copyIntToByteArray(S, N, 4);
System.arraycopy(inputString, 0, S, 8, L);
S[8 + L] = (byte) 0x80;
// S already padded with zeros
byte[] temp = new byte[_keySizeInBits / 8 + outLen];
byte[] bccOut = new byte[outLen];
byte[] IV = new byte[outLen];
int i = 0;
byte[] K = new byte[_keySizeInBits / 8];
System.arraycopy(K_BITS, 0, K, 0, K.length);
while (i * outLen * 8 < _keySizeInBits + outLen * 8) {
copyIntToByteArray(IV, i, 0);
BCC(bccOut, K, IV, S);
int bytesToCopy = ((temp.length - i * outLen) > outLen) ? outLen : (temp.length - i * outLen);
System.arraycopy(bccOut, 0, temp, i * outLen, bytesToCopy);
++i;
}
byte[] X = new byte[outLen];
System.arraycopy(temp, 0, K, 0, K.length);
System.arraycopy(temp, K.length, X, 0, X.length);
temp = new byte[bitLength / 8];
i = 0;
_engine.init(true, new KeyParameter(expandKey(K)));
while (i * outLen < temp.length) {
_engine.processBlock(X, 0, X, 0);
int bytesToCopy = ((temp.length - i * outLen) > outLen) ? outLen : (temp.length - i * outLen);
System.arraycopy(X, 0, temp, i * outLen, bytesToCopy);
i++;
}
return temp;
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class CTRSP800DRBG method generate.
/**
* Populate a passed in array with random data.
*
* @param output output array for generated bits.
* @param additionalInput additional input to be added to the DRBG in this step.
* @param predictionResistant true if a reseed should be forced, false otherwise.
* @return number of bits generated, -1 if a reseed required.
*/
public int generate(byte[] output, byte[] additionalInput, boolean predictionResistant) {
if (_isTDEA) {
if (_reseedCounter > TDEA_RESEED_MAX) {
return -1;
}
if (Utils.isTooLarge(output, TDEA_MAX_BITS_REQUEST / 8)) {
throw new IllegalArgumentException("Number of bits per request limited to " + TDEA_MAX_BITS_REQUEST);
}
} else {
if (_reseedCounter > AES_RESEED_MAX) {
return -1;
}
if (Utils.isTooLarge(output, AES_MAX_BITS_REQUEST / 8)) {
throw new IllegalArgumentException("Number of bits per request limited to " + AES_MAX_BITS_REQUEST);
}
}
if (predictionResistant) {
CTR_DRBG_Reseed_algorithm(additionalInput);
additionalInput = null;
}
if (additionalInput != null) {
additionalInput = Block_Cipher_df(additionalInput, _seedLength);
CTR_DRBG_Update(additionalInput, _Key, _V);
} else {
additionalInput = new byte[_seedLength / 8];
}
byte[] out = new byte[_V.length];
_engine.init(true, new KeyParameter(expandKey(_Key)));
for (int i = 0; i <= output.length / out.length; i++) {
int bytesToCopy = ((output.length - i * out.length) > out.length) ? out.length : (output.length - i * _V.length);
if (bytesToCopy != 0) {
addOneTo(_V);
_engine.processBlock(_V, 0, out, 0);
System.arraycopy(out, 0, output, i * out.length, bytesToCopy);
}
}
CTR_DRBG_Update(additionalInput, _Key, _V);
_reseedCounter++;
return output.length * 8;
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class HMacSP800DRBG method hmac_DRBG_Update_Func.
private void hmac_DRBG_Update_Func(byte[] seedMaterial, byte vValue) {
_hMac.init(new KeyParameter(_K));
_hMac.update(_V, 0, _V.length);
_hMac.update(vValue);
if (seedMaterial != null) {
_hMac.update(seedMaterial, 0, seedMaterial.length);
}
_hMac.doFinal(_K, 0);
_hMac.init(new KeyParameter(_K));
_hMac.update(_V, 0, _V.length);
_hMac.doFinal(_V, 0);
}
Aggregations