kubernetes/kubernetes · error
size of data is less than the nonce
Error message
size of data is less than the nonce
What it means
Returned by DecryptBytes when len(data) < gcm.NonceSize(). The AES-GCM ciphertext produced by EncryptBytes is structured as nonce||ciphertext; if the input is shorter than the nonce prefix it cannot contain a valid nonce and is rejected before any decryption attempt.
Source
Thrown at cmd/kubeadm/app/util/crypto/crypto.go:67
return nil, err
}
return gcm.Seal(nonce, nonce, data, nil), nil
}
// DecryptBytes takes a byte slice of encrypted data and an encryption key and returns a decrypted byte slice of data.
// The key must be an AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256
func DecryptBytes(data, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(data) < nonceSize {
return nil, errors.New("size of data is less than the nonce")
}
nonce, out := data[:nonceSize], data[nonceSize:]
out, err = gcm.Open(nil, nonce, out, nil)
if err != nil {
return nil, err
}
return out, nil
}
View on GitHub (pinned to b882c60b40)
Solutions
- Ensure the data passed is the complete output of EncryptBytes (nonce + ciphertext), unmodified.
- Verify the source file/blob is not truncated; re-copy or regenerate the encrypted payload.
- Confirm the AES key length is 16, 24, or 32 bytes matching the encryption key used.
- If the payload was base64-encoded, decode fully before calling DecryptBytes.
Example fix
// before
plain, err := crypto.DecryptBytes(partialBytes, key) // len < nonceSize
// after
full, err := os.ReadFile(fullEncryptedPath)
if err != nil { return err }
plain, err := crypto.DecryptBytes(full, key) Defensive patterns
Strategy: validation
Validate before calling
// AES-GCM nonce is 12 bytes; ciphertext must exceed that.
if len(data) < 12 {
return nil, fmt.Errorf("ciphertext too short (%d bytes); expected nonce||ciphertext from EncryptBytes", len(data))
} Try / catch
plain, err := crypto.DecryptBytes(data, key)
if err != nil {
return fmt.Errorf("decryption failed (verify key length 16/24/32 and payload integrity): %w", err)
} Prevention
- Persist the full EncryptBytes output untouched (nonce prefix included).
- Verify key length is 16/24/32 bytes matching encryption.
- Base64-decode fully and confirm byte length before DecryptBytes.
When it happens
Trigger: Calling crypto.DecryptBytes with truncated, empty, or corrupt ciphertext, or passing data that was not produced by EncryptBytes. Also reached if the wrong AES key size leads to a larger nonce expectation than the data provides.
Common situations: Corrupted/shortened encrypted secret blob (e.g. a copied key/cert fragment). Reading from a truncated file. Passing plaintext or a base64-decoded value that is too short. Mismatched key (16/24/32 bytes) yielding unexpected GCM nonce sizing.
Related errors
- shell not specified
- too many arguments. expected only the shell type
- the --old-config flag is mandatory
- can't use upload-certs with an external CA or an external fr
- cannot use --certificate-key without --print-join-command
AI-assisted analysis of kubernetes/kubernetes@b882c60b40 (2026-08-07).
Data as JSON: /api/errors/622d8f9224d8c96e.
Report an issue: GitHub.