aboutsummaryrefslogtreecommitdiff
path: root/tests/test-rsa.c
blob: dc1a7220d73260b8881d73ecbe3b32712973ab1c (plain) (blame)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * test-rsa.c
 * ----------
 * First stumblings towards a test harness for RSA using Cryptech
 * ModExp core.
 *
 * For the moment this just does modular exponentiation tests using
 * RSA keys and pre-formatted data-to-be-signed, without attempting
 * CRT or any of the other clever stuff we should be doing.  This is
 * not usable for any sane purpose other than testing.
 *
 * Authors: Rob Austein
 * Copyright (c) 2015, SUNET
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "cryptech.h"
#include "test-rsa.h"

/*
 * Constant value to use with hal_io_write() when we don't want
 * a read-back check thus aren't using set_register().
 */

static const uint8_t one[] = { 0, 0, 0, 1 };

/*
 * Debugging aid: check a result, report on failure.
 */

#define check(_expr_)                           \
  do {                                          \
    if ((_expr_) != 0)                          \
      return printf("%s failed\n", #_expr_), 1; \
  } while (0)

/*
 * Set an ordinary register, with read-back check.
 */

static int _set_register(const off_t addr,
                         const char * const name,
                         uint32_t value)
{
  uint8_t w1[4], w2[4];
  int i;
  assert(name != NULL);
  for (i = 3; i >= 0; i--) {
    w1[i] = value & 0xFF;
    value >>= 8;
  }
  printf("Setting register %#lx %s...\n", (unsigned long) addr, name);
  check(hal_io_write(addr, w1, sizeof(w1)));
  check(hal_io_read(addr,  w2, sizeof(w2)));
  if (memcmp(w1, w2, sizeof(w1)) != 0)
    printf("MISMATCH\n");
  return 0;
}

/*
 * Get value of a block memory.
 */

static int _get_blockmem(const off_t reset_addr,
                         const char * const reset_name,
                         const off_t data_addr,
                         const char * const data_name,
                         uint8_t *value,
                         const size_t length)
{
  size_t i;
  assert(reset_name != NULL && data_name != NULL && value != NULL && length % 4 == 0);
  printf("Setting register %#lx %s...\n", (unsigned long) reset_addr, reset_name);
  check(hal_io_write(reset_addr, one, sizeof(one)));
  printf("Getting blockmem %#lx %s...\n", (unsigned long) data_addr, data_name);
  for (i = 0; i < length; i += 4)
    check(hal_io_read(data_addr, &value[i], 4));
  return 0;
}

/*
 * Set value of a block memory, with read-back check.
 */

static int _set_blockmem(const off_t reset_addr,
                         const char * const reset_name,
                         const off_t data_addr,
                         const char * const data_name,
                         const uint8_t * const value,
                         const size_t value_length,
                         uint8_t *buffer,
                         const size_t buffer_length)
{
  size_t i;
  assert(reset_name != NULL && data_name != NULL && value != NULL && buffer_length >= value_length && value_length % 4 == 0);
  printf("Setting register %#lx %s...\n", (unsigned long) reset_addr, reset_name);
  check(hal_io_write(reset_addr, one, sizeof(one)));
  printf("Setting blockmem %#lx %s...\n", (unsigned long) data_addr, data_name);
  for (i = 0; i < value_length; i += 4)
    check(hal_io_write(data_addr, &value[i], 4));
  check(_get_blockmem(reset_addr, reset_name, data_addr, data_name, buffer, value_length));
  if (memcmp(value, buffer, value_length))
    printf("MISMATCH\n");
  printf("\n");
  return 0;
}

/*
 * Syntactic sugar.
 */

#define set_register(_field_, _value_) \
  _set_register(_field_, #_field_, _value_)

#define get_blockmem(_field_, _value_, _length_) \
  _get_blockmem(_field_##_PTR_RST, #_field_ "_PTR_RST", _field_##_DATA, #_field_ "_DATA", _value_, _length_)

#define set_blockmem(_field_, _value_, _buffer_) \
  _set_blockmem(_field_##_PTR_RST, #_field_ "_PTR_RST", _field_##_DATA, #_field_ "_DATA", (_value_).val, (_value_).len, _buffer_, sizeof(_buffer_))

/*
 * Run one modexp test.
 */

static int test_modexp(const char * const kind,
                       const rsa_tc_t * const tc,
                       const rsa_tc_bn_t * const msg, /* Input message */
                       const rsa_tc_bn_t * const exp, /* Exponent */
                       const rsa_tc_bn_t * const val) /* Expected result */
{
  uint8_t b[4096];

  hal_io_set_debug(1);

  printf("%s test for %lu-bit RSA key\n", kind, (unsigned long) tc->size);

  check(set_blockmem(MODEXP_MODULUS, tc->n, b));
  check(set_blockmem(MODEXP_MESSAGE, (*msg), b));
  check(set_register(MODEXP_MODULUS_LENGTH, tc->n.len / 4));

  check(set_blockmem(MODEXP_EXPONENT, (*exp), b));
  check(set_register(MODEXP_EXPONENT_LENGTH, val->len / 4));

  printf("Checking ready status\n");
  check(hal_io_wait_ready(MODEXP_ADDR_STATUS));
  printf("\n");

  check(set_register(MODEXP_ADDR_CTRL, 1));

  hal_io_set_debug(0);

  printf("Waiting for ready\n");
  check(hal_io_wait(MODEXP_ADDR_STATUS, STATUS_READY, NULL));
  printf("\n");

  hal_io_set_debug(1);

  check(get_blockmem(MODEXP_RESULT, b, tc->n.len));

  printf("Comparing results with known value...");
  if (memcmp(b, val->val, val->len))
    printf("MISMATCH\n");
  else
    printf("OK\n");
  printf("\n");

  return 0;
}

/*
 * Test signature and exponentiation for one RSA keypair.
 */

static int test_rsa(const rsa_tc_t * const tc)
{
  return (test_modexp("Signature",    tc, &tc->m, &tc->d, &tc->s) || /* RSA decryption */
          test_modexp("Verification", tc, &tc->s, &tc->e, &tc->m));  /* RSA encryption */
}

int main(int argc, char *argv[])
{
  uint8_t name[8], version[4];
  int i;

  /*
   * Initialize EIM and report what core we're running.
   */

  check(hal_io_read(MODEXP_ADDR_NAME0,   name,    sizeof(name)));
  check(hal_io_read(MODEXP_ADDR_VERSION, version, sizeof(version)));
  printf("\"%8.8s\"  \"%4.4s\"\n\n", name, version);

  /*
   * Run the test cases.
   */

  for (i = 0; i < (sizeof(rsa_tc)/sizeof(*rsa_tc)); i++)
    if (test_rsa(&rsa_tc[i]))
      return 1;

  return 0;
}

/*
 * Local variables:
 * indent-tabs-mode: nil
 * End:
 */