我需要一些弱熵。我知道我可以使用统一的缓冲区,但如果我只需要一个整数值,它的地址就足够了吗?
我使用的是 Linux,它具有地址空间布局随机化。是否有必要在堆栈上声明一个缓冲区,使其未初始化,并将其用于熵,或者我可以只获取堆栈上已有内容的地址,将其转换为整数并且(知道它有点随机)解决空间布局随机化)使用该整数作为熵?
指针方法的一个优点是,当您尝试操作它时,它不会像未初始化缓冲区那样生成编译器警告,但在我的测试中,似乎只有地址的低位部分(可能是最后一个或两个字节)会改变从调用到调用。熵缓冲区似乎表现更差,通常根本不包含任何内容。
I'm on Linux, which has address space layout randomization. Is it necessary to to declare a buffer on the stack, leave it uninitialized, and use it for entropy, or can I just take the address of something already on the stack, cast it to an integer and (knowing that it is somewhat random due to address space layout randomization) use that integer for entropy instead?
The pointer approach has an advantage in that it generates no compiler warnings as the unitialized buffer does when you attempt to manipulate it, but in my tests it seemed only the lower-order part of the address (maybe the last byte or two) would change from invocation to invocation. The entropy buffer seemed to perform even worse, often containing nothing at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您在 Linux 上需要弱熵,为什么不阅读
/dev/urandom
?它是/dev/random
的非阻塞变体,随机性较低(但同样是非阻塞)。If you need weak entropy on Linux, why not read
/dev/urandom
? It's a non-blocking variant of/dev/random
that's less...random (but, again, non-blocking).从根本上讲,如果您需要任何东西的熵,则需要从某些外部源获取它,而不是编译器的某些怪癖或内存分配布局的期望。不能保证编译器会给你不同的指针。您可能编写在一个系统上运行正常的代码,但在另一个系统上完全无法给出熵。
正如其他人所建议的,使用 /dev/random 是一个好主意。如果这不可用,如果您只需要一点熵,您可能可以调用 time() 函数(time.h)。
然而,当人们只要求一点熵时,我会非常担心,因为这表明对随机值的某种依赖。只有一点熵意味着它将频繁返回相同的值,可能导致系统以意想不到的方式失败。我强烈的建议是始终从 /dev/random 等来源获得良好的熵。
Fundamentally, if you need entropy for anything, you need to take it from some external source, not some quirk of the compiler, or expectations of memory allocation layout. There's no guarantee that the compiler will give you different pointers. You might write code that works okay on one system, but completely fails to give entropy on another.
As others have suggested, using /dev/random is a good idea. If that's not available, you might be able to get away with calling the time() function (time.h) if you just need a little entropy.
However, I get very worried when people ask for just a little entropy because that suggests some kind of reliance on a random value. Having only a little entropy means that it will frequently return the same value, possibly causing the system to fail in unexpected ways. My strong recommendation is to always get good entropy from sources like /dev/random.
/dev/random 有什么问题吗?
不要将未初始化的内存用于熵。
特别是堆栈。在连续运行中它往往看起来非常相似。
而且它是相当可预测的,而不是非常随机的。
What's wrong with /dev/random?
Do not use uninitialized memory for entropy.
Especially stack. It has tendency to look very similarly in consecutive runs.
And it's quite predictable and not very random.
你所说的弱到底是什么意思? C 中熵的规范来源(用于非加密目的)是来自
的time
。访问未初始化的变量是未定义的行为,在某些平台上可能会产生不可预测的后果。不要这样做。
What exactly do you mean by weak? The canonical source of entropy in C (for non-cryptographic purposes) is
time
from<time.h>
.Accessing an uninitialized variable is undefined behavior and may have unpredictable consequences on some platforms. Don't do it.
为什么不从
/dev/random
或/dev/urandom
读取一些字节?Why don't you read a few bytes from
/dev/random
or/dev/urandom
?