Skip to content

Commit

Permalink
Add tests and fix response formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Oct 4, 2022
1 parent 4ce0c8b commit 71cc9a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/command/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,11 @@ pub fn encipher<const R: usize, T: trussed::Client>(
let plaintext = syscall!(ctx.backend.client_mut().encrypt(
Mechanism::Aes256Cbc,
key_id,
&ctx.data[..],
ctx.data,
&[],
None
))
.ciphertext;
ctx.reply.expand(&[0x02])?;
ctx.reply.expand(&plaintext)
}
46 changes: 46 additions & 0 deletions tests/command_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2022 Nitrokey GmbH
// SPDX-License-Identifier: LGPL-3.0-only
#![cfg(feature = "virtual")]

use hex_literal::hex;

#[test_log::test]
fn command_response() {
trussed::virt::with_ram_client("opcard", |client| {
let mut card = opcard::Card::new(client, opcard::Options::default());
let reset_command: iso7816::Command<4> =
iso7816::Command::try_from(&hex!("00 44 0000")).unwrap();
let mut rep: heapless::Vec<u8, 0> = heapless::Vec::new();
card.handle(&reset_command, &mut rep).unwrap();
rep.clear();

let admin_pin_cmd: iso7816::Command<32> =
iso7816::Command::try_from(hex!("00200083 08 3132333435363738").as_slice()).unwrap();
card.handle(&admin_pin_cmd, &mut rep).unwrap();
rep.clear();

let user_pin_cmd: iso7816::Command<32> =
iso7816::Command::try_from(hex!("00200082 06 313233343536").as_slice()).unwrap();
card.handle(&user_pin_cmd, &mut rep).unwrap();

let mut set_aes_key = Vec::from(hex!("0C DA 00D5 20 "));
set_aes_key.extend_from_slice(&[0; 32]);
let import_cmd: iso7816::Command<32> = iso7816::Command::try_from(&set_aes_key).unwrap();
card.handle(&import_cmd, &mut rep).unwrap();

let encrypt_aes = Vec::from(hex!("00 2A 86 80 10 00112233445566778899AABBCCDDEEFF 00"));
let encrypt_cmd: iso7816::Command<16> = iso7816::Command::try_from(&encrypt_aes).unwrap();
let mut rep: heapless::Vec<u8, 17> = heapless::Vec::new();
card.handle(&encrypt_cmd, &mut rep).unwrap();
assert_eq!(rep, hex!("02 1c060f4c9e7ea8d6ca961a2d64c05c18"));

let mut decrypt_aes = Vec::from(hex!("00 2A 80 86 11"));
decrypt_aes.extend_from_slice(&rep);
decrypt_aes.push(0x00);

let decrypt_cmd: iso7816::Command<17> = iso7816::Command::try_from(&decrypt_aes).unwrap();
let mut rep: heapless::Vec<u8, 16> = heapless::Vec::new();
card.handle(&decrypt_cmd, &mut rep).unwrap();
assert_eq!(rep, hex!("00112233445566778899AABBCCDDEEFF"));
})
}

0 comments on commit 71cc9a8

Please sign in to comment.