-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Audible AAX support #99
Open
LinusU
wants to merge
3
commits into
alfg:master
Choose a base branch
from
LinusU:aax
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
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
Here is some work in progress code for actually decoding the data: use aes::{
cipher::{generic_array::GenericArray, BlockDecryptMut, KeyIvInit},
Aes128,
};
use cbc::Decryptor;
use mp4::Mp4Reader;
use sha1::{Digest, Sha1};
use std::fs::File;
use std::io::BufReader;
const AUDIBLE_FIXED_KEY: [u8; 16] = [
0x77, 0x21, 0x4d, 0x4b, 0x19, 0x6a, 0x87, 0xcd, 0x52, 0x00, 0x45, 0xfd, 0x20, 0xa5, 0x1d, 0x67,
];
fn get_reader(path: &str) -> Mp4Reader<BufReader<File>> {
let f = File::open(path).unwrap();
let f_size = f.metadata().unwrap().len();
let reader = BufReader::new(f);
mp4::Mp4Reader::read_header(reader, f_size).unwrap()
}
#[test]
fn test_read_aax() {
let mut mp4 = get_reader("tests/samples/YourFirstListen_ep7.aax");
assert_eq!(mp4.ftyp.major_brand, "aax ".parse().unwrap());
let track = mp4.tracks().get(&1).unwrap();
// Put your activation bytes here!
let activation_bytes = [0x00, 0x00, 0x00, 0x00];
let adrm = track
.trak
.mdia
.minf
.stbl
.stsd
.mp4a
.as_ref()
.and_then(|mp4a| mp4a.adrm.as_ref())
.unwrap();
// Key Derivation
let mut sha = Sha1::new();
sha.update(AUDIBLE_FIXED_KEY);
sha.update(activation_bytes);
let intermediate_key = sha.finalize();
let mut sha = Sha1::new();
sha.update(AUDIBLE_FIXED_KEY);
sha.update(intermediate_key);
sha.update(activation_bytes);
let intermediate_iv = sha.finalize();
let mut sha = Sha1::new();
sha.update(&intermediate_key[..16]);
sha.update(&intermediate_iv[..16]);
let calculated_checksum = sha.finalize();
eprintln!("file_checksum: {:x?}", adrm.file_checksum);
eprintln!("calculated_checksum: {:x?}", calculated_checksum);
assert_eq!(calculated_checksum, adrm.file_checksum.into());
// Decryption setup
let mut aes =
Decryptor::<Aes128>::new_from_slices(&intermediate_key[..16], &intermediate_iv[..16])
.unwrap();
let mut data = adrm.drm_blob.to_owned();
aes.decrypt_block_mut(GenericArray::from_mut_slice(&mut data[0..16]));
aes.decrypt_block_mut(GenericArray::from_mut_slice(&mut data[16..32]));
aes.decrypt_block_mut(GenericArray::from_mut_slice(&mut data[32..48]));
assert_eq!(activation_bytes[0], data[3]);
assert_eq!(activation_bytes[1], data[2]);
assert_eq!(activation_bytes[2], data[1]);
assert_eq!(activation_bytes[3], data[0]);
eprintln!(
"Activation bytes: {:02x}{:02x}{:02x}{:02x}",
data[3], data[2], data[1], data[0]
);
// Read the entire track
for sample_id in 1.. {
if let Some(sample) = mp4.read_sample(1, sample_id).unwrap() {
assert!(!sample.bytes.is_empty());
let mut data = sample.bytes.to_vec();
// Reset the IV for each sample
let mut aes = Decryptor::<Aes128>::new_from_slices(
&intermediate_key[..16],
&intermediate_iv[..16],
)
.unwrap();
// trailing bytes are not encrypted!
let block_count = data.len() / 16;
for j in 0..block_count {
let start = j * 16;
let end = start + 16;
aes.decrypt_block_mut(GenericArray::from_mut_slice(&mut data[start..end]));
}
eprintln!("Decrypted sample {}: {} bytes", sample_id, data.len());
} else {
break;
}
}
} Now, I think that this works, but I haven't actually tested it yet since I need to figure out how to write the decoded bytes into a new mp4 file with the aac data 😅 |
(rebased on ping @alfg, do you have any input on this? 🙏 |
Hey @LinusU, sorry for the late response and thanks for the PR. I will check this out soon. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Opening this for some early feedback. My goal is to read and decrypt audio data from Audibles AAX files.
I learned how the
adrm
boxed worked by looking at FFmpeg and at some sample files with a hex editor. There is still 60 bytes that I don't know what they are for (unknown0
) but they seem to be random bytes, maybe more checksums? 🤔FFmpeg/libavformat also does the actual decryption when reading the track data, but I thought that that was out of scope for this project. Let me know what you think!
The biggest issue right now is the
BoxType::UnknownBox(0x61617664)
part.aavd
boxes are basically identical tomp4a
boxes, so I wanted to just reuse that instead of re-implementing the entireMp4aBox
again. It seems like something similar is also true foralac
andfLaC
as well. I'm very open to suggestions here!