Andres Amaya Garcia | 614d9c0 | 2017-10-24 21:27:43 +0100 | [diff] [blame] | 1 | /* |
Andres Amaya Garcia | 0bd4237 | 2017-10-26 23:19:01 +0100 | [diff] [blame] | 2 | * mbed TLS utility functions |
Andres Amaya Garcia | 614d9c0 | 2017-10-24 21:27:43 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2017, 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 | |
Andres Amaya Garcia | b1262a3 | 2017-10-25 09:51:14 +0100 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
Andres Amaya Garcia | 614d9c0 | 2017-10-24 21:27:43 +0100 | [diff] [blame] | 28 | #include "mbedtls/utils.h" |
| 29 | |
| 30 | #include <stddef.h> |
Andres Amaya Garcia | ecd1891 | 2017-10-26 22:43:41 +0100 | [diff] [blame] | 31 | #include <string.h> |
Andres Amaya Garcia | 614d9c0 | 2017-10-24 21:27:43 +0100 | [diff] [blame] | 32 | |
Andres Amaya Garcia | b1262a3 | 2017-10-25 09:51:14 +0100 | [diff] [blame] | 33 | #if !defined(MBEDTLS_UTILS_ZEROIZE_ALT) |
Andres Amaya Garcia | ecd1891 | 2017-10-26 22:43:41 +0100 | [diff] [blame] | 34 | /* |
| 35 | * This implementation should never be optimized out by the compiler |
| 36 | * |
| 37 | * This implementation for mbedtls_zeroize() uses a volatile function pointer. |
| 38 | * We always know that it points to memset(), but because it is volatile the |
| 39 | * compiler expects it to change at any time and will not optimize out the |
| 40 | * call that could potentially perform other operations on the input buffer |
| 41 | * instead of just setting it to 0. Nevertheless, optimizations of the |
| 42 | * following form are still possible: |
| 43 | * |
| 44 | * if( memset_func != memset ) |
| 45 | * memset_func( buf, 0, len ); |
| 46 | * |
| 47 | * Note that it is extremely difficult to guarantee that mbedtls_zeroize() |
| 48 | * will not be optimized out by aggressive compilers in a portable way. For |
| 49 | * this reason, mbed TLS also provides the configuration option |
| 50 | * MBEDTLS_UTILS_ZEROIZE_ALT, which allows users to configure |
| 51 | * mbedtls_zeroize() to use a suitable implementation for their platform and |
| 52 | * needs. |
| 53 | */ |
| 54 | static void * (* const volatile memset_func)( void *, int, size_t ) = memset; |
| 55 | |
Andres Amaya Garcia | 614d9c0 | 2017-10-24 21:27:43 +0100 | [diff] [blame] | 56 | void mbedtls_zeroize( void *buf, size_t len ) |
| 57 | { |
Andres Amaya Garcia | ecd1891 | 2017-10-26 22:43:41 +0100 | [diff] [blame] | 58 | memset_func( buf, 0, len ); |
Andres Amaya Garcia | 614d9c0 | 2017-10-24 21:27:43 +0100 | [diff] [blame] | 59 | } |
Andres Amaya Garcia | b1262a3 | 2017-10-25 09:51:14 +0100 | [diff] [blame] | 60 | #endif /* MBEDTLS_UTILS_ZEROIZE_ALT */ |