Files
HackBrowserData/internal/browser/chromium/cipher.go
T
Roger 0ed920f846 docs: trim code comments to the why (#676)
Comments that restated names or narrated the code are gone, and multi-line ones are cut to a line of why.
Only comments change: each edited Go file has the same token stream as before. winapi and abe come later.
2026-11-02 00:03:03 +08:00

80 lines
1.8 KiB
Go

package chromium
import (
"bytes"
"crypto/aes"
"crypto/sha1"
"errors"
"github.com/moond4rk/hackbrowserdata/internal/crypto"
)
type cipherVersion string
const (
cipherV10 cipherVersion = "v10"
cipherV11 cipherVersion = "v11"
cipherV20 cipherVersion = "v20"
// Recognized only to report a known gap: v12 is not implemented yet.
cipherV12 cipherVersion = "v12"
cipherDPAPI cipherVersion = "dpapi"
versionPrefixLen = 3
)
func detectVersion(ciphertext []byte) cipherVersion {
if len(ciphertext) < versionPrefixLen {
return cipherDPAPI
}
prefix := string(ciphertext[:versionPrefixLen])
switch prefix {
case "v10":
return cipherV10
case "v11":
return cipherV11
case "v12":
return cipherV12
case "v20":
return cipherV20
default:
return cipherDPAPI
}
}
var errShortCiphertext = errors.New("ciphertext too short")
const gcmNonceSize = 12
var chromiumCBCIV = bytes.Repeat([]byte{0x20}, aes.BlockSize)
// Chromium's decrypt-only fallback for data a KWallet race corrupted in Chrome ~89 (crbug.com/40055416).
var kEmptyKey = crypto.PBKDF2Key([]byte(""), []byte("saltysalt"), 1, 16, sha1.New)
func decryptGCM(key, ciphertext []byte) ([]byte, error) {
if len(ciphertext) < versionPrefixLen+gcmNonceSize {
return nil, errShortCiphertext
}
nonce := ciphertext[versionPrefixLen : versionPrefixLen+gcmNonceSize]
payload := ciphertext[versionPrefixLen+gcmNonceSize:]
return crypto.AESGCMDecrypt(key, nonce, payload)
}
func decryptCBC(key, ciphertext []byte) ([]byte, error) {
if len(ciphertext) < versionPrefixLen+aes.BlockSize {
return nil, errShortCiphertext
}
payload := ciphertext[versionPrefixLen:]
plaintext, err := crypto.AESCBCDecrypt(key, chromiumCBCIV, payload)
if err == nil {
return plaintext, nil
}
if alt, altErr := crypto.AESCBCDecrypt(kEmptyKey, chromiumCBCIV, payload); altErr == nil {
return alt, nil
}
return nil, err
}