blob: 06f33699db78e718c58bb8340ee87020fdd3090c [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
4 * Warning: auto-generated file.
5 */
6/* Copyright (C) 2020, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23
24#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
26
27/* Include test driver definition when running tests */
28#if defined(MBEDTLS_TEST_HOOKS)
29#undef MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT
30#define MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman7a250572020-07-17 16:43:05 +020031#undef MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
32#define MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremancd84cb42020-07-16 20:28:36 +020033#include "drivers/test_driver.h"
34#endif
35
Steven Cooreman7a250572020-07-17 16:43:05 +020036#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
37#undef MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT
38#define MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT
39#include "psa_crypto_se.h"
40#endif
41
Steven Cooremancd84cb42020-07-16 20:28:36 +020042/* Include driver definition file for each registered driver */
43
44/* Start delegation functions */
45psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot,
46 psa_algorithm_t alg,
47 const uint8_t *hash,
48 size_t hash_length,
49 uint8_t *signature,
50 size_t signature_size,
51 size_t *signature_length )
52{
53#if defined(MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT)
Steven Cooreman7a250572020-07-17 16:43:05 +020054 /* Try dynamically-registered SE interface first */
55#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
56 const psa_drv_se_t *drv;
57 psa_drv_se_context_t *drv_context;
58
59 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
60 {
61 if( drv->asymmetric == NULL ||
62 drv->asymmetric->p_sign == NULL )
63 {
64 /* Key is defined in SE, but we have no way to exercise it */
65 return PSA_ERROR_INVALID_ARGUMENT;
66 }
67 return( drv->asymmetric->p_sign( drv_context,
68 slot->data.se.slot_number,
69 alg,
70 hash, hash_length,
71 signature, signature_size,
72 signature_length ) );
73 }
74#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
75
76 /* Then try accelerator API */
77#if defined(MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremancd84cb42020-07-16 20:28:36 +020078 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
79 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
80 psa_key_attributes_t attributes = {
81 .core = slot->attr
82 };
83
84 switch( location )
85 {
86 case PSA_KEY_LOCATION_LOCAL_STORAGE:
87 /* Key is stored in the slot in export representation, so
88 * cycle through all known transparent accelerators */
89#if defined(MBEDTLS_TEST_HOOKS)
90 status = test_transparent_signature_sign_hash( &attributes,
91 slot->data.key.data,
92 slot->data.key.bytes,
93 alg,
94 hash,
95 hash_length,
96 signature,
97 signature_size,
98 signature_length );
99 /* Declared with fallback == true */
100 if( status != PSA_ERROR_NOT_SUPPORTED )
101 return status;
102#endif /* MBEDTLS_TEST_HOOKS */
103 /* Fell through, meaning no accelerator supports this operation */
104 return PSA_ERROR_NOT_SUPPORTED;
105 /* Add cases for opaque driver here */
106#if defined(MBEDTLS_TEST_HOOKS)
107 case MBEDTLS_PSA_CRYPTO_TEST_DRIVER_LIFETIME:
108 return( test_opaque_signature_sign_hash( &attributes,
109 slot->data.key.data,
110 slot->data.key.bytes,
111 alg,
112 hash,
113 hash_length,
114 signature,
115 signature_size,
116 signature_length ) );
117#endif /* MBEDTLS_TEST_HOOKS */
118 default:
119 /* Key is declared with a lifetime not known to us */
120 return status;
121 }
Steven Cooreman7a250572020-07-17 16:43:05 +0200122#else /* MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
123 return PSA_ERROR_NOT_SUPPORTED;
124#endif /* MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200125#else /* MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT */
126 (void)slot;
127 (void)alg;
128 (void)hash;
129 (void)hash_length;
130 (void)signature;
131 (void)signature_size;
132 (void)signature_length;
133
134 return PSA_ERROR_NOT_SUPPORTED;
135#endif /* MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT */
136}
137
138/* End of automatically generated file. */