Blame lib/safe-memfuncs.c

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