diff --git a/lib/bitlk/bitlk.c b/lib/bitlk/bitlk.c index 308abd53..b331f9e0 100644 --- a/lib/bitlk/bitlk.c +++ b/lib/bitlk/bitlk.c @@ -982,6 +982,7 @@ static int bitlk_kdf(const char *password, struct crypt_hash *hd = NULL; int len = 0; char16_t *utf16Password = NULL; + size_t utf16Len = 0; int i = 0; int r = 0; @@ -1007,7 +1008,8 @@ static int bitlk_kdf(const char *password, if (r < 0) goto out; - crypt_hash_write(hd, (char*)utf16Password, passwordLen * 2); + utf16Len = crypt_char16_strlen(utf16Password); + crypt_hash_write(hd, (char*)utf16Password, utf16Len * 2); r = crypt_hash_final(hd, kdf.initial_sha256, len); if (r < 0) goto out; diff --git a/lib/crypto_backend/crypto_backend.h b/lib/crypto_backend/crypto_backend.h index c2a66db3..9c37cf12 100644 --- a/lib/crypto_backend/crypto_backend.h +++ b/lib/crypto_backend/crypto_backend.h @@ -93,6 +93,7 @@ int crypt_base64_decode(char **out, size_t *out_length, const char *in, size_t i /* UTF8/16 */ int crypt_utf16_to_utf8(char **out, const char16_t *s, size_t length /* bytes! */); int crypt_utf8_to_utf16(char16_t **out, const char *s, size_t length); +size_t crypt_char16_strlen(const char16_t *s); /* Block ciphers */ int crypt_cipher_ivsize(const char *name, const char *mode); diff --git a/lib/crypto_backend/utf8.c b/lib/crypto_backend/utf8.c index 4829dc1d..c68d1b7d 100644 --- a/lib/crypto_backend/utf8.c +++ b/lib/crypto_backend/utf8.c @@ -274,3 +274,20 @@ int crypt_utf8_to_utf16(char16_t **out, const char *s, size_t length) *p = 0; return 0; } + +/** + * crypt_char16_strlen() + * @s: string to get length of + * + * Returns: number of 16-bit words in the string + */ +size_t crypt_char16_strlen(const char16_t *s) { + size_t n = 0; + + assert(s); + + while (*s != 0) + n++, s++; + + return n; +}