forked from janimo/textsecure
/
crypto.go
70 lines (61 loc) · 1.92 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package axolotl
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"io"
)
func randBytes(data []byte) {
if _, err := io.ReadFull(rand.Reader, data); err != nil {
panic(err)
}
}
// Decrypt returns the AES-CBC decryption of a ciphertext under a given key.
func Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(ciphertext)%aes.BlockSize != 0 {
return nil, errors.New("not multiple of AES blocksize")
}
iv := ciphertext[:aes.BlockSize]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(ciphertext, ciphertext)
pad := ciphertext[len(ciphertext)-1]
if pad > aes.BlockSize {
return nil, fmt.Errorf("pad value (%d) larger than AES blocksize (%d)", pad, aes.BlockSize)
}
return ciphertext[aes.BlockSize : len(ciphertext)-int(pad)], nil
}
// Encrypt returns the AES-CBC encryption of a plaintext under a given key.
func Encrypt(key, iv, plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
pad := aes.BlockSize - len(plaintext)%aes.BlockSize
plaintext = append(plaintext, bytes.Repeat([]byte{byte(pad)}, pad)...)
ciphertext := make([]byte, len(plaintext))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)
return ciphertext, nil
}
// ValidTruncMAC checks whether a message is correctly authenticated using HMAC-SHA256.
func ValidTruncMAC(msg, expectedMAC, key []byte) bool {
actualMAC := ComputeTruncatedMAC(msg, key, len(expectedMAC))
return hmac.Equal(actualMAC, expectedMAC)
}
// ComputeTruncatedMAC computes a HMAC-SHA256 MAC and returns its prefix of a given size.
func ComputeTruncatedMAC(msg, key []byte, size int) []byte {
m := hmac.New(sha256.New, key)
m.Write(msg)
return m.Sum(nil)[:size]
}