public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException { //padding with zero while (bufOff < blockSize) { buf[bufOff] = 0; bufOff++; } byte[] sumbuf = new byte[buf.length]; System.arraycopy(buf, 0, sumbuf, 0, mac.length); if (firstStep) { firstStep = false; } else { sumbuf = CM5func(buf, 0, mac); } gost28147MacFunc(workingKey, sumbuf, 0, mac, 0); System.arraycopy(mac, (mac.length/2)-macSize, out, outOff, macSize); reset(); return macSize; }
private void gost28147MacFunc( int[] workingKey, byte[] in, int inOff, byte[] out, int outOff) { int N1, N2, tmp; //tmp -> for saving N1 N1 = bytesToint(in, inOff); N2 = bytesToint(in, inOff + 4); for(int k = 0; k < 2; k++) // 1-16 steps { for(int j = 0; j < 8; j++) { tmp = N1; N1 = N2 ^ gost28147_mainStep(N1, workingKey[j]); // CM2 N2 = tmp; } } intTobytes(N1, out, outOff); intTobytes(N2, out, outOff + 4); }
public Mac() { super(new GOST28147Mac()); } }
public void update(byte in) throws IllegalStateException { if (bufOff == buf.length) { byte[] sumbuf = new byte[buf.length]; System.arraycopy(buf, 0, sumbuf, 0, mac.length); if (firstStep) { firstStep = false; } else { sumbuf = CM5func(buf, 0, mac); } gost28147MacFunc(workingKey, sumbuf, 0, mac, 0); bufOff = 0; } buf[bufOff++] = in; }
public void init( CipherParameters params) throws IllegalArgumentException { reset(); buf = new byte[blockSize]; if (params instanceof ParametersWithSBox) { ParametersWithSBox param = (ParametersWithSBox)params; // // Set the S-Box // System.arraycopy(param.getSBox(), 0, this.S, 0, param.getSBox().length); // // set key if there is one // if (param.getParameters() != null) { workingKey = generateWorkingKey(((KeyParameter)param.getParameters()).getKey()); } } else if (params instanceof KeyParameter) { workingKey = generateWorkingKey(((KeyParameter)params).getKey()); } else { throw new IllegalArgumentException("invalid parameter passed to GOST28147 init - " + params.getClass().getName()); } }
public byte[] unwrap(byte[] input, int inOff, int inLen) throws InvalidCipherTextException { byte[] decKey = new byte[inLen - mac.getMacSize()]; cipher.processBlock(input, inOff, decKey, 0); cipher.processBlock(input, inOff + 8, decKey, 8); cipher.processBlock(input, inOff + 16, decKey, 16); cipher.processBlock(input, inOff + 24, decKey, 24); byte[] macResult = new byte[mac.getMacSize()]; mac.update(decKey, 0, decKey.length); mac.doFinal(macResult, 0); byte[] macExpected = new byte[mac.getMacSize()]; System.arraycopy(input, inOff + inLen - 4, macExpected, 0, mac.getMacSize()); if (!Arrays.constantTimeAreEqual(macResult, macExpected)) { throw new IllegalStateException("mac mismatch"); } return decKey; } }
private int[] generateWorkingKey( byte[] userKey) { if (userKey.length != 32) { throw new IllegalArgumentException("Key length invalid. Key needs to be 32 byte - 256 bit!!!"); } int key[] = new int[8]; for(int i=0; i!=8; i++) { key[i] = bytesToint(userKey,i*4); } return key; }
public void update(byte in) throws IllegalStateException { if (bufOff == buf.length) { byte[] sumbuf = new byte[buf.length]; System.arraycopy(buf, 0, sumbuf, 0, mac.length); if (firstStep) { firstStep = false; if (macIV != null) { sumbuf = CM5func(buf, 0, macIV); } } else { sumbuf = CM5func(buf, 0, mac); } gost28147MacFunc(workingKey, sumbuf, 0, mac, 0); bufOff = 0; } buf[bufOff++] = in; }
throws IllegalArgumentException reset(); buf = new byte[blockSize]; macIV = null; workingKey = generateWorkingKey(((KeyParameter)param.getParameters()).getKey()); workingKey = generateWorkingKey(((KeyParameter)params).getKey()); workingKey = generateWorkingKey(((KeyParameter)p.getParameters()).getKey()); System.arraycopy(p.getIV(), 0, mac, 0, mac.length);
public byte[] wrap(byte[] input, int inOff, int inLen) { mac.update(input, inOff, inLen); byte[] wrappedKey = new byte[inLen + mac.getMacSize()]; cipher.processBlock(input, inOff, wrappedKey, 0); cipher.processBlock(input, inOff + 8, wrappedKey, 8); cipher.processBlock(input, inOff + 16, wrappedKey, 16); cipher.processBlock(input, inOff + 24, wrappedKey, 24); mac.doFinal(wrappedKey, inLen); return wrappedKey; }
private int[] generateWorkingKey( byte[] userKey) { if (userKey.length != 32) { throw new IllegalArgumentException("Key length invalid. Key needs to be 32 byte - 256 bit!!!"); } int key[] = new int[8]; for(int i=0; i!=8; i++) { key[i] = bytesToint(userKey,i*4); } return key; }
private void gost28147MacFunc( int[] workingKey, byte[] in, int inOff, byte[] out, int outOff) { int N1, N2, tmp; //tmp -> for saving N1 N1 = bytesToint(in, inOff); N2 = bytesToint(in, inOff + 4); for(int k = 0; k < 2; k++) // 1-16 steps { for(int j = 0; j < 8; j++) { tmp = N1; N1 = N2 ^ gost28147_mainStep(N1, workingKey[j]); // CM2 N2 = tmp; } } intTobytes(N1, out, outOff); intTobytes(N2, out, outOff + 4); }
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException { //padding with zero while (bufOff < blockSize) { buf[bufOff] = 0; bufOff++; } byte[] sumbuf = new byte[buf.length]; System.arraycopy(buf, 0, sumbuf, 0, mac.length); if (firstStep) { firstStep = false; } else { sumbuf = CM5func(buf, 0, mac); } gost28147MacFunc(workingKey, sumbuf, 0, mac, 0); System.arraycopy(mac, (mac.length/2)-macSize, out, outOff, macSize); reset(); return macSize; }
if (macIV != null) sumbuf = CM5func(buf, 0, macIV); sumbuf = CM5func(buf, 0, mac); gost28147MacFunc(workingKey, sumbuf, 0, mac, 0); sumbuf = CM5func(in, inOff, mac); gost28147MacFunc(workingKey, sumbuf, 0, mac, 0);
sumbuf = CM5func(buf, 0, mac); gost28147MacFunc(workingKey, sumbuf, 0, mac, 0); sumbuf = CM5func(in, inOff, mac); gost28147MacFunc(workingKey, sumbuf, 0, mac, 0);