github.com/liloew/wireguard-go@v0.0.0-20220224014633-9cd745e6f114/device/kdf_test.go (about) 1 /* SPDX-License-Identifier: MIT 2 * 3 * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. 4 */ 5 6 package device 7 8 import ( 9 "encoding/hex" 10 "testing" 11 12 "golang.org/x/crypto/blake2s" 13 ) 14 15 type KDFTest struct { 16 key string 17 input string 18 t0 string 19 t1 string 20 t2 string 21 } 22 23 func assertEquals(t *testing.T, a, b string) { 24 if a != b { 25 t.Fatal("expected", a, "=", b) 26 } 27 } 28 29 func TestKDF(t *testing.T) { 30 tests := []KDFTest{ 31 { 32 key: "746573742d6b6579", 33 input: "746573742d696e707574", 34 t0: "6f0e5ad38daba1bea8a0d213688736f19763239305e0f58aba697f9ffc41c633", 35 t1: "df1194df20802a4fe594cde27e92991c8cae66c366e8106aaa937a55fa371e8a", 36 t2: "fac6e2745a325f5dc5d11a5b165aad08b0ada28e7b4e666b7c077934a4d76c24", 37 }, 38 { 39 key: "776972656775617264", 40 input: "776972656775617264", 41 t0: "491d43bbfdaa8750aaf535e334ecbfe5129967cd64635101c566d4caefda96e8", 42 t1: "1e71a379baefd8a79aa4662212fcafe19a23e2b609a3db7d6bcba8f560e3d25f", 43 t2: "31e1ae48bddfbe5de38f295e5452b1909a1b4e38e183926af3780b0c1e1f0160", 44 }, 45 { 46 key: "", 47 input: "", 48 t0: "8387b46bf43eccfcf349552a095d8315c4055beb90208fb1be23b894bc2ed5d0", 49 t1: "58a0e5f6faefccf4807bff1f05fa8a9217945762040bcec2f4b4a62bdfe0e86e", 50 t2: "0ce6ea98ec548f8e281e93e32db65621c45eb18dc6f0a7ad94178610a2f7338e", 51 }, 52 } 53 54 var t0, t1, t2 [blake2s.Size]byte 55 56 for _, test := range tests { 57 key, _ := hex.DecodeString(test.key) 58 input, _ := hex.DecodeString(test.input) 59 KDF3(&t0, &t1, &t2, key, input) 60 t0s := hex.EncodeToString(t0[:]) 61 t1s := hex.EncodeToString(t1[:]) 62 t2s := hex.EncodeToString(t2[:]) 63 assertEquals(t, t0s, test.t0) 64 assertEquals(t, t1s, test.t1) 65 assertEquals(t, t2s, test.t2) 66 } 67 68 for _, test := range tests { 69 key, _ := hex.DecodeString(test.key) 70 input, _ := hex.DecodeString(test.input) 71 KDF2(&t0, &t1, key, input) 72 t0s := hex.EncodeToString(t0[:]) 73 t1s := hex.EncodeToString(t1[:]) 74 assertEquals(t, t0s, test.t0) 75 assertEquals(t, t1s, test.t1) 76 } 77 78 for _, test := range tests { 79 key, _ := hex.DecodeString(test.key) 80 input, _ := hex.DecodeString(test.input) 81 KDF1(&t0, key, input) 82 t0s := hex.EncodeToString(t0[:]) 83 assertEquals(t, t0s, test.t0) 84 } 85 }