diff options
-rw-r--r-- | eim/build/Makefile | 1 | ||||
-rw-r--r-- | eim/iseconfig/.gitignore | 1 | ||||
-rw-r--r-- | sw/cryptech.h | 73 | ||||
-rw-r--r-- | sw/hash.c | 4 | ||||
-rw-r--r-- | sw/hash_tester.c | 51 | ||||
-rw-r--r-- | sw/novena-eim.h | 7 | ||||
-rw-r--r-- | sw/trng_extractor.c | 66 | ||||
-rw-r--r-- | sw/trng_tester.c | 62 |
8 files changed, 173 insertions, 92 deletions
diff --git a/eim/build/Makefile b/eim/build/Makefile index e93b05f..5ef3e7e 100644 --- a/eim/build/Makefile +++ b/eim/build/Makefile @@ -45,7 +45,6 @@ vfiles = \ ../../../../rng/trng/src/rtl/trng_csprng.v \ ../../../../rng/trng/src/rtl/trng_csprng_fifo.v \ ../../../../rng/trng/src/rtl/trng_mixer.v \ - ../../../../cipher/chacha/src/rtl/chacha.v \ ../../../../cipher/chacha/src/rtl/chacha_core.v \ ../../../../cipher/chacha/src/rtl/chacha_qr.v diff --git a/eim/iseconfig/.gitignore b/eim/iseconfig/.gitignore index 91e8e2b..c0a165f 100644 --- a/eim/iseconfig/.gitignore +++ b/eim/iseconfig/.gitignore @@ -1,6 +1,7 @@ iseconfig _ngo *.bgn +*.bit *_bitgen.xwbt *.bld *.cmd_log diff --git a/sw/cryptech.h b/sw/cryptech.h index 509c4e9..5afd08c 100644 --- a/sw/cryptech.h +++ b/sw/cryptech.h @@ -43,6 +43,7 @@ #define SEGMENT_OFFSET_HASHES 1 * SEGMENT_SIZE #define SEGMENT_OFFSET_RNGS 2 * SEGMENT_SIZE #define SEGMENT_OFFSET_CIPHERS 3 * SEGMENT_SIZE +#define SEGMENT_OFFSET_MATH 4 * SEGMENT_SIZE // addresses and codes common to all cores @@ -74,6 +75,18 @@ #define COMM_ADDR_NAME1 COMM_ADDR_BASE + ADDR_NAME1 #define COMM_ADDR_VERSION COMM_ADDR_BASE + ADDR_VERSION +#define NOVENA_BOARD_NAME0 "PVT1" +#define NOVENA_BOARD_NAME1 " " +#define NOVENA_BOARD_VERSION "0.10" + +#define EIM_INTERFACE_NAME0 "eim " +#define EIM_INTERFACE_NAME1 " " +#define EIM_INTERFACE_VERSION "0.10" + +#define I2C_INTERFACE_NAME0 "i2c " +#define I2C_INTERFACE_NAME1 " " +#define I2C_INTERFACE_VERSION "0.10" + //------------------------------------------------------------------ // Hashes segment. @@ -134,6 +147,18 @@ #define MODE_SHA_384 2 << 2 #define MODE_SHA_512 3 << 2 +#define SHA1_NAME0 "sha1" +#define SHA1_NAME1 " " +#define SHA1_VERSION "0.50" + +#define SHA256_NAME0 "sha2" +#define SHA256_NAME1 "-256" +#define SHA256_VERSION "0.80" + +#define SHA512_NAME0 "sha2" +#define SHA512_NAME1 "-512" +#define SHA512_VERSION "0.80" + //----------------------------------------------------------------- // TRNG segment. @@ -141,7 +166,7 @@ #define TRNG_CORE_SIZE 0x100 // addresses and codes for the TRNG cores */ -#define TRNG_ADDR_BASE SEGMENT_OFFSET_RNGS + (0 * TRNG_CORE_SIZE) +#define TRNG_ADDR_BASE SEGMENT_OFFSET_RNGS + (0x00 * TRNG_CORE_SIZE) #define TRNG_ADDR_NAME0 TRNG_ADDR_BASE + ADDR_NAME0 #define TRNG_ADDR_NAME1 TRNG_ADDR_BASE + ADDR_NAME1 #define TRNG_ADDR_VERSION TRNG_ADDR_BASE + ADDR_VERSION @@ -152,7 +177,7 @@ // no status bits defined (yet) #define TRNG_ADDR_DELAY TRNG_ADDR_BASE + 0x13 -#define ENTROPY1_ADDR_BASE SEGMENT_OFFSET_RNGS + (5 * TRNG_CORE_SIZE) +#define ENTROPY1_ADDR_BASE SEGMENT_OFFSET_RNGS + (0x05 * TRNG_CORE_SIZE) #define ENTROPY1_ADDR_NAME0 ENTROPY1_ADDR_BASE + ADDR_NAME0 #define ENTROPY1_ADDR_NAME1 ENTROPY1_ADDR_BASE + ADDR_NAME1 #define ENTROPY1_ADDR_VERSION ENTROPY1_ADDR_BASE + ADDR_VERSION @@ -163,7 +188,7 @@ #define ENTROPY1_ADDR_ENTROPY ENTROPY1_ADDR_BASE + 0x20 #define ENTROPY1_ADDR_DELTA ENTROPY1_ADDR_BASE + 0x30 -#define ENTROPY2_ADDR_BASE SEGMENT_OFFSET_RNGS + (6 * TRNG_CORE_SIZE) +#define ENTROPY2_ADDR_BASE SEGMENT_OFFSET_RNGS + (0x06 * TRNG_CORE_SIZE) #define ENTROPY2_ADDR_NAME0 ENTROPY2_ADDR_BASE + ADDR_NAME0 #define ENTROPY2_ADDR_NAME1 ENTROPY2_ADDR_BASE + ADDR_NAME1 #define ENTROPY2_ADDR_VERSION ENTROPY2_ADDR_BASE + ADDR_VERSION @@ -202,6 +227,48 @@ #define CSPRNG_ADDR_NBLOCKS_LO CSPRNG_ADDR_BASE + 0x41 #define CSPRNG_ADDR_NBLOCKS_HI CSPRNG_ADDR_BASE + 0x42 +#define TRNG_NAME0 "trng" +#define TRNG_NAME1 " " +#define TRNG_VERSION "0.50" + +#define AVALANCHE_ENTROPY_NAME0 "extn" +#define AVALANCHE_ENTROPY_NAME1 "oise" +#define AVALANCHE_ENTROPY_VERSION "0.10" + +#define ROSC_ENTROPY_NAME0 "rosc" +#define ROSC_ENTROPY_NAME1 " ent" +#define ROSC_ENTROPY_VERSION "0.10" + +#define CSPRNG_NAME0 "cspr" +#define CSPRNG_NAME1 "ng " +#define CSPRNG_VERSION "0.50" + + +// ----------------------------------------------------------------- +// MATH segment. +// ----------------------------------------------------------------- +#define MATH_CORE_SIZE 0x100 + +// addresses and codes for the MATH cores */ +#define MODEXP_ADDR_BASE SEGMENT_OFFSET_MATH + (0x00 * MATH_CORE_SIZE) +#define MODEXP_ADDR_NAME0 MODEXP_ADDR_BASE + ADDR_NAME0 +#define MODEXP_ADDR_NAME1 MODEXP_ADDR_BASE + ADDR_NAME1 +#define MODEXP_ADDR_VERSION MODEXP_ADDR_BASE + ADDR_VERSION +#define MODEXP_ADDR_CTRL MODEXP_ADDR_BASE + 0x08 +#define MODEXP_CTRL_INIT 1 +#define MODEXP_CTRL_NEXT 2 +#define MODEXP_ADDR_STATUS MODEXP_ADDR_BASE + 0x09 + +#define MODEXP_ADDR_DELAY MODEXP_ADDR_BASE + 0x13 +#define MODEXP_STATUS_READY 1 + +#define MODEXP_MODULUS_LENGTH MODEXP_ADDR_BASE + 0x20 +#define MODEXP_EXPONENT_LENGTH MODEXP_ADDR_BASE + 0x21 + +#define MODEXP_NAME0 "mode" +#define MODEXP_NAME1 "xp " +#define MODEXP_VERSION "0.50" + //------------------------------------------------------------------ // Test case public functions @@ -44,10 +44,10 @@ #include <unistd.h> #include <time.h> #include <sys/time.h> -#include <linux/i2c-dev.h> #include <sys/ioctl.h> #include <arpa/inet.h> #include <ctype.h> +#include <stdint.h> #include <assert.h> #include "cryptech.h" @@ -202,7 +202,7 @@ static int hash(char *algo, char *file, uint8_t *digest) * cores always assert valid before ready. */ if (tc_read(daddr, digest, dlen) != 0) { - perror("i2c read failed"); + perror("eim read failed"); goto out; } diff --git a/sw/hash_tester.c b/sw/hash_tester.c index 6ddc09a..3df62d3 100644 --- a/sw/hash_tester.c +++ b/sw/hash_tester.c @@ -229,39 +229,26 @@ const uint8_t SHA512_DOUBLE_DIGEST[] = int TC0() { - uint8_t board_name0[4] = "PVT1"; - uint8_t board_name1[4] = " "; - uint8_t board_version[4] = "0.10"; - - uint8_t comm_name0[4] = "eim "; - uint8_t comm_name1[4] = " "; - uint8_t comm_version[4] = "0.10"; - + uint8_t board_name0[4] = NOVENA_BOARD_NAME0; + uint8_t board_name1[4] = NOVENA_BOARD_NAME1; + uint8_t board_version[4] = NOVENA_BOARD_VERSION; uint8_t t[4]; if (!quiet) - printf("TC0-1: Reading board type, version, and dummy reg from global registers.\n"); + printf("TC0: Reading board type, version, and dummy reg from global registers.\n"); /* write current time into dummy register, then try to read it back * to make sure that we can actually write something into EIM */ (void)time((time_t *)t); - if (tc_write(BOARD_ADDR_DUMMY, (void *)&t, 4) != 0) + if (tc_write(BOARD_ADDR_DUMMY, t, 4) != 0) return 1; - if (tc_expected(BOARD_ADDR_NAME0, board_name0, 4) || + return + tc_expected(BOARD_ADDR_NAME0, board_name0, 4) || tc_expected(BOARD_ADDR_NAME1, board_name1, 4) || tc_expected(BOARD_ADDR_VERSION, board_version, 4) || - tc_expected(BOARD_ADDR_DUMMY, (void *)t, 4)) - return 1; - - if (!quiet) - printf("TC0-2: Reading name and version words from communications core.\n"); - - return - tc_expected(COMM_ADDR_NAME0, comm_name0, 4) || - tc_expected(COMM_ADDR_NAME1, comm_name1, 4) || - tc_expected(COMM_ADDR_VERSION, comm_version, 4); + tc_expected(BOARD_ADDR_DUMMY, t, 4); } /* ---------------- SHA-1 test cases ---------------- */ @@ -269,9 +256,9 @@ int TC0() /* TC1: Read name and version from SHA-1 core. */ int TC1(void) { - uint8_t name0[4] = "sha1"; - uint8_t name1[4] = " "; - uint8_t version[4] = "0.50"; + uint8_t name0[4] = SHA1_NAME0; + uint8_t name1[4] = SHA1_NAME1; + uint8_t version[4] = SHA1_VERSION; if (!quiet) printf("TC1: Reading name and version words from SHA-1 core.\n"); @@ -338,12 +325,12 @@ int TC3(void) /* TC4: Read name and version from SHA-256 core. */ int TC4(void) { - uint8_t name0[4] = "sha2"; - uint8_t name1[4] = "-256"; - uint8_t version[4] = "0.80"; + uint8_t name0[4] = SHA256_NAME0; + uint8_t name1[4] = SHA256_NAME1; + uint8_t version[4] = SHA256_VERSION; if (!quiet) - printf("TC4: Reading name, type and version words from SHA-256 core.\n"); + printf("TC4: Reading name and version words from SHA-256 core.\n"); return tc_expected(SHA256_ADDR_NAME0, name0, 4) || @@ -455,12 +442,12 @@ int TC7() /* TC8: Read name and version from SHA-512 core. */ int TC8() { - uint8_t name0[4] = "sha2"; - uint8_t name1[4] = "-512"; - uint8_t version[4] = "0.80"; + uint8_t name0[4] = SHA512_NAME0; + uint8_t name1[4] = SHA512_NAME1; + uint8_t version[4] = SHA512_VERSION; if (!quiet) - printf("TC8: Reading name, type and version words from SHA-512 core.\n"); + printf("TC8: Reading name and version words from SHA-512 core.\n"); return tc_expected(SHA512_ADDR_NAME0, name0, 4) || diff --git a/sw/novena-eim.h b/sw/novena-eim.h index 75613bf..dcceaf4 100644 --- a/sw/novena-eim.h +++ b/sw/novena-eim.h @@ -1,12 +1,12 @@ -/* +/* * novena-eim.h * ------------ * This module contains the userland magic to set up and use the EIM bus. * - * + * * Author: Pavel Shatov * Copyright (c) 2014-2015, NORDUnet A/S All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: @@ -34,6 +34,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <stdint.h> #define EIM_BASE_ADDR 0x08000000 /* Set up EIM bus. diff --git a/sw/trng_extractor.c b/sw/trng_extractor.c index f71a5d6..5c373f4 100644 --- a/sw/trng_extractor.c +++ b/sw/trng_extractor.c @@ -42,7 +42,7 @@ #include "cryptech.h" -char *usage = +char *usage = "%s [-a|r|c] [-n #] [-o file]\n\ \n\ -a avalanche entropy\n\ @@ -52,6 +52,30 @@ char *usage = -o output file (defaults to stdout)\n\ "; +/* check availability of avalanche entropy core by reading core name and version */ +static int avalanche_check(void) +{ + return + tc_expected(ENTROPY1_ADDR_NAME0, (const uint8_t *)AVALANCHE_ENTROPY_NAME0, 4) || + tc_expected(ENTROPY1_ADDR_NAME1, (const uint8_t *)AVALANCHE_ENTROPY_NAME1, 4); +} + +/* check availability of rosc core by reading the core name and version */ +static int rosc_check(void) +{ + return + tc_expected(ENTROPY2_ADDR_NAME0, (const uint8_t *)ROSC_ENTROPY_NAME0, 4) || + tc_expected(ENTROPY2_ADDR_NAME1, (const uint8_t *)ROSC_ENTROPY_NAME1, 4); +} + +/* check availability of csprng core by reading the core name and version */ +static int csprng_check(void) +{ + return + tc_expected(CSPRNG_ADDR_NAME0, (const uint8_t *)CSPRNG_NAME0, 4) || + tc_expected(CSPRNG_ADDR_NAME1, (const uint8_t *)CSPRNG_NAME1, 4); +} + /* extract one data sample */ static int extract(off_t status_addr, off_t data_addr, uint32_t *data) { @@ -79,6 +103,12 @@ int main(int argc, char *argv[]) FILE *output = stdout; uint32_t data; + // Check that we have can talk to the trng. + if (avalanche_check() || rosc_check() || csprng_check()) { + fprintf(stderr, "Can't properly access the trng.\n"); + return EXIT_FAILURE; + } + /* parse command line */ while ((opt = getopt(argc, argv, "h?arcn:o:")) != -1) { switch (opt) { @@ -99,23 +129,23 @@ int main(int argc, char *argv[]) data_addr = CSPRNG_ADDR_RANDOM; break; case 'n': - num_words = strtoul(optarg, &endptr, 10); - switch (toupper(*endptr)) { - case '\0': - break; - case 'K': - num_words *= 1000; - break; - case 'M': - num_words *= 1000000; - break; - case 'G': - num_words *= 1000000000; - break; - default: - fprintf(stderr, "unsupported -n suffix %s\n", endptr); - return EXIT_FAILURE; - } + num_words = strtoul(optarg, &endptr, 10); + switch (toupper(*endptr)) { + case '\0': + break; + case 'K': + num_words *= 1000; + break; + case 'M': + num_words *= 1000000; + break; + case 'G': + num_words *= 1000000000; + break; + default: + fprintf(stderr, "unsupported -n suffix %s\n", endptr); + return EXIT_FAILURE; + } break; case 'o': output = fopen(optarg, "wb+"); diff --git a/sw/trng_tester.c b/sw/trng_tester.c index f0b80d0..715a20c 100644 --- a/sw/trng_tester.c +++ b/sw/trng_tester.c @@ -61,39 +61,26 @@ int wait_stats = 0; int TC0() { - uint8_t board_name0[4] = "PVT1"; - uint8_t board_name1[4] = " "; - uint8_t board_version[4] = "0.10"; - - uint8_t comm_name0[4] = "eim "; - uint8_t comm_name1[4] = " "; - uint8_t comm_version[4] = "0.10"; - + uint8_t name0[4] = NOVENA_BOARD_NAME0; + uint8_t name1[4] = NOVENA_BOARD_NAME1; + uint8_t version[4] = NOVENA_BOARD_VERSION; uint8_t t[4]; if (!quiet) - printf("TC0-1: Reading board type, version, and dummy reg from global registers.\n"); + printf("TC0: Reading board type, version, and dummy reg from global registers.\n"); /* write current time into dummy register, then try to read it back * to make sure that we can actually write something into EIM */ (void)time((time_t *)t); - if (tc_write(BOARD_ADDR_DUMMY, (void *)&t, 4) != 0) - return 1; - - if (tc_expected(BOARD_ADDR_NAME0, board_name0, 4) || - tc_expected(BOARD_ADDR_NAME1, board_name1, 4) || - tc_expected(BOARD_ADDR_VERSION, board_version, 4) || - tc_expected(BOARD_ADDR_DUMMY, (void *)t, 4)) + if (tc_write(BOARD_ADDR_DUMMY, t, 4) != 0) return 1; - if (!quiet) - printf("TC0-2: Reading name and version words from communications core.\n"); - return - tc_expected(COMM_ADDR_NAME0, comm_name0, 4) || - tc_expected(COMM_ADDR_NAME1, comm_name1, 4) || - tc_expected(COMM_ADDR_VERSION, comm_version, 4); + tc_expected(BOARD_ADDR_NAME0, name0, 4) || + tc_expected(BOARD_ADDR_NAME1, name1, 4) || + tc_expected(BOARD_ADDR_VERSION, version, 4) || + tc_expected(BOARD_ADDR_DUMMY, t, 4); } /* ---------------- trng test cases ---------------- */ @@ -101,9 +88,9 @@ int TC0() /* TC1: Read name and version from trng core. */ int TC1(void) { - uint8_t name0[4] = "trng"; - uint8_t name1[4] = " "; - uint8_t version[4] = "0.01"; + uint8_t name0[4] = TRNG_NAME0; + uint8_t name1[4] = TRNG_NAME1; + uint8_t version[4] = TRNG_VERSION; if (!quiet) printf("TC1: Reading name and version words from trng core.\n"); @@ -122,9 +109,9 @@ int TC1(void) /* TC2: Read name and version from avalanche_entropy core. */ int TC2(void) { - uint8_t name0[4] = "extn"; - uint8_t name1[4] = "oise"; - uint8_t version[4] = "0.10"; + uint8_t name0[4] = AVALANCHE_ENTROPY_NAME0; + uint8_t name1[4] = AVALANCHE_ENTROPY_NAME1; + uint8_t version[4] = AVALANCHE_ENTROPY_VERSION; if (!quiet) printf("TC2: Reading name and version words from avalanche_entropy core.\n"); @@ -171,9 +158,9 @@ int TC3(void) /* TC4: Read name and version from rosc_entropy core. */ int TC4(void) { - uint8_t name0[4] = "rosc"; - uint8_t name1[4] = " ent"; - uint8_t version[4] = "0.10"; + uint8_t name0[4] = ROSC_ENTROPY_NAME0; + uint8_t name1[4] = ROSC_ENTROPY_NAME1; + uint8_t version[4] = ROSC_ENTROPY_VERSION; if (!quiet) printf("TC4: Reading name and version words from rosc_entropy core.\n"); @@ -220,8 +207,17 @@ int TC5(void) /* TC6: Read name and version from trng_csprng core. */ int TC6(void) { - /* XXX csprng core currently doesn't have name/version registers */ - return 0; + uint8_t name0[4] = CSPRNG_NAME0; + uint8_t name1[4] = CSPRNG_NAME1; + uint8_t version[4] = CSPRNG_VERSION; + + if (!quiet) + printf("TC6: Reading name and version words from trng_csprng core.\n"); + + return + tc_expected(CSPRNG_ADDR_NAME0, name0, 4) || + tc_expected(CSPRNG_ADDR_NAME1, name1, 4) || + tc_expected(CSPRNG_ADDR_VERSION, version, 4); } /* XXX clear 'enable' control bit, see if we read the same value */ |