1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) Foundries Ltd. 2020 - All Rights Reserved
4 * Author: Jorge Ramirez <jorge@foundries.io>
5 */
6 #include <compiler.h>
7 #include <config.h>
8 #include <crypto/crypto.h>
9 #include <fsl_sss_user_apis.h>
10 #include <glue.h>
11 #include <stdlib.h>
12
glue_mac_context_init(void ** mac,const uint8_t * key,size_t len)13 sss_status_t glue_mac_context_init(void **mac, const uint8_t *key, size_t len)
14 {
15 if (crypto_mac_alloc_ctx(mac, TEE_ALG_AES_CMAC))
16 return kStatus_SSS_Fail;
17
18 if (crypto_mac_init(*mac, key, len))
19 return kStatus_SSS_Fail;
20
21 return kStatus_SSS_Success;
22 }
23
glue_mac_context_free(void * mac)24 void glue_mac_context_free(void *mac)
25 {
26 crypto_mac_free_ctx(mac);
27 }
28
glue_mac_update(void * mac,const uint8_t * msg,size_t len)29 sss_status_t glue_mac_update(void *mac, const uint8_t *msg, size_t len)
30 {
31 if (crypto_mac_update(mac, msg, len))
32 return kStatus_SSS_Fail;
33
34 return kStatus_SSS_Success;
35 }
36
glue_mac_final(void * mac,uint8_t * buf,size_t len)37 sss_status_t glue_mac_final(void *mac, uint8_t *buf, size_t len)
38 {
39 if (crypto_mac_final(mac, buf, len))
40 return kStatus_SSS_Fail;
41
42 return kStatus_SSS_Success;
43 }
44
glue_mac_one_go(void * mac,const uint8_t * msg,size_t msg_len,uint8_t * buf,size_t mac_len)45 sss_status_t glue_mac_one_go(void *mac, const uint8_t *msg, size_t msg_len,
46 uint8_t *buf, size_t mac_len)
47 {
48 if (crypto_mac_update(mac, msg, msg_len))
49 return kStatus_SSS_Fail;
50
51 if (crypto_mac_final(mac, buf, mac_len))
52 return kStatus_SSS_Fail;
53
54 return kStatus_SSS_Success;
55 }
56
glue_symmetric_context_init(void ** cipher)57 sss_status_t glue_symmetric_context_init(void **cipher)
58 {
59 if (crypto_cipher_alloc_ctx(cipher, TEE_ALG_AES_CBC_NOPAD))
60 return kStatus_SSS_Fail;
61
62 return kStatus_SSS_Success;
63 }
64
glue_cipher_one_go(void * cipher,TEE_OperationMode mode,uint8_t * iv,size_t iv_len,uint8_t * key,size_t key_len,const uint8_t * src,uint8_t * dst,size_t len)65 sss_status_t glue_cipher_one_go(void *cipher, TEE_OperationMode mode,
66 uint8_t *iv, size_t iv_len,
67 uint8_t *key, size_t key_len,
68 const uint8_t *src, uint8_t *dst, size_t len)
69 {
70 if (crypto_cipher_init(cipher, mode, key, key_len, NULL, 0, iv, iv_len))
71 return kStatus_SSS_Fail;
72
73 if (crypto_cipher_update(cipher, 0, true, src, len, dst))
74 return kStatus_SSS_Fail;
75
76 crypto_cipher_final(cipher);
77
78 return kStatus_SSS_Success;
79 }
80
glue_context_free(void * cipher)81 void glue_context_free(void *cipher)
82 {
83 crypto_cipher_free_ctx(cipher);
84 }
85
glue_rng_get_random(uint8_t * data,size_t len)86 sss_status_t glue_rng_get_random(uint8_t *data, size_t len)
87 {
88 if (IS_ENABLED(CFG_NXP_SE05X_RNG_DRV))
89 return kStatus_SSS_InvalidArgument;
90
91 if (crypto_rng_read(data, len))
92 return kStatus_SSS_Fail;
93
94 return kStatus_SSS_Success;
95 }
96