org.spongycastle.crypto.DataLengthException Java Examples

The following examples show how to use org.spongycastle.crypto.DataLengthException. 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: MGF1BytesGeneratorExt.java    From wkcwallet-java with Apache License 2.0 5 votes vote down vote up
public int generateBytes(byte[] out, int outOff, int len) throws DataLengthException, IllegalArgumentException {
    if(out.length - len < outOff) {
        throw new DataLengthException("output buffer too small");
    } else {
        byte[] hashBuf = new byte[this.hLen];
        byte[] C = new byte[4];
        int counter = 0;
        int hashCounter = counterStart;
        this.digest.reset();
        if(len > this.hLen) {
            do {
                this.ItoOSP(hashCounter++, C);
                this.digest.update(this.seed, 0, this.seed.length);
                this.digest.update(C, 0, C.length);
                this.digest.doFinal(hashBuf, 0);
                System.arraycopy(hashBuf, 0, out, outOff + counter * this.hLen, this.hLen);
                ++counter;
            } while(counter < len / this.hLen);
        }

        if(counter * this.hLen < len) {
            this.ItoOSP(hashCounter, C);
            this.digest.update(this.seed, 0, this.seed.length);
            this.digest.update(C, 0, C.length);
            this.digest.doFinal(hashBuf, 0);
            System.arraycopy(hashBuf, 0, out, outOff + counter * this.hLen, len - counter * this.hLen);
        }

        return len;
    }
}
 
Example #2
Source File: ConcatKDFBytesGenerator.java    From wkcwallet-java with Apache License 2.0 4 votes vote down vote up
/**
 * fill len bytes of the output buffer with bytes generated from the
 * derivation function.
 * 
 * @throws IllegalArgumentException
 *             if the size of the request will cause an overflow.
 * @throws DataLengthException
 *             if the out buffer is too small.
 */
public int generateBytes(byte[] out, int outOff, int len) throws DataLengthException,
        IllegalArgumentException
{
    if ((out.length - len) < outOff)
    {
        throw new DataLengthException("output buffer too small");
    }

    long oBytes = len;
    int outLen = digest.getDigestSize();

    //
    // this is at odds with the standard implementation, the
    // maximum value should be hBits * (2^32 - 1) where hBits
    // is the digest output size in bits. We can't have an
    // array with a long index at the moment...
    //
    if (oBytes > ((2L << 32) - 1))
    {
        throw new IllegalArgumentException("Output length too large");
    }

    int cThreshold = (int)((oBytes + outLen - 1) / outLen);

    byte[] dig = new byte[digest.getDigestSize()];

    byte[] C = new byte[4];
    Pack.intToBigEndian(counterStart, C, 0);

    int counterBase = counterStart & ~0xFF;

    for (int i = 0; i < cThreshold; i++)
    {
        digest.update(C, 0, C.length);
        digest.update(shared, 0, shared.length);

        if (iv != null)
        {
            digest.update(iv, 0, iv.length);
        }

        digest.doFinal(dig, 0);

        if (len > outLen)
        {
            System.arraycopy(dig, 0, out, outOff, outLen);
            outOff += outLen;
            len -= outLen;
        }
        else
        {
            System.arraycopy(dig, 0, out, outOff, len);
        }

        if (++C[3] == 0)
        {
            counterBase += 0x100;
            Pack.intToBigEndian(counterBase, C, 0);
        }
    }

    digest.reset();

    return (int)oBytes;
}