hash_drbg.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2014, Yawning Angel <yawning at schwanenlied dot me>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Package drbg implements a minimalistic DRBG based off SipHash-2-4 in OFB
  28. // mode.
  29. package drbg // import "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/common/drbg"
  30. import (
  31. "encoding/binary"
  32. "encoding/hex"
  33. "fmt"
  34. "hash"
  35. "github.com/dchest/siphash"
  36. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/common/csrand"
  37. )
  38. // Size is the length of the HashDrbg output.
  39. const Size = siphash.Size
  40. // SeedLength is the length of the HashDrbg seed.
  41. const SeedLength = 16 + Size
  42. // Seed is the initial state for a HashDrbg. It consists of a SipHash-2-4
  43. // key, and 8 bytes of initial data.
  44. type Seed [SeedLength]byte
  45. // Bytes returns a pointer to the raw HashDrbg seed.
  46. func (seed *Seed) Bytes() *[SeedLength]byte {
  47. return (*[SeedLength]byte)(seed)
  48. }
  49. // Hex returns the hexdecimal representation of the seed.
  50. func (seed *Seed) Hex() string {
  51. return hex.EncodeToString(seed.Bytes()[:])
  52. }
  53. // NewSeed returns a Seed initialized with the runtime CSPRNG.
  54. func NewSeed() (seed *Seed, err error) {
  55. seed = new(Seed)
  56. if err = csrand.Bytes(seed.Bytes()[:]); err != nil {
  57. return nil, err
  58. }
  59. return
  60. }
  61. // SeedFromBytes creates a Seed from the raw bytes, truncating to SeedLength as
  62. // appropriate.
  63. func SeedFromBytes(src []byte) (seed *Seed, err error) {
  64. if len(src) < SeedLength {
  65. return nil, InvalidSeedLengthError(len(src))
  66. }
  67. seed = new(Seed)
  68. copy(seed.Bytes()[:], src)
  69. return
  70. }
  71. // SeedFromHex creates a Seed from the hexdecimal representation, truncating to
  72. // SeedLength as appropriate.
  73. func SeedFromHex(encoded string) (seed *Seed, err error) {
  74. var raw []byte
  75. if raw, err = hex.DecodeString(encoded); err != nil {
  76. return nil, err
  77. }
  78. return SeedFromBytes(raw)
  79. }
  80. // InvalidSeedLengthError is the error returned when the seed provided to the
  81. // DRBG is an invalid length.
  82. type InvalidSeedLengthError int
  83. func (e InvalidSeedLengthError) Error() string {
  84. return fmt.Sprintf("invalid seed length: %d", int(e))
  85. }
  86. // HashDrbg is a CSDRBG based off of SipHash-2-4 in OFB mode.
  87. type HashDrbg struct {
  88. sip hash.Hash64
  89. ofb [Size]byte
  90. }
  91. // NewHashDrbg makes a HashDrbg instance based off an optional seed. The seed
  92. // is truncated to SeedLength.
  93. func NewHashDrbg(seed *Seed) (*HashDrbg, error) {
  94. drbg := new(HashDrbg)
  95. if seed == nil {
  96. var err error
  97. if seed, err = NewSeed(); err != nil {
  98. return nil, err
  99. }
  100. }
  101. drbg.sip = siphash.New(seed.Bytes()[:16])
  102. copy(drbg.ofb[:], seed.Bytes()[16:])
  103. return drbg, nil
  104. }
  105. // Int63 returns a uniformly distributed random integer [0, 1 << 63).
  106. func (drbg *HashDrbg) Int63() int64 {
  107. block := drbg.NextBlock()
  108. ret := binary.BigEndian.Uint64(block)
  109. ret &= (1<<63 - 1)
  110. return int64(ret)
  111. }
  112. // Seed does nothing, call NewHashDrbg if you want to reseed.
  113. func (drbg *HashDrbg) Seed(seed int64) {
  114. // No-op.
  115. }
  116. // NextBlock returns the next 8 byte DRBG block.
  117. func (drbg *HashDrbg) NextBlock() []byte {
  118. _, _ = drbg.sip.Write(drbg.ofb[:])
  119. copy(drbg.ofb[:], drbg.sip.Sum(nil))
  120. ret := make([]byte, Size)
  121. copy(ret, drbg.ofb[:])
  122. return ret
  123. }