1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/init.h>
8 #include <zephyr/kernel.h>
9 #include <string.h>
10 #include <zephyr/device.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/sys/__assert.h>
13 #include <zephyr/crypto/crypto.h>
14
15 #define DT_DRV_COMPAT atmel_ataes132a
16
17 #include "crypto_ataes132a_priv.h"
18
19 #define D10D24S 11
20 #define MAX_RETRIES 3
21 #define ATAES132A_AES_KEY_SIZE 16
22
23 /* ATAES132A can store up to 16 different crypto keys */
24 #define CRYPTO_MAX_SESSION 16
25
26 #define LOG_LEVEL CONFIG_CRYPTO_LOG_LEVEL
27 #include <zephyr/logging/log.h>
28 LOG_MODULE_REGISTER(ataes132a);
29
30 static struct ataes132a_driver_state ataes132a_state[CRYPTO_MAX_SESSION];
31
ataes132a_init_states(void)32 static void ataes132a_init_states(void)
33 {
34 int i;
35
36 for (i = 0; i < ATAES132A_AES_KEY_SIZE; i++) {
37 ataes132a_state[i].in_use = false;
38 ataes132a_state[i].key_id = i;
39 }
40 }
41
ataes132a_send_command(const struct device * dev,uint8_t opcode,uint8_t mode,uint8_t * params,uint8_t nparams,uint8_t * response,uint8_t * nresponse)42 static int ataes132a_send_command(const struct device *dev, uint8_t opcode,
43 uint8_t mode, uint8_t *params,
44 uint8_t nparams, uint8_t *response,
45 uint8_t *nresponse)
46 {
47 int retry_count = 0;
48 struct ataes132a_device_data *data = dev->data;
49 const struct ataes132a_device_config *cfg = dev->config;
50 uint8_t count;
51 uint8_t status;
52 uint8_t crc[2];
53 int i, i2c_return;
54
55 count = nparams + 5;
56 if (count > 64) {
57 LOG_ERR("command too large for command buffer");
58 return -EDOM;
59 }
60
61 /* If there is a command in progress, idle wait until it is available.
62 * If there is concurrency protection around the driver, this should
63 * never happen.
64 */
65 read_reg_i2c(&cfg->i2c, ATAES_STATUS_REG, &status);
66
67 while (status & ATAES_STATUS_WIP) {
68 k_busy_wait(D10D24S);
69 read_reg_i2c(&cfg->i2c, ATAES_STATUS_REG, &status);
70 }
71
72 data->command_buffer[0] = count;
73 data->command_buffer[1] = opcode;
74 data->command_buffer[2] = mode;
75 for (i = 0; i < nparams; i++) {
76 data->command_buffer[i + 3] = params[i];
77 }
78
79 /*Calculate command CRC*/
80 ataes132a_atmel_crc(data->command_buffer, nparams + 3, crc);
81 data->command_buffer[nparams + 3] = crc[0];
82 data->command_buffer[nparams + 4] = crc[1];
83
84 /*Reset i/O address start before sending a command*/
85 write_reg_i2c(&cfg->i2c, ATAES_COMMAND_ADDRR_RESET, 0x0);
86
87 /*Send a command through the command buffer*/
88 i2c_return = burst_write_i2c(&cfg->i2c, ATAES_COMMAND_MEM_ADDR,
89 data->command_buffer, count);
90
91 LOG_DBG("BURST WRITE RETURN: %d", i2c_return);
92
93 /* Idle-waiting for the command completion*/
94 do {
95 k_busy_wait(D10D24S);
96 read_reg_i2c(&cfg->i2c, ATAES_STATUS_REG, &status);
97 } while (status & ATAES_STATUS_WIP);
98
99 if (status & ATAES_STATUS_CRC) {
100 LOG_ERR("incorrect CRC command");
101 return -EINVAL;
102 }
103
104 if (!(status & ATAES_STATUS_RDY)) {
105 LOG_ERR("expected response is not in place");
106 return -EINVAL;
107 }
108
109 /* Read the response */
110 burst_read_i2c(&cfg->i2c, ATAES_COMMAND_MEM_ADDR, data->command_buffer, 64);
111
112 count = data->command_buffer[0];
113
114 /* Calculate and validate response CRC */
115 ataes132a_atmel_crc(data->command_buffer, count - 2, crc);
116
117 LOG_DBG("COMMAND CRC %x%x", data->command_buffer[count - 2],
118 data->command_buffer[count - 1]);
119 LOG_DBG("CALCULATED CRC %x%x", crc[0], crc[1]);
120
121 /* If CRC fails retry reading MAX RETRIES times */
122 while (crc[0] != data->command_buffer[count - 2] ||
123 crc[1] != data->command_buffer[count - 1]) {
124 if (retry_count > MAX_RETRIES - 1) {
125 LOG_ERR("response crc validation rebase"
126 " max retries");
127 return -EINVAL;
128 }
129
130 burst_read_i2c(&cfg->i2c, ATAES_COMMAND_MEM_ADDR, data->command_buffer, 64);
131
132 count = data->command_buffer[0];
133
134 ataes132a_atmel_crc(data->command_buffer, count - 2, crc);
135 retry_count++;
136
137 LOG_DBG("COMMAND RETRY %d", retry_count);
138 LOG_DBG("COMMAND CRC %x%x",
139 data->command_buffer[count - 2],
140 data->command_buffer[count - 1]);
141 LOG_DBG("CALCULATED CRC %x%x", crc[0], crc[1]);
142 }
143
144 if ((status & ATAES_STATUS_ERR) || data->command_buffer[1] != 0x00) {
145 LOG_ERR("command execution error %x",
146 data->command_buffer[1]);
147 return -EIO;
148 }
149
150 LOG_DBG("Read the response count: %d", count);
151
152 for (i = 0; i < count - 3; i++) {
153 response[i] = data->command_buffer[i + 1];
154 }
155
156 *nresponse = count - 3;
157
158 return 0;
159 }
160
ataes132a_init(const struct device * dev)161 int ataes132a_init(const struct device *dev)
162 {
163 struct ataes132a_device_data *ataes132a = dev->data;
164 const struct ataes132a_device_config *cfg = dev->config;
165 uint32_t i2c_cfg;
166
167 LOG_DBG("ATAES132A INIT");
168
169 if (!device_is_ready(cfg->i2c.bus)) {
170 LOG_ERR("Bus device is not ready");
171 return -ENODEV;
172 }
173
174 i2c_cfg = I2C_MODE_CONTROLLER | I2C_SPEED_SET(ATAES132A_BUS_SPEED);
175
176 i2c_configure(cfg->i2c.bus, i2c_cfg);
177
178 k_sem_init(&ataes132a->device_sem, 1, K_SEM_MAX_LIMIT);
179
180 ataes132a_init_states();
181
182 return 0;
183 }
184
ataes132a_aes_ccm_decrypt(const struct device * dev,uint8_t key_id,struct ataes132a_mac_mode * mac_mode,struct ataes132a_mac_packet * mac_packet,struct cipher_aead_pkt * aead_op,uint8_t * nonce_buf)185 int ataes132a_aes_ccm_decrypt(const struct device *dev,
186 uint8_t key_id,
187 struct ataes132a_mac_mode *mac_mode,
188 struct ataes132a_mac_packet *mac_packet,
189 struct cipher_aead_pkt *aead_op,
190 uint8_t *nonce_buf)
191 {
192 uint8_t command_mode = 0x0;
193 struct ataes132a_device_data *data = dev->data;
194 uint8_t out_len;
195 uint8_t in_buf_len;
196 uint8_t return_code;
197 uint8_t expected_out_len;
198 uint8_t param_buffer[52];
199
200 if (!aead_op) {
201 LOG_ERR("Parameter cannot be null");
202 return -EINVAL;
203 }
204
205 if (!aead_op->pkt) {
206 LOG_ERR("Parameter cannot be null");
207 return -EINVAL;
208 }
209
210 in_buf_len = aead_op->pkt->in_len;
211 expected_out_len = aead_op->pkt->out_len;
212
213 /*The KeyConfig[EKeyID].ExternalCrypto bit must be 1b.*/
214 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_EXTERNAL)) {
215 LOG_ERR("key %x external mode disabled", key_id);
216 return -EINVAL;
217 }
218
219 if (in_buf_len != 16U && in_buf_len != 32U) {
220 LOG_ERR("ccm mode only accepts input blocks of 16"
221 " and 32 bytes");
222 return -EINVAL;
223 }
224
225 if (expected_out_len > 32) {
226 LOG_ERR("ccm mode cannot generate more than"
227 " 32 output bytes");
228 return -EINVAL;
229 }
230
231 /* If KeyConfig[key_id].AuthKey is set, then prior authentication
232 * is required
233 */
234 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_AUTHKEY)) {
235 LOG_DBG("keep in mind key %x will require"
236 " previous authentication", key_id);
237 }
238
239 if (!aead_op->pkt->in_buf || !aead_op->pkt->out_buf) {
240 return 0;
241 }
242
243 /* If the KeyConfig[EKeyID].RandomNonce bit is set
244 * the current nonce register content will be used.
245 * If there is an invalid random nonce or if there
246 * is no nonce synchronization between device
247 * the decrypt operation will fail accordingly.
248 */
249 if (ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_RAND_NONCE) {
250 LOG_DBG("key %x requires random nonce,"
251 " nonce_buf will be ignored", key_id);
252
253 LOG_DBG("current nonce register will be used");
254
255 }
256
257 k_sem_take(&data->device_sem, K_FOREVER);
258
259 /* If the KeyConfig[EKeyID].RandomNonce bit is not set
260 * then the nonce send as parameter will be loaded into
261 * the nonce register.
262 */
263 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_RAND_NONCE)
264 && nonce_buf) {
265 param_buffer[0] = 0x0;
266 param_buffer[1] = 0x0;
267 param_buffer[2] = 0x0;
268 param_buffer[3] = 0x0;
269 memcpy(param_buffer + 4, nonce_buf, 12);
270
271 return_code = ataes132a_send_command(dev, ATAES_NONCE_OP,
272 0x0, param_buffer, 16,
273 param_buffer, &out_len);
274
275 if (return_code != 0U) {
276 LOG_ERR("nonce command ended with code %d",
277 return_code);
278 k_sem_give(&data->device_sem);
279 return -EINVAL;
280 }
281
282 if (param_buffer[0] != 0U) {
283 LOG_ERR("nonce command failed with error"
284 " code %d", param_buffer[0]);
285 k_sem_give(&data->device_sem);
286 return -EIO;
287 }
288 }
289
290 /* If the KeyConfig[EKeyID].RandomNonce bit is not set
291 * and the nonce send as parameter is a null value,
292 * the command will use the current nonce register value.
293 */
294 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_RAND_NONCE)
295 && !nonce_buf) {
296 LOG_DBG("current nonce register will be used");
297 }
298
299 /* Client decryption mode requires a MAC packet to specify the
300 * encryption key id and the MAC count of the encryption device
301 * to synchronize MAC generation
302 */
303 if (mac_packet) {
304 param_buffer[0] = mac_packet->encryption_key_id;
305 param_buffer[2] = mac_packet->encryption_mac_count;
306 } else {
307 param_buffer[0] = 0x0;
308 param_buffer[2] = 0x0;
309 LOG_DBG("normal decryption mode"
310 " ignores mac_packet parameter");
311 }
312
313 /* Client decryption mode requires a MAC packet to specify
314 * if MAC counter, serial number and small zone number are
315 * included in MAC generation.
316 */
317 if (mac_mode) {
318 if (mac_mode->include_counter) {
319 LOG_DBG("including usage counter in the MAC: "
320 "decrypt and encrypt dev must be the same");
321 command_mode = command_mode | ATAES_MAC_MODE_COUNTER;
322 }
323
324 if (mac_mode->include_serial) {
325 LOG_DBG("including serial number in the MAC: "
326 "decrypt and encrypt dev must be the same");
327 command_mode = command_mode | ATAES_MAC_MODE_SERIAL;
328 }
329
330 if (mac_mode->include_smallzone) {
331 LOG_DBG("including small zone in the MAC: "
332 "decrypt and encrypt dev share the "
333 "first four bytes of their small zone");
334 command_mode = command_mode | ATAES_MAC_MODE_SMALLZONE;
335 }
336 }
337
338 param_buffer[1] = key_id;
339 param_buffer[3] = expected_out_len;
340 if (aead_op->tag) {
341 memcpy(param_buffer + 4, aead_op->tag, 16);
342 }
343 memcpy(param_buffer + 20, aead_op->pkt->in_buf, in_buf_len);
344
345 return_code = ataes132a_send_command(dev, ATAES_DECRYPT_OP,
346 command_mode, param_buffer,
347 in_buf_len + 4, param_buffer,
348 &out_len);
349
350 if (return_code != 0U) {
351 LOG_ERR("decrypt command ended with code %d", return_code);
352 k_sem_give(&data->device_sem);
353 return -EINVAL;
354 }
355
356 if (out_len < 2 || out_len > 33) {
357 LOG_ERR("decrypt command response has invalid"
358 " size %d", out_len);
359 k_sem_give(&data->device_sem);
360 return -EINVAL;
361 }
362
363 if (param_buffer[0] != 0U) {
364 LOG_ERR("legacy command failed with error"
365 " code %d", param_buffer[0]);
366 k_sem_give(&data->device_sem);
367 return -param_buffer[0];
368 }
369
370 if (expected_out_len != out_len - 1) {
371 LOG_ERR("decrypted output data size %d and expected data"
372 " size %d are different", out_len - 1,
373 expected_out_len);
374 k_sem_give(&data->device_sem);
375 return -EINVAL;
376 }
377
378 memcpy(aead_op->pkt->out_buf, param_buffer + 1, out_len - 1);
379
380 k_sem_give(&data->device_sem);
381
382 return 0;
383 }
384
ataes132a_aes_ccm_encrypt(const struct device * dev,uint8_t key_id,struct ataes132a_mac_mode * mac_mode,struct cipher_aead_pkt * aead_op,uint8_t * nonce_buf,uint8_t * mac_count)385 int ataes132a_aes_ccm_encrypt(const struct device *dev,
386 uint8_t key_id,
387 struct ataes132a_mac_mode *mac_mode,
388 struct cipher_aead_pkt *aead_op,
389 uint8_t *nonce_buf,
390 uint8_t *mac_count)
391 {
392 uint8_t command_mode = 0x0;
393 struct ataes132a_device_data *data = dev->data;
394 uint8_t buf_len;
395 uint8_t out_len;
396 uint8_t return_code;
397 uint8_t param_buffer[40];
398
399 if (!aead_op) {
400 LOG_ERR("Parameter cannot be null");
401 return -EINVAL;
402 }
403
404 if (!aead_op->pkt) {
405 LOG_ERR("Parameter cannot be null");
406 return -EINVAL;
407 }
408
409 buf_len = aead_op->pkt->in_len;
410
411 /*The KeyConfig[EKeyID].ExternalCrypto bit must be 1b.*/
412 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_EXTERNAL)) {
413 LOG_ERR("key %x external mode disabled", key_id);
414 return -EINVAL;
415 }
416
417 if (buf_len > 32) {
418 LOG_ERR("only up to 32 bytes accepted for ccm mode");
419 return -EINVAL;
420 }
421
422 /* If KeyConfig[key_id].AuthKey is set, then prior authentication
423 * is required
424 */
425 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_AUTHKEY)) {
426 LOG_DBG("keep in mind key %x will require"
427 " previous authentication", key_id);
428 }
429
430 if (!aead_op->pkt->in_buf || !aead_op->pkt->out_buf) {
431 return 0;
432 }
433
434 /* If the KeyConfig[EKeyID].RandomNonce bit is set
435 * the current nonce register content will be used.
436 * If there is an invalid random nonce or if there
437 * is no nonce synchronization between device
438 * the decrypt operation will fail accordingly.
439 */
440 if (ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_RAND_NONCE) {
441 LOG_DBG("key %x requires random nonce,"
442 " nonce_buf will be ignored", key_id);
443
444 LOG_DBG("current nonce register will be used");
445
446 }
447
448 k_sem_take(&data->device_sem, K_FOREVER);
449
450 /* If the KeyConfig[EKeyID].RandomNonce bit is not set
451 * then the nonce send as parameter will be loaded into
452 * the nonce register.
453 */
454 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_RAND_NONCE)
455 && nonce_buf) {
456 param_buffer[0] = 0x0;
457 param_buffer[1] = 0x0;
458 param_buffer[2] = 0x0;
459 param_buffer[3] = 0x0;
460 memcpy(param_buffer + 4, nonce_buf, 12);
461
462 return_code = ataes132a_send_command(dev, ATAES_NONCE_OP,
463 0x0, param_buffer, 16,
464 param_buffer, &out_len);
465
466 if (return_code != 0U) {
467 LOG_ERR("nonce command ended with code %d",
468 return_code);
469 k_sem_give(&data->device_sem);
470 return -EINVAL;
471 }
472
473 if (param_buffer[0] != 0U) {
474 LOG_ERR("nonce command failed with error"
475 " code %d", param_buffer[0]);
476 k_sem_give(&data->device_sem);
477 return -EIO;
478 }
479 }
480 /* If the KeyConfig[EKeyID].RandomNonce bit is not set
481 * and the nonce send as parameter is a null value,
482 * the command will use the current nonce register value.
483 */
484 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_RAND_NONCE)
485 && !nonce_buf) {
486 LOG_DBG("current nonce register will be used");
487 }
488
489 /* MAC packet to specify if MAC counter, serial number and small zone
490 * number are included in MAC generation.
491 */
492 if (mac_mode) {
493 if (mac_mode->include_counter) {
494 LOG_DBG("including usage counter in the MAC: "
495 "decrypt and encrypt dev must be the same");
496 command_mode = command_mode | ATAES_MAC_MODE_COUNTER;
497 }
498
499 if (mac_mode->include_serial) {
500 LOG_DBG("including serial number in the MAC: "
501 "decrypt and encrypt dev must be the same");
502 command_mode = command_mode | ATAES_MAC_MODE_SERIAL;
503 }
504
505 if (mac_mode->include_smallzone) {
506 LOG_DBG("including small zone in the MAC: "
507 "decrypt and encrypt dev share the "
508 "first four bytes of their small zone");
509 command_mode = command_mode | ATAES_MAC_MODE_SMALLZONE;
510 }
511 }
512
513 param_buffer[0] = key_id;
514 param_buffer[1] = buf_len;
515 memcpy(param_buffer + 2, aead_op->pkt->in_buf, buf_len);
516
517 return_code = ataes132a_send_command(dev, ATAES_ENCRYPT_OP,
518 command_mode, param_buffer,
519 buf_len + 2, param_buffer,
520 &out_len);
521
522 if (return_code != 0U) {
523 LOG_ERR("encrypt command ended with code %d", return_code);
524 k_sem_give(&data->device_sem);
525 return -EINVAL;
526 }
527
528 if (out_len < 33 || out_len > 49) {
529 LOG_ERR("encrypt command response has invalid"
530 " size %d", out_len);
531 k_sem_give(&data->device_sem);
532 return -EINVAL;
533 }
534
535 if (param_buffer[0] != 0U) {
536 LOG_ERR("encrypt command failed with error"
537 " code %d", param_buffer[0]);
538 k_sem_give(&data->device_sem);
539 return -EIO;
540 }
541
542 if (aead_op->tag) {
543 memcpy(aead_op->tag, param_buffer + 1, 16);
544 }
545 memcpy(aead_op->pkt->out_buf, param_buffer + 17, out_len - 17U);
546
547 if (mac_mode) {
548 if (mac_mode->include_counter) {
549 param_buffer[0] = 0x0;
550 param_buffer[1] = 0x0;
551 param_buffer[2] = 0x0;
552 param_buffer[3] = 0x0;
553 ataes132a_send_command(dev, ATAES_INFO_OP, 0x0,
554 param_buffer, 4,
555 param_buffer, &out_len);
556 if (param_buffer[0] != 0U) {
557 LOG_ERR("info command failed with error"
558 " code %d", param_buffer[0]);
559 k_sem_give(&data->device_sem);
560 return -EIO;
561 }
562 if (mac_count) {
563 *mac_count = param_buffer[2];
564 }
565 }
566 }
567
568 k_sem_give(&data->device_sem);
569
570 return 0;
571 }
572
ataes132a_aes_ecb_block(const struct device * dev,uint8_t key_id,struct cipher_pkt * pkt)573 int ataes132a_aes_ecb_block(const struct device *dev,
574 uint8_t key_id,
575 struct cipher_pkt *pkt)
576 {
577 struct ataes132a_device_data *data = dev->data;
578 uint8_t buf_len;
579 uint8_t out_len;
580 uint8_t return_code;
581 uint8_t param_buffer[19];
582
583 if (!pkt) {
584 LOG_ERR("Parameter cannot be null");
585 return -EINVAL;
586 }
587
588 buf_len = pkt->in_len;
589 if (buf_len > 16) {
590 LOG_ERR("input block cannot be above 16 bytes");
591 return -EINVAL;
592 }
593
594 /* AES ECB can only be executed if the ChipConfig.LegacyE configuration
595 * is set to 1 and if KeyConfig[key_id].LegacyOK is set to 1.
596 */
597 if (!(ataes132a_state[key_id].chip_config & ATAES_CHIPCONFIG_LEGACYE)) {
598 LOG_ERR("legacy mode disabled");
599 return -EINVAL;
600 }
601
602 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_LEGACYOK)) {
603 LOG_ERR("key %x legacy mode disabled", key_id);
604 return -EINVAL;
605 }
606
607 LOG_DBG("Chip config: %x", ataes132a_state[key_id].chip_config);
608 LOG_DBG("Key ID: %d", key_id);
609 LOG_DBG("Key config: %x", ataes132a_state[key_id].key_config);
610
611 /* If KeyConfig[key_id].AuthKey is set, then prior authentication
612 * is required
613 */
614 if (!(ataes132a_state[key_id].key_config & ATAES_KEYCONFIG_AUTHKEY)) {
615 LOG_DBG("keep in mind key %x will require"
616 " previous authentication", key_id);
617 }
618
619 if (!pkt->in_buf || !pkt->out_buf) {
620 return 0;
621 }
622
623 k_sem_take(&data->device_sem, K_FOREVER);
624
625 param_buffer[0] = 0x0;
626 param_buffer[1] = key_id;
627 param_buffer[2] = 0x0;
628 memcpy(param_buffer + 3, pkt->in_buf, buf_len);
629 (void)memset(param_buffer + 3 + buf_len, 0x0, 16 - buf_len);
630
631 return_code = ataes132a_send_command(dev, ATAES_LEGACY_OP, 0x00,
632 param_buffer, buf_len + 3,
633 param_buffer, &out_len);
634
635 if (return_code != 0U) {
636 LOG_ERR("legacy command ended with code %d", return_code);
637 k_sem_give(&data->device_sem);
638 return -EINVAL;
639 }
640
641 if (out_len != 17U) {
642 LOG_ERR("legacy command response has invalid"
643 " size %d", out_len);
644 k_sem_give(&data->device_sem);
645 return -EINVAL;
646 }
647 if (param_buffer[0] != 0U) {
648 LOG_ERR("legacy command failed with error"
649 " code %d", param_buffer[0]);
650 k_sem_give(&data->device_sem);
651 return -EIO;
652 }
653
654 memcpy(pkt->out_buf, param_buffer + 1, 16);
655
656 k_sem_give(&data->device_sem);
657
658 return 0;
659 }
660
do_ccm_encrypt_mac(struct cipher_ctx * ctx,struct cipher_aead_pkt * aead_op,uint8_t * nonce)661 static int do_ccm_encrypt_mac(struct cipher_ctx *ctx,
662 struct cipher_aead_pkt *aead_op, uint8_t *nonce)
663 {
664 const struct device *dev = ctx->device;
665 struct ataes132a_driver_state *state = ctx->drv_sessn_state;
666 struct ataes132a_mac_mode mac_mode;
667 uint8_t key_id;
668
669 key_id = state->key_id;
670
671 __ASSERT_NO_MSG(*(uint8_t *)ctx->key.handle == key_id);
672
673 /* Removing all this salt from the MAC reduces the protection
674 * but allows any other crypto implementations to authorize
675 * the message.
676 */
677 mac_mode.include_counter = false;
678 mac_mode.include_serial = false;
679 mac_mode.include_smallzone = false;
680
681 if (aead_op->pkt->in_len <= 16 &&
682 aead_op->pkt->out_buf_max < 16) {
683 LOG_ERR("Not enough space available in out buffer.");
684 return -EINVAL;
685 }
686
687 if (aead_op->pkt->in_len > 16 &&
688 aead_op->pkt->out_buf_max < 32) {
689 LOG_ERR("Not enough space available in out buffer.");
690 return -EINVAL;
691 }
692
693 if (aead_op->pkt->in_len <= 16) {
694 aead_op->pkt->out_len = 16;
695 } else if (aead_op->pkt->in_len > 16) {
696 aead_op->pkt->out_len = 32;
697 }
698
699 if (aead_op->ad != NULL || aead_op->ad_len != 0U) {
700 LOG_ERR("Associated data is not supported.");
701 return -EINVAL;
702 }
703
704 ataes132a_aes_ccm_encrypt(dev, key_id, &mac_mode,
705 aead_op, nonce, NULL);
706
707 return 0;
708 }
709
do_ccm_decrypt_auth(struct cipher_ctx * ctx,struct cipher_aead_pkt * aead_op,uint8_t * nonce)710 static int do_ccm_decrypt_auth(struct cipher_ctx *ctx,
711 struct cipher_aead_pkt *aead_op, uint8_t *nonce)
712 {
713 const struct device *dev = ctx->device;
714 struct ataes132a_driver_state *state = ctx->drv_sessn_state;
715 struct ataes132a_mac_mode mac_mode;
716 uint8_t key_id;
717
718 key_id = state->key_id;
719
720 __ASSERT_NO_MSG(*(uint8_t *)ctx->key.handle == key_id);
721
722 /* Removing all this salt from the MAC reduces the protection
723 * but allows any other crypto implementations to authorize
724 * the message.
725 */
726 mac_mode.include_counter = false;
727 mac_mode.include_serial = false;
728 mac_mode.include_smallzone = false;
729
730 if (aead_op->pkt->in_len <= 16 &&
731 aead_op->pkt->out_buf_max < 16) {
732 LOG_ERR("Not enough space available in out buffer.");
733 return -EINVAL;
734 }
735
736 if (aead_op->pkt->in_len > 16 &&
737 aead_op->pkt->out_buf_max < 32) {
738 LOG_ERR("Not enough space available in out buffer.");
739 return -EINVAL;
740 }
741
742 aead_op->pkt->ctx = ctx;
743
744 if (aead_op->ad != NULL || aead_op->ad_len != 0U) {
745 LOG_ERR("Associated data is not supported.");
746 return -EINVAL;
747 }
748
749 /* Normal Decryption Mode will only decrypt host generated packets */
750 ataes132a_aes_ccm_decrypt(dev, key_id, &mac_mode,
751 NULL, aead_op, nonce);
752
753 return 0;
754 }
755
do_block(struct cipher_ctx * ctx,struct cipher_pkt * pkt)756 static int do_block(struct cipher_ctx *ctx, struct cipher_pkt *pkt)
757 {
758 const struct device *dev = ctx->device;
759 struct ataes132a_driver_state *state = ctx->drv_sessn_state;
760 uint8_t key_id;
761
762 key_id = state->key_id;
763
764 __ASSERT_NO_MSG(*(uint8_t *)ctx->key.handle == key_id);
765
766 if (pkt->out_buf_max < 16) {
767 LOG_ERR("Not enough space available in out buffer.");
768 return -EINVAL;
769 }
770
771 pkt->out_len = 16;
772
773 return ataes132a_aes_ecb_block(dev, key_id, pkt);
774 }
775
ataes132a_session_free(const struct device * dev,struct cipher_ctx * session)776 static int ataes132a_session_free(const struct device *dev,
777 struct cipher_ctx *session)
778 {
779 struct ataes132a_driver_state *state = session->drv_sessn_state;
780
781 ARG_UNUSED(dev);
782
783 state->in_use = false;
784
785 return 0;
786 }
787
ataes132a_session_setup(const struct device * dev,struct cipher_ctx * ctx,enum cipher_algo algo,enum cipher_mode mode,enum cipher_op op_type)788 static int ataes132a_session_setup(const struct device *dev,
789 struct cipher_ctx *ctx,
790 enum cipher_algo algo, enum cipher_mode mode,
791 enum cipher_op op_type)
792 {
793 uint8_t key_id = *((uint8_t *)ctx->key.handle);
794 const struct ataes132a_device_config *cfg = dev->config;
795 uint8_t config;
796
797 if (ataes132a_state[key_id].in_use) {
798 LOG_ERR("Session in progress");
799 return -EINVAL;
800 }
801 if (mode == CRYPTO_CIPHER_MODE_CCM &&
802 ctx->mode_params.ccm_info.tag_len != 16U) {
803 LOG_ERR("ATAES132A support 16 byte tag only.");
804 return -EINVAL;
805 }
806 if (mode == CRYPTO_CIPHER_MODE_CCM &&
807 ctx->mode_params.ccm_info.nonce_len != 12U) {
808 LOG_ERR("ATAES132A support 12 byte nonce only.");
809 return -EINVAL;
810 }
811
812 ataes132a_state[key_id].in_use = true;
813 read_reg_i2c(&cfg->i2c, ATAES_KEYCFG_REG(key_id), &config);
814 ataes132a_state[key_id].key_config = config;
815 read_reg_i2c(&cfg->i2c, ATAES_CHIPCONFIG_REG, &config);
816 ataes132a_state[key_id].chip_config = config;
817
818 ctx->drv_sessn_state = &ataes132a_state[key_id];
819 ctx->device = dev;
820
821 if (algo != CRYPTO_CIPHER_ALGO_AES) {
822 LOG_ERR("ATAES132A unsupported algorithm");
823 return -EINVAL;
824 }
825
826 /*ATAES132A support I2C polling only*/
827 if (!(ctx->flags & CAP_SYNC_OPS)) {
828 LOG_ERR("Async not supported by this driver");
829 return -EINVAL;
830 }
831
832 if (ctx->keylen != ATAES132A_AES_KEY_SIZE) {
833 LOG_ERR("ATAES132A unsupported key size");
834 return -EINVAL;
835 }
836
837 if (op_type == CRYPTO_CIPHER_OP_ENCRYPT) {
838 switch (mode) {
839 case CRYPTO_CIPHER_MODE_ECB:
840 ctx->ops.block_crypt_hndlr = do_block;
841 break;
842 case CRYPTO_CIPHER_MODE_CCM:
843 ctx->ops.ccm_crypt_hndlr = do_ccm_encrypt_mac;
844 break;
845 default:
846 LOG_ERR("ATAES132A unsupported mode");
847 return -EINVAL;
848 }
849 } else {
850 switch (mode) {
851 case CRYPTO_CIPHER_MODE_ECB:
852 ctx->ops.block_crypt_hndlr = do_block;
853 break;
854 case CRYPTO_CIPHER_MODE_CCM:
855 ctx->ops.ccm_crypt_hndlr = do_ccm_decrypt_auth;
856 break;
857 default:
858 LOG_ERR("ATAES132A unsupported mode");
859 return -EINVAL;
860 }
861 }
862
863 ctx->ops.cipher_mode = mode;
864
865 return 0;
866 }
867
ataes132a_query_caps(const struct device * dev)868 static int ataes132a_query_caps(const struct device *dev)
869 {
870 return (CAP_OPAQUE_KEY_HNDL | CAP_SEPARATE_IO_BUFS |
871 CAP_SYNC_OPS | CAP_AUTONONCE);
872 }
873
874 static const struct ataes132a_device_config ataes132a_config = {
875 .i2c = I2C_DT_SPEC_INST_GET(0),
876 };
877
878 static struct crypto_driver_api crypto_enc_funcs = {
879 .cipher_begin_session = ataes132a_session_setup,
880 .cipher_free_session = ataes132a_session_free,
881 .cipher_async_callback_set = NULL,
882 .query_hw_caps = ataes132a_query_caps,
883 };
884
885 struct ataes132a_device_data ataes132a_data;
886
887 DEVICE_DT_INST_DEFINE(0, ataes132a_init,
888 NULL, &ataes132a_data, &ataes132a_config,
889 POST_KERNEL, CONFIG_CRYPTO_INIT_PRIORITY,
890 (void *)&crypto_enc_funcs);
891