From 34c579f1e2208003c2391cfeeab07e2753fa14f5 Mon Sep 17 00:00:00 2001 From: irriden Date: Fri, 24 Nov 2023 18:12:11 +0000 Subject: [PATCH] sphinx-key: ota binary checks in standalone module --- sphinx-key/src/bitcoin_utils.rs | 41 +++++++++++++++++++++++++++++++++ sphinx-key/src/core/control.rs | 2 +- sphinx-key/src/core/events.rs | 2 +- sphinx-key/src/main.rs | 5 +++- sphinx-key/src/ota.rs | 35 +++------------------------- 5 files changed, 50 insertions(+), 35 deletions(-) create mode 100644 sphinx-key/src/bitcoin_utils.rs diff --git a/sphinx-key/src/bitcoin_utils.rs b/sphinx-key/src/bitcoin_utils.rs new file mode 100644 index 0000000..74f30d1 --- /dev/null +++ b/sphinx-key/src/bitcoin_utils.rs @@ -0,0 +1,41 @@ +use crate::bitcoin::{ + hashes::{sha256, Hash}, + secp256k1::Secp256k1, + util::misc::{signed_msg_hash, MessageSignature}, + Address, +}; +use anyhow::{anyhow, Result}; +use base64::{engine::general_purpose::STANDARD, Engine as _}; +use std::fs::File; +use std::io::BufReader; + +const ADDRESS: &str = "1K51sSTyoVxHhKFtwWpzMZsoHvLshtw3Dp"; + +pub(crate) fn check_signature(msg: &str, sig: &str) -> Result<()> { + let add = ADDRESS.parse::
()?; + let sig = STANDARD.decode(sig)?; + let sig = MessageSignature::from_slice(&sig)?; + let secp = Secp256k1::verification_only(); + let signed = sig.is_signed_by_address(&secp, &add, signed_msg_hash(msg))?; + match signed { + true => Ok(()), + false => Err(anyhow!("Failed signature check")), + } +} + +pub(crate) fn check_integrity(file_path: &str, check: &str) -> Result<()> { + let f = File::open(file_path)?; + let mut reader = BufReader::new(f); + let mut engine = sha256::HashEngine::default(); + std::io::copy(&mut reader, &mut engine)?; + let hash = sha256::Hash::from_engine(engine).to_string(); + if hash == check { + Ok(()) + } else { + Err(anyhow!( + "Integrity check failed! check: {} vs calculated: {}", + check, + hash + )) + } +} diff --git a/sphinx-key/src/core/control.rs b/sphinx-key/src/core/control.rs index e9ae733..cc13ae4 100644 --- a/sphinx-key/src/core/control.rs +++ b/sphinx-key/src/core/control.rs @@ -1,9 +1,9 @@ +use crate::bitcoin::Network; use crate::ID_LEN; use anyhow::{anyhow, Context, Result}; use esp_idf_svc::nvs::{EspDefaultNvs, EspDefaultNvsPartition}; use glyph::control::{Config, ControlPersist, Controller, FlashKey, Policy, Velocity}; use glyph::ser::*; -use sphinx_signer::lightning_signer::bitcoin::Network; use sphinx_signer::sphinx_glyph as glyph; use std::convert::TryInto; use std::sync::{Arc, Mutex}; diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 22503c4..6a20dc7 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -3,13 +3,13 @@ use crate::core::lss; use crate::ota::{update_sphinx_key, validate_ota_message}; use crate::status::Status; +use crate::bitcoin::Network; use glyph::control::{Config, ControlMessage, ControlResponse, Controller, Policy, Velocity}; use glyph::error::Error as GlyphError; use glyph::ser::{serialize_controlresponse, ByteBuf}; use glyph::topics; use lss_connector::secp256k1::PublicKey; use sphinx_signer::approver::SphinxApprover; -use sphinx_signer::lightning_signer::bitcoin::Network; //use sphinx_signer::lightning_signer::persist::DummyPersister; use sphinx_signer::kvv::{CloudKVVStore, FsKVVStore}; use sphinx_signer::lightning_signer::persist::Persist; diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 8ee35d0..69348d6 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -1,3 +1,4 @@ +mod bitcoin_utils; mod button; mod conn; mod core; @@ -6,6 +7,9 @@ mod ota; mod sd; mod status; +pub(crate) use sphinx_signer::lightning_signer::bitcoin; + +use crate::bitcoin::Network; use crate::button::button_loop; use crate::core::control::controller_from_seed; use crate::core::{config::*, events::*, FlashPersister}; @@ -20,7 +24,6 @@ use esp_idf_svc::hal::peripherals::Peripherals; use esp_idf_svc::nvs::EspDefaultNvsPartition; #[allow(unused_imports)] use esp_idf_svc::sys as _; -use sphinx_signer::lightning_signer::bitcoin::Network; use sphinx_signer::sphinx_glyph::control::{Config, ControlPersist, Policy, Velocity}; use std::sync::{mpsc, Arc, Mutex}; use std::thread; diff --git a/sphinx-key/src/ota.rs b/sphinx-key/src/ota.rs index 45ff5dc..66a0ffe 100644 --- a/sphinx-key/src/ota.rs +++ b/sphinx-key/src/ota.rs @@ -1,25 +1,17 @@ use anyhow::{anyhow, Result}; -use base64::{engine::general_purpose::STANDARD, Engine as _}; use esp_idf_svc::http::client::Configuration; use esp_idf_svc::http::client::EspHttpConnection; use esp_idf_svc::http::client::FollowRedirectsPolicy::FollowNone; use esp_idf_svc::http::Method; use esp_idf_svc::ota::EspOta; use log::{error, info}; -use sphinx_signer::lightning_signer::bitcoin::{ - hashes::{sha256, Hash}, - secp256k1::Secp256k1, - util::misc::{signed_msg_hash, MessageSignature}, - Address, -}; use sphinx_signer::sphinx_glyph::control::OtaParams; use std::fs::{remove_file, File}; +use std::io::BufWriter; use std::io::Write; -use std::io::{BufReader, BufWriter}; const BUFFER_LEN: usize = 1024; const UPDATE_BIN_PATH: &str = "/sdcard/update.bin"; -const ADDRESS: &str = "1K51sSTyoVxHhKFtwWpzMZsoHvLshtw3Dp"; fn factory_reset() -> Result<()> { let mut ota = EspOta::new()?; @@ -70,32 +62,11 @@ fn get_update(params: &OtaParams) -> Result<()> { } fn check_signature(params: &OtaParams) -> Result<()> { - let add = ADDRESS.parse::
()?; - let sig = STANDARD.decode(¶ms.message_sig)?; - let sig = MessageSignature::from_slice(&sig)?; - let secp = Secp256k1::verification_only(); - let signed = sig.is_signed_by_address(&secp, &add, signed_msg_hash(¶ms.sha256_hash))?; - match signed { - true => Ok(()), - false => Err(anyhow!("Failed signature check")), - } + crate::bitcoin_utils::check_signature(¶ms.sha256_hash, ¶ms.message_sig) } fn check_integrity(params: &OtaParams) -> Result<()> { - let f = File::open(UPDATE_BIN_PATH)?; - let mut reader = BufReader::new(f); - let mut engine = sha256::HashEngine::default(); - std::io::copy(&mut reader, &mut engine)?; - let hash = sha256::Hash::from_engine(engine); - if hash.to_string() == params.sha256_hash { - Ok(()) - } else { - Err(anyhow!( - "Integrity check failed! params: {} vs sdcard: {}", - params.sha256_hash, - hash.to_string() - )) - } + crate::bitcoin_utils::check_integrity(UPDATE_BIN_PATH, ¶ms.sha256_hash) } pub fn update_sphinx_key(params: &OtaParams) -> Result<()> {