summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sys/crypto/idgen.c30
-rw-r--r--sys/crypto/idgen.h13
2 files changed, 24 insertions, 19 deletions
diff --git a/sys/crypto/idgen.c b/sys/crypto/idgen.c
index e96de57c6a0..b5512a44ec3 100644
--- a/sys/crypto/idgen.c
+++ b/sys/crypto/idgen.c
@@ -22,12 +22,11 @@
#include <sys/types.h>
#include <sys/systm.h>
+#include <sys/time.h>
+
#include <dev/rndvar.h>
-#include <crypto/idgen.h>
-static u_int16_t idgen32_g(u_int8_t *, int, u_int16_t);
-u_int32_t idgen32_permute(u_int8_t key[IDGEN32_KEYLEN], u_int32_t);
-void idgen32_rekey(struct idgen32_ctx *);
+#include <crypto/idgen.h>
static const u_int8_t ftable[256] = {
0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4,
@@ -81,7 +80,7 @@ idgen32_g(u_int8_t *key, int k, u_int16_t w)
return (g5 << 8) | g6;
}
-u_int32_t
+static u_int32_t
idgen32_permute(u_int8_t key[IDGEN32_KEYLEN], u_int32_t in)
{
u_int i, r;
@@ -102,19 +101,21 @@ idgen32_permute(u_int8_t key[IDGEN32_KEYLEN], u_int32_t in)
return (wl << 16) | wr;
}
-void
+static void
idgen32_rekey(struct idgen32_ctx *ctx)
{
- ctx->counter = 0;
- ctx->hibit ^= 0x80000000;
- arc4random_buf(ctx->key, sizeof(ctx->key));
+ ctx->id_counter = 0;
+ ctx->id_hibit ^= 0x80000000;
+ ctx->id_offset = arc4random();
+ arc4random_buf(ctx->id_key, sizeof(ctx->id_key));
+ ctx->id_rekey_time = time_second + IDGEN32_REKEY_TIME;
}
void
idgen32_init(struct idgen32_ctx *ctx)
{
bzero(ctx, sizeof(ctx));
- ctx->hibit = arc4random() & 0x80000000;
+ ctx->id_hibit = arc4random() & 0x80000000;
idgen32_rekey(ctx);
}
@@ -125,13 +126,14 @@ idgen32(struct idgen32_ctx *ctx)
/* Avoid emitting a zero ID as they often have special meaning */
do {
- ret = idgen32_permute(ctx->key, ctx->counter++) | ctx->hibit;
+ ret = idgen32_permute(ctx->id_key,
+ ctx->id_offset + ctx->id_counter++);
/* Rekey a little early to avoid "card counting" attack */
- if (ctx->counter > IDGEN32_REKEY_LIMIT)
+ if (ctx->id_counter > IDGEN32_REKEY_LIMIT ||
+ ctx->id_rekey_time > time_second)
idgen32_rekey(ctx);
} while (ret == 0);
- return ret;
+ return ret | ctx->id_hibit;
}
-
diff --git a/sys/crypto/idgen.h b/sys/crypto/idgen.h
index 588aa15aa64..02d5110c767 100644
--- a/sys/crypto/idgen.h
+++ b/sys/crypto/idgen.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: idgen.h,v 1.1 2008/06/09 22:47:41 djm Exp $ */
+/* $OpenBSD: idgen.h,v 1.2 2008/06/25 00:55:53 djm Exp $ */
/*
* Copyright (c) 2008 Damien Miller <djm@mindrot.org>
*
@@ -17,12 +17,15 @@
#define IDGEN32_ROUNDS 31
#define IDGEN32_KEYLEN 32
-#define IDGEN32_REKEY_LIMIT 0x7ff00000
+#define IDGEN32_REKEY_LIMIT 0x60000000
+#define IDGEN32_REKEY_TIME 600
struct idgen32_ctx {
- u_int32_t counter;
- u_int32_t hibit;
- u_int8_t key[IDGEN32_KEYLEN];
+ u_int32_t id_counter;
+ u_int32_t id_offset;
+ u_int32_t id_hibit;
+ u_int8_t id_key[IDGEN32_KEYLEN];
+ time_t id_rekey_time;
};
void idgen32_init(struct idgen32_ctx *);