Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constant time memcmp_ct function #3

Merged
1 commit merged into from
Jun 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/tee/tee_authenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <string_ext.h>
#include <tee/tee_authenc.h>
#include <tee/tee_cipher.h>
#include <tee/tee_mac.h>
Expand Down Expand Up @@ -413,7 +414,7 @@ TEE_Result tee_authenc_dec_final(
goto out;
}

if (memcmp(dst_tag, tag, tag_len) != 0)
if (buf_compare_ct(dst_tag, tag, tag_len) != 0)
res = TEE_ERROR_MAC_INVALID;
else
res = TEE_SUCCESS;
Expand Down
3 changes: 2 additions & 1 deletion core/tee/tee_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <tee/tee_hash.h>
#include <kernel/tee_core_trace.h>
#include "tee_ltc_wrapper.h"
#include <string_ext.h>

#define MAX_DIGEST 64

Expand Down Expand Up @@ -197,7 +198,7 @@ TEE_Result tee_hash_check(
if (res != TEE_SUCCESS)
return res;

if (memcmp(digest, hash, hash_size) != 0)
if (buf_compare_ct(digest, hash, hash_size) != 0)
return TEE_ERROR_SECURITY;

return TEE_SUCCESS;
Expand Down
11 changes: 6 additions & 5 deletions core/tee/tee_rpmb.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <string_ext.h>
#include <kernel/tee_core_trace.h>
#include <tee_api_types.h>
#include <kernel/tee_common_otp.h>
Expand Down Expand Up @@ -657,8 +658,8 @@ static TEE_Result tee_rpmb_resp_unpack_verify(struct rpmb_data_frame *datafrm,
}

if (rawdata->nonce != NULL) {
if (memcmp(rawdata->nonce, lastfrm.nonce, RPMB_NONCE_SIZE)
!= 0) {
if (buf_compare_ct(rawdata->nonce, lastfrm.nonce,
RPMB_NONCE_SIZE) != 0) {
DMSG("%s: nonce mismatched", __func__);
return TEE_ERROR_SECURITY;
}
Expand Down Expand Up @@ -688,9 +689,9 @@ static TEE_Result tee_rpmb_resp_unpack_verify(struct rpmb_data_frame *datafrm,
return res;
}

if (memcmp(rawdata->key_mac,
(datafrm + nbr_frms - 1)->key_mac,
RPMB_KEY_MAC_SIZE) != 0) {
if (buf_compare_ct(rawdata->key_mac,
(datafrm + nbr_frms - 1)->key_mac,
RPMB_KEY_MAC_SIZE) != 0) {
DMSG("%s: MAC mismatched:", __func__);
#ifdef ENABLE_RPMB_DATA_DUMP
HEX_PRINT_BUF((uint8_t *)rawdata->key_mac, 32);
Expand Down
42 changes: 42 additions & 0 deletions lib/libutils/ext/buf_compare_ct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2014, Linaro Limited
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <string_ext.h>

int buf_compare_ct(const void *s1, const void *s2, size_t n)
{
int res = 0;
unsigned char *c1 = (unsigned char *)s1;
unsigned char *c2 = (unsigned char *)s2;

while (n--) {
res |= (*c1 ^ *c2);
c1++;
c2++;
}

return res;
}
10 changes: 10 additions & 0 deletions lib/libutils/ext/include/string_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@
size_t strlcpy(char *dst, const char *src, size_t size);
size_t strlcat(char *dst, const char *src, size_t size);

/*
* This memory compare function will compare two buffers in a constant time.
*
* Note that this function will not have same kind of return values as the
* traditional libc memcmp which return either less than or greater than zero
* depending on which string that is lexically greater. This function will
* return 0 if it is a match, otherwise it will return a non-zero value.
*/
int buf_compare_ct(const void *s1, const void *s2, size_t n);

#endif /* STRING_EXT_H */
1 change: 1 addition & 0 deletions lib/libutils/ext/sub.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ global-incdirs-y += include

srcs-y += strlcat.c
srcs-y += strlcpy.c
srcs-y += buf_compare_ct.c