You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert_wycheproof.go 7.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright (c) 2018, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. // convert_wycheproof.go converts Wycheproof test vectors into a format more
  15. // easily consumed by BoringSSL.
  16. package main
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "os"
  23. "sort"
  24. "strings"
  25. )
  26. type wycheproofTest struct {
  27. Algorithm string `json:"algorithm"`
  28. GeneratorVersion string `json:"generatorVersion"`
  29. NumberOfTests int `json:"numberOfTests"`
  30. Notes map[string]string `json:"notes"`
  31. Header []string `json:"header"`
  32. // encoding/json does not support collecting unused keys, so we leave
  33. // everything past this point as generic.
  34. TestGroups []map[string]interface{} `json:"testGroups"`
  35. }
  36. func sortedKeys(m map[string]interface{}) []string {
  37. keys := make([]string, 0, len(m))
  38. for k, _ := range m {
  39. keys = append(keys, k)
  40. }
  41. sort.Strings(keys)
  42. return keys
  43. }
  44. func printAttribute(w io.Writer, key string, valueI interface{}, isInstruction bool) error {
  45. switch value := valueI.(type) {
  46. case float64:
  47. if float64(int(value)) != value {
  48. panic(key + "was not an integer.")
  49. }
  50. if isInstruction {
  51. if _, err := fmt.Fprintf(w, "[%s = %d]\n", key, int(value)); err != nil {
  52. return err
  53. }
  54. } else {
  55. if _, err := fmt.Fprintf(w, "%s = %d\n", key, int(value)); err != nil {
  56. return err
  57. }
  58. }
  59. case string:
  60. if strings.Contains(value, "\n") {
  61. panic(key + " contained a newline.")
  62. }
  63. if isInstruction {
  64. if _, err := fmt.Fprintf(w, "[%s = %s]\n", key, value); err != nil {
  65. return err
  66. }
  67. } else {
  68. if _, err := fmt.Fprintf(w, "%s = %s\n", key, value); err != nil {
  69. return err
  70. }
  71. }
  72. case map[string]interface{}:
  73. for _, k := range sortedKeys(value) {
  74. if err := printAttribute(w, key+"."+k, value[k], isInstruction); err != nil {
  75. return err
  76. }
  77. }
  78. default:
  79. panic(fmt.Sprintf("Unknown type for %q: %T", key, valueI))
  80. }
  81. return nil
  82. }
  83. func printComment(w io.Writer, in string) error {
  84. const width = 80 - 2
  85. lines := strings.Split(in, "\n")
  86. for _, line := range lines {
  87. for {
  88. if len(line) <= width {
  89. if _, err := fmt.Fprintf(w, "# %s\n", line); err != nil {
  90. return err
  91. }
  92. break
  93. }
  94. // Find the last space we can break at.
  95. n := strings.LastIndexByte(line[:width+1], ' ')
  96. if n < 0 {
  97. // The next word is too long. Wrap as soon as that word ends.
  98. n = strings.IndexByte(line[width+1:], ' ')
  99. if n < 0 {
  100. // This was the last word.
  101. if _, err := fmt.Fprintf(w, "# %s\n", line); err != nil {
  102. return nil
  103. }
  104. break
  105. }
  106. n += width + 1
  107. }
  108. if _, err := fmt.Fprintf(w, "# %s\n", line[:n]); err != nil {
  109. return err
  110. }
  111. line = line[n+1:] // Ignore the space.
  112. }
  113. }
  114. return nil
  115. }
  116. func isSupportedCurve(curve string) bool {
  117. switch curve {
  118. case "brainpoolP224r1", "brainpoolP224t1", "brainpoolP256r1", "brainpoolP256t1", "brainpoolP320r1", "brainpoolP320t1", "brainpoolP384r1", "brainpoolP384t1", "brainpoolP512r1", "brainpoolP512t1", "secp256k1":
  119. return false
  120. case "edwards25519", "curve25519", "secp224r1", "secp256r1", "secp384r1", "secp521r1":
  121. return true
  122. default:
  123. panic("Unknown curve: " + curve)
  124. }
  125. }
  126. func convertWycheproof(jsonPath, txtPath string) error {
  127. jsonData, err := ioutil.ReadFile(jsonPath)
  128. if err != nil {
  129. return err
  130. }
  131. var w wycheproofTest
  132. if err := json.Unmarshal(jsonData, &w); err != nil {
  133. return err
  134. }
  135. f, err := os.OpenFile(txtPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
  136. if err != nil {
  137. return err
  138. }
  139. defer f.Close()
  140. if _, err := fmt.Fprintf(f, `# Imported from Wycheproof's %s.
  141. # This file is generated by convert_wycheproof.go. Do not edit by hand.
  142. #
  143. # Algorithm: %s
  144. # Generator version: %s
  145. `, jsonPath, w.Algorithm, w.GeneratorVersion); err != nil {
  146. return err
  147. }
  148. for _, group := range w.TestGroups {
  149. // Skip tests with unsupported curves. We filter these out at
  150. // conversion time to avoid unnecessarily inflating
  151. // crypto_test_data.cc.
  152. if curve, ok := group["curve"]; ok && !isSupportedCurve(curve.(string)) {
  153. continue
  154. }
  155. if keyI, ok := group["key"]; ok {
  156. if key, ok := keyI.(map[string]interface{}); ok {
  157. if curve, ok := key["curve"]; ok && !isSupportedCurve(curve.(string)) {
  158. continue
  159. }
  160. }
  161. }
  162. for _, k := range sortedKeys(group) {
  163. // Wycheproof files always include both keyPem and
  164. // keyDer. Skip keyPem as they contain newlines. We
  165. // process keyDer more easily.
  166. if k == "type" || k == "tests" || k == "keyPem" {
  167. continue
  168. }
  169. if err := printAttribute(f, k, group[k], true); err != nil {
  170. return err
  171. }
  172. }
  173. fmt.Fprintf(f, "\n")
  174. tests := group["tests"].([]interface{})
  175. for _, testI := range tests {
  176. test := testI.(map[string]interface{})
  177. // Skip tests with unsupported curves.
  178. if curve, ok := test["curve"]; ok && !isSupportedCurve(curve.(string)) {
  179. continue
  180. }
  181. if comment, ok := test["comment"]; ok {
  182. if err := printComment(f, comment.(string)); err != nil {
  183. return err
  184. }
  185. }
  186. for _, k := range sortedKeys(test) {
  187. if k == "comment" || k == "flags" || k == "tcId" {
  188. continue
  189. }
  190. if err := printAttribute(f, k, test[k], false); err != nil {
  191. return err
  192. }
  193. }
  194. if flags, ok := test["flags"]; ok {
  195. for _, flag := range flags.([]interface{}) {
  196. if note, ok := w.Notes[flag.(string)]; ok {
  197. if err := printComment(f, note); err != nil {
  198. return err
  199. }
  200. }
  201. }
  202. }
  203. if _, err := fmt.Fprintf(f, "\n"); err != nil {
  204. return err
  205. }
  206. }
  207. }
  208. return nil
  209. }
  210. func main() {
  211. jsonPaths := []string{
  212. "ecdsa_secp224r1_sha224_test.json",
  213. "ecdsa_secp224r1_sha256_test.json",
  214. "ecdsa_secp256r1_sha256_test.json",
  215. "ecdsa_secp384r1_sha384_test.json",
  216. "ecdsa_secp384r1_sha512_test.json",
  217. "ecdsa_secp521r1_sha512_test.json",
  218. "rsa_signature_test.json",
  219. "x25519_test.json",
  220. // TODO(davidben): The following tests still need test drivers.
  221. // "aes_cbc_pkcs5_test.json",
  222. // "aes_gcm_siv_test.json",
  223. // "aes_gcm_test.json",
  224. // "chacha20_poly1305_test.json",
  225. // "dsa_test.json",
  226. // "ecdh_test.json",
  227. // "eddsa_test.json",
  228. }
  229. for _, jsonPath := range jsonPaths {
  230. if !strings.HasSuffix(jsonPath, ".json") {
  231. panic(jsonPath)
  232. }
  233. txtPath := jsonPath[:len(jsonPath)-len(".json")] + ".txt"
  234. if err := convertWycheproof(jsonPath, txtPath); err != nil {
  235. fmt.Fprintf(os.Stderr, "Error converting %s: %s\n", jsonPath, err)
  236. os.Exit(1)
  237. }
  238. }
  239. }