From f5a7d9fd0410b4e717714fa323289daf367d0b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Stro=CC=88mbergson?= Date: Wed, 20 May 2015 11:18:48 +0200 Subject: Cleanup. Adding more functions for checking and cleaning memories. --- sw/modexp_tester.c | 184 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 106 insertions(+), 78 deletions(-) diff --git a/sw/modexp_tester.c b/sw/modexp_tester.c index 95a6513..21bebbd 100644 --- a/sw/modexp_tester.c +++ b/sw/modexp_tester.c @@ -45,7 +45,6 @@ #include "cryptech.h" - //------------------------------------------------------------------ // Global defines. //------------------------------------------------------------------ @@ -56,12 +55,12 @@ //------------------------------------------------------------------ // Robs macros. Scary scary. //------------------------------------------------------------------ -#define check(_expr_) \ - do { \ - if ((_expr_) != 0) { \ - printf("%s failed\n", #_expr_); \ - exit(1); \ - } \ +#define check(_expr_) \ + do { \ + if ((_expr_) != 0) { \ + printf("%s failed\n", #_expr_); \ + exit(1); \ + } \ } while (0) @@ -122,13 +121,13 @@ static void check_modulus_mem(void) { uint8_t i; uint32_t j; - + printf("Testing modulus mem access.\n"); tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); // Write test data to modulus mempory. for (i = 0 ; i < 64; i = i + 1) { - j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) + + j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) + ((i * 4 + 1) << 8) + i * 4; tc_w32(MODEXP_MODULUS_DATA, j); } @@ -136,7 +135,7 @@ static void check_modulus_mem(void) tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); // Read out test data from modulus mempory. for (i = 0 ; i < 64 ; i = i + 4) { - printf("modulus med: 0x%08x 0x%08x 0x%08x 0x%08x\n", + printf("modulus mem: 0x%08x 0x%08x 0x%08x 0x%08x\n", tc_r32(MODEXP_MODULUS_DATA), tc_r32(MODEXP_MODULUS_DATA), tc_r32(MODEXP_MODULUS_DATA), @@ -154,13 +153,13 @@ static void check_exponent_mem(void) { uint8_t i; uint32_t j; - + printf("Testing exponent mem access.\n"); tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); // Write test data to exponent memory. for (i = 0 ; i < 64; i = i + 1) { - j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) + + j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) + ((i * 4 + 1) << 8) + i * 4; tc_w32(MODEXP_EXPONENT_DATA, j); } @@ -187,13 +186,13 @@ static void check_message_mem(void) { uint8_t i; uint32_t j; - + printf("Testing message mem access.\n"); tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); // Write test data to message memory. for (i = 0 ; i < 64; i = i + 1) { - j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) + + j = ((i * 4 + 3) << 24) + ((i * 4 + 2) << 16) + ((i * 4 + 1) << 8) + i * 4; tc_w32(MODEXP_MESSAGE_DATA, j); } @@ -211,46 +210,34 @@ static void check_message_mem(void) //------------------------------------------------------------------ -// tc1() +// clear_mems() // -// c = m ** e % N with the following (decimal) test values: -// m = 3 -// e = 7 -// n = 11 -// c = 3 ** 7 % 11 = 9 +// Zero fill the memories. //------------------------------------------------------------------ -static void tc1() +static void clear_mems() { - uint32_t result; + uint32_t i; + tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); + tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); + tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); - printf("Running TC1: 0x03 ** 0x07 mod 0x0b = 0x09\n"); + for (i = 0 ; i < 256 ; i++) { + tc_w32(MODEXP_MESSAGE_DATA, 0x00000000); + tc_w32(MODEXP_EXPONENT_DATA, 0x00000000); + tc_w32(MODEXP_MODULUS_DATA, 0x00000000); + } - // Write operands and set associated lengths. - tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); - tc_w32(MODEXP_MESSAGE_DATA, 0x00000003); - tc_w32(MODEXP_LENGTH, 0x00000001); - + tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); - tc_w32(MODEXP_EXPONENT_DATA, 0x00000007); - tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000001); - - tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); - tc_w32(MODEXP_MODULUS_DATA, 0x0000000b); - tc_w32(MODEXP_MODULUS_LENGTH, 0x00000001); - - // Start processing and wait for ready. - tc_w32(MODEXP_ADDR_CTRL, 0x00000001); - check(tc_wait_ready(MODEXP_ADDR_STATUS)); - - // Dump contents of result memory. - tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000); - result = tc_r32(MODEXP_RESULT_DATA); - if (result == 0x00000009) - printf("TC1: OK\n"); - else - printf("TC1: Error. Expected 0x00000009, got 0x%08x\n", result); + tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); } + +//------------------------------------------------------------------ +// dump_mems() +// +// Dump the first words from the memories. +//------------------------------------------------------------------ static void dump_mems() { tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); @@ -276,11 +263,53 @@ static void dump_mems() printf("0x%08x 0x%08x 0x%08x 0x%08x\n", tc_r32(MODEXP_RESULT_DATA), tc_r32(MODEXP_RESULT_DATA), tc_r32(MODEXP_RESULT_DATA), tc_r32(MODEXP_RESULT_DATA)); - + + tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); + tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); + tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); + tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000); +} + + +//------------------------------------------------------------------ +// tc1() +// +// c = m ** e % N with the following (decimal) test values: +// m = 3 +// e = 7 +// n = 11 (0x0b) +// c = 3 ** 7 % 11 = 9 +//------------------------------------------------------------------ +static void tc1() +{ + uint32_t result; + + printf("Running TC1: 0x03 ** 0x07 mod 0x0b = 0x09\n"); + + // Write operands and set associated lengths. tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); + tc_w32(MODEXP_MESSAGE_DATA, 0x00000003); + tc_w32(MODEXP_LENGTH, 0x00000001); + tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); + tc_w32(MODEXP_EXPONENT_DATA, 0x00000007); + tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000001); + tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); + tc_w32(MODEXP_MODULUS_DATA, 0x0000000b); + tc_w32(MODEXP_MODULUS_LENGTH, 0x00000001); + + // Start processing and wait for ready. + tc_w32(MODEXP_ADDR_CTRL, 0x00000001); + check(tc_wait_ready(MODEXP_ADDR_STATUS)); + + // Check result with expected value. tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000); + result = tc_r32(MODEXP_RESULT_DATA); + if (result == 0x00000009) + printf("TC1: OK\n"); + else + printf("TC1: Error. Expected 0x00000009, got 0x%08x\n", result); } @@ -304,28 +333,24 @@ static void tc2() tc_w32(MODEXP_MESSAGE_DATA, 0x000000fb); tc_w32(MODEXP_MESSAGE_DATA, 0x00000000); tc_w32(MODEXP_LENGTH, 0x00000001); - + tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); tc_w32(MODEXP_EXPONENT_DATA, 0x000000fb); tc_w32(MODEXP_EXPONENT_DATA, 0x00000000); tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000001); - + tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); tc_w32(MODEXP_MODULUS_DATA, 0x00000101); tc_w32(MODEXP_MODULUS_DATA, 0x00000000); tc_w32(MODEXP_MODULUS_LENGTH, 0x00000001); - dump_mems(); - // Start processing and wait for ready. printf("TC2: Starting processing. Waiting for ready...\n"); tc_w32(MODEXP_ADDR_CTRL, 0x00000001); check(tc_wait_ready(MODEXP_ADDR_STATUS)); printf("TC2: Ready seen.\n"); - dump_mems(); - - // Dump contents of result memory. + // Check result with expected value. tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000); result = tc_r32(MODEXP_RESULT_DATA); if (result == 0x000000b7) @@ -348,26 +373,28 @@ static void tc3() { uint32_t result; + clear_mems(); + printf("Running TC3: 0x81 ** 0x41 mod 0x87 = 0x36\n"); // Write operands and set associated lengths. tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); tc_w32(MODEXP_MESSAGE_DATA, 0x00000081); tc_w32(MODEXP_LENGTH, 0x00000001); - + tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); tc_w32(MODEXP_EXPONENT_DATA, 0x00000041); tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000001); - + tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); tc_w32(MODEXP_MODULUS_DATA, 0x00000087); tc_w32(MODEXP_MODULUS_LENGTH, 0x00000001); - + // Start processing and wait for ready. tc_w32(MODEXP_ADDR_CTRL, 0x00000001); check(tc_wait_ready(MODEXP_ADDR_STATUS)); - - // Dump contents of result memory. + + // Check result with expected value. tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000); result = tc_r32(MODEXP_RESULT_DATA); if (result == 0x00000036) @@ -381,70 +408,71 @@ static void tc3() // tc4() // // c = m ** e % N with the following test values: -// m = 251 (0xfb) -// e = 251 (0xfb) -// n = 257 (0x101) -// c = 251 ** 251 % 257 = 183 (0xb7) +// m = 0x00000001946473e1 +// e = 0xh000000010e85e74f +// n = 0x0000000170754797 +// c = 0x000000007761ed4f +// +// These operands spans two 32-bit words. //------------------------------------------------------------------ static void tc4() { - uint32_t result0; - uint32_t result1; + uint32_t result0, result1; - printf("Running TC4: 0x00000001946473e1 ** 0xh000000010e85e74f mod 0000000170754797 = 0x000000007761ed4f\n"); + printf("Running TC4: 0x00000001946473e1 ** 0xh000000010e85e74f mod 0x0000000170754797 = 0x000000007761ed4f\n"); // Write operands and set associated lengths. tc_w32(MODEXP_MESSAGE_PTR_RST, 0x00000000); tc_w32(MODEXP_MESSAGE_DATA, 0x00000001); tc_w32(MODEXP_MESSAGE_DATA, 0x946473e1); tc_w32(MODEXP_LENGTH, 0x00000002); - + tc_w32(MODEXP_EXPONENT_PTR_RST, 0x00000000); tc_w32(MODEXP_EXPONENT_DATA, 0x00000001); tc_w32(MODEXP_EXPONENT_DATA, 0x0e85e74f); tc_w32(MODEXP_EXPONENT_LENGTH, 0x00000002); - + tc_w32(MODEXP_MODULUS_PTR_RST, 0x00000000); tc_w32(MODEXP_MODULUS_DATA, 0x00000001); tc_w32(MODEXP_MODULUS_DATA, 0x70754797); tc_w32(MODEXP_MODULUS_LENGTH, 0x00000002); - + // Start processing and wait for ready. tc_w32(MODEXP_ADDR_CTRL, 0x00000001); check(tc_wait_ready(MODEXP_ADDR_STATUS)); - - // Dump contents of result memory. + + // Check result with expected value. tc_w32(MODEXP_RESULT_PTR_RST, 0x00000000); result0 = tc_r32(MODEXP_RESULT_DATA); result1 = tc_r32(MODEXP_RESULT_DATA); if ((result0 == 0x00000000) && (result1 == 0x7761ed4f)) printf("TC4: OK\n"); else - printf("TC4: Error. Expected 0x000000007761ed4f, got 0x%08x%08x\n", + printf("TC4: Error. Expected 0x000000007761ed4f, got 0x%08x%08x\n", result0, result1); } //------------------------------------------------------------------ +// main() //------------------------------------------------------------------ -int main(int argc, char *argv[]) +int main(void) { check_modexp_access(); - tc_set_debug(1); // check_modulus_mem(); // check_exponent_mem(); // check_message_mem(); -// tc1(); -// tc2(); + tc1(); + tc2(); tc3(); -// tc4(); + tc4(); return 0; } //====================================================================== -// EOF aes_tester.c +// EOF modexp_tester.c //====================================================================== -- cgit v1.2.3