2
0

Better ftl failure handling.

This commit is contained in:
Maarten Billemont 2017-08-01 16:50:50 -04:00
parent 46cdf56944
commit 99e286456e
4 changed files with 42 additions and 28 deletions

View File

@ -105,16 +105,15 @@ const char **mpw_templatesForType(MPPasswordType type, size_t *count) {
if (!(type & MPPasswordTypeClassGenerated)) { if (!(type & MPPasswordTypeClassGenerated)) {
ftl( "Not a generated type: %d", type ); ftl( "Not a generated type: %d", type );
*count = 0;
return NULL; return NULL;
} }
switch (type) { switch (type) {
case MPPasswordTypeGeneratedMaximum: case MPPasswordTypeGeneratedMaximum:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"anoxxxxxxxxxxxxxxxxx", "axxxxxxxxxxxxxxxxxno" ); "anoxxxxxxxxxxxxxxxxx", "axxxxxxxxxxxxxxxxxno" );
case MPPasswordTypeGeneratedLong: case MPPasswordTypeGeneratedLong:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"CvcvnoCvcvCvcv", "CvcvCvcvnoCvcv", "CvcvCvcvCvcvno", "CvcvnoCvcvCvcv", "CvcvCvcvnoCvcv", "CvcvCvcvCvcvno",
"CvccnoCvcvCvcv", "CvccCvcvnoCvcv", "CvccCvcvCvcvno", "CvccnoCvcvCvcv", "CvccCvcvnoCvcv", "CvccCvcvCvcvno",
"CvcvnoCvccCvcv", "CvcvCvccnoCvcv", "CvcvCvccCvcvno", "CvcvnoCvccCvcv", "CvcvCvccnoCvcv", "CvcvCvccCvcvno",
@ -123,26 +122,25 @@ const char **mpw_templatesForType(MPPasswordType type, size_t *count) {
"CvcvnoCvccCvcc", "CvcvCvccnoCvcc", "CvcvCvccCvccno", "CvcvnoCvccCvcc", "CvcvCvccnoCvcc", "CvcvCvccCvccno",
"CvccnoCvcvCvcc", "CvccCvcvnoCvcc", "CvccCvcvCvccno" ); "CvccnoCvcvCvcc", "CvccCvcvnoCvcc", "CvccCvcvCvccno" );
case MPPasswordTypeGeneratedMedium: case MPPasswordTypeGeneratedMedium:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"CvcnoCvc", "CvcCvcno" ); "CvcnoCvc", "CvcCvcno" );
case MPPasswordTypeGeneratedBasic: case MPPasswordTypeGeneratedBasic:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"aaanaaan", "aannaaan", "aaannaaa" ); "aaanaaan", "aannaaan", "aaannaaa" );
case MPPasswordTypeGeneratedShort: case MPPasswordTypeGeneratedShort:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"Cvcn" ); "Cvcn" );
case MPPasswordTypeGeneratedPIN: case MPPasswordTypeGeneratedPIN:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"nnnn" ); "nnnn" );
case MPPasswordTypeGeneratedName: case MPPasswordTypeGeneratedName:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"cvccvcvcv" ); "cvccvcvcv" );
case MPPasswordTypeGeneratedPhrase: case MPPasswordTypeGeneratedPhrase:
return mpw_alloc_array( *count, const char *, return mpw_alloc_array( count, const char *,
"cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" ); "cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" );
default: { default: {
ftl( "Unknown generated type: %d", type ); ftl( "Unknown generated type: %d", type );
*count = 0;
return NULL; return NULL;
} }
} }
@ -174,6 +172,7 @@ const MPKeyPurpose mpw_purposeWithName(const char *purposeName) {
return MPKeyPurposeRecovery; return MPKeyPurposeRecovery;
ftl( "Not a purpose name: %s", stdPurposeName ); ftl( "Not a purpose name: %s", stdPurposeName );
return MPKeyPurposeAuthentication;
} }
const char *mpw_nameForPurpose(MPKeyPurpose purpose) { const char *mpw_nameForPurpose(MPKeyPurpose purpose) {

View File

@ -60,8 +60,7 @@ extern int mpw_verbosity;
#define ftl_level -2 #define ftl_level -2
#define ftl(...) ({ \ #define ftl(...) ({ \
if (mpw_verbosity >= -2) \ if (mpw_verbosity >= -2) \
fprintf( stderr, __VA_ARGS__ ); \ fprintf( stderr, __VA_ARGS__ ); })
exit( 2 ); })
#endif #endif
#ifndef min #ifndef min
#define min(a, b) ({ \ #define min(a, b) ({ \
@ -78,9 +77,11 @@ extern int mpw_verbosity;
//// Buffers and memory. //// Buffers and memory.
/** 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, ...) ({ \ #define mpw_alloc_array(_count, _type, ...) ({ \
_type stackElements[] = { __VA_ARGS__ }; \ _type stackElements[] = { __VA_ARGS__ }; \
_count = sizeof( stackElements ) / sizeof( _type ); \ if (_count) \
*_count = sizeof( stackElements ) / sizeof( _type ); \
_type *allocElements = malloc( sizeof( stackElements ) ); \ _type *allocElements = malloc( sizeof( stackElements ) ); \
memcpy( allocElements, stackElements, sizeof( stackElements ) ); \ memcpy( allocElements, stackElements, sizeof( stackElements ) ); \
allocElements; \ allocElements; \

View File

@ -21,8 +21,6 @@
#define MP_N 32768 #define MP_N 32768
#define MP_r 8 #define MP_r 8
#define MP_p 2 #define MP_p 2
#define MP_dkLen 64
#define MP_hash PearlHashSHA256
static void mpw_getTime(struct timeval *time) { static void mpw_getTime(struct timeval *time) {
@ -65,8 +63,10 @@ int main(int argc, char *const argv[]) {
uint8_t *sitePasswordInfo = malloc( 128 ); uint8_t *sitePasswordInfo = malloc( 128 );
iterations = 3000000; iterations = 3000000;
masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent ); masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent );
if (!masterKey) if (!masterKey) {
ftl( "Could not allocate master key: %d\n", errno ); ftl( "Could not allocate master key: %d\n", errno );
abort();
}
mpw_getTime( &startTime ); mpw_getTime( &startTime );
for (int i = 1; i <= iterations; ++i) { for (int i = 1; i <= iterations; ++i) {
free( (void *)mpw_hmac_sha256( masterKey, MPMasterKeySize, sitePasswordInfo, 128 ) ); free( (void *)mpw_hmac_sha256( masterKey, MPMasterKeySize, sitePasswordInfo, 128 ) );
@ -108,8 +108,10 @@ int main(int argc, char *const argv[]) {
mpw_getTime( &startTime ); mpw_getTime( &startTime );
for (int i = 1; i <= iterations; ++i) { for (int i = 1; i <= iterations; ++i) {
masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent ); masterKey = mpw_masterKey( fullName, masterPassword, MPAlgorithmVersionCurrent );
if (!masterKey) if (!masterKey) {
ftl( "Could not allocate master key: %d\n", errno ); ftl( "Could not allocate master key: %d\n", errno );
break;
}
MPSiteKey siteKey = mpw_siteKey( MPSiteKey siteKey = mpw_siteKey(
masterKey, siteName, siteCounter, keyPurpose, keyContext, MPAlgorithmVersionCurrent ); masterKey, siteName, siteCounter, keyPurpose, keyContext, MPAlgorithmVersionCurrent );

View File

@ -155,20 +155,20 @@ int main(int argc, char *const argv[]) {
switch (optopt) { switch (optopt) {
case 'u': case 'u':
ftl( "Missing full name to option: -%c\n", optopt ); ftl( "Missing full name to option: -%c\n", optopt );
break; abort();
case 't': case 't':
ftl( "Missing type name to option: -%c\n", optopt ); ftl( "Missing type name to option: -%c\n", optopt );
break; abort();
case 'c': case 'c':
ftl( "Missing counter value to option: -%c\n", optopt ); ftl( "Missing counter value to option: -%c\n", optopt );
break; abort();
default: default:
ftl( "Unknown option: -%c\n", optopt ); ftl( "Unknown option: -%c\n", optopt );
break; abort();
} }
default: default:
ftl( "Unexpected option: %c", opt ); ftl( "Unexpected option: %c", opt );
break; abort();
} }
if (optind < argc) if (optind < argc)
siteNameArg = argv[optind]; siteNameArg = argv[optind];
@ -185,11 +185,15 @@ int main(int argc, char *const argv[]) {
// Determine fullName, siteName & masterPassword. // Determine fullName, siteName & masterPassword.
if (!(fullNameArg && (fullName = strdup( fullNameArg ))) && if (!(fullNameArg && (fullName = strdup( fullNameArg ))) &&
!(fullName = getline_prompt( "Your full name:" ))) !(fullName = getline_prompt( "Your full name:" ))) {
ftl( "Missing full name.\n" ); ftl( "Missing full name.\n" );
abort();
}
if (!(siteNameArg && (siteName = strdup( siteNameArg ))) && if (!(siteNameArg && (siteName = strdup( siteNameArg ))) &&
!(siteName = getline_prompt( "Site name:" ))) !(siteName = getline_prompt( "Site name:" ))) {
ftl( "Missing site name.\n" ); ftl( "Missing site name.\n" );
abort();
}
if (!(masterPasswordArg && (masterPassword = strdup( masterPasswordArg )))) if (!(masterPasswordArg && (masterPassword = strdup( masterPasswordArg ))))
while (!masterPassword || !strlen( masterPassword )) while (!masterPassword || !strlen( masterPassword ))
masterPassword = getpass( "Your master password: " ); masterPassword = getpass( "Your master password: " );
@ -269,14 +273,18 @@ int main(int argc, char *const argv[]) {
// Parse default/config-overriding command-line parameters. // Parse default/config-overriding command-line parameters.
if (algorithmVersionArg) { if (algorithmVersionArg) {
int algorithmVersionInt = atoi( algorithmVersionArg ); int algorithmVersionInt = atoi( algorithmVersionArg );
if (algorithmVersionInt < MPAlgorithmVersionFirst || algorithmVersionInt > MPAlgorithmVersionLast) if (algorithmVersionInt < MPAlgorithmVersionFirst || algorithmVersionInt > MPAlgorithmVersionLast) {
ftl( "Invalid algorithm version: %s\n", algorithmVersionArg ); ftl( "Invalid algorithm version: %s\n", algorithmVersionArg );
abort();
}
algorithmVersion = (MPAlgorithmVersion)algorithmVersionInt; algorithmVersion = (MPAlgorithmVersion)algorithmVersionInt;
} }
if (siteCounterArg) { if (siteCounterArg) {
long long int siteCounterInt = atoll( siteCounterArg ); long long int siteCounterInt = atoll( siteCounterArg );
if (siteCounterInt < 0 || siteCounterInt > UINT32_MAX) if (siteCounterInt < 0 || siteCounterInt > UINT32_MAX) {
ftl( "Invalid site counter: %s\n", siteCounterArg ); ftl( "Invalid site counter: %s\n", siteCounterArg );
abort();
}
siteCounter = (uint32_t)siteCounterInt; siteCounter = (uint32_t)siteCounterInt;
} }
if (keyPurposeArg) if (keyPurposeArg)
@ -313,8 +321,10 @@ int main(int argc, char *const argv[]) {
fullName, masterPassword, algorithmVersion ); fullName, masterPassword, algorithmVersion );
mpw_free_string( masterPassword ); mpw_free_string( masterPassword );
mpw_free_string( fullName ); mpw_free_string( fullName );
if (!masterKey) if (!masterKey) {
ftl( "Couldn't derive master key." ); ftl( "Couldn't derive master key." );
abort();
}
MPSiteKey siteKey = mpw_siteKey( masterKey, siteName, siteCounter, keyPurpose, keyContext, algorithmVersion ); MPSiteKey siteKey = mpw_siteKey( masterKey, siteName, siteCounter, keyPurpose, keyContext, algorithmVersion );
const char *sitePassword = mpw_sitePassword(siteKey, passwordType, algorithmVersion ); const char *sitePassword = mpw_sitePassword(siteKey, passwordType, algorithmVersion );
@ -322,8 +332,10 @@ int main(int argc, char *const argv[]) {
mpw_free( siteKey, MPSiteKeySize ); mpw_free( siteKey, MPSiteKeySize );
mpw_free_string( siteName ); mpw_free_string( siteName );
mpw_free_string( keyContext ); mpw_free_string( keyContext );
if (!sitePassword) if (!sitePassword) {
ftl( "Couldn't derive site password." ); ftl( "Couldn't derive site password." );
abort();
}
fprintf( stdout, "%s\n", sitePassword ); fprintf( stdout, "%s\n", sitePassword );
mpw_free_string( sitePassword ); mpw_free_string( sitePassword );