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

View File

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

View File

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

View File

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