blob: 79113e4a2e1c4b0a9a51458430b17af6f51b42da (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package org.bouncycastle.pqc.crypto.ntru;
import java.nio.ByteBuffer;
import org.bouncycastle.crypto.Digest;
/**
* An implementation of the deterministic pseudo-random generator in EESS section 3.7.3.1
*/
public class NTRUSignerPrng {
private int counter;
private byte[] seed;
private Digest hashAlg;
/**
* Constructs a new PRNG and seeds it with a byte array.
* @param seed a seed
* @param hashAlg the hash algorithm to use
*/
NTRUSignerPrng(byte[] seed, Digest hashAlg) {
counter = 0;
this.seed = seed;
this.hashAlg = hashAlg;
}
/**
* Returns <code>n</code> random bytes
* @param n number of bytes to return
* @return the next <code>n</code> random bytes
*/
byte[] nextBytes(int n) {
ByteBuffer buf = ByteBuffer.allocate(n);
while (buf.hasRemaining()) {
ByteBuffer cbuf = ByteBuffer.allocate(seed.length + 4);
cbuf.put(seed);
cbuf.putInt(counter);
byte[] array = cbuf.array();
byte[] hash = new byte[hashAlg.getDigestSize()];
hashAlg.update(array, 0, array.length);
hashAlg.doFinal(hash, 0);
if (buf.remaining() < hash.length)
buf.put(hash, 0, buf.remaining());
else
buf.put(hash);
counter++;
}
return buf.array();
}
}
|