summaryrefslogtreecommitdiffstats
path: root/vendor/go.mau.fi/whatsmeow/util/gcmutil
diff options
context:
space:
mode:
authorWim <wim@42.be>2022-11-27 00:42:16 +0100
committerGitHub <noreply@github.com>2022-11-27 00:42:16 +0100
commit4fd0a7672777f0ed15692ae2ba47838208537558 (patch)
treeb119834a8b9ee78aa8f1b2ad05efa7da50516cbf /vendor/go.mau.fi/whatsmeow/util/gcmutil
parent6da9d567dc9195e9a5211f23a6795a41f56a1bfc (diff)
downloadmatterbridge-msglm-4fd0a7672777f0ed15692ae2ba47838208537558.tar.gz
matterbridge-msglm-4fd0a7672777f0ed15692ae2ba47838208537558.tar.bz2
matterbridge-msglm-4fd0a7672777f0ed15692ae2ba47838208537558.zip
Update dependencies (#1929)
Diffstat (limited to 'vendor/go.mau.fi/whatsmeow/util/gcmutil')
-rw-r--r--vendor/go.mau.fi/whatsmeow/util/gcmutil/gcm.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/whatsmeow/util/gcmutil/gcm.go b/vendor/go.mau.fi/whatsmeow/util/gcmutil/gcm.go
new file mode 100644
index 00000000..2451aeab
--- /dev/null
+++ b/vendor/go.mau.fi/whatsmeow/util/gcmutil/gcm.go
@@ -0,0 +1,41 @@
+// Copyright (c) 2022 Tulir Asokan
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+package gcmutil
+
+import (
+ "crypto/aes"
+ "crypto/cipher"
+ "fmt"
+)
+
+func Prepare(secretKey []byte) (gcm cipher.AEAD, err error) {
+ var block cipher.Block
+ if block, err = aes.NewCipher(secretKey); err != nil {
+ err = fmt.Errorf("failed to initialize AES cipher: %w", err)
+ } else if gcm, err = cipher.NewGCM(block); err != nil {
+ err = fmt.Errorf("failed to initialize GCM: %w", err)
+ }
+ return
+}
+
+func Decrypt(secretKey, iv, ciphertext, additionalData []byte) ([]byte, error) {
+ if gcm, err := Prepare(secretKey); err != nil {
+ return nil, err
+ } else if plaintext, decryptErr := gcm.Open(nil, iv, ciphertext, additionalData); decryptErr != nil {
+ return nil, decryptErr
+ } else {
+ return plaintext, nil
+ }
+}
+
+func Encrypt(secretKey, iv, plaintext, additionalData []byte) ([]byte, error) {
+ if gcm, err := Prepare(secretKey); err != nil {
+ return nil, err
+ } else {
+ return gcm.Seal(nil, iv, plaintext, additionalData), nil
+ }
+}