Add strn_digest_passphrase()

Uses SHA512 internally
This commit is contained in:
Andrew Bettison 2014-11-13 10:01:08 +10:30
parent c3fdb12991
commit c1d8383226
3 changed files with 32 additions and 1 deletions

View File

@ -26,6 +26,7 @@ SERVAL_CLIENT_SOURCES = \
strbuf_helpers.c \
str.c \
strlcpy.c \
sha2.c \
uuid.c \
whence.c \
xprintf.c
@ -96,7 +97,6 @@ SERVAL_DAEMON_SOURCES = \
rhizome_sync.c \
serval_packetvisualise.c \
server.c \
sha2.c \
vomp.c \
vomp_console.c \
fec-3.0.1/ccsds_tables.c \

19
str.c
View File

@ -21,6 +21,7 @@
#include "str.h"
#include "strbuf_helpers.h"
#include "constants.h"
#include "sha2.h"
#include <stdlib.h>
#include <stdio.h>
@ -975,6 +976,24 @@ size_t strn_fromprint(unsigned char *dst, size_t dstsiz, const char *src, size_t
return dst - odst;
}
void str_digest_passphrase(unsigned char *dstBinary, size_t dstlen, const char *passphrase)
{
return strn_digest_passphrase(dstBinary, dstlen, passphrase, strlen(passphrase));
}
void strn_digest_passphrase(unsigned char *dstBinary, size_t dstlen, const char *passphrase, size_t passlen)
{
assert(dstlen <= SERVAL_PASSPHRASE_DIGEST_MAX_BINARY);
SHA512_CTX context;
static const char salt1[] = "Sago pudding";
static const char salt2[] = "Rhubarb pie";
SHA512_Init(&context);
SHA512_Update(&context, (unsigned char *)salt1, sizeof salt1 - 1);
SHA512_Update(&context, (unsigned char *)passphrase, passlen);
SHA512_Update(&context, (unsigned char *)salt2, sizeof salt2 - 1);
SHA512_Final_Len(dstBinary, dstlen, &context);
}
/* Return true if the string resembles a URI.
* Based on RFC-3986 generic syntax, assuming nothing about the hierarchical part.
*

12
str.h
View File

@ -286,6 +286,18 @@ size_t strn_fromprint(unsigned char *dst, size_t dstsiz, const char *src, size_t
#define alloca_str_toprint_quoted(str, quotes) toprint_str((char *)alloca(toprint_str_len((str), (quotes)) + 1), -1, (str), (quotes))
#define alloca_str_toprint(str) alloca_str_toprint_quoted(str, "``")
/* -------------------- Pass phrases -------------------- */
#define SERVAL_PASSPHRASE_DIGEST_MAX_BINARY 64
/* Digest a pass phrase into binary data of at most
* SERVAL_PASSPHRASE_DIGEST_MAX_BINARY bytes using a strong one-way function.
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
void str_digest_passphrase(unsigned char *dstBinary, size_t dstlen, const char *passphrase);
void strn_digest_passphrase(unsigned char *dstBinary, size_t dstlen, const char *passphrase, size_t passlen);
/* -------------------- Useful string primitives -------------------- */
/* Like strchr(3), but only looks for 'c' in the first 'n' characters of 's', stopping at the first