Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 29 | |
| 30 | #include "psa/crypto.h" |
| 31 | |
| 32 | #include "psa_crypto_slot_management.h" |
| 33 | #include "psa_crypto_storage.h" |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #if defined(MBEDTLS_PLATFORM_C) |
| 38 | #include "mbedtls/platform.h" |
| 39 | #else |
| 40 | #define mbedtls_calloc calloc |
| 41 | #define mbedtls_free free |
| 42 | #endif |
| 43 | |
| 44 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 45 | |
| 46 | psa_status_t psa_allocate_key( psa_key_type_t type, |
| 47 | size_t max_bits, |
| 48 | psa_key_handle_t *handle ) |
| 49 | { |
| 50 | /* This implementation doesn't reserve memory for the keys. */ |
| 51 | (void) type; |
| 52 | (void) max_bits; |
| 53 | *handle = 0; |
| 54 | return( psa_internal_allocate_key_slot( handle ) ); |
| 55 | } |
| 56 | |
| 57 | static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime, |
| 58 | psa_key_id_t id, |
| 59 | psa_key_handle_t *handle, |
| 60 | psa_status_t wanted_load_status ) |
| 61 | { |
| 62 | psa_status_t status; |
| 63 | |
| 64 | *handle = 0; |
| 65 | |
| 66 | if( lifetime != PSA_KEY_LIFETIME_PERSISTENT ) |
| 67 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 68 | |
| 69 | status = psa_internal_allocate_key_slot( handle ); |
| 70 | if( status != PSA_SUCCESS ) |
| 71 | return( status ); |
| 72 | |
| 73 | status = psa_internal_make_key_persistent( *handle, id ); |
| 74 | if( status != wanted_load_status ) |
| 75 | { |
| 76 | psa_internal_release_key_slot( *handle ); |
| 77 | *handle = 0; |
| 78 | } |
| 79 | return( status ); |
| 80 | } |
| 81 | |
| 82 | psa_status_t psa_open_key( psa_key_lifetime_t lifetime, |
| 83 | psa_key_id_t id, |
| 84 | psa_key_handle_t *handle ) |
| 85 | { |
| 86 | return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) ); |
| 87 | } |
| 88 | |
| 89 | psa_status_t psa_create_key( psa_key_lifetime_t lifetime, |
| 90 | psa_key_id_t id, |
| 91 | psa_key_type_t type, |
| 92 | size_t max_bits, |
| 93 | psa_key_handle_t *handle ) |
| 94 | { |
| 95 | psa_status_t status; |
| 96 | |
| 97 | /* This implementation doesn't reserve memory for the keys. */ |
| 98 | (void) type; |
| 99 | (void) max_bits; |
| 100 | |
| 101 | status = persistent_key_setup( lifetime, id, handle, |
| 102 | PSA_ERROR_EMPTY_SLOT ); |
| 103 | switch( status ) |
| 104 | { |
| 105 | case PSA_SUCCESS: return( PSA_ERROR_OCCUPIED_SLOT ); |
| 106 | case PSA_ERROR_EMPTY_SLOT: return( PSA_SUCCESS ); |
| 107 | default: return( status ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | psa_status_t psa_close_key( psa_key_handle_t handle ) |
| 112 | { |
| 113 | return( psa_internal_release_key_slot( handle ) ); |
| 114 | } |
| 115 | |
| 116 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |