Skip to content

Commit

Permalink
Disable endksgen_crypt shortcut
Browse files Browse the repository at this point in the history
We get an EasyDMA error if this shortcut is enabled and the
DataRateof the CCM is different from the one configured in the RADIO.
  • Loading branch information
thalesfragoso committed Jun 4, 2020
1 parent 0982e76 commit c5c3c74
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion examples/ccm-demo/Embed.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ restore_unwritten_bytes = false

[general]
# The chip name of the chip to be debugged.
chip = "nRF52832"
#chip = "nRF52832"
# A list of chip descriptions to be loaded during runtime.
chip_descriptions = []
# The default log level to be used.
Expand Down
15 changes: 1 addition & 14 deletions examples/ccm-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() -> ! {

let mut ccm_data_enc = CcmData::new(KEY, iv);
let mut ccm_data_dec = CcmData::new(KEY, iv);
let mut ccm = Ccm::init(p.CCM, p.AAR, DataRate::_1Mbit);
let mut ccm = Ccm::init(p.CCM, p.AAR, DataRate::_2Mbit);

let mut clear_buffer = [0u8; 254];
let mut cipher_buffer = [0u8; 258];
Expand Down Expand Up @@ -87,16 +87,6 @@ fn main() -> ! {

rprintln!("Encryption Took: {} us", now);

//rprint!("Cipher Packet: ");
//for number in cipher_buffer.iter().take(length + MIC_SIZE + HEADER_SIZE) {
// rprint!("{:x} ", *number);
//}

// Since we're both encrypting and decrypting, we need to decrement the counter to have the
// same counter that encrypted the message. `encrypt_packet` and `decrypt_packet`
// automatically increments the counter when the operation succeeds.
//ccm_data.decrement_counter();

// Clears the buffer, so we can inspect the decrypted text
clear_buffer = [0u8; 254];

Expand All @@ -122,9 +112,6 @@ fn main() -> ! {
&MSG[..length]
);

//let msg = core::str::from_utf8(&clear_buffer[HEADER_SIZE..length]).unwrap();
//rprintln!("Clear text: {}\n", msg);

// Clears the cipher text for next round
cipher_buffer = [0u8; 258];
}
Expand Down
26 changes: 17 additions & 9 deletions nrf-hal-common/src/ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ impl Ccm {
// This register is shared with AAR, reset it and write the chosen data rate
regs.mode.write(|w| w.datarate().variant(data_rate.into()));

regs.shorts.write(|w| w.endksgen_crypt().enabled());
regs.enable.write(|w| w.enable().enabled());

Self { regs, _aar: arr }
Expand All @@ -190,7 +189,7 @@ impl Ccm {
/// The generated MIC will be appended to after the payload in the `cipher_packet`. The slices
/// passed to this method must have the correct size, for more information refer to the module
/// level documentation. The counter in `ccm_data` will be incremented if the operation
/// succeeds.
/// succeeds. All parameters passed to this method must reside in RAM.
pub fn encrypt_packet(
&mut self,
ccm_data: &mut CcmData,
Expand Down Expand Up @@ -228,7 +227,7 @@ impl Ccm {
let length_variant = if payload_len <= MAXIMUM_LENGTH_5BITS {
LENGTH_A::DEFAULT
} else {
#[cfg(any(feature = "52840", feature = "52833"))]
#[cfg(any(feature = "52840", feature = "52833", feature = "52810"))]
// NOTE(unsafe) Any 8bits pattern is safe to write to this register
self.regs
.maxpacketsize
Expand Down Expand Up @@ -267,11 +266,15 @@ impl Ccm {
// "Preceding reads and writes cannot be moved past subsequent writes."
compiler_fence(Ordering::Release);

// Start key generation, encryption will start automatically because of the enabled short
// in init
// Start key generation
// NOTE(unsafe) 1 is a valid pattern to write to this register
self.regs.tasks_ksgen.write(|w| unsafe { w.bits(1) });

while self.regs.events_endksgen.read().bits() == 0 {}

// NOTE(unsafe) 1 is a valid pattern to write to this register
self.regs.tasks_crypt.write(|w| unsafe { w.bits(1) });

while self.regs.events_endcrypt.read().bits() == 0
&& self.regs.events_error.read().bits() == 0
{}
Expand All @@ -292,7 +295,8 @@ impl Ccm {
///
/// This method will return an error if the MIC verification fails. The slices passed to this
/// method must have the correct size, for more information refer to the module level
/// documentation. The counter in `ccm_data` will be incremented if the operation succeeds.
/// documentation. The counter in `ccm_data` will be incremented if the operation succeeds. All
/// parameters passed to this method must reside in RAM.
pub fn decrypt_packet(
&mut self,
ccm_data: &mut CcmData,
Expand Down Expand Up @@ -335,7 +339,7 @@ impl Ccm {
let length_variant = if payload_len <= MAXIMUM_LENGTH_5BITS {
LENGTH_A::DEFAULT
} else {
#[cfg(any(feature = "52840", feature = "52833"))]
#[cfg(any(feature = "52840", feature = "52833", feature = "52810"))]
// NOTE(unsafe) Any 8bits pattern is safe to write to this register
self.regs
.maxpacketsize
Expand Down Expand Up @@ -374,11 +378,15 @@ impl Ccm {
// "Preceding reads and writes cannot be moved past subsequent writes."
compiler_fence(Ordering::Release);

// Start key generation, decryption will start automatically because of the enabled short
// in init
// Start key generation
// NOTE(unsafe) 1 is a valid pattern to write to this register
self.regs.tasks_ksgen.write(|w| unsafe { w.bits(1) });

while self.regs.events_endksgen.read().bits() == 0 {}

// NOTE(unsafe) 1 is a valid pattern to write to this register
self.regs.tasks_crypt.write(|w| unsafe { w.bits(1) });

while self.regs.events_endcrypt.read().bits() == 0
&& self.regs.events_error.read().bits() == 0
{}
Expand Down

0 comments on commit c5c3c74

Please sign in to comment.