mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-26 13:43:55 +03:00
Merge pull request 'feat: harden keying implementation' (#6368) from gusted/forgejo-harden-keying into forgejo
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6368 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
This commit is contained in:
commit
7b5932738e
1 changed files with 9 additions and 6 deletions
|
@ -28,13 +28,16 @@ var (
|
|||
// The hash used for HKDF.
|
||||
hash = sha256.New
|
||||
// The AEAD used for encryption/decryption.
|
||||
aead = chacha20poly1305.NewX
|
||||
aeadKeySize = chacha20poly1305.KeySize
|
||||
aeadNonceSize = chacha20poly1305.NonceSizeX
|
||||
aead = chacha20poly1305.NewX
|
||||
// The pseudorandom key generated by HKDF-Extract.
|
||||
prk []byte
|
||||
)
|
||||
|
||||
const (
|
||||
aeadKeySize = chacha20poly1305.KeySize
|
||||
aeadNonceSize = chacha20poly1305.NonceSizeX
|
||||
)
|
||||
|
||||
// Set the main IKM for this module.
|
||||
func Init(ikm []byte) {
|
||||
// Salt is intentionally left empty, it's not useful to Forgejo's use case.
|
||||
|
@ -55,7 +58,7 @@ var (
|
|||
// Derive *the* key for a given context, this is a deterministic function.
|
||||
// The same key will be provided for the same context.
|
||||
func DeriveKey(context Context) *Key {
|
||||
if len(prk) == 0 {
|
||||
if len(prk) != sha256.Size {
|
||||
panic("keying: not initialized")
|
||||
}
|
||||
|
||||
|
@ -63,7 +66,7 @@ func DeriveKey(context Context) *Key {
|
|||
|
||||
key := make([]byte, aeadKeySize)
|
||||
// This should never return an error, but if it does, panic.
|
||||
if _, err := r.Read(key); err != nil {
|
||||
if n, err := r.Read(key); err != nil || n != aeadKeySize {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
@ -92,7 +95,7 @@ func (k *Key) Encrypt(plaintext, additionalData []byte) []byte {
|
|||
|
||||
// Generate a random nonce.
|
||||
nonce := make([]byte, aeadNonceSize)
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
if n, err := rand.Read(nonce); err != nil || n != aeadNonceSize {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue