From cdeee2576dd4c044f54bd2ce298022450a5aed36 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Fri, 27 Feb 2015 08:49:04 -0500 Subject: [PATCH] More algorithm trace logging. --- MasterPassword/C/mpw-algorithm_v0.c | 6 +++++- MasterPassword/C/mpw-algorithm_v1.c | 6 +++++- MasterPassword/C/mpw-algorithm_v2.c | 6 +++++- MasterPassword/C/mpw-algorithm_v3.c | 6 +++++- MasterPassword/C/mpw-util.c | 21 ++++++++++++++++----- MasterPassword/C/mpw-util.h | 1 + 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/MasterPassword/C/mpw-algorithm_v0.c b/MasterPassword/C/mpw-algorithm_v0.c index fb9f46a1..1878772f 100644 --- a/MasterPassword/C/mpw-algorithm_v0.c +++ b/MasterPassword/C/mpw-algorithm_v0.c @@ -78,7 +78,11 @@ static const char *mpw_passwordForSite_v0(const uint8_t *masterKey, const char * trc( "siteCounter: %d\n", siteCounter ); trc( "siteVariant: %d\n", siteVariant ); trc( "siteType: %d\n", siteType ); - trc( "site scope: %s, context: %s\n", siteScope, siteContext == NULL? "": siteContext ); + trc( "site scope: %s, context: %s\n", siteScope, siteContext? "": siteContext ); + trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)\n", + siteScope, mpw_hex_l( htonl( strlen( siteName ) ) ), siteName, + mpw_hex_l( htonl( siteCounter ) ), + mpw_hex_l( htonl( siteContext? strlen( siteContext ): 0 ) ), siteContext? "(null)": siteContext ); // Calculate the site seed. // sitePasswordSeed = hmac-sha256( masterKey, siteScope . #siteName . siteName . siteCounter . #siteContext . siteContext ) diff --git a/MasterPassword/C/mpw-algorithm_v1.c b/MasterPassword/C/mpw-algorithm_v1.c index 55159e7e..74923b9f 100644 --- a/MasterPassword/C/mpw-algorithm_v1.c +++ b/MasterPassword/C/mpw-algorithm_v1.c @@ -62,7 +62,11 @@ static const char *mpw_passwordForSite_v1(const uint8_t *masterKey, const char * trc( "siteCounter: %d\n", siteCounter ); trc( "siteVariant: %d\n", siteVariant ); trc( "siteType: %d\n", siteType ); - trc( "site scope: %s, context: %s\n", siteScope, siteContext == NULL? "": siteContext ); + trc( "site scope: %s, context: %s\n", siteScope, siteContext? "": siteContext ); + trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)\n", + siteScope, mpw_hex_l( htonl( strlen( siteName ) ) ), siteName, + mpw_hex_l( htonl( siteCounter ) ), + mpw_hex_l( htonl( siteContext? strlen( siteContext ): 0 ) ), siteContext? "(null)": siteContext ); // Calculate the site seed. // sitePasswordSeed = hmac-sha256( masterKey, siteScope . #siteName . siteName . siteCounter . #siteContext . siteContext ) diff --git a/MasterPassword/C/mpw-algorithm_v2.c b/MasterPassword/C/mpw-algorithm_v2.c index 2d019dbe..123d5e66 100644 --- a/MasterPassword/C/mpw-algorithm_v2.c +++ b/MasterPassword/C/mpw-algorithm_v2.c @@ -62,7 +62,11 @@ static const char *mpw_passwordForSite_v2(const uint8_t *masterKey, const char * trc( "siteCounter: %d\n", siteCounter ); trc( "siteVariant: %d\n", siteVariant ); trc( "siteType: %d\n", siteType ); - trc( "site scope: %s, context: %s\n", siteScope, siteContext == NULL? "": siteContext ); + trc( "site scope: %s, context: %s\n", siteScope, siteContext? "": siteContext ); + trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)\n", + siteScope, mpw_hex_l( htonl( strlen( siteName ) ) ), siteName, + mpw_hex_l( htonl( siteCounter ) ), + mpw_hex_l( htonl( siteContext? strlen( siteContext ): 0 ) ), siteContext? "(null)": siteContext ); // Calculate the site seed. // sitePasswordSeed = hmac-sha256( masterKey, siteScope . #siteName . siteName . siteCounter . #siteContext . siteContext ) diff --git a/MasterPassword/C/mpw-algorithm_v3.c b/MasterPassword/C/mpw-algorithm_v3.c index bfb22532..a9ed37e0 100644 --- a/MasterPassword/C/mpw-algorithm_v3.c +++ b/MasterPassword/C/mpw-algorithm_v3.c @@ -62,7 +62,11 @@ static const char *mpw_passwordForSite_v3(const uint8_t *masterKey, const char * trc( "siteCounter: %d\n", siteCounter ); trc( "siteVariant: %d\n", siteVariant ); trc( "siteType: %d\n", siteType ); - trc( "site scope: %s, context: %s\n", siteScope, siteContext == NULL? "": siteContext ); + trc( "site scope: %s, context: %s\n", siteScope, siteContext? "": siteContext ); + trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)\n", + siteScope, mpw_hex_l( htonl( strlen( siteName ) ) ), siteName, + mpw_hex_l( htonl( siteCounter ) ), + mpw_hex_l( htonl( siteContext? strlen( siteContext ): 0 ) ), siteContext? "(null)": siteContext ); // Calculate the site seed. // sitePasswordSeed = hmac-sha256( masterKey, siteScope . #siteName . siteName . siteCounter . #siteContext . siteContext ) diff --git a/MasterPassword/C/mpw-util.c b/MasterPassword/C/mpw-util.c index 3bc072cb..b50ae37b 100644 --- a/MasterPassword/C/mpw-util.c +++ b/MasterPassword/C/mpw-util.c @@ -96,14 +96,25 @@ const char *mpw_idForBuf(const void *buf, size_t length) { return mpw_hex( hash, 32 ); } -static char *mpw_hex_buf = NULL; +static char **mpw_hex_buf = NULL; +static unsigned int mpw_hex_buf_i = 0; const char *mpw_hex(const void *buf, size_t length) { - mpw_hex_buf = realloc( mpw_hex_buf, length * 2 + 1 ); - for (size_t kH = 0; kH < length; kH++) - sprintf( &(mpw_hex_buf[kH * 2]), "%02X", ((const uint8_t *)buf)[kH] ); + if (!mpw_hex_buf) { + mpw_hex_buf = malloc( 10 * sizeof( char* ) ); + for (uint8_t i = 0; i < 10; ++i) + mpw_hex_buf[i] = NULL; + } + mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10; - return mpw_hex_buf; + mpw_hex_buf[mpw_hex_buf_i] = realloc( mpw_hex_buf[mpw_hex_buf_i], length * 2 + 1 ); + for (size_t kH = 0; kH < length; kH++) + sprintf( &(mpw_hex_buf[mpw_hex_buf_i][kH * 2]), "%02X", ((const uint8_t *)buf)[kH] ); + + return mpw_hex_buf[mpw_hex_buf_i]; +} +const char *mpw_hex_l(uint32_t number) { + return mpw_hex( &number, sizeof( number ) ); } #ifdef COLOR diff --git a/MasterPassword/C/mpw-util.h b/MasterPassword/C/mpw-util.h index 8c5db151..815706ad 100644 --- a/MasterPassword/C/mpw-util.h +++ b/MasterPassword/C/mpw-util.h @@ -66,6 +66,7 @@ uint8_t const *mpw_hmac_sha256( /** Encode a buffer as a string of hexadecimal characters. * @return A C-string in a reused buffer, do not free or store it. */ const char *mpw_hex(const void *buf, size_t length); +const char *mpw_hex_l(uint32_t number); /** Encode a fingerprint for a buffer. * @return A C-string in a reused buffer, do not free or store it. */ const char *mpw_idForBuf(const void *buf, size_t length);