Skip to content

Commit

Permalink
Merge pull request #24 from kazuki0824/fix/Reformat__clippy
Browse files Browse the repository at this point in the history
Reformat code
  • Loading branch information
kazuki0824 authored Jul 18, 2022
2 parents b285c95 + 57782c4 commit 87d4dbe
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 130 deletions.
27 changes: 17 additions & 10 deletions b25-sys/src/access_control.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::access_control::block00_mac::verify_mac;
use crate::access_control::block00_structure::{expand_00, Block00};
use crate::access_control::types::Block00CbcDec;
use std::ops::Deref;
use tail_cbc::cipher::{Key, KeyIvInit};
use tail_cbc::UnalignedBytesDecryptMut;
use crate::access_control::block00_mac::verify_mac;
use crate::access_control::block00_structure::{Block00, expand_00};
use crate::access_control::types::Block00CbcDec;

pub mod types;
mod block00_mac;
mod block00_structure;
mod block40_structure;
mod macros;
mod block00_mac;
pub mod types;

pub(crate) fn select_key_by_auth(payload: &mut [u8]) -> Option<Key<Block00>> {
let size = payload.len();
Expand All @@ -20,22 +21,28 @@ pub(crate) fn select_key_by_auth(payload: &mut [u8]) -> Option<Key<Block00>> {
let encrypted_part = &mut payload[3..];

let mut ret = None;
for k in if working_key_id % 2 == 1 { crate::KEY1.lock() } else { crate::KEY0.lock() }.unwrap().deref()
for k in if working_key_id % 2 == 1 {
crate::KEY1.lock()
} else {
crate::KEY0.lock()
}
.unwrap()
.deref()
{
let expanded_key = expand_00(*k, 0);
let mut dec =
Block00CbcDec::new(&expanded_key, &0xfe27199919690911u64.to_be_bytes().into());
dec.decrypt_bytes_b2b_mut(encrypted_part, &mut temp[3..]).expect("decryption failed");
dec.decrypt_bytes_b2b_mut(encrypted_part, &mut temp[3..])
.expect("decryption failed");

//mac is the last 4 bytes of the payload
let (content, mac) = temp.split_at_mut(size - 4);

if verify_mac(mac, content, k.to_le_bytes().into()).is_ok()
{
if verify_mac(mac, content, k.to_le_bytes().into()).is_ok() {
encrypted_part.copy_from_slice(&temp[3..]);
ret = Some(expanded_key);
break;
}
}
ret
}
}
22 changes: 11 additions & 11 deletions b25-sys/src/access_control/block00_mac.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use cbc_mac::{CbcMac, Mac};
use crate::access_control::block00_structure::BlockSize00;
use cbc_mac::digest::MacError;
use cbc_mac::{CbcMac, Mac};
use cryptography_00::feistel;
use tail_cbc::cipher;
use tail_cbc::cipher::{BlockCipher, InvalidLength, Key, KeyInit, KeySizeUser};
use tail_cbc::cipher::typenum::{U1, U8};
use crate::access_control::block00_structure::BlockSize00;
use tail_cbc::cipher::{BlockCipher, InvalidLength, Key, KeyInit, KeySizeUser};

type KeySizeRound = U8;

pub fn verify_mac(mac: &[u8], text: &[u8], key: Key<Round00>) -> Result<(), MacError>
{
pub fn verify_mac(mac: &[u8], text: &[u8], key: Key<Round00>) -> Result<(), MacError> {
let mut cbc_mac = <CbcMac<Round00> as Mac>::new(&key);
cbc_mac.update(text);
cbc_mac.verify_truncated_right(mac.into())
cbc_mac.verify_truncated_right(mac)
}

#[derive(Clone)]
Expand All @@ -32,7 +31,7 @@ impl KeyInit for Round00 {
8 => {
let key = u64::from_le_bytes(key.try_into().unwrap());
Ok(Self { key })
},
}
_ => Err(InvalidLength),
}
}
Expand Down Expand Up @@ -72,9 +71,10 @@ fn test_verify_cbcmac_zero() {
#[test]
fn test_verify_cbcmac_unaligned_random_values() {
let key = 0x0001020304050607u64.to_le_bytes();
let ciphertext = [ 0xC6, 0x80, 0x20, 0x3A, 0x53, 0x1E, 0x5C, 0xC7,
0xB6, 0x08, 0xAF, 0xA1, 0x2B, 0x19, 0x26, 0x8A,
0x47, 0xE1, 0x86, 0x74, 0xE9, ];
let ciphertext = [
0xC6, 0x80, 0x20, 0x3A, 0x53, 0x1E, 0x5C, 0xC7, 0xB6, 0x08, 0xAF, 0xA1, 0x2B, 0x19, 0x26,
0x8A, 0x47, 0xE1, 0x86, 0x74, 0xE9,
];
let mac = (0x6e4087f4ef22b5du64).to_be_bytes();
assert!(verify_mac(&mac, &ciphertext, key.into()).is_ok());
}
}
12 changes: 6 additions & 6 deletions b25-sys/src/access_control/block00_structure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cryptography_00::{feistel, round_function};
use tail_cbc::cipher;
use tail_cbc::cipher::{BlockCipher, InvalidLength, Key, KeyInit, KeySizeUser};
use tail_cbc::cipher::typenum::{U16, U3, U8};
use tail_cbc::cipher::{BlockCipher, InvalidLength, Key, KeyInit, KeySizeUser};

type KeySize00 = U16;
pub(crate) type BlockSize00 = U8;
Expand All @@ -25,9 +25,9 @@ pub(crate) fn expand_00(key: u64, protocol: u8) -> Key<Block00> {
}
Key::<Block00>::from_exact_iter(
//no vec
exp00.into_iter().map(|x| x.to_le_bytes()).flatten(),
exp00.into_iter().flat_map(|x| x.to_le_bytes()),
)
.unwrap()
.unwrap()
}

#[derive(Clone)]
Expand Down Expand Up @@ -100,7 +100,6 @@ crate::impl_block_encdec!(
);
const ROUND_INDEX: [u8; 16] = [1, 0, 1, 2, 2, 2, 0, 2, 1, 3, 0, 2, 1, 0, 0, 1];


#[test]
fn cbc() {
use cipher::KeyIvInit;
Expand All @@ -122,7 +121,8 @@ fn cbc() {
);

let mut result = c.clone();
b.decrypt_bytes_b2b_mut(&c, &mut result).expect("TODO: panic message");
b.decrypt_bytes_b2b_mut(&c, &mut result)
.expect("TODO: panic message");

assert_eq!(result, p);
}
Expand Down Expand Up @@ -160,4 +160,4 @@ fn test_zero_enc_block00() {

let c = u64::from_be_bytes(p.into());
assert_eq!(c, 13290258443935467468u64);
}
}
91 changes: 45 additions & 46 deletions b25-sys/src/bindings/arib_std_b25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#![allow(non_upper_case_globals)]
#![warn(dead_code)]

use std::marker::PhantomPinned;
use crate::bindings::error::BCasCardError;
use std::marker::PhantomPinned;

pub const _STDINT_H: u32 = 1;
pub const _FEATURES_H: u32 = 1;
Expand Down Expand Up @@ -1304,7 +1304,6 @@ fn bindgen_test_layout_B_CAS_CARD_PRIVATE_DATA() {
);
}


#[test]
#[cfg(target_os = "windows")]
fn bindgen_test_layout_B_CAS_CARD_PRIVATE_DATA() {
Expand All @@ -1322,110 +1321,110 @@ fn bindgen_test_layout_B_CAS_CARD_PRIVATE_DATA() {
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).mng as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(mng)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(mng)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).card as *const _ as usize },
4usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(card)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(card)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).pool as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(pool)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(pool)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).reader as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(reader)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(reader)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).sbuf as *const _ as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(sbuf)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(sbuf)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).rbuf as *const _ as usize },
32usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(rbuf)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(rbuf)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).stat as *const _ as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(stat)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(stat)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).id as *const _ as usize },
96usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(id)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(id)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).id_max as *const _ as usize },
112usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(id_max)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(id_max)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).pwc as *const _ as usize },
120usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(pwc)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(pwc)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<B_CAS_CARD_PRIVATE_DATA>())).pwc_max as *const _ as usize },
136usize,
concat!(
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(pwc_max)
"Offset of field: ",
stringify!(B_CAS_CARD_PRIVATE_DATA),
"::",
stringify!(pwc_max)
)
);
}
7 changes: 3 additions & 4 deletions b25-sys/src/bindings/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::marker::PhantomPinned;
use std::ptr::null_mut;

use log::{debug, warn};
use crate::access_control::select_key_by_auth;
use log::{debug, warn};

use crate::bindings::arib_std_b25::{
wchar_t, B_CAS_CARD, B_CAS_CARD_PRIVATE_DATA, B_CAS_ECM_RESULT, B_CAS_ID, B_CAS_INIT_STATUS,
Expand Down Expand Up @@ -100,11 +100,11 @@ unsafe extern "C" fn proc_ecm(
Some(key) => {
debug!("Selected Kw= {:?}", key);
Ok(&payload[3..19])
},
}
None => {
warn!("No valid key found");
Err(())
},
}
}
}
};
Expand Down Expand Up @@ -165,4 +165,3 @@ impl Default for B_CAS_CARD {
}
}
}

8 changes: 3 additions & 5 deletions b25-sys/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ impl InnerDecoder {

#[cfg(feature = "block00cbc")]
if key {
return Self::new_with_key(dec)
return Self::new_with_key(dec);
}
Self::new_without_key(dec)
}

#[cfg(feature = "block00cbc")]
unsafe fn new_with_key(dec: *mut ARIB_STD_B25) -> Result<Self, AribB25DecoderError>
{
unsafe fn new_with_key(dec: *mut ARIB_STD_B25) -> Result<Self, AribB25DecoderError> {
let mut cas = B_CAS_CARD::default();
//Allocate private data inside B_CAS_CARD
cas.initialize();
Expand All @@ -55,8 +54,7 @@ impl InnerDecoder {
ret.dec.as_ref().set_b_cas_card(ret.cas.as_ref().unwrap());
Ok(ret)
}
unsafe fn new_without_key(dec: *mut ARIB_STD_B25) -> Result<Self, AribB25DecoderError>
{
unsafe fn new_without_key(dec: *mut ARIB_STD_B25) -> Result<Self, AribB25DecoderError> {
let cas = arib_std_b25::create_b_cas_card();
if cas.is_null() {
Err(AribB25DecoderError::ARIB_STD_B25_ERROR_EMPTY_B_CAS_CARD)
Expand Down
4 changes: 2 additions & 2 deletions b25-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use futures_core::ready;
use std::cell::Cell;
use std::io::{Read, Write};
use std::pin::Pin;
use std::sync::Mutex;
use std::task::{Context, Poll};
use futures_core::ready;

pub use futures_io;
use futures_io::{AsyncBufRead, AsyncRead};
Expand Down Expand Up @@ -95,7 +95,7 @@ impl AsyncRead for StreamDecoder<'_> {
// Poll::Pending
} else {
//Write to this.inner in order to decode, and read from this.inner in order to write to buf
this.inner.write(recv).unwrap();
this.inner.write_all(recv).expect("write_all failed");
this.reader.as_mut().consume(n);
this.received.set(this.received.get() + n);

Expand Down
Loading

0 comments on commit 87d4dbe

Please sign in to comment.