UoN COMP3077 Cryptography 学习笔记
文章目录
- 01 Introduction
- 02 Historic Ciphers (Caesar Cipher, Shift Cipher, Affine Cipher), Modular Arithmetic
- 03 Stream Ciphers, Randomness, One-Time Pad (OTP), Modern Stream Ciphers
- 04 Linear Feedback Shift Registers (LFSRs), Trivium, ChaCha20
- 05 Block Ciphers, Pseudorandom Permutations, Feistel Networks, Data Encryption Standard (DES)
- 06 DES (cont.), Double and Triple Encryption (2DES, 3DES, DES-X), attack DES (Meet-in-the-middle (MITM)), Differential Cryptanalysis
- 07 Groups, Fields, Finite Fields (Prime Fields, Extension Fields)
- 08 Advanced Encryption Standard (AES)
- 09 Block cipher modes of operation: Padding, Deterministic vs. Probabilistic Encryption, ECB, CBC, CTR, Galois Counter Mode, Padding Oracle Attack
- 11 RSA (Key generation, encryption and decryption), Integer Factorisation Problems, Euler Totient, Fermat’s Little Theorem and Euler’s Theorem, Efficient Computation: Square and Multiply
- 12 Cyclic Group, Diffie-Hellman Key Exchange, Discrete Logarithm Problem
- 13 Elliptic Curves (Point addition, doubling and the point at infinity), Elliptic Curve Discrete Logarithm Problem
- 14 Elliptic Curve Cryptosystems, Cyclic group, Efficient Computation: Double and Add, Elliptic Curve Diffie-Hellman (ECDH)
- 15 Digital Signatures
01 Introduction
- What is cryptography

- cryptography: the science and art of writing and solving codes to hide the meaning of messages
- symmetric: Encryption methods in which both the encryption and decryption algorithms use the same key
- asymmetric: Methods which use separate, but related, private and public keys
- protocols: The application of cryptographic algorithms in secure systems
- cryptanalysis: the science and art of breaking cryptosystems
02 Historic Ciphers (Caesar Cipher, Shift Cipher, Affine Cipher), Modular Arithmetic
-
Historic cipher: usually based on transposition or substitution
-
Historic cipher - Caesar Cipher: replace each letter of plaintext with a shifted letter further down the alphabet
- a special case of the Shift Cipher (with a key k = 3)
-
Historic cipher - Shift Cipher:

- not secure: frequency analysis, brute force
- key space: 25 (i.e. integer 1 ~ 25)
-
Historic cipher - Affine Cipher:

- not secure: frequency analysis, brute force (only a little better than Shift Cipher)
- key space: (valid a) * 25
-
Modular arithmetic
- congruence
[一致的]: let , and , if - equivalence classes:

- Integer rings

- can add or multiply any two numbers in the ring and the result is in the ring. It is closed
- commutative:
, - associative:
, - neutral element
for addition: - neutral element
for multiplication: - the additive inverse
always exists - the multiplicative inverse
exists for some but not all elements - exists when
- congruence
03 Stream Ciphers, Randomness, One-Time Pad (OTP), Modern Stream Ciphers
- stream ciphers: encrypt bits one at a time


- encryption and decryption operations are identical
- Modulo 2 - XOR: balanced, i.e. if the key bit
behaves perfectly randomly that it is unpredictable and has exactly a 50% chance to have the value 0 or 1, then both possible ciphertexts also occur with a 50% likelihood - stream ciphers give confidentiality, but not integrity
- Randomness
- True randomness: impossible to recreate except by chance, e.g. coin flips
- True Random Number Generator (TRNG)
- their output cannot be reproduced
- based on physical processes, e.g. coin flipping, rolling of dice, …
- Pseudorandom Number Generator (PRNG)
- Generate a sequence of values based on a seed
- Usually the only requirement is statistical randomness
and - Linear Congruential Generator (LCG):
and , where are integer constants
- Cryptographically Secure Pseudorandom Number Generator (CSPRNG)
- PRNG which is unpredictable
- Unconditional Security, Computational Security and Perfect Secrecy
- Unconditional Security: A cryptosystem is unconditionally or information theoretically secure if it cannot be broken even with infinite computational resources
- Perfect Secrecy: The ciphertext should reveal no information about the plaintext
- Computational Security: A cryptosystem is computationally secure if the best known algorithm for breaking it requires at least t operations
- the One-Time Pad (OTP)
- A stream cipher for which
- Key stream
is generated by a TRNG - The key stream is known only to the communicating parties
- Every key stream bit
is used only once
- Key stream
- OTP has perfect secrecy: any plaintext is equally likely depending on the key
- OTP is not practical: key size is same as plaintext size; cannot reuse a key
- A stream cipher for which
- Modern stream ciphers
- use an initial seed key to generate an infinite pseudorandom keystream

- advantage to OPT: greatly reduces key size
- use a nonce value to alter the keystream for a given key
- allows to create different keystreams for a given key

- nonce - number used once
- always use a unique (key + nonce) pair
- nonces are not secret - they are a public random seed for a key stream
- Modern stream ciphers are CSPRNGs
- use an initial seed key to generate an infinite pseudorandom keystream
- Reuse a keystream in stream cipher - breaks a stream cipher
the attacker knows the XOR of 2 plaintext messages- crib dragging attack (plain text attack): if the attacker can guess or know part of a plain text message (s)he can begin to decrypt other encrypted messages by dragging a XOR of the known plain text over two or more messages

- attack a stream cipher that uses PRNG based on the linear congruential generator (LCG)
- PRNG based on LCG:
- even if the attacker knows some (very limited) plaintext, e.g.
, in addition to ciphertext (which he certainly knows), he can compute the first few bits of key stream as , then and , and can be calculated
- PRNG based on LCG:
04 Linear Feedback Shift Registers (LFSRs), Trivium, ChaCha20
-
Linear Feedback Shift Registers (LFSRs)
- A Linear-feedback Shift Register is a register of bits whose positions shift to the right
- Usually comprised of flip-flops
- the last bit represents the output
- e.g.

- mathematical representation:

- polynomial representation:

- maximum length LFSRs: LFSRs that have primitive polynomials (irreducible)
- attack LFSRs:

-
Trivium
- 3 LFSRs
- each takes feedback from previous LFSR with non-linear AND gates
- Initialises the LFSRs with an 80-bit key and 80-bit random value
-
ChaCha20
- use only add, xor and rotate operations
- inputs and outputs:

- performs 20 rounds
- Alternates between (10) Column Rounds and (10) Diagonal Rounds
- Each round is 4 quarter rounds

05 Block Ciphers, Pseudorandom Permutations, Feistel Networks, Data Encryption Standard (DES)
-
Block ciphers: encrypt whole blocks at a time

- Block size varies, often 64 or 128-bit
-
Pseudorandom Permutations
- A pseudorandom permutation is a function that cannot be distinguished from a random permutation
- Maps a set of values
such that: - For any key, the function
is a bijection - There is an efficient algorithm to calculate F(x) for all keys and all messages
- For any key, the function
-
Confusion & Diffusion
- Confusion: Obscure the relationship between plaintext, key and ciphertext
- often achieved through substitution operations - lookup tables
- Diffusion: Influence of each plaintext and key bit is distributed throughout the ciphertext
- often achieved via permutation - swapping or otherwise mixing bits or bytes
- Confusion: Obscure the relationship between plaintext, key and ciphertext
-
product cipher - combines a sequence of simple transformations such as substitution (S-box), permutation (P-box), and modular arithmetic
- SP-networks

- SP-networks
-
Feistel Networks: one mechanism used to create block ciphers
-

-
During each round, only half of the block is encrypted
-
- pseudorandom function -
Encryption & Decryption:
- Encryption:

- Decryption:

- Encryption:
-
1 or 2 rounds is not sufficient
-
if
is a cryptographically secure pseudorandom function, then: - 3 rounds are sufficient to make a pseudorandom permutation
- 4 rounds are sufficient to make a strong pseudorandom permutation
-
-
the Data Encryption Standard (DES)
-
64-bit block size 56-bit key 16 rounds
-

-
/ - Facilitates loading of registers L and R in hardware
- Adds nothing to security
-
the
function -

-
- adds diffusion - Increases from 32 to 48 bits to match the round key
- Half of input bits are connected to two output positions

-
Substitution boxes (S-box) - adds confusion

- The S boxes map 6-bit inputs to 4-bit outputs
- There are 8 S-boxes in total, each is different

- S-box Design
-
S-boxes need to be highly non linear :
- This prevents simple systems of linear equations such as we saw in LFSRs
-
Key design principles:
- No output bit should be too close to a linear combination of input bits
- 1 bit change input should lead to at least 2 bits output
- If only the 4 middle bits change, each output must occur exactly once
- If the first two bits are different but the last two are identical, the output must differ
- For any non-zero difference in input, no more than 8 /32 inputs exhibiting this difference should share the same output difference
- A collision (zero difference) is only possible for 3 adjacent S-boxes
-
-
- moves bits between S-boxes on the next round
-
-
the avalanche effect: 1 bit changes on the input, in 1 round there will be at least 2 bits changed, 2 rounds - at least 4 bits, …
-
06 DES (cont.), Double and Triple Encryption (2DES, 3DES, DES-X), attack DES (Meet-in-the-middle (MITM)), Differential Cryptanalysis
- the Data Encryption Standard (DES) (cont.)
- Key Schedule

- Permuted Choice 1 - selects 56 out of the 64 bits - Key bits 8, 16, 24, … 64 are not used
- Left rotations
- each 28-bit block is rotated left by <<<1 for rounds {1, 2, 9, 16} and <<<2 otherwise
- The total rotation is
, which means and
- select 48 of the 56 bits to be used as a round key
- properties of the key schedule
- entirely permutation based
and - can be used for systems with almost no memory at all, when decryption can just go from back to
- Key Schedule
- Breaking DES
- brute force -
attempts - key collisions - multiple keys encrypting the same
pair - likelihood of key collision for an
bit key and bit block cipher: - for DES the probability that multiple keys work for a single
pair is
- likelihood of key collision for an
- brute force -
- Double Encryption
- Breaking Double Encryption DES
- naïve brute force -
attempts (impossible) - meet-in-the-middle (MITM)

attempts - trade off computation for storage - storage requirement is
- assumes some kind of
lookup for
- naïve brute force -
- 3DES

- either ‘enc -> enc -> enc’ or ‘enc -> dec -> enc’
- ‘enc -> dec -> enc’ can be used in legacy systems to be compatible with 1DES (if
, it becomes an 1DES)
- ‘enc -> dec -> enc’ can be used in legacy systems to be compatible with 1DES (if
- Often used in banking, smart cards and other payment systems
- MITM -
attempts (impossible) with infeasible storage requirements
- DES-X
- key whitening

- theoretically: search space of
, but meet-in-the-middle and other attacks are available
- cryptanalysis
- In modern cryptography, a cipher is declared broken by essentially any attack that is more efficient that brute force
- types of cryptanalysis
- Analytical Attacks - Exploit some underlying structural or mathematical weakness in a cipher
- E.g. meet-in-the-middle attack
- Derivation of taps in LFSRs
- Statistical Attacks - Capture statistical patterns between input and output to recover key bits
- Differential Cryptanalysis
- Linear Cryptanalysis
- Differential Cryptanalysis - a chosen plaintext attack
- aim to find predictable changes in output bits caused by known changes in input bits
- some input change resulting in some output change
is called a differential, and has some probability of occurring - tracing and calculating differential characteristics
- resisting differential cryptanalysis
- S-boxes must be designed such that the probability of any pair
is as low as possible - AES has a maximum likelihood of a differential per s=box of
- AES has a maximum likelihood of a differential per s=box of
- More rounds make differentials even less likely
- Good permutation to involve more S-boxes
- DES was specifically designed to resist this kind of attack
- S-boxes must be designed such that the probability of any pair
07 Groups, Fields, Finite Fields (Prime Fields, Extension Fields)
-
Groups
- note that the element
here does not mean integer 1, but a symbol
- note that the element
- example group: the set of integers
with the operation addition modulo form a group with the neutral element
-
Algebraic structures of Groups, Rings (see notes on Lecture 02) and Fields

- Fields are an extension of Groups and related to Rings
-
Fields

- example field: the set of real numbers
with neutral element for addition and for multiplication
-
Finite Fields (Galois Fields, GFs)
-
a field with a finite number of elements
-
add, subtract, multiply, invert (divide)
-

- e.g. a field with 11 elements
, a field with 256 elements or - e.g.
or is not a finite field
- e.g. a field with 11 elements
-
Prime and Extension fields
-
Prime Fields
- a prime field
contains the integers - arithmetic in prime fields: Let
- all members of a prime field have a multiplicative inverse
- for prime fields,
- for prime fields,
- a prime field
-
Extension Fields
- an extension field
contains polynomials of degree with coefficients in where - e.g.
- arithmetic in extension fields:
- add / subtract
- e.g.
, ,
- e.g.
- multiply: must reduce the intermediate result modulo an (pre-defined) irreducible polynomial
- e.g.
- e.g.
- invert:
- add / subtract
- an extension field
-
-
AES’s Finite Field
- extension field
or - irreducible polynomial used:
- extension field
08 Advanced Encryption Standard (AES)
-
an SP-Network with a 128-bit block size, a key length of 128, 192 or 256-bits, 10, 12 or 14 rounds
-

- the rounds operate on a state of 16 bytes (128 bits)
- the key expansion produces number of (rounds + 1) keys
- all rounds are identical, apart from the last round
-
SubBytes (confusion): replaces each byte of the input block with another byte according to a pre-defined S-box
- S-box: the multiplicative inverse of 8 bit values in
(strongly non-linear mapping, bijective, an invertible 1:1 mapping) + affine transformation (destroys remaining mathematical structure) - no fixed points
- no inverse fixed points (i.e. no
for which ) - minimise the number of fixed difference changes for specific changes in input - reduced the likelihood of differentials and helps resist differential cryptanalysis attacks
- S-box: the multiplicative inverse of 8 bit values in
-
ShiftRows (diffusion): shifts the rows of the input block cyclically to the left. The first row is not shifted, the second row is shifted one byte to the left, the third row is shifted two bytes to the left, and the fourth row is shifted three bytes to the left
-
MixColumns (diffusion): a matrix multiplication operation that operates on each column of the shifted block
- All the input bytes in a column influence all the output bytes

- All the input bytes in a column influence all the output bytes
-
AddKey: adds the key
-
Key Schedule
-
Implementation
- very fast in software and pretty fast in hardware
- There are numerous cache timing and other attacks possible - In general AES is much harder to implement safely than ChaCha20
09 Block cipher modes of operation: Padding, Deterministic vs. Probabilistic Encryption, ECB, CBC, CTR, Galois Counter Mode, Padding Oracle Attack
- Block Cipher Modes
- Most messages don’t come in convenient 128 bit block lengths - Padding
- We’ll need to run a block cipher repeatedly on consecutive blocks - ECB, CBC, CTR
- Padding: make message length to be a multiple of the block size - Public Key Cryptography Standards PKCS7 is a common padding scheme
- Padding bytes are always added to the plaintext before it is encrypted
- Each padding byte has a value equal to the total number of padding bytes that are added
- The total number of padding bytes is at least one (i.e. if a message has an exact multiple of the block cipher length, a entire block of padding bytes is added)
- Deterministic vs. Probabilistic Encryption
- An encryption scheme is deterministic if some plaintext is mapped to a fixed ciphertext if the key is unchanged
- Probabilistic encryption schemes add randomness to the encryption process to achieve a non deterministic generation of the ciphertext
- Electronic Code Book (ECB) (deterministic) - just encrypt each block one after another

- DO NOT USE: ECB allows an attacker to infer information on the plaintext - divulges whenever messages or blocks are the same
- for the same key, same plaintext results in same cyphertext
- Cipher Block Chaining (CBC) (probabilistic) - XOR the output of each cipher block with the next input

- Decryption is similar, but the XOR now takes place after decryption - Easier to parallelise
- The IV ensures that the encryption is probabilistic - that each encryption of any message is different
- weakness: Altering ciphertext bits can lead to attacks - Padding Oracle Arracks
- an oracle is a system we can query and it will tell us if, once decrypted, some text has (in)valid padding
- manipulate bits in the IV to find valid padding (from …01, to … 02 02, to … 03 03 03, …) and recover
, and then use to recover plaintext 
- Counter Mode (CTR) - Encrypt a nonce + counter and use this to mask the plaintext with XOR

- This is very easily parallelised
- Each block is encrypted differently, avoiding the issues with ECB mode
- it is constructed like a stream cipher
- weakness: attackers can make predictable changes
- Galois Counter Mode - Extends counter mode to add authenticity & integrity
- Very similar to counter mode, but adds an authentication tag
- Extremely parallelisable
- Robust to message alteration
11 RSA (Key generation, encryption and decryption), Integer Factorisation Problems, Euler Totient, Fermat’s Little Theorem and Euler’s Theorem, Efficient Computation: Square and Multiply
-
keys are usually 2048 or 4096 bits
-
Security is built around the difficulty of factoring large numbers
-
Encryption performed by the public key can only be reversed using the private key
-
The authenticity of signatures generated by the private key can be verified by the public key
-
Integer Factorisation: Any integer can be expressed as the multiplication of a list of prime numbers
-
Euler Totient Function
-
integers
and are relatively prime if they do not share a divisor except 1, i.e. -
The Euler totient
is the number of integers in for which -
e.g.
-
calculating
- e.g.
=>
-
for prime numbers:
-
for semiprimes
:
-
-
Fermat’s Little Theorem: for some prime
, and any integer : -
Euler’s theorem:
- works for any integer ring
- A generalization of Fermat’s little theorem
- works for any integer ring
-
RSA Key Generation

, are public, , , , are private - Typically the public key
is smaller (in bits) than the private key
-
RSA Encryption / Decryption
- encryption:
- decryption:
- it can be proved that
(see slides / textbook)
- encryption:
-
why RSA is secure
-
The security of RSA relies on the fact that it is difficult to derive the private key
from the public key - there is no known efficient algorithm for factoring large prime numbers -
for an attacker:
-
the attacker would like to get
<= <= the attacker would like to get <= without knowing and it would be hard enough to calculate factors of , i.e.
-
-
Efficient Computation of Exponentiation: square and multiply

- the computational complexity of exponentiation: for a
bit key, complexity is around - brute force: for a
bit key, complexity is
-
65537: a very useful public exponent for RSA
- the prime is
, the largest know number of the form , in binary is
- the prime is
12 Cyclic Group, Diffie-Hellman Key Exchange, Discrete Logarithm Problem
- Two parties can jointly agree a shared secret over an insecure channel
- the multiplicative group

- e.g.
- In the majority of cases, we use a prime number
as the modulus: - The cardinality (or order) of a group is the number of elements in that group
- In cryptography the order of a group is extremely important for security

- e.g.
- Cyclic Group: a group that contains an element
of maximum order - Any element of maximum order is called a primitive root, or generator
- e.g. 2 is a primitive root / generator of
because - e.g. 3 is not a primitive root / generator of
because but - e.g.
is a cyclic group because it has an element of maximum order
- cyclic subgroups: a subset of a group that is generated by a generator
- e.g. 4 subgroups for
: (generated by 1), (generated by 10), (generated by 3 4 5 9, (generated by 2 6 7 8)
- e.g. 4 subgroups for
- Diffie-Hellman
- steps
- Alice and Bob agree on a large prime
, and a generator that is a primitive root of - Alice and Bob choose private numbers
and at random in - Alice calculates
and sends it publicly to Bob - Bob calculates
and sends it publicly to Alice - Alice computes
- Bob computes
- Alice and Bob agree on a large prime

- shared secret:
- The Discrete Logarithm Problem - makes Diffie-Hellman hard to break
- hard enough to calculate
in (in reality, the values are much larger than in this example, making it even harder)
- hard enough to calculate
- attack: e.g. Pohlig-Hellman is based on the prime factorisation of
- To avoid any unexpected small subgroup attacks (e.g. Pohlig-Hellman attack), commonly used DH primes are safe primes
- A safe prime is a prime
where is also a prime - This will have three subgroups of order
, and - By choosing a generator of the subgroup of large prime order, we avoid attacks on small factors of the group order
- A safe prime is a prime
- steps
13 Elliptic Curves (Point addition, doubling and the point at infinity), Elliptic Curve Discrete Logarithm Problem
- elliptic curve for cryptography:
- points on the curve forms a cyclic group with point addition operation
- closed, associative, neutral element, inverses, commutative (abelian)
- point addition (note: all graphs in this chapter is the elliptic curve over
. To use it in cryptography a should be applied to the points on the curve) 
- point doubling: add a point to itself

- note:
is not i.e. multiplied by . It is just a way to say - point addition equations:

- neutral element: the point
at infinity - The point at infinity is the neutral element on an elliptic curve
- In practice the point doesn’t have coordinates, and can’t be used within the normal formula
- in point addition, when the
values are equal and values are inverses , the result will be the point at infinity => i.e. - e.g.
because and
- e.g.
- points on the curve forms a cyclic group with point addition operation
- Cyclic groups
- The points on an elliptic curve including the neutral element
form cyclic subgroups - Under certain conditions all points form a cyclic group

- The points on an elliptic curve including the neutral element
- Elliptic Curve Discrete Logarithm Problem
- give a curve
, a primitive root , and a point , it is hard enough to calculate
- give a curve
14 Elliptic Curve Cryptosystems, Cyclic group, Efficient Computation: Double and Add, Elliptic Curve Diffie-Hellman (ECDH)
- Cyclic groups in elliptic curve cryptosystems: Given a generator point, points on elliptic curves generate cyclic groups
- each cyclic group includes the point at infinity
- e.g.

- since
any multiples of that add to 19 will be inverses
- each cyclic group includes the point at infinity
- Group cardinality
- The size of cyclic groups is very important to the security
- A large
is very important to prevent various attacks on ECDLP
- A large
- a curve have
points (ideally, if all points on the curve are valid in the cyclic group) - Hasse’s theorem: for a curve
over a field , the number of elements is bounded by where - do not generate own elliptic curves - The vast majority of applications use standard curves
- The size of cyclic groups is very important to the security
- Elliptic Curve Discrete Logarithm Problem (ECDLP) in elliptic curve cryptosystems: private keys such as
are integers, generators and public keys are points - generic algorithms like Pohlig-Hellman requires
steps
- generic algorithms like Pohlig-Hellman requires
- Efficient Computation to calculate
: double and add - e.g.

- e.g.
- Elliptic Curve Diffie-Hellman (ECDH)

- shared secret:
- Elliptic Curve Cryptosystem structure:

- Implementation of Elliptic Curve Diffie-Hellman
- point compression: in practice, we do not need to transport full
coordinates - each point contains a unique
and one of two (positive / negative) where - Most implementations will use the full
value, and append a single bit representing a positive or negative value
- each point contains a unique
- projective coordinates: some implementations adjust the formula for point addition to use projective coordinates
rather than - the curve sits on the plane
- point at infinity
- the curve sits on the plane
- standard curves: The choice of curve parameters influences both security and efficiency of cryptosystems based around ECs
- no small subgroups, efficient point addition and doubling, known order (
) of the curve
- no small subgroups, efficient point addition and doubling, known order (
- point compression: in practice, we do not need to transport full
15 Digital Signatures
- digital signature that uses a symmetric key: provides authenticity and integrity, but not non-repudiation






