From b5d9b33b6f2e36e51ccfed4c02b3998ac4afe150 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 29 Apr 2016 14:18:43 +0000 Subject: [PATCH] Refactor rand * Removed dependency from internal headers in rand.h. xmpp_rand interface can be public in the future; * xmpp_rand functions accept xmpp_rand_t object instead of xmpp_ctx_t. --- src/auth.c | 9 +++++---- src/rand.c | 54 ++++++++++++++++++++++++++++++------------------------ src/rand.h | 42 ++++++++++++++++++++++++++++++------------ src/sasl.c | 2 +- src/uuid.c | 4 ++-- 5 files changed, 68 insertions(+), 43 deletions(-) diff --git a/src/auth.c b/src/auth.c index d056424..d6dee85 100644 --- a/src/auth.c +++ b/src/auth.c @@ -490,22 +490,23 @@ err: static char *_make_scram_sha1_init_msg(xmpp_conn_t * const conn) { + xmpp_ctx_t *ctx = conn->ctx; size_t message_len; char *node; char *message; char nonce[32]; - node = xmpp_jid_node(conn->ctx, conn->jid); + node = xmpp_jid_node(ctx, conn->jid); if (!node) { return NULL; } - xmpp_rand_nonce(conn->ctx, nonce, sizeof(nonce)); + xmpp_rand_nonce(ctx->rand, nonce, sizeof(nonce)); message_len = strlen(node) + strlen(nonce) + 8 + 1; - message = xmpp_alloc(conn->ctx, message_len); + message = xmpp_alloc(ctx, message_len); if (message) { xmpp_snprintf(message, message_len, "n,,n=%s,r=%s", node, nonce); } - xmpp_free(conn->ctx, node); + xmpp_free(ctx, node); return message; } diff --git a/src/rand.c b/src/rand.c index 473ac7a..fbdfce4 100644 --- a/src/rand.c +++ b/src/rand.c @@ -16,13 +16,19 @@ * Hash function is SHA1. */ -#include -#include -#include +/** @defgroup Random Pseudo-random number generator + */ -#include "common.h" -#include "ostypes.h" +#include +#include /* memeset */ +#include /* clock, time */ + +#include "common.h" /* xmpp_alloc, xmpp_free */ +#include "ostypes.h" /* uint8_t, uint32_t, size_t */ #include "sha1.h" +#include "snprintf.h" /* xmpp_snprintf */ + +#include "rand.h" /* xmpp_rand_t */ #define outlen SHA1_DIGEST_SIZE #define seedlen (440 / 8) @@ -214,30 +220,31 @@ do { \ } \ } while (0) -static void xmpp_rand_reseed(xmpp_ctx_t *ctx) +static void xmpp_rand_reseed(xmpp_rand_t *rand) { uint8_t entropy[ENTROPY_MAX]; uint8_t *ptr = entropy; const uint8_t *last = entropy + sizeof(entropy); size_t len; - xmpp_rand_t *rand = ctx->rand; /* entropy: * 1. time(2) * 2. clock(3) if != -1 - * 3. xmpp_ctx_t address to make unique seed within one process + * 3. xmpp_rand_t address to make unique seed within one process * 4. counter to make unique seed within one context - * 5. local ports of every connection in list (getsockname) - * 6. other non-constant info that can be retieved from socket + * 5. stack address + * 6. local ports of every connection in list (getsockname) + * 7. other non-constant info that can be retieved from socket * * rand(3) can't be used as it isn't thread-safe. - * XXX 5 and 6 not implemented yet. + * XXX 6 and 7 are not implemented yet. */ ENTROPY_ACCUMULATE(ptr, last, time_t, time(NULL)); ENTROPY_ACCUMULATE(ptr, last, clock_t, clock()); - ENTROPY_ACCUMULATE(ptr, last, void *, ctx); + ENTROPY_ACCUMULATE(ptr, last, void *, rand); ENTROPY_ACCUMULATE(ptr, last, unsigned, ++rand->reseed_count); + ENTROPY_ACCUMULATE(ptr, last, void *, &entropy); len = ptr - entropy; if (rand->inited) { @@ -262,35 +269,34 @@ void xmpp_rand_free(xmpp_ctx_t *ctx, xmpp_rand_t *rand) xmpp_free(ctx, rand); } -void xmpp_rand_bytes(xmpp_ctx_t *ctx, uint8_t *output, size_t len) +void xmpp_rand_bytes(xmpp_rand_t *rand, unsigned char *output, size_t len) { int rc; - xmpp_rand_t *rand = ctx->rand; - rc = Hash_DRBG_Generate(&rand->ctx, output, len); + rc = Hash_DRBG_Generate(&rand->ctx, (uint8_t *)output, len); if (rc == RESEED_NEEDED) { - xmpp_rand_reseed(ctx); - rc = Hash_DRBG_Generate(&rand->ctx, output, len); + xmpp_rand_reseed(rand); + rc = Hash_DRBG_Generate(&rand->ctx, (uint8_t *)output, len); assert(rc == 0); } } -int xmpp_rand(xmpp_ctx_t *ctx) +int xmpp_rand(xmpp_rand_t *rand) { int result; - xmpp_rand_bytes(ctx, (uint8_t *)&result, sizeof(result)); + xmpp_rand_bytes(rand, (unsigned char *)&result, sizeof(result)); return result; } -void xmpp_rand_nonce(xmpp_ctx_t *ctx, char *output, size_t len) +void xmpp_rand_nonce(xmpp_rand_t *rand, char *output, size_t len) { size_t i; size_t rand_len = len / 2; #ifndef _MSC_VER - uint8_t rand_buf[rand_len]; + unsigned char rand_buf[rand_len]; #else - uint8_t* rand_buf = (uint8_t*)_alloca(rand_len); + unsigned char *rand_buf = (unsigned char *)_alloca(rand_len); #endif /* current implementation returns printable HEX representation of @@ -299,9 +305,9 @@ void xmpp_rand_nonce(xmpp_ctx_t *ctx, char *output, size_t len) * as result can fail. */ - xmpp_rand_bytes(ctx, rand_buf, rand_len); + xmpp_rand_bytes(rand, rand_buf, rand_len); for (i = 0; i < rand_len; ++i) { - xmpp_snprintf(output + i * 2, len, "%02x", (unsigned char)rand_buf[i]); + xmpp_snprintf(output + i * 2, len, "%02x", rand_buf[i]); len -= 2; } } diff --git a/src/rand.h b/src/rand.h index 0879ce9..0a605c4 100644 --- a/src/rand.h +++ b/src/rand.h @@ -16,25 +16,43 @@ #ifndef __LIBSTROPHE_RAND_H__ #define __LIBSTROPHE_RAND_H__ -#include "strophe.h" -#include "ostypes.h" +#include /* size_t */ +#include "strophe.h" /* xmpp_ctx_t */ typedef struct _xmpp_rand_t xmpp_rand_t; +/** Create new xmpp_rand_t object. + * + * @ingroup Random + */ xmpp_rand_t *xmpp_rand_new(xmpp_ctx_t *ctx); +/** Destroy an xmpp_rand_t object. + * + * @ingroup Random + */ void xmpp_rand_free(xmpp_ctx_t *ctx, xmpp_rand_t *rand); -/** Analogue of rand(3). */ -int xmpp_rand(xmpp_ctx_t *ctx); - -/** Generates random bytes. */ -void xmpp_rand_bytes(xmpp_ctx_t *ctx, uint8_t *output, size_t len); - -/** Generates a nonce that is printable randomized string. +/** Generate random integer + * Analogue of rand(3). * - * @param len Number of bytes reserved for the output string, including - * end of line '\0'. + * @ingroup Random */ -void xmpp_rand_nonce(xmpp_ctx_t *ctx, char *output, size_t len); +int xmpp_rand(xmpp_rand_t *rand); + +/** Generate random bytes. + * Generates len bytes and stores them to the output buffer. + * + * @ingroup Random + */ +void xmpp_rand_bytes(xmpp_rand_t *rand, unsigned char *output, size_t len); + +/** Generate a nonce that is printable randomized string. + * + * @param len Number of bytes reserved for the output string, including + * end of line '\0'. + * + * @ingroup Random + */ +void xmpp_rand_nonce(xmpp_rand_t *rand, char *output, size_t len); #endif /* __LIBSTROPHE_RAND_H__ */ diff --git a/src/sasl.c b/src/sasl.c index 332947d..08af34d 100644 --- a/src/sasl.c +++ b/src/sasl.c @@ -252,7 +252,7 @@ char *sasl_digest_md5(xmpp_ctx_t *ctx, const char *challenge, /* add our response fields */ hash_add(table, "username", xmpp_strdup(ctx, node)); - xmpp_rand_nonce(ctx, cnonce, sizeof(cnonce)); + xmpp_rand_nonce(ctx->rand, cnonce, sizeof(cnonce)); hash_add(table, "cnonce", xmpp_strdup(ctx, cnonce)); hash_add(table, "nc", xmpp_strdup(ctx, "00000001")); hash_add(table, "qop", xmpp_strdup(ctx, "auth")); diff --git a/src/uuid.c b/src/uuid.c index 18ed0f3..6a5dc9f 100644 --- a/src/uuid.c +++ b/src/uuid.c @@ -28,14 +28,14 @@ */ static void crypto_uuid_gen(xmpp_ctx_t *ctx, char *uuid) { - uint8_t buf[16]; + unsigned char buf[16]; int i = 0; /* uuid iterator */ int j = 0; /* buf iterator */ static const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - xmpp_rand_bytes(ctx, buf, sizeof(buf)); + xmpp_rand_bytes(ctx->rand, buf, sizeof(buf)); buf[8] &= 0x3f; buf[8] |= 0x80; buf[6] &= 0x0f;