Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, 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 | /* FixMe: Use PSA_CONNECTION_REFUSED when performing parameter |
| 12 | * integrity checks but this will have to be revised |
| 13 | * when the full set of error codes mandated by PSA FF |
| 14 | * is available. |
| 15 | */ |
| 16 | #include "tfm_mbedcrypto_include.h" |
| 17 | |
| 18 | #include "tfm_crypto_api.h" |
| 19 | #include "tfm_crypto_defs.h" |
| 20 | |
| 21 | /*! |
| 22 | * \defgroup public_psa Public functions, PSA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | /*!@{*/ |
| 27 | psa_status_t tfm_crypto_asymmetric_sign(psa_invec in_vec[], |
| 28 | size_t in_len, |
| 29 | psa_outvec out_vec[], |
| 30 | size_t out_len) |
| 31 | { |
| 32 | if ((in_len != 2) || (out_len != 1)) { |
| 33 | return PSA_CONNECTION_REFUSED; |
| 34 | } |
| 35 | |
| 36 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
| 37 | return PSA_CONNECTION_REFUSED; |
| 38 | } |
| 39 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 40 | |
| 41 | psa_key_handle_t handle = iov->key_handle; |
| 42 | psa_algorithm_t alg = iov->alg; |
| 43 | const uint8_t *hash = in_vec[1].base; |
| 44 | size_t hash_length = in_vec[1].len; |
| 45 | uint8_t *signature = out_vec[0].base; |
| 46 | size_t signature_size = out_vec[0].len; |
| 47 | |
| 48 | return psa_asymmetric_sign(handle, alg, hash, hash_length, |
| 49 | signature, signature_size, &(out_vec[0].len)); |
| 50 | } |
| 51 | |
| 52 | psa_status_t tfm_crypto_asymmetric_verify(psa_invec in_vec[], |
| 53 | size_t in_len, |
| 54 | psa_outvec out_vec[], |
| 55 | size_t out_len) |
| 56 | { |
| 57 | if ((in_len != 3) || (out_len != 0)) { |
| 58 | return PSA_CONNECTION_REFUSED; |
| 59 | } |
| 60 | |
| 61 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
| 62 | return PSA_CONNECTION_REFUSED; |
| 63 | } |
| 64 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 65 | |
| 66 | psa_key_handle_t handle = iov->key_handle; |
| 67 | psa_algorithm_t alg = iov->alg; |
| 68 | const uint8_t *hash = in_vec[1].base; |
| 69 | size_t hash_length = in_vec[1].len; |
| 70 | const uint8_t *signature = in_vec[2].base; |
| 71 | size_t signature_length = in_vec[2].len; |
| 72 | |
| 73 | return psa_asymmetric_verify(handle, alg, hash, hash_length, |
| 74 | signature, signature_length); |
| 75 | } |
| 76 | |
| 77 | psa_status_t tfm_crypto_asymmetric_encrypt(psa_invec in_vec[], |
| 78 | size_t in_len, |
| 79 | psa_outvec out_vec[], |
| 80 | size_t out_len) |
| 81 | { |
| 82 | if (!((in_len == 2) || (in_len == 3)) || (out_len != 1)) { |
| 83 | return PSA_CONNECTION_REFUSED; |
| 84 | } |
| 85 | |
| 86 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
| 87 | return PSA_CONNECTION_REFUSED; |
| 88 | } |
| 89 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 90 | |
| 91 | psa_key_handle_t handle = iov->key_handle; |
| 92 | psa_algorithm_t alg = iov->alg; |
| 93 | const uint8_t *input = in_vec[1].base; |
| 94 | size_t input_length = in_vec[1].len; |
| 95 | const uint8_t *salt = NULL; |
| 96 | size_t salt_length = 0; |
| 97 | uint8_t *output = out_vec[0].base; |
| 98 | size_t output_size = out_vec[0].len; |
| 99 | |
| 100 | if (in_len == 3) { |
| 101 | salt = in_vec[2].base; |
| 102 | salt_length = in_vec[2].len; |
| 103 | } |
| 104 | |
| 105 | return psa_asymmetric_encrypt(handle, alg, input, input_length, |
| 106 | salt, salt_length, |
| 107 | output, output_size, &(out_vec[0].len)); |
| 108 | } |
| 109 | |
| 110 | psa_status_t tfm_crypto_asymmetric_decrypt(psa_invec in_vec[], |
| 111 | size_t in_len, |
| 112 | psa_outvec out_vec[], |
| 113 | size_t out_len) |
| 114 | { |
| 115 | if (!((in_len == 2) || (in_len == 3)) || (out_len != 1)) { |
| 116 | return PSA_CONNECTION_REFUSED; |
| 117 | } |
| 118 | |
| 119 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
| 120 | return PSA_CONNECTION_REFUSED; |
| 121 | } |
| 122 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 123 | |
| 124 | psa_key_handle_t handle = iov->key_handle; |
| 125 | psa_algorithm_t alg = iov->alg; |
| 126 | const uint8_t *input = in_vec[1].base; |
| 127 | size_t input_length = in_vec[1].len; |
| 128 | const uint8_t *salt = NULL; |
| 129 | size_t salt_length = 0; |
| 130 | uint8_t *output = out_vec[0].base; |
| 131 | size_t output_size = out_vec[0].len; |
| 132 | |
| 133 | if (in_len == 3) { |
| 134 | salt = in_vec[2].base; |
| 135 | salt_length = in_vec[2].len; |
| 136 | } |
| 137 | |
| 138 | return psa_asymmetric_decrypt(handle, alg, input, input_length, |
| 139 | salt, salt_length, |
| 140 | output, output_size, &(out_vec[0].len)); |
| 141 | } |
| 142 | /*!@}*/ |