/*1* Copyright (C) 2017 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6* Jean-Pierre FLORI <[email protected]>7*8* Contributors:9* Nicolas VIVET <[email protected]>10* Karim KHALFALLAH <[email protected]>11*12* This software is licensed under a dual BSD and GPL v2 license.13* See LICENSE file at the root folder of the project.14*/15#include <libecc/utils/utils.h>1617/*18* Return 1 in 'check' if first 'len' bytes of both buffers a and b are equal, 0 otherwise.19* It returns 0 if success, -1 on error. 'check' is only relevant on success.20*21* The test is done in constant time.22*/23int are_equal(const void *a, const void *b, u32 len, int *check)24{25const u8 *la = (const u8*)a, *lb = (const u8*)b;26int ret;27u32 i;2829MUST_HAVE((a != NULL) && (b != NULL) && (check != NULL), ret, err);3031*check = 1;32for (i = 0; i < len; i++) {33(*check) &= (*la == *lb);34la++;35lb++;36}3738ret = 0;3940err:41return ret;42}4344/*45* This function is a simple (non-optimized) reimplementation of memcpy()46* Returns 0 on success, -1 on error.47*/48int local_memcpy(void *dst, const void *src, u32 n)49{50const u8 *lsrc = (const u8*)src;51u8 *ldst = (u8*)dst;52u32 i;53int ret;5455MUST_HAVE((dst != NULL) && (src != NULL), ret, err);5657for (i = 0; i < n; i++) {58*ldst = *lsrc;59ldst++;60lsrc++;61}6263ret = 0;6465err:66return ret;67}6869/*70* This function is a simple (non-optimized) reimplementation of memset()71* Returns 0 on success, -1 on error.72*/73int local_memset(void *v, u8 c, u32 n)74{75volatile u8 *p = (volatile u8*)v;76u32 i;77int ret;7879MUST_HAVE((v != NULL), ret, err);8081for (i = 0; i < n; i++) {82*p = c;83p++;84}8586ret = 0;8788err:89return ret;90}9192/*93* Return 1 in 'check' if strings are equal, 0 otherwise.94* It returns 0 if success, -1 on error. 'check' is only relevant on success.95*96*/97int are_str_equal(const char *s1, const char *s2, int *check)98{99const char *ls1 = s1, *ls2 = s2;100int ret;101102MUST_HAVE((s1 != NULL) && (s2 != NULL) && (check != NULL), ret, err);103104while (*ls1 && (*ls1 == *ls2)) {105ls1++;106ls2++;107}108109(*check) = (*ls1 == *ls2);110111ret = 0;112113err:114return ret;115}116117/*118* Return 1 in 'check' if strings are equal up to maxlen, 0 otherwise.119* It returns 0 if success, -1 on error. 'check' is only relevant on success.120*121*/122int are_str_equal_nlen(const char *s1, const char *s2, u32 maxlen, int *check)123{124const char *ls1 = s1, *ls2 = s2;125u32 i = 0;126int ret;127128MUST_HAVE((s1 != NULL) && (s2 != NULL) && (check != NULL), ret, err);129130while (*ls1 && (*ls1 == *ls2) && (i < maxlen)) {131ls1++;132ls2++;133i++;134}135136(*check) = (*ls1 == *ls2);137ret = 0;138139err:140return ret;141}142143144145/*146* This function is a simple (non-optimized) reimplementation of strlen()147* Returns the lenth in 'len'.148* It returns 0 if success, -1 on error. 'len' is only relevant on success.149*/150int local_strlen(const char *s, u32 *len)151{152u32 i = 0;153int ret;154155MUST_HAVE((s != NULL) && (len != NULL), ret, err);156157while (s[i]) {158i++;159}160(*len) = i;161162ret = 0;163164err:165return ret;166}167168/*169* This function is a simple (non-optimized) reimplementation of strnlen()170* Returns the lenth in 'len'.171* It returns 0 if success, -1 on error. 'len' is only relevant on success.172*/173int local_strnlen(const char *s, u32 maxlen, u32 *len)174{175u32 i = 0;176int ret;177178MUST_HAVE((s != NULL) && (len != NULL), ret, err);179180while ((i < maxlen) && s[i]) {181i++;182}183(*len) = i;184185ret = 0;186187err:188return ret;189}190191/*192* This functin is a simple (non-optimized) reimplementation of strncpy()193*/194int local_strncpy(char *dst, const char *src, u32 n)195{196u32 i;197int ret;198199MUST_HAVE((dst != NULL) && (src != NULL), ret, err);200201for (i = 0; (i < n) && src[i]; i++) {202dst[i] = src[i];203}204for (; i < n; i++) {205dst[i] = 0;206}207208ret = 0;209err:210return ret;211}212213/*214* This functin is a simple (non-optimized) reimplementation of strncat()215*/216int local_strncat(char *dst, const char *src, u32 n)217{218u32 dst_len, i;219int ret;220221MUST_HAVE((dst != NULL) && (src != NULL), ret, err);222223ret = local_strlen(dst, &dst_len); EG(ret, err);224for (i = 0; (i < n) && src[i]; i++) {225dst[dst_len + i] = src[i];226}227dst[dst_len + i] = 0;228229ret = 0;230err:231return ret;232}233234235