In your last assignment, you investigated the field \(\mbox{GF}(2^3) = \mathbb{Z}_2/(1+x+x^3)\).
Identify the 3-digit bit string \(b_1b_2b_3\) with the polynomial \(b_1x^2+b_2x+b_3\). That is, \[ \begin{aligned} 000 &\leftrightarrow 0 \\ 001 &\leftrightarrow 1 \\ 010 &\leftrightarrow x \\ 011 &\leftrightarrow x+1 \\ 100 &\leftrightarrow x^2 \\ 101 & \leftrightarrow x^2+1 \\ 110 & \leftrightarrow x^2+x \\ 111 & \leftrightarrow x^2+x+1 \end{aligned} \]
Define an encryption function \(E:\mbox{GF}(2^3) \longrightarrow \mbox{GF}(2^3)\) on 3-bit blocks by \(E(p(x)) = xp(x)\).
Encrypt the block \(110\) using this cipher.
What is the decryption function?
If \(\gcd(a,n)=1\), then there are \(c,d\) such that \(ca + nd = 1\).
Therefore \(ca \equiv 1\) modulo \(n\), so \(c\) is the inverse of \(a\).
We can compute \(c, d\) using the Extended Euclidean Algorithm (fast).
Implement addition and multiplication in \(\mbox{GF}(2^8) = \mathbb{Z}_2[x]/(x^8+x^4+x^3+x+1)\). Polynomials are represented as length 8 vectors of 0s and 1s.
What you must know:
Bytes are represented as elements of \(\mbox{GF}(2^8) = \mathbb{Z}_2[x]/(x^8+x^4+x^3+x+1)\).
AddRoundKey: \(M \leftarrow M \oplus K_i\)
ByteSub: \(M \leftarrow \sigma(M)\), where \(\sigma\) is a function defined by taking inverses in \(GF(2^8)\) and multiplying by a fixed matrix over \(\mathbb{Z}_2\)
ShiftRow: \(M \leftarrow s(M)\), where \(s\) shifts the rows left by \(0,1,2,3\).
MixColumn: \(M \leftarrow TM\), where \(T = \begin{bmatrix} x & x+1 & 1 & 1 \\ 1 & x & x+1 & 1 \\ 1 & 1 & x & x+1 \\ x+1 & 1 & 1 & x \end{bmatrix}\).
digest
Package [1] 4f 6e 65 42 6c 6f 63 6b 49 73 31 32 38 42 69 74
rawKey <- charToRaw("128or192or256bit")
myAESobject <- AES(rawKey, mode="ECB") # we'll talk about modes in a minute
myAESobject$encrypt(rawPtxt)
[1] 3e ed 83 e0 01 79 9d f8 5c 93 81 10 be ae ff 33
[1] 4f 6e 65 42 6c 6f 63 6b 49 73 31 32 38 42 69 74
plaintext <- "howeverinthisextremedistresshewasnotdestituteofhisusualsagacitybuttrustinghimselftotheprovidenceofgodheputhislifeintohazardinthemannerfollowingandnowsaidhesinceitisresolvedamongyouthatyouwilldiecomeonletuscommitourmutualdeathstodeterminationbylothewhomthelotfallstofirstlethimbekilledbyhimthathaththesecondlotandthusfortuneshallmakeitsprogressthroughusallnorshallanyofusperishbyhisownrighthandforitwouldbeunfairifwhentherestaregonesomebodyshouldrepentandsavehimselfthisproposalappearedtothemtobeveryjustandwhenhehadprevailedwiththemtodeterminethismatterbylotshedrewoneofthelotsforhimselfalsohewhohadthefirstlotlaidhisneckbaretohimthathadthenextassupposingthatthegeneralwoulddieamongthemimmediatelyfortheythoughtdeathifjosephusmightbutdiewiththemwassweeterthanlifeyetwashewithanotherlefttothelastwhetherwemustsayithappenedsobychanceorwhetherbytheprovidenceofgodandashewasverydesirousneithertobecondemnedbythelotnorifhehadbeenlefttothelasttoimbruehisrighthandinthebloodofhiscountrymenhepersuadedhimtotrusthisfidelitytohimandtoliveaswellashims"
p1 <- hist(letterCounts(plaintext), plot=FALSE)
p2 <- hist(table(as.numeric(myAESobject$encrypt(charToRaw(plaintext)))), plot=FALSE)
plot( p1, col=rgb(0,0,1,0.3), main="blue=plaintext, pink=AES ciphertext",
xlab="letter counts", ylab="# of letters", ylim=c(0,55)) # transparent blue
plot( p2, col=rgb(1,0,0,0.3), add=TRUE) # transparent red
Drawback of block ciphers: An adversary could collect a codebook of ciphertext blocks.
Key: \(K\)
Encryption function: \(E_K\)
Plaintext: \(P = P_1, P_2, \ldots, P_L\)
Ciphertext: \(C = C_1, C_2, \ldots, C_L\)
In ECB mode, \(C_i = E_K(P_i)\) for \(i = 1, 2, \ldots, L\).
Encryption: \(C_j = E_K(P_j\oplus C_{j-1})\)
Decryption: \(P_j = D_K(C_j) \oplus C_{j-1}\)
Note: \(C_0\) is the initial vector IV. It should be chosen at random, but can be sent unencrypted.
\(O_j = L_8(E_k(X_j))\), \(\quad C_j = P_j \oplus O_j\), \(\quad X_{j+1} = R_{b-8}(X_j),C_j\), where \(b\) is the block size.
Encryption: \(O_j = L_8(E_k(X_j))\), \(\quad C_j = P_j \oplus O_j\), \(\quad X_{j+1} = R_{b-8}(X_j),C_j\)
Decryption: \(P_j = C_j \oplus L_8(E_K(X_j))\), \(\quad X_{j+1}=R_{b-8}(X_j),C_j\)
Things to notice:
\(O_j = L_8(E_K(X_j))\), \(\quad X_{j+1} = R_{b-8}(X_j),O_j\), \(\quad C_j = P_j \oplus O_j\)
Encryption: \(O_j = L_8(E_K(X_j))\), \(\quad X_{j+1} = R_{b-8}(X_j),O_j\), \(\quad C_j = P_j \oplus O_j\)
Decryption: \(O_j = L_8(E_K(X_j))\), \(\quad X_{j+1} = R_{b-8}(X_j),O_j\), \(\quad P_j = C_j \oplus O_j\)
Like LFSR/OTP:
Like CFB:
Unlike CFB:
\(X_j = X_{j-1}+1\), \(\quad O_j = L_8(E_K(X_j))\), \(\quad C_j = P_j \oplus O_j\)
Encryption: \(X_j = X_{j-1}+1\), \(\quad O_j = L_8(E_K(X_j))\), \(\quad C_j = P_j \oplus O_j\)
Decryption: \(X_j = X_{j-1}+1\), \(\quad O_j = L_8(E_K(X_j))\), \(\quad O_j = C_j \oplus O_j\)
Like LFSR/OTP, OFB:
Like CFB, OFB:
Like OFB:
Unlike OFB:
Use the field \(\mbox{GF}(2^3) = \mathbb{Z}_2/(1+x+x^3)\). Define an encryption function \(E:\mbox{GF}(2^3) \longrightarrow \mbox{GF}(2^3)\) on 3-bit blocks by \(E(p(x)) = xp(x)\).
Encrypt the 3-block ciphertext \(110011101\) using this cipher in CBC mode with initial vector \(111\).
Encrypt the 3-block ciphertext \(110011101\) using this cipher in CTR mode with initial vector \(010\).