From 6f4f6b8d1e8a00bcd3b331ad208c74482aa62eb2 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Wed, 30 Aug 2017 09:54:16 -0400 Subject: [PATCH] Copy TOTP counter support to v2 override. --- core/c/mpw-algorithm_v0.c | 7 ++++--- core/c/mpw-algorithm_v1.c | 11 +++++++---- core/c/mpw-algorithm_v2.c | 8 ++++++-- core/c/mpw-algorithm_v3.c | 3 ++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/core/c/mpw-algorithm_v0.c b/core/c/mpw-algorithm_v0.c index 8d6e338d..b98b474f 100644 --- a/core/c/mpw-algorithm_v0.c +++ b/core/c/mpw-algorithm_v0.c @@ -73,7 +73,7 @@ static MPMasterKey mpw_masterKey_v0( MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p ); mpw_free( &masterKeySalt, masterKeySaltSize ); if (!masterKey) { - err( "Could not allocate master key: %s\n", strerror( errno ) ); + err( "Could not derive master key: %s\n", strerror( errno ) ); return NULL; } trc( " => masterKey.id: %s\n", mpw_id_buf( masterKey, MPMasterKeySize ) ); @@ -90,7 +90,7 @@ static MPSiteKey mpw_siteKey_v0( // OTP counter value. if (siteCounter == MPCounterValueTOTP) - siteCounter = ((uint32_t)time(NULL) / MP_otp_window) * MP_otp_window; + siteCounter = ((uint32_t)time( NULL ) / MP_otp_window) * MP_otp_window; // Calculate the site seed. trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s\n", @@ -128,8 +128,9 @@ static MPSiteKey mpw_siteKey_v0( static const char *mpw_sitePasswordFromTemplate_v0( MPMasterKey __unused masterKey, MPSiteKey siteKey, MPResultType resultType, const char __unused *resultParam) { - // Determine the template. const char *_siteKey = (const char *)siteKey; + + // Determine the template. uint16_t seedByte; mpw_uint16( (uint16_t)_siteKey[0], (uint8_t *)&seedByte ); const char *template = mpw_templateForType_v0( resultType, seedByte ); diff --git a/core/c/mpw-algorithm_v1.c b/core/c/mpw-algorithm_v1.c index 2c898f2e..c5ed13be 100644 --- a/core/c/mpw-algorithm_v1.c +++ b/core/c/mpw-algorithm_v1.c @@ -23,6 +23,7 @@ #define MP_N 32768LU #define MP_r 8U #define MP_p 2U +#define MP_otp_window 5 * 60 /* s */ // Inherited functions. MPMasterKey mpw_masterKey_v0( @@ -55,8 +56,9 @@ static const char *mpw_sitePasswordFromTemplate_v1( MPMasterKey __unused masterKey, MPSiteKey siteKey, MPResultType resultType, const char __unused *resultParam) { // Determine the template. - const char *template = mpw_templateForType( resultType, siteKey[0] ); - trc( "template: %u => %s\n", siteKey[0], template ); + uint8_t seedByte = siteKey[0]; + const char *template = mpw_templateForType( resultType, seedByte ); + trc( "template: %u => %s\n", seedByte, template ); if (!template) return NULL; if (strlen( template ) > MPSiteKeySize) { @@ -67,9 +69,10 @@ static const char *mpw_sitePasswordFromTemplate_v1( // Encode the password from the seed using the template. char *const sitePassword = calloc( strlen( template ) + 1, sizeof( char ) ); for (size_t c = 0; c < strlen( template ); ++c) { - sitePassword[c] = mpw_characterFromClass( template[c], siteKey[c + 1] ); + seedByte = siteKey[c + 1]; + sitePassword[c] = mpw_characterFromClass( template[c], seedByte ); trc( " - class: %c, index: %3u (0x%02hhX) => character: %c\n", - template[c], siteKey[c + 1], 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 be466dfa..d622884e 100644 --- a/core/c/mpw-algorithm_v2.c +++ b/core/c/mpw-algorithm_v2.c @@ -18,12 +18,14 @@ #include #include +#include #include "mpw-util.h" #define MP_N 32768LU #define MP_r 8U #define MP_p 2U +#define MP_otp_window 5 * 60 /* s */ // Inherited functions. MPMasterKey mpw_masterKey_v1( @@ -51,7 +53,9 @@ static MPSiteKey mpw_siteKey_v2( const char *keyScope = mpw_scopeForPurpose( keyPurpose ); trc( "keyScope: %s\n", keyScope ); - // TODO: Implement MPCounterValueTOTP + // OTP counter value. + if (siteCounter == MPCounterValueTOTP) + siteCounter = ((uint32_t)time( NULL ) / MP_otp_window) * MP_otp_window; // Calculate the site seed. trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s\n", @@ -78,7 +82,7 @@ static MPSiteKey mpw_siteKey_v2( MPSiteKey siteKey = mpw_hash_hmac_sha256( masterKey, MPMasterKeySize, siteSalt, siteSaltSize ); mpw_free( &siteSalt, siteSaltSize ); if (!siteKey) { - err( "Could not allocate site key: %s\n", strerror( errno ) ); + err( "Could not derive site key: %s\n", strerror( errno ) ); return NULL; } trc( " => siteKey.id: %s\n", mpw_id_buf( siteKey, MPSiteKeySize ) ); diff --git a/core/c/mpw-algorithm_v3.c b/core/c/mpw-algorithm_v3.c index e5be8239..7b5f0b6c 100644 --- a/core/c/mpw-algorithm_v3.c +++ b/core/c/mpw-algorithm_v3.c @@ -24,6 +24,7 @@ #define MP_N 32768LU #define MP_r 8U #define MP_p 2U +#define MP_otp_window 5 * 60 /* s */ // Inherited functions. MPSiteKey mpw_siteKey_v2( @@ -64,7 +65,7 @@ static MPMasterKey mpw_masterKey_v3( MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p ); mpw_free( &masterKeySalt, masterKeySaltSize ); if (!masterKey) { - err( "Could not allocate master key: %s\n", strerror( errno ) ); + err( "Could not derive master key: %s\n", strerror( errno ) ); return NULL; } trc( " => masterKey.id: %s\n", mpw_id_buf( masterKey, MPMasterKeySize ) );