Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA cipher driver entry points |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa_crypto_cipher.h> |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 26 | #include "psa_crypto_core.h" |
| 27 | #include "mbedtls/cipher.h" |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 28 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 29 | #include <string.h> |
| 30 | |
| 31 | static psa_status_t cipher_setup( |
| 32 | psa_cipher_operation_t *operation, |
| 33 | const psa_key_attributes_t *attributes, |
| 34 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 35 | psa_algorithm_t alg, |
| 36 | mbedtls_operation_t cipher_operation ) |
| 37 | { |
| 38 | int ret = 0; |
| 39 | size_t key_bits; |
| 40 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 41 | psa_key_type_t key_type = attributes->core.type; |
| 42 | |
| 43 | (void)key_buffer_size; |
| 44 | |
| 45 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
| 46 | * available for the given algorithm & key. */ |
| 47 | mbedtls_cipher_init( &operation->ctx.cipher ); |
| 48 | |
| 49 | /* Once the cipher context is initialised, it needs to be freed using |
| 50 | * psa_cipher_abort. Indicate there is something to be freed through setting |
| 51 | * alg, and indicate the operation is being done using mbedtls crypto through |
| 52 | * setting mbedtls_in_use. */ |
| 53 | operation->alg = alg; |
| 54 | operation->mbedtls_in_use = 1; |
| 55 | |
| 56 | key_bits = attributes->core.bits; |
| 57 | cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, |
| 58 | key_bits, NULL ); |
| 59 | if( cipher_info == NULL ) |
| 60 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 61 | |
| 62 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
| 63 | if( ret != 0 ) |
| 64 | goto exit; |
| 65 | |
| 66 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
| 67 | if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
| 68 | { |
| 69 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
| 70 | uint8_t keys[24]; |
| 71 | memcpy( keys, key_buffer, 16 ); |
| 72 | memcpy( keys + 16, key_buffer, 8 ); |
| 73 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
| 74 | keys, |
| 75 | 192, cipher_operation ); |
| 76 | } |
| 77 | else |
| 78 | #endif |
| 79 | { |
| 80 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer, |
| 81 | (int) key_bits, cipher_operation ); |
| 82 | } |
| 83 | if( ret != 0 ) |
| 84 | goto exit; |
| 85 | |
| 86 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \ |
| 87 | defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) |
| 88 | switch( alg ) |
| 89 | { |
| 90 | case PSA_ALG_CBC_NO_PADDING: |
| 91 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 92 | MBEDTLS_PADDING_NONE ); |
| 93 | break; |
| 94 | case PSA_ALG_CBC_PKCS7: |
| 95 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
| 96 | MBEDTLS_PADDING_PKCS7 ); |
| 97 | break; |
| 98 | default: |
| 99 | /* The algorithm doesn't involve padding. */ |
| 100 | ret = 0; |
| 101 | break; |
| 102 | } |
| 103 | if( ret != 0 ) |
| 104 | goto exit; |
| 105 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */ |
| 106 | |
| 107 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
| 108 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
| 109 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 110 | alg != PSA_ALG_ECB_NO_PADDING ) |
| 111 | { |
| 112 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
| 113 | } |
| 114 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
| 115 | else |
| 116 | if( ( alg == PSA_ALG_STREAM_CIPHER ) && |
| 117 | ( key_type == PSA_KEY_TYPE_CHACHA20 ) ) |
| 118 | operation->iv_size = 12; |
| 119 | #endif |
| 120 | |
| 121 | exit: |
| 122 | return( mbedtls_to_psa_error( ret ) ); |
| 123 | } |
| 124 | |
| 125 | psa_status_t mbedtls_psa_cipher_encrypt_setup( |
| 126 | psa_cipher_operation_t *operation, |
| 127 | const psa_key_attributes_t *attributes, |
| 128 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 129 | psa_algorithm_t alg ) |
| 130 | { |
| 131 | return( cipher_setup( operation, attributes, |
| 132 | key_buffer, key_buffer_size, |
| 133 | alg, MBEDTLS_ENCRYPT ) ); |
| 134 | } |
| 135 | |
| 136 | psa_status_t mbedtls_psa_cipher_decrypt_setup( |
| 137 | psa_cipher_operation_t *operation, |
| 138 | const psa_key_attributes_t *attributes, |
| 139 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 140 | psa_algorithm_t alg ) |
| 141 | { |
| 142 | return( cipher_setup( operation, attributes, |
| 143 | key_buffer, key_buffer_size, |
| 144 | alg, MBEDTLS_DECRYPT ) ); |
| 145 | } |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 146 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |