<i id='g7XNq'><tr id='g7XNq'><dt id='g7XNq'><q id='g7XNq'><span id='g7XNq'><b id='g7XNq'><form id='g7XNq'><ins id='g7XNq'></ins><ul id='g7XNq'></ul><sub id='g7XNq'></sub></form><legend id='g7XNq'></legend><bdo id='g7XNq'><pre id='g7XNq'><center id='g7XNq'></center></pre></bdo></b><th id='g7XNq'></th></span></q></dt></tr></i><div id='g7XNq'><tfoot id='g7XNq'></tfoot><dl id='g7XNq'><fieldset id='g7XNq'></fieldset></dl></div>

  • <tfoot id='g7XNq'></tfoot>
    1. <legend id='g7XNq'><style id='g7XNq'><dir id='g7XNq'><q id='g7XNq'></q></dir></style></legend>

      <small id='g7XNq'></small><noframes id='g7XNq'>

          <bdo id='g7XNq'></bdo><ul id='g7XNq'></ul>

        随机睡眠可以防止定时攻击吗?

        Could a random sleep prevent timing attacks?(随机睡眠可以防止定时攻击吗?)
        • <small id='7sPYz'></small><noframes id='7sPYz'>

            <tfoot id='7sPYz'></tfoot>

                <i id='7sPYz'><tr id='7sPYz'><dt id='7sPYz'><q id='7sPYz'><span id='7sPYz'><b id='7sPYz'><form id='7sPYz'><ins id='7sPYz'></ins><ul id='7sPYz'></ul><sub id='7sPYz'></sub></form><legend id='7sPYz'></legend><bdo id='7sPYz'><pre id='7sPYz'><center id='7sPYz'></center></pre></bdo></b><th id='7sPYz'></th></span></q></dt></tr></i><div id='7sPYz'><tfoot id='7sPYz'></tfoot><dl id='7sPYz'><fieldset id='7sPYz'></fieldset></dl></div>

                <legend id='7sPYz'><style id='7sPYz'><dir id='7sPYz'><q id='7sPYz'></q></dir></style></legend>
                  <tbody id='7sPYz'></tbody>
                  <bdo id='7sPYz'></bdo><ul id='7sPYz'></ul>
                  本文介绍了随机睡眠可以防止定时攻击吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  来自 维基百科

                  在密码学中,定时攻击是一种边信道攻击,其中攻击者试图通过分析时间来破坏密码系统用于执行加密算法.

                  In cryptography, a timing attack is a side channel attack in which the attacker attempts to compromise a cryptosystem by analyzing the time taken to execute cryptographic algorithms.

                  实际上,为了防止定时攻击,我使用了以下函数,取自 这个答案:

                  Actually, to prevent timing attacks, I'm using the following function taken from this answer:

                  function timingSafeCompare($safe, $user) {
                      // Prevent issues if string length is 0
                      $safe .= chr(0);
                      $user .= chr(0);
                  
                      $safeLen = strlen($safe);
                      $userLen = strlen($user);
                  
                      // Set the result to the difference between the lengths
                      $result = $safeLen - $userLen;
                  
                      // Note that we ALWAYS iterate over the user-supplied length
                      // This is to prevent leaking length information
                      for ($i = 0; $i < $userLen; $i++) {
                          // Using % here is a trick to prevent notices
                          // It's safe, since if the lengths are different
                          // $result is already non-0
                          $result |= (ord($safe[$i % $safeLen]) ^ ord($user[$i]));
                      }
                  
                      // They are only identical strings if $result is exactly 0...
                      return $result === 0;
                  }
                  

                  但我在想是否可以使用随机睡眠来防止这种攻击

                  But I was thinking if is possible prevent this kind of attack using a random sleep like

                  function timingSafeCompare($a,$b) {
                      sleep(rand(0,100));
                      if ($a === $b) {
                          return true;
                      } else {
                          return false;
                      }
                  }
                  

                  或者也许增加睡眠的随机性

                  Or maybe augmenting the randomness of sleep

                  sleep(rand(1,10)+rand(1,10)+rand(1,10)+rand(1,10));
                  

                  这种方法可以完全防止定时攻击吗?还是只是让工作更加努力?

                  This kind of approach can totally prevent timing attacks? Or just make the work harder?

                  推荐答案

                  这种方法可以完全防止定时攻击吗?还是只是让工作更加努力?

                  This kind of approach can totally prevent timing attacks? Or just make the work harder?

                  两者都没有.它不会阻止定时攻击,也不会让它们变得更加困难.

                  Neither. It doesn't prevent timing attacks, nor does it make them any more difficult at all.

                  要了解原因,请查看睡眠文档.具体来说,第一个参数的含义:

                  To understand why, look at the docs for sleep. Specifically, the meaning of the first parameter:

                  暂停时间以秒为单位.

                  因此,您的应用需要 0.3 秒才能在不休眠的情况下做出响应.睡眠需要 0.3、1.3、2.3 等...

                  So your app takes 0.3 seconds to respond without sleep. With sleep it takes either 0.3, 1.3, 2.3, etc...

                  所以说真的,要得到我们关心的部分(时间差),我们只需要去掉整数部分:

                  So really, to get the part we care about (the timing difference), we just need to chop off the integer part:

                  $real_time = $time - floor($time);
                  

                  但让我们更进一步.假设您使用 usleep 随机睡眠.这要细得多.那是在微秒内休眠.

                  But let's go a step further. Let's say that you randomly sleep using usleep. That's a lot more granular. That's sleeping in microseconds.

                  嗯,测量是在 15-50 纳米秒的范围内进行的.因此,睡眠的粒度仍然比正在进行的测量大约 100 倍.所以我们可以平均到一微秒:

                  Well, the measurements are being made in the 15-50 nanosecond scale. So that sleep is still about 100 times less granular than the measurements being made. So we can average off to the single microsecond:

                  $microseconds = $time * 1000000;
                  $real_microseconds = $microseconds - floor($microseconds);
                  

                  并且仍然有有意义的数据.

                  And still have meaningful data.

                  您可以更进一步并使用 time_nanosleep,它可以睡眠到纳秒级精度.

                  You could go further and use time_nanosleep which can sleep to nanosecond scale precision.

                  然后你就可以开始玩弄数字了.

                  Then you could start fuddling with the numbers.

                  但数据仍然存在.随机性的美妙之处在于您可以将其平均化:

                  But the data is still there. The beauty of randomness is that you can just average it out:

                  $x = 15 + rand(1, 10000);
                  

                  运行足够多的时间,你会得到一个漂亮的图表.您会知道大约有 10000 个不同的数字,因此您可以平均掉随机性并推断出私人"15.

                  Run that enough times and you'll get a nice pretty graph. You'll tell that there are about 10000 different numbers, so you can then average away the randomness and deduce the "private" 15.

                  由于表现良好的随机性是无偏的,因此在足够大的样本上进行统计检测非常容易.

                  Because well-behaved randomness is unbiased, it's pretty easy to detect statistically over a large enough sample.

                  所以我要问的问题是:

                  这篇关于随机睡眠可以防止定时攻击吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tfoot id='trPpR'></tfoot>
                    1. <small id='trPpR'></small><noframes id='trPpR'>

                        <tbody id='trPpR'></tbody>

                        <bdo id='trPpR'></bdo><ul id='trPpR'></ul>

                        1. <i id='trPpR'><tr id='trPpR'><dt id='trPpR'><q id='trPpR'><span id='trPpR'><b id='trPpR'><form id='trPpR'><ins id='trPpR'></ins><ul id='trPpR'></ul><sub id='trPpR'></sub></form><legend id='trPpR'></legend><bdo id='trPpR'><pre id='trPpR'><center id='trPpR'></center></pre></bdo></b><th id='trPpR'></th></span></q></dt></tr></i><div id='trPpR'><tfoot id='trPpR'></tfoot><dl id='trPpR'><fieldset id='trPpR'></fieldset></dl></div>

                            <legend id='trPpR'><style id='trPpR'><dir id='trPpR'><q id='trPpR'></q></dir></style></legend>