Skip to content

Commit

Permalink
Update rust-crypto crates
Browse files Browse the repository at this point in the history
  • Loading branch information
DCNick3 committed Jan 8, 2023
1 parent 5072c73 commit 7b20e6f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 111 deletions.
105 changes: 28 additions & 77 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Thog <[email protected]>"]
license = "MIT/Apache-2.0"
homepage = "https://github.com/MegatonHammer/linkle/"
repository = "https://github.com/MegatonHammer/linkle/"
edition = "2018"
edition = "2021"
description = "Nintendo file format manipulation library and tools."

[[bin]]
Expand Down Expand Up @@ -42,10 +42,11 @@ bit_field = "0.10"
cargo-toml2 = { version = "1.3.2", optional = true }

# This group of crates had some breaking changes, so we can't update them
block-modes = "0.8.1"
ctr = "0.6"
aes = "0.6"
cmac = "0.5"
cipher = "0.4.3"
digest = "0.10.6"
ctr = "0.9.2"
aes = "0.8.2"
cmac = "0.7.1"

[features]
binaries = ["structopt", "cargo_metadata", "semver", "scroll", "goblin", "clap", "cargo-toml2"]
21 changes: 3 additions & 18 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use block_modes::BlockModeError;
use snafu::Snafu;
use snafu::{Backtrace, GenerateImplicitData};
use std::io;
Expand All @@ -24,11 +23,6 @@ pub enum Error {
error: io::Error,
backtrace: Backtrace,
},
#[snafu(display("Decryption failed"))]
BlockMode {
error: BlockModeError,
backtrace: Backtrace,
},
#[snafu(display("Error parsing the INI file: {}", error))]
Ini {
error: ini::Error,
Expand All @@ -38,7 +32,7 @@ pub enum Error {
Crypto { error: String, backtrace: Backtrace },
#[snafu(display("Invalid keyblob {}: {}.", id, error))]
MacError {
error: cmac::crypto_mac::MacError,
error: digest::MacError,
id: usize,
backtrace: Backtrace,
},
Expand Down Expand Up @@ -117,15 +111,6 @@ impl From<ini::Error> for Error {
}
}

impl From<BlockModeError> for Error {
fn from(error: BlockModeError) -> Error {
Error::BlockMode {
error,
backtrace: Backtrace::generate(),
}
}
}

impl From<serde_json::error::Error> for Error {
fn from(error: serde_json::error::Error) -> Error {
Error::Deserialization { error }
Expand All @@ -143,8 +128,8 @@ impl From<FromUtf8Error> for Error {
}
}

impl From<(usize, cmac::crypto_mac::MacError)> for Error {
fn from((id, error): (usize, cmac::crypto_mac::MacError)) -> Error {
impl From<(usize, digest::MacError)> for Error {
fn from((id, error): (usize, digest::MacError)) -> Error {
Error::MacError {
error,
id,
Expand Down
21 changes: 10 additions & 11 deletions src/pki.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::error::Error;
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{BlockCipher, SyncStreamCipher};
use aes::Aes128;
use aes::NewBlockCipher;
use cmac::{Cmac, Mac, NewMac};
use ctr::cipher::stream::NewStreamCipher;
use ctr::Ctr128;
use cipher::{
generic_array::GenericArray, BlockDecrypt, BlockEncrypt, KeyInit, KeyIvInit, StreamCipher,
};
use cmac::{Cmac, Mac};
use ctr::Ctr128BE;
use ini::{self, Properties};
use snafu::{Backtrace, GenerateImplicitData};
use std::fmt;
Expand Down Expand Up @@ -56,13 +55,13 @@ impl Keyblob {
let mut encrypted_keyblob = [0; 0xB0];
encrypted_keyblob[0x20..].copy_from_slice(&self.0);

let mut crypter = Ctr128::<Aes128>::new(
let mut crypter = <Ctr128BE<Aes128> as KeyIvInit>::new(
GenericArray::from_slice(&key.0),
GenericArray::from_slice(&encrypted_keyblob[0x10..0x20]),
);
crypter.apply_keystream(&mut encrypted_keyblob[0x20..]);

let mut cmac = Cmac::<Aes128>::new_varkey(&mac_key.0[..]).unwrap();
let mut cmac = <Cmac<Aes128> as KeyInit>::new_from_slice(&mac_key.0[..]).unwrap();
cmac.update(&encrypted_keyblob[0x10..]);
encrypted_keyblob[..0x10].copy_from_slice(cmac.finalize().into_bytes().as_slice());
Ok(EncryptedKeyblob(encrypted_keyblob))
Expand All @@ -79,12 +78,12 @@ impl EncryptedKeyblob {
let mut keyblob = [0; 0x90];
keyblob.copy_from_slice(&self.0[0x20..]);

let mut cmac = Cmac::<Aes128>::new_varkey(&mac_key.0[..]).unwrap();
let mut cmac = <Cmac<Aes128> as KeyInit>::new_from_slice(&mac_key.0[..]).unwrap();
cmac.update(&self.0[0x10..]);
cmac.verify(&self.0[..0x10])
cmac.verify(GenericArray::from_slice(&self.0[..0x10]))
.map_err(|err| (keyblob_id, err))?;

let mut crypter = Ctr128::<Aes128>::new(
let mut crypter = Ctr128BE::<Aes128>::new(
GenericArray::from_slice(&key.0),
GenericArray::from_slice(&self.0[0x10..0x20]),
);
Expand Down

0 comments on commit 7b20e6f

Please sign in to comment.