+
+enum {kBSizeInBits = 160}; // MUST be a multiple of 32!!!
+enum {kBSizeInBytes = kBSizeInBits / 8};
+typedef u_int32_t BlockWord;
+enum {kWordSizeInBits = 32};
+enum {kBSize = 5};
+typedef BlockWord Block[kBSize];
+
+/* define prototypes to keep the compiler happy... */
+
+void add_blocks(Block a, Block b, BlockWord carry);
+void fips_initialize(void);
+void random_block(Block b);
+
+/*
+ * Get 120 bits from yarrow
+ */
+
+/*
+ * add block b to block a
+ */
+void
+add_blocks(Block a, Block b, BlockWord carry)
+{
+ int i = kBSize;
+ while (--i >= 0)
+ {
+ u_int64_t c = (u_int64_t)carry +
+ (u_int64_t)a[i] +
+ (u_int64_t)b[i];
+ a[i] = c & ((1LL << kWordSizeInBits) - 1);
+ carry = c >> kWordSizeInBits;
+ }
+}
+
+
+
+struct sha1_ctxt g_sha1_ctx;
+char zeros[(512 - kBSizeInBits) / 8];
+Block g_xkey;
+Block g_random_data;
+int g_bytes_used;
+
+/*
+ * Setup for fips compliance
+ */
+
+/*
+ * get a random block of data per fips 186-2
+ */
+void
+random_block(Block b)
+{
+ // do one iteration
+ Block xSeed;
+ prngOutput (gPrngRef, (BYTE*) &xSeed, sizeof (xSeed));
+
+ // add the seed to the previous value of g_xkey
+ add_blocks (g_xkey, xSeed, 0);
+
+ // compute "G"
+ SHA1Update (&g_sha1_ctx, (const u_int8_t *) &g_xkey, sizeof (g_xkey));
+
+ // add zeros to fill the internal SHA-1 buffer
+ SHA1Update (&g_sha1_ctx, (const u_int8_t *)zeros, sizeof (zeros));
+
+ // write the resulting block
+ memmove(b, g_sha1_ctx.h.b8, sizeof (Block));
+
+ // fix up the next value of g_xkey
+ add_blocks (g_xkey, b, 1);
+}
+