1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::constants::ATCA_CMD_SIZE_MAX;
use crate::transport::TransportProtocol;
use crate::{
command::{EccCommand, EccResponse},
Address, DataBuffer, Error, KeyConfig, Result, SlotConfig, Zone,
};
use bytes::{BufMut, Bytes, BytesMut};
use sha2::{Digest, Sha256};
pub use crate::command::KeyType;
pub struct Ecc {
transport: TransportProtocol,
}
pub const MAX_SLOT: u8 = 15;
pub(crate) const CMD_RETRIES: u8 = 10;
impl Ecc {
pub fn from_path(path: &str, address: u16) -> Result<Self> {
let transport = TransportProtocol::from_path(path, address)?;
Ok(Self { transport })
}
pub fn get_info(&mut self) -> Result<Bytes> {
self.send_command(&EccCommand::info())
}
pub fn get_serial(&mut self) -> Result<Bytes> {
let bytes = self.read(true, &Address::config(0, 0)?)?;
let mut result = BytesMut::with_capacity(9);
result.extend_from_slice(&bytes.slice(0..=3));
result.extend_from_slice(&bytes.slice(8..=12));
Ok(result.freeze())
}
pub fn genkey(&mut self, key_type: KeyType, slot: u8) -> Result<Bytes> {
self.send_command(&EccCommand::genkey(key_type, slot))
}
pub fn get_slot_config(&mut self, slot: u8) -> Result<SlotConfig> {
let bytes = self.read(false, &Address::slot_config(slot)?)?;
let (s0, s1) = bytes.split_at(2);
match slot & 1 == 0 {
true => Ok(SlotConfig::from(s0)),
false => Ok(SlotConfig::from(s1)),
}
}
pub fn set_slot_config(&mut self, slot: u8, config: &SlotConfig) -> Result {
let slot_address = Address::slot_config(slot)?;
let bytes = self.read(false, &slot_address)?;
let (s0, s1) = bytes.split_at(2);
let mut new_bytes = BytesMut::with_capacity(4);
match slot & 1 == 0 {
true => {
new_bytes.put_u16(config.into());
new_bytes.extend_from_slice(s1);
}
false => {
new_bytes.extend_from_slice(s0);
new_bytes.put_u16(config.into());
}
}
self.write(&slot_address, &new_bytes.freeze())
}
pub fn get_key_config(&mut self, slot: u8) -> Result<KeyConfig> {
let bytes = self.read(false, &Address::key_config(slot)?)?;
let (s0, s1) = bytes.split_at(2);
match slot & 1 == 0 {
true => Ok(KeyConfig::from(s0)),
false => Ok(KeyConfig::from(s1)),
}
}
pub fn set_key_config(&mut self, slot: u8, config: &KeyConfig) -> Result {
let slot_address = Address::key_config(slot)?;
let bytes = self.read(false, &slot_address)?;
let (s0, s1) = bytes.split_at(2);
let mut new_bytes = BytesMut::with_capacity(4);
match slot & 1 == 0 {
true => {
new_bytes.put_u16(config.into());
new_bytes.extend_from_slice(s1);
}
false => {
new_bytes.extend_from_slice(s0);
new_bytes.put_u16(config.into());
}
}
self.write(&slot_address, &new_bytes.freeze())
}
pub fn get_locked(&mut self, zone: &Zone) -> Result<bool> {
let bytes = self.read(false, &Address::config(2, 5)?)?;
let (_, s1) = bytes.split_at(2);
match zone {
Zone::Config => Ok(s1[1] == 0),
Zone::Data => Ok(s1[0] == 0),
}
}
pub fn set_locked(&mut self, zone: Zone) -> Result {
self.send_command(&EccCommand::lock(zone)).map(|_| ())
}
pub fn sign(&mut self, key_slot: u8, data: &[u8]) -> Result<Bytes> {
let digest = Sha256::digest(data);
let _ = self.send_command_retries(
&EccCommand::nonce(DataBuffer::MessageDigest, Bytes::copy_from_slice(&digest)),
false,
1,
)?;
self.send_command_retries(
&EccCommand::sign(DataBuffer::MessageDigest, key_slot),
true,
1,
)
}
pub fn ecdh(&mut self, key_slot: u8, x: &[u8], y: &[u8]) -> Result<Bytes> {
self.send_command(&EccCommand::ecdh(
Bytes::copy_from_slice(x),
Bytes::copy_from_slice(y),
key_slot,
))
}
pub fn random(&mut self) -> Result<Bytes> {
self.send_command(&EccCommand::random())
}
pub fn nonce(&mut self, target: DataBuffer, data: &[u8]) -> Result {
self.send_command(&EccCommand::nonce(target, Bytes::copy_from_slice(data)))
.map(|_| ())
}
pub fn read(&mut self, read_32: bool, address: &Address) -> Result<Bytes> {
self.send_command(&EccCommand::read(read_32, address.clone()))
}
pub fn write(&mut self, address: &Address, bytes: &[u8]) -> Result {
self.send_command(&EccCommand::write(address.clone(), bytes))
.map(|_| ())
}
pub(crate) fn send_command(&mut self, command: &EccCommand) -> Result<Bytes> {
self.send_command_retries(command, true, CMD_RETRIES)
}
pub(crate) fn send_command_retries(
&mut self,
command: &EccCommand,
sleep: bool,
retries: u8,
) -> Result<Bytes> {
let mut buf = BytesMut::with_capacity(ATCA_CMD_SIZE_MAX as usize);
for retry in 0..retries {
buf.clear();
buf.put_u8(self.transport.put_command_flag());
command.bytes_into(&mut buf);
self.transport.send_wake()?;
let delay = self.transport.command_duration(command);
if let Err(_err) = self.transport.send_recv_buf(delay, &mut buf) {
if retry == retries {
break;
} else {
continue;
}
}
let response = EccResponse::from_bytes(&buf[..])?;
if sleep {
self.transport.send_sleep();
}
match response {
EccResponse::Data(bytes) => return Ok(bytes),
EccResponse::Error(err) if err.is_recoverable() && retry < retries => {
continue;
}
EccResponse::Error(err) => return Err(Error::ecc(err)),
}
}
Err(Error::timeout())
}
}