// if len(payload) >= (paddingLen - 1) then the MSB of t is zero
good = byte(int32(^t) >> 31)
- toCheck := 255 // the maximum possible padding length
+ // The maximum possible padding length plus the actual length field
+ toCheck := 256
// The length of the padded data is public, so we can use an if here
- if toCheck+1 > len(payload) {
- toCheck = len(payload) - 1
+ if toCheck > len(payload) {
+ toCheck = len(payload)
}
for i := 0; i < toCheck; i++ {
}
}
+// will be initialized with {0, 255, 255, ..., 255}
+var padding255Bad = [256]byte{}
+
+// will be initialized with {255, 255, 255, ..., 255}
+var padding255Good = [256]byte{255}
+
var paddingTests = []struct {
in []byte
good bool
{[]byte{1, 4, 4, 4, 4, 4}, true, 1},
{[]byte{5, 5, 5, 5, 5, 5}, true, 0},
{[]byte{6, 6, 6, 6, 6, 6}, false, 0},
+ {padding255Bad[:], false, 0},
+ {padding255Good[:], true, 0},
}
func TestRemovePadding(t *testing.T) {
+ for i := 1; i < len(padding255Bad); i++ {
+ padding255Bad[i] = 255
+ padding255Good[i] = 255
+ }
for i, test := range paddingTests {
paddingLen, good := extractPadding(test.in)
expectedGood := byte(255)