Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zvk code samples / Proof of Concept (v2) #310

Merged
merged 12 commits into from
May 9, 2023

Conversation

egouriou-rivos
Copy link
Contributor

@egouriou-rivos egouriou-rivos commented Apr 3, 2023

Code samples covering the Zvk extensions Zvbb, Zvbc, Zvkg, Zvkned, Zvknh[ab], Zvksed, Zvksh.

The code used to live in https://github.com/rivosinc/riscv-code-samples. As part of the architecture review, there has been a request to move it to riscv/riscv-crypto. Hence this PR.

Changes in V2:

  • Fixed the logic for the vwsll test to properly mask the shift amount by 2*SEW bits instead of SEW bits, following a spec fix in the pseudo-code
  • Fixed the many typos reported by @nazar-codethink
  • Moved the definition of LMUL != 1 AES routines to the proper commit (the initial AES one)

Given the vwsll fix, this PR matches the Zvk specification version 0.9.1, 2023-04-25, freeze candidate.

Known issues remaining in V2:

  • The license file and license mentions in source files is inherited from the previous existence in rivosinc/riscv-code-samples. The intent is to be covered by the existing repository license.

egouriou-rivos and others added 3 commits April 3, 2023 02:28
Add Apache 2.0 license file.

The README explains the contents of this repository and how to use it.
It refers to content that will be added in later commits.

The .gitignore contains references to built files that will be added
in upcoming commits.

Signed-off-by: Eric Gouriou <ego@rivosinc.com>
Signed-off-by: Kornel Duleba <mindal@semihalf.com>
Signed-off-by: Stanislaw Kardach <kda@semihalf.com>
The test vectors were downloaded from offical NIST website.
They're stored in form of multiple ZIP archives, e.g. KAT_AES.zip.
After being unpacked test vectors from each archive are stored
in a separate directory.

Signed-off-by: Eric Gouriou <ego@rivosinc.com>
Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
It simply parses test vectors as defined by NIST and creates a dict.
The license part of the file was left intact.

Signed-off-by: Eric Gouriou <ego@rivosinc.com>
Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
@egouriou-rivos egouriou-rivos marked this pull request as ready for review April 3, 2023 09:40
@egouriou-rivos
Copy link
Contributor Author

@kdockser Ken, can you please assign to an appropriate contributor?

Copy link
Contributor

@nibrunieAtSi5 nibrunieAtSi5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the tests make assumption on a minimal VLEN value which is not always the one mandated by the standard. Would it be possible to increase the LMUL values (while keeping vl untouched) and to check register index values to make sure those tests can run on implementations with VLEN values supported by the standard ? (and to modify the min_vlen values accordingly)


- [1] https://datatracker.ietf.org/doc/html/draft-oscca-cfrg-sm3-00
- [2] https://datatracker.ietf.org/doc/html/draft-ribose-cfrg-sm4-10
- [3] https://github.com/rivosinc/binutils-gdb/tree/vector-crypto

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Links 3 and 4 are dead links

@nibrunieAtSi5
Copy link
Contributor

It looks like there is at least one sample for vghsh / Zvkg but the extension is not listed in the PR description. Is it covered by this PR ?

Parse the text files with test vectors and generate C headers that can
later be used by a test framework.
NIST KAT test vectors are used to provide ample coverage.
For now parsing of AES-CBC, AES-GCM, SHA-128 and SHA-256 is supported.

Signed-off-by: Eric Gouriou <ego@rivosinc.com>
Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
The zvbb-test.c file contains a set of functions verifying and
showcasing usage of instructions defined by the Zvbb extension.

Pre-requisites:

- riscv64-linux-gnu toolchain available in PATH.
- Zvbb-enabled binutils-gdb available in PATH overriding the above
  toolchain.
- Zvbb-enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:
    # Build and run Zvbb tests with default parameters
    make run-zvbb
    # Override riscv-pk location
    make run-zvbb PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-zvbb TARGET=riscv64-unknown-linux-gnu \
                  PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:
- zvbb-test - compiles the PoC code.
- run-zvbb - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:
- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

Signed-off-by: Stanislaw Kardach <kda@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
The zvbc-test.c file contains a set of functions verifying and
showcasing usage of instructions defined by the Zvbc extension.

Pre-requisites:

- riscv64-linux-gnu toolchain available in PATH.
- Zvbc-enabled binutils-gdb available in PATH overriding the above
  toolchain.
- Zvbc-enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:
    # Build and run Zvbc tests with default parameters
    make run-zvbc
    # Override riscv-pk location
    make run-zvbc PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-zvbc TARGET=riscv64-unknown-linux-gnu \
                  PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:
- zvbc-test - compiles the PoC code.
- run-zvbc - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:
- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

Signed-off-by: Stanislaw Kardach <kda@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
The aes-cbc-test.c file contains a set of functions verifying and
showcasing usage of instructions defined by the Zvkned extension to
implement the AES-CBC algorithm routines.

Pre-requisites:

- riscv64-linux-gnu toolchain available in PATH.
- Zvkned-enabled binutils-gdb available in PATH overriding the above
  toolchain.
- Zvkned-enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:

    # Build and run Zvkned tests with default parameters
    make run-aes-cbc
    # Override riscv-pk location
    make run-aes-cbc PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-aes-cbc TARGET=riscv64-unknown-linux-gnu \
                     PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:

- aes-cbc-test - compiles the PoC code.
- run-aes-cbc - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:

- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
Signed-off-by: Stanislaw Kardach <kda@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
The aes-gcm-test.c file contains a set of functions verifying and
showcasing usage of instructions defined by the Zvkned and Zvbb/Zvbc
extensions to implement AES-GCM algorithm routines.

Pre-requisites:
- riscv64-linux-gnu toolchain available in PATH.
- zvkned, zvbb, and zvbc enabled binutils-gdb available
  in PATH overriding the above toolchain.
- zvkned, zvbb, and zbbc enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:
    # Build and run aes-gcm-test with default parameters
    make run-aes-gcm
    # Override riscv-pk location
    make run-aes-gcm PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-aes-gcm TARGET=riscv64-unknown-linux-gnu \
                     PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:
- aes-gcm-test - compiles the PoC code.
- run-aes-gcm - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:
- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
The GHASH part of AES-GCM is now run twice using instructions from Zvb*
and Zvkg extensions and then compared to ensure result validity.

Pre-requisites:

- riscv64-linux-gnu toolchain available in PATH.
- Zvkg-enabled binutils-gdb available in PATH overriding the above
  toolchain.
- Zvkg-enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:
    # Build and run Zvkns+Zvkg tests with default parameters
    make run-aes-gcm
    # Override riscv-pk location
    make run-aes-gcm PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-aes-gcm TARGET=riscv64-unknown-linux-gnu \
                  PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:
- aes-gcm-test - compiles the PoC code.
- run-aes-gcm - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:
- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

Signed-off-by: Eric Gouriou <ego@rivosinc.com>
Signed-off-by: Kornel Duleba <mindal@semihalf.com>
The sha-test.c file contains a set of functions verifying and
showcasing usage of instructions defined by the Zvknha/Zvknhb
extensions to implement the SHA-128 and SHA-256 algorithm routines.

Pre-requisites:
- riscv64-linux-gnu toolchain available in PATH.
- zvknh[ab]-enabled binutils-gdb available in PATH overriding the above
  toolchain.
- zvknh[ab]-enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:
    # Build and run zvknh[ab] tests with default parameters
    make run-sha
    # Override riscv-pk location
    make run-sha PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-sha TARGET=riscv64-unknown-linux-gnu \
                 PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:
- sha-test - compiles the PoC code.
- run-sha - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:
- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
The sm3-test.c file contains a set of functions verifying and
showcasing usage of instructions implementing SM3 algorithm (see [1])
defined by the Zvksh extension (see [2]).

Pre-requisites:
- riscv64-linux-gnu toolchain available in PATH.
- Zvksh-enabled binutils-gdb available in PATH overriding the above
  toolchain.
- Zvksh-enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:
    # Build and run Zvksh tests with default parameters
    make run-sm3
    # Override riscv-pk location
    make run-sm3 PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-sm3 TARGET=riscv64-unknown-linux-gnu \
                 PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:
- sm3-test - compiles the PoC code.
- run-sm3 - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:
- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

[1] https://datatracker.ietf.org/doc/html/draft-oscca-cfrg-sm3-00
[2] https://github.com/riscv/riscv-crypto/releases

Signed-off-by: Eric Gouriou <ego@rivosinc.com>
Signed-off-by: Albert Jakieła <aja@semihalf.com>
Signed-off-by: Kornel Duleba <mindal@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
The sm4-test.c file contains a set of functions verifying and
showcasing usage of instructions implementing SM4 algorithm (see [1])
defined by the Zvksed extension (see [2]).

Pre-requisites:

- riscv64-linux-gnu toolchain available in PATH.
- Zvksed-enabled binutils-gdb available in PATH overriding the above
  toolchain.
- Zvksed-enabled Spike available in PATH.
- RISC-V Proxy kernel (riscv-pk) compiled and available in
  ~/RISC-V/riscv64-linux-gnu/bin/pk (can be overridden with PK make
  variable).

The default make target will compile all examples and run them in Spike
simulator.

Example make invocation:
    # Build and run Zvksed tests with default parameters
    make run-sm4
    # Override riscv-pk location
    make run-sm4 PK=/opt/prefix/riscv64-linux-gnu/bin/pk
    # Override target triplet and riscv-pk location
    make run-sm4 TARGET=riscv64-unknown-linux-gnu \
                 PK=/opt/prefix/riscv64-linux-gnu/bin/pk

Make targets:
- sm4-test - compiles the PoC code.
- run-sm4 - Executes the compiled binary in Spike.
- clean - Cleans the build artifacts.

Make variables:
- TARGET - Target triplet to use. By default riscv64-linux-gnu.
- PK - Location of the riscv-pk binary. By default it's
  ~/RISC-V/$(TARGET)/bin/pk

See Makefile for more details.

[1] https://datatracker.ietf.org/doc/html/draft-ribose-cfrg-sm4-10
[2] https://github.com/riscv/riscv-crypto/releases

Signed-off-by: Albert Jakieła <aja@semihalf.com>
Signed-off-by: Eric Gouriou <ego@rivosinc.com>
@egouriou-rivos egouriou-rivos changed the title Zvk code samples / Proof of Concept (v1) Zvk code samples / Proof of Concept (v2) May 2, 2023
@egouriou-rivos
Copy link
Contributor Author

It looks like the tests make assumption on a minimal VLEN value which is not always the one mandated by the standard. Would it be possible to increase the LMUL values (while keeping vl untouched) and to check register index values to make sure those tests can run on implementations with VLEN values supported by the standard ? (and to modify the min_vlen values accordingly)

I am not sure what tests you are referring to. I believe all tests that require VLEN>128 (sha512) now have alternate versions that use LMUL>1 to allow for smaller VLEN, even smaller than 128 in some cases (although not SHA-512).

There was an issue in the previous version of those commits where the "base" AES commit only had LMUL=1 versions and the LMUL>1 versions were added in a later commit. That was my mistake and I massaged the commits to introduce the multiple variants in the initial AES commit. My apologies if this caused confusion (I certainly confused myself).

@egouriou-rivos
Copy link
Contributor Author

It looks like there is at least one sample for vghsh / Zvkg but the extension is not listed in the PR description. Is it covered by this PR ?

Thanks, I updated the PR to mention Zvkg, even though only vghsh is covered.

@nazar-codethink
Copy link

I'm just a passer by, but how do people review the tests without building them? I couldn't build them when I tried

@egouriou-rivos
Copy link
Contributor Author

egouriou-rivos commented May 2, 2023 via email

@kdockser kdockser merged commit dc2f94d into riscv:master May 9, 2023
@kdockser kdockser deleted the zvk-code-samples branch May 9, 2023 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants