initial frameworks around sha512 acceleration integration

This commit is contained in:
bunnie 2021-05-19 00:39:03 +08:00
parent 3f12b0a69b
commit 7d9e84c456
11 changed files with 1254 additions and 1 deletions

46
Cargo.lock generated
View file

@ -164,6 +164,15 @@ dependencies = [
"blitstr",
]
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "bstr"
version = "0.2.14"
@ -443,6 +452,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "downcast-rs"
version = "1.2.0"
@ -468,6 +486,28 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "engine-sha512"
version = "0.1.0"
dependencies = [
"bitflags",
"block-buffer",
"digest",
"log",
"log-server",
"num-derive",
"num-traits",
"opaque-debug",
"rkyv 0.4.3",
"susres",
"ticktimer-server",
"trng",
"utralib",
"xous",
"xous-ipc",
"xous-names",
]
[[package]]
name = "env_logger"
version = "0.7.1"
@ -984,6 +1024,12 @@ version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "orbclient"
version = "0.3.30"

View file

@ -24,6 +24,7 @@ default-members = [
"services/cb-test-c2",
"services/susres",
"services/codec",
"services/engine-sha512",
]
members = [
"xous-ipc",
@ -59,6 +60,7 @@ members = [
"services/cb-test-c2",
"services/susres",
"services/codec",
"services/engine-sha512",
]
resolver = "2"

View file

@ -394,7 +394,7 @@ fn xmain() -> ! {
xous::return_scalar2(msg.sender,
((x as usize) << 16) | y as usize,
((z as usize) << 16) | id as usize
).expect("coludn't return acceleromotere read data");
).expect("coludn't return accelerometer read data");
}),
Some(Opcode::BattStats) => {
info!("batt stats request received");

View file

@ -0,0 +1,32 @@
[package]
name = "engine-sha512"
version = "0.1.0"
authors = ["bunnie <bunnie@kosagi.com>"]
edition = "2018"
description = "Sha512 hardware accelerator engine"
[dependencies]
xous = { path = "../../xous-rs" }
log-server = { path = "../log-server" }
ticktimer-server = { path = "../ticktimer-server" }
xous-names = { path = "../xous-names" }
log = "0.4"
susres = {path = "../susres"}
bitflags = "1.2.1"
trng = { path = "../trng" }
xous-ipc = { path = "../../xous-ipc" }
num-derive = {version = "0.3", default-features = false}
num-traits = {version = "0.2", default-features = false}
rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
# Rust-standard digest API dependencies
digest = "0.9"
block-buffer = "0.9"
opaque-debug = "0.3" # prevents internal hash state leakage from debug structures
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}
[features]
default = []

View file

@ -0,0 +1,3 @@
A skeleton server, based on a snapshot of the Codec server before
it was fleshed out. Search and replace the word "codec" with your
server name and use it as a starting template.

View file

@ -0,0 +1,68 @@
pub(crate) const SERVER_NAME_SHA512: &str = "_Sha512 hardware accelerator server_";
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) enum Sha2Result {
Sha512Result([u8; 64]),
Sha512Trunc256Result([u8; 32]),
SuspendError,
Uninitialized,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) struct Sha2Finalize {
pub id: [u32; 3],
pub result: Sha2Result,
pub length: Option<u64>,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum Sha2Config {
Sha512,
Sha512Trunc256,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) struct Sha2Update {
pub id: [u32; 3], // our unique identifier so the server knows who the request is coming from
pub buffer: [u8; 3968], // leave one SHA chunk-sized space for overhead, so the whole message fits in one page of memory
pub len: u16, // length of just this buffer, fits in 16 bits
}
#[allow(dead_code)]
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum Opcode {
/// Acquire an exclusive lock on the hardware
/// sends a 96-bit random key + config word, returns true or false if acquisition was successful
/// note: 96-bit space has a pcollision=10^-18 for 400,000 concurrent hash requests,
/// pcollision=10^-15 for 13,000,000 concurrent hash requests.
/// for context, a typical consumer SSD has an uncorrectable bit error rate of 10^-15,
/// and we probably expect about 3-4 concurrent hash requests in the worst case.
/// Acquisition will always fail if a Suspend request is pending.
AcquireExclusive,
/// sends a buffer of [u8] for updating the hash
/// This function will fail if the hardware was shut down with a suspend/resume while hashing
Update,
/// finalizes a hash, but exclusive lock is kept. Return value is the requested hash.
/// This function will fail if the hardware was shut down with a suspend/resume while hashing
Finalize,
/// drops the lock on hardware, resets state
/// finalize and reset are split to maintain API compatibility with the Digest API
Reset,
/// a function that can be polled to determine if the block has been currently acquired
IsIdle,
/// exit the server
Quit,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum SusResOps {
/// Suspend/resume callback
SuspendResume,
/// exit the thread
Quit,
}

View file

@ -0,0 +1,221 @@
/*
This is vendored from https://github.com/RustCrypto/hashes/tree/master/sha2/src
1c1182c8b8e9aee5f3e082b77b9a6cb1d273350f
License is Apache 2.0
*/
#![allow(dead_code, clippy::unreadable_literal)]
pub const STATE_LEN: usize = 8;
pub const BLOCK_LEN: usize = 16;
/// Constants necessary for SHA-256 family of digests.
pub const K32: [u32; 64] = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];
/// Constants necessary for SHA-256 family of digests.
pub const K32X4: [[u32; 4]; 16] = [
[K32[3], K32[2], K32[1], K32[0]],
[K32[7], K32[6], K32[5], K32[4]],
[K32[11], K32[10], K32[9], K32[8]],
[K32[15], K32[14], K32[13], K32[12]],
[K32[19], K32[18], K32[17], K32[16]],
[K32[23], K32[22], K32[21], K32[20]],
[K32[27], K32[26], K32[25], K32[24]],
[K32[31], K32[30], K32[29], K32[28]],
[K32[35], K32[34], K32[33], K32[32]],
[K32[39], K32[38], K32[37], K32[36]],
[K32[43], K32[42], K32[41], K32[40]],
[K32[47], K32[46], K32[45], K32[44]],
[K32[51], K32[50], K32[49], K32[48]],
[K32[55], K32[54], K32[53], K32[52]],
[K32[59], K32[58], K32[57], K32[56]],
[K32[63], K32[62], K32[61], K32[60]],
];
/// Constants necessary for SHA-512 family of digests.
pub const K64: [u64; 80] = [
0x428a2f98d728ae22,
0x7137449123ef65cd,
0xb5c0fbcfec4d3b2f,
0xe9b5dba58189dbbc,
0x3956c25bf348b538,
0x59f111f1b605d019,
0x923f82a4af194f9b,
0xab1c5ed5da6d8118,
0xd807aa98a3030242,
0x12835b0145706fbe,
0x243185be4ee4b28c,
0x550c7dc3d5ffb4e2,
0x72be5d74f27b896f,
0x80deb1fe3b1696b1,
0x9bdc06a725c71235,
0xc19bf174cf692694,
0xe49b69c19ef14ad2,
0xefbe4786384f25e3,
0x0fc19dc68b8cd5b5,
0x240ca1cc77ac9c65,
0x2de92c6f592b0275,
0x4a7484aa6ea6e483,
0x5cb0a9dcbd41fbd4,
0x76f988da831153b5,
0x983e5152ee66dfab,
0xa831c66d2db43210,
0xb00327c898fb213f,
0xbf597fc7beef0ee4,
0xc6e00bf33da88fc2,
0xd5a79147930aa725,
0x06ca6351e003826f,
0x142929670a0e6e70,
0x27b70a8546d22ffc,
0x2e1b21385c26c926,
0x4d2c6dfc5ac42aed,
0x53380d139d95b3df,
0x650a73548baf63de,
0x766a0abb3c77b2a8,
0x81c2c92e47edaee6,
0x92722c851482353b,
0xa2bfe8a14cf10364,
0xa81a664bbc423001,
0xc24b8b70d0f89791,
0xc76c51a30654be30,
0xd192e819d6ef5218,
0xd69906245565a910,
0xf40e35855771202a,
0x106aa07032bbd1b8,
0x19a4c116b8d2d0c8,
0x1e376c085141ab53,
0x2748774cdf8eeb99,
0x34b0bcb5e19b48a8,
0x391c0cb3c5c95a63,
0x4ed8aa4ae3418acb,
0x5b9cca4f7763e373,
0x682e6ff3d6b2b8a3,
0x748f82ee5defb2fc,
0x78a5636f43172f60,
0x84c87814a1f0ab72,
0x8cc702081a6439ec,
0x90befffa23631e28,
0xa4506cebde82bde9,
0xbef9a3f7b2c67915,
0xc67178f2e372532b,
0xca273eceea26619c,
0xd186b8c721c0c207,
0xeada7dd6cde0eb1e,
0xf57d4f7fee6ed178,
0x06f067aa72176fba,
0x0a637dc5a2c898a6,
0x113f9804bef90dae,
0x1b710b35131c471b,
0x28db77f523047d84,
0x32caab7b40c72493,
0x3c9ebe0a15c9bebc,
0x431d67c49c100d4c,
0x4cc5d4becb3e42b6,
0x597f299cfc657e2a,
0x5fcb6fab3ad6faec,
0x6c44198c4a475817,
];
/// Constants necessary for SHA-512 family of digests.
pub const K64X2: [[u64; 2]; 40] = [
[K64[1], K64[0]],
[K64[3], K64[2]],
[K64[5], K64[4]],
[K64[7], K64[6]],
[K64[9], K64[8]],
[K64[11], K64[10]],
[K64[13], K64[12]],
[K64[15], K64[14]],
[K64[17], K64[16]],
[K64[19], K64[18]],
[K64[21], K64[20]],
[K64[23], K64[22]],
[K64[25], K64[24]],
[K64[27], K64[26]],
[K64[29], K64[28]],
[K64[31], K64[30]],
[K64[33], K64[32]],
[K64[35], K64[34]],
[K64[37], K64[36]],
[K64[39], K64[38]],
[K64[41], K64[40]],
[K64[43], K64[42]],
[K64[45], K64[44]],
[K64[47], K64[46]],
[K64[49], K64[48]],
[K64[51], K64[50]],
[K64[53], K64[52]],
[K64[55], K64[54]],
[K64[57], K64[56]],
[K64[59], K64[58]],
[K64[61], K64[60]],
[K64[63], K64[62]],
[K64[65], K64[64]],
[K64[67], K64[66]],
[K64[69], K64[68]],
[K64[71], K64[70]],
[K64[73], K64[72]],
[K64[75], K64[74]],
[K64[77], K64[76]],
[K64[79], K64[78]],
];
pub static H224: [u32; STATE_LEN] = [
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
];
pub static H256: [u32; STATE_LEN] = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
];
pub static H384: [u64; STATE_LEN] = [
0xcbbb9d5dc1059ed8,
0x629a292a367cd507,
0x9159015a3070dd17,
0x152fecd8f70e5939,
0x67332667ffc00b31,
0x8eb44a8768581511,
0xdb0c2e0d64f98fa7,
0x47b5481dbefa4fa4,
];
pub static H512: [u64; STATE_LEN] = [
0x6a09e667f3bcc908,
0xbb67ae8584caa73b,
0x3c6ef372fe94f82b,
0xa54ff53a5f1d36f1,
0x510e527fade682d1,
0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b,
0x5be0cd19137e2179,
];
pub static H512_TRUNC_224: [u64; STATE_LEN] = [
0x8c3d37c819544da2,
0x73e1996689dcd4d6,
0x1dfab7ae32ff9c82,
0x679dd514582f9fcf,
0x0f6d2b697bd44da8,
0x77e36f7304c48942,
0x3f9d85a86a1d36c8,
0x1112e6ad91d692a1,
];
pub static H512_TRUNC_256: [u64; STATE_LEN] = [
0x22312194fc2bf72c,
0x9f555fa3c84c64c2,
0x2393b86b6f53b151,
0x963877195940eabd,
0x96283ee2a88effe3,
0xbe5e1e2553863992,
0x2b0199fc2c85b8aa,
0x0eb72ddc81c52ca2,
];

View file

@ -0,0 +1,462 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use api::*;
use xous::{CID, send_message};
use num_traits::ToPrimitive;
use xous_ipc::Buffer;
use digest::{BlockInput, FixedOutputDirty, Reset, Update};
use digest::generic_array::GenericArray;
use digest::consts::{U28, U32, U48, U64, U128};
use block_buffer::BlockBuffer;
use core::slice::from_ref;
type BlockSize = U128;
use bitflags::*;
mod soft;
mod consts;
use soft::*;
use consts::*;
bitflags! {
pub struct Sha512Config: u32 {
const NONE = 0b0000_0000;
const SHA512_EN = 0b0000_0001;
const ENDIAN_SWAP = 0b0000_0010;
const DIGEST_SWAP = 0b0000_0100;
const SHA512_256 = 0b0000_1000;
}
}
bitflags! {
pub struct Sha512Command: u32 {
const HASH_START = 0b0000_0001;
const HASH_DIGEST = 0b0000_0010;
}
}
bitflags! {
pub struct Sha512Status: u32 {
const DONE = 0b0000_0001;
}
}
bitflags! {
pub struct Sha512Fifo: u32 {
const READ_COUNT_MASK = 0b0000_0000_0000_0000_0001_1111_1111;
const WRITE_COUNT_MASK = 0b0000_0011_1111_1111_1110_0000_0000;
const READ_ERROR = 0b0000_0100_0000_0000_0000_0000_0000;
const WRITE_ERROR = 0b0000_1000_0000_0000_0000_0000_0000;
const ALMOST_FULL = 0b0001_0000_0000_0000_0000_0000_0000;
const ALMOST_EMPTY = 0b0010_0000_0000_0000_0000_0000_0000;
const ENGINE_RUNNING = 0b0100_0000_0000_0000_0000_0000_0000;
}
}
bitflags! {
pub struct Sha512Event: u32 {
const ERROR = 0b0001;
const FIFO_FULL = 0b0010;
const SHA512_DONE = 0b0100;
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FallbackStrategy {
HardwareThenSoftware,
WaitForHardware,
SoftwareOnly,
}
/*
Software emulation vendored from https://github.com/RustCrypto/hashes/tree/master/sha2/src
License is Apache 2.0
*/
/// Structure that keeps state of the software-emulated Sha-512 operation and
/// contains the logic necessary to perform the final calculations.
#[derive(Clone)]
struct Engine512 {
len: u128,
buffer: BlockBuffer<BlockSize>,
state: [u64; 8],
}
impl Engine512 {
fn new(h: &[u64; STATE_LEN]) -> Engine512 {
Engine512 {
len: 0,
buffer: Default::default(),
state: *h,
}
}
fn update(&mut self, input: &[u8]) {
self.len += (input.len() as u128) << 3;
let s = &mut self.state;
self.buffer.input_blocks(input, |b| compress512(s, b));
}
fn finish(&mut self) {
let s = &mut self.state;
self.buffer
.len128_padding_be(self.len, |d| compress512(s, from_ref(d)));
}
fn reset(&mut self, h: &[u64; STATE_LEN]) {
self.len = 0;
self.buffer.reset();
self.state = *h;
}
}
/// The SHA-512 hash algorithm with the SHA-512 initial hash value.
#[derive(Clone)]
pub struct Sha512 {
/// connection to the accelerator engine
conn: CID,
/// whether or not this current hasher instance will use software or hardware acceleration
use_soft: bool,
/// specifies the strategy for fallback in case multiple hashes are initiated simultaneously
strategy: FallbackStrategy,
/// software fallback engine
engine: Engine512,
/// track if a hash is in progress
in_progress: bool,
/// a unique-enough random ID number to identify our connection to the hashing engine hardware
id: [u32; 3],
/// track the length of the message processed so far
length: u64,
}
impl Sha512 {
// use this function instead of default for more control over configuration of the hardware engine
pub fn new(xns: &xous_names::XousNames, maybe_strategy: Option<FallbackStrategy>) -> Self {
let conn = xns.request_connection_blocking(api::SERVER_NAME_SHA512).expect("Can't connect to Sha512 server");
let trng = trng::Trng::new(&xns).expect("Can't connect to TRNG server");
let id1 = trng.get_u64().unwrap();
let id2 = trng.get_u32().unwrap();
let strategy = if let Some(strat) = maybe_strategy {
strat
} else {
FallbackStrategy::HardwareThenSoftware
};
Sha512 {
conn,
use_soft: true,
strategy,
engine: Engine512::new(&H512),
in_progress: false,
id: [(id1 >> 32) as u32, id1 as u32, id2],
length: 0,
}
}
}
impl Default for Sha512 {
fn default() -> Self {
let xns = xous_names::XousNames::new().unwrap();
let conn = xns.request_connection_blocking(api::SERVER_NAME_SHA512).expect("Can't connect to Sha512 server");
let trng = trng::Trng::new(&xns).expect("Can't connect to TRNG server");
let id1 = trng.get_u64().unwrap();
let id2 = trng.get_u32().unwrap();
Sha512 {
conn,
use_soft: true,
strategy: FallbackStrategy::HardwareThenSoftware,
engine: Engine512::new(&H512),
in_progress: false,
id: [(id1 >> 32) as u32, id1 as u32, id2],
length: 0,
}
// xns should Drop here and release the connection allocated by it automatically
}
}
impl Drop for Sha512 {
fn drop(&mut self) {
// de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection.
unsafe{xous::disconnect(self.conn).unwrap();}
}
}
impl BlockInput for Sha512 {
type BlockSize = BlockSize;
}
impl Update for Sha512 {
fn update(&mut self, input: impl AsRef<[u8]>) {
if !self.in_progress && (self.strategy != FallbackStrategy::SoftwareOnly) {
loop {
let response = xous::send_message(self.conn,
xous::Message::new_blocking_scalar(Opcode::AcquireExclusive.to_usize().unwrap(),
self.id[0] as usize, self.id[1] as usize, self.id[2] as usize,
Sha2Config::Sha512.to_usize().unwrap(),
)
).expect("couldn't send AcquireExclusive message to Sha2 hardware!");
if let xous::Result::Scalar1(result) = response {
if result != 0 {
self.use_soft = false;
self.in_progress = true;
break;
} else {
if self.strategy == FallbackStrategy::HardwareThenSoftware {
self.use_soft = true;
self.in_progress = true;
break;
} else {
// this is hardware-exclusive mode, we block until we can get the hardware
xous::yield_slice();
}
}
} else {
log::error!("AcquireExclusive had an unexpected error: {:?}", response);
panic!("Internal error in AcquireExclusive");
}
}
}
// split the incoming blocks to page size and send to the engine
if self.use_soft {
self.engine.update(input.as_ref());
} else {
for chunk in input.as_ref().chunks(3968) {
// one SHA512 block (128 bytes) short of 4096 to give space for struct overhead in page remap handling
let mut update = Sha2Update {
id: self.id,
buffer: [0; 3968],
len: 0,
};
self.length += (chunk.len() as u64) * 8;
for (&src, dest) in chunk.iter().zip(&mut update.buffer) {
*dest = src;
}
update.len = chunk.len() as u16;
let buf = Buffer::into_buf(update).expect("couldn't map chunk into IPC buffer");
buf.lend(self.conn, Opcode::Update.to_u32().unwrap()).expect("hardware rejected our hash chunk!");
}
}
}
}
impl FixedOutputDirty for Sha512 {
type OutputSize = U64;
fn finalize_into_dirty(&mut self, out: &mut digest::Output<Self>) {
if self.use_soft {
self.engine.finish();
let s = self.engine.state;
for (chunk, v) in out.chunks_exact_mut(8).zip(s.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
} else {
let result = Sha2Finalize {
id: self.id,
result: Sha2Result::Uninitialized,
length: None,
};
let mut buf = Buffer::into_buf(result).expect("couldn't map memory for the return buffer");
buf.lend_mut(self.conn, Opcode::Finalize.to_u32().unwrap());
let returned: Sha2Finalize = buf.to_original().expect("couldn't decode return buffer");
match returned.result {
Sha2Result::Sha512Result(s) => {
if self.length != returned.length.expect("hardware did not return a length field!") {
panic!("Sha512 hardware did not hash as many bits as we had expected!")
}
for (chunk, v) in out.chunks_exact_mut(8).zip(s.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
}
Sha2Result::Sha512Trunc256Result(_) => {
panic!("Sha512 hardware returned the wrong type of buffer!");
}
Sha2Result::SuspendError => {
panic!("Hardware was suspended during Sha512 operation, result is invalid.");
}
Sha2Result::Uninitialized => {
panic!("Hardware didn't copy Sha512 hash result to the return buffer.")
}
}
}
}
}
impl Reset for Sha512 {
fn reset(&mut self) {
if self.use_soft {
self.engine.reset(&H512);
} else {
xous::send_message(self.conn,
xous::Message::new_blocking_scalar(Opcode::Reset.to_usize().unwrap(), self.id[0] as usize, self.id[1] as usize, self.id[2] as usize, 0)
).expect("couldn't send reset to hardware");
// reset internal flags
self.length = 0;
self.in_progress = false;
self.use_soft = true;
}
}
}
/// The SHA-512 hash algorithm with the SHA-512/256 initial hash value. The
/// result is truncated to 256 bits.
/// TODO: this is a software-only implementation; this needs to be extended to have hardware
/// support as the constants for this mode are supported in-hardware, once we have validated
/// that the core hardware API even works...
#[derive(Clone)]
pub struct Sha512Trunc256 {
engine: Engine512,
}
impl Default for Sha512Trunc256 {
fn default() -> Self {
Sha512Trunc256 {
engine: Engine512::new(&H512_TRUNC_256),
}
}
}
impl BlockInput for Sha512Trunc256 {
type BlockSize = BlockSize;
}
impl Update for Sha512Trunc256 {
fn update(&mut self, input: impl AsRef<[u8]>) {
self.engine.update(input.as_ref());
}
}
impl FixedOutputDirty for Sha512Trunc256 {
type OutputSize = U32;
fn finalize_into_dirty(&mut self, out: &mut digest::Output<Self>) {
self.engine.finish();
let s = &self.engine.state[..4];
for (chunk, v) in out.chunks_exact_mut(8).zip(s.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
}
}
impl Reset for Sha512Trunc256 {
fn reset(&mut self) {
self.engine.reset(&H512_TRUNC_256);
}
}
//////////////////////////////////////////////
//////////////// the following modes are software-only emulation, as we currently do not have the constants in hardware to support these.
//////////////////////////////////////////////
/// The SHA-512 hash algorithm with the SHA-384 initial hash value. The result
/// is truncated to 384 bits.
#[derive(Clone)]
pub struct Sha384 {
engine: Engine512,
}
impl Default for Sha384 {
fn default() -> Self {
Sha384 {
engine: Engine512::new(&H384),
}
}
}
impl BlockInput for Sha384 {
type BlockSize = BlockSize;
}
impl Update for Sha384 {
fn update(&mut self, input: impl AsRef<[u8]>) {
self.engine.update(input.as_ref());
}
}
impl FixedOutputDirty for Sha384 {
type OutputSize = U48;
fn finalize_into_dirty(&mut self, out: &mut digest::Output<Self>) {
self.engine.finish();
let s = &self.engine.state[..6];
for (chunk, v) in out.chunks_exact_mut(8).zip(s.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
}
}
impl Reset for Sha384 {
fn reset(&mut self) {
self.engine.reset(&H384);
}
}
/// The SHA-512 hash algorithm with the SHA-512/224 initial hash value.
/// The result is truncated to 224 bits.
#[derive(Clone)]
pub struct Sha512Trunc224 {
engine: Engine512,
}
impl Default for Sha512Trunc224 {
fn default() -> Self {
Sha512Trunc224 {
engine: Engine512::new(&H512_TRUNC_224),
}
}
}
impl BlockInput for Sha512Trunc224 {
type BlockSize = BlockSize;
}
impl Update for Sha512Trunc224 {
fn update(&mut self, input: impl AsRef<[u8]>) {
self.engine.update(input.as_ref());
}
}
impl FixedOutputDirty for Sha512Trunc224 {
type OutputSize = U28;
fn finalize_into_dirty(&mut self, out: &mut digest::Output<Self>) {
self.engine.finish();
let s = &self.engine.state;
for (chunk, v) in out.chunks_exact_mut(8).zip(s[..3].iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
out[24..28].copy_from_slice(&s[3].to_be_bytes()[..4]);
}
}
impl Reset for Sha512Trunc224 {
fn reset(&mut self) {
self.engine.reset(&H512_TRUNC_224);
}
}
opaque_debug::implement!(Sha384);
opaque_debug::implement!(Sha512);
opaque_debug::implement!(Sha512Trunc224);
opaque_debug::implement!(Sha512Trunc256);
digest::impl_write!(Sha384);
digest::impl_write!(Sha512);
digest::impl_write!(Sha512Trunc224);
digest::impl_write!(Sha512Trunc256);
/// Raw SHA-512 compression function.
///
/// This is a low-level "hazmat" API which provides direct access to the core
/// functionality of SHA-512.
#[cfg_attr(docsrs, doc(cfg(feature = "compress")))]
pub fn compress512(state: &mut [u64; 8], blocks: &[GenericArray<u8, U128>]) {
// SAFETY: GenericArray<u8, U128> and [u8; 128] have
// exactly the same memory layout
#[allow(unsafe_code)]
let blocks: &[[u8; 128]] = unsafe { &*(blocks as *const _ as *const [[u8; 128]]) };
compress(state, blocks)
}

View file

@ -0,0 +1,198 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
mod api;
use num_traits::{ToPrimitive, FromPrimitive};
use xous_ipc::Buffer;
use api::{Opcode, SusResOps, Sha2Config};
use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack};
use log::info;
use core::sync::atomic::{AtomicBool, Ordering};
#[cfg(target_os = "none")]
mod implementation {
use utralib::generated::*;
use susres::{RegManager, RegOrField, SuspendResume};
pub struct Engine512 {
csr: utralib::CSR<u32>,
fifo: xous::MemoryRange,
susres_manager: RegManager::<{utra::sha512::SHA512_NUMREGS}>,
in_progress: bool,
}
impl Engine512 {
pub fn new() -> Engine512 {
let csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::sha512::HW_SHA512_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map Engine512 CSR range");
let fifo = xous::syscall::map_memory(
xous::MemoryAddress::new(utralib::HW_SHA512_MEM),
None,
utralib::HW_SHA512_MEM_LEN,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map Engine512 CSR range");
let mut engine512 = Engine512 {
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
susres_manager: RegManager::new(csr.as_mut_ptr() as *mut u32),
fifo,
in_progress: false,
};
engine512
}
pub fn suspend(&mut self) {
self.susres_manager.suspend();
}
pub fn resume(&mut self) {
self.susres_manager.resume();
}
pub fn reset(&mut self) {
///////////// TODO
}
}
}
// a stub to try to avoid breaking hosted mode for as long as possible.
#[cfg(not(target_os = "none"))]
mod implementation {
use log::info;
pub struct Engine512 {
}
impl Engine512 {
pub fn new() -> Engine512 {
Engine512 {
}
}
pub fn suspend(&self) {
}
pub fn resume(&self) {
}
pub fn reset(&self) {
}
}
}
static HASH_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
fn susres_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let susres_sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
let xns = xous_names::XousNames::new().unwrap();
// register a suspend/resume listener
let sr_cid = xous::connect(susres_sid).expect("couldn't create suspend callback connection");
let mut susres = susres::Susres::new(&xns, api::SusResOps::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
log::trace!("starting Sha512 suspend/resume manager loop");
loop {
let mut msg = xous::receive_message(susres_sid).unwrap();
log::trace!("Message: {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) {
Some(SusResOps::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
while HASH_IN_PROGRESS.load(Ordering::Relaxed) {
xous::yield_slice();
}
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
}),
Some(SusResOps::Quit) => {
log::info!("Received quit opcode, exiting!");
break;
}
None => {
log::error!("Received unknown opcode: {:?}", msg);
}
}
}
xns.unregister_server(susres_sid).unwrap();
xous::destroy_server(susres_sid).unwrap();
}
#[xous::xous_main]
fn xmain() -> ! {
use crate::implementation::Engine512;
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();
let engine512_sid = xns.register_name(api::SERVER_NAME_SHA512).expect("can't register server");
log::trace!("registered with NS -- {:?}", engine512_sid);
let mut engine512 = Engine512::new();
log::trace!("ready to accept requests");
// handle suspend/resume with a separate thread, which monitors our in-progress state
// we can't save hardware state of a hash, so the hash MUST finish before we can suspend.
let susres_mgr_sid = xous::create_server().unwrap();
let (sid0, sid1, sid2, sid3) = susres_mgr_sid.to_u32();
xous::create_thread_4(susres_thread, sid0 as usize, sid1 as usize, sid2 as usize, sid3 as usize).expect("couldn't start susres handler thread");
let mut client_id: Option<[u32; 3]> = None;
let mut mode: Option<Sha2Config> = None;
loop {
let msg = xous::receive_message(engine512_sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(Opcode::AcquireExclusive) => msg_blocking_scalar_unpack!(msg, id0, id1, id2, flags, {
if client_id.is_none() {
client_id = Some([id0 as u32, id1 as u32, id2 as u32]);
mode = Some(FromPrimitive::from_usize(flags).unwrap());
HASH_IN_PROGRESS.store(true, Ordering::Relaxed);
xous::return_scalar(msg.sender, 1);
} else {
xous::return_scalar(msg.sender, 0);
}
}),
Some(Opcode::Reset) => msg_blocking_scalar_unpack!(msg, r_id0, r_id1, r_id2, _, {
match client_id {
Some([id0, id1, id2]) => {
if id0 == r_id0 as u32 && id1 == r_id1 as u32 && id2 == r_id2 as u32 {
client_id = None;
mode = None;
engine512.reset();
xous::return_scalar(msg.sender, 1);
} else {
xous::return_scalar(msg.sender, 0);
}
}
_ => {
xous::return_scalar(msg.sender, 0);
}
}
}),
Some(Opcode::Quit) => {
log::info!("Received quit opcode, exiting!");
break;
}
None => {
log::error!("couldn't convert opcode");
}
}
}
// clean up our program
log::trace!("main loop exit, destroying servers");
let quitconn = xous::connect(susres_mgr_sid).unwrap();
xous::send_message(quitconn, xous::Message::new_scalar(SusResOps::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
unsafe{xous::disconnect(quitconn);}
xns.unregister_server(engine512_sid).unwrap();
xous::destroy_server(engine512_sid).unwrap();
log::trace!("quitting");
xous::terminate_process(); loop {}
}

View file

@ -0,0 +1,220 @@
/*
This is vendored from https://github.com/RustCrypto/hashes/tree/master/sha2/src
1c1182c8b8e9aee5f3e082b77b9a6cb1d273350f
License is Apache 2.0
*/
#![allow(clippy::many_single_char_names)]
use crate::consts::{BLOCK_LEN, K64X2};
use core::convert::TryInto;
fn add(a: [u64; 2], b: [u64; 2]) -> [u64; 2] {
[a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1])]
}
/// Not an intrinsic, but works like an unaligned load.
fn sha512load(v0: [u64; 2], v1: [u64; 2]) -> [u64; 2] {
[v1[1], v0[0]]
}
/// Performs 2 rounds of the SHA-512 message schedule update.
pub fn sha512_schedule_x2(v0: [u64; 2], v1: [u64; 2], v4to5: [u64; 2], v7: [u64; 2]) -> [u64; 2] {
// sigma 0
fn sigma0(x: u64) -> u64 {
((x << 63) | (x >> 1)) ^ ((x << 56) | (x >> 8)) ^ (x >> 7)
}
// sigma 1
fn sigma1(x: u64) -> u64 {
((x << 45) | (x >> 19)) ^ ((x << 3) | (x >> 61)) ^ (x >> 6)
}
let [w1, w0] = v0;
let [_, w2] = v1;
let [w10, w9] = v4to5;
let [w15, w14] = v7;
let w16 = sigma1(w14)
.wrapping_add(w9)
.wrapping_add(sigma0(w1))
.wrapping_add(w0);
let w17 = sigma1(w15)
.wrapping_add(w10)
.wrapping_add(sigma0(w2))
.wrapping_add(w1);
[w17, w16]
}
/// Performs one round of the SHA-512 message block digest.
pub fn sha512_digest_round(
ae: [u64; 2],
bf: [u64; 2],
cg: [u64; 2],
dh: [u64; 2],
wk0: u64,
) -> [u64; 2] {
macro_rules! big_sigma0 {
($a:expr) => {
($a.rotate_right(28) ^ $a.rotate_right(34) ^ $a.rotate_right(39))
};
}
macro_rules! big_sigma1 {
($a:expr) => {
($a.rotate_right(14) ^ $a.rotate_right(18) ^ $a.rotate_right(41))
};
}
macro_rules! bool3ary_202 {
($a:expr, $b:expr, $c:expr) => {
$c ^ ($a & ($b ^ $c))
};
} // Choose, MD5F, SHA1C
macro_rules! bool3ary_232 {
($a:expr, $b:expr, $c:expr) => {
($a & $b) ^ ($a & $c) ^ ($b & $c)
};
} // Majority, SHA1M
let [a0, e0] = ae;
let [b0, f0] = bf;
let [c0, g0] = cg;
let [d0, h0] = dh;
// a round
let x0 = big_sigma1!(e0)
.wrapping_add(bool3ary_202!(e0, f0, g0))
.wrapping_add(wk0)
.wrapping_add(h0);
let y0 = big_sigma0!(a0).wrapping_add(bool3ary_232!(a0, b0, c0));
let (a1, _, _, _, e1, _, _, _) = (
x0.wrapping_add(y0),
a0,
b0,
c0,
x0.wrapping_add(d0),
e0,
f0,
g0,
);
[a1, e1]
}
/// Process a block with the SHA-512 algorithm.
pub fn sha512_digest_block_u64(state: &mut [u64; 8], block: &[u64; 16]) {
let k = &K64X2;
macro_rules! schedule {
($v0:expr, $v1:expr, $v4:expr, $v5:expr, $v7:expr) => {
sha512_schedule_x2($v0, $v1, sha512load($v4, $v5), $v7)
};
}
macro_rules! rounds4 {
($ae:ident, $bf:ident, $cg:ident, $dh:ident, $wk0:expr, $wk1:expr) => {{
let [u, t] = $wk0;
let [w, v] = $wk1;
$dh = sha512_digest_round($ae, $bf, $cg, $dh, t);
$cg = sha512_digest_round($dh, $ae, $bf, $cg, u);
$bf = sha512_digest_round($cg, $dh, $ae, $bf, v);
$ae = sha512_digest_round($bf, $cg, $dh, $ae, w);
}};
}
let mut ae = [state[0], state[4]];
let mut bf = [state[1], state[5]];
let mut cg = [state[2], state[6]];
let mut dh = [state[3], state[7]];
// Rounds 0..20
let (mut w1, mut w0) = ([block[3], block[2]], [block[1], block[0]]);
rounds4!(ae, bf, cg, dh, add(k[0], w0), add(k[1], w1));
let (mut w3, mut w2) = ([block[7], block[6]], [block[5], block[4]]);
rounds4!(ae, bf, cg, dh, add(k[2], w2), add(k[3], w3));
let (mut w5, mut w4) = ([block[11], block[10]], [block[9], block[8]]);
rounds4!(ae, bf, cg, dh, add(k[4], w4), add(k[5], w5));
let (mut w7, mut w6) = ([block[15], block[14]], [block[13], block[12]]);
rounds4!(ae, bf, cg, dh, add(k[6], w6), add(k[7], w7));
let mut w8 = schedule!(w0, w1, w4, w5, w7);
let mut w9 = schedule!(w1, w2, w5, w6, w8);
rounds4!(ae, bf, cg, dh, add(k[8], w8), add(k[9], w9));
// Rounds 20..40
w0 = schedule!(w2, w3, w6, w7, w9);
w1 = schedule!(w3, w4, w7, w8, w0);
rounds4!(ae, bf, cg, dh, add(k[10], w0), add(k[11], w1));
w2 = schedule!(w4, w5, w8, w9, w1);
w3 = schedule!(w5, w6, w9, w0, w2);
rounds4!(ae, bf, cg, dh, add(k[12], w2), add(k[13], w3));
w4 = schedule!(w6, w7, w0, w1, w3);
w5 = schedule!(w7, w8, w1, w2, w4);
rounds4!(ae, bf, cg, dh, add(k[14], w4), add(k[15], w5));
w6 = schedule!(w8, w9, w2, w3, w5);
w7 = schedule!(w9, w0, w3, w4, w6);
rounds4!(ae, bf, cg, dh, add(k[16], w6), add(k[17], w7));
w8 = schedule!(w0, w1, w4, w5, w7);
w9 = schedule!(w1, w2, w5, w6, w8);
rounds4!(ae, bf, cg, dh, add(k[18], w8), add(k[19], w9));
// Rounds 40..60
w0 = schedule!(w2, w3, w6, w7, w9);
w1 = schedule!(w3, w4, w7, w8, w0);
rounds4!(ae, bf, cg, dh, add(k[20], w0), add(k[21], w1));
w2 = schedule!(w4, w5, w8, w9, w1);
w3 = schedule!(w5, w6, w9, w0, w2);
rounds4!(ae, bf, cg, dh, add(k[22], w2), add(k[23], w3));
w4 = schedule!(w6, w7, w0, w1, w3);
w5 = schedule!(w7, w8, w1, w2, w4);
rounds4!(ae, bf, cg, dh, add(k[24], w4), add(k[25], w5));
w6 = schedule!(w8, w9, w2, w3, w5);
w7 = schedule!(w9, w0, w3, w4, w6);
rounds4!(ae, bf, cg, dh, add(k[26], w6), add(k[27], w7));
w8 = schedule!(w0, w1, w4, w5, w7);
w9 = schedule!(w1, w2, w5, w6, w8);
rounds4!(ae, bf, cg, dh, add(k[28], w8), add(k[29], w9));
// Rounds 60..80
w0 = schedule!(w2, w3, w6, w7, w9);
w1 = schedule!(w3, w4, w7, w8, w0);
rounds4!(ae, bf, cg, dh, add(k[30], w0), add(k[31], w1));
w2 = schedule!(w4, w5, w8, w9, w1);
w3 = schedule!(w5, w6, w9, w0, w2);
rounds4!(ae, bf, cg, dh, add(k[32], w2), add(k[33], w3));
w4 = schedule!(w6, w7, w0, w1, w3);
w5 = schedule!(w7, w8, w1, w2, w4);
rounds4!(ae, bf, cg, dh, add(k[34], w4), add(k[35], w5));
w6 = schedule!(w8, w9, w2, w3, w5);
w7 = schedule!(w9, w0, w3, w4, w6);
rounds4!(ae, bf, cg, dh, add(k[36], w6), add(k[37], w7));
w8 = schedule!(w0, w1, w4, w5, w7);
w9 = schedule!(w1, w2, w5, w6, w8);
rounds4!(ae, bf, cg, dh, add(k[38], w8), add(k[39], w9));
let [a, e] = ae;
let [b, f] = bf;
let [c, g] = cg;
let [d, h] = dh;
state[0] = state[0].wrapping_add(a);
state[1] = state[1].wrapping_add(b);
state[2] = state[2].wrapping_add(c);
state[3] = state[3].wrapping_add(d);
state[4] = state[4].wrapping_add(e);
state[5] = state[5].wrapping_add(f);
state[6] = state[6].wrapping_add(g);
state[7] = state[7].wrapping_add(h);
}
pub fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
let mut block_u32 = [0u64; BLOCK_LEN];
// since LLVM can't properly use aliasing yet it will make
// unnecessary state stores without this copy
let mut state_cpy = *state;
for block in blocks {
for (o, chunk) in block_u32.iter_mut().zip(block.chunks_exact(8)) {
*o = u64::from_be_bytes(chunk.try_into().unwrap());
}
sha512_digest_block_u64(&mut state_cpy, &block_u32);
}
*state = state_cpy;
}

View file

@ -52,6 +52,7 @@ fn try_main() -> Result<(), DynError> {
"rtc",
"susres",
"codec",
"engine-sha512",
];
let benchmark_pkgs = [
"benchmark",