rust-cryptoauthlib 0.4.5

Rust wrappers for CryptoAuthentication Library bindings.
Documentation
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use std::cmp::min;
use std::mem::MaybeUninit;

use super::{AtcaDeviceType, AtcaStatus, AteccDevice, KeyType, MacParam, NonceTarget};

use super::{
    ATCA_AES_DATA_SIZE, ATCA_AES_KEY_SIZE, ATCA_ATECC_SLOTS_COUNT, ATCA_ATECC_TEMPKEY_KEYID,
    ATCA_NONCE_SIZE, ATCA_SHA2_256_DIGEST_SIZE, SHA_MODE_TARGET_TEMPKEY,
};

use cryptoauthlib_sys::atca_aes_cmac_ctx_t;

impl AteccDevice {
    /// function that calculates the MAC code of the HMAC-SHA256 type
    pub(crate) fn compute_mac_hmac_sha256(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
    ) -> Result<Vec<u8>, AtcaStatus> {
        let mut mac_length: u8 = ATCA_SHA2_256_DIGEST_SIZE as u8;
        if let Some(val) = &mac_param.mac_length {
            mac_length = *val
        };

        let mac = self.common_mac_hmac_sha256(mac_param, slot_id, data, mac_length as usize)?;

        Ok(mac)
    } // AteccDevice::compute_mac_hmac_sha256()

    /// function that verifies the MAC code of the HMAC-SHA256 type
    pub(crate) fn verify_mac_hmac_sha256(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
    ) -> Result<bool, AtcaStatus> {
        let mac_to_check: Vec<u8>;

        if let Some(val) = mac_param.mac.clone() {
            mac_to_check = val;
        } else {
            return Err(AtcaStatus::AtcaBadParam);
        }

        let mac = self.common_mac_hmac_sha256(mac_param, slot_id, data, mac_to_check.len())?;

        Ok(mac == mac_to_check)
    } // AteccDevice::verify_mac_hmac_sha256()

    /// a helper function implementing common functionality for HMAC-SHA256
    fn common_mac_hmac_sha256(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
        mac_length: usize,
    ) -> Result<Vec<u8>, AtcaStatus> {
        let result = self.common_mac_hmac(mac_param, slot_id);
        if result != AtcaStatus::AtcaSuccess {
            return Err(result);
        }
        let mut slot = slot_id as u16;
        if slot_id == ATCA_ATECC_SLOTS_COUNT {
            slot = ATCA_ATECC_TEMPKEY_KEYID;
        }

        let mut mac: [u8; ATCA_SHA2_256_DIGEST_SIZE] = [0x00; ATCA_SHA2_256_DIGEST_SIZE];

        let result = AtcaStatus::from(unsafe {
            let _guard = self
                .api_mutex
                .lock()
                .expect("Could not lock atcab API mutex");
            cryptoauthlib_sys::atcab_sha_hmac(
                data.as_ptr(),
                data.len() as u64,
                slot,
                mac.as_mut_ptr(),
                SHA_MODE_TARGET_TEMPKEY,
            )
        });

        match result {
            AtcaStatus::AtcaSuccess => Ok({
                let mut out_mac: Vec<u8> = vec![0x00; mac_length as usize];
                out_mac.copy_from_slice(&mac[..mac_length as usize]);
                out_mac
            }),
            _ => Err(result),
        }
    } // AteccDevice::common_mac_hmac_sha256()

    /// function that calculates the MAC code of the CMAC type
    pub(crate) fn compute_mac_cmac(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
    ) -> Result<Vec<u8>, AtcaStatus> {
        let mut mac_length: u8 = ATCA_AES_DATA_SIZE as u8;
        if let Some(val) = &mac_param.mac_length {
            mac_length = *val
        };

        let mac = self.common_mac_cmac(mac_param, slot_id, data, mac_length as usize)?;

        Ok(mac)
    } // AteccDevice::compute_mac_cmac()

    /// function that verifies the MAC code of the CMAC type
    pub(crate) fn verify_mac_cmac(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
    ) -> Result<bool, AtcaStatus> {
        let mac_to_check: Vec<u8>;

        if let Some(val) = mac_param.mac.clone() {
            mac_to_check = val;
        } else {
            return Err(AtcaStatus::AtcaBadParam);
        }

        let mac = self.common_mac_cmac(mac_param, slot_id, data, mac_to_check.len())?;

        Ok(mac == mac_to_check)
    } // AteccDevice::verify_mac_cmac()

    /// a helper function implementing common functionality for CMAC
    fn common_mac_cmac(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
        mac_length: usize,
    ) -> Result<Vec<u8>, AtcaStatus> {
        let result = self.common_mac(mac_param, slot_id);
        if result != AtcaStatus::AtcaSuccess {
            return Err(result);
        }

        let mut ctx = self.aes_cmac_init(slot_id)?;

        let mut start_pos: usize = 0;
        let mut shift: usize = min(data.len(), ATCA_AES_DATA_SIZE);
        while shift > 0 {
            let block = &data[start_pos..(start_pos + shift)];
            ctx = self.aes_cmac_update(ctx, block)?;
            start_pos += shift;
            let remaining_bytes = data.len() - start_pos;
            if remaining_bytes < ATCA_AES_DATA_SIZE {
                shift = remaining_bytes
            }
        }

        let mac = self.aes_cmac_finish(ctx, mac_length as u8)?;
        Ok(mac)
    } // AteccDevice::common_mac_cmac()

    /// function that calculates the MAC code of the CBC-MAC type
    /// read this: https://blog.cryptographyengineering.com/2013/02/15/why-i-hate-cbc-mac/ and don't use this mode directly
    pub(crate) fn compute_mac_cbcmac(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
    ) -> Result<Vec<u8>, AtcaStatus> {
        let mut mac_length: u8 = ATCA_AES_DATA_SIZE as u8;
        if let Some(val) = &mac_param.mac_length {
            mac_length = *val
        };

        let mac = self.common_mac_cbcmac(mac_param, slot_id, data, mac_length as usize)?;

        Ok(mac)
    } // AteccDevice::compute_mac_cbcmac()

    /// function that verifies the MAC code of the CBC-MAC type
    /// read this: https://blog.cryptographyengineering.com/2013/02/15/why-i-hate-cbc-mac/ and don't use this mode directly
    pub(crate) fn verify_mac_cbcmac(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
    ) -> Result<bool, AtcaStatus> {
        let mac_to_check: Vec<u8>;

        if let Some(val) = mac_param.mac.clone() {
            mac_to_check = val;
        } else {
            return Err(AtcaStatus::AtcaBadParam);
        }

        let mac = self.common_mac_cbcmac(mac_param, slot_id, data, mac_to_check.len())?;

        Ok(mac == mac_to_check)
    } // AteccDevice::verify_mac_cbcmac()

    /// a helper function implementing common functionality for CBC-MAC
    fn common_mac_cbcmac(
        &self,
        mac_param: MacParam,
        slot_id: u8,
        data: &[u8],
        mac_length: usize,
    ) -> Result<Vec<u8>, AtcaStatus> {
        let result = self.common_mac(mac_param, slot_id);
        if result != AtcaStatus::AtcaSuccess {
            return Err(result);
        }

        let mut ctx = self.aes_cbcmac_init(slot_id);

        let mut start_pos: usize = 0;
        let mut shift: usize = min(data.len(), ATCA_AES_DATA_SIZE);
        while shift > 0 {
            let block = &data[start_pos..(start_pos + shift)];
            ctx = self.aes_cbcmac_update(ctx, block)?;
            start_pos += shift;
            let remaining_bytes = data.len() - start_pos;
            if remaining_bytes < ATCA_AES_DATA_SIZE {
                shift = remaining_bytes
            }
        }

        let mac = self.aes_cbcmac_finish(ctx, mac_length)?;
        Ok(mac)
    } // AteccDevice::common_mac_cbcmac()

    /// Initialize a CMAC calculation using an AES-128 key in the device
    fn aes_cmac_init(&self, slot_id: u8) -> Result<atca_aes_cmac_ctx_t, AtcaStatus> {
        const BLOCK_IDX: u8 = 0;

        let mut slot = slot_id as u16;
        if slot_id == ATCA_ATECC_SLOTS_COUNT {
            slot = ATCA_ATECC_TEMPKEY_KEYID;
        }

        let ctx: atca_aes_cmac_ctx_t = {
            let ctx = MaybeUninit::<atca_aes_cmac_ctx_t>::zeroed();
            unsafe { ctx.assume_init() }
        };
        let ctx_ptr = Box::into_raw(Box::new(ctx));

        let result = AtcaStatus::from(unsafe {
            let _guard = self
                .api_mutex
                .lock()
                .expect("Could not lock atcab API mutex");
            cryptoauthlib_sys::atcab_aes_cmac_init(ctx_ptr, slot, BLOCK_IDX)
        });

        match result {
            AtcaStatus::AtcaSuccess => Ok({
                let result = unsafe { *ctx_ptr };
                unsafe { Box::from_raw(ctx_ptr) };
                result
            }),
            _ => Err(result),
        }
    } // AteccDevice::aes_cmac_init()

    /// Add data to an initialized CMAC calculation
    fn aes_cmac_update(
        &self,
        ctx: atca_aes_cmac_ctx_t,
        data: &[u8],
    ) -> Result<atca_aes_cmac_ctx_t, AtcaStatus> {
        if data.len() > ATCA_AES_DATA_SIZE {
            return Err(AtcaStatus::AtcaInvalidSize);
        }

        let ctx_ptr = Box::into_raw(Box::new(ctx));

        let result = AtcaStatus::from(unsafe {
            let _guard = self
                .api_mutex
                .lock()
                .expect("Could not lock atcab API mutex");
            cryptoauthlib_sys::atcab_aes_cmac_update(ctx_ptr, data.as_ptr(), data.len() as u32)
        });

        let ctx = unsafe { *ctx_ptr };
        unsafe { Box::from_raw(ctx_ptr) };

        match result {
            AtcaStatus::AtcaSuccess => Ok(ctx),
            _ => Err(result),
        }
    } // AteccDevice::aes_cmac_update()

    /// Finish a CMAC operation returning the CMAC value
    fn aes_cmac_finish(
        &self,
        ctx: atca_aes_cmac_ctx_t,
        mac_length: u8,
    ) -> Result<Vec<u8>, AtcaStatus> {
        let ctx_ptr = Box::into_raw(Box::new(ctx));
        let mut mac: [u8; ATCA_AES_DATA_SIZE] = [0; ATCA_AES_DATA_SIZE];

        let result = AtcaStatus::from(unsafe {
            let _guard = self
                .api_mutex
                .lock()
                .expect("Could not lock atcab API mutex");
            cryptoauthlib_sys::atcab_aes_cmac_finish(ctx_ptr, mac.as_mut_ptr(), mac_length as u32)
        });

        unsafe { Box::from_raw(ctx_ptr) };

        match result {
            AtcaStatus::AtcaSuccess => Ok({
                let mut out_mac: Vec<u8> = vec![0x00; mac_length as usize];
                out_mac.copy_from_slice(&mac[..mac_length as usize]);
                out_mac
            }),
            _ => Err(result),
        }
    } // AteccDevice::aes_cmac_finish()

    /// Initialize context for AES CBC-MAC operation
    pub(crate) fn aes_cbcmac_init(&self, slot_id: u8) -> atca_aes_cmac_ctx_t {
        let mut slot = slot_id as u16;
        if slot_id == ATCA_ATECC_SLOTS_COUNT {
            slot = ATCA_ATECC_TEMPKEY_KEYID;
        }

        let mut ctx: atca_aes_cmac_ctx_t = {
            let ctx = MaybeUninit::<atca_aes_cmac_ctx_t>::zeroed();
            unsafe { ctx.assume_init() }
        };

        ctx.cbc_ctx.key_id = slot;
        ctx.cbc_ctx.key_block = 0x00;

        ctx
    } // AteccDevice::aes_cbcmac_init()

    /// Calculate AES CBC-MAC with key stored within ECC608A device.
    /// aes_cbcmac_init() should be called before the first use of this function.
    pub(crate) fn aes_cbcmac_update(
        &self,
        ctx: atca_aes_cmac_ctx_t,
        data: &[u8],
    ) -> Result<atca_aes_cmac_ctx_t, AtcaStatus> {
        if data.is_empty() {
            // Nothing to do
            return Ok(ctx);
        }

        // Process full blocks of data with AES-CBC
        let mut temp_ctx = ctx;
        let mut idx: usize = 0;
        let mut buffer: [u8; ATCA_AES_DATA_SIZE] = [0x00; ATCA_AES_DATA_SIZE];

        for i in 0..(data.len() / ATCA_AES_DATA_SIZE) {
            let start_pos = i * ATCA_AES_DATA_SIZE;
            let end_pos = start_pos + ATCA_AES_DATA_SIZE;
            idx += 1;

            temp_ctx.cbc_ctx = self.aes_cbc_encrypt_block(
                temp_ctx.cbc_ctx,
                &data[start_pos..end_pos],
                &mut buffer,
            )?;
        }

        // Store incomplete block to context structure
        let start_pos = idx * ATCA_AES_DATA_SIZE;
        match start_pos < data.len() {
            true => {
                temp_ctx.block_size = (data.len() - start_pos) as u32;
                temp_ctx.block[..(temp_ctx.block_size as usize)]
                    .copy_from_slice(&data[start_pos..(start_pos + temp_ctx.block_size as usize)]);
            }
            false => temp_ctx.block_size = 0,
        }

        Ok(temp_ctx)
    } // AteccDevice::aes_cbcmac_update()

    /// Finish a CBC-MAC operation returning the CBC-MAC value. If the data
    /// provided to the aes_cbcmac_update() function has incomplete
    /// block this function will return an error code
    pub(crate) fn aes_cbcmac_finish(
        &self,
        ctx: atca_aes_cmac_ctx_t,
        tag_size: usize,
    ) -> Result<Vec<u8>, AtcaStatus> {
        let mut tag: Vec<u8> = vec![0x00; ATCA_AES_DATA_SIZE];
        if tag_size > ATCA_AES_DATA_SIZE {
            return Err(AtcaStatus::AtcaBadParam);
        }

        // Check for incomplete data block
        if ctx.block_size != 0 {
            return Err(AtcaStatus::AtcaInvalidSize); // Returns INVALID_SIZE if incomplete blocks are present
        }

        // All processing is already done, copying the mac to result buffer
        tag[..tag_size].copy_from_slice(&ctx.cbc_ctx.ciphertext[..tag_size]);
        tag.resize(tag_size, 0x00);
        tag.shrink_to_fit();
        Ok(tag)
    } // AteccDevice::aes_cbcmac_finish()

    /// auxiliary function checking input parameters common to CMAC and CBC-MAC modes
    fn common_mac(&self, mac_param: MacParam, slot_id: u8) -> AtcaStatus {
        const MIN_MAC_SIZE: usize = 1;
        const MAX_MAC_SIZE: usize = ATCA_AES_DATA_SIZE;

        if (slot_id > ATCA_ATECC_SLOTS_COUNT)
            || ((slot_id < ATCA_ATECC_SLOTS_COUNT)
                && (self.slots[slot_id as usize].config.key_type != KeyType::Aes))
        {
            return AtcaStatus::AtcaInvalidId;
        }
        if (ATCA_ATECC_SLOTS_COUNT == slot_id) && mac_param.key.is_none()
            || (mac_param.mac_length.is_some() && mac_param.mac.is_some())
        {
            return AtcaStatus::AtcaBadParam;
        }
        if (mac_param.mac_length.is_some()
            && ((mac_param.mac_length < Some(MIN_MAC_SIZE as u8))
                || (mac_param.mac_length > Some(MAX_MAC_SIZE as u8))))
            || (mac_param.mac.is_some()
                && ((mac_param.mac.as_ref().unwrap().len() < MIN_MAC_SIZE)
                    || (mac_param.mac.as_ref().unwrap().len() > MAX_MAC_SIZE)))
            || (mac_param.key.is_some()
                && (mac_param.key.as_ref().unwrap().len() != ATCA_AES_KEY_SIZE))
        {
            return AtcaStatus::AtcaInvalidSize;
        }

        if let Some(mut key) = mac_param.key {
            key.resize(ATCA_NONCE_SIZE, 0x00);
            let result = self.nonce(NonceTarget::TempKey, &key);
            if AtcaStatus::AtcaSuccess != result {
                return result;
            }
        }

        AtcaStatus::AtcaSuccess
    }

    // auxiliary function checking input parameters for the HMAC-SHA256 mode
    fn common_mac_hmac(&self, mac_param: MacParam, slot_id: u8) -> AtcaStatus {
        const MIN_MAC_SIZE: usize = 1;
        const MAX_MAC_SIZE: usize = ATCA_SHA2_256_DIGEST_SIZE;

        if (slot_id > ATCA_ATECC_SLOTS_COUNT)
            || ((slot_id < ATCA_ATECC_SLOTS_COUNT)
                && (self.slots[slot_id as usize].config.key_type != KeyType::ShaOrText))
        {
            return AtcaStatus::AtcaInvalidId;
        }
        if (ATCA_ATECC_SLOTS_COUNT == slot_id)
            && (mac_param.key.is_none() || (self.get_device_type() != AtcaDeviceType::ATECC608A))
            || (mac_param.mac_length.is_some() && mac_param.mac.is_some())
        {
            return AtcaStatus::AtcaBadParam;
        }
        if (mac_param.mac_length.is_some()
            && ((mac_param.mac_length < Some(MIN_MAC_SIZE as u8))
                || (mac_param.mac_length > Some(MAX_MAC_SIZE as u8))))
            || (mac_param.mac.is_some()
                && ((mac_param.mac.as_ref().unwrap().len() < MIN_MAC_SIZE)
                    || (mac_param.mac.as_ref().unwrap().len() > MAX_MAC_SIZE)))
            || (mac_param.key.is_some()
                && (mac_param.key.as_ref().unwrap().len() > ATCA_SHA2_256_DIGEST_SIZE))
        {
            return AtcaStatus::AtcaInvalidSize;
        }

        if let Some(mut key) = mac_param.key {
            key.resize(ATCA_NONCE_SIZE, 0x00);
            let result = self.nonce(NonceTarget::TempKey, &key);
            if AtcaStatus::AtcaSuccess != result {
                return result;
            }
        }

        AtcaStatus::AtcaSuccess
    }
}