1 /*
2 * Copyright (c) 2015 Google, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #pragma once
25
26 #include <compiler.h>
27 #include <string.h>
28 #include <sys/types.h>
29
30 __BEGIN_CDECLS;
31
32 /**
33 * tc_memcmp() - Timing resistent memory compare
34 *
35 * @p1: points to first memory area to compare
36 * @p2: points to second memory area to compare
37 * @len: number of bytes to compare
38 *
39 * This function runs in constant time. Note, that return value
40 * differ from memcmp.
41 *
42 * Return: 0 if first @len bytes in both areas are identical,
43 * non-zero otherwise.
44 */
45 int tc_memcmp(const void* p1, const void* p2, size_t len);
46
47 /**
48 * tc_memset() - variant of memset that disables optimization
49 * so it is not optimized away.
50 *
51 * @p - pointer to memory area to fill with @c
52 * @c - constant byte to fill memory area @p with
53 * @n - number of bytes to fill
54 *
55 * Return: pointer to memory area @p.
56 */
__OPTIMIZE(O0)57 inline __OPTIMIZE(O0) void* tc_memset(void* p, int c, size_t n) {
58 if (!p)
59 return p;
60 return memset(p, c, n);
61 }
62
63 __END_CDECLS;
64