Skip to content

Commit e3ea893

Browse files
committed
Fix slicing error when data is too short
1 parent 07d41ba commit e3ea893

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

aesutils/aesutils.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ func DecryptLegacy(data, key []byte) ([]byte, error) {
104104
}
105105

106106
func prepareCMAC(data, key []byte) ([]byte, error) {
107+
// Validate data length to avoid slicing error
108+
if len(data) < 16 {
109+
return nil, errors.New("data too short for CMAC computation")
110+
}
111+
107112
// Create AES cipher
108113
block, err := aes.NewCipher(key)
109114
if err != nil {
@@ -117,10 +122,19 @@ func prepareCMAC(data, key []byte) ([]byte, error) {
117122
}
118123

119124
// Update CMAC with parts of the data
120-
cmacObj.Write(data[:16])
121-
cmacObj.Write(data[4:])
125+
if _, err := cmacObj.Write(data[:16]); err != nil {
126+
return nil, err
127+
}
128+
if len(data) > 4 {
129+
if _, err := cmacObj.Write(data[4:]); err != nil {
130+
return nil, err
131+
}
132+
}
122133

123134
// Compute MAC and return the first 4 bytes (MAC length of 4 bytes)
124135
mac := cmacObj.Sum(nil)
136+
if len(mac) < 4 {
137+
return nil, errors.New("computed CMAC too short")
138+
}
125139
return mac[:4], nil
126140
}

0 commit comments

Comments
 (0)