aboutsummaryrefslogtreecommitdiff
path: root/tests/gen-shake-tests.rb
blob: 55aec7c7d53604af33862846c7f34b49c5041629 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# Generate SHAKE128 and SHAKE256 test functions from response files in
# SHAKE byte test vectors archive (`shakebytetestvectors.zip`) from the
# Cryptographic Algorithm Validation Program (CAVP).
#
# Usage:
#   # download archive of byte test vectors
#   wget https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/shakebytetestvectors.zip
#
#   # run script to generate test functions on standard output
#   tests/gen-shake-tests.rb path/to/shakebytetestvectors.zip
#

# load libraries
require 'zip'

# number of elements per line of test data
PER_LINE = 128

# TODO
ALGOS = [{
  name: 'SHAKE128', # algo name
  fn: 'shake128', # function prefix

  # response files
  rsp_files: %w{
    SHAKE128ShortMsg.rsp
    SHAKE128LongMsg.rsp
    SHAKE128VariableOut.rsp
  },
}, {
  name: 'SHAKE256', # algo name
  fn: 'shake256', # function prefix

  # response files
  rsp_files: %w{
    SHAKE256ShortMsg.rsp
    SHAKE256LongMsg.rsp
    SHAKE256VariableOut.rsp
  },
}]

# test vector template
TEST_TMPL = <<END_TEST_TMPL
    { %<file_pos>d, %<line>d, %<src_ofs>d, %<src_len>d, %<exp_ofs>d, %<exp_len>d },
END_TEST_TMPL

# test function template
FN_TMPL = <<END_FN_TMPL
// %<name>s tests from shakebytetestvectors.zip
static void test_%<fn>s_xof_cmvp(void) {
  // data for tests
  static const uint8_t DATA[] = {
%<data>s
  }; // total size = %<data_size>d

  // response file names
  static const char *RSP_FILES[] = { %<rsp_files>s };

  // test vectors
  static const struct {
    const size_t rsp_file_ofs, // offset of source file name in RSP_FILES
                 rsp_file_line, // line within rsp file
                 src_ofs, // message offset into DATA
                 src_len, // message length of data
                 exp_ofs, // expected output offset into DATA
                 exp_len; // expected output length
  } TESTS[] = {
%<tests>s
  };

  // run tests
  for (size_t i = 0; i < sizeof(TESTS) / sizeof(TESTS[0]); i++) {
    // get expected result
    const uint8_t * const exp = DATA + TESTS[i].exp_ofs;
    const size_t exp_len = TESTS[i].exp_len;

    // hash data into "got"
    uint8_t got[%<max_exp_len>d] = { 0 };
    %<fn>s_xof_once(DATA + TESTS[i].src_ofs, TESTS[i].src_len, got, exp_len);

    // check for expected result
    if (memcmp(got, exp, exp_len)) {
      // get response file name and test vector line
      const char * const file_name = RSP_FILES[TESTS[i].rsp_file_ofs];
      const size_t file_line = TESTS[i].rsp_file_line;

      fprintf(stderr, "test_%<fn>s_xof_cmvp[%%zu, \\"%%s\\", line %%zu] failed:\\ngot = ", i, file_name, file_line);
      dump_hex(stderr, got,  exp_len);
      fprintf(stderr, "\\nexp = ");
      dump_hex(stderr, exp, exp_len);
      fprintf(stderr, "\\n");
    }
  }
}
END_FN_TMPL

# parse hash of key/value pairs into test vector row
def parse(file, h, data_size, &block)
  # parse hex-encoded message into array of bytes, then
  # truncate message to "Len" bytes, if "Len" is specified
  src = h[:Msg].scan(/../).map { |s| s.to_i(16) }
  src = src[0, h[:Len].to_i] if h.key?(:Len)
  src_ofs = data_size

  # parse hex-encoded expected output into array of bytes, then
  # truncate expected output to "OutputLen" bytes, if "OutputLen" is
  # specified
  exp = h[:Output].scan(/../).map { |s| s.to_i(16) }
  exp = exp[0, h[:OutputLen].to_i] if h.key?(:OutputLen)
  exp_ofs = data_size + src.size

  # append message bytes and expected output bytes to data
  block.call(src + exp)

  # return parsed entry
  {
    file: file,
    line: h[:line],

    # message
    src_ofs: src_ofs,
    src_len: src.size,

    # expected output
    exp_ofs: exp_ofs,
    exp_len: exp.size,
  }
end

# get zip path from first command-line argument
zip_path = ARGV.shift
raise "Usage #$0 <zip_path>" unless zip_path

# open zip file
Zip::File.open(zip_path, 'rb') do |zip|
  ALGOS.each do |algo|
    data = [] # test data (shared across all rsp files)

    # parse test vectors from rsp files
    rows = algo[:rsp_files].each_with_object([]) do |rsp_file, rows|
      curr = {} # current row

      # read lines from rsp file
      lines = zip.glob(rsp_file).first.get_input_stream.readlines.to_a.map { |line| line.strip }

      # parse lines into rows
      lines.size.times.each do |line_i|
        case lines[line_i]
        when /^(\w+) = (\w+)$/
          k, v = $1, $2 # extract key and value
          curr[k.intern] = v

          # cache line number
          curr[:line] = line_i unless curr[:line]
        when ''
          if curr.size > 0
            # parse current hash into test vector and add it to results
            rows << parse(rsp_file, curr, data.size) { |bytes| data += bytes }
          end

          curr = {} # clear current hash
        end
      end
    end

    # get maximum length of expected output
    # used to for size of "got" buffer in emitted code
    max_exp_len = rows.reduce(0) { |r, row| row[:exp_len] > r ? row[:exp_len] : r }

    # get maximum number of lines of emitted static DATA array
    num_lines = data.size / PER_LINE + ((data.size % PER_LINE) > 0 ? 1 : 0)

    # generate test function
    puts(FN_TMPL % {
      name: algo[:name], # algorithm name
      fn: algo[:fn], # algorithm function prefix

      # test vector data
      data: num_lines.times.map { |ofs|
        "    %s,\n" % [data[PER_LINE * ofs, PER_LINE].join(', ')]
      }.join,

      # response file names
      rsp_files: algo[:rsp_files].map { |file| '"%s"' % [file] }.join(', '),

      data_size: data.size, # total size of emitted DATA array, in bytes
      max_exp_len: max_exp_len, # maximum expected output length, in bytes

      # test vectors
      tests: rows.map { |row|
        TEST_TMPL % row.merge({ file_pos: algo[:rsp_files].index(row[:file]) })
      }.join,
    })
  end
end