From 6bdd58a59dc2c03b82c63fe89a729bd161192d71 Mon Sep 17 00:00:00 2001 From: sina Date: Sat, 30 Jul 2022 19:16:47 +0430 Subject: subtle package --- go.mod | 6 ++++-- go.sum | 2 ++ rabbit.go | 4 +++- subtle/overlap.go | 18 ++++++++++++++++++ subtle/purelap.go | 18 ++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 go.sum create mode 100644 subtle/overlap.go create mode 100644 subtle/purelap.go diff --git a/go.mod b/go.mod index 9c472c0..2879915 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ -module snix.ir/rabbitio +module rabbitio -go 1.17 +go 1.18 + +require snix.ir/rabbitio v0.0.0-20220730143105-e697fd436cab diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..290f6e5 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +snix.ir/rabbitio v0.0.0-20220730143105-e697fd436cab h1:Bvqk4nRrgrzZ8m+MNUzUzPUeGTbM+I/e4denc2Cmk5U= +snix.ir/rabbitio v0.0.0-20220730143105-e697fd436cab/go.mod h1:fGdJrOtVK0uqEI8x6SBI/NPAAQmtQi/4sjwQPShasEA= diff --git a/rabbit.go b/rabbit.go index 3d77daa..fe88c92 100644 --- a/rabbit.go +++ b/rabbit.go @@ -5,6 +5,8 @@ import ( "encoding/binary" "errors" "math/bits" + + "rabbitio/subtle" ) const ( @@ -141,7 +143,7 @@ func (r *rabbitCipher) XORKeyStream(dst, src []byte) { panic("rabbitio: output smaller than input") } - if InexactOverlap(dst, src) { + if subtle.InexactOverlap(dst, src) { panic("rabbitio: invalid buffer memory overlap") } diff --git a/subtle/overlap.go b/subtle/overlap.go new file mode 100644 index 0000000..82ab250 --- /dev/null +++ b/subtle/overlap.go @@ -0,0 +1,18 @@ +//go:build !purego + +package subtle + +import "unsafe" + +func AnyOverlap(x, y []byte) bool { + return len(x) > 0 && len(y) > 0 && + uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) && + uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1])) +} + +func InexactOverlap(x, y []byte) bool { + if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] { + return false + } + return AnyOverlap(x, y) +} diff --git a/subtle/purelap.go b/subtle/purelap.go new file mode 100644 index 0000000..e983129 --- /dev/null +++ b/subtle/purelap.go @@ -0,0 +1,18 @@ +//go:build purego + +package subtle + +import "reflect" + +func AnyOverlap(x, y []byte) bool { + return len(x) > 0 && len(y) > 0 && + reflect.ValueOf(&x[0]).Pointer() <= reflect.ValueOf(&y[len(y)-1]).Pointer() && + reflect.ValueOf(&y[0]).Pointer() <= reflect.ValueOf(&x[len(x)-1]).Pointer() +} + +func InexactOverlap(x, y []byte) bool { + if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] { + return false + } + return AnyOverlap(x, y) +} -- cgit v1.2.3