2015-01-15 22:43:41 +00:00
|
|
|
//
|
|
|
|
// mpw-algorithm.c
|
|
|
|
// MasterPassword
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 2014-12-20.
|
|
|
|
// Copyright (c) 2014 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
#define MP_N 32768
|
|
|
|
#define MP_r 8
|
|
|
|
#define MP_p 2
|
|
|
|
#define MP_hash PearlHashSHA256
|
|
|
|
|
|
|
|
static const uint8_t *mpw_masterKeyForUser_v2(const char *fullName, const char *masterPassword) {
|
|
|
|
|
|
|
|
const char *mpKeyScope = mpw_scopeForVariant( MPSiteVariantPassword );
|
2015-02-18 22:32:33 +00:00
|
|
|
trc( "algorithm: v%d\n", 2 );
|
2016-11-03 14:04:18 +00:00
|
|
|
trc( "fullName: %s (%zu)\n", fullName, mpw_utf8_strlen( fullName ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
trc( "masterPassword: %s\n", masterPassword );
|
|
|
|
trc( "key scope: %s\n", mpKeyScope );
|
|
|
|
|
|
|
|
// Calculate the master key salt.
|
|
|
|
// masterKeySalt = mpKeyScope . #fullName . fullName
|
|
|
|
size_t masterKeySaltSize = 0;
|
|
|
|
uint8_t *masterKeySalt = NULL;
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_push_string( &masterKeySalt, &masterKeySaltSize, mpKeyScope );
|
|
|
|
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) {
|
|
|
|
ftl( "Could not allocate master key salt: %d\n", errno );
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-11-03 14:04:18 +00:00
|
|
|
trc( "masterKeySalt ID: %s\n", mpw_id_buf( masterKeySalt, masterKeySaltSize ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Calculate the master key.
|
|
|
|
// masterKey = scrypt( masterPassword, masterKeySalt )
|
|
|
|
const uint8_t *masterKey = mpw_scrypt( MP_dkLen, masterPassword, masterKeySalt, masterKeySaltSize, MP_N, MP_r, MP_p );
|
|
|
|
mpw_free( masterKeySalt, masterKeySaltSize );
|
|
|
|
if (!masterKey) {
|
|
|
|
ftl( "Could not allocate master key: %d\n", errno );
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-11-03 14:04:18 +00:00
|
|
|
trc( "masterKey ID: %s\n", mpw_id_buf( masterKey, MP_dkLen ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
return masterKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *mpw_passwordForSite_v2(const uint8_t *masterKey, const char *siteName, const MPSiteType siteType, const uint32_t siteCounter,
|
|
|
|
const MPSiteVariant siteVariant, const char *siteContext) {
|
|
|
|
|
|
|
|
const char *siteScope = mpw_scopeForVariant( siteVariant );
|
2015-02-18 22:32:33 +00:00
|
|
|
trc( "algorithm: v%d\n", 2 );
|
2015-01-15 22:43:41 +00:00
|
|
|
trc( "siteName: %s\n", siteName );
|
|
|
|
trc( "siteCounter: %d\n", siteCounter );
|
|
|
|
trc( "siteVariant: %d\n", siteVariant );
|
|
|
|
trc( "siteType: %d\n", siteType );
|
2015-02-27 13:49:04 +00:00
|
|
|
trc( "site scope: %s, context: %s\n", siteScope, siteContext? "<empty>": siteContext );
|
|
|
|
trc( "seed from: hmac-sha256(masterKey, %s | %s | %s | %s | %s | %s)\n",
|
|
|
|
siteScope, mpw_hex_l( htonl( strlen( siteName ) ) ), siteName,
|
|
|
|
mpw_hex_l( htonl( siteCounter ) ),
|
|
|
|
mpw_hex_l( htonl( siteContext? strlen( siteContext ): 0 ) ), siteContext? "(null)": siteContext );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Calculate the site seed.
|
|
|
|
// sitePasswordSeed = hmac-sha256( masterKey, siteScope . #siteName . siteName . siteCounter . #siteContext . siteContext )
|
|
|
|
size_t sitePasswordInfoSize = 0;
|
|
|
|
uint8_t *sitePasswordInfo = NULL;
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_push_string( &sitePasswordInfo, &sitePasswordInfoSize, siteScope );
|
|
|
|
mpw_push_int( &sitePasswordInfo, &sitePasswordInfoSize, htonl( strlen( siteName ) ) );
|
|
|
|
mpw_push_string( &sitePasswordInfo, &sitePasswordInfoSize, siteName );
|
|
|
|
mpw_push_int( &sitePasswordInfo, &sitePasswordInfoSize, htonl( siteCounter ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
if (siteContext) {
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_push_int( &sitePasswordInfo, &sitePasswordInfoSize, htonl( strlen( siteContext ) ) );
|
|
|
|
mpw_push_string( &sitePasswordInfo, &sitePasswordInfoSize, siteContext );
|
2015-01-15 22:43:41 +00:00
|
|
|
}
|
|
|
|
if (!sitePasswordInfo) {
|
|
|
|
ftl( "Could not allocate site seed info: %d\n", errno );
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-11-03 14:04:18 +00:00
|
|
|
trc( "sitePasswordInfo ID: %s\n", mpw_id_buf( sitePasswordInfo, sitePasswordInfoSize ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
const uint8_t *sitePasswordSeed = mpw_hmac_sha256( masterKey, MP_dkLen, sitePasswordInfo, sitePasswordInfoSize );
|
|
|
|
mpw_free( sitePasswordInfo, sitePasswordInfoSize );
|
|
|
|
if (!sitePasswordSeed) {
|
|
|
|
ftl( "Could not allocate site seed: %d\n", errno );
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-11-03 14:04:18 +00:00
|
|
|
trc( "sitePasswordSeed ID: %s\n", mpw_id_buf( sitePasswordSeed, 32 ) );
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
// Determine the template.
|
|
|
|
const char *template = mpw_templateForType( siteType, sitePasswordSeed[0] );
|
|
|
|
trc( "type %d, template: %s\n", siteType, template );
|
|
|
|
if (strlen( template ) > 32) {
|
|
|
|
ftl( "Template too long for password seed: %lu", strlen( template ) );
|
|
|
|
mpw_free( sitePasswordSeed, sizeof( sitePasswordSeed ) );
|
|
|
|
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) {
|
|
|
|
sitePassword[c] = mpw_characterFromClass( template[c], sitePasswordSeed[c + 1] );
|
|
|
|
trc( "class %c, index %u (0x%02X) -> character: %c\n", template[c], sitePasswordSeed[c + 1], sitePasswordSeed[c + 1],
|
|
|
|
sitePassword[c] );
|
|
|
|
}
|
|
|
|
mpw_free( sitePasswordSeed, sizeof( sitePasswordSeed ) );
|
|
|
|
|
|
|
|
return sitePassword;
|
|
|
|
}
|