hkdf_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package hkdf
  5. import (
  6. "bytes"
  7. "crypto/md5"
  8. "crypto/sha1"
  9. "crypto/sha256"
  10. "crypto/sha512"
  11. "hash"
  12. "io"
  13. "testing"
  14. )
  15. type hkdfTest struct {
  16. hash func() hash.Hash
  17. master []byte
  18. salt []byte
  19. prk []byte
  20. info []byte
  21. out []byte
  22. }
  23. var hkdfTests = []hkdfTest{
  24. // Tests from RFC 5869
  25. {
  26. sha256.New,
  27. []byte{
  28. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31. },
  32. []byte{
  33. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  34. 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  35. },
  36. []byte{
  37. 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf,
  38. 0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63,
  39. 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31,
  40. 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5,
  41. },
  42. []byte{
  43. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  44. 0xf8, 0xf9,
  45. },
  46. []byte{
  47. 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  48. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  49. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  50. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  51. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  52. 0x58, 0x65,
  53. },
  54. },
  55. {
  56. sha256.New,
  57. []byte{
  58. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  59. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  60. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  61. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  62. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  63. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  64. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  65. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  66. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  67. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  68. },
  69. []byte{
  70. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  71. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  72. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  73. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  74. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  75. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  76. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  77. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  78. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  79. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  80. },
  81. []byte{
  82. 0x06, 0xa6, 0xb8, 0x8c, 0x58, 0x53, 0x36, 0x1a,
  83. 0x06, 0x10, 0x4c, 0x9c, 0xeb, 0x35, 0xb4, 0x5c,
  84. 0xef, 0x76, 0x00, 0x14, 0x90, 0x46, 0x71, 0x01,
  85. 0x4a, 0x19, 0x3f, 0x40, 0xc1, 0x5f, 0xc2, 0x44,
  86. },
  87. []byte{
  88. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  89. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  90. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  91. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  92. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  93. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  94. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  95. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  96. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  97. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
  98. },
  99. []byte{
  100. 0xb1, 0x1e, 0x39, 0x8d, 0xc8, 0x03, 0x27, 0xa1,
  101. 0xc8, 0xe7, 0xf7, 0x8c, 0x59, 0x6a, 0x49, 0x34,
  102. 0x4f, 0x01, 0x2e, 0xda, 0x2d, 0x4e, 0xfa, 0xd8,
  103. 0xa0, 0x50, 0xcc, 0x4c, 0x19, 0xaf, 0xa9, 0x7c,
  104. 0x59, 0x04, 0x5a, 0x99, 0xca, 0xc7, 0x82, 0x72,
  105. 0x71, 0xcb, 0x41, 0xc6, 0x5e, 0x59, 0x0e, 0x09,
  106. 0xda, 0x32, 0x75, 0x60, 0x0c, 0x2f, 0x09, 0xb8,
  107. 0x36, 0x77, 0x93, 0xa9, 0xac, 0xa3, 0xdb, 0x71,
  108. 0xcc, 0x30, 0xc5, 0x81, 0x79, 0xec, 0x3e, 0x87,
  109. 0xc1, 0x4c, 0x01, 0xd5, 0xc1, 0xf3, 0x43, 0x4f,
  110. 0x1d, 0x87,
  111. },
  112. },
  113. {
  114. sha256.New,
  115. []byte{
  116. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  117. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  118. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  119. },
  120. []byte{},
  121. []byte{
  122. 0x19, 0xef, 0x24, 0xa3, 0x2c, 0x71, 0x7b, 0x16,
  123. 0x7f, 0x33, 0xa9, 0x1d, 0x6f, 0x64, 0x8b, 0xdf,
  124. 0x96, 0x59, 0x67, 0x76, 0xaf, 0xdb, 0x63, 0x77,
  125. 0xac, 0x43, 0x4c, 0x1c, 0x29, 0x3c, 0xcb, 0x04,
  126. },
  127. []byte{},
  128. []byte{
  129. 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  130. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  131. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  132. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  133. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  134. 0x96, 0xc8,
  135. },
  136. },
  137. {
  138. sha256.New,
  139. []byte{
  140. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  141. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  142. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  143. },
  144. nil,
  145. []byte{
  146. 0x19, 0xef, 0x24, 0xa3, 0x2c, 0x71, 0x7b, 0x16,
  147. 0x7f, 0x33, 0xa9, 0x1d, 0x6f, 0x64, 0x8b, 0xdf,
  148. 0x96, 0x59, 0x67, 0x76, 0xaf, 0xdb, 0x63, 0x77,
  149. 0xac, 0x43, 0x4c, 0x1c, 0x29, 0x3c, 0xcb, 0x04,
  150. },
  151. nil,
  152. []byte{
  153. 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  154. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  155. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  156. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  157. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  158. 0x96, 0xc8,
  159. },
  160. },
  161. {
  162. sha1.New,
  163. []byte{
  164. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  165. 0x0b, 0x0b, 0x0b,
  166. },
  167. []byte{
  168. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  169. 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  170. },
  171. []byte{
  172. 0x9b, 0x6c, 0x18, 0xc4, 0x32, 0xa7, 0xbf, 0x8f,
  173. 0x0e, 0x71, 0xc8, 0xeb, 0x88, 0xf4, 0xb3, 0x0b,
  174. 0xaa, 0x2b, 0xa2, 0x43,
  175. },
  176. []byte{
  177. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  178. 0xf8, 0xf9,
  179. },
  180. []byte{
  181. 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  182. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  183. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  184. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  185. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  186. 0xf8, 0x96,
  187. },
  188. },
  189. {
  190. sha1.New,
  191. []byte{
  192. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  193. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  194. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  195. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  196. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  197. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  198. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  199. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  200. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  201. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  202. },
  203. []byte{
  204. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  205. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  206. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  207. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  208. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  209. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  210. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  211. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  212. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  213. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  214. },
  215. []byte{
  216. 0x8a, 0xda, 0xe0, 0x9a, 0x2a, 0x30, 0x70, 0x59,
  217. 0x47, 0x8d, 0x30, 0x9b, 0x26, 0xc4, 0x11, 0x5a,
  218. 0x22, 0x4c, 0xfa, 0xf6,
  219. },
  220. []byte{
  221. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  222. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  223. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  224. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  225. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  226. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  227. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  228. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  229. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  230. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
  231. },
  232. []byte{
  233. 0x0b, 0xd7, 0x70, 0xa7, 0x4d, 0x11, 0x60, 0xf7,
  234. 0xc9, 0xf1, 0x2c, 0xd5, 0x91, 0x2a, 0x06, 0xeb,
  235. 0xff, 0x6a, 0xdc, 0xae, 0x89, 0x9d, 0x92, 0x19,
  236. 0x1f, 0xe4, 0x30, 0x56, 0x73, 0xba, 0x2f, 0xfe,
  237. 0x8f, 0xa3, 0xf1, 0xa4, 0xe5, 0xad, 0x79, 0xf3,
  238. 0xf3, 0x34, 0xb3, 0xb2, 0x02, 0xb2, 0x17, 0x3c,
  239. 0x48, 0x6e, 0xa3, 0x7c, 0xe3, 0xd3, 0x97, 0xed,
  240. 0x03, 0x4c, 0x7f, 0x9d, 0xfe, 0xb1, 0x5c, 0x5e,
  241. 0x92, 0x73, 0x36, 0xd0, 0x44, 0x1f, 0x4c, 0x43,
  242. 0x00, 0xe2, 0xcf, 0xf0, 0xd0, 0x90, 0x0b, 0x52,
  243. 0xd3, 0xb4,
  244. },
  245. },
  246. {
  247. sha1.New,
  248. []byte{
  249. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  250. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  251. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  252. },
  253. []byte{},
  254. []byte{
  255. 0xda, 0x8c, 0x8a, 0x73, 0xc7, 0xfa, 0x77, 0x28,
  256. 0x8e, 0xc6, 0xf5, 0xe7, 0xc2, 0x97, 0x78, 0x6a,
  257. 0xa0, 0xd3, 0x2d, 0x01,
  258. },
  259. []byte{},
  260. []byte{
  261. 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  262. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  263. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  264. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  265. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  266. 0x49, 0x18,
  267. },
  268. },
  269. {
  270. sha1.New,
  271. []byte{
  272. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  273. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  274. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  275. },
  276. nil,
  277. []byte{
  278. 0x2a, 0xdc, 0xca, 0xda, 0x18, 0x77, 0x9e, 0x7c,
  279. 0x20, 0x77, 0xad, 0x2e, 0xb1, 0x9d, 0x3f, 0x3e,
  280. 0x73, 0x13, 0x85, 0xdd,
  281. },
  282. nil,
  283. []byte{
  284. 0x2c, 0x91, 0x11, 0x72, 0x04, 0xd7, 0x45, 0xf3,
  285. 0x50, 0x0d, 0x63, 0x6a, 0x62, 0xf6, 0x4f, 0x0a,
  286. 0xb3, 0xba, 0xe5, 0x48, 0xaa, 0x53, 0xd4, 0x23,
  287. 0xb0, 0xd1, 0xf2, 0x7e, 0xbb, 0xa6, 0xf5, 0xe5,
  288. 0x67, 0x3a, 0x08, 0x1d, 0x70, 0xcc, 0xe7, 0xac,
  289. 0xfc, 0x48,
  290. },
  291. },
  292. }
  293. func TestHKDF(t *testing.T) {
  294. for i, tt := range hkdfTests {
  295. prk := Extract(tt.hash, tt.master, tt.salt)
  296. if !bytes.Equal(prk, tt.prk) {
  297. t.Errorf("test %d: incorrect PRK: have %v, need %v.", i, prk, tt.prk)
  298. }
  299. hkdf := New(tt.hash, tt.master, tt.salt, tt.info)
  300. out := make([]byte, len(tt.out))
  301. n, err := io.ReadFull(hkdf, out)
  302. if n != len(tt.out) || err != nil {
  303. t.Errorf("test %d: not enough output bytes: %d.", i, n)
  304. }
  305. if !bytes.Equal(out, tt.out) {
  306. t.Errorf("test %d: incorrect output: have %v, need %v.", i, out, tt.out)
  307. }
  308. hkdf = Expand(tt.hash, prk, tt.info)
  309. n, err = io.ReadFull(hkdf, out)
  310. if n != len(tt.out) || err != nil {
  311. t.Errorf("test %d: not enough output bytes from Expand: %d.", i, n)
  312. }
  313. if !bytes.Equal(out, tt.out) {
  314. t.Errorf("test %d: incorrect output from Expand: have %v, need %v.", i, out, tt.out)
  315. }
  316. }
  317. }
  318. func TestHKDFMultiRead(t *testing.T) {
  319. for i, tt := range hkdfTests {
  320. hkdf := New(tt.hash, tt.master, tt.salt, tt.info)
  321. out := make([]byte, len(tt.out))
  322. for b := 0; b < len(tt.out); b++ {
  323. n, err := io.ReadFull(hkdf, out[b:b+1])
  324. if n != 1 || err != nil {
  325. t.Errorf("test %d.%d: not enough output bytes: have %d, need %d .", i, b, n, len(tt.out))
  326. }
  327. }
  328. if !bytes.Equal(out, tt.out) {
  329. t.Errorf("test %d: incorrect output: have %v, need %v.", i, out, tt.out)
  330. }
  331. }
  332. }
  333. func TestHKDFLimit(t *testing.T) {
  334. hash := sha1.New
  335. master := []byte{0x00, 0x01, 0x02, 0x03}
  336. info := []byte{}
  337. hkdf := New(hash, master, nil, info)
  338. limit := hash().Size() * 255
  339. out := make([]byte, limit)
  340. // The maximum output bytes should be extractable
  341. n, err := io.ReadFull(hkdf, out)
  342. if n != limit || err != nil {
  343. t.Errorf("not enough output bytes: %d, %v.", n, err)
  344. }
  345. // Reading one more should fail
  346. n, err = io.ReadFull(hkdf, make([]byte, 1))
  347. if n > 0 || err == nil {
  348. t.Errorf("key expansion overflowed: n = %d, err = %v", n, err)
  349. }
  350. }
  351. func Benchmark16ByteMD5Single(b *testing.B) {
  352. benchmarkHKDFSingle(md5.New, 16, b)
  353. }
  354. func Benchmark20ByteSHA1Single(b *testing.B) {
  355. benchmarkHKDFSingle(sha1.New, 20, b)
  356. }
  357. func Benchmark32ByteSHA256Single(b *testing.B) {
  358. benchmarkHKDFSingle(sha256.New, 32, b)
  359. }
  360. func Benchmark64ByteSHA512Single(b *testing.B) {
  361. benchmarkHKDFSingle(sha512.New, 64, b)
  362. }
  363. func Benchmark8ByteMD5Stream(b *testing.B) {
  364. benchmarkHKDFStream(md5.New, 8, b)
  365. }
  366. func Benchmark16ByteMD5Stream(b *testing.B) {
  367. benchmarkHKDFStream(md5.New, 16, b)
  368. }
  369. func Benchmark8ByteSHA1Stream(b *testing.B) {
  370. benchmarkHKDFStream(sha1.New, 8, b)
  371. }
  372. func Benchmark20ByteSHA1Stream(b *testing.B) {
  373. benchmarkHKDFStream(sha1.New, 20, b)
  374. }
  375. func Benchmark8ByteSHA256Stream(b *testing.B) {
  376. benchmarkHKDFStream(sha256.New, 8, b)
  377. }
  378. func Benchmark32ByteSHA256Stream(b *testing.B) {
  379. benchmarkHKDFStream(sha256.New, 32, b)
  380. }
  381. func Benchmark8ByteSHA512Stream(b *testing.B) {
  382. benchmarkHKDFStream(sha512.New, 8, b)
  383. }
  384. func Benchmark64ByteSHA512Stream(b *testing.B) {
  385. benchmarkHKDFStream(sha512.New, 64, b)
  386. }
  387. func benchmarkHKDFSingle(hasher func() hash.Hash, block int, b *testing.B) {
  388. master := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
  389. salt := []byte{0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17}
  390. info := []byte{0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27}
  391. out := make([]byte, block)
  392. b.SetBytes(int64(block))
  393. b.ResetTimer()
  394. for i := 0; i < b.N; i++ {
  395. hkdf := New(hasher, master, salt, info)
  396. io.ReadFull(hkdf, out)
  397. }
  398. }
  399. func benchmarkHKDFStream(hasher func() hash.Hash, block int, b *testing.B) {
  400. master := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
  401. salt := []byte{0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17}
  402. info := []byte{0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27}
  403. out := make([]byte, block)
  404. b.SetBytes(int64(block))
  405. b.ResetTimer()
  406. hkdf := New(hasher, master, salt, info)
  407. for i := 0; i < b.N; i++ {
  408. _, err := io.ReadFull(hkdf, out)
  409. if err != nil {
  410. hkdf = New(hasher, master, salt, info)
  411. i--
  412. }
  413. }
  414. }