1 /*
2  * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include "config_tfm.h"
12 #include "tfm_mbedcrypto_include.h"
13 
14 #include "tfm_crypto_api.h"
15 #include "tfm_crypto_key.h"
16 #include "tfm_crypto_defs.h"
17 
18 #include "crypto_library.h"
19 
20 /*!
21  * \addtogroup tfm_crypto_api_shim_layer
22  *
23  */
24 
25 /*!@{*/
26 #if CRYPTO_ASYM_SIGN_MODULE_ENABLED
tfm_crypto_asymmetric_sign_interface(psa_invec in_vec[],psa_outvec out_vec[],struct tfm_crypto_key_id_s * encoded_key)27 psa_status_t tfm_crypto_asymmetric_sign_interface(psa_invec in_vec[],
28                                                   psa_outvec out_vec[],
29                                                   struct tfm_crypto_key_id_s *encoded_key)
30 {
31     const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
32     psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
33 
34     tfm_crypto_library_key_id_t library_key = tfm_crypto_library_key_id_init(
35                                                   encoded_key->owner, encoded_key->key_id);
36     switch (iov->function_id) {
37     case TFM_CRYPTO_ASYMMETRIC_SIGN_MESSAGE_SID:
38     {
39         const uint8_t *input = in_vec[1].base;
40         size_t input_length = in_vec[1].len;
41         uint8_t *signature = out_vec[0].base;
42         size_t signature_size = out_vec[0].len;
43 
44         status = psa_sign_message(library_key, iov->alg, input, input_length,
45                                   signature, signature_size, &(out_vec[0].len));
46         if (status != PSA_SUCCESS) {
47             out_vec[0].len = 0;
48         }
49         return status;
50     }
51     case TFM_CRYPTO_ASYMMETRIC_VERIFY_MESSAGE_SID:
52     {
53         const uint8_t *input = in_vec[1].base;
54         size_t input_length = in_vec[1].len;
55         const uint8_t *signature = in_vec[2].base;
56         size_t signature_length = in_vec[2].len;
57 
58         return psa_verify_message(library_key, iov->alg, input, input_length,
59                                   signature, signature_length);
60     }
61     case TFM_CRYPTO_ASYMMETRIC_SIGN_HASH_SID:
62     {
63         const uint8_t *hash = in_vec[1].base;
64         size_t hash_length = in_vec[1].len;
65         uint8_t *signature = out_vec[0].base;
66         size_t signature_size = out_vec[0].len;
67 
68         status = psa_sign_hash(library_key, iov->alg, hash, hash_length,
69                                signature, signature_size, &(out_vec[0].len));
70         if (status != PSA_SUCCESS) {
71             out_vec[0].len = 0;
72         }
73         return status;
74     }
75     case TFM_CRYPTO_ASYMMETRIC_VERIFY_HASH_SID:
76     {
77         const uint8_t *hash = in_vec[1].base;
78         size_t hash_length = in_vec[1].len;
79         const uint8_t *signature = in_vec[2].base;
80         size_t signature_length = in_vec[2].len;
81 
82         return psa_verify_hash(library_key, iov->alg, hash, hash_length,
83                                signature, signature_length);
84     }
85     default:
86         return PSA_ERROR_NOT_SUPPORTED;
87     }
88 
89     return PSA_ERROR_NOT_SUPPORTED;
90 }
91 #else /* CRYPTO_ASYM_SIGN_MODULE_ENABLED */
tfm_crypto_asymmetric_sign_interface(psa_invec in_vec[],psa_outvec out_vec[],struct tfm_crypto_key_id_s * encoded_key)92 psa_status_t tfm_crypto_asymmetric_sign_interface(psa_invec in_vec[],
93                                                   psa_outvec out_vec[],
94                                                   struct tfm_crypto_key_id_s *encoded_key)
95 {
96     (void)in_vec;
97     (void)out_vec;
98     (void)encoded_key;
99 
100     return PSA_ERROR_NOT_SUPPORTED;
101 }
102 #endif /* CRYPTO_ASYM_SIGN_MODULE_ENABLED */
103 
104 #if CRYPTO_ASYM_ENCRYPT_MODULE_ENABLED
tfm_crypto_asymmetric_encrypt_interface(psa_invec in_vec[],psa_outvec out_vec[],struct tfm_crypto_key_id_s * encoded_key)105 psa_status_t tfm_crypto_asymmetric_encrypt_interface(psa_invec in_vec[],
106                                                      psa_outvec out_vec[],
107                                                      struct tfm_crypto_key_id_s *encoded_key)
108 {
109     const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
110     psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
111 
112     tfm_crypto_library_key_id_t library_key = tfm_crypto_library_key_id_init(
113                                                   encoded_key->owner, encoded_key->key_id);
114     switch (iov->function_id) {
115     case TFM_CRYPTO_ASYMMETRIC_ENCRYPT_SID:
116     {
117         const uint8_t *input = in_vec[1].base;
118         size_t input_length = in_vec[1].len;
119         const uint8_t *salt = in_vec[2].base;
120         size_t salt_length = in_vec[2].len;
121         uint8_t *output = out_vec[0].base;
122         size_t output_size = out_vec[0].len;
123 
124         status = psa_asymmetric_encrypt(library_key, iov->alg,
125                                         input, input_length,
126                                         salt, salt_length,
127                                         output, output_size,
128                                         &(out_vec[0].len));
129         if (status != PSA_SUCCESS) {
130             out_vec[0].len = 0;
131         }
132         return status;
133     }
134     case TFM_CRYPTO_ASYMMETRIC_DECRYPT_SID:
135     {
136         const uint8_t *input = in_vec[1].base;
137         size_t input_length = in_vec[1].len;
138         const uint8_t *salt = in_vec[2].base;
139         size_t salt_length = in_vec[2].len;
140         uint8_t *output = out_vec[0].base;
141         size_t output_size = out_vec[0].len;
142 
143         status = psa_asymmetric_decrypt(library_key, iov->alg,
144                                         input, input_length,
145                                         salt, salt_length,
146                                         output, output_size,
147                                         &(out_vec[0].len));
148         if (status != PSA_SUCCESS) {
149             out_vec[0].len = 0;
150         }
151         return status;
152     }
153     default:
154         return PSA_ERROR_NOT_SUPPORTED;
155     }
156 
157     return PSA_ERROR_NOT_SUPPORTED;
158 }
159 #else /* CRYPTO_ASYM_ENCRYPT_MODULE_ENABLED */
tfm_crypto_asymmetric_encrypt_interface(psa_invec in_vec[],psa_outvec out_vec[],struct tfm_crypto_key_id_s * encoded_key)160 psa_status_t tfm_crypto_asymmetric_encrypt_interface(psa_invec in_vec[],
161                                                      psa_outvec out_vec[],
162                                                      struct tfm_crypto_key_id_s *encoded_key)
163 {
164     (void)in_vec;
165     (void)out_vec;
166     (void)encoded_key;
167 
168     return PSA_ERROR_NOT_SUPPORTED;
169 }
170 #endif /* CRYPTO_ASYM_ENCRYPT_MODULE_ENABLED */
171 /*!@}*/
172