|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2014 Red Hat
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GnuTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The GnuTLS is free software; you can redistribute it and/or
|
|
Packit Service |
4684c1 |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
4684c1 |
* the License, or (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This library is distributed in the hope that it will be useful, but
|
|
Packit Service |
4684c1 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
4684c1 |
* Lesser General Public License for more details.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "gnutls_int.h"
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_memset:
|
|
Packit Service |
4684c1 |
* @data: the memory to set
|
|
Packit Service |
4684c1 |
* @c: the constant byte to fill the memory with
|
|
Packit Service |
4684c1 |
* @size: the size of memory
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will operate similarly to memset(), but will
|
|
Packit Service |
4684c1 |
* not be optimized out by the compiler.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.4.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void gnutls_memset(void *data, int c, size_t size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
volatile unsigned volatile_zero;
|
|
Packit Service |
4684c1 |
volatile char *vdata = (volatile char*)data;
|
|
Packit Service |
4684c1 |
#ifdef HAVE_EXPLICIT_BZERO
|
|
Packit Service |
4684c1 |
if (c == 0) {
|
|
Packit Service |
4684c1 |
explicit_bzero(data, size);
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
volatile_zero = 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This is based on a nice trick for safe memset,
|
|
Packit Service |
4684c1 |
* sent by David Jacobson in the openssl-dev mailing list.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (size > 0) {
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
memset(data, c, size);
|
|
Packit Service |
4684c1 |
} while(vdata[volatile_zero] != c);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_memcmp:
|
|
Packit Service |
4684c1 |
* @s1: the first address to compare
|
|
Packit Service |
4684c1 |
* @s2: the second address to compare
|
|
Packit Service |
4684c1 |
* @n: the size of memory to compare
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will operate similarly to memcmp(), but will operate
|
|
Packit Service |
4684c1 |
* on time that depends only on the size of the string. That is will
|
|
Packit Service |
4684c1 |
* not return early if the strings don't match on the first byte.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: non zero on difference and zero if the buffers are identical.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.4.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int gnutls_memcmp(const void *s1, const void *s2, size_t n)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
unsigned i;
|
|
Packit Service |
4684c1 |
unsigned status = 0;
|
|
Packit Service |
4684c1 |
const uint8_t *_s1 = s1;
|
|
Packit Service |
4684c1 |
const uint8_t *_s2 = s2;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i=0;i
|
|
Packit Service |
4684c1 |
status |= (_s1[i] ^ _s2[i]);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return status;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef TEST_SAFE_MEMSET
|
|
Packit Service |
4684c1 |
int main()
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
char x[64];
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_memset(x, 0, sizeof(x));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#endif
|