Skip to content

Commit 1a9cc1c

Browse files
committed
Correct bitfield bits position and manage bus endianness
While bitfield should follow exactly datasheet's information, endianness must be handled for bus transport which is always little. This fix converts endianness to/from bus for data to be handled by arch endianness (big or little). Signed-off-by: Yannick Lanz <yannick.lanz@wifx.net>
1 parent 2c68569 commit 1a9cc1c

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

src/address.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ bitfield! {
6868
#[derive(PartialEq, Clone)]
6969
pub struct OffsetAddress(u16);
7070
impl Debug;
71-
u8, offset, set_offset: 10, 8;
72-
u8, block, set_block: 12, 11;
71+
u8, block, set_block: 4, 3;
72+
u8, offset, set_offset: 2, 0;
7373
}
7474

7575
bitfield! {
7676
#[derive(PartialEq, Clone)]
7777
pub struct DataAddress(u16);
7878
impl Debug;
79-
u8, block, set_block: 3, 0;
80-
u8, offset, set_offset: 10, 8;
81-
u8, slot, set_slot: 14, 11;
79+
u8, block, set_block: 11, 8;
80+
u8, slot, set_slot: 6, 3;
81+
u8, offset, set_offset: 2, 0;
8282
}
8383

8484
impl From<&Address> for u16 {

src/command.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ bitfield! {
8585
#[derive(PartialEq)]
8686
pub struct LockParam(u8);
8787
impl Debug;
88-
u8, zone, set_zone: 1, 0;
89-
u8, slot, set_slot: 5, 2;
9088
crc, set_crc: 7;
89+
u8, slot, set_slot: 5, 2;
90+
u8, zone, set_zone: 1, 0;
9191
}
9292

9393
impl From<LockParam> for u8 {
@@ -138,7 +138,7 @@ macro_rules! put_cmd {
138138
($dest:ident, $cmd:ident, $param1:expr, $param2:expr) => {
139139
$dest.put_u8($cmd);
140140
$dest.put_u8($param1);
141-
$dest.put_u16($param2);
141+
$dest.put_u16_le($param2);
142142
};
143143
}
144144

@@ -189,7 +189,7 @@ impl EccCommand {
189189
put_cmd!(bytes, ATCA_INFO, 0, 0);
190190
}
191191
Self::GenKey { key_type, slot } => {
192-
put_cmd!(bytes, ATCA_GENKEY, u8::from(key_type), (*slot as u16) << 8);
192+
put_cmd!(bytes, ATCA_GENKEY, u8::from(key_type), *slot as u16);
193193
}
194194
Self::Read { is_32, address } => {
195195
let mut param1 = ReadWriteParam(0);
@@ -228,7 +228,7 @@ impl EccCommand {
228228
let mut param1 = SignParam(0);
229229
param1.set_source(source.into());
230230
param1.set_external(true);
231-
put_cmd!(bytes, ATCA_SIGN, u8::from(param1), (*key_slot as u16) << 8);
231+
put_cmd!(bytes, ATCA_SIGN, u8::from(param1), *key_slot as u16);
232232
}
233233
Self::Ecdh { x, y, key_slot } => {
234234
put_cmd!(bytes, ATCA_ECDH, 0, *key_slot as u16);

src/ecc.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
command::{EccCommand, EccResponse},
55
Address, DataBuffer, Error, KeyConfig, Result, SlotConfig, Zone,
66
};
7-
use bytes::{BufMut, Bytes, BytesMut};
7+
use bytes::{Buf, BufMut, Bytes, BytesMut};
88
use sha2::{Digest, Sha256};
99

1010
pub use crate::command::KeyType;
@@ -47,8 +47,14 @@ impl Ecc {
4747
let bytes = self.read(false, &Address::slot_config(slot)?)?;
4848
let (s0, s1) = bytes.split_at(2);
4949
match slot & 1 == 0 {
50-
true => Ok(SlotConfig::from(s0)),
51-
false => Ok(SlotConfig::from(s1)),
50+
true => {
51+
let mut buf = s0;
52+
Ok(SlotConfig::from(buf.get_u16_le()))
53+
}
54+
false => {
55+
let mut buf = s1;
56+
Ok(SlotConfig::from(buf.get_u16_le()))
57+
}
5258
}
5359
}
5460

@@ -59,12 +65,12 @@ impl Ecc {
5965
let mut new_bytes = BytesMut::with_capacity(4);
6066
match slot & 1 == 0 {
6167
true => {
62-
new_bytes.put_u16(config.into());
68+
new_bytes.put_u16_le(config.into());
6369
new_bytes.extend_from_slice(s1);
6470
}
6571
false => {
6672
new_bytes.extend_from_slice(s0);
67-
new_bytes.put_u16(config.into());
73+
new_bytes.put_u16_le(config.into());
6874
}
6975
}
7076
self.write(&slot_address, &new_bytes.freeze())
@@ -74,8 +80,14 @@ impl Ecc {
7480
let bytes = self.read(false, &Address::key_config(slot)?)?;
7581
let (s0, s1) = bytes.split_at(2);
7682
match slot & 1 == 0 {
77-
true => Ok(KeyConfig::from(s0)),
78-
false => Ok(KeyConfig::from(s1)),
83+
true => {
84+
let mut buf = s0;
85+
Ok(KeyConfig::from(buf.get_u16_le()))
86+
}
87+
false => {
88+
let mut buf = s1;
89+
Ok(KeyConfig::from(buf.get_u16_le()))
90+
}
7991
}
8092
}
8193

@@ -86,12 +98,12 @@ impl Ecc {
8698
let mut new_bytes = BytesMut::with_capacity(4);
8799
match slot & 1 == 0 {
88100
true => {
89-
new_bytes.put_u16(config.into());
101+
new_bytes.put_u16_le(config.into());
90102
new_bytes.extend_from_slice(s1);
91103
}
92104
false => {
93105
new_bytes.extend_from_slice(s0);
94-
new_bytes.put_u16(config.into());
106+
new_bytes.put_u16_le(config.into());
95107
}
96108
}
97109
self.write(&slot_address, &new_bytes.freeze())
@@ -101,8 +113,8 @@ impl Ecc {
101113
let bytes = self.read(false, &Address::config(2, 5)?)?;
102114
let (_, s1) = bytes.split_at(2);
103115
match zone {
104-
Zone::Config => Ok(s1[1] == 0),
105-
Zone::Data => Ok(s1[0] == 0),
116+
Zone::Config => Ok(s1[1] != 0x55),
117+
Zone::Data => Ok(s1[0] != 0x55),
106118
}
107119
}
108120

src/key_config.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ impl From<KeyConfigType> for u8 {
2727
}
2828
}
2929

30+
// Should probably be removed as it should manage bus endianness while
31+
// this responsability should not be there
3032
impl From<&[u8]> for KeyConfig {
3133
fn from(v: &[u8]) -> Self {
3234
let mut buf = v;
@@ -38,17 +40,15 @@ bitfield! {
3840
#[derive(PartialEq)]
3941
pub struct KeyConfig(u16);
4042
impl Debug;
41-
42-
pub u8, auth_key, set_auth_key: 3, 0;
43-
pub intrusion_disable, set_intrusion_disable: 4;
44-
pub u8, x509_index, set_x509_index: 7, 6;
45-
46-
pub private, set_private: 8;
47-
pub pub_info, set_pub_info: 9;
48-
pub u8, from into KeyConfigType, key_type, set_key_type: 12, 10;
49-
pub lockable, set_is_lockable: 13;
50-
pub req_random, set_req_random: 14;
51-
pub req_auth, set_req_auth: 15;
43+
pub u8, x509_index, set_x509_index: 15,14;
44+
pub intrusion_disable, set_intrusion_disable: 12;
45+
pub u8, auth_key, set_auth_key:11, 8;
46+
pub req_auth, set_req_auth: 7;
47+
pub req_random, set_req_random: 6;
48+
pub lockable, set_is_lockable: 5;
49+
pub u8, from into KeyConfigType, key_type, set_key_type: 4, 2;
50+
pub pub_info, set_pub_info: 1;
51+
pub private, set_private: 0;
5252
}
5353

5454
impl From<u16> for KeyConfig {

src/slot_config.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,17 @@ bitfield! {
223223
#[derive(PartialEq)]
224224
pub struct SlotConfig(u16);
225225
impl Debug;
226-
pub secret, set_secret: 15;
227-
pub encrypt_read, set_encrypt_read: 14;
228-
pub limited_use, set_limited_use: 13;
229-
pub no_mac, set_no_mac: 12;
230-
pub u8, from into ReadKey, read_key, set_read_key: 11, 8;
231-
u8, _write_config, _set_write_config: 7, 4;
232-
pub u8, write_key, set_write_key: 3, 0;
226+
u8, _write_config, _set_write_config: 15, 12;
227+
pub u8, write_key, set_write_key: 11, 8;
228+
pub secret, set_secret: 7;
229+
pub encrypt_read, set_encrypt_read: 6;
230+
pub limited_use, set_limited_use: 5;
231+
pub no_mac, set_no_mac: 4;
232+
pub u8, from into ReadKey, read_key, set_read_key: 3, 0;
233233
}
234234

235+
// Should probably be removed as it should manage bus endianness while
236+
// this responsability should not be there
235237
impl From<&[u8]> for SlotConfig {
236238
fn from(v: &[u8]) -> Self {
237239
let mut buf = v;

0 commit comments

Comments
 (0)