xref: /xnu-10063.121.3/osfmk/corecrypto/cc_clear.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /* Copyright (c) (2014-2019,2021,2022) Apple Inc. All rights reserved.
2*2c2f96dcSApple OSS Distributions  *
3*2c2f96dcSApple OSS Distributions  * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4*2c2f96dcSApple OSS Distributions  * is contained in the License.txt file distributed with corecrypto) and only to
5*2c2f96dcSApple OSS Distributions  * people who accept that license. IMPORTANT:  Any license rights granted to you by
6*2c2f96dcSApple OSS Distributions  * Apple Inc. (if any) are limited to internal use within your organization only on
7*2c2f96dcSApple OSS Distributions  * devices and computers you own or control, for the sole purpose of verifying the
8*2c2f96dcSApple OSS Distributions  * security characteristics and correct functioning of the Apple Software.  You may
9*2c2f96dcSApple OSS Distributions  * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10*2c2f96dcSApple OSS Distributions  *
11*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
12*2c2f96dcSApple OSS Distributions  *
13*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
14*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
15*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
16*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
17*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
18*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
19*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
20*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
21*2c2f96dcSApple OSS Distributions  *
22*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
23*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
24*2c2f96dcSApple OSS Distributions  *
25*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
26*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
27*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
28*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
29*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
30*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
31*2c2f96dcSApple OSS Distributions  * limitations under the License.
32*2c2f96dcSApple OSS Distributions  *
33*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
34*2c2f96dcSApple OSS Distributions  */
35*2c2f96dcSApple OSS Distributions 
36*2c2f96dcSApple OSS Distributions #include "cc_internal.h"
37*2c2f96dcSApple OSS Distributions #include <corecrypto/cc.h>
38*2c2f96dcSApple OSS Distributions #include <corecrypto/cc_config.h>
39*2c2f96dcSApple OSS Distributions #include "fipspost_trace.h"
40*2c2f96dcSApple OSS Distributions 
41*2c2f96dcSApple OSS Distributions #if CC_HAS_SECUREZEROMEMORY
42*2c2f96dcSApple OSS Distributions #include <windows.h>
43*2c2f96dcSApple OSS Distributions #endif
44*2c2f96dcSApple OSS Distributions 
45*2c2f96dcSApple OSS Distributions #if !(CC_HAS_MEMSET_S || CC_HAS_SECUREZEROMEMORY || CC_HAS_EXPLICIT_BZERO)
46*2c2f96dcSApple OSS Distributions /*
47*2c2f96dcSApple OSS Distributions  * Pointer to memset is volatile so that the compiler must dereference
48*2c2f96dcSApple OSS Distributions  * it and can't assume it points to any function in particular
49*2c2f96dcSApple OSS Distributions  * (such as memset, which it then might further "optimize").
50*2c2f96dcSApple OSS Distributions  */
51*2c2f96dcSApple OSS Distributions     #if CC_EFI
52*2c2f96dcSApple OSS Distributions static void(*const volatile zero_mem_ptr)(void *, size_t) = EfiCommonLibZeroMem;
53*2c2f96dcSApple OSS Distributions     #else
54*2c2f96dcSApple OSS Distributions static void* (*const volatile memset_ptr)(void*, int, size_t) = memset;
55*2c2f96dcSApple OSS Distributions     #endif
56*2c2f96dcSApple OSS Distributions #endif
57*2c2f96dcSApple OSS Distributions 
58*2c2f96dcSApple OSS Distributions void
cc_clear(size_t len,void * dst)59*2c2f96dcSApple OSS Distributions cc_clear(size_t len, void *dst)
60*2c2f96dcSApple OSS Distributions {
61*2c2f96dcSApple OSS Distributions 	FIPSPOST_TRACE_EVENT;
62*2c2f96dcSApple OSS Distributions 
63*2c2f96dcSApple OSS Distributions #if CC_HAS_MEMSET_S
64*2c2f96dcSApple OSS Distributions 	memset_s(dst, len, 0, len);
65*2c2f96dcSApple OSS Distributions #elif CC_HAS_SECUREZEROMEMORY
66*2c2f96dcSApple OSS Distributions 	SecureZeroMemory(dst, len);
67*2c2f96dcSApple OSS Distributions #elif CC_HAS_EXPLICIT_BZERO
68*2c2f96dcSApple OSS Distributions 	explicit_bzero(dst, len);
69*2c2f96dcSApple OSS Distributions #else
70*2c2f96dcSApple OSS Distributions     #if CC_EFI
71*2c2f96dcSApple OSS Distributions 	(zero_mem_ptr)(dst, len);
72*2c2f96dcSApple OSS Distributions     #else
73*2c2f96dcSApple OSS Distributions 	(memset_ptr)(dst, 0, len);
74*2c2f96dcSApple OSS Distributions     #endif
75*2c2f96dcSApple OSS Distributions 
76*2c2f96dcSApple OSS Distributions 	/* One more safeguard, should all hell break loose - a memory barrier.
77*2c2f96dcSApple OSS Distributions 	 * The volatile function pointer _should_ work, but compilers are by
78*2c2f96dcSApple OSS Distributions 	 * spec allowed to load `memset_ptr` into a register and skip the
79*2c2f96dcSApple OSS Distributions 	 * call if `memset_ptr == memset`. However, too many systems rely
80*2c2f96dcSApple OSS Distributions 	 * on such behavior for compilers to try and optimize it. */
81*2c2f96dcSApple OSS Distributions 	__asm__ __volatile__ ("" : : "r"(dst) : "memory");
82*2c2f96dcSApple OSS Distributions #endif
83*2c2f96dcSApple OSS Distributions }
84