加密
加密
Kopia 采用标准的信封加密(envelope encryption)技术,把仓库口令与用于加密、认证仓库内容的密钥解耦。
每个仓库都有一个格式数据块,其中保存该仓库的配置参数。格式数据块本身被序列化为持久化的 JSON,如下所示。
{
"tool": "https://github.com/kopia/kopia",
"buildVersion": "v0.3.0",
"buildInfo": "unknown",
"uniqueID": "bm90IGEgZ29vZCByYW5kb20gaW5pdGlhbCB2YWx1ZQ==",
"keyAlgo": "scrypt-65536-8-1",
"version": "1",
"encryption": "AES256_GCM",
"encryptedBlockFormat": "ZSxqt/gXwVPS6pNvFZOOHlCKr18TmziuUnN8nnuvwQ/+mjbcvEHUfKS11RJl/sWrIOyiYqpSwAZt
BzOxQAUWQt7vHc2ZT4Y75ODrPHhzd0CcBlqHa3HSx8pxIXqpgMy5K3xDvIkBN1qdb/cTyU5s9lZ2
J+rBh2CGQ3phIlKFCCuS3lgB+rnYRrExGg5rm4BUmZZWHfBPQ7QiOJNg86PK+n///dejsAA/+FBj
qIODf7Gr3ppaGZeAHYJuz0BkRSPC4XIAgFj1yPo="
}
对应的 Go 结构体如下:
type formatBlob struct {
Tool string `json:"tool"`
BuildVersion string `json:"buildVersion"`
BuildInfo string `json:"buildInfo"`
UniqueID []byte `json:"uniqueID"`
KeyDerivationAlgorithm string `json:"keyAlgo"`
Version string `json:"version"`
EncryptionAlgorithm string `json:"encryption"`
EncryptedFormatBytes []byte `json:"encryptedBlockFormat,omitempty"`
UnencryptedFormat *format.RepositoryObjectFormat `json:"blockFormat,omitempty"`
}
Version标识格式数据块的版本。tool和buildInfo字段只是信息性内容。buildVersion是 Kopia 的版本,同时被间接用作仓库格式版本。UniqueID是为该仓库随机生成的标识符,也被用作多种加密操作的输入。keyAlgo标识基于口令的密钥派生函数(PBKDF)。目前只支持使用当前推荐代价参数的 scrypt(N=65536, r=8, p=1)。该字段的主要作用是便于将来更换和扩展密钥派生算法,例如提高 scrypt 的代价参数,或整体换成别的算法。encryption标识用于加密 encryptedBlockFormat 字段的加密算法。encryptedBlockFormat是一段密文,其中包含用于加密仓库内容的加密密钥和参数等信息。关于其明文内容以及加密方式,下文有更多说明。- 另外,未加密的数据块格式参数也可以直接写在
blockFormat字段中。
formatBlob.EncryptedBlockFormat 字段的内容,是对下面这个 EncryptedRepositoryConfig 结构体的 JSON 序列化结果加密后得到的。其明文版本包含数据分块参数,以及加密和认证 “content” 对象所需的参数。
type EncryptedRepositoryConfig struct {
Format RepositoryObjectFormat `json:"format"`
}
type RepositoryObjectFormat struct {
format.ContentFormat
format.ObjectFormat
}
package content
// ContentFormat describes the rules for formatting contents in repository.
type ContentFormat struct {
Version int `json:"version,omitempty"` // version number, must be "1"
Hash string `json:"hash,omitempty"` // identifier of the hash algorithm used
Encryption string `json:"encryption,omitempty"` // identifier of the encryption algorithm used
HMACSecret []byte `json:"secret,omitempty"` // HMAC secret used to generate encryption keys
MasterKey []byte `json:"masterKey,omitempty"` // master encryption key (SIV-mode encryption only)
MaxPackSize int `json:"maxPackSize,omitempty"` // maximum size of a pack object
}
package object
type Format struct {
Splitter string `json:"splitter,omitempty"` // splitter used to break objects into pieces of content
}
formatBlob.encryptedBlockFormat 中的密文是这样得到的:
- 把已填充的 encryptedRepositoryConfig 结构体序列化为 JSON。
- 用
formatBlob.encryption指定的算法加密得到的字节流。目前默认使用AES256_GCM,它同时提供加密和认证。输入参数为:- “Plaintext”(明文):序列化后的 JSON 字节流
- “IV”:随机生成,并被前置于
formatBlob.encryptedBlockFormat中的密文之前 - Key:由口令派生出的 256 位 AES 密钥,详见下文
- Additional Data(AD):用作“认证数据”,其取值同样由口令派生,详见下文
口令用于生成加密密钥和 附加数据(AD),以对 formatBlob.encryptedBlockFormat 密文进行加密和解密,具体如下:
- 主密钥(Km)由口令派生:使用 (a)
formatBlob.keyAlgo指定的基于口令的密钥派生函数,以及 (b)formatBlob.UniqueID作为盐值。得到的密钥长度为 32 字节(256 位)。Km = PBKDF( passphrase, formatBlob.UniqueID, … cost parameters)。 - AES-256 加密密钥(Ke)由 Km 派生:使用基于哈希的密钥派生函数(HKDF),哈希算法为 SHA256。
Ke = HKDF(SHA256, Km, formatBlob.UniqueID, "AES", 32) - 附加数据(AD)同样用 HKDF 派生:
AD = HKDF(SHA256, Km, formatBlob.UniqueID, "CHECKSUM", 32)
最后修改 November 29, 2026: docs(site): add zh-CN translations for 17 pages (5c87a8f)