summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/pqc/crypto/gmss/util/GMSSRandom.java
blob: c6d30227aa2da89a25b6a4ee4f3554459947b0a2 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.bouncycastle.pqc.crypto.gmss.util;

import org.bouncycastle.crypto.Digest;

/**
 * This class provides a PRNG for GMSS
 */
public class GMSSRandom
{
    /**
     * Hash function for the construction of the authentication trees
     */
    private Digest messDigestTree;

    /**
     * Constructor
     *
     * @param messDigestTree2
     */
    public GMSSRandom(Digest messDigestTree2)
    {

        this.messDigestTree = messDigestTree2;
    }

    /**
     * computes the next seed value, returns a random byte array and sets
     * outseed to the next value
     *
     * @param outseed byte array in which ((1 + SEEDin +RAND) mod 2^n) will be
     *                stored
     * @return byte array of H(SEEDin)
     */
    public byte[] nextSeed(byte[] outseed)
    {
        // RAND <-- H(SEEDin)
        byte[] rand = new byte[outseed.length];
        messDigestTree.update(outseed, 0, outseed.length);
        rand = new byte[messDigestTree.getDigestSize()];
        messDigestTree.doFinal(rand, 0);

        // SEEDout <-- (1 + SEEDin +RAND) mod 2^n
        addByteArrays(outseed, rand);
        addOne(outseed);

        // System.arraycopy(outseed, 0, outseed, 0, outseed.length);

        return rand;
    }

    private void addByteArrays(byte[] a, byte[] b)
    {

        byte overflow = 0;
        int temp;

        for (int i = 0; i < a.length; i++)
        {
            temp = (0xFF & a[i]) + (0xFF & b[i]) + overflow;
            a[i] = (byte)temp;
            overflow = (byte)(temp >> 8);
        }
    }

    private void addOne(byte[] a)
    {

        byte overflow = 1;
        int temp;

        for (int i = 0; i < a.length; i++)
        {
            temp = (0xFF & a[i]) + overflow;
            a[i] = (byte)temp;
            overflow = (byte)(temp >> 8);
        }
    }
}