2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-12-20 19:30:34 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2014-12-20 19:30:34 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2014-12-20 19:30:34 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2014-10-15 19:32:10 +00:00
|
|
|
|
2014-12-21 17:37:21 +00:00
|
|
|
#include "mpw-algorithm.h"
|
2015-01-15 22:43:41 +00:00
|
|
|
#include "mpw-algorithm_v0.c"
|
|
|
|
#include "mpw-algorithm_v1.c"
|
|
|
|
#include "mpw-algorithm_v2.c"
|
|
|
|
#include "mpw-algorithm_v3.c"
|
2014-05-05 15:16:59 +00:00
|
|
|
|
2017-08-01 12:31:39 +00:00
|
|
|
MPMasterKey mpw_masterKey(const char *fullName, const char *masterPassword, const MPAlgorithmVersion algorithmVersion) {
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-22 22:18:24 +00:00
|
|
|
if (fullName && !strlen( fullName ))
|
|
|
|
fullName = NULL;
|
|
|
|
if (masterPassword && !strlen( masterPassword ))
|
|
|
|
masterPassword = NULL;
|
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "-- mpw_masterKey (algorithm: %u)\n", algorithmVersion );
|
|
|
|
trc( "fullName: %s\n", fullName );
|
2017-08-22 22:18:24 +00:00
|
|
|
trc( "masterPassword.id: %s\n", masterPassword? mpw_id_buf( masterPassword, strlen( masterPassword ) ): NULL );
|
2016-07-23 15:44:17 +00:00
|
|
|
if (!fullName || !masterPassword)
|
|
|
|
return NULL;
|
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
switch (algorithmVersion) {
|
|
|
|
case MPAlgorithmVersion0:
|
2017-08-06 15:40:10 +00:00
|
|
|
return mpw_masterKey_v0( fullName, masterPassword );
|
2015-01-15 22:43:41 +00:00
|
|
|
case MPAlgorithmVersion1:
|
2017-08-06 15:40:10 +00:00
|
|
|
return mpw_masterKey_v1( fullName, masterPassword );
|
2015-01-15 22:43:41 +00:00
|
|
|
case MPAlgorithmVersion2:
|
2017-08-06 15:40:10 +00:00
|
|
|
return mpw_masterKey_v2( fullName, masterPassword );
|
2015-01-15 22:43:41 +00:00
|
|
|
case MPAlgorithmVersion3:
|
2017-08-06 15:40:10 +00:00
|
|
|
return mpw_masterKey_v3( fullName, masterPassword );
|
2015-01-15 22:43:41 +00:00
|
|
|
default:
|
2017-08-04 13:36:03 +00:00
|
|
|
err( "Unsupported version: %d\n", algorithmVersion );
|
2015-01-15 22:43:41 +00:00
|
|
|
return NULL;
|
2014-12-21 17:37:21 +00:00
|
|
|
}
|
2014-12-20 04:15:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:31:39 +00:00
|
|
|
MPSiteKey mpw_siteKey(
|
2017-08-08 00:27:08 +00:00
|
|
|
MPMasterKey masterKey, const char *siteName, const MPCounterValue siteCounter,
|
2017-08-01 17:45:54 +00:00
|
|
|
const MPKeyPurpose keyPurpose, const char *keyContext, const MPAlgorithmVersion algorithmVersion) {
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-22 22:18:24 +00:00
|
|
|
if (keyContext && !strlen( keyContext ))
|
|
|
|
keyContext = NULL;
|
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "-- mpw_siteKey (algorithm: %u)\n", algorithmVersion );
|
|
|
|
trc( "siteName: %s\n", siteName );
|
|
|
|
trc( "siteCounter: %d\n", siteCounter );
|
|
|
|
trc( "keyPurpose: %d (%s)\n", keyPurpose, mpw_nameForPurpose( keyPurpose ) );
|
|
|
|
trc( "keyContext: %s\n", keyContext );
|
2016-07-23 15:44:17 +00:00
|
|
|
if (!masterKey || !siteName)
|
|
|
|
return NULL;
|
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
switch (algorithmVersion) {
|
|
|
|
case MPAlgorithmVersion0:
|
2017-08-01 17:45:54 +00:00
|
|
|
return mpw_siteKey_v0( masterKey, siteName, siteCounter, keyPurpose, keyContext );
|
2017-08-01 12:31:39 +00:00
|
|
|
case MPAlgorithmVersion1:
|
2017-08-01 17:45:54 +00:00
|
|
|
return mpw_siteKey_v1( masterKey, siteName, siteCounter, keyPurpose, keyContext );
|
2017-08-01 12:31:39 +00:00
|
|
|
case MPAlgorithmVersion2:
|
2017-08-01 17:45:54 +00:00
|
|
|
return mpw_siteKey_v2( masterKey, siteName, siteCounter, keyPurpose, keyContext );
|
2017-08-01 12:31:39 +00:00
|
|
|
case MPAlgorithmVersion3:
|
2017-08-01 17:45:54 +00:00
|
|
|
return mpw_siteKey_v3( masterKey, siteName, siteCounter, keyPurpose, keyContext );
|
2017-08-01 12:31:39 +00:00
|
|
|
default:
|
2017-08-04 13:36:03 +00:00
|
|
|
err( "Unsupported version: %d\n", algorithmVersion );
|
2017-08-01 12:31:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *mpw_siteResult(
|
|
|
|
MPMasterKey masterKey, const char *siteName, const MPCounterValue siteCounter,
|
|
|
|
const MPKeyPurpose keyPurpose, const char *keyContext,
|
|
|
|
const MPResultType resultType, const char *resultParam,
|
|
|
|
const MPAlgorithmVersion algorithmVersion) {
|
2017-08-01 12:31:39 +00:00
|
|
|
|
2017-08-22 22:18:24 +00:00
|
|
|
if (keyContext && !strlen( keyContext ))
|
|
|
|
keyContext = NULL;
|
|
|
|
if (resultParam && !strlen( resultParam ))
|
|
|
|
resultParam = NULL;
|
|
|
|
|
2017-08-13 15:35:15 +00:00
|
|
|
MPSiteKey siteKey = mpw_siteKey( masterKey, siteName, siteCounter, keyPurpose, keyContext, algorithmVersion );
|
2017-08-01 12:31:39 +00:00
|
|
|
if (!siteKey)
|
|
|
|
return NULL;
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
trc( "-- mpw_siteResult (algorithm: %u)\n", algorithmVersion );
|
|
|
|
trc( "resultType: %d (%s)\n", resultType, mpw_nameForType( resultType ) );
|
|
|
|
trc( "resultParam: %s\n", resultParam );
|
|
|
|
|
|
|
|
char *sitePassword = NULL;
|
|
|
|
if (resultType & MPResultTypeClassTemplate) {
|
|
|
|
switch (algorithmVersion) {
|
|
|
|
case MPAlgorithmVersion0:
|
|
|
|
return mpw_sitePasswordFromTemplate_v0( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion1:
|
|
|
|
return mpw_sitePasswordFromTemplate_v1( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion2:
|
|
|
|
return mpw_sitePasswordFromTemplate_v2( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion3:
|
|
|
|
return mpw_sitePasswordFromTemplate_v3( masterKey, siteKey, resultType, resultParam );
|
|
|
|
default:
|
|
|
|
err( "Unsupported version: %d\n", algorithmVersion );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 01:57:47 +00:00
|
|
|
else if (resultType & MPResultTypeClassStateful) {
|
2017-08-10 16:30:42 +00:00
|
|
|
switch (algorithmVersion) {
|
|
|
|
case MPAlgorithmVersion0:
|
|
|
|
return mpw_sitePasswordFromCrypt_v0( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion1:
|
|
|
|
return mpw_sitePasswordFromCrypt_v1( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion2:
|
|
|
|
return mpw_sitePasswordFromCrypt_v2( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion3:
|
|
|
|
return mpw_sitePasswordFromCrypt_v3( masterKey, siteKey, resultType, resultParam );
|
|
|
|
default:
|
|
|
|
err( "Unsupported version: %d\n", algorithmVersion );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (resultType & MPResultTypeClassDerive) {
|
|
|
|
switch (algorithmVersion) {
|
|
|
|
case MPAlgorithmVersion0:
|
|
|
|
return mpw_sitePasswordFromDerive_v0( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion1:
|
|
|
|
return mpw_sitePasswordFromDerive_v1( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion2:
|
|
|
|
return mpw_sitePasswordFromDerive_v2( masterKey, siteKey, resultType, resultParam );
|
|
|
|
case MPAlgorithmVersion3:
|
|
|
|
return mpw_sitePasswordFromDerive_v3( masterKey, siteKey, resultType, resultParam );
|
|
|
|
default:
|
|
|
|
err( "Unsupported version: %d\n", algorithmVersion );
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-19 04:55:26 +00:00
|
|
|
}
|
2017-08-10 16:30:42 +00:00
|
|
|
else {
|
|
|
|
err( "Unsupported password type: %d\n", resultType );
|
|
|
|
}
|
|
|
|
|
|
|
|
return sitePassword;
|
2014-05-05 15:16:59 +00:00
|
|
|
}
|
2017-08-04 14:43:46 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *mpw_siteState(
|
|
|
|
MPMasterKey masterKey, const char *siteName, const MPCounterValue siteCounter,
|
|
|
|
const MPKeyPurpose keyPurpose, const char *keyContext,
|
2017-08-22 22:18:24 +00:00
|
|
|
const MPResultType resultType, const char *resultParam,
|
2017-08-10 16:30:42 +00:00
|
|
|
const MPAlgorithmVersion algorithmVersion) {
|
2017-08-04 14:43:46 +00:00
|
|
|
|
2017-08-22 22:18:24 +00:00
|
|
|
if (keyContext && !strlen( keyContext ))
|
|
|
|
keyContext = NULL;
|
|
|
|
if (resultParam && !strlen( resultParam ))
|
|
|
|
resultParam = NULL;
|
|
|
|
|
2017-09-23 23:09:19 +00:00
|
|
|
MPSiteKey siteKey = mpw_siteKey( masterKey, siteName, siteCounter, keyPurpose, keyContext, algorithmVersion );
|
2017-08-10 16:30:42 +00:00
|
|
|
if (!siteKey)
|
2017-08-04 14:43:46 +00:00
|
|
|
return NULL;
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
trc( "-- mpw_siteState (algorithm: %u)\n", algorithmVersion );
|
|
|
|
trc( "resultType: %d (%s)\n", resultType, mpw_nameForType( resultType ) );
|
2017-09-24 16:00:38 +00:00
|
|
|
trc( "resultParam: %zu bytes = %s\n", sizeof( resultParam ), resultParam );
|
2017-08-22 22:18:24 +00:00
|
|
|
if (!masterKey || !resultParam)
|
2017-08-04 14:43:46 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
switch (algorithmVersion) {
|
|
|
|
case MPAlgorithmVersion0:
|
2017-08-22 22:18:24 +00:00
|
|
|
return mpw_siteState_v0( masterKey, siteKey, resultType, resultParam );
|
2017-08-04 14:43:46 +00:00
|
|
|
case MPAlgorithmVersion1:
|
2017-08-22 22:18:24 +00:00
|
|
|
return mpw_siteState_v1( masterKey, siteKey, resultType, resultParam );
|
2017-08-04 14:43:46 +00:00
|
|
|
case MPAlgorithmVersion2:
|
2017-08-22 22:18:24 +00:00
|
|
|
return mpw_siteState_v2( masterKey, siteKey, resultType, resultParam );
|
2017-08-04 14:43:46 +00:00
|
|
|
case MPAlgorithmVersion3:
|
2017-08-22 22:18:24 +00:00
|
|
|
return mpw_siteState_v3( masterKey, siteKey, resultType, resultParam );
|
2017-08-04 14:43:46 +00:00
|
|
|
default:
|
|
|
|
err( "Unsupported version: %d\n", algorithmVersion );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 16:46:43 +00:00
|
|
|
|
|
|
|
MPIdenticon mpw_identicon(const char *fullName, const char *masterPassword) {
|
|
|
|
|
|
|
|
const char *leftArm[] = { "╔", "╚", "╰", "═" };
|
|
|
|
const char *rightArm[] = { "╗", "╝", "╯", "═" };
|
|
|
|
const char *body[] = { "█", "░", "▒", "▓", "☺", "☻" };
|
|
|
|
const char *accessory[] = {
|
|
|
|
"◈", "◎", "◐", "◑", "◒", "◓", "☀", "☁", "☂", "☃", "", "★", "☆", "☎", "☏", "⎈", "⌂", "☘", "☢", "☣",
|
|
|
|
"☕", "⌚", "⌛", "⏰", "⚡", "⛄", "⛅", "☔", "♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟",
|
|
|
|
"♨", "♩", "♪", "♫", "⚐", "⚑", "⚔", "⚖", "⚙", "⚠", "⌘", "⏎", "✄", "✆", "✈", "✉", "✌"
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t *identiconSeed = NULL;
|
|
|
|
if (fullName && strlen( fullName ) && masterPassword && strlen( masterPassword ))
|
|
|
|
identiconSeed = mpw_hash_hmac_sha256(
|
|
|
|
(const uint8_t *)masterPassword, strlen( masterPassword ),
|
|
|
|
(const uint8_t *)fullName, strlen( fullName ) );
|
|
|
|
if (!identiconSeed)
|
|
|
|
return (MPIdenticon){
|
|
|
|
.leftArm = "",
|
|
|
|
.body = "",
|
|
|
|
.rightArm = "",
|
|
|
|
.accessory = "",
|
|
|
|
.color=0,
|
|
|
|
};
|
|
|
|
|
|
|
|
MPIdenticon identicon = {
|
|
|
|
.leftArm = leftArm[identiconSeed[0] % (sizeof( leftArm ) / sizeof( leftArm[0] ))],
|
|
|
|
.body = body[identiconSeed[1] % (sizeof( body ) / sizeof( body[0] ))],
|
|
|
|
.rightArm = rightArm[identiconSeed[2] % (sizeof( rightArm ) / sizeof( rightArm[0] ))],
|
|
|
|
.accessory = accessory[identiconSeed[3] % (sizeof( accessory ) / sizeof( accessory[0] ))],
|
|
|
|
.color = (uint8_t)(identiconSeed[4] % (MPIdenticonColorLast - MPIdenticonColorFirst + 1) + MPIdenticonColorFirst),
|
|
|
|
};
|
|
|
|
mpw_free( &identiconSeed, 32 );
|
|
|
|
|
|
|
|
return identicon;
|
|
|
|
}
|