diff options
| author | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
| commit | e9c24f4af7ed56760f6db7941827d09f6db9020b (patch) | |
| tree | 62128c43b883ce5e3148113350978755779bb5de /teleirc/matterbridge/vendor/go.mau.fi/libsignal/cipher/Cipher.go | |
| parent | 58d5e7cfda4781d8a57ec52aefd02983835c301a (diff) | |
add matterbridge
Diffstat (limited to 'teleirc/matterbridge/vendor/go.mau.fi/libsignal/cipher/Cipher.go')
| -rw-r--r-- | teleirc/matterbridge/vendor/go.mau.fi/libsignal/cipher/Cipher.go | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/teleirc/matterbridge/vendor/go.mau.fi/libsignal/cipher/Cipher.go b/teleirc/matterbridge/vendor/go.mau.fi/libsignal/cipher/Cipher.go new file mode 100644 index 0000000..edfb428 --- /dev/null +++ b/teleirc/matterbridge/vendor/go.mau.fi/libsignal/cipher/Cipher.go @@ -0,0 +1,105 @@ +// Package cipher is a package for common encrypt/decrypt of symmetric key messages. +package cipher + +import ( + "bytes" + "crypto/aes" + "crypto/cipher" + "errors" +) + +// Decrypt will use the given key, iv, and ciphertext and return +// the plaintext bytes. +func Decrypt(iv, key, ciphertext []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + if len(ciphertext) < aes.BlockSize { + return nil, errors.New("ciphertext too short") + } + cbc := cipher.NewCBCDecrypter(block, iv) + cbc.CryptBlocks(ciphertext, ciphertext) + + unpaddedText, err := pkcs7Unpad(ciphertext, aes.BlockSize) + if err != nil { + return nil, err + } + + return unpaddedText, nil +} + +// Encrypt will use the given iv, key, and plaintext bytes +// and return ciphertext bytes. +func Encrypt(iv, key, plaintext []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + paddedText, err := pkcs7Pad(plaintext, block.BlockSize()) + if err != nil { + return nil, err + } + ciphertext := make([]byte, len(paddedText)) + mode := cipher.NewCBCEncrypter(block, iv) + mode.CryptBlocks(ciphertext, paddedText) + + return ciphertext, nil +} + +// PKCS7 padding. + +// PKCS7 errors. +var ( + // ErrInvalidBlockSize indicates hash blocksize <= 0. + ErrInvalidBlockSize = errors.New("invalid blocksize") + + // ErrInvalidPKCS7Data indicates bad input to PKCS7 pad or unpad. + ErrInvalidPKCS7Data = errors.New("invalid PKCS7 data (empty or not padded)") + + // ErrInvalidPKCS7Padding indicates PKCS7 unpad fails to bad input. + ErrInvalidPKCS7Padding = errors.New("invalid padding on input") +) + +// pkcs7Pad right-pads the given byte slice with 1 to n bytes, where +// n is the block size. The size of the result is x times n, where x +// is at least 1. +func pkcs7Pad(b []byte, blocksize int) ([]byte, error) { + if blocksize <= 0 { + return nil, ErrInvalidBlockSize + } + if b == nil || len(b) == 0 { + return nil, ErrInvalidPKCS7Data + } + n := blocksize - (len(b) % blocksize) + pb := make([]byte, len(b)+n) + copy(pb, b) + copy(pb[len(b):], bytes.Repeat([]byte{byte(n)}, n)) + return pb, nil +} + +// pkcs7Unpad validates and unpads data from the given bytes slice. +// The returned value will be 1 to n bytes smaller depending on the +// amount of padding, where n is the block size. +func pkcs7Unpad(b []byte, blocksize int) ([]byte, error) { + if blocksize <= 0 { + return nil, ErrInvalidBlockSize + } + if b == nil || len(b) == 0 { + return nil, ErrInvalidPKCS7Data + } + if len(b)%blocksize != 0 { + return nil, ErrInvalidPKCS7Padding + } + c := b[len(b)-1] + n := int(c) + if n == 0 || n > len(b) { + return nil, ErrInvalidPKCS7Padding + } + for i := 0; i < n; i++ { + if b[len(b)-n+i] != c { + return nil, ErrInvalidPKCS7Padding + } + } + return b[:len(b)-n], nil +} |
