blob: 28ea88192091c7e5ed820222663e90949bec28b9 [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/*
2 * Common and shared functions used by multiple modules in the Mbed Crypto
3 * library.
4 *
5 * Copyright (C) 2018, Arm Limited, All Rights Reserved
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 * This file is part of Mbed Crypto (https://tls.mbed.org)
21 */
22
23#if !defined(MBEDCRYPTO_CONFIG_FILE)
24#include "mbedcrypto/config.h"
25#else
26#include MBEDCRYPTO_CONFIG_FILE
27#endif
28
29#include "mbedcrypto/platform_util.h"
30
31#include <stddef.h>
32#include <string.h>
33
34#if !defined(MBEDCRYPTO_PLATFORM_ZEROIZE_ALT)
35/*
36 * This implementation should never be optimized out by the compiler
37 *
38 * This implementation for mbedcrypto_platform_zeroize() was inspired from Colin
39 * Percival's blog article at:
40 *
41 * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
42 *
43 * It uses a volatile function pointer to the standard memset(). Because the
44 * pointer is volatile the compiler expects it to change at
45 * any time and will not optimize out the call that could potentially perform
46 * other operations on the input buffer instead of just setting it to 0.
47 * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
48 * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
49 * details), optimizations of the following form are still possible:
50 *
51 * if( memset_func != memset )
52 * memset_func( buf, 0, len );
53 *
54 * Note that it is extremely difficult to guarantee that
55 * mbedcrypto_platform_zeroize() will not be optimized out by aggressive compilers
56 * in a portable way. For this reason, Mbed Crypto also provides the configuration
57 * option MBEDCRYPTO_PLATFORM_ZEROIZE_ALT, which allows users to configure
58 * mbedcrypto_platform_zeroize() to use a suitable implementation for their
59 * platform and needs.
60 */
61static void * (* const volatile memset_func)( void *, int, size_t ) = memset;
62
63void mbedcrypto_platform_zeroize( void *buf, size_t len )
64{
65 memset_func( buf, 0, len );
66}
67#endif /* MBEDCRYPTO_PLATFORM_ZEROIZE_ALT */