HmacDRBGTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. require_once __DIR__ . "/../vendor/autoload.php";
  3. class HmacDRBGTest extends \PHPUnit\Framework\TestCase {
  4. public function test_should_support_hmac_drbg_sha256() {
  5. function doDrbg($opt) {
  6. $drbg = new \Elliptic\HmacDRBG([
  7. "hash" => ["algo" => 'sha256', 'outSize' => 256, 'hmacStrength' => 192],//hash.sha256,
  8. "entropy" => $opt["entropy"],
  9. "nonce" => $opt["nonce"],
  10. "pers" => $opt["pers"]
  11. ]);
  12. return $drbg->generate($opt["size"], 'hex');
  13. }
  14. $test = [
  15. [
  16. "entropy" => 'totally random0123456789',
  17. "nonce" => 'secret nonce',
  18. "pers" => 'my drbg',
  19. "size" => 32,
  20. "res" => '018ec5f8e08c41e5ac974eb129ac297c5388ee1864324fa13d9b15cf98d9a157'
  21. ],
  22. [
  23. "entropy" => 'totally random0123456789',
  24. "nonce" => 'secret nonce',
  25. "pers" => null,
  26. "size" => 32,
  27. "res" => 'ed5d61ecf0ef38258e62f03bbb49f19f2cd07ba5145a840d83b134d5963b3633'
  28. ]
  29. ];
  30. for ($i = 0; $i < count($test); $i++)
  31. $this->assertEquals(doDrbg($test[$i]), $test[$i]["res"]);
  32. }
  33. /**
  34. * @dataProvider NISTVector
  35. */
  36. public function test_should_not_fail_at_NIST_vector($opt) {
  37. $drbg = new \Elliptic\HmacDRBG([
  38. "hash" => ["algo" => 'sha256', 'outSize' => 256, 'hmacStrength' => 192],//hash.sha256,
  39. "entropy" => $opt["entropy"],
  40. "entropyEnc" => 'hex',
  41. "nonce" => $opt["nonce"],
  42. "nonceEnc" => 'hex',
  43. "pers" => $opt["pers"],
  44. "persEnc" => 'hex'
  45. ]);
  46. for ($i = 0; $i < count($opt["add"]); $i++) {
  47. $last = $drbg->generate(strlen($opt["expected"]) / 2,
  48. 'hex',
  49. $opt["add"][$i],
  50. 'hex');
  51. }
  52. $this->assertEquals($last, $opt["expected"]);
  53. }
  54. function NISTVector() {
  55. $data = json_decode(file_get_contents(__DIR__."/fixtures/hmac-drbg-nist.json"), true);
  56. $cases = array();
  57. for($i = 0; $i < count($data); ++$i) {
  58. $cases[ $data[$i]["name"] ] = [ $data[$i] ];
  59. }
  60. return $cases;
  61. }
  62. }