// // Copyright (c) 2017 anygone contributors (see AUTHORS file) // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * Neither the name of anygone nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // package satp import ( "bytes" "crypto/aes" "math/rand" "testing" ) func TestAESCTRKeyDerivationNew(t *testing.T) { var buffer [100]byte rand.Read(buffer[:]) testvectors := []struct { key []byte salt []byte valid bool }{ {[]byte{}, []byte{}, false}, {[]byte{}, buffer[:], false}, {buffer[:], []byte{}, false}, {[]byte{}, buffer[:14], false}, {buffer[:16], []byte{}, false}, {buffer[:16], buffer[:14], true}, } for _, vector := range testvectors { for _, role := range []KDRole{RoleLeft, RoleRight} { _, err := NewAESCTRKeyDerivation(vector.key, vector.salt, role) if vector.valid { if err != nil { t.Fatal("unexpected error:", err) } } else { if err == nil { t.Fatalf("creating aes-ctr key derivation with key %v and salt %v should give an error", vector.key, vector.salt) } } } } } func TestAESCTRKeyDerivationGenerateCtr(t *testing.T) { testvectors := []struct { salt []byte role KDRole dir Direction usage KeyUsage sequenceNumber uint32 ctr []byte }{ {[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, RoleLeft, Outbound, UsageEncryptKey, 0x12345678, []byte{0, 0, 0, 0, 0, 0, 0x35, 0x6A, 0x19, 0x2B, 0x12, 0x34, 0x56, 0x78, 0, 0}}, {[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, RoleRight, Outbound, UsageEncryptSalt, 0x01020304, []byte{0, 0, 0, 0, 0, 0, 0x1B, 0x64, 0x53, 0x89, 0x01, 0x02, 0x03, 0x04, 0, 0}}, {[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, RoleLeft, Outbound, UsageAuthKey, 0xdeadbeef, []byte{0, 0, 0, 0, 0, 0, 0xAC, 0x34, 0x78, 0xD6, 0xDE, 0xAD, 0xBE, 0xEF, 0, 0}}, {[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, RoleLeft, Outbound, UsageAuthKey, 0xdeadbeef, []byte{0, 1, 2, 3, 4, 5, 0xAA, 0x33, 0x70, 0xDF, 0xD4, 0xA6, 0xB2, 0xE2, 0, 0}}, {[]byte{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC}, RoleLeft, Outbound, UsageEncryptSalt, 0x01020304, []byte{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xA9, 0x2E, 0x7A, 0xEE, 0x57, 0x7A, 0x99, 0xB8, 0, 0}}, } for _, vector := range testvectors { key := [aes.BlockSize]byte{} kd, err := NewAESCTRKeyDerivation(key[:], vector.salt, vector.role) if err != nil { t.Fatal("unexpected error:", err) } aesCTR := kd.(*AESCTRKeyDerivation) ctr := [aes.BlockSize]byte{} aesCTR.generateCTR(vector.dir, vector.usage, vector.sequenceNumber, ctr[:]) if bytes.Compare(ctr[:], vector.ctr) != 0 { t.Fatalf("resulting ctr is invalid, is: '%v', should be '%v'", ctr[:], vector.ctr) } } } func TestAESCTRKeyDerivationGenerate(t *testing.T) { key := []byte{0x77, 0x67, 0xA3, 0x90, 0x3E, 0xB9, 0x22, 0x06, 0x87, 0xBF, 0xF8, 0xA1, 0x4E, 0x94, 0x0F, 0xA7} salt := []byte{0x3D, 0x58, 0x3F, 0xC6, 0x70, 0xA9, 0x6B, 0xCF, 0x92, 0xDA, 0x27, 0x0D, 0x07, 0x67} kdLeft, err := NewAESCTRKeyDerivation(key, salt, RoleLeft) if err != nil { t.Fatal("unexpected error:", err) } kdRight, err := NewAESCTRKeyDerivation(key, salt, RoleRight) if err != nil { t.Fatal("unexpected error:", err) } testvectors := []struct { kd KeyDerivation dir Direction usage KeyUsage sequenceNumber uint32 out []byte }{ {kdLeft, Outbound, UsageEncryptKey, 0, []byte{0x46, 0x04, 0x12, 0x67, 0x0C, 0x7A, 0x20, 0x07, 0x67, 0xC5, 0x9A, 0xB4, 0xBE, 0x2E, 0xBE, 0x50, 0xE9, 0xBC, 0x04, 0x7C, 0x17, 0x54, 0x73, 0x02, 0x4A, 0x54, 0x28, 0x44, 0x6F, 0xFA, 0xD2, 0xB6}}, {kdLeft, Inbound, UsageEncryptKey, 0x0012CA37, []byte{0x93, 0xEE, 0xC4, 0xD4, 0x94, 0xDA, 0x29, 0x72, 0x0C, 0xF5, 0xB6, 0x1D, 0x45, 0x3F, 0xA1, 0x19, 0xB0, 0x31, 0xB1, 0x7D, 0xA3, 0x78, 0x2C, 0x7A, 0x72, 0x19, 0x39, 0x4E, 0xEE, 0x40, 0x52, 0xDA}}, {kdRight, Outbound, UsageEncryptSalt, 0x0012CA37, []byte{0x7C, 0x20, 0x7D, 0x35, 0xAD, 0x9C, 0xED, 0xC6, 0xE6, 0x16, 0xE4, 0xA9, 0x36, 0x3C, 0x49, 0xAA, 0xF4, 0xA9, 0x59, 0x0D, 0x6A, 0xC1, 0x63, 0xAB, 0x17, 0x3D, 0xA8, 0x9F, 0x3F, 0xD7, 0x10, 0xC3}}, {kdRight, Outbound, UsageAuthKey, 0xE, []byte{0xE3, 0x26, 0x29, 0x37, 0xED, 0xF1, 0x36, 0x80, 0xB4, 0x8E, 0x6C, 0xF7, 0x7C, 0xEE, 0x8C, 0x49, 0xEA, 0x70, 0x6D, 0x24, 0x25, 0x35, 0xAF, 0x79, 0x7F, 0x02, 0x7C, 0x59, 0x2C, 0x7E, 0x41, 0x55}}, {kdRight, Inbound, UsageAuthKey, 0xC, []byte{0x2C, 0xF9, 0x7A, 0x96, 0x17, 0x28, 0x52, 0xFD, 0xF6, 0x57, 0xC4, 0x86, 0x7D, 0xA8, 0x04, 0xE3, 0xB4, 0x23, 0xA6, 0xAE, 0x66, 0xF8, 0xB9, 0xCC, 0x79, 0x72, 0xA6, 0xB9, 0x54, 0x72, 0x7F, 0xF4}}, } for _, vector := range testvectors { out := [32]byte{} vector.kd.Generate(vector.dir, vector.usage, vector.sequenceNumber, out[:]) if bytes.Compare(out[:], vector.out) != 0 { t.Fatalf("resulting key material is invalid, is: '%v', should be '%v'", out[:], vector.out) } } } func TestAESCTRKeyDerivationGenerateRepeat(t *testing.T) { var buffer [256]byte rand.Read(buffer[:]) testvectors := []struct { key []byte salt []byte role KDRole dir Direction usage KeyUsage sequenceNumber uint32 length int }{ {buffer[:16], buffer[16:30], RoleLeft, Outbound, UsageEncryptKey, 0x12345678, 23}, {buffer[32:48], buffer[48:62], RoleLeft, Inbound, UsageEncryptKey, 0x1, 16}, {buffer[64:80], buffer[80:94], RoleRight, Outbound, UsageEncryptKey, 0x1, 20}, {buffer[96:112], buffer[112:126], RoleRight, Outbound, UsageEncryptKey, 0x1, 14}, } for _, vector := range testvectors { kd, err := NewAESCTRKeyDerivation(vector.key, vector.salt, vector.role) if err != nil { t.Fatal("unexpected error:", err) } out1 := make([]byte, vector.length) out2 := buffer[128 : 128+vector.length] kd.Generate(vector.dir, vector.usage, vector.sequenceNumber, out1) kd.Generate(vector.dir, vector.usage, vector.sequenceNumber, out2) if bytes.Compare(out1, out2) != 0 { t.Fatalf("result of 2 consecutive key generations differ, out1: '%v', out2: '%v'", out1, out2) } } } // // Benchmarking // func BenchmarkAESCTRKeyDerivationGenerateCtr(b *testing.B) { var buffer [256]byte rand.Read(buffer[:]) kd, err := NewAESCTRKeyDerivation(buffer[:16], buffer[16:30], RoleRight) if err != nil { b.Fatal("unexpected error:", err) } aesCTR := kd.(*AESCTRKeyDerivation) ctr := make([]byte, 16) b.ResetTimer() for i := 0; i < b.N; i++ { aesCTR.generateCTR(Outbound, UsageAuthKey, 0xAA55AA55, ctr) } } func BenchmarkAESCTRKeyDerivationGenerate(b *testing.B) { var buffer [256]byte rand.Read(buffer[:]) kd, err := NewAESCTRKeyDerivation(buffer[:16], buffer[16:30], RoleLeft) if err != nil { b.Fatal("unexpected error:", err) } out := make([]byte, 16) b.ResetTimer() for i := 0; i < b.N; i++ { kd.Generate(Outbound, UsageEncryptKey, 0xdeadbeef, out) } }