Skip to content

Commit b544f76

Browse files
author
Alex Couture-Beil
committed
add sha256 to verify integrity of message
Signed-off-by: Alex Couture-Beil <alex@mofo.ca>
1 parent 2c3b57b commit b544f76

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cmd/secretshare/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func encryptAES256(data []byte) ([]byte, []byte, error) {
7373
}
7474
plaintext := buf.Bytes()
7575

76+
sum := sha256.Sum256(plaintext)
77+
plaintext = append(sum[:], plaintext...)
78+
7679
block, err := aes.NewCipher(key)
7780
if err != nil {
7881
return nil, nil, err
@@ -86,7 +89,6 @@ func encryptAES256(data []byte) ([]byte, []byte, error) {
8689

8790
mode := cipher.NewCBCEncrypter(block, iv)
8891
mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
89-
9092
return key, ciphertext, nil
9193
}
9294

@@ -111,7 +113,13 @@ func decryptAES(key, ciphertext []byte) ([]byte, error) {
111113
// works inplace when both args are the same
112114
mode.CryptBlocks(ciphertext, ciphertext)
113115

114-
buf := bytes.NewReader(ciphertext)
116+
expectedSum := ciphertext[:32]
117+
actualSum := sha256.Sum256(ciphertext[32:])
118+
if !bytes.Equal(expectedSum, actualSum[:]) {
119+
return nil, fmt.Errorf("sha256 mismatch %v vs %v", expectedSum, actualSum)
120+
}
121+
122+
buf := bytes.NewReader(ciphertext[32:])
115123
var n uint64
116124
if err = binary.Read(buf, binary.LittleEndian, &n); err != nil {
117125
return nil, err

0 commit comments

Comments
 (0)