Decrypting aes256-ctr

How can I decrypt aes256 in ctr mode?

const std = @import("std");
const crypto = std.crypto;

const algo = crypto.core.aes.Aes256;
const mode = crypto.core.modes.ctr;

pub fn main() !void {
    const key:[32]u8 = .{'a'} ** 32;
    const iv:[16]u8 = .{'b'} ** 16;
    var out:[1024]u8 = undefined;
    var in:[1024]u8 = .{0x42} ** 1024;

    // this is fine
    const ctx_enc = algo.initEnc(key);
    mode(crypto.core.aes.AesEncryptCtx(algo), ctx_enc, &out, &in, iv, std.builtin.Endian.big);

    // this is not
    const ctx_dec = algo.initDec(key);
    mode(crypto.core.aes.AesDecryptCtx(algo), ctx_dec, &in, &out, iv, std.builtin.Endian.big);

}

The first section encrypts fine. The decryption section gives me:

zig/0.13.0/lib/zig/std/crypto/modes.zig:30:25: error: no field or member function named 'xorWide' in 'crypto.aes.soft.AesDecryptCtx(crypto.aes.soft.Aes256)'
            block_cipher.xorWide(parallel_count, dst[i .. i + wide_block_length][0..wide_block_length], src[i .. i + wide_block_length][0..wide_block_length], counters);

I guess xorWide is an optimisation to decrypt many blocks in parallel and the ctr code is trying to use it.

Is it just missing, or should I be doing this differently?

Thanks

Call AesEncryptCtx to decrypt. The same function both encrypts and decrypts.

2 Likes