From 2f99855cd4329e3377b024bf9a6f3486a319f9e4 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sun, 27 Aug 2017 07:46:34 -0400 Subject: [PATCH] Remove non-standard host-endian functions. --- core/c/mpw-algorithm_v0.c | 35 ++++++++++++++++--------------- core/c/mpw-algorithm_v2.c | 10 ++++----- core/c/mpw-algorithm_v3.c | 4 ++-- core/c/mpw-types.c | 4 ++-- core/c/mpw-types.h | 2 +- core/c/mpw-util.c | 43 +++++++++++++++++++++++++++++++++------ core/c/mpw-util.h | 5 +++++ 7 files changed, 71 insertions(+), 32 deletions(-) diff --git a/core/c/mpw-algorithm_v0.c b/core/c/mpw-algorithm_v0.c index 79d21ff0..7828bf30 100644 --- a/core/c/mpw-algorithm_v0.c +++ b/core/c/mpw-algorithm_v0.c @@ -28,22 +28,22 @@ #define MP_p 2U // Algorithm version helpers. -static const char *mpw_templateForType_v0(MPResultType type, uint16_t seedByte) { +static const char *mpw_templateForType_v0(MPResultType type, uint16_t templateIndex) { size_t count = 0; const char **templates = mpw_templatesForType( type, &count ); - char const *template = templates && count? templates[seedByte % count]: NULL; + char const *template = templates && count? templates[templateIndex % count]: NULL; free( templates ); return template; } -static const char mpw_characterFromClass_v0(char characterClass, uint16_t seedByte) { +static const char mpw_characterFromClass_v0(char characterClass, uint16_t classIndex) { const char *classCharacters = mpw_charactersInClass( characterClass ); if (!classCharacters) return '\0'; - return classCharacters[seedByte % strlen( classCharacters )]; + return classCharacters[classIndex % strlen( classCharacters )]; } // Algorithm version overrides. @@ -55,11 +55,11 @@ static MPMasterKey mpw_masterKey_v0( // Calculate the master key salt. trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s\n", - keyScope, mpw_hex_l( htonl( mpw_utf8_strlen( fullName ) ) ), fullName ); + keyScope, mpw_hex_l( mpw_utf8_strlen( fullName ) ), fullName ); size_t masterKeySaltSize = 0; uint8_t *masterKeySalt = NULL; mpw_push_string( &masterKeySalt, &masterKeySaltSize, keyScope ); - mpw_push_int( &masterKeySalt, &masterKeySaltSize, htonl( mpw_utf8_strlen( fullName ) ) ); + mpw_push_int( &masterKeySalt, &masterKeySaltSize, mpw_utf8_strlen( fullName ) ); mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName ); if (!masterKeySalt) { err( "Could not allocate master key salt: %s\n", strerror( errno ) ); @@ -91,16 +91,16 @@ static MPSiteKey mpw_siteKey_v0( // Calculate the site seed. trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s\n", - keyScope, mpw_hex_l( htonl( mpw_utf8_strlen( siteName ) ) ), siteName, mpw_hex_l( htonl( siteCounter ) ), - keyContext? mpw_hex_l( htonl( mpw_utf8_strlen( keyContext ) ) ): NULL, keyContext ); + keyScope, mpw_hex_l( mpw_utf8_strlen( siteName ) ), siteName, mpw_hex_l( siteCounter ), + keyContext? mpw_hex_l( mpw_utf8_strlen( keyContext ) ): NULL, keyContext ); size_t siteSaltSize = 0; uint8_t *siteSalt = NULL; mpw_push_string( &siteSalt, &siteSaltSize, keyScope ); - mpw_push_int( &siteSalt, &siteSaltSize, htonl( mpw_utf8_strlen( siteName ) ) ); + mpw_push_int( &siteSalt, &siteSaltSize, mpw_utf8_strlen( siteName ) ); mpw_push_string( &siteSalt, &siteSaltSize, siteName ); - mpw_push_int( &siteSalt, &siteSaltSize, htonl( siteCounter ) ); + mpw_push_int( &siteSalt, &siteSaltSize, siteCounter ); if (keyContext) { - mpw_push_int( &siteSalt, &siteSaltSize, htonl( mpw_utf8_strlen( keyContext ) ) ); + mpw_push_int( &siteSalt, &siteSaltSize, mpw_utf8_strlen( keyContext ) ); mpw_push_string( &siteSalt, &siteSaltSize, keyContext ); } if (!siteSalt) { @@ -127,8 +127,10 @@ static const char *mpw_sitePasswordFromTemplate_v0( // Determine the template. const char *_siteKey = (const char *)siteKey; - const char *template = mpw_templateForType_v0( resultType, htons( _siteKey[0] ) ); - trc( "template: %u => %s\n", htons( _siteKey[0] ), template ); + uint16_t seedByte; + mpw_uint16( (uint16_t)_siteKey[0], (uint8_t *)&seedByte ); + const char *template = mpw_templateForType_v0( resultType, seedByte ); + trc( "template: %u => %s\n", seedByte, template ); if (!template) return NULL; if (strlen( template ) > MPSiteKeySize) { @@ -137,11 +139,12 @@ static const char *mpw_sitePasswordFromTemplate_v0( } // Encode the password from the seed using the template. - char *sitePassword = calloc( strlen( template ) + 1, sizeof( char ) ); + char *const sitePassword = calloc( strlen( template ) + 1, sizeof( char ) ); for (size_t c = 0; c < strlen( template ); ++c) { - sitePassword[c] = mpw_characterFromClass_v0( template[c], htons( _siteKey[c + 1] ) ); + mpw_uint16( (uint16_t)_siteKey[c + 1], (uint8_t *)&seedByte ); + sitePassword[c] = mpw_characterFromClass_v0( template[c], seedByte ); trc( " - class: %c, index: %5u (0x%02hX) => character: %c\n", - template[c], htons( _siteKey[c + 1] ), htons( _siteKey[c + 1] ), sitePassword[c] ); + template[c], seedByte, seedByte, sitePassword[c] ); } trc( " => password: %s\n", sitePassword ); diff --git a/core/c/mpw-algorithm_v2.c b/core/c/mpw-algorithm_v2.c index f6d875a6..3c1dacd2 100644 --- a/core/c/mpw-algorithm_v2.c +++ b/core/c/mpw-algorithm_v2.c @@ -57,16 +57,16 @@ static MPSiteKey mpw_siteKey_v2( // Calculate the site seed. trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s\n", - keyScope, mpw_hex_l( htonl( strlen( siteName ) ) ), siteName, mpw_hex_l( htonl( siteCounter ) ), - keyContext? mpw_hex_l( htonl( strlen( keyContext ) ) ): NULL, keyContext ); + keyScope, mpw_hex_l( strlen( siteName ) ), siteName, mpw_hex_l( siteCounter ), + keyContext? mpw_hex_l( strlen( keyContext ) ): NULL, keyContext ); size_t siteSaltSize = 0; uint8_t *siteSalt = NULL; mpw_push_string( &siteSalt, &siteSaltSize, keyScope ); - mpw_push_int( &siteSalt, &siteSaltSize, htonl( strlen( siteName ) ) ); + mpw_push_int( &siteSalt, &siteSaltSize, strlen( siteName ) ); mpw_push_string( &siteSalt, &siteSaltSize, siteName ); - mpw_push_int( &siteSalt, &siteSaltSize, htonl( siteCounter ) ); + mpw_push_int( &siteSalt, &siteSaltSize, siteCounter ); if (keyContext) { - mpw_push_int( &siteSalt, &siteSaltSize, htonl( strlen( keyContext ) ) ); + mpw_push_int( &siteSalt, &siteSaltSize, strlen( keyContext ) ); mpw_push_string( &siteSalt, &siteSaltSize, keyContext ); } if (!siteSalt) { diff --git a/core/c/mpw-algorithm_v3.c b/core/c/mpw-algorithm_v3.c index 1afbe5d8..c8f557b9 100644 --- a/core/c/mpw-algorithm_v3.c +++ b/core/c/mpw-algorithm_v3.c @@ -49,11 +49,11 @@ static MPMasterKey mpw_masterKey_v3( // Calculate the master key salt. trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s\n", - keyScope, mpw_hex_l( htonl( strlen( fullName ) ) ), fullName ); + keyScope, mpw_hex_l( strlen( fullName ) ), fullName ); size_t masterKeySaltSize = 0; uint8_t *masterKeySalt = NULL; mpw_push_string( &masterKeySalt, &masterKeySaltSize, keyScope ); - mpw_push_int( &masterKeySalt, &masterKeySaltSize, htonl( strlen( fullName ) ) ); + mpw_push_int( &masterKeySalt, &masterKeySaltSize, strlen( fullName ) ); mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName ); if (!masterKeySalt) { err( "Could not allocate master key salt: %s\n", strerror( errno ) ); diff --git a/core/c/mpw-types.c b/core/c/mpw-types.c index 7d253c22..a6cdc218 100644 --- a/core/c/mpw-types.c +++ b/core/c/mpw-types.c @@ -170,11 +170,11 @@ const char **mpw_templatesForType(MPResultType type, size_t *count) { } } -const char *mpw_templateForType(MPResultType type, uint8_t seedByte) { +const char *mpw_templateForType(MPResultType type, uint8_t templateIndex) { size_t count = 0; const char **templates = mpw_templatesForType( type, &count ); - char const *template = templates && count? templates[seedByte % count]: NULL; + char const *template = templates && count? templates[templateIndex % count]: NULL; free( templates ); return template; diff --git a/core/c/mpw-types.h b/core/c/mpw-types.h index 45ff7c0e..c19cadad 100644 --- a/core/c/mpw-types.h +++ b/core/c/mpw-types.h @@ -142,7 +142,7 @@ const char **mpw_templatesForType(MPResultType type, size_t *count); * @return An internal string that contains the password encoding template of the given type * for a seed that starts with the given byte. */ -const char *mpw_templateForType(MPResultType type, uint8_t seedByte); +const char *mpw_templateForType(MPResultType type, uint8_t templateIndex); /** * @return An internal string that contains all the characters that occur in the given character class. diff --git a/core/c/mpw-util.c b/core/c/mpw-util.c index 592d511b..dd50a6c7 100644 --- a/core/c/mpw-util.c +++ b/core/c/mpw-util.c @@ -39,6 +39,32 @@ int mpw_verbosity = inf_level; #endif +void mpw_uint16(const uint16_t number, uint8_t buf[2]) { + + buf[0] = (uint8_t)((number >> 8L) & UINT8_MAX); + buf[1] = (uint8_t)((number >> 0L) & UINT8_MAX); +} + +void mpw_uint32(const uint32_t number, uint8_t buf[4]) { + + buf[0] = (uint8_t)((number >> 24) & UINT8_MAX); + buf[1] = (uint8_t)((number >> 16) & UINT8_MAX); + buf[2] = (uint8_t)((number >> 8L) & UINT8_MAX); + buf[3] = (uint8_t)((number >> 0L) & UINT8_MAX); +} + +void mpw_uint64(const uint64_t number, uint8_t buf[8]) { + + buf[0] = (uint8_t)((number >> 56) & UINT8_MAX); + buf[1] = (uint8_t)((number >> 48) & UINT8_MAX); + buf[2] = (uint8_t)((number >> 40) & UINT8_MAX); + buf[3] = (uint8_t)((number >> 32) & UINT8_MAX); + buf[4] = (uint8_t)((number >> 24) & UINT8_MAX); + buf[5] = (uint8_t)((number >> 16) & UINT8_MAX); + buf[6] = (uint8_t)((number >> 8L) & UINT8_MAX); + buf[7] = (uint8_t)((number >> 0L) & UINT8_MAX); +} + bool mpw_push_buf(uint8_t **buffer, size_t *bufferSize, const void *pushBuffer, const size_t pushSize) { if (!buffer || !bufferSize || !pushBuffer || !pushSize) @@ -87,7 +113,9 @@ bool mpw_string_pushf(char **string, const char *pushFormat, ...) { bool mpw_push_int(uint8_t **buffer, size_t *bufferSize, const uint32_t pushInt) { - return mpw_push_buf( buffer, bufferSize, &pushInt, sizeof( pushInt ) ); + uint8_t pushBuf[4 /* 32 / 8 */]; + mpw_uint32( pushInt, pushBuf ); + return mpw_push_buf( buffer, bufferSize, &pushBuf, sizeof( pushBuf ) ); } bool __mpw_realloc(const void **buffer, size_t *bufferSize, const size_t deltaSize) { @@ -188,10 +216,8 @@ uint8_t const *mpw_kdf_blake2b(const size_t subkeySize, const uint8_t *key, cons uint8_t saltBuf[crypto_generichash_blake2b_SALTBYTES]; bzero( saltBuf, sizeof saltBuf ); - if (id) { - uint64_t id_n = htonll( id ); - memcpy( saltBuf, &id_n, sizeof id_n ); - } + if (id) + mpw_uint64( id, saltBuf ); uint8_t personalBuf[crypto_generichash_blake2b_PERSONALBYTES]; bzero( personalBuf, sizeof saltBuf ); @@ -349,7 +375,12 @@ const char *mpw_hex(const void *buf, size_t length) { const char *mpw_hex_l(uint32_t number) { - return mpw_hex( &number, sizeof( number ) ); + uint8_t buf[4 /* 32 / 8 */]; + buf[0] = (uint8_t)((number >> 24) & UINT8_MAX); + buf[1] = (uint8_t)((number >> 16) & UINT8_MAX); + buf[2] = (uint8_t)((number >> 8L) & UINT8_MAX); + buf[3] = (uint8_t)((number >> 0L) & UINT8_MAX); + return mpw_hex( &buf, sizeof( buf ) ); } #ifdef COLOR diff --git a/core/c/mpw-util.h b/core/c/mpw-util.h index c40d59a9..f8c7cf28 100644 --- a/core/c/mpw-util.h +++ b/core/c/mpw-util.h @@ -94,6 +94,11 @@ extern int mpw_verbosity; //// Buffers and memory. +/** Write a number to a byte buffer using mpw's endianness (big/network endian). */ +void mpw_uint16(const uint16_t number, uint8_t buf[2]); +void mpw_uint32(const uint32_t number, uint8_t buf[4]); +void mpw_uint64(const uint64_t number, uint8_t buf[8]); + /** Allocate a new array of _type, assign its element count to _count if not NULL and populate it with the varargs. */ #define mpw_alloc_array(_count, _type, ...) ({ \ _type stackElements[] = { __VA_ARGS__ }; \