Cryptography

Cryptography is the study of techniques for secure communication in the presence of adversarial behavior. In particular, cryptography is concerned with various aspects in computer security such as

Two settings

We usually consider two participants, Alice and Bob. In this course, we will mainly deal with two settings.

Cryptographic tools we will study

Roughly speaking, depending on the goal and setting, we will need a different cryptographic tool. The table below summarizes this.
private key public key
data confidentiality private key encryption public key encryption
data integerity message authentication code digital signature

Private Key Encryption

A private-key encryption scheme is defined by specifying the following:

Correctness

The scheme is said to provide correct decryption if for any key \( K \in {\cal K} \) and any message \( M \in {\cal M} \), it holds that
\( {\sf Dec}_K ( {\sf Enc}_K ( M ) ) = M \).

Threat model

Situational attack capabilities

Security of encryption needs to specify what kind of adversary (or the capability of the adversary) it considers.

Computational power of the adversary

Security notion: Indistinguishability

Conceptually, we want each ciphertext to reveal nothing about the underlying plaintext. How to mathematically model this concept is a bit tricky but interesting. Here's the rough idea:
  1. Give the adversary time to inspect the encryption scheme.
  2. Let the adversary choose two plaintexts that will be most easily crackable from his perspective.
  3. Pick one of the two messages at random and encrypt it.
  4. Give the resulting ciphertext to the adversary.
  5. Have the adversary guess which message has been encrypted.
  6. The ciphertext reveals nothing about the plaintext, the adversary can be successfully only by random guessing, which is exactly probability 1/2.

    To capture this (relaxing it only slightly), if the probability that the adversary makes the good guess is 1/2 + negligible, then the encryption scheme is called indistinguishable. Here, negligible is something like \(\frac{1}{2^{80}}\)

We say that the encryption scheme is indistinguishable if for every possible adversary, the probability that the adversary makes a good guess in the above experiment is 1/2 + negligible. If the probability is exactly 1/2 for all possible adversary, then we call the scheme perfectly indistinguishable.

Warm-up about XOR

What we learned

What about \( a \oplus a \) = ? (Drag the mouse for the answer below)
It is 0. To see this, we have
 - 0 xor 0 = 0
 - 1 xor 1 = 0

One-Time Pad Encryption

One-bit OTP

The one-time pad encryption scheme for encryption a single bit is defined as follows.
  • \( {\sf Gen}() \): Choose one bit at random. The key \(K\) is the chosen bit.
  • \( {\sf Enc}_K(M) \): Given a one-bit key \( K \), and a one-bit message \( M \), the encryption algorithm outputs \( C = M \oplus K \).

    That is, depending on the key, the ciphertext is the flip of the message or the message itself.

  • \( {\sf Dec}_K(C) \): Given a key \( K \), and a ciphertext \(C\), the decryption algorithm outputs \( M = C \oplus K \).

    That is, depending on the key, the decryption is performed by flipping the ciphertext or just returning the ciphertext.

    Note: \[ {\sf Dec}_K( {\sf Enc}_K (M) ) = {\sf Dec}_K( M \oplus K ) = M \oplus K \oplus K = M \oplus 0 = M \]

The one-bit OTP is shown in the table (drag your mouse for the answer).

M=0 M=1
K=0 0 1
K=1 1 0

One-bit OTP is perfectly indistinguishable under a passive attack

Any smart adversary would start with choosing 0 and 1 as the two messages. (A dumb adversary would choose the same bit, say 0 and 0 for the two messages, and it will have no additional information to smartly guess what \(b\) is.)

For example, suppose that the ciphertext \(C\) that adversary is given is 0.

  1. Is it possible that the message is 0?
    Yes, if K is 0. 
    
  2. Is it possible that the message is 1?
    Yes, if K is 1. 
    
  3. OK, both are possible scenarios. How likely is it that K is 0? How likely is it that K is 1?
    We know that K is randomly chosen. Therefore, Pr[K=0] = Pr[K=1] = 1/2. 
    
  4. In summary, the uncertainty of the key is transferred to the uncertainty of the message. That is, keys being equally likely means that the messages are equally likely. This is an information theoretic argument, and this one-bit OTP is perfectly indistinguishable.

Extending the one-bit OTP to a multi-bit scenario

Fix an integer \( \ell \). The message space \( \cal M \) and key space \( \cal K \) are all equal to \( \{0,1\}^\ell \).

\( \{0,1\}^\ell \) is the set of all \(\ell\)-bits. For example \[ \{0,1\}^2 = \{00, 01, 10, 11\} \]
Note that in this scheme, only a single message is ever encrypted under one key.

Example: OTP when \( \ell = 2 \)

Drag the mouse for answers.

M=00 M=01 M=10 M=11
K=00 00 01 10 11
K=01 01 00 11 10
K=10 10 11 00 01
K=11 11 10 01 00
Theorem

The one-time pad encryption scheme is perfectly indistinguishable under a passive attack.

Limitations of OTP

Choosing Random Bytes in Python

You can use Cryptodome package in Python to choose random bytes. The package can be installed as follows:
sudo apt-get install build-essential python3-dev
pip3 install pycryptodomex --user
Now, see the sample run.

The input to function get_random_bytes specifies how many bytes you need. In the above, each time the function returned 10 random bytes.


>>> from Cryptodome.Random import get_random_bytes
>>> get_random_bytes(10)
b'\xb4\x81\x03\xdf\xe5\xe9\xb9\x852\xd0'
>>> get_random_bytes(10)
b'\xa3\xe3ph\xb4\xf2\x14)\xbcF'
>>> get_random_bytes(10)
b"a\xaa\x14\xe9\xe05\xf2'T\x1b"