README.md
1# SiFive RISC-V HiFive1 Port
2
3## Overview
4You can enable the wolfSSL support for RISC-V using the `#define WOLFSSL_SIFIVE_RISC_V`.
5
6## Prerequisites
71. Follow the instructions on the SiFive GitHub [here](https://github.com/sifive/freedom-e-sdk) and SiFive website [here](https://www.sifive.com/) to download the freedom-e-sdk and software tools.
83. Run a simple hello application on your development board to confirm that your board functions as expected and the communication between your computer and the board works.
9
10## Usage
11You can start with a wolfcrypt example project to integrate the wolfSSL source code.
12wolfSSL supports a compile-time user configurable options in the `IDE/RISCV/SIFIVE-HIFIVE1/user_settings.h` file.
13
14The `IDE/RISCV/SIFIVE-HIFIVE1/main.c` example application provides a function to run the selected examples at compile time through the following two #defines in user_settings.h. You can define these macro options to disable the test run.
15```
16- #undef NO_CRYPT_TEST
17- #undef NO_CRYPT_BENCHMARK
18```
19
20## Setup
21### Setting up the SDK with wolfSSL
221. Download the wolfSSL source code or a zip file from GitHub and place it under your SDK `$HOME` directory. You can also copy or simlink to the source.
23```
24 For example,
25 $ cd $HOME
26 $ git clone --depth=1 https://github.com/wolfSSL/wolfssl.git
27
28```
292. Copy the wolfcrypt example project into your `freedom-e-sdk/software` directory.
30
31```
32 $ cp -rf ~/wolfssl/IDE/RISCV/SIFIVE-HIFIVE1 ~/freedom-e-sdk/software/wolfcrypt
33```
34
353. Edit your `~/freedom-e-sdk/scripts/standalone.mk` and add the following line after the last RISCV_CFLAGS entry:
36
37```
38 RISCV_CFLAGS += -I$(WOLFSSL_SRC_DIR) -I$(WOLFSSL_SRC_DIR)/IDE/RISCV/SIFIVE-HIFIVE1 -DWOLFSSL_USER_SETTINGS
39```
40
414. WOLFSSL_SRC_DIR variable must be set in the environment when GNU make is started.
42
43```
44 $ export WOLFSSL_SRC_DIR=~/wolfssl
45```
46
475. Setup your riscv64 compiler
48
49```
50 $ export RISCV_OPENOCD_PATH=/opt/riscv-openocd
51```
526. (Optional) Setup OpenOCD if your target supports it:
53
54```
55 $ export RISCV_OPENOCD_PATH=/opt/riscv-openocd
56```
57## Building and Running
58
59You can build from source or create a static library.
60
611. Using command-line:
62
63```
64 $ cd freedom-e-sdk
65 $ make PROGRAM=wolfcrypt TARGET=sifive-hifive1-revb CONFIGURATION=debug clean software upload
66```
67This example cleans, builds and uploads the software on the sifive-hifive1-revb target but you can also combine and build for any of the supported targets.
68
69Review the test results on the target console.
70
712. Building a static library for RISC-V using a cross-compiler:
72
73```
74$ cd $WOLFSSL_SRC_DIR
75
76$./configure --host=riscv64-unknown-elf \
77CC=riscv64-unknown-elf-gcc \
78AR=riscv64-unknown-elf-ar \
79AS=riscv64-unknown-elf-as \
80RANLIB=$RISCV_PATH/bin/riscv64-unknown-elf-gcc-ranlib \
81LD=riscv64-unknown-elf-ld \
82CXX=riscv64-unknown-elf-g++ \
83--disable-examples --enable-static --disable-shared \
84CFLAGS="-march=rv32imac -mabi=ilp32 -mcmodel=medlow -ffunction-sections -fdata-sections -I~/freedom-e-sdk/bsp/sifive-hifive1/install/include -O0 -g -DNO_FILESYSTEM -DWOLFSSL_NO_SOCK -DNO_WRITEV -DWOLFCRYPT_ONLY -DWOLFSSL_SIFIVE_RISC_V"
85
86$make
87$sudo make install
88```
89You can now build and link your software to the wolfSSL libwolfssl.a static library.
90
91### `wolfcrypt_test()`
92
93wolfcrypt_test() prints a message on the target console similar to the following output:
94
95```
96SiFive HiFive1 Demo
97Setting clock to 320MHz
98Actual Clock 320MHz
99
100error test passed!
101MEMORY test passed!
102base64 test passed!
103asn test passed!
104SHA test passed!
105SHA-256 test passed!
106SHA-512 test passed!
107Hash test passed!
108HMAC-SHA test passed!
109HMAC-SHA256 test passed!
110HMAC-SHA512 test passed!
111GMAC test passed!
112Chacha test passed!
113POLY1305 test passed!
114ChaCha20-Poly1305 AEAD test passed!
115AES test passed!
116AES192 test passed!
117AES256 test passed!
118AES-GCM test passed!
119RANDOM test passed!
120ECC test passed!
121ECC buffer test passed!
122CURVE25519 test passed!
123ED25519 test passed!
124logging test passed!
125mutex test passed!
126Test complete
127```
128### `benchmark_test()`
129
130benchmark_test() prints a message on the target console similar to the following output.
131
132TARGET=sifive-hifive1-revb:
133
134```
135SiFive HiFive1 Demo
136Setting clock to 320MHz
137Actual Clock 320MHz
138
139------------------------------------------------------------------------------
140 wolfSSL version 4.0.0
141------------------------------------------------------------------------------
142wolfCrypt Benchmark (block bytes 1024, min 1.0 sec each)
143RNG 250 KB took 1.098 seconds, 227.714 KB/s
144AES-128-CBC-enc 50 KB took 1.132 seconds, 44.175 KB/s
145AES-128-CBC-dec 50 KB took 1.142 seconds, 43.778 KB/s
146AES-192-CBC-enc 50 KB took 1.250 seconds, 40.007 KB/s
147AES-192-CBC-dec 50 KB took 1.260 seconds, 39.677 KB/s
148AES-256-CBC-enc 50 KB took 1.368 seconds, 36.552 KB/s
149AES-256-CBC-dec 50 KB took 1.378 seconds, 36.279 KB/s
150AES-128-GCM-enc 25 KB took 1.225 seconds, 20.412 KB/s
151AES-128-GCM-dec 25 KB took 1.225 seconds, 20.402 KB/s
152AES-192-GCM-enc 25 KB took 1.290 seconds, 19.373 KB/s
153AES-192-GCM-dec 25 KB took 1.291 seconds, 19.366 KB/s
154AES-256-GCM-enc 25 KB took 1.352 seconds, 18.487 KB/s
155AES-256-GCM-dec 25 KB took 1.353 seconds, 18.478 KB/s
156CHACHA 1 MB took 1.006 seconds, 1.020 MB/s
157CHA-POLY 700 KB took 1.032 seconds, 678.045 KB/s
158POLY1305 2 MB took 1.007 seconds, 2.255 MB/s
159SHA 2 MB took 1.002 seconds, 1.511 MB/s
160SHA-256 525 KB took 1.011 seconds, 519.279 KB/s
161SHA-512 275 KB took 1.017 seconds, 270.477 KB/s
162HMAC-SHA 1 MB took 1.013 seconds, 1.399 MB/s
163HMAC-SHA256 525 KB took 1.019 seconds, 515.020 KB/s
164HMAC-SHA512 275 KB took 1.032 seconds, 266.351 KB/s
165ECC 256 key gen 2 ops took 1.104 sec, avg 551.834 ms, 1.812 ops/sec
166ECDHE 256 agree 2 ops took 1.101 sec, avg 550.400 ms, 1.817 ops/sec
167ECDSA 256 sign 2 ops took 1.173 sec, avg 586.502 ms, 1.705 ops/sec
168ECDSA 256 verify 2 ops took 2.153 sec, avg 1076.294 ms, 0.929 ops/sec
169CURVE 25519 key gen 2 ops took 1.629 sec, avg 814.423 ms, 1.228 ops/sec
170CURVE 25519 agree 2 ops took 1.626 sec, avg 813.156 ms, 1.230 ops/sec
171ED 25519 key gen 1 ops took 1.436 sec, avg 1436.096 ms, 0.696 ops/sec
172ED 25519 sign 2 ops took 2.913 sec, avg 1456.421 ms, 0.687 ops/sec
173ED 25519 verify 2 ops took 5.012 sec, avg 2506.012 ms, 0.399 ops/sec
174Benchmark complete
175```
176
177## Tested Configurations
178- P-RNG (NIST DRBG) with SHA-256
179- SHA 1/256/512
180- AES 128/192/256 CBC/GCM
181- ECC 256 sign/verify/shared secret with fast math or Single Precision (SP) library
182- ED25519/Curve25519
183- HMAC
184- ChaCha20/Poly1305
185
186## Known Caveats
187- If you find the wolfcrypt test stuck on early_trap_vector error, it is like related to memory issues
188- Using the `__stack_size` default value of 0x400 will not be enough for the ECC test to pass.
189The `IDE/RISCV/SIFIVE-HIFIVE1/Makefile` overwrites the value with 0x1000 (4 KBytes)
190- Enabling RSA will cause the ECC test to fail due to memory shortage.
191
192## References
193
194The test results were collected from a SiFive reference platform target with the following hardware, software and tool chains:
195- HiFive1 Rev A/Rev B: HiFive1 Development Board with the Freedom Everywhere SoC, E300
196- freedom-e-sdk
197- wolfssl [latest version](https://github.com/wolfSSL/wolfssl)
198
199For more information or questions, please email [support@wolfssl.com](mailto:support@wolfssl.com)
200