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
|
|
|
|
2016-01-04 19:52:05 +00:00
|
|
|
#include <stdio.h>
|
2015-01-20 04:21:10 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-12-22 04:45:19 +00:00
|
|
|
//// Logging.
|
|
|
|
|
2016-01-04 19:52:05 +00:00
|
|
|
#ifndef trc
|
2017-04-08 18:25:54 +00:00
|
|
|
extern int mpw_verbosity;
|
2016-01-04 19:52:05 +00:00
|
|
|
#define trc_level 3
|
|
|
|
#define trc(...) \
|
|
|
|
if (mpw_verbosity >= 3) \
|
|
|
|
fprintf( stderr, __VA_ARGS__ )
|
|
|
|
#endif
|
|
|
|
#ifndef dbg
|
|
|
|
#define dbg_level 2
|
|
|
|
#define dbg(...) \
|
|
|
|
if (mpw_verbosity >= 2) \
|
|
|
|
fprintf( stderr, __VA_ARGS__ )
|
|
|
|
#endif
|
|
|
|
#ifndef inf
|
|
|
|
#define inf_level 1
|
|
|
|
#define inf(...) \
|
|
|
|
if (mpw_verbosity >= 1) \
|
|
|
|
fprintf( stderr, __VA_ARGS__ )
|
|
|
|
#endif
|
|
|
|
#ifndef wrn
|
|
|
|
#define wrn_level 0
|
|
|
|
#define wrn(...) \
|
|
|
|
if (mpw_verbosity >= 0) \
|
|
|
|
fprintf( stderr, __VA_ARGS__ )
|
|
|
|
#endif
|
|
|
|
#ifndef err
|
|
|
|
#define err_level -1
|
|
|
|
#define err(...) \
|
|
|
|
if (mpw_verbosity >= -1) \
|
|
|
|
fprintf( stderr, __VA_ARGS__ )
|
2015-02-02 19:47:16 +00:00
|
|
|
#endif
|
2014-12-22 04:45:19 +00:00
|
|
|
#ifndef ftl
|
2016-01-04 19:52:05 +00:00
|
|
|
#define ftl_level -2
|
|
|
|
#define ftl(...) \
|
|
|
|
do { \
|
|
|
|
if (mpw_verbosity >= -2) \
|
|
|
|
fprintf( stderr, __VA_ARGS__ ); \
|
|
|
|
exit( 2 ); \
|
|
|
|
} while (0)
|
2014-12-22 04:45:19 +00:00
|
|
|
#endif
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
//// Buffers and memory.
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
#define mpw_alloc_array(_count, _type, ...) ({ \
|
2015-02-09 18:02:22 +00:00
|
|
|
_type stackElements[] = { __VA_ARGS__ }; \
|
2015-02-07 16:10:59 +00:00
|
|
|
_count = sizeof( stackElements ) / sizeof( _type ); \
|
|
|
|
_type *allocElements = malloc( sizeof( stackElements ) ); \
|
|
|
|
memcpy( allocElements, stackElements, sizeof( stackElements ) ); \
|
|
|
|
allocElements; \
|
|
|
|
})
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
/** Push a buffer onto a buffer. reallocs the given buffer and appends the given buffer. */
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_push_buf(
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t **const buffer, size_t *const bufferSize, const void *pushBuffer, const size_t pushSize);
|
|
|
|
/** Push a string onto a buffer. reallocs the given buffer and appends the given string. */
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_push_string(
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t **buffer, size_t *const bufferSize, const char *pushString);
|
|
|
|
/** Push an integer onto a buffer. reallocs the given buffer and appends the given integer. */
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_push_int(
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t **const buffer, size_t *const bufferSize, const uint32_t pushInt);
|
|
|
|
/** Free a buffer after zero'ing its contents. */
|
|
|
|
void mpw_free(
|
|
|
|
const void *buffer, const size_t bufferSize);
|
2014-12-22 04:45:19 +00:00
|
|
|
/** Free a string after zero'ing its contents. */
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_free_string(
|
2014-12-22 04:45:19 +00:00
|
|
|
const char *string);
|
2014-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
//// Cryptographic functions.
|
|
|
|
|
|
|
|
/** Perform a scrypt-based key derivation on the given key using the given salt and scrypt parameters.
|
|
|
|
* @return A new keySize-size allocated buffer. */
|
|
|
|
uint8_t const *mpw_scrypt(
|
|
|
|
const size_t keySize, const char *secret, const uint8_t *salt, const size_t saltSize,
|
|
|
|
uint64_t N, uint32_t r, uint32_t p);
|
|
|
|
/** Calculate a SHA256-based HMAC by encrypting the given salt with the given key.
|
|
|
|
* @return A new 32-byte allocated buffer. */
|
|
|
|
uint8_t const *mpw_hmac_sha256(
|
|
|
|
const uint8_t *key, const size_t keySize, const uint8_t *salt, const size_t saltSize);
|
|
|
|
|
|
|
|
//// Visualizers.
|
|
|
|
|
|
|
|
/** Encode a buffer as a string of hexadecimal characters.
|
2015-01-15 22:43:41 +00:00
|
|
|
* @return A C-string in a reused buffer, do not free or store it. */
|
2014-12-20 19:30:34 +00:00
|
|
|
const char *mpw_hex(const void *buf, size_t length);
|
2015-02-27 13:49:04 +00:00
|
|
|
const char *mpw_hex_l(uint32_t number);
|
2014-12-20 19:30:34 +00:00
|
|
|
/** Encode a fingerprint for a buffer.
|
2015-01-15 22:43:41 +00:00
|
|
|
* @return A C-string in a reused buffer, do not free or store it. */
|
2016-11-03 14:04:18 +00:00
|
|
|
const char *mpw_id_buf(const void *buf, size_t length);
|
2014-12-20 19:30:34 +00:00
|
|
|
/** Encode a visual fingerprint for a user.
|
|
|
|
* @return A newly allocated string. */
|
|
|
|
const char *mpw_identicon(const char *fullName, const char *masterPassword);
|
2015-01-15 22:43:41 +00:00
|
|
|
|
|
|
|
//// String utilities.
|
|
|
|
|
2015-02-18 22:32:33 +00:00
|
|
|
/** @return The amount of display characters in the given UTF-8 string. */
|
2016-11-03 14:04:18 +00:00
|
|
|
const size_t mpw_utf8_strlen(const char *utf8String);
|