diff --git a/MasterPassword/C/mpw-algorithm_v0.c b/MasterPassword/C/mpw-algorithm_v0.c index 78dae9f7..a2e52876 100644 --- a/MasterPassword/C/mpw-algorithm_v0.c +++ b/MasterPassword/C/mpw-algorithm_v0.c @@ -18,6 +18,22 @@ #define MP_p 2 #define MP_hash PearlHashSHA256 +static const char *mpw_templateForType_v0(MPSiteType type, uint16_t seedByte) { + + size_t count = 0; + const char **templates = mpw_templatesForType( type, &count ); + if (!count) + return NULL; + + return templates[seedByte % count]; +} + +static const char mpw_characterFromClass_v0(char characterClass, uint16_t seedByte) { + + const char *classCharacters = mpw_charactersInClass( characterClass ); + return classCharacters[seedByte % strlen( classCharacters )]; +} + static const uint8_t *mpw_masterKeyForUser_v0(const char *fullName, const char *masterPassword) { const char *mpKeyScope = mpw_scopeForVariant( MPSiteVariantPassword ); @@ -88,7 +104,7 @@ static const char *mpw_passwordForSite_v0(const uint8_t *masterKey, const char * trc( "sitePasswordSeed ID: %s\n", mpw_idForBuf( sitePasswordSeed, 32 ) ); // Determine the template. - const char *template = mpw_templateForType( siteType, sitePasswordSeed[0] ); + const char *template = mpw_templateForType_v0( siteType, htons( sitePasswordSeed[0] ) ); trc( "type %d, template: %s\n", siteType, template ); if (strlen( template ) > 32) { ftl( "Template too long for password seed: %lu", strlen( template ) ); @@ -99,9 +115,9 @@ static const char *mpw_passwordForSite_v0(const uint8_t *masterKey, const char * // 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], sitePasswordSeed[c + 1] ); - trc( "class %c, index %u (0x%02X) -> character: %c\n", template[c], sitePasswordSeed[c + 1], sitePasswordSeed[c + 1], - sitePassword[c] ); + sitePassword[c] = mpw_characterFromClass_v0( template[c], htons( sitePasswordSeed[c + 1] ) ); + trc( "class %c, index %u (0x%02X) -> character: %c\n", + template[c], htons( sitePasswordSeed[c + 1] ), htons( sitePasswordSeed[c + 1] ), sitePassword[c] ); } mpw_free( sitePasswordSeed, sizeof( sitePasswordSeed ) ); diff --git a/MasterPassword/C/mpw-cli.c b/MasterPassword/C/mpw-cli.c index 50925446..ce146c73 100644 --- a/MasterPassword/C/mpw-cli.c +++ b/MasterPassword/C/mpw-cli.c @@ -22,6 +22,7 @@ #define MP_env_fullname "MP_FULLNAME" #define MP_env_sitetype "MP_SITETYPE" #define MP_env_sitecounter "MP_SITECOUNTER" +#define MP_env_algorithm "MP_ALGORITHM" static void usage() { @@ -39,9 +40,9 @@ static void usage() { " n, name | 9 letter name.\n" " p, phrase | 20 character sentence.\n\n", MP_env_sitetype ); fprintf( stderr, " -c counter The value of the counter.\n" - " Defaults to %s in env or '1'.\n\n", MP_env_sitecounter ); + " Defaults to %s in env or 1.\n\n", MP_env_sitecounter ); fprintf( stderr, " -V version The algorithm version to use.\n" - " Defaults to %d.\n\n", MPAlgorithmVersionCurrent ); + " Defaults to %s in env or %d.\n\n", MP_env_algorithm, MPAlgorithmVersionCurrent ); fprintf( stderr, " -v variant The kind of content to generate.\n" " Defaults to 'password'.\n" " p, password | The password to log in with.\n" @@ -105,6 +106,10 @@ int main(int argc, char *const argv[]) { uint32_t siteCounter = 1; const char *siteCounterString = getenv( MP_env_sitecounter ); MPAlgorithmVersion algorithmVersion = MPAlgorithmVersionCurrent; + const char *algorithmVersionString = getenv( MP_env_algorithm ); + if (algorithmVersionString && strlen( algorithmVersionString )) + if (sscanf( algorithmVersionString, "%u", &algorithmVersion ) != 1) + ftl( "Invalid %s: %s\n", MP_env_algorithm, algorithmVersionString ); // Read the options. for (int opt; (opt = getopt( argc, argv, "u:t:c:v:V:C:h" )) != -1;) @@ -168,6 +173,7 @@ int main(int argc, char *const argv[]) { siteType = MPSiteTypeGeneratedPhrase; if (siteTypeString) siteType = mpw_typeWithName( siteTypeString ); + trc( "algorithmVersion: %u\n", algorithmVersion ); // Read the master password. char *mpwConfigPath = homedir( ".mpw" );