also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -1,141 +0,0 @@ | ||
| 1 | //! Kernel structs and constants from: /usr/include/linux/tls.h or | |
| 2 | //! https://github.com/torvalds/linux/blob/master/include/uapi/linux/tls.h | |
| 3 | ||
| 4 | const std = @import("std"); | |
| 5 | const mem = std.mem; | |
| 6 | const Cipher = @import("cipher.zig").Cipher; | |
| 7 | ||
| 8 | const Ktls = @This(); | |
| 9 | ||
| 10 | const U = union(enum) { | |
| 11 | aes_gcm_128: AesGcm128, | |
| 12 | aes_gcm_256: AesGcm256, | |
| 13 | chacha20_poly1305: Chacha20Poly1305, | |
| 14 | }; | |
| 15 | ||
| 16 | tx: U, | |
| 17 | rx: U, | |
| 18 | ||
| 19 | pub fn txBytes(k: *Ktls) []const u8 { | |
| 20 | return switch (k.tx) { | |
| 21 | inline else => |*v| mem.asBytes(v), | |
| 22 | }; | |
| 23 | } | |
| 24 | ||
| 25 | pub fn rxBytes(k: *Ktls) []const u8 { | |
| 26 | return switch (k.rx) { | |
| 27 | inline else => |*v| mem.asBytes(v), | |
| 28 | }; | |
| 29 | } | |
| 30 | ||
| 31 | pub const VERSION_1_2 = 0x0303; | |
| 32 | pub const VERSION_1_3 = 0x0304; | |
| 33 | pub const TX = 1; | |
| 34 | pub const RX = 2; | |
| 35 | ||
| 36 | pub const AES_GCM_128 = 51; | |
| 37 | pub const AES_GCM_256 = 52; | |
| 38 | pub const CHACHA20_POLY1305 = 54; | |
| 39 | ||
| 40 | pub const Info = extern struct { | |
| 41 | version: u16 = VERSION_1_3, | |
| 42 | cipher_type: u16 = mem.zeroes(u16), | |
| 43 | }; | |
| 44 | pub const AesGcm128 = extern struct { | |
| 45 | info: Info = mem.zeroes(Info), | |
| 46 | iv: [8]u8 = mem.zeroes([8]u8), | |
| 47 | key: [16]u8 = mem.zeroes([16]u8), | |
| 48 | salt: [4]u8 = mem.zeroes([4]u8), | |
| 49 | rec_seq: [8]u8 = mem.zeroes([8]u8), | |
| 50 | }; | |
| 51 | pub const AesGcm256 = extern struct { | |
| 52 | info: Info = mem.zeroes(Info), | |
| 53 | iv: [8]u8 = mem.zeroes([8]u8), | |
| 54 | key: [32]u8 = mem.zeroes([32]u8), | |
| 55 | salt: [4]u8 = mem.zeroes([4]u8), | |
| 56 | rec_seq: [8]u8 = mem.zeroes([8]u8), | |
| 57 | }; | |
| 58 | pub const Chacha20Poly1305 = extern struct { | |
| 59 | info: Info = mem.zeroes(Info), | |
| 60 | iv: [12]u8 = mem.zeroes([12]u8), | |
| 61 | key: [32]u8 = mem.zeroes([32]u8), | |
| 62 | salt: [0]u8 = mem.zeroes([0]u8), | |
| 63 | rec_seq: [8]u8 = mem.zeroes([8]u8), | |
| 64 | }; | |
| 65 | ||
| 66 | pub fn init(cipher: Cipher) Ktls { | |
| 67 | switch (cipher) { | |
| 68 | // TLS 1.3 ciphers | |
| 69 | .AES_128_GCM_SHA256 => |keys| { | |
| 70 | const k = makeKeys(AesGcm128, .{ .cipher_type = AES_GCM_128 }, keys); | |
| 71 | return .{ | |
| 72 | .tx = .{ .aes_gcm_128 = k.tx }, | |
| 73 | .rx = .{ .aes_gcm_128 = k.rx }, | |
| 74 | }; | |
| 75 | }, | |
| 76 | .AES_256_GCM_SHA384 => |keys| { | |
| 77 | const k = makeKeys(AesGcm256, .{ .cipher_type = AES_GCM_256 }, keys); | |
| 78 | return .{ | |
| 79 | .tx = .{ .aes_gcm_256 = k.tx }, | |
| 80 | .rx = .{ .aes_gcm_256 = k.rx }, | |
| 81 | }; | |
| 82 | }, | |
| 83 | .CHACHA20_POLY1305_SHA256 => |keys| { | |
| 84 | const k = makeKeys(Chacha20Poly1305, .{ .cipher_type = CHACHA20_POLY1305 }, keys); | |
| 85 | return .{ | |
| 86 | .tx = .{ .chacha20_poly1305 = k.tx }, | |
| 87 | .rx = .{ .chacha20_poly1305 = k.rx }, | |
| 88 | }; | |
| 89 | }, | |
| 90 | ||
| 91 | // TLS 1.2 ciphers | |
| 92 | .ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | |
| 93 | .ECDHE_RSA_WITH_AES_128_GCM_SHA256, | |
| 94 | => |keys| { | |
| 95 | const k = makeKeys(AesGcm128, .{ .cipher_type = AES_GCM_128, .version = VERSION_1_2 }, keys); | |
| 96 | return .{ | |
| 97 | .tx = .{ .aes_gcm_128 = k.tx }, | |
| 98 | .rx = .{ .aes_gcm_128 = k.rx }, | |
| 99 | }; | |
| 100 | }, | |
| 101 | .ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | |
| 102 | .ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
| 103 | => |keys| { | |
| 104 | const k = makeKeys(AesGcm256, .{ .cipher_type = AES_GCM_256, .version = VERSION_1_2 }, keys); | |
| 105 | return .{ | |
| 106 | .tx = .{ .aes_gcm_256 = k.tx }, | |
| 107 | .rx = .{ .aes_gcm_256 = k.rx }, | |
| 108 | }; | |
| 109 | }, | |
| 110 | .ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 111 | .ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, | |
| 112 | => |keys| { | |
| 113 | const k = makeKeys(Chacha20Poly1305, .{ .cipher_type = CHACHA20_POLY1305, .version = VERSION_1_2 }, keys); | |
| 114 | return .{ | |
| 115 | .tx = .{ .chacha20_poly1305 = k.tx }, | |
| 116 | .rx = .{ .chacha20_poly1305 = k.rx }, | |
| 117 | }; | |
| 118 | }, | |
| 119 | ||
| 120 | else => unreachable, | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | fn makeKeys(comptime T: type, info: Info, keys: anytype) struct { tx: T, rx: T } { | |
| 125 | return .{ | |
| 126 | .tx = makeKey(T, info, keys.encrypt_iv, keys.encrypt_key, keys.encrypt_seq), | |
| 127 | .rx = makeKey(T, info, keys.decrypt_iv, keys.decrypt_key, keys.decrypt_seq), | |
| 128 | }; | |
| 129 | } | |
| 130 | ||
| 131 | fn makeKey(comptime T: type, info: Info, iv: anytype, key: anytype, seq: u64) T { | |
| 132 | const salt_size = @sizeOf(@FieldType(T, "salt")); | |
| 133 | var t: T = .{ | |
| 134 | .info = info, | |
| 135 | .salt = iv[0..salt_size].*, | |
| 136 | .iv = if (iv.len > salt_size) iv[salt_size..].* else @splat(0), | |
| 137 | .key = key, | |
| 138 | }; | |
| 139 | mem.writeInt(u64, &t.rec_seq, seq, .big); | |
| 140 | return t; | |
| 141 | } | |