2
0
MasterPassword/core/c/mpw-algorithm_v0.c

258 lines
10 KiB
C
Raw Normal View History

2017-04-05 20:56:22 +00:00
//==============================================================================
// This file is part of Master Password.
// Copyright (c) 2011-2017, Maarten Billemont.
//
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.
//
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.
//
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/>.
//==============================================================================
#include <string.h>
#include <errno.h>
#include <time.h>
#include "mpw-util.h"
#include "base64.h"
#define MP_N 32768LU
#define MP_r 8U
#define MP_p 2U
#define MP_otp_window 5 * 60 /* s */
// Algorithm version helpers.
static const char *mpw_templateForType_v0(MPResultType type, uint16_t templateIndex) {
2015-01-17 16:17:16 +00:00
size_t count = 0;
const char **templates = mpw_templatesForType( type, &count );
char const *template = templates && count? templates[templateIndex % count]: NULL;
free( templates );
return template;
2015-01-17 16:17:16 +00:00
}
static const char mpw_characterFromClass_v0(char characterClass, uint16_t classIndex) {
2015-01-17 16:17:16 +00:00
const char *classCharacters = mpw_charactersInClass( characterClass );
2017-08-02 18:26:41 +00:00
if (!classCharacters)
return '\0';
return classCharacters[classIndex % strlen( classCharacters )];
2015-01-17 16:17:16 +00:00
}
// Algorithm version overrides.
static MPMasterKey mpw_masterKey_v0(
const char *fullName, const char *masterPassword) {
const char *keyScope = mpw_scopeForPurpose( MPKeyPurposeAuthentication );
trc( "keyScope: %s", keyScope );
// Calculate the master key salt.
trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s",
2017-08-27 13:04:18 +00:00
keyScope, mpw_hex_l( (uint32_t)mpw_utf8_strlen( fullName ) ), fullName );
size_t masterKeySaltSize = 0;
uint8_t *masterKeySalt = NULL;
mpw_push_string( &masterKeySalt, &masterKeySaltSize, keyScope );
2017-08-27 13:04:18 +00:00
mpw_push_int( &masterKeySalt, &masterKeySaltSize, (uint32_t)mpw_utf8_strlen( fullName ) );
mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName );
if (!masterKeySalt) {
err( "Could not allocate master key salt: %s", strerror( errno ) );
return NULL;
}
trc( " => masterKeySalt.id: %s", mpw_id_buf( masterKeySalt, masterKeySaltSize ) );
// Calculate the master key.
trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )", MP_N, MP_r, MP_p );
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 );
if (!masterKey) {
err( "Could not derive master key: %s", strerror( errno ) );
return NULL;
}
trc( " => masterKey.id: %s", mpw_id_buf( masterKey, MPMasterKeySize ) );
return masterKey;
}
static MPSiteKey mpw_siteKey_v0(
MPMasterKey masterKey, const char *siteName, MPCounterValue siteCounter,
MPKeyPurpose keyPurpose, const char *keyContext) {
const char *keyScope = mpw_scopeForPurpose( keyPurpose );
trc( "keyScope: %s", keyScope );
// OTP counter value.
if (siteCounter == MPCounterValueTOTP)
siteCounter = ((uint32_t)time( NULL ) / MP_otp_window) * MP_otp_window;
// Calculate the site seed.
trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s",
2017-08-27 13:04:18 +00:00
keyScope, mpw_hex_l( (uint32_t)mpw_utf8_strlen( siteName ) ), siteName, mpw_hex_l( siteCounter ),
keyContext? mpw_hex_l( (uint32_t)mpw_utf8_strlen( keyContext ) ): NULL, keyContext );
size_t siteSaltSize = 0;
uint8_t *siteSalt = NULL;
mpw_push_string( &siteSalt, &siteSaltSize, keyScope );
2017-08-27 13:04:18 +00:00
mpw_push_int( &siteSalt, &siteSaltSize, (uint32_t)mpw_utf8_strlen( siteName ) );
mpw_push_string( &siteSalt, &siteSaltSize, siteName );
mpw_push_int( &siteSalt, &siteSaltSize, siteCounter );
if (keyContext) {
2017-08-27 13:04:18 +00:00
mpw_push_int( &siteSalt, &siteSaltSize, (uint32_t)mpw_utf8_strlen( keyContext ) );
mpw_push_string( &siteSalt, &siteSaltSize, keyContext );
}
2017-08-23 04:01:23 +00:00
if (!siteSalt) {
err( "Could not allocate site salt: %s", strerror( errno ) );
return NULL;
}
trc( " => siteSalt.id: %s", mpw_id_buf( siteSalt, siteSaltSize ) );
trc( "siteKey: hmac-sha256( masterKey.id=%s, siteSalt )",
mpw_id_buf( masterKey, MPMasterKeySize ) );
MPSiteKey siteKey = mpw_hash_hmac_sha256( masterKey, MPMasterKeySize, siteSalt, siteSaltSize );
2017-08-23 04:01:23 +00:00
mpw_free( &siteSalt, siteSaltSize );
if (!siteKey) {
err( "Could not derive site key: %s", strerror( errno ) );
return NULL;
}
trc( " => siteKey.id: %s", mpw_id_buf( siteKey, MPSiteKeySize ) );
return siteKey;
}
static const char *mpw_sitePasswordFromTemplate_v0(
MPMasterKey __unused masterKey, MPSiteKey siteKey, MPResultType resultType, const char __unused *resultParam) {
const char *_siteKey = (const char *)siteKey;
// Determine the template.
uint16_t seedByte;
mpw_uint16( (uint16_t)_siteKey[0], (uint8_t *)&seedByte );
const char *template = mpw_templateForType_v0( resultType, seedByte );
trc( "template: %u => %s", seedByte, template );
2017-08-02 18:26:41 +00:00
if (!template)
return NULL;
if (strlen( template ) > MPSiteKeySize) {
err( "Template too long for password seed: %zu", strlen( template ) );
return NULL;
}
// Encode the password from the seed using the template.
char *const sitePassword = calloc( strlen( template ) + 1, sizeof( char ) );
for (size_t c = 0; c < strlen( template ); ++c) {
mpw_uint16( (uint16_t)_siteKey[c + 1], (uint8_t *)&seedByte );
sitePassword[c] = mpw_characterFromClass_v0( template[c], seedByte );
trc( " - class: %c, index: %5u (0x%02hX) => character: %c",
template[c], seedByte, seedByte, sitePassword[c] );
}
trc( " => password: %s", sitePassword );
return sitePassword;
}
2017-08-04 14:43:46 +00:00
static const char *mpw_sitePasswordFromCrypt_v0(
MPMasterKey masterKey, MPSiteKey __unused siteKey, MPResultType __unused resultType, const char *cipherText) {
2017-08-04 14:43:46 +00:00
if (!cipherText) {
err( "Missing encrypted state." );
2017-08-06 01:52:00 +00:00
return NULL;
}
2017-08-04 14:43:46 +00:00
// Base64-decode
2017-08-06 22:56:37 +00:00
uint8_t *cipherBuf = calloc( 1, mpw_base64_decode_max( cipherText ) );
2017-09-24 17:06:19 +00:00
size_t bufSize = (size_t)mpw_base64_decode( cipherBuf, cipherText ), cipherBufSize = bufSize;
2017-08-06 22:56:37 +00:00
if ((int)bufSize < 0) {
2017-08-06 01:52:00 +00:00
err( "Base64 decoding error." );
2017-08-23 04:01:23 +00:00
mpw_free( &cipherBuf, mpw_base64_decode_max( cipherText ) );
2017-08-06 01:52:00 +00:00
return NULL;
}
trc( "b64 decoded: %zu bytes = %s", bufSize, mpw_hex( cipherBuf, bufSize ) );
// Decrypt
2017-09-24 17:06:19 +00:00
const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, &bufSize );
mpw_free( &cipherBuf, cipherBufSize );
const char *plainText = mpw_strndup( (char *)plainBytes, bufSize );
2017-08-23 04:01:23 +00:00
mpw_free( &plainBytes, bufSize );
2017-08-06 01:52:00 +00:00
if (!plainText)
err( "AES decryption error: %s", strerror( errno ) );
trc( "decrypted -> plainText: %zu bytes = %s = %s", strlen( plainText ), plainText, mpw_hex( plainText, strlen( plainText ) ) );
return plainText;
2017-08-04 14:43:46 +00:00
}
static const char *mpw_sitePasswordFromDerive_v0(
MPMasterKey __unused masterKey, MPSiteKey siteKey, MPResultType resultType, const char *resultParam) {
switch (resultType) {
case MPResultTypeDeriveKey: {
if (!resultParam) {
err( "Missing key size parameter." );
return NULL;
}
int resultParamInt = atoi( resultParam );
if (!resultParamInt)
resultParamInt = 512;
2017-08-10 16:45:25 +00:00
if (resultParamInt < 128 || resultParamInt > 512 || resultParamInt % 8 != 0) {
err( "Parameter is not a valid key size (should be 128 - 512): %s", resultParam );
return NULL;
}
uint16_t keySize = (uint16_t)(resultParamInt / 8);
trc( "keySize: %u", keySize );
// Derive key
const uint8_t *resultKey = mpw_kdf_blake2b( keySize, siteKey, MPSiteKeySize, NULL, 0, 0, NULL );
if (!resultKey) {
err( "Could not derive result key: %s", strerror( errno ) );
return NULL;
}
// Base64-encode
size_t b64Max = mpw_base64_encode_max( keySize );
2017-08-23 04:01:23 +00:00
char *b64Key = calloc( 1, b64Max + 1 );
if (mpw_base64_encode( b64Key, resultKey, keySize ) < 0) {
err( "Base64 encoding error." );
2017-08-23 04:01:23 +00:00
mpw_free_string( &b64Key );
}
2017-08-23 04:01:23 +00:00
else
trc( "b64 encoded -> key: %s", b64Key );
2017-08-23 04:01:23 +00:00
mpw_free( &resultKey, keySize );
2017-08-23 04:01:23 +00:00
return b64Key;
}
default:
err( "Unsupported derived password type: %d", resultType );
return NULL;
}
}
static const char *mpw_siteState_v0(
MPMasterKey masterKey, MPSiteKey __unused siteKey, MPResultType __unused resultType, const char *plainText) {
// Encrypt
size_t bufSize = strlen( plainText );
2017-09-24 17:06:19 +00:00
const uint8_t *cipherBuf = mpw_aes_encrypt( masterKey, MPMasterKeySize, (const uint8_t *)plainText, &bufSize );
if (!cipherBuf) {
err( "AES encryption error: %s", strerror( errno ) );
return NULL;
}
trc( "cipherBuf: %zu bytes = %s", bufSize, mpw_hex( cipherBuf, bufSize ) );
// Base64-encode
size_t b64Max = mpw_base64_encode_max( bufSize );
char *cipherText = calloc( 1, b64Max + 1 );
if (mpw_base64_encode( cipherText, cipherBuf, bufSize ) < 0) {
err( "Base64 encoding error." );
2017-08-23 04:01:23 +00:00
mpw_free_string( &cipherText );
}
2017-08-23 04:01:23 +00:00
else
trc( "b64 encoded -> cipherText: %s", cipherText );
2017-08-23 04:01:23 +00:00
mpw_free( &cipherBuf, bufSize );
return cipherText;
}