| 1 |
/* |
| 2 |
* src/port/timingsafe_bcmp.c |
| 3 |
* |
| 4 |
* $OpenBSD: timingsafe_bcmp.c,v 1.3 2015/08/31 02:53:57 guenther Exp $ |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
* Copyright (c) 2010 Damien Miller. All rights reserved. |
| 9 |
* |
| 10 |
* Permission to use, copy, modify, and distribute this software for any |
| 11 |
* purpose with or without fee is hereby granted, provided that the above |
| 12 |
* copyright notice and this permission notice appear in all copies. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 15 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 16 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 17 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 18 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 19 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 20 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "c.h" |
| 24 |
|
| 25 |
#ifdef USE_SSL |
| 26 |
#include <openssl/crypto.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
int |
| 30 |
timingsafe_bcmp(const void *b1, const void *b2, size_t n) |
| 31 |
{ |
| 32 |
#ifdef USE_SSL |
| 33 |
return CRYPTO_memcmp(b1, b2, n); |
| 34 |
#else |
| 35 |
const unsigned char *p1 = b1, |
| 36 |
*p2 = b2; |
| 37 |
int ret = 0; |
| 38 |
|
| 39 |
for (; n > 0; n--) |
| 40 |
ret |= *p1++ ^ *p2++; |
| 41 |
return (ret != 0); |
| 42 |
#endif |
| 43 |
} |