|
| 1 | +// Copyright 2025 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package encryption |
| 15 | + |
| 16 | +import ( |
| 17 | + cerrors "github.com/pingcap/ticdc/pkg/errors" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + // EncryptionHeaderSize is the size of encryption header (4 bytes) |
| 22 | + // Format: [version(1 byte)][dataKeyID(3 bytes)] |
| 23 | + EncryptionHeaderSize = 4 |
| 24 | + |
| 25 | + // VersionUnencrypted indicates data is not encrypted |
| 26 | + VersionUnencrypted byte = 0x00 |
| 27 | +) |
| 28 | + |
| 29 | +// EncryptionHeader represents the 4-byte encryption header |
| 30 | +// Format: [version(1 byte)][dataKeyID(3 bytes)] |
| 31 | +type EncryptionHeader struct { |
| 32 | + Version byte |
| 33 | + DataKeyID [3]byte |
| 34 | +} |
| 35 | + |
| 36 | +// EncodeEncryptedData encodes data with encryption header |
| 37 | +// Format: [version(1)][dataKeyID(3)][encryptedData] |
| 38 | +// The version byte comes from the encryption metadata obtained from TiKV |
| 39 | +func EncodeEncryptedData(data []byte, version byte, dataKeyID string) ([]byte, error) { |
| 40 | + if len(dataKeyID) != 3 { |
| 41 | + return nil, cerrors.ErrInvalidDataKeyID.GenWithStackByArgs("data key ID must be 3 bytes") |
| 42 | + } |
| 43 | + |
| 44 | + if version == VersionUnencrypted { |
| 45 | + return nil, cerrors.ErrEncryptionFailed.GenWithStackByArgs("version cannot be 0 for encrypted data") |
| 46 | + } |
| 47 | + |
| 48 | + result := make([]byte, EncryptionHeaderSize+len(data)) |
| 49 | + result[0] = version |
| 50 | + copy(result[1:4], dataKeyID) |
| 51 | + copy(result[4:], data) |
| 52 | + |
| 53 | + return result, nil |
| 54 | +} |
| 55 | + |
| 56 | +// DecodeEncryptedData decodes data and extracts encryption header |
| 57 | +// Returns: (version, dataKeyID, encryptedData, error) |
| 58 | +func DecodeEncryptedData(data []byte) (byte, string, []byte, error) { |
| 59 | + if len(data) < EncryptionHeaderSize { |
| 60 | + return 0, "", nil, cerrors.ErrDecodeFailed.GenWithStackByArgs("data too short for encryption header") |
| 61 | + } |
| 62 | + |
| 63 | + version := data[0] |
| 64 | + var dataKeyID [3]byte |
| 65 | + copy(dataKeyID[:], data[1:4]) |
| 66 | + encryptedData := data[4:] |
| 67 | + |
| 68 | + return version, string(dataKeyID[:]), encryptedData, nil |
| 69 | +} |
| 70 | + |
| 71 | +// IsEncrypted checks if data is encrypted by examining the version byte |
| 72 | +// Data is considered encrypted if version != 0 (VersionUnencrypted) |
| 73 | +// The caller should validate that the version matches expected versions from TiKV metadata |
| 74 | +func IsEncrypted(data []byte) bool { |
| 75 | + if len(data) < EncryptionHeaderSize { |
| 76 | + return false |
| 77 | + } |
| 78 | + return data[0] != VersionUnencrypted |
| 79 | +} |
| 80 | + |
| 81 | +// IsEncryptedWithVersion checks if data is encrypted with a specific version |
| 82 | +// This is useful when you know the expected version from TiKV metadata |
| 83 | +func IsEncryptedWithVersion(data []byte, expectedVersion byte) bool { |
| 84 | + if len(data) < EncryptionHeaderSize { |
| 85 | + return false |
| 86 | + } |
| 87 | + return data[0] == expectedVersion |
| 88 | +} |
| 89 | + |
| 90 | +// GetVersion extracts the version byte from data |
| 91 | +// Returns 0 if data is too short |
| 92 | +func GetVersion(data []byte) byte { |
| 93 | + if len(data) < EncryptionHeaderSize { |
| 94 | + return 0 |
| 95 | + } |
| 96 | + return data[0] |
| 97 | +} |
| 98 | + |
| 99 | +// EncodeUnencryptedData encodes unencrypted data with version=0 header |
| 100 | +// This creates a unified format where all new data has the 4-byte header |
| 101 | +func EncodeUnencryptedData(data []byte) []byte { |
| 102 | + result := make([]byte, EncryptionHeaderSize+len(data)) |
| 103 | + result[0] = VersionUnencrypted |
| 104 | + // DataKeyID is zero for unencrypted data (3 bytes) |
| 105 | + result[1] = 0 |
| 106 | + result[2] = 0 |
| 107 | + result[3] = 0 |
| 108 | + copy(result[4:], data) |
| 109 | + return result |
| 110 | +} |
| 111 | + |
| 112 | +// DecodeUnencryptedData decodes unencrypted data (removes header if present) |
| 113 | +func DecodeUnencryptedData(data []byte) ([]byte, error) { |
| 114 | + if len(data) < EncryptionHeaderSize { |
| 115 | + // No header, return as-is (backward compatibility) |
| 116 | + return data, nil |
| 117 | + } |
| 118 | + |
| 119 | + version := data[0] |
| 120 | + dataKeyID1, dataKeyID2, dataKeyID3 := data[1], data[2], data[3] |
| 121 | + dataKeyIDIsZero := dataKeyID1 == 0 && dataKeyID2 == 0 && dataKeyID3 == 0 |
| 122 | + |
| 123 | + if version == VersionUnencrypted && dataKeyIDIsZero { |
| 124 | + // New-format unencrypted data with header, remove header |
| 125 | + return data[4:], nil |
| 126 | + } |
| 127 | + |
| 128 | + // For backward compatibility, treat any other format as legacy unencrypted data |
| 129 | + // This includes: |
| 130 | + // - Legacy data without header (any pattern) |
| 131 | + // - Data that might look like encrypted but is actually legacy |
| 132 | + // The caller is responsible for ensuring data is not actually encrypted |
| 133 | + return data, nil |
| 134 | +} |
| 135 | + |
| 136 | +// ExtractDataKeyID extracts the data key ID from encrypted data |
| 137 | +func ExtractDataKeyID(data []byte) (string, error) { |
| 138 | + if len(data) < EncryptionHeaderSize { |
| 139 | + return "", cerrors.ErrDecodeFailed.GenWithStackByArgs("data too short") |
| 140 | + } |
| 141 | + |
| 142 | + version := data[0] |
| 143 | + dataKeyID1, dataKeyID2, dataKeyID3 := data[1], data[2], data[3] |
| 144 | + dataKeyIDIsZero := dataKeyID1 == 0 && dataKeyID2 == 0 && dataKeyID3 == 0 |
| 145 | + |
| 146 | + // Only extract key ID from data that definitively looks like new-format encrypted: |
| 147 | + // - version != 0 (encrypted data has non-zero version) |
| 148 | + // - DataKeyID is non-zero (encrypted data always has a valid key ID) |
| 149 | + if version != VersionUnencrypted && !dataKeyIDIsZero { |
| 150 | + var keyID [3]byte |
| 151 | + copy(keyID[:], data[1:4]) |
| 152 | + return string(keyID[:]), nil |
| 153 | + } |
| 154 | + |
| 155 | + // Otherwise, this is not encrypted data (legacy data or new-format unencrypted) |
| 156 | + return "", cerrors.ErrDecodeFailed.GenWithStackByArgs("data is not encrypted") |
| 157 | +} |
0 commit comments