-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
507 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Licensed under the Apache License, Version 2.0 or the MIT License. | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
# Copyright Tock Contributors 2023. | ||
|
||
[package] | ||
name = "rsa-sw" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
kernel = { path = "../../kernel" } | ||
rsa = { version = "0.9.2", default-features = false, features = ["sha2"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
RSA Software Implementation | ||
=========================== | ||
|
||
This crate provides a software-based implementation of RSA algorithms using | ||
the RustCrypto RSA crate. | ||
|
||
Dependency Tree | ||
--------------- | ||
|
||
``` | ||
rsa-sw v0.1.0 (/Users/bradjc/git/tock/capsules/rsa_sw) | ||
├── kernel v0.1.0 (/Users/bradjc/git/tock/kernel) | ||
│ ├── tock-cells v0.1.0 (/Users/bradjc/git/tock/libraries/tock-cells) | ||
│ ├── tock-registers v0.8.1 (/Users/bradjc/git/tock/libraries/tock-register-interface) | ||
│ └── tock-tbf v0.1.0 (/Users/bradjc/git/tock/libraries/tock-tbf) | ||
└── rsa v0.9.2 | ||
├── byteorder v1.4.3 | ||
├── const-oid v0.9.2 | ||
├── digest v0.10.7 | ||
│ ├── block-buffer v0.10.4 | ||
│ │ └── generic-array v0.14.7 | ||
│ │ └── typenum v1.16.0 | ||
│ │ [build-dependencies] | ||
│ │ └── version_check v0.9.4 | ||
│ ├── const-oid v0.9.2 | ||
│ └── crypto-common v0.1.6 | ||
│ ├── generic-array v0.14.7 (*) | ||
│ └── typenum v1.16.0 | ||
├── num-bigint-dig v0.8.2 | ||
│ ├── byteorder v1.4.3 | ||
│ ├── lazy_static v1.4.0 | ||
│ │ └── spin v0.5.2 | ||
│ ├── libm v0.2.7 | ||
│ ├── num-integer v0.1.45 | ||
│ │ └── num-traits v0.2.15 | ||
│ │ └── libm v0.2.7 | ||
│ │ [build-dependencies] | ||
│ │ └── autocfg v1.1.0 | ||
│ │ [build-dependencies] | ||
│ │ └── autocfg v1.1.0 | ||
│ ├── num-iter v0.1.43 | ||
│ │ ├── num-integer v0.1.45 (*) | ||
│ │ └── num-traits v0.2.15 (*) | ||
│ │ [build-dependencies] | ||
│ │ └── autocfg v1.1.0 | ||
│ ├── num-traits v0.2.15 (*) | ||
│ ├── rand v0.8.5 | ||
│ │ ├── rand_chacha v0.3.1 | ||
│ │ │ ├── ppv-lite86 v0.2.17 | ||
│ │ │ └── rand_core v0.6.4 | ||
│ │ └── rand_core v0.6.4 | ||
│ ├── smallvec v1.10.0 | ||
│ └── zeroize v1.6.0 | ||
├── num-integer v0.1.45 (*) | ||
├── num-iter v0.1.43 (*) | ||
├── num-traits v0.2.15 (*) | ||
├── pkcs1 v0.7.5 | ||
│ ├── der v0.7.6 | ||
│ │ ├── const-oid v0.9.2 | ||
│ │ └── zeroize v1.6.0 | ||
│ ├── pkcs8 v0.10.2 | ||
│ │ ├── der v0.7.6 (*) | ||
│ │ └── spki v0.7.2 | ||
│ │ └── der v0.7.6 (*) | ||
│ └── spki v0.7.2 (*) | ||
├── pkcs8 v0.10.2 (*) | ||
├── rand_core v0.6.4 | ||
├── sha2 v0.10.6 | ||
│ ├── cfg-if v1.0.0 | ||
│ ├── cpufeatures v0.2.7 | ||
│ └── digest v0.10.7 (*) | ||
├── signature v2.1.0 | ||
│ ├── digest v0.10.7 (*) | ||
│ └── rand_core v0.6.4 | ||
├── spki v0.7.2 (*) | ||
├── subtle v2.5.0 | ||
└── zeroize v1.6.0 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![forbid(unsafe_code)] | ||
#![no_std] | ||
|
||
pub mod verifier; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Licensed under the Apache License, Version 2.0 or the MIT License. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// Copyright Tock Contributors 2023. | ||
|
||
//! RSA Signature Verifier for SHA256 hashes and RSA2048 keys. | ||
use core::cell::Cell; | ||
use kernel::hil; | ||
use kernel::utilities::cells::{MapCell, OptionalCell, TakeCell}; | ||
|
||
pub struct RsaSignatureVerifier<'a, const H: usize, const S: usize> { | ||
verified: Cell<bool>, | ||
client: OptionalCell<&'a dyn hil::public_key_crypto::signature::ClientVerify<H, S>>, | ||
rsa_public_key: MapCell<rsa::RsaPublicKey>, | ||
hash_storage: TakeCell<'static, [u8; 32]>, | ||
signature_storage: TakeCell<'static, [u8; 256]>, | ||
|
||
deferred_call: kernel::deferred_call::DeferredCall, | ||
} | ||
|
||
impl<'a, const H: usize, const S: usize> RsaSignatureVerifier<'a, H, S> { | ||
pub fn new() -> Self { | ||
// my ACTUAL public key | ||
let n = rsa::BigUint::parse_bytes(b"24207257266404723702480416527933364039116773666417951609465570931679686940076207109293072612569267113256147168695608811123741758650429326896362330556657608406060222154960934834802893052320574456334624928389491520892685313371199210386475223296696579831840058897720325126562243770933238678183073031561719244791265232863605896837907058881808599654200582034457810596804897754743492491685117186551986141408292570229581725243489406524879436825146596246620952685529114397828868686804212048259156058108264250596765840612650253797791010731257875056647324896013942698591287080293236802963873491363585251097167389150803020633481", 10).unwrap(); | ||
let e = rsa::BigUint::parse_bytes(b"65537", 10).unwrap(); | ||
|
||
// Incorrect public key for testing | ||
// let n = rsa::BigUint::parse_bytes(b"34207257266404723702480416527933364039116773666417951609465570931679686940076207109293072612569267113256147168695608811123741758650429326896362330556657608406060222154960934834802893052320574456334624928389491520892685313371199210386475223296696579831840058897720325126562243770933238678183073031561719244791265232863605896837907058881808599654200582034457810596804897754743492491685117186551986141408292570229581725243489406524879436825146596246620952685529114397828868686804212048259156058108264250596765840612650253797791010731257875056647324896013942698591287080293236802963873491363585251097167389150803020633481", 10).unwrap(); | ||
// let e = rsa::BigUint::parse_bytes(b"65537", 10).unwrap(); | ||
|
||
let pub_key = | ||
rsa::RsaPublicKey::new(n, e).map_or_else(|_e| MapCell::empty(), |v| MapCell::new(v)); | ||
|
||
Self { | ||
verified: Cell::new(false), | ||
client: OptionalCell::empty(), | ||
rsa_public_key: pub_key, | ||
hash_storage: TakeCell::empty(), | ||
signature_storage: TakeCell::empty(), | ||
|
||
deferred_call: kernel::deferred_call::DeferredCall::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> hil::public_key_crypto::signature::SignatureVerify<'a, 32, 256> | ||
for RsaSignatureVerifier<'a, 32, 256> | ||
{ | ||
fn set_verify_client( | ||
&'a self, | ||
client: &'a dyn hil::public_key_crypto::signature::ClientVerify<32, 256>, | ||
) { | ||
self.client.replace(client); | ||
} | ||
|
||
fn verify( | ||
&'a self, | ||
hash: &'static mut [u8; 32], | ||
signature: &'static mut [u8; 256], | ||
) -> Result< | ||
(), | ||
( | ||
kernel::ErrorCode, | ||
&'static mut [u8; 32], | ||
&'static mut [u8; 256], | ||
), | ||
> { | ||
if self.rsa_public_key.is_some() { | ||
self.rsa_public_key | ||
.map(|pub_key| { | ||
self.verified.set( | ||
pub_key | ||
.verify( | ||
rsa::Pkcs1v15Sign::new::<rsa::sha2::Sha256>(), | ||
hash, | ||
signature, | ||
) | ||
.is_ok(), | ||
); | ||
self.hash_storage.replace(hash); | ||
self.signature_storage.replace(signature); | ||
self.deferred_call.set(); | ||
Ok(()) | ||
}) | ||
.unwrap() | ||
} else { | ||
Err((kernel::ErrorCode::FAIL, hash, signature)) | ||
} | ||
} | ||
} | ||
|
||
impl<'a> kernel::deferred_call::DeferredCallClient for RsaSignatureVerifier<'a, 32, 256> { | ||
fn handle_deferred_call(&self) { | ||
self.client.map(|client| { | ||
self.hash_storage.take().map(|h| { | ||
self.signature_storage.take().map(|s| { | ||
client.verification_done(Ok(self.verified.get()), h, s); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
fn register(&'static self) { | ||
self.deferred_call.register(self); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
pub mod keys; | ||
pub mod rsa_math; | ||
pub mod signature; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Licensed under the Apache License, Version 2.0 or the MIT License. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// Copyright Tock Contributors 2022. | ||
|
||
//! Interface for verifying signatures. | ||
use crate::ErrorCode; | ||
|
||
/// This trait provides callbacks for when the verification has completed. | ||
pub trait ClientVerify<const H: usize, const S: usize> { | ||
fn verification_done( | ||
&self, | ||
result: Result<bool, ErrorCode>, | ||
hash: &'static mut [u8; H], | ||
signature: &'static mut [u8; S], | ||
); | ||
} | ||
|
||
/// Verify a signature. | ||
/// | ||
/// - `H`: The length in bytes of the hash. | ||
/// - `S`: The length in bytes of the signature. | ||
pub trait SignatureVerify<'a, const H: usize, const S: usize> { | ||
/// Set the client instance which will receive the `verification_done()` | ||
/// callback. | ||
#[allow(unused_variables)] | ||
fn set_verify_client(&'a self, client: &'a dyn ClientVerify<H, S>) {} | ||
|
||
// Verify the signature. Returns `Ok(())` if the signature matches. | ||
fn verify( | ||
&'a self, | ||
hash: &'static mut [u8; H], | ||
signature: &'static mut [u8; S], | ||
) -> Result<(), (ErrorCode, &'static mut [u8; H], &'static mut [u8; S])>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.