Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers platform_util.c Source File

platform_util.c

00001 /*
00002  * Common and shared functions used by multiple modules in the Mbed TLS
00003  * library.
00004  *
00005  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
00006  *  SPDX-License-Identifier: Apache-2.0
00007  *
00008  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00009  *  not use this file except in compliance with the License.
00010  *  You may obtain a copy of the License at
00011  *
00012  *  http://www.apache.org/licenses/LICENSE-2.0
00013  *
00014  *  Unless required by applicable law or agreed to in writing, software
00015  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00016  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  *  See the License for the specific language governing permissions and
00018  *  limitations under the License.
00019  *
00020  *  This file is part of Mbed TLS (https://tls.mbed.org)
00021  */
00022 
00023 #if !defined(MBEDTLS_CONFIG_FILE)
00024 #include "mbedtls/config.h"
00025 #else
00026 #include MBEDTLS_CONFIG_FILE
00027 #endif
00028 
00029 #include "mbedtls/platform_util.h"
00030 
00031 #include <stddef.h>
00032 #include <string.h>
00033 
00034 #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
00035 /*
00036  * This implementation should never be optimized out by the compiler
00037  *
00038  * This implementation for mbedtls_platform_zeroize() was inspired from Colin
00039  * Percival's blog article at:
00040  *
00041  * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
00042  *
00043  * It uses a volatile function pointer to the standard memset(). Because the
00044  * pointer is volatile the compiler expects it to change at
00045  * any time and will not optimize out the call that could potentially perform
00046  * other operations on the input buffer instead of just setting it to 0.
00047  * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
00048  * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
00049  * details), optimizations of the following form are still possible:
00050  *
00051  * if( memset_func != memset )
00052  *     memset_func( buf, 0, len );
00053  *
00054  * Note that it is extremely difficult to guarantee that
00055  * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
00056  * in a portable way. For this reason, Mbed TLS also provides the configuration
00057  * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
00058  * mbedtls_platform_zeroize() to use a suitable implementation for their
00059  * platform and needs.
00060  */
00061 static void * (* const volatile memset_func)( void *, int, size_t ) = memset;
00062 
00063 void mbedtls_platform_zeroize( void *buf, size_t len )
00064 {
00065     memset_func( buf, 0, len );
00066 }
00067 #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */