From 99524348037553e71b445aa07e8b0214f14745cb Mon Sep 17 00:00:00 2001 From: "Pavel V. Shatov (Meister)" Date: Tue, 17 Apr 2018 15:21:23 +0300 Subject: Initial commit of P-384 point multiplier suitable for ECDH. --- stm32_driver/ecdhp384_driver_sample.c | 258 ++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 stm32_driver/ecdhp384_driver_sample.c (limited to 'stm32_driver/ecdhp384_driver_sample.c') diff --git a/stm32_driver/ecdhp384_driver_sample.c b/stm32_driver/ecdhp384_driver_sample.c new file mode 100644 index 0000000..5ab4b1f --- /dev/null +++ b/stm32_driver/ecdhp384_driver_sample.c @@ -0,0 +1,258 @@ +// +// simple driver to test "ecdhp384" core in hardware +// + +// +// note, that the test program needs a custom bitstream where +// the core is located at offset 0 (without the core selector) +// + +// stm32 headers +#include "stm-init.h" +#include "stm-led.h" +#include "stm-fmc.h" + +// locations of core registers +#define CORE_ADDR_NAME0 (0x00 << 2) +#define CORE_ADDR_NAME1 (0x01 << 2) +#define CORE_ADDR_VERSION (0x02 << 2) +#define CORE_ADDR_CONTROL (0x08 << 2) +#define CORE_ADDR_STATUS (0x09 << 2) + +// locations of data buffers +#define CORE_ADDR_BUF_K (0x80 << 2) +#define CORE_ADDR_BUF_XIN (0x90 << 2) +#define CORE_ADDR_BUF_YIN (0xA0 << 2) +#define CORE_ADDR_BUF_XOUT (0xB0 << 2) +#define CORE_ADDR_BUF_YOUT (0xC0 << 2) + +// bit maps +#define CORE_CONTROL_BIT_NEXT 0x00000002 +#define CORE_STATUS_BIT_READY 0x00000002 + +// curve selection +#define USE_CURVE 2 + +#include "../../../user/shatov/ecdh_fpga_model/ecdh_fpga_model.h" +#include "../../../user/shatov/ecdh_fpga_model/test_vectors/ecdh_test_vectors.h" + +#define BUF_NUM_WORDS (OPERAND_WIDTH / (sizeof(uint32_t) << 3)) // 8 + +// +// test vectors +// +static const uint32_t p384_da[BUF_NUM_WORDS] = P_384_DA; +static const uint32_t p384_db[BUF_NUM_WORDS] = P_384_DB; + +static const uint32_t p384_gx[BUF_NUM_WORDS] = P_384_G_X; +static const uint32_t p384_gy[BUF_NUM_WORDS] = P_384_G_Y; + +static const uint32_t p384_qax[BUF_NUM_WORDS] = P_384_QA_X; +static const uint32_t p384_qay[BUF_NUM_WORDS] = P_384_QA_Y; + +static const uint32_t p384_qbx[BUF_NUM_WORDS] = P_384_QB_X; +static const uint32_t p384_qby[BUF_NUM_WORDS] = P_384_QB_Y; + +static const uint32_t p384_qa2x[BUF_NUM_WORDS] = P_384_QA2_X; +static const uint32_t p384_qa2y[BUF_NUM_WORDS] = P_384_QA2_Y; + +static const uint32_t p384_qb2x[BUF_NUM_WORDS] = P_384_QB2_X; +static const uint32_t p384_qb2y[BUF_NUM_WORDS] = P_384_QB2_Y; + +static const uint32_t p384_sx[BUF_NUM_WORDS] = P_384_S_X; +static const uint32_t p384_sy[BUF_NUM_WORDS] = P_384_S_Y; + +static const uint32_t p384_0[BUF_NUM_WORDS] = P_384_ZERO; +static const uint32_t p384_1[BUF_NUM_WORDS] = P_384_ONE; + +static const uint32_t p384_hx[BUF_NUM_WORDS] = P_384_H_X; +static const uint32_t p384_hy[BUF_NUM_WORDS] = P_384_H_Y; + +static const uint32_t p384_n[BUF_NUM_WORDS] = P_384_N; + +static uint32_t p384_2[BUF_NUM_WORDS]; // 2 +static uint32_t p384_n1[BUF_NUM_WORDS]; // n + 1 +static uint32_t p384_n2[BUF_NUM_WORDS]; // n + 2 + +// +// prototypes +// +void toggle_yellow_led(void); +int test_p384_multiplier(const uint32_t *k, + const uint32_t *xin, const uint32_t *yin, + const uint32_t *xout, const uint32_t *yout); + +// +// test routine +// +int main() +{ + int ok; + + stm_init(); + fmc_init(); + + led_on(LED_GREEN); + led_off(LED_RED); + + led_off(LED_YELLOW); + led_off(LED_BLUE); + + uint32_t core_name0; + uint32_t core_name1; + + fmc_read_32(CORE_ADDR_NAME0, &core_name0); + fmc_read_32(CORE_ADDR_NAME1, &core_name1); + + // "ecdh", "p384" + if ((core_name0 != 0x65636468) || (core_name1 != 0x70333834)) { + led_off(LED_GREEN); + led_on(LED_RED); + while (1); + } + + // prepare more numbers + size_t w; + for (w=0; w