summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-09-16 23:42:31 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-09-16 23:42:31 +0200
commit942abf948a9121a4595f782093ca623534997776 (patch)
tree9e55153bbb5e3d5129a651aeaefe39a81b92de02
parent47d1140361eaeca7c0a4a940397f0a71b42c59ce (diff)
send: more precise padding calculation
-rw-r--r--send.go12
1 files changed, 7 insertions, 5 deletions
diff --git a/send.go b/send.go
index 3b6cfa3..bc0f5e4 100644
--- a/send.go
+++ b/send.go
@@ -488,11 +488,13 @@ func (device *Device) RoutineEncryption() {
// pad content to multiple of 16
mtu := int(atomic.LoadInt32(&device.tun.mtu))
- rem := len(elem.packet) % PaddingMultiple
- if rem > 0 {
- for i := 0; i < PaddingMultiple-rem && len(elem.packet) < mtu; i++ {
- elem.packet = append(elem.packet, 0)
- }
+ lastUnit := len(elem.packet) % mtu
+ paddedSize := (lastUnit + PaddingMultiple - 1) & ^(PaddingMultiple - 1)
+ if paddedSize > mtu {
+ paddedSize = mtu
+ }
+ for i := len(elem.packet); i < paddedSize; i++ {
+ elem.packet = append(elem.packet, 0)
}
// encrypt content and release to consumer