/src/gnutls/lib/safe-memfuncs.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2014 Red Hat |
3 | | * |
4 | | * This file is part of GnuTLS. |
5 | | * |
6 | | * The GnuTLS is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public License |
8 | | * as published by the Free Software Foundation; either version 2.1 of |
9 | | * the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
18 | | * |
19 | | */ |
20 | | |
21 | | #include "gnutls_int.h" |
22 | | #include <string.h> |
23 | | |
24 | | /** |
25 | | * gnutls_memset: |
26 | | * @data: the memory to set |
27 | | * @c: the constant byte to fill the memory with |
28 | | * @size: the size of memory |
29 | | * |
30 | | * This function will operate similarly to memset(), but will |
31 | | * not be optimized out by the compiler. |
32 | | * |
33 | | * Since: 3.4.0 |
34 | | **/ |
35 | | void gnutls_memset(void *data, int c, size_t size) |
36 | 0 | { |
37 | 0 | explicit_bzero(data, size); |
38 | 0 | memset(data, c, size); |
39 | 0 | } |
40 | | |
41 | | /** |
42 | | * gnutls_memcmp: |
43 | | * @s1: the first address to compare |
44 | | * @s2: the second address to compare |
45 | | * @n: the size of memory to compare |
46 | | * |
47 | | * This function will operate similarly to memcmp(), but will operate |
48 | | * on time that depends only on the size of the string. That is will |
49 | | * not return early if the strings don't match on the first byte. |
50 | | * |
51 | | * Returns: non zero on difference and zero if the buffers are identical. |
52 | | * |
53 | | * Since: 3.4.0 |
54 | | **/ |
55 | | int gnutls_memcmp(const void *s1, const void *s2, size_t n) |
56 | 0 | { |
57 | 0 | unsigned i; |
58 | 0 | unsigned status = 0; |
59 | 0 | const uint8_t *_s1 = s1; |
60 | 0 | const uint8_t *_s2 = s2; |
61 | |
|
62 | 0 | for (i = 0; i < n; i++) { |
63 | 0 | status |= (_s1[i] ^ _s2[i]); |
64 | 0 | } |
65 | |
|
66 | 0 | return status; |
67 | 0 | } |
68 | | |
69 | | #ifdef TEST_SAFE_MEMSET |
70 | | int main(void) |
71 | | { |
72 | | char x[64]; |
73 | | |
74 | | gnutls_memset(x, 0, sizeof(x)); |
75 | | |
76 | | return 0; |
77 | | |
78 | | } |
79 | | |
80 | | #endif |