also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -1,148 +0,0 @@ | ||
| 1 | // This file is originally copied from: https://github.com/jedisct1/zig-cbc. | |
| 2 | // | |
| 3 | // It is modified then to have TLS padding insead of PKCS#7 padding. | |
| 4 | // Reference: | |
| 5 | // https://datatracker.ietf.org/doc/html/rfc5246/#section-6.2.3.2 | |
| 6 | // https://crypto.stackexchange.com/questions/98917/on-the-correctness-of-the-padding-example-of-rfc-5246 | |
| 7 | // | |
| 8 | // If required padding i n bytes | |
| 9 | // PKCS#7 padding is (n...n) | |
| 10 | // TLS padding is (n-1...n-1) - n times of n-1 value | |
| 11 | // | |
| 12 | const std = @import("std"); | |
| 13 | const aes = std.crypto.core.aes; | |
| 14 | const mem = std.mem; | |
| 15 | const debug = std.debug; | |
| 16 | ||
| 17 | /// CBC mode with TLS 1.2 padding | |
| 18 | /// | |
| 19 | /// Important: the counter mode doesn't provide authenticated encryption: the ciphertext can be trivially modified without this being detected. | |
| 20 | /// If you need authenticated encryption, use anything from `std.crypto.aead` instead. | |
| 21 | /// If you really need to use CBC mode, make sure to use a MAC to authenticate the ciphertext. | |
| 22 | pub fn CBC(comptime BlockCipher: anytype) type { | |
| 23 | const EncryptCtx = aes.AesEncryptCtx(BlockCipher); | |
| 24 | const DecryptCtx = aes.AesDecryptCtx(BlockCipher); | |
| 25 | ||
| 26 | return struct { | |
| 27 | const Self = @This(); | |
| 28 | ||
| 29 | enc_ctx: EncryptCtx, | |
| 30 | dec_ctx: DecryptCtx, | |
| 31 | ||
| 32 | /// Initialize the CBC context with the given key. | |
| 33 | pub fn init(key: [BlockCipher.key_bits / 8]u8) Self { | |
| 34 | const enc_ctx = BlockCipher.initEnc(key); | |
| 35 | const dec_ctx = DecryptCtx.initFromEnc(enc_ctx); | |
| 36 | ||
| 37 | return Self{ .enc_ctx = enc_ctx, .dec_ctx = dec_ctx }; | |
| 38 | } | |
| 39 | ||
| 40 | /// Return the length of the ciphertext given the length of the plaintext. | |
| 41 | pub fn paddedLength(length: usize) usize { | |
| 42 | return (std.math.divCeil(usize, length + 1, EncryptCtx.block_length) catch unreachable) * EncryptCtx.block_length; | |
| 43 | } | |
| 44 | ||
| 45 | /// Encrypt the given plaintext for the given IV. | |
| 46 | /// The destination buffer must be large enough to hold the padded plaintext. | |
| 47 | /// Use the `paddedLength()` function to compute the ciphertext size. | |
| 48 | /// IV must be secret and unpredictable. | |
| 49 | pub fn encrypt(self: Self, dst: []u8, src: []const u8, iv: [EncryptCtx.block_length]u8) void { | |
| 50 | // Note: encryption *could* be parallelized, see https://research.kudelskisecurity.com/2022/11/17/some-aes-cbc-encryption-myth-busting/ | |
| 51 | const block_length = EncryptCtx.block_length; | |
| 52 | const padded_length = paddedLength(src.len); | |
| 53 | debug.assert(dst.len == padded_length); // destination buffer must hold the padded plaintext | |
| 54 | var cv = iv; | |
| 55 | var i: usize = 0; | |
| 56 | while (i + block_length <= src.len) : (i += block_length) { | |
| 57 | const in = src[i..][0..block_length]; | |
| 58 | for (cv[0..], in) |*x, y| x.* ^= y; | |
| 59 | self.enc_ctx.encrypt(&cv, &cv); | |
| 60 | @memcpy(dst[i..][0..block_length], &cv); | |
| 61 | } | |
| 62 | // Last block | |
| 63 | var in: [block_length]u8 = @splat(0); | |
| 64 | const padding_length: u8 = @intCast(padded_length - src.len - 1); | |
| 65 | @memset(&in, padding_length); | |
| 66 | @memcpy(in[0 .. src.len - i], src[i..]); | |
| 67 | for (cv[0..], in) |*x, y| x.* ^= y; | |
| 68 | self.enc_ctx.encrypt(&cv, &cv); | |
| 69 | @memcpy(dst[i..], cv[0 .. dst.len - i]); | |
| 70 | } | |
| 71 | ||
| 72 | /// Decrypt the given ciphertext for the given IV. | |
| 73 | /// The destination buffer must be large enough to hold the plaintext. | |
| 74 | /// IV must be secret, unpredictable and match the one used for encryption. | |
| 75 | pub fn decrypt(self: Self, dst: []u8, src: []const u8, iv: [DecryptCtx.block_length]u8) !void { | |
| 76 | const block_length = DecryptCtx.block_length; | |
| 77 | if (src.len != dst.len) { | |
| 78 | return error.EncodingError; | |
| 79 | } | |
| 80 | debug.assert(src.len % block_length == 0); | |
| 81 | var i: usize = 0; | |
| 82 | var cv = iv; | |
| 83 | var out: [block_length]u8 = undefined; | |
| 84 | // Decryption could be parallelized | |
| 85 | while (i + block_length <= dst.len) : (i += block_length) { | |
| 86 | const in = src[i..][0..block_length]; | |
| 87 | self.dec_ctx.decrypt(&out, in); | |
| 88 | for (&out, cv) |*x, y| x.* ^= y; | |
| 89 | cv = in.*; | |
| 90 | @memcpy(dst[i..][0..block_length], &out); | |
| 91 | } | |
| 92 | // Last block - We intentionally don't check the padding to mitigate timing attacks | |
| 93 | if (i < dst.len) { | |
| 94 | const in = src[i..][0..block_length]; | |
| 95 | @memset(&out, 0); | |
| 96 | self.dec_ctx.decrypt(&out, in); | |
| 97 | for (&out, cv) |*x, y| x.* ^= y; | |
| 98 | @memcpy(dst[i..], out[0 .. dst.len - i]); | |
| 99 | } | |
| 100 | } | |
| 101 | }; | |
| 102 | } | |
| 103 | ||
| 104 | test "CBC mode" { | |
| 105 | const M = CBC(aes.Aes128); | |
| 106 | const key = [_]u8{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; | |
| 107 | const iv = [_]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; | |
| 108 | const src_ = "This is a test of AES-CBC that goes on longer than a couple blocks. It is a somewhat long test case to type out!"; | |
| 109 | const expected = "\xA0\x8C\x09\x7D\xFF\x42\xB6\x65\x4D\x4B\xC6\x90\x90\x39\xDE\x3D\xC7\xCA\xEB\xF6\x9A\x4F\x09\x97\xC9\x32\xAB\x75\x88\xB7\x57\x17"; | |
| 110 | var res: [32]u8 = undefined; | |
| 111 | ||
| 112 | try comptime std.testing.expect(src_.len / M.paddedLength(1) >= 3); // Ensure that we have at least 3 blocks | |
| 113 | ||
| 114 | const z = M.init(key); | |
| 115 | ||
| 116 | // Test encryption and decryption with distinct buffers | |
| 117 | var h = std.crypto.hash.sha2.Sha256.init(.{}); | |
| 118 | inline for (0..src_.len) |len| { | |
| 119 | const src = src_[0..len]; | |
| 120 | var dst: [M.paddedLength(src.len)]u8 = @splat(0); | |
| 121 | ||
| 122 | z.encrypt(&dst, src, iv); | |
| 123 | h.update(&dst); | |
| 124 | ||
| 125 | var decrypted: [dst.len]u8 = @splat(0); | |
| 126 | try z.decrypt(&decrypted, &dst, iv); | |
| 127 | ||
| 128 | const padding = decrypted[decrypted.len - 1] + 1; | |
| 129 | try std.testing.expectEqualSlices(u8, src, decrypted[0 .. decrypted.len - padding]); | |
| 130 | } | |
| 131 | h.final(&res); | |
| 132 | try std.testing.expectEqualSlices(u8, expected, &res); | |
| 133 | ||
| 134 | // Test encryption and decryption with the same buffer | |
| 135 | h = std.crypto.hash.sha2.Sha256.init(.{}); | |
| 136 | inline for (0..src_.len) |len| { | |
| 137 | var buf: [M.paddedLength(len)]u8 = @splat(0); | |
| 138 | @memcpy(buf[0..len], src_[0..len]); | |
| 139 | z.encrypt(&buf, buf[0..len], iv); | |
| 140 | h.update(&buf); | |
| 141 | ||
| 142 | try z.decrypt(&buf, &buf, iv); | |
| 143 | ||
| 144 | try std.testing.expectEqualSlices(u8, src_[0..len], buf[0..len]); | |
| 145 | } | |
| 146 | h.final(&res); | |
| 147 | try std.testing.expectEqualSlices(u8, expected, &res); | |
| 148 | } | |