2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2015-01-15 22:43:41 +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.
|
2015-01-15 22:43:41 +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.
|
2015-01-15 22:43:41 +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/>.
|
|
|
|
//==============================================================================
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "mpw-util.h"
|
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
#define MP_N 32768LU
|
|
|
|
#define MP_r 8U
|
|
|
|
#define MP_p 2U
|
2017-08-30 13:54:16 +00:00
|
|
|
#define MP_otp_window 5 * 60 /* s */
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
// Inherited functions.
|
|
|
|
MPSiteKey mpw_siteKey_v2(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, const char *siteName, MPCounterValue siteCounter,
|
|
|
|
MPKeyPurpose keyPurpose, const char *keyContext);
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *mpw_sitePasswordFromTemplate_v2(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *resultParam);
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *mpw_sitePasswordFromCrypt_v2(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *cipherText);
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *mpw_sitePasswordFromDerive_v2(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *resultParam);
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *mpw_siteState_v2(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *state);
|
2017-08-06 15:40:10 +00:00
|
|
|
|
|
|
|
// Algorithm version overrides.
|
|
|
|
static MPMasterKey mpw_masterKey_v3(
|
|
|
|
const char *fullName, const char *masterPassword) {
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
const char *keyScope = mpw_scopeForPurpose( MPKeyPurposeAuthentication );
|
2018-03-24 19:14:41 +00:00
|
|
|
trc( "keyScope: %s", keyScope );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Calculate the master key salt.
|
2018-03-24 19:14:41 +00:00
|
|
|
trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s",
|
2017-08-27 13:04:18 +00:00
|
|
|
keyScope, mpw_hex_l( (uint32_t)strlen( fullName ) ), fullName );
|
2015-01-15 22:43:41 +00:00
|
|
|
size_t masterKeySaltSize = 0;
|
|
|
|
uint8_t *masterKeySalt = NULL;
|
2017-08-06 15:40:10 +00:00
|
|
|
mpw_push_string( &masterKeySalt, &masterKeySaltSize, keyScope );
|
2017-08-27 13:04:18 +00:00
|
|
|
mpw_push_int( &masterKeySalt, &masterKeySaltSize, (uint32_t)strlen( fullName ) );
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName );
|
2015-01-15 22:43:41 +00:00
|
|
|
if (!masterKeySalt) {
|
2018-03-24 19:14:41 +00:00
|
|
|
err( "Could not allocate master key salt: %s", strerror( errno ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-24 19:14:41 +00:00
|
|
|
trc( " => masterKeySalt.id: %s", mpw_id_buf( masterKeySalt, masterKeySaltSize ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Calculate the master key.
|
2018-03-24 19:14:41 +00:00
|
|
|
trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )", MP_N, MP_r, MP_p );
|
2017-08-10 16:30:42 +00:00
|
|
|
MPMasterKey masterKey = mpw_kdf_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
|
2017-08-23 04:01:23 +00:00
|
|
|
mpw_free( &masterKeySalt, masterKeySaltSize );
|
2015-01-15 22:43:41 +00:00
|
|
|
if (!masterKey) {
|
2018-03-24 19:14:41 +00:00
|
|
|
err( "Could not derive master key: %s", strerror( errno ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-24 19:14:41 +00:00
|
|
|
trc( " => masterKey.id: %s", mpw_id_buf( masterKey, MPMasterKeySize ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
return masterKey;
|
|
|
|
}
|
|
|
|
|
2017-08-01 12:31:39 +00:00
|
|
|
static MPSiteKey mpw_siteKey_v3(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, const char *siteName, MPCounterValue siteCounter,
|
|
|
|
MPKeyPurpose keyPurpose, const char *keyContext) {
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
return mpw_siteKey_v2( masterKey, siteName, siteCounter, keyPurpose, keyContext );
|
2017-08-01 12:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
static const char *mpw_sitePasswordFromTemplate_v3(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *resultParam) {
|
2017-08-10 16:30:42 +00:00
|
|
|
|
|
|
|
return mpw_sitePasswordFromTemplate_v2( masterKey, siteKey, resultType, resultParam );
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *mpw_sitePasswordFromCrypt_v3(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *cipherText) {
|
2017-08-01 12:31:39 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
return mpw_sitePasswordFromCrypt_v2( masterKey, siteKey, resultType, cipherText );
|
2015-01-15 22:43:41 +00:00
|
|
|
}
|
2017-08-04 14:43:46 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
static const char *mpw_sitePasswordFromDerive_v3(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *resultParam) {
|
2017-08-04 14:43:46 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
return mpw_sitePasswordFromDerive_v2( masterKey, siteKey, resultType, resultParam );
|
2017-08-04 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
static const char *mpw_siteState_v3(
|
2017-08-30 13:39:35 +00:00
|
|
|
MPMasterKey masterKey, MPSiteKey siteKey, MPResultType resultType, const char *state) {
|
2017-08-04 14:43:46 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
return mpw_siteState_v2( masterKey, siteKey, resultType, state );
|
2017-08-04 14:43:46 +00:00
|
|
|
}
|