aboutsummaryrefslogtreecommitdiff
path: root/tests/test-rsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-rsa.c')
-rw-r--r--tests/test-rsa.c156
1 files changed, 21 insertions, 135 deletions
diff --git a/tests/test-rsa.c b/tests/test-rsa.c
index dc1a722..5491399 100644
--- a/tests/test-rsa.c
+++ b/tests/test-rsa.c
@@ -48,107 +48,6 @@
#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.
*/
@@ -158,43 +57,23 @@ static int test_modexp(const char * const kind,
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);
+ uint8_t result[tc->n.len];
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));
+ if (hal_modexp(msg->val, msg->len, exp->val, exp->len,
+ tc->n.val, tc->n.len, result, sizeof(result)) != HAL_OK) {
+ printf("ModExp failed\n");
+ return 0;
+ }
- printf("Comparing results with known value...");
- if (memcmp(b, val->val, val->len))
+ if (memcmp(result, val->val, val->len)) {
printf("MISMATCH\n");
- else
- printf("OK\n");
- printf("\n");
+ return 0;
+ }
- return 0;
+ printf("OK\n");
+ return 1;
}
/*
@@ -210,22 +89,29 @@ static int test_rsa(const rsa_tc_t * const tc)
int main(int argc, char *argv[])
{
uint8_t name[8], version[4];
+ hal_error_t err;
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)));
+ if ((err = hal_io_read(MODEXP_ADDR_NAME0, name, sizeof(name))) != HAL_OK ||
+ (err = hal_io_read(MODEXP_ADDR_VERSION, version, sizeof(version))) != HAL_OK) {
+ printf("Initialization failed: %s\n", hal_error_string(err));
+ return 1;
+ }
+
printf("\"%8.8s\" \"%4.4s\"\n\n", name, version);
/*
* Run the test cases.
*/
+ hal_modexp_set_debug(1);
+
for (i = 0; i < (sizeof(rsa_tc)/sizeof(*rsa_tc)); i++)
- if (test_rsa(&rsa_tc[i]))
+ if (!test_rsa(&rsa_tc[i]))
return 1;
return 0;