cryptoauthlib-sys 0.2.2

Automatically generated Rust bindings for CryptoAuthentication Library calls.
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
 * \file
 *
 * \brief Provides APIs to use with ATSHA206A device.
 *
 * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries.
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip software
 * and any derivatives exclusively with Microchip products. It is your
 * responsibility to comply with third party license terms applicable to your
 * use of third party software (including open source software) that may
 * accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
 * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT,
 * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE
 * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
 * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
 * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL
 * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED
 * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR
 * THIS SOFTWARE.
 */
#include <stdlib.h>
#include <stdio.h>
#include "cryptoauthlib.h"
#include "api_206a.h"

/** \brief Computes the diversified key based on the parent key provided and device serial number
 *  \param[in]  parent_key      parent key to be diversified
 *  \param[out] diversified_key diversified parent key
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_diversify_parent_key(uint8_t* parent_key, uint8_t* diversified_key)
{
    ATCA_STATUS status;
    uint8_t sha_data[96];
    uint8_t* p_temp = sha_data;
    uint8_t param1 = 0x0;      //These are fixed values.. Should match with value used in provisioning
    uint16_t param2 = 0x0;
    uint8_t sn[9];

    if ((parent_key == NULL) || (diversified_key == NULL))
    {
        return ATCA_BAD_PARAM;
    }

    do
    {
        if ((status = atcab_read_serial_number(sn)) != ATCA_SUCCESS)
        {
            break;
        }

        memcpy(p_temp, parent_key, ATCA_KEY_SIZE);
        p_temp += ATCA_KEY_SIZE;

        *p_temp++ = ATCA_DERIVE_KEY;
        *p_temp++ = param1;
        *p_temp++ = param2 & 0xFF;
        *p_temp++ = (param2 >> 8) & 0xFF;
        *p_temp++ = sn[8];
        *p_temp++ = sn[0];
        *p_temp++ = sn[1];
        memset(p_temp, 0, 25);
        p_temp += 25;
        memcpy(p_temp, sn, sizeof(sn));
        p_temp += sizeof(sn);
        memset(p_temp, 0, 23);

        if ((status = atcac_sw_sha2_256(sha_data, sizeof(sha_data), diversified_key)) != ATCA_SUCCESS)
        {
            break;
        }
    }
    while (0);

    return status;
}
/** \brief Generates the derived key based on the parent key and other parameters provided
 *  \param[in]  parent_key      Input data contains device's parent key
 *  \param[out] derived_key     Output data derived from parent key
 *  \param[in]  param1          Input data to be used in derive key calculation
 *  \param[in]  param2          Input data to be used in derive key calculation
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_generate_derive_key(uint8_t* parent_key, uint8_t* derived_key, uint8_t param1, uint16_t param2)
{
    ATCA_STATUS status;
    uint8_t sha_data[96];
    uint8_t* p_temp = sha_data;
    uint8_t sn[9];

    if ((parent_key == NULL) || (derived_key == NULL) || (param1 == 0xFF))
    {
        return ATCA_BAD_PARAM;
    }

    do
    {
        if ((status = atcab_derivekey(param1, param2, NULL)) != ATCA_SUCCESS)
        {
            break;
        }

        if ((status = atcab_read_serial_number(sn)) != ATCA_SUCCESS)
        {
            break;
        }

        memcpy(p_temp, parent_key, ATCA_KEY_SIZE);
        p_temp += ATCA_KEY_SIZE;
        *p_temp++ = ATCA_DERIVE_KEY;
        *p_temp++ = param1;
        *p_temp++ = param2 & 0xFF;
        *p_temp++ = (param2 >> 8) & 0xFF;
        *p_temp++ = sn[8];
        *p_temp++ = sn[0];
        *p_temp++ = sn[1];
        memset(p_temp, 0, (25 + 32));

        if ((status = atcac_sw_sha2_256(sha_data, sizeof(sha_data), derived_key)) != ATCA_SUCCESS)
        {
            break;
        }
    }
    while (0);

    return status;
}
/** \brief Generates the response based on Key and Challenge provided
 *  \param[in]  key         Input data contains device's key
 *  \param[in]  challenge   Input data to be used in challenge response calculation
 *  \param[out] response    response derived from key and challenge
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_generate_challenge_response_pair(uint8_t* key, uint8_t* challenge, uint8_t* response)
{
    ATCA_STATUS status;
    uint8_t sha_data[88];
    uint8_t* p_temp = sha_data;
    uint8_t param1 = 0x0; //Should use the same value while authenticating with device
    uint16_t param2 = 0x0;
    uint8_t sn[9];

    if ((key == NULL) || (challenge == NULL) || (response == NULL))
    {
        return ATCA_BAD_PARAM;
    }

    do
    {
        if ((status = atcab_read_serial_number(sn)) != ATCA_SUCCESS)
        {
            break;
        }

        memcpy(p_temp, key, ATCA_KEY_SIZE);
        p_temp += ATCA_KEY_SIZE;
        memcpy(p_temp, challenge, ATCA_KEY_SIZE);
        p_temp += ATCA_KEY_SIZE;
        *p_temp++ = ATCA_MAC;
        *p_temp++ = param1;
        *p_temp++ = param2 & 0xFF;
        *p_temp++ = (param2 >> 8) & 0xFF;
        memset(p_temp, 0, (8 + 3));
        p_temp += (8 + 3);
        *p_temp++ = sn[8];
        memset(p_temp, 0, 4);
        p_temp += 4;
        *p_temp++ = sn[0];
        *p_temp++ = sn[1];
        memset(p_temp, 0, 2);

        if ((status = atcac_sw_sha2_256(sha_data, sizeof(sha_data), response)) != ATCA_SUCCESS)
        {
            break;
        }

    }
    while (0);

    return status;
}
/** \brief verifies the challenge and provided response using key in device
 *  \param[in]  challenge           Challenge to be used in the response calculations
 *  \param[in]  expected_response   Expected response from the device.
 *  \param[out] is_authenticated    result of expected of response and calcualted response
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_authenticate(uint8_t* challenge, uint8_t* expected_response, uint8_t* is_authenticated)
{
    ATCA_STATUS status;
    uint8_t is_consumed;
    uint8_t digest[ATCA_KEY_SIZE];

    if ((challenge == NULL) || (expected_response == NULL) || (is_authenticated == NULL))
    {
        return ATCA_BAD_PARAM;
    }

    do
    {
        if ((status = sha206a_verify_device_consumption(&is_consumed)) != ATCA_SUCCESS)
        {
            break;
        }

        if ((is_consumed & ATCA_SHA206A_PKEY_CONSUMPTION_MASK) == ATCA_SHA206A_PKEY_CONSUMPTION_MASK)
        {
            status = ATCA_USE_FLAGS_CONSUMED;
            break;
        }
        else if ((is_consumed & ATCA_SHA206A_DKEY_CONSUMPTION_MASK) == ATCA_SHA206A_DKEY_CONSUMPTION_MASK)
        {
            if ((status = atcab_derivekey(0xFF, 0x0, NULL)) != ATCA_SUCCESS)
            {
                break;
            }
        }

        if ((status = atcab_mac(0x00, ATCA_SHA206A_SYMMETRIC_KEY_ID_SLOT, challenge, digest)) != ATCA_SUCCESS)
        {
            break;
        }
    }
    while (0);

    *is_authenticated = false;
    if ((status == ATCA_SUCCESS) && (memcmp(digest, expected_response, ATCA_KEY_SIZE) == 0))
    {
        *is_authenticated = true;
    }

    return status;
}
/** \brief verifies the device is fully consumed or not based on Parent and Derived Key use flags.
 *  \param[out] is_consumed    result of device consumption
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_verify_device_consumption(uint8_t* is_consumed)
{
    ATCA_STATUS status;
    uint8_t is_dk_valid;
    uint8_t is_pk_valid = false;

    if (is_consumed == NULL)
    {
        return ATCA_BAD_PARAM;
    }

    *is_consumed = ATCA_SHA206A_DKEY_CONSUMPTION_MASK | ATCA_SHA206A_PKEY_CONSUMPTION_MASK;
    do
    {
        if ((status = sha206a_check_dk_useflag_validity(&is_dk_valid)) != ATCA_SUCCESS)
        {
            break;
        }

        if (is_dk_valid)
        {
            *is_consumed = 0;
        }
        else
        {
            if ((status = sha206a_check_pk_useflag_validity(&is_pk_valid)) != ATCA_SUCCESS)
            {
                break;
            }

            if (is_pk_valid)
            {
                *is_consumed &= ~ATCA_SHA206A_PKEY_CONSUMPTION_MASK;
            }
        }
    }
    while (0);

    return status;
}
/** \brief verifies Derived Key use flags for consumption
 *  \param[out] is_consumed    indicates if DK is available for consumption.
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_check_dk_useflag_validity(uint8_t* is_consumed)
{
    ATCA_STATUS status;
    uint8_t dk_available_count;

    if (is_consumed == NULL)
    {
        return ATCA_BAD_PARAM;
    }

    *is_consumed = true;
    if ((status = sha206a_get_dk_useflag_count(&dk_available_count)) != ATCA_SUCCESS)
    {
        return status;
    }

    if (!dk_available_count)
    {
        *is_consumed = false;
    }

    return status;
}
/** \brief verifies Parent Key use flags for consumption
 *  \param[out] is_consumed    indicates if PK is available for consumption
 *  \return ATCA_SUCCESS on success, otherwise an error code
 */
ATCA_STATUS sha206a_check_pk_useflag_validity(uint8_t* is_consumed)
{
    ATCA_STATUS status;
    uint8_t pk_available_count;

    if (is_consumed == NULL)
    {
        return ATCA_BAD_PARAM;
    }

    *is_consumed = true;
    if ((status = sha206a_get_pk_useflag_count(&pk_available_count)) != ATCA_SUCCESS)
    {
        return status;
    }

    if (!pk_available_count)
    {
        *is_consumed = false;
    }

    return status;
}
/** \brief calculates available Derived Key use counts
 *  \param[out] dk_available_count    counts available bit's as 1
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_get_dk_useflag_count(uint8_t* dk_available_count)
{
    ATCA_STATUS status;
    uint8_t read_buf[4];
    uint8_t bit_count;

    if (dk_available_count == NULL)
    {
        return ATCA_BAD_PARAM;
    }

    *dk_available_count = 0;
    if ((status = atcab_read_bytes_zone(ATCA_ZONE_CONFIG, 0, 64, read_buf, sizeof(read_buf))) != ATCA_SUCCESS)
    {
        return status;
    }

    for (bit_count = 0; bit_count < 8; bit_count++)
    {
        if (!(read_buf[2] & 0x01))
        {
            break;
        }

        read_buf[2] >>= 1;
    }

    *dk_available_count = bit_count;

    return status;
}
/** \brief calculates available Parent Key use counts
 *  \param[out] pk_available_count    counts available bit's as 1
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_get_pk_useflag_count(uint8_t* pk_available_count)
{
    ATCA_STATUS status;
    uint8_t read_buf[16];
    uint8_t byte_count;
    uint8_t bit_count;

    if (pk_available_count == NULL)
    {
        return ATCA_BAD_PARAM;
    }

    *pk_available_count = 0;
    if ((status = atcab_read_bytes_zone(ATCA_ZONE_CONFIG, 0, 68, read_buf, sizeof(read_buf))) != ATCA_SUCCESS)
    {
        return status;
    }

    for (bit_count = 0, byte_count = 16; bit_count < sizeof(read_buf) * 8; bit_count++)
    {
        if (bit_count % 8 == 0)
        {
            byte_count--;
        }

        if (!(read_buf[byte_count] & 0x01))
        {
            break;
        }

        read_buf[byte_count] >>= 1;
    }

    *pk_available_count = bit_count;

    return status;
}
/** \brief Read Derived Key slot update count. It will be wraps around 256
 *  \param[out] dk_update_count    returns number of times the slot has been updated with derived key
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_get_dk_update_count(uint8_t* dk_update_count)
{
    ATCA_STATUS status;
    uint8_t read_buf[4];

    if (dk_update_count == NULL)
    {
        return ATCA_BAD_PARAM;
    }

    *dk_update_count = 0;
    if ((status = atcab_read_bytes_zone(ATCA_ZONE_CONFIG, 0, 64, read_buf, sizeof(read_buf))) != ATCA_SUCCESS)
    {
        return status;
    }

    *dk_update_count = read_buf[3];

    return status;
}

/** \brief Update the data store slot with user data and lock it if necessary
 *  \param[in] slot Slot number to be written with data
 *  \param[in] data Pointer that holds the data
 *  \param[in] block   32-byte block to write to.
 *  \param[in] offset  4-byte word within the specified block to write to. If
 *                     performing a 32-byte write, this should be 0.
 *  \param[in] len data length
 *  \param[in] lock_after_write set 1 to lock slot after write, otherwise 0
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_write_data_store(uint8_t slot, uint8_t* data, uint8_t block, uint8_t offset, uint8_t len, bool lock_after_write)
{
    ATCA_STATUS status;
    uint8_t zone = ATCA_ZONE_DATA;

    if ((slot < SHA206A_DATA_STORE0) || (slot > SHA206A_DATA_STORE2) ||
        (slot == SHA206A_DATA_STORE0 && lock_after_write == true) || (data == NULL))
    {
        return ATCA_BAD_PARAM;
    }

    if (lock_after_write)
    {
        zone |= ATCA_SHA206A_ZONE_WRITE_LOCK;
    }

    status = atcab_write_zone(zone, slot, block, offset, data, len);

    return status;
}
/** \brief Read the data stored in Data store
 *  \param[in] slot Slot number to read from
 *  \param[in] data Pointer to hold slot data data
 *  \param[in]  offset  Byte offset within the zone to read from.
 *  \param[in] len data length
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_read_data_store(uint8_t slot, uint8_t* data, uint8_t offset, uint8_t len)
{
    ATCA_STATUS status;
    uint8_t zone = ATCA_ZONE_DATA;

    if ((slot < SHA206A_DATA_STORE0) || (slot > SHA206A_DATA_STORE2) || (data == NULL))
    {
        return ATCA_BAD_PARAM;
    }

    status = atcab_read_bytes_zone(zone, slot, offset, data, len);

    return status;
}
/** \brief Returns the lock status of the given data store
 *  \param[in] slot Slot number of the data store
 *  \param[out] is_locked lock status of the data store
 *  \return ATCA_SUCCESS on success, otherwise an error code.
 */
ATCA_STATUS sha206a_get_data_store_lock_status(uint8_t slot, uint8_t* is_locked)
{
    ATCA_STATUS status;
    uint16_t read_buf[2];

    if ((is_locked == NULL) || (slot < SHA206A_DATA_STORE1) || (slot > SHA206A_DATA_STORE2))
    {
        return ATCA_BAD_PARAM;
    }

    *is_locked = true;

    if ((status = atcab_read_bytes_zone(ATCA_ZONE_CONFIG, 0, 4, read_buf, sizeof(read_buf))) != ATCA_SUCCESS)
    {
        return status;
    }

    if (slot == SHA206A_DATA_STORE1)
    {
        *is_locked = read_buf[0] == 0x5555 ? false : true;
    }
    else
    {
        *is_locked = read_buf[1] == 0x5555 ? false : true;
    }

    return status;
}