Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

103 righe
3.1 KiB

  1. package drbg
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. )
  7. func S2H(s string) []byte {
  8. hex, e := hex.DecodeString(s)
  9. if e != nil {
  10. panic("Can't import private key")
  11. }
  12. return hex
  13. }
  14. func TestNominal(t *testing.T) {
  15. var entropy [16]byte
  16. var data [48]byte
  17. c := NewCtrDrbg()
  18. if !c.Init(entropy[:], nil) {
  19. t.FailNow()
  20. }
  21. c.ReadWithAdditionalData(entropy[0:16], data[:])
  22. exp := S2H("16BA361FA14563FB1E8BCF88932F9FA7")
  23. if !bytes.Equal(exp, entropy[:]) {
  24. t.FailNow()
  25. }
  26. }
  27. // TODO: should parse *.req file from here: https://raw.githubusercontent.com/coruus/nist-testvectors/master/csrc.nist.gov/groups/STM/cavp/documents/drbg/drbgtestvectors/drbgvectors_pr_false/CTR_DRBG.rsp
  28. var vectors = []struct {
  29. EntropyInput []byte
  30. PersonalizationString []byte
  31. EntropyInputReseed []byte
  32. AdditionalInputReseed []byte
  33. AdditionalInput1 []byte
  34. AdditionalInput2 []byte
  35. ReturnedBits []byte
  36. }{
  37. // With Reseeding
  38. {
  39. S2H("99903165903fea49c2db26ed675e44cc14cb2c1f28b836b203240b02771e831146ffc4335373bb344688c5c950670291"),
  40. []byte{},
  41. S2H("b4ee99fa9e0eddaf4a3612013cd636c4af69177b43eebb3c58a305b9979b68b5cc820504f6c029aad78a5d29c66e84a0"),
  42. S2H("2d8c5c28b05696e74774eb69a10f01c5fabc62691ddf7848a8004bb5eeb4d2c5febe1aa01f4d557b23d7e9a0e4e90655"),
  43. S2H("0dc9cde42ac6e856f01a55f219c614de90c659260948db5053d414bab0ec2e13e995120c3eb5aafc25dc4bdcef8ace24"),
  44. S2H("711be6c035013189f362211889248ca8a3268e63a7eb26836d915810a680ac4a33cd1180811a31a0f44f08db3dd64f91"),
  45. S2H("11c7a0326ea737baa7a993d510fafee5374e7bbe17ef0e3e29f50fa68aac2124b017d449768491cac06d136d691a4e80785739f9aaedf311bba752a3268cc531"),
  46. },
  47. {
  48. S2H("ffad10100025a879672ff50374b286712f457dd01441d76ac1a1cd15c7390dd93179a2f5920d198bf34a1b76fbc21289"),
  49. S2H("1d2be6f25e88fa30c4ef42e4d54efd957dec231fa00143ca47580be666a8c143a916c90b3819a0a7ea914e3c9a2e7a3f"),
  50. S2H("6c1a089cae313363bc76a780139eb4f2f2048b1f6b07896c5c412bff0385440fc43b73facbb79e3a252fa01fe17ab391"),
  51. []byte{},
  52. []byte{},
  53. []byte{},
  54. S2H("e053c7d4bd9099ef6a99f190a5fd80219437d642006672338da6e0fe73ca4d24ffa51151bfbdac78d8a2f6255046edf57a04626e9977139c6933274299f3bdff"),
  55. },
  56. }
  57. func TestVector(t *testing.T) {
  58. for i := range vectors {
  59. result := make([]byte, len(vectors[i].ReturnedBits))
  60. c := NewCtrDrbg()
  61. if !c.Init(vectors[i].EntropyInput[:], vectors[i].PersonalizationString) {
  62. t.Error("Init failed")
  63. }
  64. if len(vectors[i].EntropyInputReseed) > 0 {
  65. c.Reseed(vectors[i].EntropyInputReseed[:], vectors[i].AdditionalInputReseed[:])
  66. }
  67. c.ReadWithAdditionalData(result[:], vectors[i].AdditionalInput1)
  68. c.ReadWithAdditionalData(result[:], vectors[i].AdditionalInput2)
  69. if !bytes.Equal(vectors[i].ReturnedBits[:], result[:]) {
  70. t.Errorf("KAT failed \nexp: %X\ngot: %X\n", vectors[i].ReturnedBits, result[:])
  71. }
  72. }
  73. }
  74. func BenchmarkInit(b *testing.B) {
  75. c := NewCtrDrbg()
  76. for i := 0; i < b.N; i++ {
  77. c.Init(vectors[0].EntropyInput[:], vectors[0].PersonalizationString)
  78. }
  79. }
  80. func BenchmarkRead(b *testing.B) {
  81. var result [16 * 10]byte
  82. c := NewCtrDrbg()
  83. c.Init(vectors[0].EntropyInput[:], vectors[0].PersonalizationString)
  84. for i := 0; i < b.N; i++ {
  85. c.ReadWithAdditionalData(result[:], vectors[0].AdditionalInput1)
  86. }
  87. }