ct.c revision 302408
1251881Speter/* 2251881Speter * Copyright (c) 2009 Kungliga Tekniska H��gskolan 3251881Speter * (Royal Institute of Technology, Stockholm, Sweden). 4251881Speter * All rights reserved. 5251881Speter * 6251881Speter * Redistribution and use in source and binary forms, with or without 7251881Speter * modification, are permitted provided that the following conditions 8251881Speter * are met: 9251881Speter * 10251881Speter * 1. Redistributions of source code must retain the above copyright 11251881Speter * notice, this list of conditions and the following disclaimer. 12251881Speter * 13251881Speter * 2. Redistributions in binary form must reproduce the above copyright 14251881Speter * notice, this list of conditions and the following disclaimer in the 15251881Speter * documentation and/or other materials provided with the distribution. 16251881Speter * 17251881Speter * 3. Neither the name of the Institute nor the names of its contributors 18251881Speter * may be used to endorse or promote products derived from this software 19251881Speter * without specific prior written permission. 20251881Speter * 21251881Speter * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22251881Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23251881Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24251881Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25251881Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26251881Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27251881Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28251881Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29251881Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30251881Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31251881Speter * SUCH DAMAGE. 32251881Speter */ 33251881Speter 34251881Speter#include <config.h> 35251881Speter#include "roken.h" 36251881Speter 37251881Speter/** 38251881Speter * Constant time compare to memory regions. The reason for making it 39251881Speter * constant time is to make sure that timeing information leak from 40251881Speter * where in the function the diffrence is. 41251881Speter * 42251881Speter * ct_memcmp() can't be used to order memory regions like memcmp(), 43251881Speter * for example, use ct_memcmp() with qsort(). 44251881Speter * 45251881Speter * @param p1 memory region 1 to compare 46251881Speter * @param p2 memory region 2 to compare 47251881Speter * @param len length of memory 48251881Speter * 49251881Speter * @return 0 when the memory regions are equal, non zero if not 50251881Speter * 51251881Speter * @ingroup roken 52251881Speter */ 53251881Speter 54251881Speterint 55251881Speterct_memcmp(const void *p1, const void *p2, size_t len) 56251881Speter{ 57251881Speter const unsigned char *s1 = p1, *s2 = p2; 58251881Speter size_t i; 59251881Speter int r = 0; 60251881Speter 61251881Speter for (i = 0; i < len; i++) 62251881Speter r |= (s1[i] ^ s2[i]); 63251881Speter return !!r; 64251881Speter} 65251881Speter