uniformdh_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 uniformdh
  28. import (
  29. "bytes"
  30. "crypto/rand"
  31. "encoding/hex"
  32. "testing"
  33. )
  34. const (
  35. xPrivStr = "6f592d676f536874746f20686e6b776f" +
  36. "20736874206561676574202e6f592d67" +
  37. "6f536874746f20687369742065686720" +
  38. "74612e655920676f532d746f6f686874" +
  39. "6920207368742065656b20796e612064" +
  40. "7567726169646e616f20206668742065" +
  41. "61676574202e61507473202c72707365" +
  42. "6e652c746620747572752c6561206c6c" +
  43. "612065726f20656e6920206e6f592d67" +
  44. "6f536874746f2e68482020656e6b776f" +
  45. "2073687772652065687420656c4f2064" +
  46. "6e4f736562206f72656b74207268756f"
  47. xPubStr = "76a3d17d5c55b03e865fa3e8267990a7" +
  48. "24baa24b0bdd0cc4af93be8de30be120" +
  49. "d5533c91bf63ef923b02edcb84b74438" +
  50. "3f7de232cca6eb46d07cad83dcaa317f" +
  51. "becbc68ca13e2c4019e6a36531067450" +
  52. "04aecc0be1dff0a78733fb0e7d5cb7c4" +
  53. "97cab77b1331bf347e5f3a7847aa0bc0" +
  54. "f4bc64146b48407fed7b931d16972d25" +
  55. "fb4da5e6dc074ce2a58daa8de7624247" +
  56. "cdf2ebe4e4dfec6d5989aac778c87559" +
  57. "d3213d6040d4111ce3a2acae19f9ee15" +
  58. "32509e037f69b252fdc30243cbbce9d0"
  59. yPrivStr = "736562206f72656b74207268756f6867" +
  60. "6f2020666c6f2c646120646e77206568" +
  61. "657254206568207968736c61206c7262" +
  62. "6165206b68746f726775206867616961" +
  63. "2e6e482020656e6b776f207368777265" +
  64. "2065685479656820766120657274646f" +
  65. "652072616874732766206569646c2c73" +
  66. "6120646e772065686572542065682079" +
  67. "74736c69206c72746165206468746d65" +
  68. "202c6e612064687720796f6e6f20656e" +
  69. "63206e61622068656c6f206468546d65" +
  70. "61202073685479657420657264610a2e"
  71. yPubStr = "d04e156e554c37ffd7aba749df662350" +
  72. "1e4ff4466cb12be055617c1a36872237" +
  73. "36d2c3fdce9ee0f9b27774350849112a" +
  74. "a5aeb1f126811c9c2f3a9cb13d2f0c3a" +
  75. "7e6fa2d3bf71baf50d839171534f227e" +
  76. "fbb2ce4227a38c25abdc5ba7fc430111" +
  77. "3a2cb2069c9b305faac4b72bf21fec71" +
  78. "578a9c369bcac84e1a7dcf0754e342f5" +
  79. "bc8fe4917441b88254435e2abaf297e9" +
  80. "3e1e57968672d45bd7d4c8ba1bc3d314" +
  81. "889b5bc3d3e4ea33d4f2dfdd34e5e5a7" +
  82. "2ff24ee46316d4757dad09366a0b66b3"
  83. ssStr = "78afaf5f457f1fdb832bebc397644a33" +
  84. "038be9dba10ca2ce4a076f327f3a0ce3" +
  85. "151d477b869ee7ac467755292ad8a77d" +
  86. "b9bd87ffbbc39955bcfb03b1583888c8" +
  87. "fd037834ff3f401d463c10f899aa6378" +
  88. "445140b7f8386a7d509e7b9db19b677f" +
  89. "062a7a1a4e1509604d7a0839ccd5da61" +
  90. "73e10afd9eab6dda74539d60493ca37f" +
  91. "a5c98cd9640b409cd8bb3be2bc5136fd" +
  92. "42e764fc3f3c0ddb8db3d87abcf2e659" +
  93. "8d2b101bef7a56f50ebc658f9df1287d" +
  94. "a81359543e77e4a4cfa7598a4152e4c0"
  95. )
  96. var xPriv, xPub, yPriv, yPub, ss []byte
  97. // TestGenerateKeyOdd tests creating a UniformDH keypair with a odd private
  98. // key.
  99. func TestGenerateKeyOdd(t *testing.T) {
  100. xX, err := generateKey(xPriv)
  101. if err != nil {
  102. t.Fatal("generateKey(xPriv) failed:", err)
  103. }
  104. xPubGen, err := xX.PublicKey.Bytes()
  105. if err != nil {
  106. t.Fatal("xX.PublicKey.Bytes() failed:", err)
  107. }
  108. if 0 != bytes.Compare(xPubGen, xPub) {
  109. t.Fatal("Generated public key does not match known answer")
  110. }
  111. }
  112. // TestGenerateKeyEven tests creating a UniformDH keypair with a even private
  113. // key.
  114. func TestGenerateKeyEven(t *testing.T) {
  115. yY, err := generateKey(yPriv)
  116. if err != nil {
  117. t.Fatal("generateKey(yPriv) failed:", err)
  118. }
  119. yPubGen, err := yY.PublicKey.Bytes()
  120. if err != nil {
  121. t.Fatal("yY.PublicKey.Bytes() failed:", err)
  122. }
  123. if 0 != bytes.Compare(yPubGen, yPub) {
  124. t.Fatal("Generated public key does not match known answer")
  125. }
  126. }
  127. // TestHandshake tests conductiong a UniformDH handshake with know values.
  128. func TestHandshake(t *testing.T) {
  129. xX, err := generateKey(xPriv)
  130. if err != nil {
  131. t.Fatal("generateKey(xPriv) failed:", err)
  132. }
  133. yY, err := generateKey(yPriv)
  134. if err != nil {
  135. t.Fatal("generateKey(yPriv) failed:", err)
  136. }
  137. xY, err := Handshake(xX, &yY.PublicKey)
  138. if err != nil {
  139. t.Fatal("Handshake(xX, yY.PublicKey) failed:", err)
  140. }
  141. yX, err := Handshake(yY, &xX.PublicKey)
  142. if err != nil {
  143. t.Fatal("Handshake(yY, xX.PublicKey) failed:", err)
  144. }
  145. if 0 != bytes.Compare(xY, yX) {
  146. t.Fatal("Generated shared secrets do not match between peers")
  147. }
  148. if 0 != bytes.Compare(xY, ss) {
  149. t.Fatal("Generated shared secret does not match known value")
  150. }
  151. }
  152. // Benchmark UniformDH key generation + exchange. THe actual time taken per
  153. // peer is half of the reported time as this does 2 key generation an
  154. // handshake operations.
  155. func BenchmarkHandshake(b *testing.B) {
  156. for i := 0; i < b.N; i++ {
  157. xX, err := GenerateKey(rand.Reader)
  158. if err != nil {
  159. b.Fatal("Failed to generate xX keypair", err)
  160. }
  161. yY, err := GenerateKey(rand.Reader)
  162. if err != nil {
  163. b.Fatal("Failed to generate yY keypair", err)
  164. }
  165. xY, err := Handshake(xX, &yY.PublicKey)
  166. if err != nil {
  167. b.Fatal("Handshake(xX, yY.PublicKey) failed:", err)
  168. }
  169. yX, err := Handshake(yY, &xX.PublicKey)
  170. if err != nil {
  171. b.Fatal("Handshake(yY, xX.PublicKey) failed:", err)
  172. }
  173. _ = xY
  174. _ = yX
  175. }
  176. }
  177. func init() {
  178. // Load the test vectors into byte slices.
  179. var err error
  180. xPriv, err = hex.DecodeString(xPrivStr)
  181. if err != nil {
  182. panic("hex.DecodeString(xPrivStr) failed")
  183. }
  184. xPub, err = hex.DecodeString(xPubStr)
  185. if err != nil {
  186. panic("hex.DecodeString(xPubStr) failed")
  187. }
  188. yPriv, err = hex.DecodeString(yPrivStr)
  189. if err != nil {
  190. panic("hex.DecodeString(yPrivStr) failed")
  191. }
  192. yPub, err = hex.DecodeString(yPubStr)
  193. if err != nil {
  194. panic("hex.DecodeString(yPubStr) failed")
  195. }
  196. ss, err = hex.DecodeString(ssStr)
  197. if err != nil {
  198. panic("hex.DecodeString(ssStr) failed")
  199. }
  200. }