# Deterministic nonces in CTR mode

I want to encrypt a file with AES in CTR mode. I have a 256 bit master key and the file. Given these, the encryption must be deterministic, so I can't use a random nonce in the usual way. Fortunately the master key will be unique¹.

My original plan was to simply set the nonce to 0. Assuming no collision happens when deriving the 128 bit AES key from the master key, this should as secure as conventional CTR, where the nonce is prepended to the message.

An alternative plan is to also derive the nonce from the master-key. This seems to offer two advantages:

1. It makes key-nonce pair reuse more unlikely, since now they have 256 bits and not just 128
2. It prevents some kinds of known-plaintext attacks, since the attacker now doesn't know the content of the counter-stream, effectively turning the nonce into some kind of secondary key.

Is there a problem with either scheme? Is the second scheme better than the first?

¹ assuming the 256 bit hashfunction I use is collision free

-

– CodesInChaos Apr 16 '12 at 12:54

## 1 Answer

Assuming that you can indeed guarantee that the keys will never be reused, both schemes should be secure.

The only requirement for the nonce in CTR mode is that it must be unique (and, if used directly as the initial counter value, not equal to any intermediate counter value used in the past or in the future). If you're only encrypting one message with a given key, the nonce $0$ is as unique as any other.

As you correctly note, your second scheme provides somewhat less information to an attacker who can guess some of the plaintext. (Reading between the lines in your question, I'm assuming you're not planning to store the nonce along with the ciphertext, but to re-derive it from the master key on decryption.) Whether it's "better" is hard to say — it only makes a difference if the cipher you're using is broken, and at that point it will depend on just how it's broken — but it's at least unlikely to be worse.

-
The AES key will be as unique as random 128 bit numbers are. It's derived from the hash of the plaintext file, optionally combined with a password. – CodesInChaos Apr 16 '12 at 10:16
Pardon for asking, but how are you going to get the hash of the plain text file before you have decrypted the cipher text? It's obviously not a good idea to append a deterministic hash of the plain text to the cipher text, since it will trivially reveal information about the plain text. – Henrick Hellström Apr 16 '12 at 10:41
@HenrickHellström You need to get that hash for an outside source, typically it's part of the download link. This is a scheme similar to freenet's CHK or tahoe-lafs immutable files. – CodesInChaos Apr 16 '12 at 11:57
@HenrickHellström How does a hash trivially reveal information about the preimage? (For a hash with "good" properties such as SHA256) – MartinSuecia Apr 16 '12 at 12:00

1
@MartinSuecia: Well, for instance the hash will not change if the plain text doesn't change, so the adversary will know if the file at the other end has changed or not. This is a piece of information that a properly used confidentiality mode will hide (with the exception of information about the maximum length of the plain text). – Henrick Hellström Apr 16 '12 at 12:05
show 2 more comments
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9196478128433228, "perplexity_flag": "middle"}