Add memutils.c for backend and move existing mem helpers there.

Also remove inline definitions.
This commit is contained in:
Milan Broz
2024-04-24 15:24:44 +02:00
parent bede116926
commit aae5cba2b9
5 changed files with 51 additions and 22 deletions

View File

@@ -13,7 +13,8 @@ libcrypto_backend_la_SOURCES = \
lib/crypto_backend/utf8.c \ lib/crypto_backend/utf8.c \
lib/crypto_backend/argon2_generic.c \ lib/crypto_backend/argon2_generic.c \
lib/crypto_backend/cipher_generic.c \ lib/crypto_backend/cipher_generic.c \
lib/crypto_backend/cipher_check.c lib/crypto_backend/cipher_check.c \
lib/crypto_backend/memutils.c
if CRYPTO_BACKEND_GCRYPT if CRYPTO_BACKEND_GCRYPT
libcrypto_backend_la_SOURCES += lib/crypto_backend/crypto_gcrypt.c libcrypto_backend_la_SOURCES += lib/crypto_backend/crypto_gcrypt.c

View File

@@ -144,15 +144,7 @@ int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
const char *tag, size_t tag_length); const char *tag, size_t tag_length);
/* Memzero helper (memset on stack can be optimized out) */ /* Memzero helper (memset on stack can be optimized out) */
static inline void crypt_backend_memzero(void *s, size_t n) void crypt_backend_memzero(void *s, size_t n);
{
#ifdef HAVE_EXPLICIT_BZERO
explicit_bzero(s, n);
#else
volatile uint8_t *p = (volatile uint8_t *)s;
while(n--) *p++ = 0;
#endif
}
/* Memcmp helper (memcmp in constant time) */ /* Memcmp helper (memcmp in constant time) */
int crypt_backend_memeq(const void *m1, const void *m2, size_t n); int crypt_backend_memeq(const void *m1, const void *m2, size_t n);

View File

@@ -59,17 +59,6 @@ int crypt_bitlk_decrypt_key_kernel(const void *key, size_t key_length,
const char *tag, size_t tag_length); const char *tag, size_t tag_length);
/* Internal implementation for constant time memory comparison */ /* Internal implementation for constant time memory comparison */
static inline int crypt_internal_memeq(const void *m1, const void *m2, size_t n) int crypt_internal_memeq(const void *m1, const void *m2, size_t n);
{
const unsigned char *_m1 = (const unsigned char *) m1;
const unsigned char *_m2 = (const unsigned char *) m2;
unsigned char result = 0;
size_t i;
for (i = 0; i < n; i++)
result |= _m1[i] ^ _m2[i];
return result;
}
#endif /* _CRYPTO_BACKEND_INTERNAL_H */ #endif /* _CRYPTO_BACKEND_INTERNAL_H */

View File

@@ -0,0 +1,46 @@
/*
* Safe memory utilities
*
* Copyright (C) 2024 Milan Broz
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "crypto_backend_internal.h"
/* Memzero helper (memset on stack can be optimized out) */
void crypt_backend_memzero(void *s, size_t n)
{
#ifdef HAVE_EXPLICIT_BZERO
explicit_bzero(s, n);
#else
volatile uint8_t *p = (volatile uint8_t *)s;
while(n--) *p++ = 0;
#endif
}
/* Internal implementation for constant time memory comparison */
int crypt_internal_memeq(const void *m1, const void *m2, size_t n)
{
const unsigned char *_m1 = (const unsigned char *) m1;
const unsigned char *_m2 = (const unsigned char *) m2;
unsigned char result = 0;
size_t i;
for (i = 0; i < n; i++)
result |= _m1[i] ^ _m2[i];
return result;
}

View File

@@ -11,6 +11,7 @@ libcrypto_backend_link_with = []
libcrypto_backend_sources = files( libcrypto_backend_sources = files(
'argon2_generic.c', 'argon2_generic.c',
'base64.c', 'base64.c',
'memutils.c',
'cipher_check.c', 'cipher_check.c',
'cipher_generic.c', 'cipher_generic.c',
'crc32.c', 'crc32.c',