X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fsec%2Fprng%2Fx917.c;h=4111aa228d5f5942b5e50fb47472a08d7d3da40b;hb=4d8a6a97c6c0e15296b51d5f02674079bede1aa0;hp=63888c8f3b7ae99bb7f941fc294bc265c3b9b8e2;hpb=f2108fe515132a85c7e84e588789f482f8feb750;p=bertos.git diff --git a/bertos/sec/prng/x917.c b/bertos/sec/prng/x917.c index 63888c8f..4111aa22 100644 --- a/bertos/sec/prng/x917.c +++ b/bertos/sec/prng/x917.c @@ -33,6 +33,7 @@ * \brief ANSI X9.17 PRNG implementation * \author Giovanni Bajo * + * notest:avr */ #include "x917.h" @@ -44,30 +45,28 @@ static void x917_next(X917Context *ctx, BlockCipher *cipher, uint8_t *out) { const size_t blen = cipher_block_len(cipher); - union + struct { - uint8_t bytes[blen]; - struct - { - time_t t0; - hptime_t t1; - } data; + time_t t0; + hptime_t t1; + uint8_t padding[blen - sizeof(time_t) - sizeof(hptime_t)]; } DT; - ASSERT(sizeof(DT.bytes) >= sizeof(ticks_t) + sizeof(hptime_t)); + ASSERT(sizeof(DT) == blen); - DT.data.t0 = timer_clock(); - DT.data.t1 = timer_hw_hpread(); + memset(&DT, 0, sizeof(DT)); + DT.t0 = timer_clock(); + DT.t1 = timer_hw_hpread(); - cipher_ecb_encrypt(cipher, DT.bytes); + cipher_ecb_encrypt(cipher, &DT); - xor_block(out, DT.bytes, ctx->state, blen); + xor_block(out, (uint8_t*)&DT, ctx->state, blen); cipher_ecb_encrypt(cipher, out); - xor_block(ctx->state, DT.bytes, out, blen); + xor_block(ctx->state, (uint8_t*)&DT, out, blen); cipher_ecb_encrypt(cipher, ctx->state); - PURGE(DT.bytes); + PURGE(DT); } @@ -103,17 +102,24 @@ static void x917_reseed(PRNG *ctx_, const uint8_t *seed) // of the seed, and use the result as new seed. // * Generate and throw away a block to update the state. X917Context *ctx = (X917Context *)ctx_; + const size_t klen = sizeof(ctx->key); + const size_t blen = sizeof(ctx->state); - size_t klen = sizeof(ctx->key); - size_t blen = sizeof(ctx->state); - - uint8_t buf[klen]; - x917_generate(ctx_, buf, klen); + if (!ctx->rng.seeded) + { + memcpy(ctx->key, seed, klen); + memcpy(ctx->state, seed+klen, blen); + } + else + { + uint8_t buf[klen]; + x917_generate(ctx_, buf, klen); - xor_block(ctx->key, buf, seed, klen); - xor_block(ctx->state, ctx->state, seed+klen, blen); + xor_block(ctx->key, buf, seed, klen); + xor_block(ctx->state, ctx->state, seed+klen, blen); - PURGE(buf); + PURGE(buf); + } } /*********************************************************************/