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-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2017-07-23 04:48:38 +00:00
|
|
|
#include <ctype.h>
|
2017-08-10 16:30:42 +00:00
|
|
|
#include <errno.h>
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
#if MPW_COLOR
|
2015-02-09 20:51:12 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <curses.h>
|
|
|
|
#include <term.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-08 18:25:54 +00:00
|
|
|
#if HAS_CPERCIVA
|
2014-12-20 19:30:34 +00:00
|
|
|
#include <scrypt/crypto_scrypt.h>
|
2017-04-08 18:25:54 +00:00
|
|
|
#include <scrypt/sha256.h>
|
2017-04-08 20:20:58 +00:00
|
|
|
#elif HAS_SODIUM
|
2017-04-10 15:43:55 +00:00
|
|
|
#include "sodium.h"
|
2017-04-08 18:25:54 +00:00
|
|
|
#endif
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
#include "mpw-util.h"
|
2017-07-28 13:50:26 +00:00
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
#ifdef inf_level
|
2017-07-16 01:13:49 +00:00
|
|
|
int mpw_verbosity = inf_level;
|
2017-08-13 01:57:47 +00:00
|
|
|
#endif
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
bool mpw_push_buf(uint8_t **const buffer, size_t *const bufferSize, const void *pushBuffer, const size_t pushSize) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-08-03 03:13:51 +00:00
|
|
|
if (!buffer || !bufferSize || !pushBuffer || !pushSize)
|
2017-08-02 18:26:41 +00:00
|
|
|
return false;
|
2017-08-11 01:29:59 +00:00
|
|
|
if (*bufferSize == (size_t)ERR)
|
2014-12-21 17:37:21 +00:00
|
|
|
// The buffer was marked as broken, it is missing a previous push. Abort to avoid corrupt content.
|
2017-07-16 01:13:49 +00:00
|
|
|
return false;
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-08-05 23:04:42 +00:00
|
|
|
if (!mpw_realloc( buffer, bufferSize, pushSize )) {
|
2014-12-21 17:37:21 +00:00
|
|
|
// realloc failed, we can't push. Mark the buffer as broken.
|
2017-08-05 23:04:42 +00:00
|
|
|
mpw_free( *buffer, *bufferSize );
|
2017-08-04 13:36:03 +00:00
|
|
|
*bufferSize = (size_t)ERR;
|
2014-12-21 17:37:21 +00:00
|
|
|
*buffer = NULL;
|
2017-07-16 01:13:49 +00:00
|
|
|
return false;
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 23:04:42 +00:00
|
|
|
uint8_t *bufferOffset = *buffer + *bufferSize - pushSize;
|
|
|
|
memcpy( bufferOffset, pushBuffer, pushSize );
|
2017-07-16 01:13:49 +00:00
|
|
|
return true;
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
bool mpw_push_string(uint8_t **const buffer, size_t *const bufferSize, const char *pushString) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-08-02 18:26:41 +00:00
|
|
|
return pushString && mpw_push_buf( buffer, bufferSize, pushString, strlen( pushString ) );
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
bool mpw_string_push(char **const string, const char *pushString) {
|
|
|
|
|
2017-08-05 21:33:45 +00:00
|
|
|
if (!*string)
|
|
|
|
*string = calloc( 1, sizeof( char ) );
|
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
size_t stringLength = strlen( *string );
|
|
|
|
return pushString && mpw_push_buf( (uint8_t **const)string, &stringLength, pushString, strlen( pushString ) + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpw_string_pushf(char **const string, const char *pushFormat, ...) {
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start( args, pushFormat );
|
|
|
|
char *pushString = NULL;
|
|
|
|
bool success = vasprintf( &pushString, pushFormat, args ) >= 0 && mpw_string_push( string, pushString );
|
|
|
|
va_end( args );
|
|
|
|
mpw_free_string( pushString );
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
bool mpw_push_int(uint8_t **const buffer, size_t *const bufferSize, const uint32_t pushInt) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
return mpw_push_buf( buffer, bufferSize, &pushInt, sizeof( pushInt ) );
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 22:56:37 +00:00
|
|
|
bool __mpw_realloc(void **buffer, size_t *bufferSize, const size_t deltaSize) {
|
2017-08-05 23:04:42 +00:00
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
void *newBuffer = realloc( *buffer, (bufferSize? *bufferSize: 0) + deltaSize );
|
|
|
|
if (!newBuffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*buffer = newBuffer;
|
|
|
|
if (bufferSize)
|
|
|
|
*bufferSize += deltaSize;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
bool mpw_free(const void *buffer, const size_t bufferSize) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
if (!buffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memset( (void *)buffer, 0, bufferSize );
|
|
|
|
free( (void *)buffer );
|
|
|
|
return true;
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
bool mpw_free_string(const char *string) {
|
2014-12-22 04:45:19 +00:00
|
|
|
|
2017-07-23 04:48:38 +00:00
|
|
|
return string && mpw_free( string, strlen( string ) );
|
2014-12-22 04:45:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
uint8_t const *mpw_kdf_scrypt(const size_t keySize, const char *secret, const uint8_t *salt, const size_t saltSize,
|
2014-12-20 19:30:34 +00:00
|
|
|
uint64_t N, uint32_t r, uint32_t p) {
|
|
|
|
|
2016-07-06 05:16:10 +00:00
|
|
|
if (!secret || !salt)
|
|
|
|
return NULL;
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t *key = malloc( keySize );
|
|
|
|
if (!key)
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-08 18:25:54 +00:00
|
|
|
#if HAS_CPERCIVA
|
2014-12-20 19:30:34 +00:00
|
|
|
if (crypto_scrypt( (const uint8_t *)secret, strlen( secret ), salt, saltSize, N, r, p, key, keySize ) < 0) {
|
|
|
|
mpw_free( key, keySize );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-08 20:20:58 +00:00
|
|
|
#elif HAS_SODIUM
|
2017-07-28 13:50:26 +00:00
|
|
|
if (crypto_pwhash_scryptsalsa208sha256_ll( (const uint8_t *)secret, strlen( secret ), salt, saltSize, N, r, p, key, keySize ) != 0) {
|
2017-04-08 18:25:54 +00:00
|
|
|
mpw_free( key, keySize );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-05 21:33:45 +00:00
|
|
|
#else
|
|
|
|
#error No crypto support for mpw_scrypt.
|
2017-04-08 18:25:54 +00:00
|
|
|
#endif
|
2014-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
uint8_t const *mpw_kdf_blake2b(const size_t subkeySize, const uint8_t *key, const size_t keySize,
|
|
|
|
const uint8_t *context, const size_t contextSize, const uint64_t id, const char *personal) {
|
|
|
|
|
|
|
|
if (!key || !keySize || !subkeySize) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *subkey = malloc( subkeySize );
|
|
|
|
if (!subkey)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#if HAS_SODIUM
|
2017-08-10 16:45:25 +00:00
|
|
|
if (keySize < crypto_generichash_blake2b_KEYBYTES_MIN || keySize > crypto_generichash_blake2b_KEYBYTES_MAX ||
|
|
|
|
subkeySize < crypto_generichash_blake2b_KEYBYTES_MIN || subkeySize > crypto_generichash_blake2b_KEYBYTES_MAX ||
|
|
|
|
contextSize < crypto_generichash_blake2b_BYTES_MIN || contextSize > crypto_generichash_blake2b_BYTES_MAX ||
|
|
|
|
(personal && strlen( personal ) > crypto_generichash_blake2b_PERSONALBYTES)) {
|
2017-08-10 16:30:42 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
free( subkey );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t saltBuf[crypto_generichash_blake2b_SALTBYTES];
|
|
|
|
bzero( saltBuf, sizeof saltBuf );
|
|
|
|
if (id) {
|
|
|
|
uint64_t id_n = htonll( id );
|
|
|
|
memcpy( saltBuf, &id_n, sizeof id_n );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t personalBuf[crypto_generichash_blake2b_PERSONALBYTES];
|
|
|
|
bzero( personalBuf, sizeof saltBuf );
|
|
|
|
if (personal && strlen( personal ))
|
|
|
|
memcpy( personalBuf, personal, strlen( personal ) );
|
|
|
|
|
|
|
|
if (crypto_generichash_blake2b_salt_personal( subkey, subkeySize, context, contextSize, key, keySize, saltBuf, personalBuf ) != 0) {
|
|
|
|
mpw_free( subkey, subkeySize );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
2017-08-10 16:48:04 +00:00
|
|
|
#error No crypto support for mpw_kdf_blake2b.
|
2017-08-10 16:30:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return subkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t const *mpw_hash_hmac_sha256(const uint8_t *key, const size_t keySize, const uint8_t *message, const size_t messageSize) {
|
2017-08-05 21:33:45 +00:00
|
|
|
|
|
|
|
if (!key || !keySize || !message || !messageSize)
|
|
|
|
return NULL;
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-04-08 18:25:54 +00:00
|
|
|
#if HAS_CPERCIVA
|
2017-08-05 21:33:45 +00:00
|
|
|
uint8_t *const mac = malloc( 32 );
|
|
|
|
if (!mac)
|
2014-12-20 19:30:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
2017-08-05 21:33:45 +00:00
|
|
|
HMAC_SHA256_Buf( key, keySize, message, messageSize, mac );
|
2017-04-08 20:20:58 +00:00
|
|
|
#elif HAS_SODIUM
|
2017-08-05 21:33:45 +00:00
|
|
|
uint8_t *const mac = malloc( crypto_auth_hmacsha256_BYTES );
|
|
|
|
if (!mac)
|
2017-04-08 18:25:54 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
crypto_auth_hmacsha256_state state;
|
|
|
|
if (crypto_auth_hmacsha256_init( &state, key, keySize ) != 0 ||
|
2017-08-05 21:33:45 +00:00
|
|
|
crypto_auth_hmacsha256_update( &state, message, messageSize ) != 0 ||
|
|
|
|
crypto_auth_hmacsha256_final( &state, mac ) != 0) {
|
|
|
|
mpw_free( mac, crypto_auth_hmacsha256_BYTES );
|
2017-04-08 18:25:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-05 21:33:45 +00:00
|
|
|
#else
|
|
|
|
#error No crypto support for mpw_hmac_sha256.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mac;
|
|
|
|
}
|
2017-04-08 18:25:54 +00:00
|
|
|
|
2017-08-05 21:33:45 +00:00
|
|
|
static uint8_t const *mpw_aes(bool encrypt, const uint8_t *key, const size_t keySize, const uint8_t *buf, const size_t bufSize) {
|
|
|
|
|
|
|
|
#if HAS_SODIUM
|
|
|
|
if (!key || keySize < crypto_stream_KEYBYTES)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
uint8_t nonce[crypto_stream_NONCEBYTES];
|
|
|
|
bzero( (void *)nonce, sizeof( nonce ) );
|
|
|
|
|
2017-08-06 22:56:37 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2017-08-05 21:33:45 +00:00
|
|
|
if (encrypt) {
|
|
|
|
uint8_t *const cipherBuf = malloc( bufSize );
|
2017-08-06 01:52:00 +00:00
|
|
|
if (crypto_stream_aes128ctr_xor( cipherBuf, buf, bufSize, nonce, key ) != 0) {
|
|
|
|
mpw_free( cipherBuf, bufSize );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-05 21:33:45 +00:00
|
|
|
return cipherBuf;
|
2017-08-06 22:56:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-08-05 21:33:45 +00:00
|
|
|
uint8_t *const plainBuf = malloc( bufSize );
|
2017-08-06 01:52:00 +00:00
|
|
|
if (crypto_stream_aes128ctr( plainBuf, bufSize, nonce, key ) != 0) {
|
|
|
|
mpw_free( plainBuf, bufSize );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-05 21:33:45 +00:00
|
|
|
for (size_t c = 0; c < bufSize; ++c)
|
|
|
|
plainBuf[c] = buf[c] ^ plainBuf[c];
|
|
|
|
return plainBuf;
|
|
|
|
}
|
2017-08-06 22:56:37 +00:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#pragma GCC diagnostic pop
|
2017-08-05 21:33:45 +00:00
|
|
|
#else
|
|
|
|
#error No crypto support for mpw_aes.
|
2017-04-08 18:25:54 +00:00
|
|
|
#endif
|
2017-08-05 21:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t const *mpw_aes_encrypt(const uint8_t *key, const size_t keySize, const uint8_t *plainBuf, const size_t bufSize) {
|
|
|
|
|
|
|
|
return mpw_aes( true, key, keySize, plainBuf, bufSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t const *mpw_aes_decrypt(const uint8_t *key, const size_t keySize, const uint8_t *cipherBuf, const size_t bufSize) {
|
2017-04-08 18:25:54 +00:00
|
|
|
|
2017-08-05 21:33:45 +00:00
|
|
|
return mpw_aes( false, key, keySize, cipherBuf, bufSize );
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 12:31:39 +00:00
|
|
|
MPKeyID mpw_id_buf(const void *buf, size_t length) {
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return "<unset>";
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-04-08 18:25:54 +00:00
|
|
|
#if HAS_CPERCIVA
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t hash[32];
|
|
|
|
SHA256_Buf( buf, length, hash );
|
2017-04-08 20:20:58 +00:00
|
|
|
#elif HAS_SODIUM
|
2017-04-08 18:25:54 +00:00
|
|
|
uint8_t hash[crypto_hash_sha256_BYTES];
|
|
|
|
crypto_hash_sha256( hash, buf, length );
|
2017-08-05 21:33:45 +00:00
|
|
|
#else
|
|
|
|
#error No crypto support for mpw_id_buf.
|
2017-04-08 18:25:54 +00:00
|
|
|
#endif
|
2017-07-23 04:48:38 +00:00
|
|
|
|
|
|
|
return mpw_hex( hash, sizeof( hash ) / sizeof( uint8_t ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpw_id_buf_equals(const char *id1, const char *id2) {
|
|
|
|
|
|
|
|
size_t size = strlen( id1 );
|
|
|
|
if (size != strlen( id2 ))
|
|
|
|
return false;
|
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
for (size_t c = 0; c < size; ++c)
|
|
|
|
if (tolower( id1[c] ) != tolower( id2[c] ))
|
2017-07-23 04:48:38 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
static char *str_str;
|
|
|
|
|
|
|
|
const char *mpw_str(const char *format, ...) {
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start( args, format );
|
|
|
|
vasprintf( &str_str, format, args );
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
return str_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **mpw_hex_buf;
|
|
|
|
static unsigned int mpw_hex_buf_i;
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
const char *mpw_hex(const void *buf, size_t length) {
|
2015-02-27 13:49:04 +00:00
|
|
|
|
2017-08-02 18:26:41 +00:00
|
|
|
// FIXME: Not thread-safe
|
2017-08-03 05:07:19 +00:00
|
|
|
if (!mpw_hex_buf)
|
|
|
|
mpw_hex_buf = calloc( 10, sizeof( char * ) );
|
2015-03-11 21:30:34 +00:00
|
|
|
mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10;
|
|
|
|
|
2017-08-05 23:04:42 +00:00
|
|
|
if (mpw_realloc( &mpw_hex_buf[mpw_hex_buf_i], NULL, length * 2 + 1 ))
|
2017-08-06 22:56:37 +00:00
|
|
|
for (size_t kH = 0; kH < length; kH++)
|
|
|
|
sprintf( &(mpw_hex_buf[mpw_hex_buf_i][kH * 2]), "%02X", ((const uint8_t *)buf)[kH] );
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
return mpw_hex_buf[mpw_hex_buf_i];
|
2015-02-27 13:49:04 +00:00
|
|
|
}
|
2015-03-05 22:28:04 +00:00
|
|
|
|
2015-02-27 13:49:04 +00:00
|
|
|
const char *mpw_hex_l(uint32_t number) {
|
2015-03-05 22:28:04 +00:00
|
|
|
|
2015-02-27 13:49:04 +00:00
|
|
|
return mpw_hex( &number, sizeof( number ) );
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COLOR
|
|
|
|
static int putvari;
|
|
|
|
static char *putvarc = NULL;
|
2016-11-10 16:15:23 +00:00
|
|
|
static int termsetup;
|
2016-11-06 16:19:52 +00:00
|
|
|
static int initputvar() {
|
|
|
|
if (!isatty(STDERR_FILENO))
|
|
|
|
return 0;
|
2014-12-20 19:30:34 +00:00
|
|
|
if (putvarc)
|
2017-04-08 18:25:54 +00:00
|
|
|
free( putvarc );
|
2016-11-10 16:15:23 +00:00
|
|
|
if (!termsetup) {
|
|
|
|
int status;
|
2017-04-08 18:25:54 +00:00
|
|
|
if (! (termsetup = (setupterm( NULL, STDERR_FILENO, &status ) == 0 && status == 1))) {
|
2016-11-10 16:15:23 +00:00
|
|
|
wrn( "Terminal doesn't support color (setupterm errno %d).\n", status );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 16:19:52 +00:00
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
putvarc=(char *)calloc(256, sizeof(char));
|
|
|
|
putvari=0;
|
2016-11-06 16:19:52 +00:00
|
|
|
return 1;
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
static int putvar(int c) {
|
|
|
|
putvarc[putvari++]=c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *mpw_identicon(const char *fullName, const char *masterPassword) {
|
|
|
|
|
|
|
|
const char *leftArm[] = { "╔", "╚", "╰", "═" };
|
|
|
|
const char *rightArm[] = { "╗", "╝", "╯", "═" };
|
|
|
|
const char *body[] = { "█", "░", "▒", "▓", "☺", "☻" };
|
|
|
|
const char *accessory[] = {
|
|
|
|
"◈", "◎", "◐", "◑", "◒", "◓", "☀", "☁", "☂", "☃", "☄", "★", "☆", "☎", "☏", "⎈", "⌂", "☘", "☢", "☣",
|
|
|
|
"☕", "⌚", "⌛", "⏰", "⚡", "⛄", "⛅", "☔", "♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟",
|
2015-03-05 22:28:04 +00:00
|
|
|
"♨", "♩", "♪", "♫", "⚐", "⚑", "⚔", "⚖", "⚙", "⚠", "⌘", "⏎", "✄", "✆", "✈", "✉", "✌"
|
|
|
|
};
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
const uint8_t *identiconSeed = mpw_hash_hmac_sha256( (const uint8_t *)masterPassword, strlen( masterPassword ),
|
|
|
|
(const uint8_t *)fullName,
|
2017-07-28 13:50:26 +00:00
|
|
|
strlen( fullName ) );
|
2017-04-08 18:25:54 +00:00
|
|
|
if (!identiconSeed)
|
|
|
|
return NULL;
|
2014-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
char *colorString, *resetString;
|
|
|
|
#ifdef COLOR
|
2016-11-06 16:19:52 +00:00
|
|
|
if (initputvar()) {
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t colorIdentifier = (uint8_t)(identiconSeed[4] % 7 + 1);
|
|
|
|
tputs(tparm(tgetstr("AF", NULL), colorIdentifier), 1, putvar);
|
|
|
|
colorString = calloc(strlen(putvarc) + 1, sizeof(char));
|
|
|
|
strcpy(colorString, putvarc);
|
|
|
|
tputs(tgetstr("me", NULL), 1, putvar);
|
|
|
|
resetString = calloc(strlen(putvarc) + 1, sizeof(char));
|
|
|
|
strcpy(resetString, putvarc);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
colorString = calloc( 1, sizeof( char ) );
|
|
|
|
resetString = calloc( 1, sizeof( char ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
char *identicon = (char *)calloc( 256, sizeof( char ) );
|
|
|
|
snprintf( identicon, 256, "%s%s%s%s%s%s",
|
|
|
|
colorString,
|
|
|
|
leftArm[identiconSeed[0] % (sizeof( leftArm ) / sizeof( leftArm[0] ))],
|
|
|
|
body[identiconSeed[1] % (sizeof( body ) / sizeof( body[0] ))],
|
|
|
|
rightArm[identiconSeed[2] % (sizeof( rightArm ) / sizeof( rightArm[0] ))],
|
|
|
|
accessory[identiconSeed[3] % (sizeof( accessory ) / sizeof( accessory[0] ))],
|
|
|
|
resetString );
|
|
|
|
|
2017-04-08 18:25:54 +00:00
|
|
|
mpw_free( identiconSeed, 32 );
|
2014-12-20 19:30:34 +00:00
|
|
|
free( colorString );
|
|
|
|
free( resetString );
|
|
|
|
return identicon;
|
|
|
|
}
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2015-02-18 22:32:33 +00:00
|
|
|
/**
|
|
|
|
* @return the amount of bytes used by UTF-8 to encode a single character that starts with the given byte.
|
|
|
|
*/
|
2016-11-03 14:04:18 +00:00
|
|
|
static int mpw_utf8_sizeof(unsigned char utf8Byte) {
|
2015-02-18 22:32:33 +00:00
|
|
|
|
|
|
|
if (!utf8Byte)
|
|
|
|
return 0;
|
|
|
|
if ((utf8Byte & 0x80) == 0)
|
|
|
|
return 1;
|
|
|
|
if ((utf8Byte & 0xC0) != 0xC0)
|
|
|
|
return 0;
|
|
|
|
if ((utf8Byte & 0xE0) == 0xC0)
|
|
|
|
return 2;
|
|
|
|
if ((utf8Byte & 0xF0) == 0xE0)
|
|
|
|
return 3;
|
|
|
|
if ((utf8Byte & 0xF8) == 0xF0)
|
|
|
|
return 4;
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2015-02-18 22:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
const size_t mpw_utf8_strlen(const char *utf8String) {
|
2015-02-18 22:32:33 +00:00
|
|
|
|
|
|
|
size_t charlen = 0;
|
|
|
|
char *remainingString = (char *)utf8String;
|
2016-11-03 14:04:18 +00:00
|
|
|
for (int charByteSize; (charByteSize = mpw_utf8_sizeof( (unsigned char)*remainingString )); remainingString += charByteSize)
|
2015-02-18 22:32:33 +00:00
|
|
|
++charlen;
|
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
return charlen;
|
2015-01-15 22:43:41 +00:00
|
|
|
}
|