Re: [PATCH net-next 1/2] random32: improvements to prandom_bytes
From: David Miller <davem@davemloft.net>
Date: 2014-08-01 05:28:05
From: David Miller <davem@davemloft.net>
Date: 2014-08-01 05:28:05
From: Daniel Borkmann <redacted> Date: Thu, 31 Jul 2014 22:11:16 +0200
- for (j = 0; j < sizeof(u32); j++) {
- p[i + j] = random;
- random >>= BITS_PER_BYTE;
- }
+ while (bytes > sizeof(u32)) {
+ put_unaligned(prandom_u32_state(state), (u32 *) ptr);
+ ptr += sizeof(u32);
+ bytes -= sizeof(u32);
}
- if (i < bytes) {
- u32 random = prandom_u32_state(state);
- for (; i < bytes; i++) {
- p[i] = random;
- random >>= BITS_PER_BYTE;
- }
+ if (bytes > 0) {
+ u32 rem = prandom_u32_state(state);
+ do {
+ *ptr++ = (u8) rem;
+ bytes--;
+ rem >>= BITS_PER_BYTE;
+ } while (bytes > 0);
}This conversion to put_unaligned() is not an equivalent depending upon the endianness of the cpu. And this means the random value gets distributed differently into full words than it will into trailing bytes. Let's just not mess around with this, ok? Thanks.