From 190f71ae9c7b78334fde553ef93a85504130b8c0 Mon Sep 17 00:00:00 2001 From: Silke Date: Sun, 16 Jul 2017 12:06:47 +0200 Subject: [PATCH] Split range checks to Size and Int CheckSizeInRange now has a []byte argument CheckIntInRange has an Int argument --- cryptogenerichash/crypto_generichash.go | 10 +++++----- cryptokdf/crypto_kdf.go | 2 +- support/support.go | 7 +++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cryptogenerichash/crypto_generichash.go b/cryptogenerichash/crypto_generichash.go index 8e084c0..0a74753 100644 --- a/cryptogenerichash/crypto_generichash.go +++ b/cryptogenerichash/crypto_generichash.go @@ -40,11 +40,11 @@ func CryptoGenericHashStateBytes() int { // I took care of the typedef confusions. This should work okay. func CryptoGenericHash(outlen int, in []byte, key []byte) ([]byte, int) { - support.CheckSizeInRange(outlen, CryptoGenericHashBytesMin(), CryptoGenericHashBytesMax(), "out") + support.CheckIntInRange(outlen, CryptoGenericHashBytesMin(), CryptoGenericHashBytesMax(), "out") // Check size of key only if actually given if len(key) > 0 { - support.CheckSizeInRange(len(key), CryptoGenericHashKeyBytesMin(), CryptoGenericHashKeyBytesMax(), "key") + support.CheckSizeInRange(key, CryptoGenericHashKeyBytesMin(), CryptoGenericHashKeyBytesMax(), "key") } out := make([]byte, outlen) @@ -61,11 +61,11 @@ func CryptoGenericHash(outlen int, in []byte, key []byte) ([]byte, int) { // I took care of the typedef confusions. This should work okay. func CryptoGenericHashInit(key []byte, outlen int) (*C.struct_crypto_generichash_blake2b_state, int) { - support.CheckSizeInRange(outlen, CryptoGenericHashBytesMin(), CryptoGenericHashBytesMax(), "out") + support.CheckIntInRange(outlen, CryptoGenericHashBytesMin(), CryptoGenericHashBytesMax(), "out") // Check size of key only if actually given if len(key) > 0 { - support.CheckSizeInRange(len(key), CryptoGenericHashKeyBytesMin(), CryptoGenericHashKeyBytesMax(), "key") + support.CheckSizeInRange(key, CryptoGenericHashKeyBytesMin(), CryptoGenericHashKeyBytesMax(), "key") } state := new(C.struct_crypto_generichash_blake2b_state) @@ -89,7 +89,7 @@ func CryptoGenericHashUpdate(state *C.struct_crypto_generichash_blake2b_state, i } func CryptoGenericHashFinal(state *C.struct_crypto_generichash_blake2b_state, outlen int) (*C.struct_crypto_generichash_blake2b_state, []byte, int) { - support.CheckSizeInRange(outlen, CryptoGenericHashBytesMin(), CryptoGenericHashBytesMax(), "out") + support.CheckIntInRange(outlen, CryptoGenericHashBytesMin(), CryptoGenericHashBytesMax(), "out") out := make([]byte, outlen) exit := int(C.crypto_generichash_final( state, diff --git a/cryptokdf/crypto_kdf.go b/cryptokdf/crypto_kdf.go index 660150c..e761051 100644 --- a/cryptokdf/crypto_kdf.go +++ b/cryptokdf/crypto_kdf.go @@ -31,7 +31,7 @@ func CryptoKdfKeygen() []byte { func CryptoKdfDeriveFromKey(l int, i uint64, c string, k []byte) ([]byte, int) { support.CheckSize(k, CryptoKdfKeybytes(), "keybytes") support.CheckSize([]byte(c), CryptoKdfContextbytes(), "contextbytes") - support.CheckSizeInRange(l, CryptoKdfBytesMin(), CryptoKdfBytesMax(), "subkey_len") + support.CheckIntInRange(l, CryptoKdfBytesMin(), CryptoKdfBytesMax(), "subkey_len") out := make([]byte, l) exit := int(C.crypto_kdf_derive_from_key( diff --git a/support/support.go b/support/support.go index 657412b..414cd25 100644 --- a/support/support.go +++ b/support/support.go @@ -19,6 +19,13 @@ func CheckSizeMin(buf []byte, min int, descrip string) { } } +// CheckIntInRange checks if the size of an integer is between a lower and upper boundaries. +func CheckIntInRange(n int, min int, max int, descrip string) { + if n < min || n > max { + panic(fmt.Sprintf("Incorrect %s size, expected (%d - %d), got (%d).", descrip, min, max, n)) + } +} + // CheckSizeInRange checks if the length of a byte slice is between a lower and upper boundaries. func CheckSizeInRange(buf []byte, min int, max int, descrip string) { if len(buf) < min || len(buf) > max {