0.2.0-dev Download ZIP
149 lines ·3.6 KB Raw
| 1 | /* |
| 2 | * PSA XOF (extendable-output function) layer on top of software crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "tf_psa_crypto_common.h" |
| 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 12 | |
| 13 | #include <psa/crypto.h> |
| 14 | #include "psa_crypto_xof.h" |
| 15 | |
| 16 | #if defined(MBEDTLS_PSA_BUILTIN_XOF) |
| 17 | |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "mbedtls/private/sha3.h" |
| 21 | |
| 22 | psa_status_t mbedtls_psa_xof_abort( |
| 23 | mbedtls_psa_xof_operation_t *operation) |
| 24 | { |
| 25 | switch (operation->alg) { |
| 26 | case 0: |
| 27 | /* The object has (apparently) been initialized but it is not |
| 28 | * in use. It's ok to call abort on such an object, and there's |
| 29 | * nothing to do. */ |
| 30 | break; |
| 31 | |
| 32 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE128) |
| 33 | case PSA_ALG_SHAKE128: |
| 34 | #endif |
| 35 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE256) |
| 36 | case PSA_ALG_SHAKE256: |
| 37 | #endif |
| 38 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SOME_SHAKE) |
| 39 | mbedtls_sha3_free(&operation->ctx.shake); |
| 40 | break; |
| 41 | #endif |
| 42 | |
| 43 | default: |
| 44 | return PSA_ERROR_BAD_STATE; |
| 45 | } |
| 46 | operation->alg = 0; |
| 47 | return PSA_SUCCESS; |
| 48 | } |
| 49 | |
| 50 | psa_status_t mbedtls_psa_xof_setup( |
| 51 | mbedtls_psa_xof_operation_t *operation, |
| 52 | psa_algorithm_t alg) |
| 53 | { |
| 54 | /* A context must be freshly initialized before it can be set up. */ |
| 55 | if (operation->alg != 0) { |
| 56 | return PSA_ERROR_BAD_STATE; |
| 57 | } |
| 58 | |
| 59 | switch (alg) { |
| 60 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE128) |
| 61 | case PSA_ALG_SHAKE128: |
| 62 | mbedtls_sha3_starts(&operation->ctx.shake, MBEDTLS_SHA3_SHAKE128); |
| 63 | break; |
| 64 | #endif |
| 65 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE256) |
| 66 | case PSA_ALG_SHAKE256: |
| 67 | mbedtls_sha3_starts(&operation->ctx.shake, MBEDTLS_SHA3_SHAKE256); |
| 68 | break; |
| 69 | #endif |
| 70 | |
| 71 | default: |
| 72 | return PSA_ALG_IS_XOF(alg) ? |
| 73 | PSA_ERROR_NOT_SUPPORTED : |
| 74 | PSA_ERROR_INVALID_ARGUMENT; |
| 75 | } |
| 76 | |
| 77 | operation->alg = alg; |
| 78 | return PSA_SUCCESS; |
| 79 | } |
| 80 | |
| 81 | psa_status_t mbedtls_psa_xof_set_context( |
| 82 | mbedtls_psa_xof_operation_t *operation, |
| 83 | const uint8_t *context, size_t context_length) |
| 84 | { |
| 85 | switch (operation->alg) { |
| 86 | case 0: |
| 87 | return PSA_ERROR_BAD_STATE; |
| 88 | |
| 89 | default: |
| 90 | (void) context; |
| 91 | (void) context_length; |
| 92 | return PSA_ERROR_INVALID_ARGUMENT; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | psa_status_t mbedtls_psa_xof_update( |
| 97 | mbedtls_psa_xof_operation_t *operation, |
| 98 | const uint8_t *input, size_t input_length) |
| 99 | { |
| 100 | switch (operation->alg) { |
| 101 | |
| 102 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE128) |
| 103 | case PSA_ALG_SHAKE128: |
| 104 | #endif |
| 105 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE256) |
| 106 | case PSA_ALG_SHAKE256: |
| 107 | #endif |
| 108 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SOME_SHAKE) |
| 109 | mbedtls_sha3_update(&operation->ctx.shake, input, input_length); |
| 110 | return PSA_SUCCESS; |
| 111 | #endif |
| 112 | |
| 113 | default: |
| 114 | (void) input; |
| 115 | (void) input_length; |
| 116 | return PSA_ERROR_BAD_STATE; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | psa_status_t mbedtls_psa_xof_output( |
| 121 | mbedtls_psa_xof_operation_t *operation, |
| 122 | uint8_t *output, size_t output_size) |
| 123 | { |
| 124 | /* TODO: fill output with something "safe" in case of error. |
| 125 | * What would be safe here? */ |
| 126 | |
| 127 | switch (operation->alg) { |
| 128 | |
| 129 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE128) |
| 130 | case PSA_ALG_SHAKE128: |
| 131 | #endif |
| 132 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHAKE256) |
| 133 | case PSA_ALG_SHAKE256: |
| 134 | #endif |
| 135 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SOME_SHAKE) |
| 136 | mbedtls_sha3_finish(&operation->ctx.shake, output, output_size); |
| 137 | return PSA_SUCCESS; |
| 138 | #endif |
| 139 | |
| 140 | default: |
| 141 | (void) output; |
| 142 | (void) output_size; |
| 143 | return PSA_ERROR_BAD_STATE; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #endif /* MBEDTLS_PSA_BUILTIN_XOF */ |
| 148 | |
| 149 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |