1 /**
2  * @file mem/secure.c  Secure memory functions
3  *
4  * Copyright (C) 2010 Creytiv.com
5  */
6 
7 #include <re_types.h>
8 #include <re_mem.h>
9 
10 
11 /**
12  * Compare two byte strings in constant time. This function can be used
13  * by secure code to compare secret data, such as authentication tags,
14  * to avoid side-channel attacks.
15  *
16  * @param s1 First byte string
17  * @param s2 Second byte string
18  * @param n  Number of bytes
19  *
20  * @return a negative number if argument errors
21  *         0 if both byte strings matching
22  *         a positive number if not matching
23  */
mem_seccmp(const volatile uint8_t * volatile s1,const volatile uint8_t * volatile s2,size_t n)24 int mem_seccmp(const volatile uint8_t *volatile s1,
25 	       const volatile uint8_t *volatile s2,
26 	       size_t n)
27 {
28 	uint8_t val = 0;
29 
30 	if (!s1 || !s2)
31 		return -1;
32 
33 	while (n--)
34 		val |= *s1++ ^ *s2++;
35 
36 	return val;
37 }
38