| 1 | package pushover |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "compress/gzip" |
| 6 | "crypto/aes" |
| 7 | "crypto/cipher" |
| 8 | "crypto/hmac" |
| 9 | "crypto/rand" |
| 10 | "crypto/sha256" |
| 11 | "encoding/base64" |
| 12 | "encoding/hex" |
| 13 | "fmt" |
| 14 | ) |
| 15 | |
| 16 | const encryptionKeySize = 32 |
| 17 | |
| 18 | // parseEncryptionKey decodes a 64-character hex AES-256 key. |
| 19 | // |
| 20 | // An empty key disables E2EE and returns a nil slice. |
| 21 | // |
| 22 | // Parameters: |
| 23 | // - hexKey: 64-character hexadecimal string, or empty to disable encryption. |
| 24 | // |
| 25 | // Returns: |
| 26 | // - key: 32-byte AES key, or nil when hexKey is empty. |
| 27 | // - err: ErrInvalidEncryptionKey when hexKey is non-empty but not 32 decoded bytes. |
| 28 | func parseEncryptionKey(hexKey string) ([]byte, error) { |
| 29 | if hexKey == "" { |
| 30 | return nil, nil |
| 31 | } |
| 32 | |
| 33 | key, err := hex.DecodeString(hexKey) |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("%w: %w", ErrInvalidEncryptionKey, err) |
| 36 | } |
| 37 | |
| 38 | if len(key) != encryptionKeySize { |
| 39 | return nil, fmt.Errorf("%w: got %d bytes, want %d", ErrInvalidEncryptionKey, len(key), encryptionKeySize) |
| 40 | } |
| 41 | |
| 42 | return key, nil |
| 43 | } |
| 44 | |
| 45 | // encryptField encrypts a single Pushover field using the documented E2EE scheme. |
| 46 | // |
| 47 | // The plaintext is gzip-compressed, encrypted with AES-256-CBC and PKCS7 padding, |
| 48 | // authenticated with HMAC-SHA256 over IV||ciphertext, then Base64-encoded. |
| 49 | // |
| 50 | // Parameters: |
| 51 | // - plaintext: UTF-8 field value to encrypt. |
| 52 | // - key: 32-byte AES-256 key. |
| 53 | // |
| 54 | // Returns: |
| 55 | // - ciphertext: Base64-encoded IV||ciphertext||HMAC. |
| 56 | // - err: ErrEncryptionFailed when compression, padding, or encryption fails. |
| 57 | func encryptField(plaintext string, key []byte) (string, error) { |
| 58 | var compressed bytes.Buffer |
| 59 | |
| 60 | writer, err := gzip.NewWriterLevel(&compressed, gzip.BestCompression) |
| 61 | if err != nil { |
| 62 | return "", fmt.Errorf("%w: gzip: %w", ErrEncryptionFailed, err) |
| 63 | } |
| 64 | |
| 65 | if _, err := writer.Write([]byte(plaintext)); err != nil { |
| 66 | return "", fmt.Errorf("%w: gzip: %w", ErrEncryptionFailed, err) |
| 67 | } |
| 68 | |
| 69 | if err := writer.Close(); err != nil { |
| 70 | return "", fmt.Errorf("%w: gzip close: %w", ErrEncryptionFailed, err) |
| 71 | } |
| 72 | |
| 73 | padded, err := pkcs7Pad(compressed.Bytes(), aes.BlockSize) |
| 74 | if err != nil { |
| 75 | return "", fmt.Errorf("%w: %w", ErrEncryptionFailed, err) |
| 76 | } |
| 77 | |
| 78 | initVector := make([]byte, aes.BlockSize) |
| 79 | if _, err := rand.Read(initVector); err != nil { |
| 80 | return "", fmt.Errorf("%w: iv: %w", ErrEncryptionFailed, err) |
| 81 | } |
| 82 | |
| 83 | block, err := aes.NewCipher(key) |
| 84 | if err != nil { |
| 85 | return "", fmt.Errorf("%w: cipher: %w", ErrEncryptionFailed, err) |
| 86 | } |
| 87 | |
| 88 | ciphertext := make([]byte, len(padded)) |
| 89 | cipher.NewCBCEncrypter(block, initVector).CryptBlocks(ciphertext, padded) |
| 90 | |
| 91 | mac := hmac.New(sha256.New, key) |
| 92 | if _, err := mac.Write(initVector); err != nil { |
| 93 | return "", fmt.Errorf("%w: hmac: %w", ErrEncryptionFailed, err) |
| 94 | } |
| 95 | |
| 96 | if _, err := mac.Write(ciphertext); err != nil { |
| 97 | return "", fmt.Errorf("%w: hmac: %w", ErrEncryptionFailed, err) |
| 98 | } |
| 99 | |
| 100 | payload := make([]byte, 0, len(initVector)+len(ciphertext)+sha256.Size) |
| 101 | payload = append(payload, initVector...) |
| 102 | payload = append(payload, ciphertext...) |
| 103 | payload = append(payload, mac.Sum(nil)...) |
| 104 | |
| 105 | return base64.StdEncoding.EncodeToString(payload), nil |
| 106 | } |
| 107 | |
| 108 | // pkcs7Pad appends PKCS7 padding so data is a multiple of blockSize. |
| 109 | // |
| 110 | // Parameters: |
| 111 | // - data: bytes to pad. |
| 112 | // - blockSize: AES block size in bytes. |
| 113 | // |
| 114 | // Returns: |
| 115 | // - padded: a new slice containing data followed by PKCS7 padding. |
| 116 | // - err: ErrEncryptionFailed when blockSize is invalid. |
| 117 | func pkcs7Pad(data []byte, blockSize int) ([]byte, error) { |
| 118 | if blockSize <= 0 || blockSize > 255 { |
| 119 | return nil, fmt.Errorf("%w: invalid block size %d", ErrEncryptionFailed, blockSize) |
| 120 | } |
| 121 | |
| 122 | padLen := blockSize - (len(data) % blockSize) |
| 123 | if padLen < 1 || padLen > 255 { |
| 124 | return nil, fmt.Errorf("%w: invalid pad length %d", ErrEncryptionFailed, padLen) |
| 125 | } |
| 126 | |
| 127 | padByte := byte(padLen) |
| 128 | padded := make([]byte, len(data)+padLen) |
| 129 | copy(padded, data) |
| 130 | |
| 131 | for i := len(data); i < len(padded); i++ { |
| 132 | padded[i] = padByte |
| 133 | } |
| 134 | |
| 135 | return padded, nil |
| 136 | } |