# Chapter 6 – Cryptography

## Basics
The **goals of cryptography** are confidentiality, integrity, authentication and nonrepudiation.

For **integrity** you can use message digests (hashes), or more advanced,   digital signatures (encrypted message digests).

**Authentication** can be provided by a challenge-response technique: the receiver of a message sends a challenge back to the sender (“encrypt this text with our secret key”). If the original receiver can then decode the encrypted text back to the original, he/she knows the sender uses their shared secret key.

**Nonrepudiation** is provided by public key or asymmetric crypto systems.

The **Kerckhoffs Principle** states that a cryptographic system is most secure if everything about it, except the key, is public knowledge.

Cryptographic keys are sometimes called *cryptovariables*.

A **nonce** (number only used once) is a random number added during an encryption process to make it harder to crack.

**Zero-Knowledge Proof** is proving your knowledge of a secret fact to another party without disclosing the fact. 

**Split Knowledge** is when the information, or privilege required to perform an operation, is divided between multiple users.
*M of N Control* requires that minimum M out of N agents need to work together to perform high-security tasks.

The **work function** expresses the time and effort required to perform a brute-force attack successfully. It is a measure for the strength of a cryptographic system. It should match the value of the protected asset.

## Boolean mathematics
AND operation
Output is true if ~both~ inputs are true.
X ⋀ Y

OR operation
Output is true if ~at least one~ of the inputs is true.
X ⋁ Y

NOT operation
Reverses the value of the input
∼X or !X

XOR
Output is true if ~only one~ of the inputs is true.
X⨁Y

Modulo operation
Remainder left after a division
8 mod 6 = 2
6 mod 8 = 6

## Ciphers
**Substitution ciphers** replace each character of the plaintext message with another. The Caesar Cipher is a substitution cipher. It shifts each letter a number of places, e.g. A becomes C, F becomes I, etc.
Mono-alphabetic Substitution ciphers are vulnerable to an attack called *frequency analysis*.
Poly-alphabetic substitution ciphers, like Vigenère, protect against direct frequency analysis, but are vulnerable to *period analysis* – a second-order frequency analysis.

**One-time pad** substitution ciphers are very powerful because they use a different alphabet for each substitution. C = (P+K) mod 26. Also known as Vernam ciphers. 

Requirements of the one-time pad:
* the pad must be randomly generated
* the pad must be kept secret
* each pad can only be used once
* the key must be at least as long as the message itself.

**Transposition ciphers** rearrange the letters of a plaintext message. They are an example of **Block ciphers**, that operate on blocks of the message.

The **running key cipher** is also known as the **book cipher**.

**Stream ciphers** operate on one character at a time.

Cryptographic algorithms rely on confusion and diffusion. **Confusion** is when the relationship between plaintext and key is so complex, that altering the plaintext and analysing the results won’t help in finding the key.
**Diffusion** occurs when a change in the plaintext result in multiple changes throughout the cipher text.

## Symmetric vs Asymmetric key algorithms 
In Symmetric cryptography, encryption and decryption happens with the same key, which must be known by sender and receiver. It’s also called secret key cryptography or private key cryptography.

Asymmetric cryptography works with individual combinations of public and private keys. It is also known as public key cryptography.

## Symmetric key algorithms 
Rely on a single shared secret. It’s fast but not strong. 

Weaknesses are:
* key distribution (keeping it secret while communicating it to the intended receiver)
* do not implement nonrepudiation
* not scalable – each sender-receiver pair needs a unique key; N=n(n-1)/2
* need for frequent regeneration of keys – if people leave a group all remaining members need new keys

## Asymmetric key algorithms 
Each user has a private and a public key. The combination of a user’s public-private keys can decrypt the message.

The sender uses the public key of the receiver to encrypt the message, the receiver decrypts it with her private key.

Advantages are:
* see page 218
* Can provide integrity, authentication and nonrepudiation. 

Because of the slowness of public key cryptography, many applications use PKI to establish a secure connection, and then exchange a symmetric key for the body of the transmission. 

## Symmetric Cryptography 
### Data Encryption Standard (DES)
DES is a 64-bit block cipher with a 56 bit key. (The specification calls for a 64 bit key but 8 of those are dor parity information). 

DES operates in several modes:
* Electronic Code Book Mode (ECB) encrypts the block with the key. A repeated plaintext block will result in a repeated cipher text block, making it easier to decipher. In practice only used for small amounts of data. 
* In Cipher Block Chaining Mode (CBC), each block is XORed with the preceding block of cipher text, before it is encrypted. The first block of text is XORed with an Initialization Vector, which must be shared with the recipient. An important consideration is that CBC mode errors propagate throughout the text. 
* Cipher Feedback Mode (CFB) is the streaming version of CBC. The input stream is buffered in memory until the block size is reached. 
* Output Feedback Mode (OFB) is like CFB but chains a seed value. The advantage is that errors do not propagate. 
* Counter Mode (CTR) uses a simple counter instead of a seed value. This allows you to break up the processing in independent steps, making it suited for parallel computing. 

TripleDES or 3DES uses multiple passes, with different key combinations, see p. 222. It has an effective key length of 112 bits. 

Other symmetric cryptographic algorithms are:
* IDEA
* Blowfish
* Skipjack
* Rivest Cipher 5 or RC5
* AES
* Twofish 

Diffie-Hellman was specifically designed for exchanging secret keys. 



