blob: 62b3244ed2afc19fb884f4483d0212f4b6d10077 [file] [log] [blame]
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01001/*
Andres Amaya Garcia0bd42372017-10-26 23:19:01 +01002 * mbed TLS utility functions
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01003 *
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 Garciab1262a32017-10-25 09:51:14 +010022#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010028#include "mbedtls/utils.h"
29
30#include <stddef.h>
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010031#include <string.h>
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010032
Andres Amaya Garciab1262a32017-10-25 09:51:14 +010033#if !defined(MBEDTLS_UTILS_ZEROIZE_ALT)
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010034/*
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 */
54static void * (* const volatile memset_func)( void *, int, size_t ) = memset;
55
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010056void mbedtls_zeroize( void *buf, size_t len )
57{
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010058 memset_func( buf, 0, len );
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010059}
Andres Amaya Garciab1262a32017-10-25 09:51:14 +010060#endif /* MBEDTLS_UTILS_ZEROIZE_ALT */