Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_memset_secure.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 - 2024, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20#ifndef LC_MEMSET_SECURE_H
21#define LC_MEMSET_SECURE_H
22
23#include "ext_headers.h"
24
25/*
26 * Tested following code:
27 *
28 * (1) __asm__ __volatile__("" : "=r" (s) : "0" (s));
29 * (2) __asm__ __volatile__("": : :"memory");
30 * (3) __asm__ __volatile__("" : "=r" (s) : "0" (s) : "memory");
31 * (4) __asm__ __volatile__("" : : "r" (s) : "memory");
32 *
33 * Requred result:
34 *
35 * gcc -O3: objdump -d shows the following:
36 *
37 * 0000000000400440 <main>:
38 * ...
39 * 400469: 48 c7 04 24 00 00 00 movq $0x0,(%rsp)
40 * 400470: 00
41 * 400471: 48 c7 44 24 08 00 00 movq $0x0,0x8(%rsp)
42 * 400478: 00 00
43 * 40047a: c7 44 24 10 00 00 00 movl $0x0,0x10(%rsp)
44 * 400481: 00
45 *
46 * clang -O3: objdump -d shows the following:
47 *
48 * 0000000000400590 <main>:
49 * ...
50 * 4005c3: c7 44 24 10 00 00 00 movl $0x0,0x10(%rsp)
51 * 4005ca: 00
52 *
53 *
54 * Test results:
55 *
56 * The following table marks an X when the aforementioned movq/movl code is
57 * present (or an invocation of memset@plt) in the object code
58 * (i.e. the code we want). Contrary, the table marks - where the code is not
59 * present (i.e. the code we do not want):
60 *
61 * | BARRIER | (1) | (2) | (3) | (4)
62 * ---------+----------+ | | |
63 * Compiler | | | | |
64 * =========+==========+=======================
65 * | | | |
66 * gcc -O0 | X | X | X | X
67 * | | | |
68 * gcc -O2 | - | X | X | X
69 * | | | |
70 * gcc -O3 | - | X | X | X
71 * | | | |
72 * clang -00 | X | X | X | X
73 * | | | |
74 * clang -02 | X | - | X | X
75 * | | | |
76 * clang -03 | - | - | X | X
77 */
78
79static inline void lc_memset_secure(void *s, int c, size_t n)
80{
81 memset(s, c, n);
82 __asm__ __volatile__("" : : "r"(s) : "memory");
83}
84
85#if 0
86#include <stdio.h>
87
88int main(int argc, char *argv[])
89{
90 char buf[20];
91
92 snprintf(buf, sizeof(buf) - 1, "test");
93 printf("%s\n", buf);
94
95 memset_secure(buf, 0, sizeof(buf));
96 return 0;
97}
98#endif
99
100#endif /* LC_MEMSET_SECURE_H */
static void lc_memset_secure(void *s, int c, size_t n)