x448_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2011 Stanford University.
  4. // Copyright (c) 2014-2015 Cryptography Research, Inc.
  5. // Copyright (c) 2015 Yawning Angel.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. package x448
  25. import (
  26. "bytes"
  27. "crypto/rand"
  28. "encoding/hex"
  29. "testing"
  30. )
  31. // Cowardly refuse to run the full slow test vector case unless this is set
  32. // at compile time, because the timeout for the test harness needs to be
  33. // adjusted at runtime.
  34. var reallyRunSlowTest = false
  35. func TestX448(t *testing.T) {
  36. type KATVectors struct {
  37. scalar [x448Bytes]byte
  38. base [x448Bytes]byte
  39. answer [x448Bytes]byte
  40. }
  41. vectors := []KATVectors{
  42. {
  43. [x448Bytes]byte{
  44. 0x3d, 0x26, 0x2f, 0xdd, 0xf9, 0xec, 0x8e, 0x88,
  45. 0x49, 0x52, 0x66, 0xfe, 0xa1, 0x9a, 0x34, 0xd2,
  46. 0x88, 0x82, 0xac, 0xef, 0x04, 0x51, 0x04, 0xd0,
  47. 0xd1, 0xaa, 0xe1, 0x21, 0x70, 0x0a, 0x77, 0x9c,
  48. 0x98, 0x4c, 0x24, 0xf8, 0xcd, 0xd7, 0x8f, 0xbf,
  49. 0xf4, 0x49, 0x43, 0xeb, 0xa3, 0x68, 0xf5, 0x4b,
  50. 0x29, 0x25, 0x9a, 0x4f, 0x1c, 0x60, 0x0a, 0xd3,
  51. },
  52. [x448Bytes]byte{
  53. 0x06, 0xfc, 0xe6, 0x40, 0xfa, 0x34, 0x87, 0xbf,
  54. 0xda, 0x5f, 0x6c, 0xf2, 0xd5, 0x26, 0x3f, 0x8a,
  55. 0xad, 0x88, 0x33, 0x4c, 0xbd, 0x07, 0x43, 0x7f,
  56. 0x02, 0x0f, 0x08, 0xf9, 0x81, 0x4d, 0xc0, 0x31,
  57. 0xdd, 0xbd, 0xc3, 0x8c, 0x19, 0xc6, 0xda, 0x25,
  58. 0x83, 0xfa, 0x54, 0x29, 0xdb, 0x94, 0xad, 0xa1,
  59. 0x8a, 0xa7, 0xa7, 0xfb, 0x4e, 0xf8, 0xa0, 0x86,
  60. },
  61. [x448Bytes]byte{
  62. 0xce, 0x3e, 0x4f, 0xf9, 0x5a, 0x60, 0xdc, 0x66,
  63. 0x97, 0xda, 0x1d, 0xb1, 0xd8, 0x5e, 0x6a, 0xfb,
  64. 0xdf, 0x79, 0xb5, 0x0a, 0x24, 0x12, 0xd7, 0x54,
  65. 0x6d, 0x5f, 0x23, 0x9f, 0xe1, 0x4f, 0xba, 0xad,
  66. 0xeb, 0x44, 0x5f, 0xc6, 0x6a, 0x01, 0xb0, 0x77,
  67. 0x9d, 0x98, 0x22, 0x39, 0x61, 0x11, 0x1e, 0x21,
  68. 0x76, 0x62, 0x82, 0xf7, 0x3d, 0xd9, 0x6b, 0x6f,
  69. },
  70. },
  71. {
  72. [x448Bytes]byte{
  73. 0x20, 0x3d, 0x49, 0x44, 0x28, 0xb8, 0x39, 0x93,
  74. 0x52, 0x66, 0x5d, 0xdc, 0xa4, 0x2f, 0x9d, 0xe8,
  75. 0xfe, 0xf6, 0x00, 0x90, 0x8e, 0x0d, 0x46, 0x1c,
  76. 0xb0, 0x21, 0xf8, 0xc5, 0x38, 0x34, 0x5d, 0xd7,
  77. 0x7c, 0x3e, 0x48, 0x06, 0xe2, 0x5f, 0x46, 0xd3,
  78. 0x31, 0x5c, 0x44, 0xe0, 0xa5, 0xb4, 0x37, 0x12,
  79. 0x82, 0xdd, 0x2c, 0x8d, 0x5b, 0xe3, 0x09, 0x5f,
  80. },
  81. [x448Bytes]byte{
  82. 0x0f, 0xbc, 0xc2, 0xf9, 0x93, 0xcd, 0x56, 0xd3,
  83. 0x30, 0x5b, 0x0b, 0x7d, 0x9e, 0x55, 0xd4, 0xc1,
  84. 0xa8, 0xfb, 0x5d, 0xbb, 0x52, 0xf8, 0xe9, 0xa1,
  85. 0xe9, 0xb6, 0x20, 0x1b, 0x16, 0x5d, 0x01, 0x58,
  86. 0x94, 0xe5, 0x6c, 0x4d, 0x35, 0x70, 0xbe, 0xe5,
  87. 0x2f, 0xe2, 0x05, 0xe2, 0x8a, 0x78, 0xb9, 0x1c,
  88. 0xdf, 0xbd, 0xe7, 0x1c, 0xe8, 0xd1, 0x57, 0xdb,
  89. },
  90. [x448Bytes]byte{
  91. 0x88, 0x4a, 0x02, 0x57, 0x62, 0x39, 0xff, 0x7a,
  92. 0x2f, 0x2f, 0x63, 0xb2, 0xdb, 0x6a, 0x9f, 0xf3,
  93. 0x70, 0x47, 0xac, 0x13, 0x56, 0x8e, 0x1e, 0x30,
  94. 0xfe, 0x63, 0xc4, 0xa7, 0xad, 0x1b, 0x3e, 0xe3,
  95. 0xa5, 0x70, 0x0d, 0xf3, 0x43, 0x21, 0xd6, 0x20,
  96. 0x77, 0xe6, 0x36, 0x33, 0xc5, 0x75, 0xc1, 0xc9,
  97. 0x54, 0x51, 0x4e, 0x99, 0xda, 0x7c, 0x17, 0x9d,
  98. },
  99. },
  100. }
  101. var out [x448Bytes]byte
  102. for i, vec := range vectors {
  103. ret := ScalarMult(&out, &vec.scalar, &vec.base)
  104. if ret != 0 {
  105. t.Errorf("KAT[%d]: ScalarMultiply failed", i)
  106. }
  107. if !bytes.Equal(out[:], vec.answer[:]) {
  108. t.Errorf("KAT[%d]: Mismatch", i)
  109. }
  110. }
  111. }
  112. func TestX448IETFDraft(t *testing.T) {
  113. // Run the other test vectors from 5.2 of the IETF draft.
  114. // WARNING: The full version of the test will easily take longer than the
  115. // default 10 min test timeout, even on a moderately powerful box.
  116. //
  117. // Unless reallyRunSlowTest is set in the source code, it will cowardly
  118. // refuse to run the full 1 million iterations, and the `go test`
  119. // timeout will need to be increased (`go test -timeout 30m`).
  120. var k, u, out [x448Bytes] byte
  121. copy(k[:], basePoint[:])
  122. copy(u[:], basePoint[:])
  123. for i := 0; i < 1000000; i++ {
  124. ret := ScalarMult(&out, &k, &u)
  125. if ret != 0 {
  126. t.Fatalf("Iterated[%d]: ScalarMultiply failed", i)
  127. }
  128. switch (i+1) {
  129. case 1:
  130. known, _ := hex.DecodeString("3f482c8a9f19b01e6c46ee9711d9dc14fd4bf67af30765c2ae2b846a4d23a8cd0db897086239492caf350b51f833868b9bc2b3bca9cf4113")
  131. if !bytes.Equal(out[:], known) {
  132. t.Fatalf("Iterated[%d]: Mismatch", i)
  133. }
  134. case 1000:
  135. known, _ := hex.DecodeString("aa3b4749d55b9daf1e5b00288826c467274ce3ebbdd5c17b975e09d4af6c67cf10d087202db88286e2b79fceea3ec353ef54faa26e219f38")
  136. if !bytes.Equal(out[:], known) {
  137. t.Fatalf("Iterated[%d]: Mismatch", i)
  138. }
  139. if testing.Short() || !reallyRunSlowTest {
  140. t.Skipf("Short test requested, skipping remaining, was correct at 1k")
  141. }
  142. }
  143. copy(u[:], k[:])
  144. copy(k[:], out[:])
  145. }
  146. known, _ := hex.DecodeString("077f453681caca3693198420bbe515cae0002472519b3e67661a7e89cab94695c8f4bcd66e61b9b9c946da8d524de3d69bd9d9d66b997e37")
  147. if !bytes.Equal(k[:], known) {
  148. t.Fatal("Final value mismatch")
  149. }
  150. }
  151. func TestCurve448(t *testing.T) {
  152. alicePriv := [x448Bytes]byte{
  153. 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57,
  154. 0x75, 0xcf, 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4,
  155. 0xee, 0x9e, 0xe8, 0xba, 0xe8, 0xbc, 0x55, 0x65,
  156. 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9, 0xba, 0xf5,
  157. 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91,
  158. 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d,
  159. 0x9a, 0xc2, 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b,
  160. }
  161. alicePub := [x448Bytes]byte{
  162. 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6,
  163. 0x7d, 0x22, 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a,
  164. 0x27, 0x3b, 0xd2, 0xb8, 0x3d, 0xe0, 0x9c, 0x63,
  165. 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5, 0xd9, 0xbb,
  166. 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4,
  167. 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53,
  168. 0x17, 0x7f, 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0,
  169. }
  170. bobPriv := [x448Bytes]byte{
  171. 0x1c, 0x30, 0x6a, 0x7a, 0xc2, 0xa0, 0xe2, 0xe0,
  172. 0x99, 0x0b, 0x29, 0x44, 0x70, 0xcb, 0xa3, 0x39,
  173. 0xe6, 0x45, 0x37, 0x72, 0xb0, 0x75, 0x81, 0x1d,
  174. 0x8f, 0xad, 0x0d, 0x1d, 0x69, 0x27, 0xc1, 0x20,
  175. 0xbb, 0x5e, 0xe8, 0x97, 0x2b, 0x0d, 0x3e, 0x21,
  176. 0x37, 0x4c, 0x9c, 0x92, 0x1b, 0x09, 0xd1, 0xb0,
  177. 0x36, 0x6f, 0x10, 0xb6, 0x51, 0x73, 0x99, 0x2d,
  178. }
  179. bobPub := [x448Bytes]byte{
  180. 0x3e, 0xb7, 0xa8, 0x29, 0xb0, 0xcd, 0x20, 0xf5,
  181. 0xbc, 0xfc, 0x0b, 0x59, 0x9b, 0x6f, 0xec, 0xcf,
  182. 0x6d, 0xa4, 0x62, 0x71, 0x07, 0xbd, 0xb0, 0xd4,
  183. 0xf3, 0x45, 0xb4, 0x30, 0x27, 0xd8, 0xb9, 0x72,
  184. 0xfc, 0x3e, 0x34, 0xfb, 0x42, 0x32, 0xa1, 0x3c,
  185. 0xa7, 0x06, 0xdc, 0xb5, 0x7a, 0xec, 0x3d, 0xae,
  186. 0x07, 0xbd, 0xc1, 0xc6, 0x7b, 0xf3, 0x36, 0x09,
  187. }
  188. aliceBob := [x448Bytes]byte{
  189. 0x07, 0xff, 0xf4, 0x18, 0x1a, 0xc6, 0xcc, 0x95,
  190. 0xec, 0x1c, 0x16, 0xa9, 0x4a, 0x0f, 0x74, 0xd1,
  191. 0x2d, 0xa2, 0x32, 0xce, 0x40, 0xa7, 0x75, 0x52,
  192. 0x28, 0x1d, 0x28, 0x2b, 0xb6, 0x0c, 0x0b, 0x56,
  193. 0xfd, 0x24, 0x64, 0xc3, 0x35, 0x54, 0x39, 0x36,
  194. 0x52, 0x1c, 0x24, 0x40, 0x30, 0x85, 0xd5, 0x9a,
  195. 0x44, 0x9a, 0x50, 0x37, 0x51, 0x4a, 0x87, 0x9d,
  196. }
  197. var out [x448Bytes]byte
  198. ret := ScalarBaseMult(&out, &alicePriv)
  199. if ret != 0 {
  200. t.Error("Alice: ScalarBaseMult failed")
  201. }
  202. if !bytes.Equal(out[:], alicePub[:]) {
  203. t.Error("Alice: ScalarBaseMult Mismatch")
  204. }
  205. ret = ScalarBaseMult(&out, &bobPriv)
  206. if ret != 0 {
  207. t.Error("Bob: ScalarBaseMult failed")
  208. }
  209. if !bytes.Equal(out[:], bobPub[:]) {
  210. t.Error("Bob: ScalarBaseMult Mismatch")
  211. }
  212. ret = ScalarMult(&out, &bobPriv, &alicePub)
  213. if ret != 0 {
  214. t.Error("Bob: ScalarMult failed")
  215. }
  216. if !bytes.Equal(out[:], aliceBob[:]) {
  217. t.Error("Bob: ScalarMult Mismatch")
  218. }
  219. ret = ScalarMult(&out, &alicePriv, &bobPub)
  220. if ret != 0 {
  221. t.Error("Alice: ScalarMult failed")
  222. }
  223. if !bytes.Equal(out[:], aliceBob[:]) {
  224. t.Error("Alice: ScalarMult Mismatch")
  225. }
  226. }
  227. func BenchmarkECDH(b *testing.B) {
  228. var sa, sb, pa, pb, ab, ba [x448Bytes]byte
  229. ret := 0
  230. rand.Read(sa[:])
  231. rand.Read(sb[:])
  232. b.ResetTimer()
  233. b.StopTimer()
  234. for i := 0; i < b.N; i++ {
  235. ret |= ScalarBaseMult(&pa, &sa)
  236. ret |= ScalarBaseMult(&pb, &sb)
  237. b.StartTimer()
  238. ret |= ScalarMult(&ab, &sa, &pb)
  239. b.StopTimer()
  240. ret |= ScalarMult(&ba, &sb, &pa)
  241. if !bytes.Equal(ab[:], ba[:]) {
  242. b.Fatal("Alice/Bob: Mismatch")
  243. }
  244. copy(sa[:], pa[:])
  245. copy(sb[:], pb[:])
  246. }
  247. }