diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
commit | 2b976f5364cfdbc37d3086019d93483c983eb80b (patch) | |
tree | cb846af3fd1d43f9c2562a1fb2d06b997ad8f229 /core/src/main/java/org/bouncycastle/crypto/generators/MGF1BytesGenerator.java | |
parent | 5f714bd92fbd780d22406f4bc3681be005f6f04a (diff) |
initial reshuffle
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/generators/MGF1BytesGenerator.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/generators/MGF1BytesGenerator.java | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/generators/MGF1BytesGenerator.java b/core/src/main/java/org/bouncycastle/crypto/generators/MGF1BytesGenerator.java new file mode 100644 index 00000000..e93c0d73 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/generators/MGF1BytesGenerator.java @@ -0,0 +1,114 @@ +package org.bouncycastle.crypto.generators; + +import org.bouncycastle.crypto.DataLengthException; +import org.bouncycastle.crypto.DerivationFunction; +import org.bouncycastle.crypto.DerivationParameters; +import org.bouncycastle.crypto.Digest; +import org.bouncycastle.crypto.params.MGFParameters; + +/** + * Generator for MGF1 as defined in PKCS 1v2 + */ +public class MGF1BytesGenerator + implements DerivationFunction +{ + private Digest digest; + private byte[] seed; + private int hLen; + + /** + * @param digest the digest to be used as the source of generated bytes + */ + public MGF1BytesGenerator( + Digest digest) + { + this.digest = digest; + this.hLen = digest.getDigestSize(); + } + + public void init( + DerivationParameters param) + { + if (!(param instanceof MGFParameters)) + { + throw new IllegalArgumentException("MGF parameters required for MGF1Generator"); + } + + MGFParameters p = (MGFParameters)param; + + seed = p.getSeed(); + } + + /** + * return the underlying digest. + */ + public Digest getDigest() + { + return digest; + } + + /** + * int to octet string. + */ + private void ItoOSP( + int i, + byte[] sp) + { + sp[0] = (byte)(i >>> 24); + sp[1] = (byte)(i >>> 16); + sp[2] = (byte)(i >>> 8); + sp[3] = (byte)(i >>> 0); + } + + /** + * fill len bytes of the output buffer with bytes generated from + * the derivation function. + * + * @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"); + } + + byte[] hashBuf = new byte[hLen]; + byte[] C = new byte[4]; + int counter = 0; + + digest.reset(); + + if (len > hLen) + { + do + { + ItoOSP(counter, C); + + digest.update(seed, 0, seed.length); + digest.update(C, 0, C.length); + digest.doFinal(hashBuf, 0); + + System.arraycopy(hashBuf, 0, out, outOff + counter * hLen, hLen); + } + while (++counter < (len / hLen)); + } + + if ((counter * hLen) < len) + { + ItoOSP(counter, C); + + digest.update(seed, 0, seed.length); + digest.update(C, 0, C.length); + digest.doFinal(hashBuf, 0); + + System.arraycopy(hashBuf, 0, out, outOff + counter * hLen, len - (counter * hLen)); + } + + return len; + } +} |