mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-05 16:00:05 +01:00
Add constant time crypt_bytes_to_hex helper and use it in libdevmapper.
Fixes: #736
This commit is contained in:
@@ -183,6 +183,11 @@ static int hex_to_bin(unsigned char ch)
|
||||
((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
|
||||
}
|
||||
|
||||
static char hex2asc(unsigned char c)
|
||||
{
|
||||
return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27);
|
||||
}
|
||||
|
||||
ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
|
||||
{
|
||||
char *bytes;
|
||||
@@ -214,6 +219,32 @@ ssize_t crypt_hex_to_bytes(const char *hex, char **result, int safe_alloc)
|
||||
return i;
|
||||
}
|
||||
|
||||
char *crypt_bytes_to_hex(size_t size, const char *bytes)
|
||||
{
|
||||
unsigned i;
|
||||
char *hex;
|
||||
|
||||
if (size && !bytes)
|
||||
return NULL;
|
||||
|
||||
/* Alloc adds trailing \0 */
|
||||
if (size == 0)
|
||||
hex = crypt_safe_alloc(2);
|
||||
else
|
||||
hex = crypt_safe_alloc(size * 2 + 1);
|
||||
if (!hex)
|
||||
return NULL;
|
||||
|
||||
if (size == 0)
|
||||
hex[0] = '-';
|
||||
else for (i = 0; i < size; i++) {
|
||||
hex[i * 2] = hex2asc((const unsigned char)bytes[i] >> 4);
|
||||
hex[i * 2 + 1] = hex2asc((const unsigned char)bytes[i] & 0xf);
|
||||
}
|
||||
|
||||
return hex;
|
||||
}
|
||||
|
||||
bool crypt_is_cipher_null(const char *cipher_spec)
|
||||
{
|
||||
if (!cipher_spec)
|
||||
|
||||
Reference in New Issue
Block a user