diff --git a/MasterPassword/C/mpw-algorithm_v0.c b/MasterPassword/C/mpw-algorithm_v0.c index 98e70e1c..28eefb3e 100644 --- a/MasterPassword/C/mpw-algorithm_v0.c +++ b/MasterPassword/C/mpw-algorithm_v0.c @@ -11,6 +11,7 @@ #include #include +#include "mpw-types.h" #include "mpw-util.h" #define MP_N 32768 diff --git a/MasterPassword/C/mpw-algorithm_v1.c b/MasterPassword/C/mpw-algorithm_v1.c index 1a20107f..925e02bb 100644 --- a/MasterPassword/C/mpw-algorithm_v1.c +++ b/MasterPassword/C/mpw-algorithm_v1.c @@ -11,6 +11,7 @@ #include #include +#include "mpw-types.h" #include "mpw-util.h" #define MP_N 32768 diff --git a/MasterPassword/C/mpw-algorithm_v2.c b/MasterPassword/C/mpw-algorithm_v2.c index 1823c6ff..935b5016 100644 --- a/MasterPassword/C/mpw-algorithm_v2.c +++ b/MasterPassword/C/mpw-algorithm_v2.c @@ -11,6 +11,7 @@ #include #include +#include "mpw-types.h" #include "mpw-util.h" #define MP_N 32768 diff --git a/MasterPassword/C/mpw-algorithm_v3.c b/MasterPassword/C/mpw-algorithm_v3.c index 72ba92cb..bd1feeb4 100644 --- a/MasterPassword/C/mpw-algorithm_v3.c +++ b/MasterPassword/C/mpw-algorithm_v3.c @@ -11,6 +11,7 @@ #include #include +#include "mpw-types.h" #include "mpw-util.h" #define MP_N 32768 diff --git a/MasterPassword/C/mpw-types.c b/MasterPassword/C/mpw-types.c index 4020b1e4..06d7e6f8 100644 --- a/MasterPassword/C/mpw-types.c +++ b/MasterPassword/C/mpw-types.c @@ -52,7 +52,7 @@ const MPSiteType mpw_typeWithName(const char *typeName) { ftl( "Not a generated type name: %s", stdTypeName ); } -inline const char **mpw_templatesForType(MPSiteType type, size_t *count) { +const char **mpw_templatesForType(MPSiteType type, size_t *count) { if (!(type & MPSiteTypeClassGenerated)) { ftl( "Not a generated type: %d", type ); @@ -62,42 +62,42 @@ inline const char **mpw_templatesForType(MPSiteType type, size_t *count) { switch (type) { case MPSiteTypeGeneratedMaximum: { - *count = 2; - return (const char *[]){ "anoxxxxxxxxxxxxxxxxx", "axxxxxxxxxxxxxxxxxno" }; + return alloc_array( *count, const char *, + "anoxxxxxxxxxxxxxxxxx", "axxxxxxxxxxxxxxxxxno" ); } case MPSiteTypeGeneratedLong: { - *count = 21; - return (const char *[]){ "CvcvnoCvcvCvcv", "CvcvCvcvnoCvcv", "CvcvCvcvCvcvno", - "CvccnoCvcvCvcv", "CvccCvcvnoCvcv", "CvccCvcvCvcvno", - "CvcvnoCvccCvcv", "CvcvCvccnoCvcv", "CvcvCvccCvcvno", - "CvcvnoCvcvCvcc", "CvcvCvcvnoCvcc", "CvcvCvcvCvccno", - "CvccnoCvccCvcv", "CvccCvccnoCvcv", "CvccCvccCvcvno", - "CvcvnoCvccCvcc", "CvcvCvccnoCvcc", "CvcvCvccCvccno", - "CvccnoCvcvCvcc", "CvccCvcvnoCvcc", "CvccCvcvCvccno" }; + return alloc_array( *count, const char *, + "CvcvnoCvcvCvcv", "CvcvCvcvnoCvcv", "CvcvCvcvCvcvno", + "CvccnoCvcvCvcv", "CvccCvcvnoCvcv", "CvccCvcvCvcvno", + "CvcvnoCvccCvcv", "CvcvCvccnoCvcv", "CvcvCvccCvcvno", + "CvcvnoCvcvCvcc", "CvcvCvcvnoCvcc", "CvcvCvcvCvccno", + "CvccnoCvccCvcv", "CvccCvccnoCvcv", "CvccCvccCvcvno", + "CvcvnoCvccCvcc", "CvcvCvccnoCvcc", "CvcvCvccCvccno", + "CvccnoCvcvCvcc", "CvccCvcvnoCvcc", "CvccCvcvCvccno" ); } case MPSiteTypeGeneratedMedium: { - *count = 2; - return (const char *[]){ "CvcnoCvc", "CvcCvcno" }; + return alloc_array( *count, const char *, + "CvcnoCvc", "CvcCvcno" ); } case MPSiteTypeGeneratedBasic: { - *count = 3; - return (const char *[]){ "aaanaaan", "aannaaan", "aaannaaa" }; + return alloc_array( *count, const char *, + "aaanaaan", "aannaaan", "aaannaaa" ); } case MPSiteTypeGeneratedShort: { - *count = 1; - return (const char *[]){"Cvcn"}; + return alloc_array( *count, const char *, + "Cvcn" ); } case MPSiteTypeGeneratedPIN: { - *count = 1; - return (const char *[]){ "nnnn" }; + return alloc_array( *count, const char *, + "nnnn" ); } case MPSiteTypeGeneratedName: { - *count = 1; - return (const char *[]) {"cvccvcvcv"}; + return alloc_array( *count, const char *, + "cvccvcvcv" ); } case MPSiteTypeGeneratedPhrase: { - *count = 3; - return (const char *[]){ "cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" }; + return alloc_array( *count, const char *, + "cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" ); } default: { ftl( "Unknown generated type: %d", type ); @@ -114,15 +114,19 @@ const char *mpw_templateForType(MPSiteType type, uint8_t seedByte) { if (!count) return NULL; - return templates[seedByte % count]; + char const *template = templates[seedByte % count]; + free( templates ); + return template; } const MPSiteVariant mpw_variantWithName(const char *variantName) { - char stdVariantName[strlen( variantName )]; - strcpy( stdVariantName, variantName ); - for (char *vN = stdVariantName; *vN; ++vN) - *vN = (char)tolower( *vN ); + // Lower-case and trim optionally leading "generated" string from typeName to standardize it. + size_t stdVariantNameSize = strlen( variantName ); + char stdVariantName[stdVariantNameSize + 1]; + for (size_t c = 0; c < stdVariantNameSize; ++c) + stdVariantName[c] = (char)tolower( variantName[c] ); + stdVariantName[stdVariantNameSize] = '\0'; if (0 == strcmp( stdVariantName, "p" ) || 0 == strcmp( stdVariantName, "password" )) return MPSiteVariantPassword; diff --git a/MasterPassword/C/mpw-types.h b/MasterPassword/C/mpw-types.h index 2b17394d..a476d81c 100644 --- a/MasterPassword/C/mpw-types.h +++ b/MasterPassword/C/mpw-types.h @@ -6,6 +6,8 @@ // Copyright (c) 2014 Lyndir. All rights reserved. // +#ifndef _MPW_TYPES_H +#define _MPW_TYPES_H #include #include @@ -73,9 +75,10 @@ const char *mpw_scopeForVariant(MPSiteVariant variant); const MPSiteType mpw_typeWithName(const char *typeName); /** - * @return An array of internal strings that express the templates to use for the given type. + * @return A newly allocated array of internal strings that express the templates to use for the given type. * The amount of elements in the array is stored in count. * If an unsupported type is given, count will be 0 and will return NULL. +* The array needs to be free'ed, the strings themselves must not be free'ed or modified. */ const char **mpw_templatesForType(MPSiteType type, size_t *count); /** @@ -93,3 +96,4 @@ const char *mpw_charactersInClass(char characterClass); */ const char mpw_characterFromClass(char characterClass, uint8_t seedByte); +#endif // _MPW_TYPES_H diff --git a/MasterPassword/C/mpw-util.h b/MasterPassword/C/mpw-util.h index 44ac0644..66cb8419 100644 --- a/MasterPassword/C/mpw-util.h +++ b/MasterPassword/C/mpw-util.h @@ -25,6 +25,14 @@ //// Buffers and memory. +#define alloc_array(_count, _type, ...) ({ \ + _type stackElements[] = (_type[]){ __VA_ARGS__ }; \ + _count = sizeof( stackElements ) / sizeof( _type ); \ + _type *allocElements = malloc( sizeof( stackElements ) ); \ + memcpy( allocElements, stackElements, sizeof( stackElements ) ); \ + allocElements; \ + }) + /** Push a buffer onto a buffer. reallocs the given buffer and appends the given buffer. */ void mpw_pushBuf( uint8_t **const buffer, size_t *const bufferSize, const void *pushBuffer, const size_t pushSize); diff --git a/MasterPassword/ObjC/MPAlgorithmV0.m b/MasterPassword/ObjC/MPAlgorithmV0.m index 79f13dd0..a6f938c6 100644 --- a/MasterPassword/ObjC/MPAlgorithmV0.m +++ b/MasterPassword/ObjC/MPAlgorithmV0.m @@ -730,6 +730,7 @@ BN_add( permutations, permutations, templatePermutations ); } BN_free( templatePermutations ); + free( templates ); return [self timeToCrack:timeToCrack permutations:permutations forAttacker:attacker]; } diff --git a/MasterPassword/ObjC/Mac/MPPasswordWindowController.m b/MasterPassword/ObjC/Mac/MPPasswordWindowController.m index 5dffcea7..ae733511 100644 --- a/MasterPassword/ObjC/Mac/MPPasswordWindowController.m +++ b/MasterPassword/ObjC/Mac/MPPasswordWindowController.m @@ -605,7 +605,8 @@ CGDirectDisplayID displayID = [self.window.screen.deviceDescription[@"NSScreenNumber"] unsignedIntValue]; CGImageRef capturedImage = CGDisplayCreateImage( displayID ); if (!capturedImage || CGImageGetWidth( capturedImage ) <= 1) { - CFRelease( capturedImage ); + if (capturedImage) + CFRelease( capturedImage ); wrn( @"Failed to capture screen image for display: %d", displayID ); return; }