Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CAVPReader.java « cavp « test « crypto « bouncycastle « org « java « test « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9cd33afc262ac9d5d40d7cacbb260d44f3b5a8f5 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.bouncycastle.crypto.test.cavp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.macs.CMac;
import org.bouncycastle.crypto.macs.HMac;

public class CAVPReader
{

    private static final Pattern COMMENT_PATTERN = Pattern.compile("^\\s*\\#\\s*(.*)$");
    private static final Pattern CONFIG_PATTERN = Pattern.compile("^\\s*+\\[\\s*+(.*?)\\s*+=\\s*+(.*?)\\s*+\\]\\s*+$");
    private static final Pattern VECTOR_PATTERN = Pattern.compile("^\\s*+(.*?)\\s*+=\\s*+(.*?)\\s*+$");
    private static final Pattern EMPTY_PATTERN = Pattern.compile("^\\s*+$");
    static final Pattern PATTERN_FOR_R = Pattern.compile("(\\d+)_BITS");
    private final CAVPListener listener;
    private String name;
    private BufferedReader lineReader;


    public CAVPReader(CAVPListener listener)
    {
        this.listener = listener;
    }

    public void setInput(String name, Reader reader)
    {
        this.name = name;
        this.lineReader = new BufferedReader(reader);
    }

    public void readAll()
        throws IOException
    {

        listener.setup();

        Properties config = new Properties();

        boolean startNewVector = true;

        Properties vectors = new Properties();

        while (true)
        {
            final String line = lineReader.readLine();
            if (line == null)
            {
                listener.receiveEnd();
                break;
            }

            final Matcher commentMatcher = COMMENT_PATTERN.matcher(line);
            if (commentMatcher.matches())
            {
                listener.receiveCommentLine(commentMatcher.group(1));
                continue;
            }

            final Matcher configMatcher = CONFIG_PATTERN.matcher(line);
            if (configMatcher.matches())
            {
                config.put(configMatcher.group(1), configMatcher.group(2));
                continue;
            }

            final Matcher vectorMatcher = VECTOR_PATTERN.matcher(line);
            if (vectorMatcher.matches())
            {
                vectors.put(vectorMatcher.group(1), vectorMatcher.group(2));
                startNewVector = false;
                continue;
            }

            final Matcher emptyMatcher = EMPTY_PATTERN.matcher(line);
            if (emptyMatcher.matches())
            {
                if (startNewVector)
                {
                    continue;
                }

                listener.receiveCAVPVectors(name, config, vectors);
                vectors = new Properties();
                startNewVector = true;
            }
        }

        listener.tearDown();
    }

    static Mac createPRF(Properties config)
    {
        final Mac prf;
        if (config.getProperty("PRF").matches("CMAC_AES\\d\\d\\d"))
        {
            BlockCipher blockCipher = new AESFastEngine();
            prf = new CMac(blockCipher);
        }
        else if (config.getProperty("PRF").matches("CMAC_TDES\\d"))
        {
            BlockCipher blockCipher = new DESedeEngine();
            prf = new CMac(blockCipher);
        }
        else if (config.getProperty("PRF").matches("HMAC_SHA1"))
        {
            Digest digest = new SHA1Digest();
            prf = new HMac(digest);
        }
        else if (config.getProperty("PRF").matches("HMAC_SHA224"))
        {
            Digest digest = new SHA224Digest();
            prf = new HMac(digest);
        }
        else if (config.getProperty("PRF").matches("HMAC_SHA256"))
        {
            Digest digest = new SHA256Digest();
            prf = new HMac(digest);
        }
        else if (config.getProperty("PRF").matches("HMAC_SHA384"))
        {
            Digest digest = new SHA384Digest();
            prf = new HMac(digest);
        }
        else if (config.getProperty("PRF").matches("HMAC_SHA512"))
        {
            Digest digest = new SHA512Digest();
            prf = new HMac(digest);
        }
        else
        {
            throw new IllegalStateException("Unknown Mac for PRF");
        }
        return prf;
    }

}