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>
|
2016-02-19 13:34:06 +00:00
|
|
|
#include <arpa/inet.h>
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2015-02-07 16:10:59 +00:00
|
|
|
#include "mpw-types.h"
|
2015-01-15 22:43:41 +00:00
|
|
|
#include "mpw-util.h"
|
2017-08-05 21:33:45 +00:00
|
|
|
#include "base64.h"
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
#define MP_N 32768LU
|
|
|
|
#define MP_r 8U
|
|
|
|
#define MP_p 2U
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
// Algorithm version helpers.
|
2017-08-01 17:45:54 +00:00
|
|
|
static const char *mpw_templateForType_v0(MPPasswordType type, uint16_t seedByte) {
|
2015-01-17 16:17:16 +00:00
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
const char **templates = mpw_templatesForType( type, &count );
|
2017-08-01 21:13:30 +00:00
|
|
|
char const *template = templates && count? templates[seedByte % count]: NULL;
|
2016-11-03 14:31:07 +00:00
|
|
|
free( templates );
|
|
|
|
return template;
|
2015-01-17 16:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char mpw_characterFromClass_v0(char characterClass, uint16_t seedByte) {
|
|
|
|
|
|
|
|
const char *classCharacters = mpw_charactersInClass( characterClass );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!classCharacters)
|
|
|
|
return '\0';
|
|
|
|
|
2015-01-17 16:17:16 +00:00
|
|
|
return classCharacters[seedByte % strlen( classCharacters )];
|
|
|
|
}
|
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
// Algorithm version overrides.
|
|
|
|
static MPMasterKey mpw_masterKey_v0(
|
|
|
|
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 );
|
|
|
|
trc( "keyScope: %s\n", keyScope );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Calculate the master key salt.
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "masterKeySalt: keyScope=%s | #fullName=%s | fullName=%s\n",
|
|
|
|
keyScope, mpw_hex_l( htonl( mpw_utf8_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 );
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_push_int( &masterKeySalt, &masterKeySaltSize, htonl( mpw_utf8_strlen( fullName ) ) );
|
|
|
|
mpw_push_string( &masterKeySalt, &masterKeySaltSize, fullName );
|
2015-01-15 22:43:41 +00:00
|
|
|
if (!masterKeySalt) {
|
2017-08-04 13:36:03 +00:00
|
|
|
err( "Could not allocate master key salt: %s\n", strerror( errno ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( " => masterKeySalt.id: %s\n", mpw_id_buf( masterKeySalt, masterKeySaltSize ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Calculate the master key.
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "masterKey: scrypt( masterPassword, masterKeySalt, N=%lu, r=%u, p=%u )\n", MP_N, MP_r, MP_p );
|
|
|
|
MPMasterKey masterKey = mpw_scrypt( MPMasterKeySize, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
|
2015-01-15 22:43:41 +00:00
|
|
|
mpw_free( masterKeySalt, masterKeySaltSize );
|
|
|
|
if (!masterKey) {
|
2017-08-04 13:36:03 +00:00
|
|
|
err( "Could not allocate master key: %s\n", strerror( errno ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( " => masterKey.id: %s\n", 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_v0(
|
|
|
|
MPMasterKey masterKey, const char *siteName, const uint32_t siteCounter,
|
2017-08-01 17:45:54 +00:00
|
|
|
const MPKeyPurpose keyPurpose, const char *keyContext) {
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-01 17:45:54 +00:00
|
|
|
const char *keyScope = mpw_scopeForPurpose( keyPurpose );
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "keyScope: %s\n", keyScope );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Calculate the site seed.
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "siteSalt: keyScope=%s | #siteName=%s | siteName=%s | siteCounter=%s | #keyContext=%s | keyContext=%s\n",
|
|
|
|
keyScope, mpw_hex_l( htonl( mpw_utf8_strlen( siteName ) ) ), siteName, mpw_hex_l( htonl( siteCounter ) ),
|
|
|
|
keyContext? mpw_hex_l( htonl( mpw_utf8_strlen( keyContext ) ) ): NULL, keyContext );
|
2017-08-01 17:45:54 +00:00
|
|
|
size_t siteSaltSize = 0;
|
|
|
|
uint8_t *siteSalt = NULL;
|
|
|
|
mpw_push_string( &siteSalt, &siteSaltSize, keyScope );
|
|
|
|
mpw_push_int( &siteSalt, &siteSaltSize, htonl( mpw_utf8_strlen( siteName ) ) );
|
|
|
|
mpw_push_string( &siteSalt, &siteSaltSize, siteName );
|
|
|
|
mpw_push_int( &siteSalt, &siteSaltSize, htonl( siteCounter ) );
|
|
|
|
if (keyContext) {
|
|
|
|
mpw_push_int( &siteSalt, &siteSaltSize, htonl( mpw_utf8_strlen( keyContext ) ) );
|
|
|
|
mpw_push_string( &siteSalt, &siteSaltSize, keyContext );
|
2015-01-15 22:43:41 +00:00
|
|
|
}
|
2017-08-01 17:45:54 +00:00
|
|
|
if (!siteSalt || !siteSaltSize) {
|
2017-08-04 13:36:03 +00:00
|
|
|
err( "Could not allocate site salt: %s\n", strerror( errno ) );
|
2017-08-01 17:45:54 +00:00
|
|
|
mpw_free( siteSalt, siteSaltSize );
|
2015-01-15 22:43:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( " => siteSalt.id: %s\n", mpw_id_buf( siteSalt, siteSaltSize ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "siteKey: hmac-sha256( masterKey.id=%s, siteSalt )\n",
|
|
|
|
mpw_id_buf( masterKey, MPMasterKeySize ) );
|
2017-08-01 17:45:54 +00:00
|
|
|
MPSiteKey siteKey = mpw_hmac_sha256( masterKey, MPMasterKeySize, siteSalt, siteSaltSize );
|
|
|
|
mpw_free( siteSalt, siteSaltSize );
|
2017-08-01 12:31:39 +00:00
|
|
|
if (!siteKey) {
|
2017-08-04 13:36:03 +00:00
|
|
|
err( "Could not allocate site key: %s\n", strerror( errno ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( " => siteKey.id: %s\n", mpw_id_buf( siteKey, MPSiteKeySize ) );
|
2017-08-01 12:31:39 +00:00
|
|
|
|
|
|
|
return siteKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *mpw_sitePassword_v0(
|
2017-08-01 17:45:54 +00:00
|
|
|
MPSiteKey siteKey, const MPPasswordType passwordType) {
|
2017-08-01 12:31:39 +00:00
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
// Determine the template.
|
2017-08-01 12:31:39 +00:00
|
|
|
const char *_siteKey = (const char *)siteKey;
|
2017-08-01 17:45:54 +00:00
|
|
|
const char *template = mpw_templateForType_v0( passwordType, htons( _siteKey[0] ) );
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( "template: %u => %s\n", htons( _siteKey[0] ), template );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!template)
|
|
|
|
return NULL;
|
2017-08-01 17:45:54 +00:00
|
|
|
if (strlen( template ) > MPSiteKeySize) {
|
2017-08-04 13:36:03 +00:00
|
|
|
err( "Template too long for password seed: %lu\n", strlen( template ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
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) {
|
2017-08-01 12:31:39 +00:00
|
|
|
sitePassword[c] = mpw_characterFromClass_v0( template[c], htons( _siteKey[c + 1] ) );
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( " - class: %c, index: %5u (0x%02hX) => character: %c\n",
|
2017-08-01 12:31:39 +00:00
|
|
|
template[c], htons( _siteKey[c + 1] ), htons( _siteKey[c + 1] ), sitePassword[c] );
|
2015-01-15 22:43:41 +00:00
|
|
|
}
|
2017-08-06 15:40:10 +00:00
|
|
|
trc( " => password: %s\n", sitePassword );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
return sitePassword;
|
|
|
|
}
|
2017-08-04 14:43:46 +00:00
|
|
|
|
|
|
|
const char *mpw_encrypt_v0(
|
|
|
|
MPMasterKey masterKey, const char *plainText) {
|
|
|
|
|
2017-08-05 21:33:45 +00:00
|
|
|
// Encrypt
|
|
|
|
size_t bufSize = strlen( plainText );
|
|
|
|
const uint8_t *cipherBuf = mpw_aes_encrypt( masterKey, MPMasterKeySize, (const uint8_t *)plainText, bufSize );
|
2017-08-06 01:52:00 +00:00
|
|
|
if (!cipherBuf) {
|
|
|
|
err( "AES encryption error: %s\n", strerror( errno ) );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-06 03:42:47 +00:00
|
|
|
trc( "cipherBuf: %lu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) );
|
2017-08-06 01:52:00 +00:00
|
|
|
|
2017-08-05 21:33:45 +00:00
|
|
|
// Base64-encode
|
2017-08-06 01:52:00 +00:00
|
|
|
size_t b64Max = mpw_base64_encode_max( bufSize );
|
|
|
|
char *cipherText = calloc( 1, b64Max + 1 );
|
|
|
|
if (mpw_base64_encode( cipherText, b64Max, cipherBuf, bufSize ) < 0) {
|
|
|
|
err( "Base64 encoding error." );
|
|
|
|
mpw_free_string( cipherText );
|
|
|
|
cipherText = NULL;
|
|
|
|
}
|
2017-08-06 03:42:47 +00:00
|
|
|
trc( "b64 encoded -> cipherText: %s = %s\n", cipherText, mpw_hex( cipherText, sizeof( cipherText ) ) );
|
2017-08-05 21:33:45 +00:00
|
|
|
mpw_free( cipherBuf, bufSize );
|
|
|
|
|
|
|
|
return cipherText;
|
2017-08-04 14:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *mpw_decrypt_v0(
|
|
|
|
MPMasterKey masterKey, const char *cipherText) {
|
|
|
|
|
2017-08-05 21:33:45 +00:00
|
|
|
// Base64-decode
|
2017-08-06 01:52:00 +00:00
|
|
|
size_t bufSize = mpw_base64_decode_max( cipherText );
|
2017-08-05 21:33:45 +00:00
|
|
|
uint8_t *cipherBuf = calloc( 1, bufSize );
|
2017-08-06 01:52:00 +00:00
|
|
|
if ((bufSize = (size_t)mpw_base64_decode( cipherBuf, bufSize, cipherText )) < 0) {
|
|
|
|
err( "Base64 decoding error." );
|
|
|
|
mpw_free( cipherBuf, mpw_base64_decode_max( cipherText ) );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-06 03:42:47 +00:00
|
|
|
trc( "b64 decoded: %lu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) );
|
2017-08-05 21:33:45 +00:00
|
|
|
|
|
|
|
// Decrypt
|
2017-08-06 03:42:47 +00:00
|
|
|
const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, bufSize );
|
|
|
|
const char *plainText = strndup( (char *)plainBytes, bufSize );
|
|
|
|
mpw_free( plainBytes, bufSize );
|
2017-08-06 01:52:00 +00:00
|
|
|
if (!plainText)
|
|
|
|
err( "AES decryption error: %s\n", strerror( errno ) );
|
2017-08-06 03:42:47 +00:00
|
|
|
trc( "decrypted -> plainText: %s = %s\n", plainText, mpw_hex( plainText, sizeof( plainText ) ) );
|
2017-08-05 21:33:45 +00:00
|
|
|
mpw_free( cipherBuf, bufSize );
|
|
|
|
|
|
|
|
return plainText;
|
2017-08-04 14:43:46 +00:00
|
|
|
}
|