git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/kdf_test.go (about) 1 package crypto 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestKeySizes(t *testing.T) { 9 if KeySize256 != 32 { 10 t.Error("KeySize256 != 32") 11 } 12 if KeySize384 != 48 { 13 t.Error("KeySize384 != 48") 14 } 15 if KeySize512 != 64 { 16 t.Error("KeySize512 != 64") 17 } 18 if KeySize1024 != 128 { 19 t.Error("KeySize1024 != 128") 20 } 21 if KeySize2048 != 256 { 22 t.Error("KeySize2048 != 256") 23 } 24 if KeySize4096 != 512 { 25 t.Error("KeySize4096 != 512") 26 } 27 } 28 29 func TestDeriveKeyFromKeyKeyLen(t *testing.T) { 30 info := []byte("com.bloom42.gobox") 31 key, err := RandBytes(KeySize512) 32 if err != nil { 33 t.Error(err) 34 } 35 36 _, err = DeriveKeyFromKey(key, info, 128) 37 if err == nil { 38 t.Error("Accept invalid keyLen") 39 } 40 41 _, err = DeriveKeyFromKey(key, info, 65) 42 if err == nil { 43 t.Error("Accept invalid keyLen") 44 } 45 46 _, err = DeriveKeyFromKey(key, info, 0) 47 if err == nil { 48 t.Error("Accept invalid keyLen") 49 } 50 51 _, err = DeriveKeyFromKey(key, info, 1) 52 if err != nil { 53 t.Error("Reject valid keyLen") 54 } 55 56 _, err = DeriveKeyFromKey(key, info, 64) 57 if err != nil { 58 t.Error("Reject valid keyLen") 59 } 60 } 61 62 func TestDeriveKeyFromKeyContext(t *testing.T) { 63 info1 := []byte("com.bloom42.gobox1") 64 info2 := []byte("com.bloom42.gobox2") 65 key1, err := RandBytes(KeySize512) 66 if err != nil { 67 t.Error(err) 68 } 69 key2, err := RandBytes(KeySize512) 70 if err != nil { 71 t.Error(err) 72 } 73 74 subKey1, err := DeriveKeyFromKey(key1, info1, KeySize256) 75 if err != nil { 76 t.Error(err) 77 } 78 79 subKey2, err := DeriveKeyFromKey(key1, info2, KeySize256) 80 if err != nil { 81 t.Error(err) 82 } 83 84 if bytes.Equal(subKey1, subKey2) { 85 t.Error("subKey1 and subKey2 are equal") 86 } 87 88 subKey3, err := DeriveKeyFromKey(key1, info1, KeySize256) 89 if err != nil { 90 t.Error(err) 91 } 92 93 if !bytes.Equal(subKey1, subKey3) { 94 t.Error("subKey1 and subKey3 are different") 95 } 96 97 subKey4, err := DeriveKeyFromKey(key2, info1, KeySize256) 98 if err != nil { 99 t.Error(err) 100 } 101 102 if bytes.Equal(subKey1, subKey4) { 103 t.Error("subKey1 and subKey4 are equal") 104 } 105 }