Skip to content

Commit

Permalink
Split range checks to Size and Int
Browse files Browse the repository at this point in the history
CheckSizeInRange now has a []byte argument
CheckIntInRange has an Int argument
  • Loading branch information
silkeh committed Aug 24, 2017
1 parent 9ee2988 commit 190f71a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
10 changes: 5 additions & 5 deletions cryptogenerichash/crypto_generichash.go
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cryptokdf/crypto_kdf.go
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions support/support.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 190f71a

Please sign in to comment.