Debian Codegraph @

crt/s2n/scram/aes_scram.py

raw symbols SCIP
1 # SCRAM mode Python script
2 import sys
3 import hmac
4 import hashlib
5 from Crypto.Cipher import AES
6 from Crypto import Random
7 rndfile = Random.new()
8
9 DEBUG_ENABLED = True
10
11 # When reading/writing byte strings, the first (aka left-most) byte is the Most Significant Byte (aka Big-Endian)
12 # (Eg "0x0001", 0x00 is the MSB and 0x01 is the LSB, meaning 0x0001 == 1)
13 ENDIANNESS = 'big'
14
15
16 def byteStr(val, numBytes):
17 return val.to_bytes(numBytes, ENDIANNESS)
18
19
20 def debugByteStr(debugStr, byteStrVal):
21 if DEBUG_ENABLED:
22 print(debugStr + ": 0x" + byteStrVal.hex().upper())
23
24
25 def debugInt(debugStr, intVal):
26 if DEBUG_ENABLED:
27 print(debugStr + ": " + str(intVal))
28
29
30 def scram_generate_key():
31 # Generate Random 32 Byte Key
32 K = rndfile.read(32)
33 debugByteStr("K", K)
34
35 return K
36
37
38 def scram_encrypt(K, N, A, M, F):
39 """
40 SCRAM Encryption
41
42 Parameters:
43 K: Key
44 N: Nonce
45 A: Additional Authenticated Data
46 M: Plaintext Message
47 F: Frame Size
48
49 Returns:
50 C: Ciphertext
51 X: Excrypted R and Padding Len
52 Tag: Authentication Tag
53 """
54 # Generate a random 32-byte value R
55 R = rndfile.read(32)
56
57 # Prepare the Padding. We append 0x00 bytes to the end up to the next frame size.
58 M_LEN = len(M)
59 PADDING_LEN = 0
60
61 if (F > 0):
62 PADDING_LEN = (F - M_LEN) % F
63
64 PADDING_STR = byteStr(0x0, PADDING_LEN)
65 PADDING_LEN_STR = byteStr(PADDING_LEN, 2)
66 PADDED_MSG = M + PADDING_STR
67
68 debugInt("len(M)", M_LEN)
69 debugInt("PADDING_LEN", PADDING_LEN)
70 debugByteStr("PADDING_STR", PADDING_STR)
71 debugByteStr("PADDING_LEN_STR", PADDING_LEN_STR)
72 debugByteStr("PADDED_MSG", PADDED_MSG)
73
74 # Derive Message encryption key (KE)
75 # S1 = N || 0x00 0x00 0x00 0x1 || 0^{8} || 0^{8} || 0^{16} || R
76 S1 = N + byteStr(0x01, 4) + byteStr(0x0, 8) + byteStr(0x0, 8) + byteStr(0x0, 16) + R
77 U1 = hmac.new(K, S1, hashlib.sha512).digest()
78 KE = U1[0:32]
79
80 # AES_CTR encrypt PADDED_MSG with Nonce N and Key KE
81 C = AES.new(key=KE, mode=AES.MODE_CTR, nonce=N).encrypt(PADDED_MSG)
82
83 # Derive MAC Key (KM) used to with GMAC to generate T
84 # S2 = N || 0x00 0x00 0x00 0x2 || 0^{8} || 0^{8} || 0^{16} || 0^{32}
85 S2 = N + byteStr(0x02, 4) + byteStr(0x0, 8) + byteStr(0x0, 8) + byteStr(0x0, 16) + byteStr(0x0, 32)
86 U2 = hmac.new(K, S2, hashlib.sha512).digest()
87 KM = U2[0:32]
88
89 # GMAC the string A || C , using the GMAC key KM and nonce N
90 T = AES.new(key=KM, mode=AES.MODE_GCM, nonce=N).update(A + C).digest()
91
92 # Derive a one-time pad (U3) from T
93 # S3 = N || 0x00 0x00 0x00 0x3 || 0^{8} || 0^{8} || T || 0^{32}
94 S3 = N + byteStr(0x03, 4) + byteStr(0x0, 8) + byteStr(0x0, 8) + T + byteStr(0x0, 32)
95 U3 = hmac.new(K, S3, hashlib.sha512).digest()
96
97 # Encrypt R and PaddingLen with one-time pad U3
98 Y1 = bytes(a ^ b for (a, b) in zip(U3[0:32], R))
99 Y0 = bytes(a ^ b for (a, b) in zip(U3[32:34], PADDING_LEN_STR))
100 X = Y1 + Y0
101
102 # Authenticate (Tag) T and R
103 # S4 = N || 0x00 0x00 0x00 0x4 || A_LEN_STR || M_LEN_STR || T || R
104 S4 = N + byteStr(0x04, 4) + byteStr(len(A), 8) + byteStr(M_LEN, 8) + T + R
105 U4 = hmac.new(K, S4, hashlib.sha512).digest()
106
107 # Truncate to 16 bytes tag
108 Tag = U4[0:16]
109
110 debugByteStr("S1", S1)
111 debugByteStr("S2", S2)
112 debugByteStr("S3", S3)
113 debugByteStr("S4", S4)
114 debugByteStr("U1", U1)
115 debugByteStr("U2", U2)
116 debugByteStr("U3", U3)
117 debugByteStr("U4", U4)
118 debugByteStr("Y0", Y0)
119 debugByteStr("Y1", Y1)
120 debugByteStr("T", T)
121 debugByteStr("KE", KE)
122 debugByteStr("KM", KM)
123 debugInt("len(C)", len(C))
124 debugByteStr("C", C)
125 debugByteStr("X", X)
126 debugByteStr("Tag", Tag)
127
128 return C, X, Tag
129
130
131 def scram_decrypt(K, N, A, C, X, Tag):
132 """
133 SCRAM Decryption
134
135 Parameters:
136 K: Key
137 N: Nonce
138 A: Additional Authenticated Data
139 C: Ciphertext
140 X: Encrypted Random value R and Padding Length
141 Tag: Tag
142
143 Returns:
144 M_calculated: The decrypted Message
145 """
146
147 # Derive MAC key (KM)
148 # S2 = N || 0x00 0x00 0x00 0x2 || 0^{8} || 0^{8} || 0^{16} || 0^{32}
149 S2_calculated = N + byteStr(0x02, 4) + byteStr(0x0, 8) + byteStr(0x0, 8) + byteStr(0x0, 16) + byteStr(0x0, 32)
150 U2_calculated = hmac.new(K, S2_calculated, hashlib.sha512).digest()
151 KM_calculated = U2_calculated[0:32]
152
153 # Derive T
154 # T = GMAC (N, A||C, null)
155 T_calculated = AES.new(key=KM_calculated, mode=AES.MODE_GCM, nonce=N).update(A + C).digest()
156
157 # Derive one-time pad U3 from T_calculated,
158 # S3 = N || 0x00 0x00 0x00 0x3 || 0^{8} || 0^{8} || T || 0^{32}
159 S3_calculated = N + byteStr(0x03, 4) + byteStr(0x0, 8) + byteStr(0x0, 8) + T_calculated + byteStr(0x0, 32)
160 U3_calculated = hmac.new(K, S3_calculated, hashlib.sha512).digest()
161
162 # Decrypt R and PADDING_LEN, by xor'ing X and U3
163 R_calculated = bytes(a ^ b for (a, b) in zip(U3_calculated[0:32], X[0:32]))
164 PADDING_LEN_STR_calculated = bytes(a ^ b for (a, b) in zip(U3_calculated[32:34], X[32:34]))
165
166 # Derive Message and Padding Lengths
167 PADDING_LEN_calculated = int.from_bytes(PADDING_LEN_STR_calculated, ENDIANNESS)
168 M_LEN_calculated = len(C) - PADDING_LEN_calculated
169
170 # Authenticate R
171 # S4 = N || 0x00 0x00 0x00 0x4 || A_LEN_STR || M_LEN_STR || T || R
172 S4_calculated = N + byteStr(0x04, 4) + byteStr(len(A), 8) + \
173 byteStr(M_LEN_calculated, 8) + T_calculated + R_calculated
174 U4_calculated = hmac.new(K, S4_calculated, hashlib.sha512).digest()
175 Tag_calculated = U4_calculated[0:16]
176
177 if (Tag == Tag_calculated):
178 print("PASSED: Authentication")
179 else:
180 print("FAILED: Authentication")
181 return None
182
183 # Now that Ciphertext and other parameters are authenticated, we can decrypt Ciphertext to get Plaintext
184 # Derive Message Encryption key (KE)
185 # S1 = N || 0x00 0x00 0x00 0x1 || 0^{8} || 0^{8} || 0^{16} || R
186 S1_calculated = N + byteStr(0x01, 4) + byteStr(0x0, 8) + byteStr(0x0, 8) + byteStr(0x0, 16) + R_calculated
187 U1_calculated = hmac.new(K, S1_calculated, hashlib.sha512).digest()
188 KE_calculated = U1_calculated[0:32]
189
190 # Decrypt Ciphertext
191 PADDED_MSG_calculated = AES.new(key=KE_calculated, mode=AES.MODE_CTR, nonce=N).decrypt(C)
192
193 # Strip off padding bytes
194 M_calculated = PADDED_MSG_calculated[0:M_LEN_calculated]
195
196 if DEBUG_ENABLED:
197 print("\nDecryption Debug Info: ")
198 debugByteStr("S1_calculated", S1_calculated)
199 debugByteStr("S2_calculated", S2_calculated)
200 debugByteStr("S3_calculated", S3_calculated)
201 debugByteStr("S4_calculated", S4_calculated)
202 debugByteStr("U1_calculated", U1_calculated)
203 debugByteStr("U2_calculated", U2_calculated)
204 debugByteStr("U3_calculated", U3_calculated)
205 debugByteStr("U4_calculated", U4_calculated)
206 debugByteStr("T_calculated", T_calculated)
207 debugByteStr("R_calculated", R_calculated)
208 debugByteStr("KE_calculated", KE_calculated)
209 debugByteStr("KM_calculated", KM_calculated)
210 debugByteStr("PADDED_MSG_calculated", PADDED_MSG_calculated)
211 debugByteStr("M_calculated", M_calculated)
212
213 return M_calculated
214
215
216 def main(argv):
217 # Generate Random 28 Byte Message
218 M = rndfile.read(28)
219 debugByteStr("M", M)
220
221 # Generate Random 28 Byte Additional Authenticated Data
222 A = rndfile.read(28)
223 debugByteStr("A", A)
224
225 # Generate Random 12 Byte Key
226 N = rndfile.read(12)
227
228 # Frame Size. Messages will be padded up to the next Frame size before being encrypted.
229 F = 32
230 debugInt("F", F)
231
232 K = scram_generate_key()
233
234 C, X, Tag = scram_encrypt(K, N, A, M, F)
235
236 M_calculated = scram_decrypt(K, N, A, C, X, Tag)
237
238 if (M != M_calculated):
239 print("FAILED: Decryption")
240 else:
241 print("PASSED: Decryption")
242
243 return
244
245
246 if __name__ == "__main__":
247 sys.exit(main(sys.argv[1:]))