$39.99
Submission Procedure as detailed on the assignments course webpage http://people.ucalgary.ca/~rscheidl/crypto/assignments.html.
Assignments that don’t follow these instructions will incur penalties of varying degree, up to a score of zero.
Problems 1-5 are worth 63 marks and are required for both CPSC 418 and MATH 318 students.
The bonus credit policy can also be found under the link above.
Written Problems for CPSC 418 and MATH 318
Problem 1 — Arithmetic in the AES MixColumns operation (20 marks)
Recall that the MixColumns operation in AES performs arithmetic on 4-byte vectors using the polynomial M(y) = y4 + 1. In this arithmetic, we have M(y) = 0, so y4 = 1.
a. In this part of the problem, we consider multiplication of 4-byte vectors (viewed as polynomials of degree ≤ 3 whose coefficients are bytes) by powers of y.
(i) (2 marks) Formally prove that in this arithmetic, multiplication of any 4-byte vector by y is a circular left shift of the vector by one byte.
(ii) (4 marks) Generalizing part (i) to other powers of y, formally prove that multiplication of any 4-byte vector by yi (0 ≤ i ≤ 3) is a circular left shift of the vector by i bytes.
b. Next, we consider arithmetic involving the coefficients of the polynomial
c(y) = (03)y3 + (01)y2 + (01)y + (02) ,
that appears in MixColumns, where the coefficients of c(y) are bytes written in hexadecimal (i.e. base 16) notation. Arithmetic involving this polynomial requires the computation of products involving the bytes (01), (02) and (03) in the Rijndahl field GF(28). Recall that in this field, arithmetic is done modulo m(x) = x8 + x4 + x3 + x + 1.
(i) (2 marks) Write the bytes (01), (02), (03) as their respective polynomial representatives c1(x), c2(x) and c3(x) in the Rijndahl field GF(28).
(ii) (4 marks) Let b = (b7 b6 ···b1 b0) be any byte, and let d = (02)b be the product of the bytes (02) and b in the Rijndahl field GF(28). Then d is again a byte of the form d = (d7 d6 ···d1 d0). Provide formulas for the bits di, 0 ≤ i ≤ 7, in terms of the bits bi.
(iii) (3 marks) Provide analogous expressions as in part (b) (ii) for the byte product e = (03)b, where b = (b7 b6 ···b1 b0) is any byte.
c. The MixColumns operation performs multiplication of 4-byte vectors by the polynomial c(y) of part (b). In this part of the problem, you will evaluate such products symbolically.
(i) (3 marks) Let s(y) = s3y3 +s2y2 +s1y +s0 be a polynomial whose coefficients are bytes. Symbolically compute the product t(y) = s(y)c(y) mod y4 + 1. The result should be a polynomial of the form t(y) = t3y3 + t2y2 + t1y + t0 where t3,t2,t1,t0 are bytes. Provide formulas for the bytes ti, 0 ≤ i ≤ 3, in terms of the bytes si. The equations should consist of sums of byte products of the form 01si, 02si, 03si, 0 ≤ i ≤ 3. You need not compute these individual byte products as you did in part (b).
(ii) (2 marks) Write your solution of part (c) (i) in matrix form; i.e. give a 4 × 4 matrix C whose entries are bytes such that
.
Note that this yields the matrix representation of MixColumns presented (without proof) in class.
Problem 2 — Error propagation in block cipher modes (12 marks)
Error propagation is often an important consideration when choosing a mode of operation in practice. In this problem, you will analyze the error propagation properties of an arbitrary block cipher in various such modes; note that these properties are independent of the cipher used.
a. Suppose Alice wants to send a sequence of message blocks M0,M1,M2,... to Bob, encrypted to ciphertext blocks C0,C1,C2,... using some fixed key K. Assume that an error occurs during transmission of a particular block of ciphertext Ci. Justifying all your answers, explain which plaintext blocks are affected after Bob decrypts this (faulty) ciphertext block Ci when the cipher is used in
(i) (2 marks) ECB mode?
(ii) (2 marks) CBC mode? (iii) (2 marks) OFB mode?
(iv) (2 marks) CFB mode with one register?
(v) (2 marks) CTR mode?
b. (2 marks) Suppose now that an error occurs in a particular plaintext block Mi before Alice encrypts it and sends the corresponding ciphertext Ci to Bob. Upon decryption of Ci, which plaintext blocks Mj are affected when using the cipher in CBC mode?
Problem 3 — Binary exponentiation (13 marks)
Recall the exponentiation algorithm given in class for evaluating an (mod m) (a ∈ Z, m,n ∈ N):
1. Compute the binary representation of n:
n = b02k + b12k−1 + ··· + bk−12 + bk ,
with b0 = 1, bi ∈ {0,1} for 1 ≤ i ≤ k, and k = blog2 nc.
2. Initialize r0 ≡ a (mod m).
) if bi+1 = 0 ,
3. For 0 ≤ i ≤ k − 1 compute
) if bi+1 = 1 .
4. Output rk.
a. (3 marks) To warm up with a toy example, compute 1711 (mod 77) using the procedure above; answers that don’t use the binary exponentiation algorithm will receive no credit, even if they are correct. Show all your work, and write down all your intermediate quantities bi and ri. Your answer should be an integer between 0 and 76.
b. In this problem, you will formally prove that the binary exponentiation algorithm is correct.
(i) (4 marks) Define s0 = 1 and si+1 = 2si + bi+1 for 0 ≤ i ≤ k − 1. Use induction on i to prove that
i si = Xbj2i−j for 0 ≤ i ≤ k .
j=0
(ii) (4 marks) Let ri, 0 ≤ i ≤ k, be defined as in steps 2 and 3 of the exponentiation algorithm. Use induction on i to prove that ri ≡ asi (mod m) for 0 ≤ i ≤ k.
(iii) (2 marks) Prove that an ≡ rk (mod m), so the algorithm above does indeed compute an (mod m) as claimed.
Problem 4 — A modified man-in-the-middle attack on Diffie-Hellman (10 marks)
Suppose Alice and Bob wish to generate a shared cryptographic key using the Diffie-Hellman protocol. As usual, they agree on a large prime p and a primitive root g of p. Suppose also that p = mq + 1 where q is prime and m is very small (so p − 1 = mq has a large prime factor, as is generally required). Since g and p are public, it is easy for anyone to deduce m and q; for example by successively trial-dividing p − 1 by m = 2,4,6,... and running a primality test such as the Fermat test on the quotient q = (p − 1)/m until primality of q is established.
Suppose an active attacker Mallory intercepts ga (mod p) from Alice and gb (mod p) from Bob. She sends (ga)q (mod p) to Bob and (gb)q (mod p) to Alice.
a. (2 marks) Show that Alice and Bob compute the same shared key K under this attack.
b. (4 marks) Show that there are m possible values for K, and that Mallory can compute them all and hence easily guess the correct key K among them.
c. (4 marks) What is the advantage of this variation of the man-in-the-middle attack over the version we discussed in class? Recall that for the attack from class, Mallory simply suppresses the messages ga (mod p) and gb (mod p) between Alice and Bob and replaces them with her own number ge (mod p), which results in the shared key gae (mod p) between Mallory and Alice and the shared key gbe (mod p) between Mallory and Bob.
Problem 5 — A simplified password-based key agreement protocol (8 marks)
The following is a simplified (and hence problematic) version of the key generation phase of the password-based key agreement protocol that you are being asked to implement in Problem 9 (the programming problem). Here, a client first performs a one-time registration of their authentication credentials with a server. These credentials can then be used to generate authenticated session keys between server and client via communication over an insecure channel.
All participants agree on a large public prime N = 2q + 1, with q prime, and a public primitive root g of N. Each client has their own password p. To register with the server, a client computes v ≡ gp (mod N) and provides the server with the pair (I,v) where I is the client’s user id.
Protocol:
1. Client generates a random value a with 0 ≤ a ≤ N − 1, computes A ≡ ga (mod N), and sends (I,A) to server, where I is the Client’s user id.
Server generates a random value b with 0 ≤ b ≤ N − 1, computes B ≡ gb (mod N), and sends B to client.
2. Client computes Kclient ≡ Ba+p (mod N).
Server retrieves client’s authentication data (I,v) and computes Kserver ≡ (Av)b (mod N).
Note that this protocol is similar to Diffie-Hellman, except that the client’s password p and authentication credential v are incorporated in the key computation.
a. [2 marks] Prove that Client and Server have a shared key after executing this protocol, i.e. prove that Kserver = Kclient.
b. [3 marks] Suppose an adversary Mallory obtains client Ian’s authentication data (I,v) (by intercepting Ian’s transmission to the server upon his registration or by hacking into the server’s database). Show how Mallory can masquerade as Ian, i.e. execute the protocol with the server (using a value A of her choice) and generate a valid key Kclient that the server believes is shared with Ian.
c. [3 marks] Consider the following two problems:
Key Recovery Problem: Given any values A ≡ ga (mod N) and B ≡ gb (mod N) and any , find a key K produced by the protocol above.
Diffie-Hellman Problem: Given any values A ≡ ga (mod N) and B ≡ gb (mod N), find a Diffie-Hellman key K ≡ gab (mod N).
Note that the exponents a and b are assumed to be unknown for both these problems. Show how an attacker Mallory who can solve any instance of the key recovery problem can solve any instance of the Diffie-Hellman problem. (So informally, breaking the key agreement protocol above is at least as hard as breaking Diffie-Hellman.)
Written Problems for MATH 318 only
Problem 6 — Discrete logs with respect to different primitive roots (6 marks)
Let p be a prime and g a primitive root of p. Recall that for any , the discrete logarithm of a with respect to g is unique integer x with 0 ≤ x ≤ p − 2 and gx ≡ a (mod p).
Recall that the discrete problem is asserted to be computationally intractable. This raises the natural question of whether this problem might be easier to solve for some primitive roots than for others. In this problem you will prove that that the difficulty of the discrete logarithm problem is independent of the choice of primitive root.
Specifically, let g,h be primitive roots of p and assume that for any element in , computing its discrete logarithm with respect to g is easy. Give an algorithm for easily computing its discrete logarithm with respect to h.
Problem 7 — Primitive roots for safe primes (6 marks)
Let q ≥ 3 be a prime such that p = 2q+1 is also prime. Let g be any primitive root of p. Prove that with the exception of gq (mod p), all the odd powers of g (i.e. g,g3 (mod p),g5 (mod p),...,gp−2 (mod p)), are primitive roots of p.
Problem 8 — An algorithm for extracting discrete logarithms (25 marks)
Let p be a large prime and g a fixed primitive root of p. Let be the modular inverse of g, so gh ≡ 1 (mod p). Let . Define the following lists of elements in :
yi ≡ ahi (mod p), 0 ≤ i ≤ m − 1; zj ≡ (gm)j (mod p), 0 ≤ j ≤ k − 1.
Here, m is a positive integer (an as yet unspecified parameter) and k is the smallest integer with k ≥ (p − 1)/m, so k ≥ (p − 1)/m > k − 1.
a. (4 marks) Prove that there exist indices i,j with 0 ≤ i ≤ m − 1 and 0 ≤ j ≤ k − 1 such that yi = zj.
(Hint: Division with remainder of x by m where x is the (unknown) discrete logarithm of a with respect to g.)
b. (5 marks) Consider the following procedure for computing the discrete logarithm of a with respect to g.
1. Compute the list Y = (y0,y1,...,ym−1)
2. If yi ≡ 1 (mod p) for some i ∈ {0,1,...,m − 1}, then output i and quit.
3. Compute the list Z = (z0,z1,z2,...,) until an element zj is found that appears in Y.
4. Upon finding such a match, say zj = yi, output x ≡ jm + i (mod p − 1).
Prove that this procedure terminates and outputs the discrete logarithm of a.
d. (5 marks) How should m be chosen to minimize the number of modular multiplication determined in part (c)? Explain your choice.
√
(Hint: Your answer should be a function of p that’s close to p.)
e. Let p = 107.
(i) (2 marks) Use the primitive root test from class to verify that 2 is a primitive root of p. Show your work.
Programming Problem for CPSC 418 only
Problem 9 — Secure password based authentication and key exchange (37 marks)
Overview. This problem considers the full, secure version of the password-based key agreement protocol introduced in Problem 5. This protocol, executed by a Client and a Server, allows the Client to demonstrate to the Server knowledge of a password, but neither the password nor any other information that could be used to derive the password need to be transmitted. Additionally, the Server does not store password-equivalent data, so someone who intercepts authentication data or steals them from the Server’s database is unable to masquerade as the Client without bruteforcing the Client’s password.
To execute the protocol, there is an initialization and registration phase between the Client and Server. Doing this securely is in itself a complex problem, and so we instead perform a simplified version as described below.
The Server initializes in the following manner:
1. Generates a 512-bit safe prime N by first generating a random 511-bit prime q and checking whether N = 2q + 1 is prime. Repeat until a valid N is found.
3. Computes the quantity k = H(N||g), where ‘||’ denotes concatenation and H is a cryptographically secure hash function.
4. Opens a socket connection on a specified IP and port and attempts to receive a single byte, whose value indicates the Server response. This byte is limited to the UTF-8 encoding of the characters ‘r’ indicating Client wishes to initiate registration; ‘p’ indicating the Client wishes to initiate the protocol; and ‘q’ indicating the Client wishes the Server to shut down.
The Server performs registration as follows:
1. Server sends the values N,g to the Client.
2. Server receives a single byte containing the length of the Client’s username, followed by username,s,v. Server stores these values.
3. Server resumes listening on the socket, for another command.
The Client performs registration as follows:
1. The username username and a password pw are retrieved from the command line.
2. Generates a random 16-byte salt s.
3. Connect to the Server’s socket and sends a one-byte character ‘r’;
4. Receives the values N,g;
5. Computes x = H(s||pw) and v ≡ gx (mod N);
6. Computes and sends a single byte containing the byte-length of username, followed by username encoded in UTF-8, s, and v.
In a similar manner, if a Client connects to the Server and wishes to initiate the protocol, they will first send a single byte corresponding to ‘p’ before commencing with the protocol described below.
Protocol.
To generate and verify a shared authenticated session key, the Server and Client perform the following steps:
1. Client generates a random value a with 0 ≤ a ≤ N − 1, computes A ≡ ga (mod N), and sends the length of the username as a single byte, followed by username,A.
Server generates a random value b with 0 ≤ b ≤ N − 1, computes B ≡ kv + gb (mod N), and sends s,B, where s is the Client’s salt.
2. Client and Server compute u ≡ H(A||B) (mod N).
3. Client computes Kclient ≡ (B − kv)a+ux (mod N).
Server computes Kserver ≡ (Avu)b (mod N).
4. Client computes and sends M1 = H(A||B||Kclient).
5. Server computes H(A||B||Kserver) and compares the result to M1. If they do not match, the authentication has failed and the socket is closed.
6. Server computes and sends M2 = H(A||M1||Kserver).
7. Client computes H(A||M1||Kclient) and compares the result to M2. If they do not match, the authentication has failed.
8. The Server closes the socket and waits for further connections.
Steps 1-3 generate the authenticated key shared between the Client and the Server. Steps 4-7 verify that both parties have computed the same shared key. If executed honestly, Kclient and Kserver are equal and the Server and Client were able to authenticate each other and establish a shared session key.
At the end of either registration or the protocol, the Server should resume listening on the socket.
Please note the importance of the order that values are sent over the socket.
Problem. Your task is to complete the template program named
basic auth.py
which performs the above password-based key agreement protocol. The program consists of functions corresponding to establishing a connection and transmitting data through a socket, parameter generation, and the actions performed by the Client and the Server.
All messages over the socket should be echoed to standard output by both the sending and receiving party. Each echoed message should clearly indicate its sender and intended receiver. Use a template like the following:
Server: N = (integer value of N)
Client: Received <(hex representation of incoming data)> Server: Authentication was successful.
Client: ERROR, the socket was closed.
Specifications. Fill in the empty functions in the template program basic auth.py. Once complete, you should be able to perform both registration and key agreement between two parties, one acting as the Client and the other as Server, by running
python3 basicauth.py --server 127.0.4.18:3180 python3 basicauth.py --client 127.0.4.18:3180 python3 basicauth.py --quit 127.0.4.18:3180
In detail, the functions you’ll need to fill in fall into roughly three categories:
a. Networking Functions:
(i) create socket(...), which creates a socket connection on the specified IP and port.
(ii) send(...), which sends bytes through the specified socket.
(iii) receive(...), which receives a specified bytes through the socket.
b. Low-level Functions:
(i) safe prime(...), which creates a random safe prime N of a specified bit-length and whose most significant bit is 1.
(ii) prim root(...), which finds a primitive root g of N for some prime N.
(iii) calc x(...), which computes the value x from the registration step using the salt and password.
(iv) calc u(...), which computes the value u using the provided inputs.
(v) calc A(...), which computes the value A using the provided inputs.
(vi) calc B(...), which computes the value B using the provided inputs.
(vii) calc K client(...), which computes the value Kclient.
(viii) calc K server(...), which computes the value Kserver.
(ix) calc M1(...), which computes the value M1. (x) calc M2(...), which computes the value M2.
c. High-level Functions:
(i) clientregister(...), which sends and receives information to and from the Server as described in the Client registration.
(ii) serverregister(...), which receives and sends information from and to the Client through the socket connection, as outlined in the Server registration.
(iii) clientprotocol(...), which performs the Client’s side of the protocol.
(iv) serverprotocol(...), which performs the Server’s side of the protocol.
(v) clientprepare(...), which computes the Client’s registration tuple (username,s,v). (vi) serverprepare(...), which computes the Server’s registration values N,g,k.
Submission. Submit a completed version of the template program with filename
basic auth.py
If you’ve spread your code across multiple source files, submit all of them.
Provide a description of your implementation in a separate README file in text format. Do not include the written portion of the programming problem in the PDF file containing your solutions to the written problems. Your description should include the following:
The procedures you used for generating your prime N and your primitive root g of N. A list of the files you have submitted that pertain to the problem, and a short description of each file.
A list of what is implemented in the event that you are submitting a partial solution, or a statement that the problem is solved in full.
A list of what is not implemented in the event that you are submitting a partial solution.
A list of known bugs, or a statement that there are no known bugs.