Skip to content

Commit

Permalink
asdfasdfasfdasd
Browse files Browse the repository at this point in the history
  • Loading branch information
Maccraft123 committed Nov 20, 2023
1 parent 824fc9a commit c45d5a9
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 76 deletions.
8 changes: 4 additions & 4 deletions cytryna/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ bitflags = "2.4.1"
bmp = "0.5.0"
cbc = { version = "0.1.2", features = ["alloc"] }
ctr = { version = "0.9.2", features = ["std"] }
derivative = "2.2.0"
hex = "0.4.3"
derivative = { version = "2.2.0", features = ["use_core"] }
hex = "0.4.3" # FIXME: no_std blocker
hex-literal = "0.4.1"
memoffset = "0.9.0"
modular-bitfield = "0.11.2"
sha2 = "0.10.8"
sha2 = { version = "0.10.8", default-features = false }
snafu = { version = "0.7.5", default-features = false, features = ["rust_1_39", "rust_1_46", "rust_1_61"] }
static_assertions = "1.1.0"
thiserror = "1.0.50"

[dev-dependencies]
rand = "0.8.5"
2 changes: 1 addition & 1 deletion cytryna/src/cia.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use core::mem;

use crate::crypto::aes128_ctr::*;
use crate::smdh::Smdh;
Expand Down
30 changes: 14 additions & 16 deletions cytryna/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use core::{fmt, mem, num, str::FromStr};
use core::marker::PhantomData;

use std::collections::HashMap;
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::num;
use std::str::FromStr;
use std::sync::OnceLock;

use crate::string::SizedCString;
use crate::{CytrynaError, CytrynaResult, FromBytes};

use sha2::{Digest, Sha256};
use thiserror::Error;
use snafu::Snafu;

pub mod aes128_ctr {
pub use aes::cipher::block_padding::NoPadding;
Expand Down Expand Up @@ -69,7 +67,7 @@ impl KeyBag {
}
#[must_use]
pub fn get_key(&self, idx: KeyIndex) -> CytrynaResult<&[u8; 0x10]> {
self.keys.get(&idx).ok_or(CytrynaError::MissingKey(idx))
self.keys.get(&idx).ok_or(CytrynaError::MissingKey{idx: idx})
}
#[must_use]
pub fn global() -> CytrynaResult<&'static Self> {
Expand Down Expand Up @@ -108,14 +106,14 @@ impl fmt::Display for KeyIndex {
}
}

#[derive(Error, Debug)]
#[derive(Debug, Snafu)]
pub enum KeyIndexParseError {
#[error("Failed to parse a hex number")]
NumberParseError(#[from] num::ParseIntError),
#[error("Invalid key type \"{0}\"")]
InvalidKeyType(String),
#[error("Invalid X/Y/Z key type \"{0}\"")]
InvalidKeyXYNType(String),
#[snafu(display("Failed to parse a hex number"), context(false))]
NumberParseError{source: num::ParseIntError},
#[snafu(display("Invalid key type \"{ty}\""))]
InvalidKeyType{ty: String},
#[snafu(display("Invalid X/Y/Z key type \"{ty}\""))]
InvalidKeyXYNType{ty: String},
}

impl FromStr for KeyIndex {
Expand All @@ -131,7 +129,7 @@ impl FromStr for KeyIndex {
"keyx" => KeyType::X,
"keyy" => KeyType::Y,
"keyn" => KeyType::N,
_ => return Err(KeyIndexParseError::InvalidKeyXYNType(from[2..].to_string())),
_ => return Err(KeyIndexParseError::InvalidKeyXYNType{ty: from[2..].to_string()}),
};
Ok(Self::Slot(num, keytype))
} else if from.starts_with("common") {
Expand All @@ -144,7 +142,7 @@ impl FromStr for KeyIndex {
Ok(Self::CommonN(num))
}
} else {
Err(KeyIndexParseError::InvalidKeyType(from.to_string()))
Err(KeyIndexParseError::InvalidKeyType{ty: from.to_string()})
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions cytryna/src/firm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use core::mem;

use crate::crypto::sha256;
use crate::string::SizedCString;
Expand All @@ -8,7 +8,7 @@ use crate::{align_up, CytrynaError, CytrynaResult};
use derivative::Derivative;
use hex_literal::hex;
use static_assertions::assert_eq_size;
use thiserror::Error;
use snafu::Snafu;

// source: https://gist.github.com/SciresM/cdd2266efb80175d37eabbe86f9d8c52
static RETAIL_NAND_FIRM: [u8; 0x100] = hex!("B6724531C448657A2A2EE306457E350A10D544B42859B0E5B0BED27534CCCC2A4D47EDEA60A7DD99939950A6357B1E35DFC7FAC773B7E12E7C1481234AF141B31CF08E9F62293AA6BAAE246C15095F8B78402A684D852C680549FA5B3F14D9E838A2FB9C09A15ABB40DCA25E40A3DDC1F58E79CEC901974363A946E99B4346E8A372B6CD55A707E1EAB9BEC0200B5BA0B661236A8708D704517F43C6C38EE9560111E1405E5E8ED356C49C4FF6823D1219AFAEEB3DF3C36B62BBA88FC15BA8648F9333FD9FC092B8146C3D908F73155D48BE89D72612E18E4AA8EB9B7FD2A5F7328C4ECBFB0083833CBD5C983A25CEB8B941CC68EB017CE87F5D793ACA09ACF7");
Expand Down Expand Up @@ -103,17 +103,17 @@ pub enum CopyMethod {
CpuMemcpy,
}

#[derive(Error, Debug)]
#[derive(Debug, Snafu)]
pub enum FirmBuilderError {
#[error("Tried to add more than firware 4 sections")]
#[snafu(display("Tried to add more than firware 4 sections"))]
TooManySections,
#[error("Arm9 entry point is missing")]
#[snafu(display("Arm9 entry point is missing"))]
NoArm9Entry,
#[error("Arm11 entry point is missing")]
#[snafu(display("Arm11 entry point is missing"))]
NoArm11Entry,
#[error("Firmware sections are missing")]
#[snafu(display("Firmware sections are missing"))]
NoSections,
#[error("Signature type is missing")]
#[snafu(display("Signature type is missing"))]
NoSig,
}

Expand Down
46 changes: 23 additions & 23 deletions cytryna/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,45 @@ pub mod tmd;

use std::ops::Deref;

use thiserror::Error;
use snafu::Snafu;

#[derive(Error, Debug)]
#[derive(Debug, Snafu)]
pub enum CytrynaError {
#[error("Invalid magic bytes")]
#[snafu(display("Invalid magic bytes"))]
InvalidMagic,
#[error("Missing region")]
#[snafu(display("Missing region"))]
MissingRegion,
#[error("Invalid size of header")]
#[snafu(display("Invalid size of header"))]
InvalidHeaderSize,
#[error("Invalid hash")]
#[snafu(display("Invalid hash"))]
InvalidHash,
#[error("Signature corrupted/forged")]
#[snafu(display("Signature corrupted/forged"))]
SignatureCorrupted,
#[error("Invalid region position")]
#[snafu(display("Invalid region position"))]
InvalidRegionPosition,
#[error("Unsupported version of header")]
#[snafu(display("Unsupported version of header"))]
UnsupportedHeaderVersion,
#[error("Missing {0} key")]
MissingKey(crypto::KeyIndex),
#[error("Uninitialized keybag")]
#[snafu(display("Missing {idx} key"))]
MissingKey{idx: crypto::KeyIndex},
#[snafu(display("Uninitialized keybag"))]
NoKeyBag,
#[error("Value out of range for {0} enum")]
EnumValueOutOfRange(&'static str),
#[error("Byte slice passed is too small")]
#[snafu(display("Value out of range for {name} enum"))]
EnumValueOutOfRange{name: &'static str},
#[snafu(display("Byte slice passed is too small"))]
SliceTooSmall,
#[error("Invalid length of {what}: {actual} (expected {expected})")]
#[snafu(display("Invalid length of {what}: {actual} (expected {expected})"))]
InvalidLength{
what: &'static str,
actual: usize,
expected: usize,
},
#[error("Failed to parse keyindex")]
KeyIndexFail(#[from] crypto::KeyIndexParseError),
#[error("Failed to stream-encrypt/decrypt data")]
StreamCrypt(#[from] ctr::cipher::StreamCipherError),
#[error("Failed to decode hex string")]
HexError(#[from] hex::FromHexError),
#[error("Incorrect alignment")]
#[snafu(display("Failed to parse keyindex"), context(false))]
KeyIndexFail{source: crypto::KeyIndexParseError},
#[snafu(display("Failed to stream-encrypt/decrypt data"), context(false))]
StreamCrypt{source: ctr::cipher::StreamCipherError},
#[snafu(display("Failed to decode hex string"), context(false))]
HexError{source: hex::FromHexError},
#[snafu(display("Incorrect alignment"))]
BadAlign,
}

Expand Down
7 changes: 3 additions & 4 deletions cytryna/src/ncch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub mod exefs;

use std::fmt;
use std::mem;
use std::os::raw::c_char;
use core::fmt;
use core::mem;

use crate::crypto::{self, aes128_ctr::*, KeyBag, KeyIndex, KeyType};
use crate::string::SizedCString;
Expand All @@ -23,7 +22,7 @@ pub struct NcchHeader {
#[derivative(Debug = "ignore")]
content_size: u32,
partition_id: u64,
maker_code: [c_char; 2],
maker_code: [u8; 2],
version: u16,
content_lock_seed_hash: u32,
program_id: u64,
Expand Down
24 changes: 12 additions & 12 deletions cytryna/src/smdh.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::mem;
use std::slice;
use core::mem;
use core::slice;

use crate::string::{SizedCString, SizedCStringError, SizedCStringUtf16};
use crate::{CytrynaError, CytrynaResult, FromBytes};
Expand All @@ -9,23 +9,23 @@ use bmp::{px, Pixel};
use derivative::Derivative;
use modular_bitfield::prelude::*;
use static_assertions::assert_eq_size;
use thiserror::Error;
use snafu::Snafu;

#[derive(Error, Debug)]
#[derive(Debug, Snafu)]
pub enum SmdhError {
#[error("Missing short description")]
#[snafu(display("Missing short description"))]
MissingShortDesc,
#[error("Missing long description")]
#[snafu(display("Missing long description"))]
MissingLongDesc,
#[error("Missing publisher name")]
#[snafu(display("Missing publisher name"))]
MissingPublisher,
#[error("Missing icon data")]
#[snafu(display("Missing icon data"))]
MissingIcon,
#[error("SizedCString error: {0}")]
StringErr(#[from] SizedCStringError),
#[error("Invalid BMP image size, got: {got}, expected: {expected}")]
#[snafu(display("SizedCString error"), context(false))]
StringErr{source: SizedCStringError},
#[snafu(display("Invalid BMP image size, got: {got}, expected: {expected}"))]
InvalidBmpSize { got: u32, expected: u32 },
#[error("Only square images can be SMDH icons")]
#[snafu(display("Only square images can be SMDH icons"))]
OnlySquaresAllowed,
}

Expand Down
10 changes: 6 additions & 4 deletions cytryna/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{borrow::Cow, fmt, str, string};
use core::{fmt, str};

use thiserror::Error;
use std::{borrow::Cow, string};

#[derive(Error, Debug)]
use snafu::Snafu;

#[derive(Debug, Snafu)]
pub enum SizedCStringError {
#[error("Input string too big to fit into storage")]
#[snafu(display("Input string too big to fit into storage"))]
TooBig,
}

Expand Down
2 changes: 1 addition & 1 deletion cytryna/src/ticket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use core::mem;

use crate::crypto::{aes128_ctr::*, KeyBag, KeyIndex, SignedData};
use crate::titleid::MaybeTitleIdBe;
Expand Down
4 changes: 2 additions & 2 deletions cytryna/src/titleid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use core::mem;

use crate::{CytrynaError, CytrynaResult};
use bitflags::bitflags;
Expand Down Expand Up @@ -63,7 +63,7 @@ impl TitleId {
let platform = (what & 0x0000_0000_0000_ffff) as u16;

if platform >= 6 || platform == 0 {
return Err(CytrynaError::EnumValueOutOfRange("smdh::Platform"));
return Err(CytrynaError::EnumValueOutOfRange{name: "smdh::Platform"});
}

Ok(unsafe { mem::transmute(what) })
Expand Down
2 changes: 1 addition & 1 deletion cytryna/src/tmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, mem, ptr, slice};
use core::{fmt, mem, ptr, slice};

use crate::crypto::SignedData;
use crate::titleid::{MaybeTitleIdBe, TitleId};
Expand Down

0 comments on commit c45d5a9

Please sign in to comment.