|
| 1 | +// Copyright lowRISC contributors (OpenTitan project). |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +use anyhow::Result; |
| 6 | +use arrayvec::ArrayVec; |
| 7 | +use hex::FromHex; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | +use std::time::Duration; |
| 10 | + |
| 11 | +use cryptotest_commands::commands::CryptotestCommand; |
| 12 | +use cryptotest_commands::x25519_commands::{ |
| 13 | + CryptotestX25519KexOutput, CryptotestX25519PublicKey, X25519Subcommand, |
| 14 | +}; |
| 15 | + |
| 16 | +use opentitanlib::console::spi::SpiConsoleDevice; |
| 17 | +use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend}; |
| 18 | + |
| 19 | +// Input structs (from x25519_vectors.hjson) |
| 20 | + |
| 21 | +#[derive(Deserialize, PartialEq, Serialize)] |
| 22 | +#[serde(rename_all = "camelCase")] |
| 23 | +struct X25519TestCase { |
| 24 | + tc_id: usize, |
| 25 | + public_server: String, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Deserialize, PartialEq, Serialize)] |
| 29 | +#[serde(rename_all = "camelCase")] |
| 30 | +struct X25519TestGroup { |
| 31 | + tg_id: usize, |
| 32 | + #[allow(dead_code)] |
| 33 | + test_type: String, |
| 34 | + #[allow(dead_code)] |
| 35 | + curve: String, |
| 36 | + tests: Vec<X25519TestCase>, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Deserialize, PartialEq, Serialize)] |
| 40 | +#[serde(rename_all = "camelCase")] |
| 41 | +pub struct X25519TestVectorSet { |
| 42 | + vs_id: usize, |
| 43 | + algorithm: String, |
| 44 | + mode: String, |
| 45 | + revision: String, |
| 46 | + #[serde(default)] |
| 47 | + is_sample: bool, |
| 48 | + test_groups: Vec<X25519TestGroup>, |
| 49 | +} |
| 50 | + |
| 51 | +// Output structs (for the JSON result file) |
| 52 | + |
| 53 | +#[derive(Deserialize, PartialEq, Serialize)] |
| 54 | +#[serde(rename_all = "camelCase")] |
| 55 | +struct X25519ResultCase { |
| 56 | + tc_id: usize, |
| 57 | + public_iut: String, |
| 58 | + z: String, |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Deserialize, PartialEq, Serialize)] |
| 62 | +#[serde(rename_all = "camelCase")] |
| 63 | +struct X25519ResultGroup { |
| 64 | + tg_id: usize, |
| 65 | + tests: Vec<X25519ResultCase>, |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Deserialize, PartialEq, Serialize)] |
| 69 | +#[serde(rename_all = "camelCase")] |
| 70 | +pub struct X25519ResultVectorSet { |
| 71 | + vs_id: usize, |
| 72 | + algorithm: String, |
| 73 | + mode: String, |
| 74 | + revision: String, |
| 75 | + #[serde(default)] |
| 76 | + is_sample: bool, |
| 77 | + test_groups: Vec<X25519ResultGroup>, |
| 78 | +} |
| 79 | + |
| 80 | +fn run_x25519_case( |
| 81 | + timeout: Duration, |
| 82 | + spi_console: &SpiConsoleDevice, |
| 83 | + tc: &X25519TestCase, |
| 84 | +) -> Result<X25519ResultCase> { |
| 85 | + let public_server = Vec::<u8>::from_hex(&tc.public_server)?; |
| 86 | + |
| 87 | + CryptotestCommand::X25519.send(spi_console)?; |
| 88 | + X25519Subcommand::X25519KEX.send(spi_console)?; |
| 89 | + |
| 90 | + CryptotestX25519PublicKey { |
| 91 | + public_key: ArrayVec::try_from(public_server.as_slice()) |
| 92 | + .map_err(|_| std::io::Error::other("X25519 server public key unexpected length"))?, |
| 93 | + public_key_len: public_server.len(), |
| 94 | + } |
| 95 | + .send(spi_console)?; |
| 96 | + |
| 97 | + let output = CryptotestX25519KexOutput::recv(spi_console, timeout, false, false)?; |
| 98 | + |
| 99 | + let public_iut = &output.public_key[..output.public_key_len]; |
| 100 | + let z = &output.shared_secret[..output.shared_secret_len]; |
| 101 | + |
| 102 | + Ok(X25519ResultCase { |
| 103 | + tc_id: tc.tc_id, |
| 104 | + public_iut: hex::encode_upper(public_iut), |
| 105 | + z: hex::encode_upper(z), |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +fn run_x25519_group( |
| 110 | + timeout: Duration, |
| 111 | + spi_console: &SpiConsoleDevice, |
| 112 | + tg: &X25519TestGroup, |
| 113 | +) -> Result<X25519ResultGroup> { |
| 114 | + log::info!("tg_id: {}", tg.tg_id); |
| 115 | + |
| 116 | + let mut result_cases = Vec::new(); |
| 117 | + for tc in &tg.tests { |
| 118 | + log::info!("tc_id: {}", tc.tc_id); |
| 119 | + result_cases.push(run_x25519_case(timeout, spi_console, tc)?); |
| 120 | + } |
| 121 | + |
| 122 | + Ok(X25519ResultGroup { |
| 123 | + tg_id: tg.tg_id, |
| 124 | + tests: result_cases, |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +pub fn run_x25519_vector_set( |
| 129 | + timeout: Duration, |
| 130 | + spi_console: &SpiConsoleDevice, |
| 131 | + vs: &X25519TestVectorSet, |
| 132 | +) -> Result<X25519ResultVectorSet> { |
| 133 | + log::info!("vs_id: {}", vs.vs_id); |
| 134 | + |
| 135 | + let mut result_groups = Vec::with_capacity(vs.test_groups.len()); |
| 136 | + for tg in &vs.test_groups { |
| 137 | + result_groups.push(run_x25519_group(timeout, spi_console, tg)?); |
| 138 | + } |
| 139 | + |
| 140 | + Ok(X25519ResultVectorSet { |
| 141 | + vs_id: vs.vs_id, |
| 142 | + algorithm: vs.algorithm.clone(), |
| 143 | + mode: vs.mode.clone(), |
| 144 | + revision: vs.revision.clone(), |
| 145 | + is_sample: vs.is_sample, |
| 146 | + test_groups: result_groups, |
| 147 | + }) |
| 148 | +} |
0 commit comments