1
0

hkdf_test.go 11 KB

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