also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -1,1089 +0,0 @@ | ||
| 1 | const std = @import("std"); | |
| 2 | const crypto = std.crypto; | |
| 3 | const hkdfExpandLabel = crypto.tls.hkdfExpandLabel; | |
| 4 | ||
| 5 | const Sha1 = crypto.hash.Sha1; | |
| 6 | const Sha256 = crypto.hash.sha2.Sha256; | |
| 7 | const Sha384 = crypto.hash.sha2.Sha384; | |
| 8 | ||
| 9 | const record = @import("record.zig"); | |
| 10 | const Record = record.Record; | |
| 11 | const Transcript = @import("transcript.zig").Transcript; | |
| 12 | const proto = @import("protocol.zig"); | |
| 13 | ||
| 14 | const csprng = @import("random.zig").csprng; | |
| 15 | ||
| 16 | // tls 1.2 cbc cipher types | |
| 17 | const CbcAes128Sha1 = CbcType(crypto.core.aes.Aes128, Sha1); | |
| 18 | const CbcAes128Sha256 = CbcType(crypto.core.aes.Aes128, Sha256); | |
| 19 | const CbcAes256Sha384 = CbcType(crypto.core.aes.Aes256, Sha384); | |
| 20 | // tls 1.2 gcm cipher types | |
| 21 | const Aead12Aes128Gcm = Aead12Type(crypto.aead.aes_gcm.Aes128Gcm); | |
| 22 | const Aead12Aes256Gcm = Aead12Type(crypto.aead.aes_gcm.Aes256Gcm); | |
| 23 | // tls 1.2 chacha cipher type | |
| 24 | const Aead12ChaCha = Aead12ChaChaType(crypto.aead.chacha_poly.ChaCha20Poly1305); | |
| 25 | // tls 1.3 cipher types | |
| 26 | const Aes128GcmSha256 = Aead13Type(crypto.aead.aes_gcm.Aes128Gcm, Sha256); | |
| 27 | const Aes256GcmSha384 = Aead13Type(crypto.aead.aes_gcm.Aes256Gcm, Sha384); | |
| 28 | const ChaChaSha256 = Aead13Type(crypto.aead.chacha_poly.ChaCha20Poly1305, Sha256); | |
| 29 | const Aegis128Sha256 = Aead13Type(crypto.aead.aegis.Aegis128L, Sha256); | |
| 30 | ||
| 31 | pub const encrypt_overhead_tls_12: comptime_int = @max( | |
| 32 | CbcAes128Sha1.encrypt_overhead, | |
| 33 | CbcAes128Sha256.encrypt_overhead, | |
| 34 | CbcAes256Sha384.encrypt_overhead, | |
| 35 | Aead12Aes128Gcm.encrypt_overhead, | |
| 36 | Aead12Aes256Gcm.encrypt_overhead, | |
| 37 | Aead12ChaCha.encrypt_overhead, | |
| 38 | ); | |
| 39 | pub const encrypt_overhead_tls_13: comptime_int = @max( | |
| 40 | Aes128GcmSha256.encrypt_overhead, | |
| 41 | Aes256GcmSha384.encrypt_overhead, | |
| 42 | ChaChaSha256.encrypt_overhead, | |
| 43 | Aegis128Sha256.encrypt_overhead, | |
| 44 | ); | |
| 45 | ||
| 46 | // ref (length): https://www.rfc-editor.org/rfc/rfc8446#section-5.1 | |
| 47 | pub const max_cleartext_len = 1 << 14; | |
| 48 | // ref (length): https://www.rfc-editor.org/rfc/rfc8446#section-5.2 | |
| 49 | // The sum of the lengths of the content and the padding, plus one for the inner | |
| 50 | // content type, plus any expansion added by the AEAD algorithm. | |
| 51 | pub const max_ciphertext_len = max_cleartext_len + 256; | |
| 52 | pub const max_ciphertext_record_len = record.header_len + max_ciphertext_len; | |
| 53 | // max ciphertext produced with this implementation () | |
| 54 | pub const max_encrypted_record_len = max_cleartext_len + @max(encrypt_overhead_tls_13, encrypt_overhead_tls_12); | |
| 55 | ||
| 56 | test { | |
| 57 | try testing.expectEqual(16645, max_ciphertext_record_len); | |
| 58 | try testing.expectEqual(85, encrypt_overhead_tls_12); | |
| 59 | try testing.expectEqual(22, encrypt_overhead_tls_13); | |
| 60 | try testing.expectEqual(16469, max_encrypted_record_len); | |
| 61 | } | |
| 62 | ||
| 63 | /// Returns type for cipher suite tag. | |
| 64 | fn CipherType(comptime tag: CipherSuite) type { | |
| 65 | return switch (tag) { | |
| 66 | // tls 1.2 cbc | |
| 67 | .ECDHE_ECDSA_WITH_AES_128_CBC_SHA, | |
| 68 | .ECDHE_RSA_WITH_AES_128_CBC_SHA, | |
| 69 | .RSA_WITH_AES_128_CBC_SHA, | |
| 70 | => CbcAes128Sha1, | |
| 71 | .ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, | |
| 72 | .ECDHE_RSA_WITH_AES_128_CBC_SHA256, | |
| 73 | .RSA_WITH_AES_128_CBC_SHA256, | |
| 74 | => CbcAes128Sha256, | |
| 75 | .ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, | |
| 76 | .ECDHE_RSA_WITH_AES_256_CBC_SHA384, | |
| 77 | => CbcAes256Sha384, | |
| 78 | ||
| 79 | // tls 1.2 gcm | |
| 80 | .ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | |
| 81 | .ECDHE_RSA_WITH_AES_128_GCM_SHA256, | |
| 82 | => Aead12Aes128Gcm, | |
| 83 | .ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | |
| 84 | .ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
| 85 | => Aead12Aes256Gcm, | |
| 86 | ||
| 87 | // tls 1.2 chacha | |
| 88 | .ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 89 | .ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 90 | => Aead12ChaCha, | |
| 91 | ||
| 92 | // tls 1.3 | |
| 93 | .AES_128_GCM_SHA256 => Aes128GcmSha256, | |
| 94 | .AES_256_GCM_SHA384 => Aes256GcmSha384, | |
| 95 | .CHACHA20_POLY1305_SHA256 => ChaChaSha256, | |
| 96 | .AEGIS_128L_SHA256 => Aegis128Sha256, | |
| 97 | ||
| 98 | else => unreachable, | |
| 99 | }; | |
| 100 | } | |
| 101 | ||
| 102 | /// Provides initialization and common encrypt/decrypt methods for all supported | |
| 103 | /// ciphers. Tls 1.2 has only application cipher, tls 1.3 has separate cipher | |
| 104 | /// for handshake and application. | |
| 105 | pub const Cipher = union(CipherSuite) { | |
| 106 | // tls 1.2 cbc | |
| 107 | ECDHE_ECDSA_WITH_AES_128_CBC_SHA: CipherType(.ECDHE_ECDSA_WITH_AES_128_CBC_SHA), | |
| 108 | ECDHE_RSA_WITH_AES_128_CBC_SHA: CipherType(.ECDHE_RSA_WITH_AES_128_CBC_SHA), | |
| 109 | RSA_WITH_AES_128_CBC_SHA: CipherType(.RSA_WITH_AES_128_CBC_SHA), | |
| 110 | ||
| 111 | ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: CipherType(.ECDHE_ECDSA_WITH_AES_128_CBC_SHA256), | |
| 112 | ECDHE_RSA_WITH_AES_128_CBC_SHA256: CipherType(.ECDHE_RSA_WITH_AES_128_CBC_SHA256), | |
| 113 | RSA_WITH_AES_128_CBC_SHA256: CipherType(.RSA_WITH_AES_128_CBC_SHA256), | |
| 114 | ||
| 115 | ECDHE_ECDSA_WITH_AES_256_CBC_SHA384: CipherType(.ECDHE_ECDSA_WITH_AES_256_CBC_SHA384), | |
| 116 | ECDHE_RSA_WITH_AES_256_CBC_SHA384: CipherType(.ECDHE_RSA_WITH_AES_256_CBC_SHA384), | |
| 117 | // tls 1.2 gcm | |
| 118 | ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: CipherType(.ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), | |
| 119 | ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: CipherType(.ECDHE_RSA_WITH_AES_256_GCM_SHA384), | |
| 120 | ECDHE_RSA_WITH_AES_128_GCM_SHA256: CipherType(.ECDHE_RSA_WITH_AES_128_GCM_SHA256), | |
| 121 | ECDHE_RSA_WITH_AES_256_GCM_SHA384: CipherType(.ECDHE_RSA_WITH_AES_256_GCM_SHA384), | |
| 122 | // tls 1.2 chacha | |
| 123 | ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256: CipherType(.ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256), | |
| 124 | ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256: CipherType(.ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256), | |
| 125 | // tls 1.3 | |
| 126 | AES_128_GCM_SHA256: CipherType(.AES_128_GCM_SHA256), | |
| 127 | AES_256_GCM_SHA384: CipherType(.AES_256_GCM_SHA384), | |
| 128 | CHACHA20_POLY1305_SHA256: CipherType(.CHACHA20_POLY1305_SHA256), | |
| 129 | AEGIS_128L_SHA256: CipherType(.AEGIS_128L_SHA256), | |
| 130 | ||
| 131 | // tls 1.2 application cipher | |
| 132 | pub fn initTls12(tag: CipherSuite, key_material: []const u8, side: proto.Side) !Cipher { | |
| 133 | switch (tag) { | |
| 134 | inline .ECDHE_ECDSA_WITH_AES_128_CBC_SHA, | |
| 135 | .ECDHE_RSA_WITH_AES_128_CBC_SHA, | |
| 136 | .RSA_WITH_AES_128_CBC_SHA, | |
| 137 | .ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, | |
| 138 | .ECDHE_RSA_WITH_AES_128_CBC_SHA256, | |
| 139 | .RSA_WITH_AES_128_CBC_SHA256, | |
| 140 | .ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, | |
| 141 | .ECDHE_RSA_WITH_AES_256_CBC_SHA384, | |
| 142 | .ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | |
| 143 | .ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | |
| 144 | .ECDHE_RSA_WITH_AES_128_GCM_SHA256, | |
| 145 | .ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
| 146 | .ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 147 | .ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 148 | => |comptime_tag| { | |
| 149 | return @unionInit(Cipher, @tagName(comptime_tag), CipherType(comptime_tag).init(key_material, side)); | |
| 150 | }, | |
| 151 | else => return error.TlsIllegalParameter, | |
| 152 | } | |
| 153 | } | |
| 154 | ||
| 155 | // tls 1.3 handshake or application cipher | |
| 156 | pub fn initTls13(tag: CipherSuite, secret: Transcript.Secret, side: proto.Side) !Cipher { | |
| 157 | return switch (tag) { | |
| 158 | inline .AES_128_GCM_SHA256, | |
| 159 | .AES_256_GCM_SHA384, | |
| 160 | .CHACHA20_POLY1305_SHA256, | |
| 161 | .AEGIS_128L_SHA256, | |
| 162 | => |comptime_tag| { | |
| 163 | return @unionInit(Cipher, @tagName(comptime_tag), CipherType(comptime_tag).init(secret, side)); | |
| 164 | }, | |
| 165 | else => return error.TlsIllegalParameter, | |
| 166 | }; | |
| 167 | } | |
| 168 | ||
| 169 | pub fn encrypt( | |
| 170 | c: *Cipher, | |
| 171 | buf: []u8, | |
| 172 | content_type: proto.ContentType, | |
| 173 | cleartext: []const u8, | |
| 174 | ) ![]const u8 { | |
| 175 | return switch (c.*) { | |
| 176 | inline else => |*f| try f.encrypt(buf, content_type, cleartext), | |
| 177 | }; | |
| 178 | } | |
| 179 | ||
| 180 | pub fn decrypt( | |
| 181 | c: *Cipher, | |
| 182 | buf: []u8, | |
| 183 | rec: Record, | |
| 184 | ) !struct { proto.ContentType, []u8 } { | |
| 185 | return switch (c.*) { | |
| 186 | inline else => |*f| { | |
| 187 | const content_type, const cleartext = try f.decrypt(buf, rec); | |
| 188 | if (cleartext.len > max_cleartext_len) return error.TlsRecordOverflow; | |
| 189 | return .{ content_type, cleartext }; | |
| 190 | }, | |
| 191 | }; | |
| 192 | } | |
| 193 | ||
| 194 | pub fn recordLen(c: Cipher, cleartext_len: usize) usize { | |
| 195 | return switch (c) { | |
| 196 | inline else => |f| f.recordLen(cleartext_len), | |
| 197 | }; | |
| 198 | } | |
| 199 | ||
| 200 | pub fn encryptOverhead(c: Cipher) usize { | |
| 201 | return switch (c) { | |
| 202 | inline else => |f| return @TypeOf(f).encrypt_overhead, | |
| 203 | }; | |
| 204 | } | |
| 205 | ||
| 206 | pub fn encryptSeq(c: Cipher) u64 { | |
| 207 | return switch (c) { | |
| 208 | inline else => |f| f.encrypt_seq, | |
| 209 | }; | |
| 210 | } | |
| 211 | ||
| 212 | pub fn keyUpdateEncrypt(c: *Cipher) !void { | |
| 213 | return switch (c.*) { | |
| 214 | inline .AES_128_GCM_SHA256, | |
| 215 | .AES_256_GCM_SHA384, | |
| 216 | .CHACHA20_POLY1305_SHA256, | |
| 217 | .AEGIS_128L_SHA256, | |
| 218 | => |*f| f.keyUpdateEncrypt(), | |
| 219 | // can't happen on tls 1.2 | |
| 220 | else => return error.TlsUnexpectedMessage, | |
| 221 | }; | |
| 222 | } | |
| 223 | pub fn keyUpdateDecrypt(c: *Cipher) !void { | |
| 224 | return switch (c.*) { | |
| 225 | inline .AES_128_GCM_SHA256, | |
| 226 | .AES_256_GCM_SHA384, | |
| 227 | .CHACHA20_POLY1305_SHA256, | |
| 228 | .AEGIS_128L_SHA256, | |
| 229 | => |*f| f.keyUpdateDecrypt(), | |
| 230 | // can't happen on tls 1.2 | |
| 231 | else => return error.TlsUnexpectedMessage, | |
| 232 | }; | |
| 233 | } | |
| 234 | }; | |
| 235 | ||
| 236 | fn Aead12Type(comptime AeadType: type) type { | |
| 237 | return struct { | |
| 238 | const explicit_iv_len = 8; | |
| 239 | const key_len = AeadType.key_length; | |
| 240 | const auth_tag_len = AeadType.tag_length; | |
| 241 | const nonce_len = AeadType.nonce_length; | |
| 242 | const iv_len = AeadType.nonce_length - explicit_iv_len; | |
| 243 | const encrypt_overhead = record.header_len + explicit_iv_len + auth_tag_len; | |
| 244 | ||
| 245 | encrypt_key: [key_len]u8, | |
| 246 | decrypt_key: [key_len]u8, | |
| 247 | encrypt_iv: [iv_len]u8, | |
| 248 | decrypt_iv: [iv_len]u8, | |
| 249 | encrypt_seq: u64 = 0, | |
| 250 | decrypt_seq: u64 = 0, | |
| 251 | rnd: std.Random = csprng, | |
| 252 | ||
| 253 | const Self = @This(); | |
| 254 | ||
| 255 | fn init(key_material: []const u8, side: proto.Side) Self { | |
| 256 | const client_key = key_material[0..key_len].*; | |
| 257 | const server_key = key_material[key_len..][0..key_len].*; | |
| 258 | const client_iv = key_material[2 * key_len ..][0..iv_len].*; | |
| 259 | const server_iv = key_material[2 * key_len + iv_len ..][0..iv_len].*; | |
| 260 | return .{ | |
| 261 | .encrypt_key = if (side == .client) client_key else server_key, | |
| 262 | .decrypt_key = if (side == .client) server_key else client_key, | |
| 263 | .encrypt_iv = if (side == .client) client_iv else server_iv, | |
| 264 | .decrypt_iv = if (side == .client) server_iv else client_iv, | |
| 265 | }; | |
| 266 | } | |
| 267 | ||
| 268 | /// Returns encrypted tls record in format: | |
| 269 | /// ----------------- buf ---------------------- | |
| 270 | /// header | explicit_iv | ciphertext | auth_tag | |
| 271 | /// | |
| 272 | /// tls record header: 5 bytes | |
| 273 | /// explicit_iv: 8 bytes | |
| 274 | /// ciphertext: same length as cleartext | |
| 275 | /// auth_tag: 16 bytes | |
| 276 | pub fn encrypt( | |
| 277 | self: *Self, | |
| 278 | buf: []u8, | |
| 279 | content_type: proto.ContentType, | |
| 280 | cleartext: []const u8, | |
| 281 | ) ![]const u8 { | |
| 282 | const record_len = record.header_len + explicit_iv_len + cleartext.len + auth_tag_len; | |
| 283 | if (buf.len < record_len) return error.TlsCipherNoSpaceLeft; | |
| 284 | ||
| 285 | const header = buf[0..record.header_len]; | |
| 286 | const explicit_iv = buf[record.header_len..][0..explicit_iv_len]; | |
| 287 | const ciphertext = buf[record.header_len + explicit_iv_len ..][0..cleartext.len]; | |
| 288 | const auth_tag = buf[record.header_len + explicit_iv_len + cleartext.len ..][0..auth_tag_len]; | |
| 289 | ||
| 290 | header.* = record.header(content_type, explicit_iv_len + cleartext.len + auth_tag_len); | |
| 291 | self.rnd.bytes(explicit_iv); | |
| 292 | const iv = self.encrypt_iv ++ explicit_iv.*; | |
| 293 | const ad = additionalData(self.encrypt_seq, content_type, cleartext.len); | |
| 294 | AeadType.encrypt(ciphertext, auth_tag, cleartext, &ad, iv, self.encrypt_key); | |
| 295 | self.encrypt_seq +%= 1; | |
| 296 | ||
| 297 | return buf[0..record_len]; | |
| 298 | } | |
| 299 | ||
| 300 | pub fn recordLen(_: Self, cleartext_len: usize) usize { | |
| 301 | return record.header_len + explicit_iv_len + cleartext_len + auth_tag_len; | |
| 302 | } | |
| 303 | ||
| 304 | /// Decrypts payload into cleartext. Returns tls record content type and | |
| 305 | /// cleartext. | |
| 306 | /// Accepts tls record header and payload: | |
| 307 | /// header | ----------- payload --------------- | |
| 308 | /// header | explicit_iv | ciphertext | auth_tag | |
| 309 | pub fn decrypt( | |
| 310 | self: *Self, | |
| 311 | buf: []u8, | |
| 312 | rec: Record, | |
| 313 | ) !struct { proto.ContentType, []u8 } { | |
| 314 | const overhead = explicit_iv_len + auth_tag_len; | |
| 315 | if (rec.payload.len < overhead) return error.TlsDecryptError; | |
| 316 | const cleartext_len = rec.payload.len - overhead; | |
| 317 | if (buf.len < cleartext_len) return error.TlsCipherNoSpaceLeft; | |
| 318 | ||
| 319 | const explicit_iv = rec.payload[0..explicit_iv_len]; | |
| 320 | const ciphertext = rec.payload[explicit_iv_len..][0..cleartext_len]; | |
| 321 | const auth_tag = rec.payload[explicit_iv_len + cleartext_len ..][0..auth_tag_len]; | |
| 322 | ||
| 323 | const iv = self.decrypt_iv ++ explicit_iv.*; | |
| 324 | const ad = additionalData(self.decrypt_seq, rec.content_type, cleartext_len); | |
| 325 | const cleartext = buf[0..cleartext_len]; | |
| 326 | AeadType.decrypt(cleartext, ciphertext, auth_tag.*, &ad, iv, self.decrypt_key) catch return error.TlsDecryptError; | |
| 327 | self.decrypt_seq +%= 1; | |
| 328 | return .{ rec.content_type, cleartext }; | |
| 329 | } | |
| 330 | }; | |
| 331 | } | |
| 332 | ||
| 333 | fn Aead12ChaChaType(comptime AeadType: type) type { | |
| 334 | return struct { | |
| 335 | const key_len = AeadType.key_length; | |
| 336 | const auth_tag_len = AeadType.tag_length; | |
| 337 | const nonce_len = AeadType.nonce_length; | |
| 338 | const encrypt_overhead = record.header_len + auth_tag_len; | |
| 339 | ||
| 340 | encrypt_key: [key_len]u8, | |
| 341 | decrypt_key: [key_len]u8, | |
| 342 | encrypt_iv: [nonce_len]u8, | |
| 343 | decrypt_iv: [nonce_len]u8, | |
| 344 | encrypt_seq: u64 = 0, | |
| 345 | decrypt_seq: u64 = 0, | |
| 346 | ||
| 347 | const Self = @This(); | |
| 348 | ||
| 349 | fn init(key_material: []const u8, side: proto.Side) Self { | |
| 350 | const client_key = key_material[0..key_len].*; | |
| 351 | const server_key = key_material[key_len..][0..key_len].*; | |
| 352 | const client_iv = key_material[2 * key_len ..][0..nonce_len].*; | |
| 353 | const server_iv = key_material[2 * key_len + nonce_len ..][0..nonce_len].*; | |
| 354 | return .{ | |
| 355 | .encrypt_key = if (side == .client) client_key else server_key, | |
| 356 | .decrypt_key = if (side == .client) server_key else client_key, | |
| 357 | .encrypt_iv = if (side == .client) client_iv else server_iv, | |
| 358 | .decrypt_iv = if (side == .client) server_iv else client_iv, | |
| 359 | }; | |
| 360 | } | |
| 361 | ||
| 362 | /// Returns encrypted tls record in format: | |
| 363 | /// ------------ buf ------------- | |
| 364 | /// header | ciphertext | auth_tag | |
| 365 | /// | |
| 366 | /// tls record header: 5 bytes | |
| 367 | /// ciphertext: same length as cleartext | |
| 368 | /// auth_tag: 16 bytes | |
| 369 | pub fn encrypt( | |
| 370 | self: *Self, | |
| 371 | buf: []u8, | |
| 372 | content_type: proto.ContentType, | |
| 373 | cleartext: []const u8, | |
| 374 | ) ![]const u8 { | |
| 375 | const record_len = record.header_len + cleartext.len + auth_tag_len; | |
| 376 | if (buf.len < record_len) return error.TlsCipherNoSpaceLeft; | |
| 377 | ||
| 378 | const ciphertext = buf[record.header_len..][0..cleartext.len]; | |
| 379 | const auth_tag = buf[record.header_len + ciphertext.len ..][0..auth_tag_len]; | |
| 380 | ||
| 381 | const ad = additionalData(self.encrypt_seq, content_type, cleartext.len); | |
| 382 | const iv = ivWithSeq(nonce_len, self.encrypt_iv, self.encrypt_seq); | |
| 383 | AeadType.encrypt(ciphertext, auth_tag, cleartext, &ad, iv, self.encrypt_key); | |
| 384 | self.encrypt_seq +%= 1; | |
| 385 | ||
| 386 | buf[0..record.header_len].* = record.header(content_type, ciphertext.len + auth_tag.len); | |
| 387 | return buf[0..record_len]; | |
| 388 | } | |
| 389 | ||
| 390 | pub fn recordLen(_: Self, cleartext_len: usize) usize { | |
| 391 | return record.header_len + cleartext_len + auth_tag_len; | |
| 392 | } | |
| 393 | ||
| 394 | /// Decrypts payload into cleartext. Returns tls record content type and | |
| 395 | /// cleartext. | |
| 396 | /// Accepts tls record header and payload: | |
| 397 | /// header | ----- payload ------- | |
| 398 | /// header | ciphertext | auth_tag | |
| 399 | pub fn decrypt( | |
| 400 | self: *Self, | |
| 401 | buf: []u8, | |
| 402 | rec: Record, | |
| 403 | ) !struct { proto.ContentType, []u8 } { | |
| 404 | const overhead = auth_tag_len; | |
| 405 | if (rec.payload.len < overhead) return error.TlsDecryptError; | |
| 406 | const cleartext_len = rec.payload.len - overhead; | |
| 407 | if (buf.len < cleartext_len) return error.TlsCipherNoSpaceLeft; | |
| 408 | ||
| 409 | const ciphertext = rec.payload[0..cleartext_len]; | |
| 410 | const auth_tag = rec.payload[cleartext_len..][0..auth_tag_len]; | |
| 411 | const cleartext = buf[0..cleartext_len]; | |
| 412 | ||
| 413 | const ad = additionalData(self.decrypt_seq, rec.content_type, cleartext_len); | |
| 414 | const iv = ivWithSeq(nonce_len, self.decrypt_iv, self.decrypt_seq); | |
| 415 | AeadType.decrypt(cleartext, ciphertext, auth_tag.*, &ad, iv, self.decrypt_key) catch return error.TlsDecryptError; | |
| 416 | self.decrypt_seq +%= 1; | |
| 417 | return .{ rec.content_type, cleartext }; | |
| 418 | } | |
| 419 | }; | |
| 420 | } | |
| 421 | ||
| 422 | fn Aead13Type(comptime AeadType: type, comptime Hash: type) type { | |
| 423 | return struct { | |
| 424 | const Hmac = crypto.auth.hmac.Hmac(Hash); | |
| 425 | const Hkdf = crypto.kdf.hkdf.Hkdf(Hmac); | |
| 426 | ||
| 427 | const key_len = AeadType.key_length; | |
| 428 | const auth_tag_len = AeadType.tag_length; | |
| 429 | const nonce_len = AeadType.nonce_length; | |
| 430 | const digest_len = Hash.digest_length; | |
| 431 | const encrypt_overhead = record.header_len + 1 + auth_tag_len; | |
| 432 | ||
| 433 | encrypt_secret: [digest_len]u8, | |
| 434 | decrypt_secret: [digest_len]u8, | |
| 435 | encrypt_key: [key_len]u8, | |
| 436 | decrypt_key: [key_len]u8, | |
| 437 | encrypt_iv: [nonce_len]u8, | |
| 438 | decrypt_iv: [nonce_len]u8, | |
| 439 | encrypt_seq: u64 = 0, | |
| 440 | decrypt_seq: u64 = 0, | |
| 441 | ||
| 442 | const Self = @This(); | |
| 443 | ||
| 444 | pub fn init(secret: Transcript.Secret, side: proto.Side) Self { | |
| 445 | var self = Self{ | |
| 446 | .encrypt_secret = if (side == .client) secret.client[0..digest_len].* else secret.server[0..digest_len].*, | |
| 447 | .decrypt_secret = if (side == .server) secret.client[0..digest_len].* else secret.server[0..digest_len].*, | |
| 448 | .encrypt_key = undefined, | |
| 449 | .decrypt_key = undefined, | |
| 450 | .encrypt_iv = undefined, | |
| 451 | .decrypt_iv = undefined, | |
| 452 | }; | |
| 453 | self.keyGenerate(); | |
| 454 | return self; | |
| 455 | } | |
| 456 | ||
| 457 | fn keyGenerate(self: *Self) void { | |
| 458 | self.encrypt_key = hkdfExpandLabel(Hkdf, self.encrypt_secret, "key", "", key_len); | |
| 459 | self.decrypt_key = hkdfExpandLabel(Hkdf, self.decrypt_secret, "key", "", key_len); | |
| 460 | self.encrypt_iv = hkdfExpandLabel(Hkdf, self.encrypt_secret, "iv", "", nonce_len); | |
| 461 | self.decrypt_iv = hkdfExpandLabel(Hkdf, self.decrypt_secret, "iv", "", nonce_len); | |
| 462 | } | |
| 463 | ||
| 464 | pub fn keyUpdateEncrypt(self: *Self) void { | |
| 465 | self.encrypt_secret = hkdfExpandLabel(Hkdf, self.encrypt_secret, "traffic upd", "", digest_len); | |
| 466 | self.encrypt_seq = 0; | |
| 467 | self.keyGenerate(); | |
| 468 | } | |
| 469 | ||
| 470 | pub fn keyUpdateDecrypt(self: *Self) void { | |
| 471 | self.decrypt_secret = hkdfExpandLabel(Hkdf, self.decrypt_secret, "traffic upd", "", digest_len); | |
| 472 | self.decrypt_seq = 0; | |
| 473 | self.keyGenerate(); | |
| 474 | } | |
| 475 | ||
| 476 | /// Returns encrypted tls record in format: | |
| 477 | /// ------------ buf ------------- | |
| 478 | /// header | ciphertext | auth_tag | |
| 479 | /// | |
| 480 | /// tls record header: 5 bytes | |
| 481 | /// ciphertext: cleartext len + 1 byte content type | |
| 482 | /// auth_tag: 16 bytes | |
| 483 | pub fn encrypt( | |
| 484 | self: *Self, | |
| 485 | buf: []u8, | |
| 486 | content_type: proto.ContentType, | |
| 487 | cleartext: []const u8, | |
| 488 | ) ![]const u8 { | |
| 489 | const payload_len = cleartext.len + 1 + auth_tag_len; | |
| 490 | const record_len = record.header_len + payload_len; | |
| 491 | if (buf.len < record_len) return error.TlsCipherNoSpaceLeft; | |
| 492 | ||
| 493 | const header = buf[0..record.header_len]; | |
| 494 | header.* = record.header(.application_data, payload_len); | |
| 495 | ||
| 496 | // Skip @memcpy if cleartext is already part of the buf at right position | |
| 497 | if (&cleartext[0] != &buf[record.header_len]) { | |
| 498 | @memcpy(buf[record.header_len..][0..cleartext.len], cleartext); | |
| 499 | } | |
| 500 | buf[record.header_len + cleartext.len] = @backingInt(content_type); | |
| 501 | const ciphertext = buf[record.header_len..][0 .. cleartext.len + 1]; | |
| 502 | const auth_tag = buf[record.header_len + ciphertext.len ..][0..auth_tag_len]; | |
| 503 | ||
| 504 | const iv = ivWithSeq(nonce_len, self.encrypt_iv, self.encrypt_seq); | |
| 505 | AeadType.encrypt(ciphertext, auth_tag, ciphertext, header, iv, self.encrypt_key); | |
| 506 | self.encrypt_seq +%= 1; | |
| 507 | return buf[0..record_len]; | |
| 508 | } | |
| 509 | ||
| 510 | pub fn recordLen(_: Self, cleartext_len: usize) usize { | |
| 511 | const payload_len = cleartext_len + 1 + auth_tag_len; | |
| 512 | return record.header_len + payload_len; | |
| 513 | } | |
| 514 | ||
| 515 | /// Decrypts payload into cleartext. Returns tls record content type and | |
| 516 | /// cleartext. | |
| 517 | /// Accepts tls record header and payload: | |
| 518 | /// header | ------- payload --------- | |
| 519 | /// header | ciphertext | auth_tag | |
| 520 | /// header | cleartext + ct | auth_tag | |
| 521 | /// Ciphertext after decryption contains cleartext and content type (1 byte). | |
| 522 | pub fn decrypt( | |
| 523 | self: *Self, | |
| 524 | buf: []u8, | |
| 525 | rec: Record, | |
| 526 | ) !struct { proto.ContentType, []u8 } { | |
| 527 | const overhead = auth_tag_len + 1; | |
| 528 | if (rec.payload.len < overhead) return error.TlsDecryptError; | |
| 529 | const ciphertext_len = rec.payload.len - auth_tag_len; | |
| 530 | if (ciphertext_len > max_cleartext_len + 1) return error.TlsRecordOverflow; | |
| 531 | if (buf.len < ciphertext_len) return error.TlsCipherNoSpaceLeft; | |
| 532 | ||
| 533 | const ciphertext = rec.payload[0..ciphertext_len]; | |
| 534 | const auth_tag = rec.payload[ciphertext_len..][0..auth_tag_len]; | |
| 535 | ||
| 536 | const iv = ivWithSeq(nonce_len, self.decrypt_iv, self.decrypt_seq); | |
| 537 | AeadType.decrypt(buf[0..ciphertext_len], ciphertext, auth_tag.*, rec.header, iv, self.decrypt_key) catch return error.TlsBadRecordMac; | |
| 538 | ||
| 539 | // Remove zero bytes padding | |
| 540 | var content_type_idx: usize = ciphertext_len - 1; | |
| 541 | while (buf[content_type_idx] == 0 and content_type_idx > 0) : (content_type_idx -= 1) {} | |
| 542 | ||
| 543 | const cleartext = buf[0..content_type_idx]; | |
| 544 | const content_type: proto.ContentType = @fromBackingInt(@intCast(buf[content_type_idx])); | |
| 545 | self.decrypt_seq +%= 1; | |
| 546 | return .{ content_type, cleartext }; | |
| 547 | } | |
| 548 | }; | |
| 549 | } | |
| 550 | ||
| 551 | fn CbcType(comptime BlockCipher: type, comptime HashType: type) type { | |
| 552 | const CBC = @import("cbc/main.zig").CBC(BlockCipher); | |
| 553 | return struct { | |
| 554 | const mac_len = HashType.digest_length; // 20, 32, 48 bytes for sha1, sha256, sha384 | |
| 555 | const key_len = BlockCipher.key_bits / 8; // 16, 32 for Aes128, Aes256 | |
| 556 | const iv_len = 16; | |
| 557 | const encrypt_overhead = record.header_len + iv_len + mac_len + max_padding; | |
| 558 | ||
| 559 | const Hmac = crypto.auth.hmac.Hmac(HashType); | |
| 560 | const paddedLength = CBC.paddedLength; | |
| 561 | const max_padding = 16; | |
| 562 | ||
| 563 | encrypt_secret: [mac_len]u8, | |
| 564 | decrypt_secret: [mac_len]u8, | |
| 565 | encrypt_key: [key_len]u8, | |
| 566 | decrypt_key: [key_len]u8, | |
| 567 | encrypt_seq: u64 = 0, | |
| 568 | decrypt_seq: u64 = 0, | |
| 569 | rnd: std.Random = csprng, | |
| 570 | ||
| 571 | const Self = @This(); | |
| 572 | ||
| 573 | fn init(key_material: []const u8, side: proto.Side) Self { | |
| 574 | const client_secret = key_material[0..mac_len].*; | |
| 575 | const server_secret = key_material[mac_len..][0..mac_len].*; | |
| 576 | const client_key = key_material[2 * mac_len ..][0..key_len].*; | |
| 577 | const server_key = key_material[2 * mac_len + key_len ..][0..key_len].*; | |
| 578 | return .{ | |
| 579 | .encrypt_secret = if (side == .client) client_secret else server_secret, | |
| 580 | .decrypt_secret = if (side == .client) server_secret else client_secret, | |
| 581 | .encrypt_key = if (side == .client) client_key else server_key, | |
| 582 | .decrypt_key = if (side == .client) server_key else client_key, | |
| 583 | }; | |
| 584 | } | |
| 585 | ||
| 586 | /// Returns encrypted tls record in format: | |
| 587 | /// ----------------- buf ----------------- | |
| 588 | /// header | iv | ------ ciphertext ------- | |
| 589 | /// header | iv | cleartext | mac | padding | |
| 590 | /// | |
| 591 | /// tls record header: 5 bytes | |
| 592 | /// iv: 16 bytes | |
| 593 | /// ciphertext: cleartext length + mac + padding | |
| 594 | /// mac: 20, 32 or 48 (sha1, sha256, sha384) | |
| 595 | /// padding: 1-16 bytes | |
| 596 | /// | |
| 597 | /// Max encrypt buf overhead = iv + mac + padding (1-16) | |
| 598 | /// aes_128_cbc_sha => 16 + 20 + 16 = 52 | |
| 599 | /// aes_128_cbc_sha256 => 16 + 32 + 16 = 64 | |
| 600 | /// aes_256_cbc_sha384 => 16 + 48 + 16 = 80 | |
| 601 | pub fn encrypt( | |
| 602 | self: *Self, | |
| 603 | buf: []u8, | |
| 604 | content_type: proto.ContentType, | |
| 605 | cleartext: []const u8, | |
| 606 | ) ![]const u8 { | |
| 607 | if (buf.len < self.recordLen(cleartext.len)) return error.TlsCipherNoSpaceLeft; | |
| 608 | const cleartext_idx = record.header_len + iv_len; // position of cleartext in buf | |
| 609 | @memcpy(buf[cleartext_idx..][0..cleartext.len], cleartext); | |
| 610 | ||
| 611 | { // calculate mac from (ad + cleartext) | |
| 612 | // ... | ad | cleartext | mac | ... | |
| 613 | // | -- mac msg -- | mac | | |
| 614 | const ad = additionalData(self.encrypt_seq, content_type, cleartext.len); | |
| 615 | const mac_msg = buf[cleartext_idx - ad.len ..][0 .. ad.len + cleartext.len]; | |
| 616 | @memcpy(mac_msg[0..ad.len], &ad); | |
| 617 | const mac = buf[cleartext_idx + cleartext.len ..][0..mac_len]; | |
| 618 | Hmac.create(mac, mac_msg, &self.encrypt_secret); | |
| 619 | self.encrypt_seq +%= 1; | |
| 620 | } | |
| 621 | ||
| 622 | // ... | cleartext | mac | ... | |
| 623 | // ... | -- plaintext --- ... | |
| 624 | // ... | cleartext | mac | padding | |
| 625 | // ... | ------ ciphertext ------- | |
| 626 | const unpadded_len = cleartext.len + mac_len; | |
| 627 | const padded_len = paddedLength(unpadded_len); | |
| 628 | const plaintext = buf[cleartext_idx..][0..unpadded_len]; | |
| 629 | const ciphertext = buf[cleartext_idx..][0..padded_len]; | |
| 630 | ||
| 631 | // Add header and iv at the buf start | |
| 632 | // header | iv | ... | |
| 633 | buf[0..record.header_len].* = record.header(content_type, iv_len + ciphertext.len); | |
| 634 | const iv = buf[record.header_len..][0..iv_len]; | |
| 635 | self.rnd.bytes(iv); | |
| 636 | ||
| 637 | // encrypt plaintext into ciphertext | |
| 638 | CBC.init(self.encrypt_key).encrypt(ciphertext, plaintext, iv[0..iv_len].*); | |
| 639 | ||
| 640 | // header | iv | ------ ciphertext ------- | |
| 641 | return buf[0 .. record.header_len + iv_len + ciphertext.len]; | |
| 642 | } | |
| 643 | ||
| 644 | pub fn recordLen(_: Self, cleartext_len: usize) usize { | |
| 645 | const unpadded_len = cleartext_len + mac_len; | |
| 646 | const padded_len = paddedLength(unpadded_len); | |
| 647 | return record.header_len + iv_len + padded_len; | |
| 648 | } | |
| 649 | ||
| 650 | /// Decrypts payload into cleartext. Returns tls record content type and | |
| 651 | /// cleartext. | |
| 652 | pub fn decrypt( | |
| 653 | self: *Self, | |
| 654 | buf: []u8, | |
| 655 | rec: Record, | |
| 656 | ) !struct { proto.ContentType, []u8 } { | |
| 657 | if (rec.payload.len < iv_len + mac_len + 1) return error.TlsDecryptError; | |
| 658 | ||
| 659 | // --------- payload ------------ | |
| 660 | // iv | ------ ciphertext ------- | |
| 661 | // iv | cleartext | mac | padding | |
| 662 | const iv = rec.payload[0..iv_len]; | |
| 663 | const ciphertext = rec.payload[iv_len..]; | |
| 664 | ||
| 665 | if (buf.len < ciphertext.len + additional_data_len) return error.TlsCipherNoSpaceLeft; | |
| 666 | // ---------- buf --------------- | |
| 667 | // ad | ------ plaintext -------- | |
| 668 | // ad | cleartext | mac | padding | |
| 669 | const plaintext = buf[additional_data_len..][0..ciphertext.len]; | |
| 670 | // decrypt ciphertext -> plaintext | |
| 671 | CBC.init(self.decrypt_key).decrypt(plaintext, ciphertext, iv[0..iv_len].*) catch return error.TlsDecryptError; | |
| 672 | ||
| 673 | // get padding len from last padding byte | |
| 674 | const padding_len = plaintext[plaintext.len - 1] + 1; | |
| 675 | if (plaintext.len < mac_len + padding_len) return error.TlsDecryptError; | |
| 676 | // split plaintext into cleartext and mac | |
| 677 | const cleartext_len = plaintext.len - mac_len - padding_len; | |
| 678 | const cleartext = plaintext[0..cleartext_len]; | |
| 679 | const mac = plaintext[cleartext_len..][0..mac_len]; | |
| 680 | ||
| 681 | // write ad to the buf | |
| 682 | var ad = additionalData(self.decrypt_seq, rec.content_type, cleartext_len); | |
| 683 | @memcpy(buf[0..ad.len], &ad); | |
| 684 | const mac_msg = buf[0 .. ad.len + cleartext_len]; | |
| 685 | self.decrypt_seq +%= 1; | |
| 686 | ||
| 687 | // calculate expected mac and compare with received mac | |
| 688 | var expected_mac: [mac_len]u8 = undefined; | |
| 689 | Hmac.create(&expected_mac, mac_msg, &self.decrypt_secret); | |
| 690 | if (!std.mem.eql(u8, &expected_mac, mac)) | |
| 691 | return error.TlsBadRecordMac; | |
| 692 | ||
| 693 | return .{ rec.content_type, cleartext }; | |
| 694 | } | |
| 695 | }; | |
| 696 | } | |
| 697 | ||
| 698 | // xor lower 8 iv bytes with seq | |
| 699 | fn ivWithSeq(comptime nonce_len: usize, iv: [nonce_len]u8, seq: u64) [nonce_len]u8 { | |
| 700 | var res = iv; | |
| 701 | const buf = res[nonce_len - 8 ..]; | |
| 702 | const operand = std.mem.readInt(u64, buf, .big); | |
| 703 | std.mem.writeInt(u64, buf, operand ^ seq, .big); | |
| 704 | return res; | |
| 705 | } | |
| 706 | ||
| 707 | pub const additional_data_len = record.header_len + @sizeOf(u64); | |
| 708 | ||
| 709 | fn additionalData(seq: u64, content_type: proto.ContentType, payload_len: usize) [additional_data_len]u8 { | |
| 710 | const header = record.header(content_type, payload_len); | |
| 711 | var seq_buf: [8]u8 = undefined; | |
| 712 | std.mem.writeInt(u64, &seq_buf, seq, .big); | |
| 713 | return seq_buf ++ header; | |
| 714 | } | |
| 715 | ||
| 716 | // Cipher suites lists. In the order of preference. | |
| 717 | // For the preference using grades priority and rules from Go project. | |
| 718 | // https://ciphersuite.info/page/faq/ | |
| 719 | // https://github.com/golang/go/blob/73186ba00251b3ed8baaab36e4f5278c7681155b/src/crypto/tls/cipher_suites.go#L226 | |
| 720 | pub const cipher_suites = struct { | |
| 721 | pub const tls12_secure = if (crypto.core.aes.has_hardware_support) [_]CipherSuite{ | |
| 722 | // recommended | |
| 723 | .ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | |
| 724 | .ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | |
| 725 | .ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 726 | // secure | |
| 727 | .ECDHE_RSA_WITH_AES_128_GCM_SHA256, | |
| 728 | .ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
| 729 | .ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 730 | } else [_]CipherSuite{ | |
| 731 | // recommended | |
| 732 | .ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 733 | .ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | |
| 734 | .ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | |
| 735 | ||
| 736 | // secure | |
| 737 | .ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 738 | .ECDHE_RSA_WITH_AES_128_GCM_SHA256, | |
| 739 | .ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
| 740 | }; | |
| 741 | pub const tls12_weak = [_]CipherSuite{ | |
| 742 | // weak | |
| 743 | .ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, | |
| 744 | .ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, | |
| 745 | .ECDHE_ECDSA_WITH_AES_128_CBC_SHA, | |
| 746 | .ECDHE_RSA_WITH_AES_128_CBC_SHA256, | |
| 747 | .ECDHE_RSA_WITH_AES_256_CBC_SHA384, | |
| 748 | .ECDHE_RSA_WITH_AES_128_CBC_SHA, | |
| 749 | ||
| 750 | .RSA_WITH_AES_128_CBC_SHA256, | |
| 751 | .RSA_WITH_AES_128_CBC_SHA, | |
| 752 | }; | |
| 753 | pub const tls13_ = if (crypto.core.aes.has_hardware_support) [_]CipherSuite{ | |
| 754 | .AES_128_GCM_SHA256, | |
| 755 | .AES_256_GCM_SHA384, | |
| 756 | .CHACHA20_POLY1305_SHA256, | |
| 757 | // Excluded because didn't find server which supports it to test | |
| 758 | // .AEGIS_128L_SHA256 | |
| 759 | } else [_]CipherSuite{ | |
| 760 | .CHACHA20_POLY1305_SHA256, | |
| 761 | .AES_128_GCM_SHA256, | |
| 762 | .AES_256_GCM_SHA384, | |
| 763 | }; | |
| 764 | ||
| 765 | pub const tls13 = &tls13_; | |
| 766 | pub const tls12 = &(tls12_secure ++ tls12_weak); | |
| 767 | pub const secure = &(tls13_ ++ tls12_secure); | |
| 768 | pub const all = &(tls13_ ++ tls12_secure ++ tls12_weak); | |
| 769 | ||
| 770 | pub fn includes(list: []const CipherSuite, cs: CipherSuite) bool { | |
| 771 | for (list) |s| { | |
| 772 | if (cs == s) return true; | |
| 773 | } | |
| 774 | return false; | |
| 775 | } | |
| 776 | }; | |
| 777 | ||
| 778 | // Weak, secure, recommended grades are from https://ciphersuite.info/page/faq/ | |
| 779 | pub const CipherSuite = enum(u16) { | |
| 780 | // tls 1.2 cbc sha1 | |
| 781 | ECDHE_ECDSA_WITH_AES_128_CBC_SHA = 0xc009, // weak | |
| 782 | ECDHE_RSA_WITH_AES_128_CBC_SHA = 0xc013, // weak | |
| 783 | RSA_WITH_AES_128_CBC_SHA = 0x002F, // weak | |
| 784 | // tls 1.2 cbc sha256 | |
| 785 | ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 = 0xc023, // weak | |
| 786 | ECDHE_RSA_WITH_AES_128_CBC_SHA256 = 0xc027, // weak | |
| 787 | RSA_WITH_AES_128_CBC_SHA256 = 0x003c, // weak | |
| 788 | // tls 1.2 cbc sha384 | |
| 789 | ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = 0xc024, // weak | |
| 790 | ECDHE_RSA_WITH_AES_256_CBC_SHA384 = 0xc028, // weak | |
| 791 | // tls 1.2 gcm | |
| 792 | ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0xc02b, // recommended | |
| 793 | ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 = 0xc02c, // recommended | |
| 794 | ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xc02f, // secure | |
| 795 | ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0xc030, // secure | |
| 796 | // tls 1.2 chacha | |
| 797 | ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = 0xcca9, // recommended | |
| 798 | ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 = 0xcca8, // secure | |
| 799 | // tls 1.3 (all are recommended) | |
| 800 | AES_128_GCM_SHA256 = 0x1301, | |
| 801 | AES_256_GCM_SHA384 = 0x1302, | |
| 802 | CHACHA20_POLY1305_SHA256 = 0x1303, | |
| 803 | AEGIS_128L_SHA256 = 0x1307, | |
| 804 | // AEGIS_256_SHA512 = 0x1306, | |
| 805 | _, | |
| 806 | ||
| 807 | pub fn validate(cs: CipherSuite) !void { | |
| 808 | if (cipher_suites.includes(cipher_suites.tls12, cs)) return; | |
| 809 | if (cipher_suites.includes(cipher_suites.tls13, cs)) return; | |
| 810 | return error.TlsIllegalParameter; | |
| 811 | } | |
| 812 | ||
| 813 | pub const Versions = enum { | |
| 814 | both, | |
| 815 | tls_1_3, | |
| 816 | tls_1_2, | |
| 817 | }; | |
| 818 | ||
| 819 | // get tls versions from list of cipher suites | |
| 820 | pub fn versions(list: []const CipherSuite) !Versions { | |
| 821 | var has_12 = false; | |
| 822 | var has_13 = false; | |
| 823 | for (list) |cs| { | |
| 824 | if (cipher_suites.includes(cipher_suites.tls12, cs)) { | |
| 825 | has_12 = true; | |
| 826 | } else { | |
| 827 | if (cipher_suites.includes(cipher_suites.tls13, cs)) has_13 = true; | |
| 828 | } | |
| 829 | } | |
| 830 | if (has_12 and has_13) return .both; | |
| 831 | if (has_12) return .tls_1_2; | |
| 832 | if (has_13) return .tls_1_3; | |
| 833 | return error.TlsIllegalParameter; | |
| 834 | } | |
| 835 | ||
| 836 | pub const KeyExchangeAlgorithm = enum { | |
| 837 | ecdhe, | |
| 838 | rsa, | |
| 839 | }; | |
| 840 | ||
| 841 | pub fn keyExchange(s: CipherSuite) KeyExchangeAlgorithm { | |
| 842 | return switch (s) { | |
| 843 | // Random premaster secret, encrypted with publich key from certificate. | |
| 844 | // No server key exchange message. | |
| 845 | .RSA_WITH_AES_128_CBC_SHA, | |
| 846 | .RSA_WITH_AES_128_CBC_SHA256, | |
| 847 | => .rsa, | |
| 848 | else => .ecdhe, | |
| 849 | }; | |
| 850 | } | |
| 851 | ||
| 852 | pub const HashTag = enum { | |
| 853 | sha256, | |
| 854 | sha384, | |
| 855 | sha512, | |
| 856 | }; | |
| 857 | ||
| 858 | pub fn hash(cs: CipherSuite) HashTag { | |
| 859 | return switch (cs) { | |
| 860 | .ECDHE_RSA_WITH_AES_256_CBC_SHA384, | |
| 861 | .ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
| 862 | .ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, | |
| 863 | .ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | |
| 864 | .AES_256_GCM_SHA384, | |
| 865 | => .sha384, | |
| 866 | else => .sha256, | |
| 867 | }; | |
| 868 | } | |
| 869 | }; | |
| 870 | ||
| 871 | const testing = std.testing; | |
| 872 | const testu = @import("testu.zig"); | |
| 873 | ||
| 874 | test "CipherSuite validate" { | |
| 875 | { | |
| 876 | const cs: CipherSuite = .AES_256_GCM_SHA384; | |
| 877 | try cs.validate(); | |
| 878 | try testing.expectEqual(cs.hash(), .sha384); | |
| 879 | try testing.expectEqual(cs.keyExchange(), .ecdhe); | |
| 880 | } | |
| 881 | { | |
| 882 | const cs: CipherSuite = .AES_128_GCM_SHA256; | |
| 883 | try cs.validate(); | |
| 884 | try testing.expectEqual(.sha256, cs.hash()); | |
| 885 | try testing.expectEqual(.ecdhe, cs.keyExchange()); | |
| 886 | } | |
| 887 | for (cipher_suites.tls12) |cs| { | |
| 888 | try cs.validate(); | |
| 889 | _ = cs.hash(); | |
| 890 | _ = cs.keyExchange(); | |
| 891 | } | |
| 892 | } | |
| 893 | ||
| 894 | test "CipherSuite versions" { | |
| 895 | try testing.expectEqual(.tls_1_3, CipherSuite.versions(&[_]CipherSuite{.AES_128_GCM_SHA256})); | |
| 896 | try testing.expectEqual(.both, CipherSuite.versions(&[_]CipherSuite{ .AES_128_GCM_SHA256, .ECDHE_ECDSA_WITH_AES_128_CBC_SHA })); | |
| 897 | try testing.expectEqual(.tls_1_2, CipherSuite.versions(&[_]CipherSuite{.RSA_WITH_AES_128_CBC_SHA})); | |
| 898 | } | |
| 899 | ||
| 900 | test "gcm 1.2 encrypt overhead" { | |
| 901 | inline for ([_]type{ | |
| 902 | Aead12Aes128Gcm, | |
| 903 | Aead12Aes256Gcm, | |
| 904 | }) |T| { | |
| 905 | { | |
| 906 | const expected_key_len = switch (T) { | |
| 907 | Aead12Aes128Gcm => 16, | |
| 908 | Aead12Aes256Gcm => 32, | |
| 909 | else => unreachable, | |
| 910 | }; | |
| 911 | try testing.expectEqual(expected_key_len, T.key_len); | |
| 912 | try testing.expectEqual(16, T.auth_tag_len); | |
| 913 | try testing.expectEqual(12, T.nonce_len); | |
| 914 | try testing.expectEqual(4, T.iv_len); | |
| 915 | try testing.expectEqual(29, T.encrypt_overhead); | |
| 916 | } | |
| 917 | } | |
| 918 | } | |
| 919 | ||
| 920 | test "cbc 1.2 encrypt overhead" { | |
| 921 | try testing.expectEqual(85, encrypt_overhead_tls_12); | |
| 922 | ||
| 923 | inline for ([_]type{ | |
| 924 | CbcAes128Sha1, | |
| 925 | CbcAes128Sha256, | |
| 926 | CbcAes256Sha384, | |
| 927 | }) |T| { | |
| 928 | switch (T) { | |
| 929 | CbcAes128Sha1 => { | |
| 930 | try testing.expectEqual(20, T.mac_len); | |
| 931 | try testing.expectEqual(16, T.key_len); | |
| 932 | try testing.expectEqual(57, T.encrypt_overhead); | |
| 933 | }, | |
| 934 | CbcAes128Sha256 => { | |
| 935 | try testing.expectEqual(32, T.mac_len); | |
| 936 | try testing.expectEqual(16, T.key_len); | |
| 937 | try testing.expectEqual(69, T.encrypt_overhead); | |
| 938 | }, | |
| 939 | CbcAes256Sha384 => { | |
| 940 | try testing.expectEqual(48, T.mac_len); | |
| 941 | try testing.expectEqual(32, T.key_len); | |
| 942 | try testing.expectEqual(85, T.encrypt_overhead); | |
| 943 | }, | |
| 944 | else => unreachable, | |
| 945 | } | |
| 946 | try testing.expectEqual(16, T.paddedLength(1)); // cbc block padding | |
| 947 | try testing.expectEqual(16, T.iv_len); | |
| 948 | } | |
| 949 | } | |
| 950 | ||
| 951 | test "overhead tls 1.3" { | |
| 952 | try testing.expectEqual(22, encrypt_overhead_tls_13); | |
| 953 | try expectOverhead(Aes128GcmSha256, 16, 16, 12, 22); | |
| 954 | try expectOverhead(Aes256GcmSha384, 32, 16, 12, 22); | |
| 955 | try expectOverhead(ChaChaSha256, 32, 16, 12, 22); | |
| 956 | try expectOverhead(Aegis128Sha256, 16, 16, 16, 22); | |
| 957 | // and tls 1.2 chacha | |
| 958 | try expectOverhead(Aead12ChaCha, 32, 16, 12, 21); | |
| 959 | } | |
| 960 | ||
| 961 | fn expectOverhead(T: type, key_len: usize, auth_tag_len: usize, nonce_len: usize, overhead: usize) !void { | |
| 962 | try testing.expectEqual(key_len, T.key_len); | |
| 963 | try testing.expectEqual(auth_tag_len, T.auth_tag_len); | |
| 964 | try testing.expectEqual(nonce_len, T.nonce_len); | |
| 965 | try testing.expectEqual(overhead, T.encrypt_overhead); | |
| 966 | } | |
| 967 | ||
| 968 | test "client/server encryption tls 1.3" { | |
| 969 | inline for (cipher_suites.tls13) |cs| { | |
| 970 | var buf: [256]u8 = undefined; | |
| 971 | testu.fill(&buf); | |
| 972 | const secret = Transcript.Secret{ | |
| 973 | .client = buf[0..128], | |
| 974 | .server = buf[128..], | |
| 975 | }; | |
| 976 | var client_cipher = try Cipher.initTls13(cs, secret, .client); | |
| 977 | var server_cipher = try Cipher.initTls13(cs, secret, .server); | |
| 978 | try encryptDecrypt(&client_cipher, &server_cipher); | |
| 979 | ||
| 980 | try client_cipher.keyUpdateEncrypt(); | |
| 981 | try server_cipher.keyUpdateDecrypt(); | |
| 982 | try encryptDecrypt(&client_cipher, &server_cipher); | |
| 983 | ||
| 984 | try client_cipher.keyUpdateDecrypt(); | |
| 985 | try server_cipher.keyUpdateEncrypt(); | |
| 986 | try encryptDecrypt(&client_cipher, &server_cipher); | |
| 987 | } | |
| 988 | } | |
| 989 | ||
| 990 | test "client/server encryption tls 1.2" { | |
| 991 | inline for (cipher_suites.tls12) |cs| { | |
| 992 | var key_material: [256]u8 = undefined; | |
| 993 | testu.fill(&key_material); | |
| 994 | var client_cipher = try Cipher.initTls12(cs, &key_material, .client); | |
| 995 | var server_cipher = try Cipher.initTls12(cs, &key_material, .server); | |
| 996 | try encryptDecrypt(&client_cipher, &server_cipher); | |
| 997 | } | |
| 998 | } | |
| 999 | ||
| 1000 | fn encryptDecrypt(client_cipher: *Cipher, server_cipher: *Cipher) !void { | |
| 1001 | const cleartext = | |
| 1002 | \\ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do | |
| 1003 | \\ eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
| 1004 | ; | |
| 1005 | var buf: [256]u8 = undefined; | |
| 1006 | ||
| 1007 | { // client to server | |
| 1008 | // encrypt | |
| 1009 | const encrypted = try client_cipher.encrypt(&buf, .application_data, cleartext); | |
| 1010 | const expected_encrypted_len = switch (client_cipher.*) { | |
| 1011 | inline else => |f| brk: { | |
| 1012 | const T = @TypeOf(f); | |
| 1013 | break :brk switch (T) { | |
| 1014 | CbcAes128Sha1, | |
| 1015 | CbcAes128Sha256, | |
| 1016 | CbcAes256Sha384, | |
| 1017 | => record.header_len + T.paddedLength(T.iv_len + cleartext.len + T.mac_len), | |
| 1018 | Aead12Aes128Gcm, | |
| 1019 | Aead12Aes256Gcm, | |
| 1020 | Aead12ChaCha, | |
| 1021 | Aes128GcmSha256, | |
| 1022 | Aes256GcmSha384, | |
| 1023 | ChaChaSha256, | |
| 1024 | Aegis128Sha256, | |
| 1025 | => cleartext.len + T.encrypt_overhead, | |
| 1026 | else => unreachable, | |
| 1027 | }; | |
| 1028 | }, | |
| 1029 | }; | |
| 1030 | try testing.expectEqual(client_cipher.recordLen(cleartext.len), encrypted.len); | |
| 1031 | try testing.expectEqual(expected_encrypted_len, encrypted.len); | |
| 1032 | // decrypt | |
| 1033 | const content_type, const decrypted = try server_cipher.decrypt(&buf, Record.init(encrypted)); | |
| 1034 | try testing.expectEqualSlices(u8, cleartext, decrypted); | |
| 1035 | try testing.expectEqual(.application_data, content_type); | |
| 1036 | } | |
| 1037 | // server to client | |
| 1038 | { | |
| 1039 | const encrypted = try server_cipher.encrypt(&buf, .application_data, cleartext); | |
| 1040 | const content_type, const decrypted = try client_cipher.decrypt(&buf, Record.init(encrypted)); | |
| 1041 | try testing.expectEqualSlices(u8, cleartext, decrypted); | |
| 1042 | try testing.expectEqual(.application_data, content_type); | |
| 1043 | } | |
| 1044 | } | |
| 1045 | ||
| 1046 | const data13 = @import("testdata/tls13.zig"); | |
| 1047 | ||
| 1048 | /// Pair of ciphers based on keys from well-known key/iv pairs | |
| 1049 | pub fn testCiphers() struct { Cipher, Cipher } { | |
| 1050 | const client_cipher: Cipher = .{ .AES_256_GCM_SHA384 = .{ | |
| 1051 | .encrypt_secret = undefined, | |
| 1052 | .decrypt_secret = undefined, | |
| 1053 | .encrypt_key = data13.client_application_key, | |
| 1054 | .decrypt_key = data13.server_application_key, | |
| 1055 | .encrypt_iv = data13.client_application_iv, | |
| 1056 | .decrypt_iv = data13.server_application_iv, | |
| 1057 | } }; | |
| 1058 | const server_cipher: Cipher = .{ .AES_256_GCM_SHA384 = .{ | |
| 1059 | .encrypt_secret = undefined, | |
| 1060 | .decrypt_secret = undefined, | |
| 1061 | .encrypt_key = data13.server_application_key, | |
| 1062 | .decrypt_key = data13.client_application_key, | |
| 1063 | .encrypt_iv = data13.server_application_iv, | |
| 1064 | .decrypt_iv = data13.client_application_iv, | |
| 1065 | } }; | |
| 1066 | return .{ client_cipher, server_cipher }; | |
| 1067 | } | |
| 1068 | ||
| 1069 | test testCiphers { | |
| 1070 | var client_cipher, var server_cipher = testCiphers(); | |
| 1071 | var buffer: [1024]u8 = undefined; | |
| 1072 | { | |
| 1073 | const payload = "ping"; | |
| 1074 | const ciphertext = try client_cipher.encrypt(&buffer, .application_data, payload); | |
| 1075 | try testing.expectEqualSlices(u8, &data13.client_ping_wrapped, ciphertext); | |
| 1076 | ||
| 1077 | const content_type, const cleartext = try server_cipher.decrypt(&buffer, record.Record.init(ciphertext)); | |
| 1078 | try testing.expectEqualStrings(payload, cleartext); | |
| 1079 | try testing.expectEqual(.application_data, content_type); | |
| 1080 | } | |
| 1081 | { | |
| 1082 | const close_notify = &proto.Alert.closeNotify(); | |
| 1083 | const ciphertext = try client_cipher.encrypt(&buffer, .alert, close_notify); | |
| 1084 | ||
| 1085 | const content_type, const cleartext = try server_cipher.decrypt(&buffer, record.Record.init(ciphertext)); | |
| 1086 | try testing.expectEqualStrings(close_notify, cleartext); | |
| 1087 | try testing.expectEqual(.alert, content_type); | |
| 1088 | } | |
| 1089 | } | |