Initial work. Added some tests for big nums in rust and OpenCL.

This commit is contained in:
Kirill Shakirov
2026-01-11 07:03:09 +01:00
parent 8edbc09493
commit 194f4e734b
16 changed files with 2410 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
def main():
start_num = 0x8adb7b7e8a722df091ecea988a4ad2234836636a102ceb688b3985f89bf40002
num1 = start_num+1
num2 = start_num+312
print(start_num.to_bytes(32).hex())
print(num1.to_bytes(32).hex())
print(num2.to_bytes(32).hex())
if __name__ == '__main__':
main()
+255
View File
@@ -0,0 +1,255 @@
extern crate ocl;
use ocl::{
Buffer, Context, Device, DeviceType, Kernel, Platform, Program, Queue, SpatialDims, flags,
};
use std::io;
mod num_utils;
/// Exploded version. Boom!
///
/// The functions above use `ProQue` and other abstractions to greatly reduce
/// the amount of boilerplate and configuration necessary to do basic work.
/// Many tasks, however, will require more configuration and will necessitate
/// doing away with `ProQue` altogether. Enqueuing kernels and reading/writing
/// from buffers and images usually requires a more explicit interface.
///
/// The following function performs the exact same steps that the above
/// functions did, with many of the convenience abstractions peeled away.
///
/// See the function below this to take things a step deeper...
///
// trait FromStr {
// fn from_str(&self);
// }
// // Define a trait with a constructor method
// trait NewFile {
// fn new<P: AsRef<Path>>(path: P) -> std::io::Result<Self> where Self: Sized;
// }
// impl Foo for ocl::flags::DeviceType {
// fn foo(&self) {
// println!("foo");
// }
// }
fn dev_type_from_str(s: &str) -> Result<flags::DeviceType, ()> {
match s {
"CPU" => Ok(flags::DeviceType::CPU),
"GPU" => Ok(flags::DeviceType::GPU),
"ALL" => Ok(flags::DeviceType::ALL),
"CUSTOM" => Ok(flags::DeviceType::CUSTOM),
"ACCELERATOR" => Ok(flags::DeviceType::ACCELERATOR),
"DEFAULT" => Ok(flags::DeviceType::DEFAULT),
_ => Err(()),
}
}
fn str_or_empty(r: ocl::error::Result<String>) -> String {
match r {
Ok(s) => s,
Err(_) => "".to_string(),
}
}
fn print_devices(dev_list: &Vec<(Device, Platform)>) {
let mut i = 0;
for (dev, plt) in dev_list.iter() {
let dev_name = str_or_empty(dev.name());
let plt_name = str_or_empty(plt.name());
println!("({i}) device: \"{dev_name}\" ----- platorm: \"{plt_name}\"");
i += 1;
}
}
fn choose_devices(devices_num: usize) -> Result<Vec<usize>, String> {
println!("Please input desired devices to use as a white space separated list of numbers.");
let mut result: Vec<usize> = Vec::new();
let mut s_devs_nums = String::new();
io::stdin()
.read_line(&mut s_devs_nums)
.expect("Failed to read line");
for s_dev_num in s_devs_nums.split(' ') {
let dev_num: usize = match s_dev_num.trim().parse() {
Ok(num) => num,
Err(_) => return Err("You must input a number from device list.".to_string()),
};
if dev_num >= devices_num {
return Err("You must input a number from device list.".to_string());
};
result.push(dev_num);
}
return Ok(result);
}
fn list_devices(dev_type: DeviceType) -> Vec<(Device, Platform)> {
let platforms = Platform::list();
let mut devices: Vec<(Device, Platform)> = Vec::new();
for plt in platforms.iter() {
//let plat_name = str_or_empty(plt.name());
let list_res = Device::list(plt, Some(dev_type));
match list_res {
Ok(dev_l) => devices.extend(dev_l.iter().map(|dev| (*dev, plt.clone()))),
Err(_) => {}
}
}
return devices;
}
struct CtxBuffers {
start_key: Buffer<u32>,
u_data: Buffer<u32>,
enc_data: Buffer<u32>,
key_found: Buffer<u32>,
}
struct DevConfig {
train_work_size: bool,
global_work_size: SpatialDims,
}
struct ExecContext {
cfg: DevConfig,
ctx: Context,
kernel: Kernel,
prog: Program,
queue: Queue,
buffers: CtxBuffers,
}
fn init_devices(
devices: Vec<(Device, Platform, DevConfig)>,
kern_name: String,
prog_src: String,
inc_dirs: Vec<String>,
) -> Vec<ExecContext> {
let mut contexts: Vec<ExecContext> = Vec::with_capacity(devices.len());
for (dev, plt, dev_cfg) in devices {
let ctx = match Context::builder()
.platform(plt)
.devices(dev.clone())
.build()
{
Ok(c) => c,
Err(_) => continue,
};
let prg = match Program::builder().devices(dev).src(&prog_src).build(&ctx) {
Ok(p) => p,
Err(_) => continue,
};
let queue = match Queue::new(&ctx, dev, None) {
Ok(q) => q,
Err(_) => continue,
};
// Create Buffers:
let start_key_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(8)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
let u_data_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(4)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
let enc_data_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(4)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
let key_found_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_WRITE_ONLY)
.len(1)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
// (3) Create a kernel with arguments matching those in the source above:
let kernel = match Kernel::builder()
.program(&prg)
.name(&kern_name)
.queue(queue.clone())
.global_work_size(dev_cfg.global_work_size)
.arg(&start_key_b)
.arg(&u_data_b)
.arg(&enc_data_b)
.arg(&key_found_b)
.build() {
Ok(kern) => kern,
Err(_) => continue,
};
contexts.push(ExecContext {
cfg: dev_cfg,
ctx: ctx,
kernel: kernel,
prog: prg,
queue: queue,
buffers: CtxBuffers {
start_key: start_key_b,
u_data: u_data_b,
enc_data: enc_data_b,
key_found: key_found_b,
},
});
}
return contexts;
}
fn main() {
println!("Hello, world nya!");
//use ocl::{Buffer, Context, Device, Kernel, Platform, Program, Queue, flags};
let dev_type = dev_type_from_str("GPU").expect("pur");
// Get devices to be used for key search
let mut all_devices: Vec<(Device, Platform)> = list_devices(dev_type);
let devs_nums = loop {
print_devices(&all_devices);
match choose_devices(all_devices.len()) {
Ok(value) => break value,
Err(exc) => {
println!("Error! {exc}\n")
}
}
};
let devices: Vec<(Device, Platform)> = devs_nums.iter().map(|&i| all_devices[i]).collect();
all_devices.clear();
println!("{:?}", devices);
// let devices: Vec<_> = platforms.iter().flat_map(|p| Device::list(p, Some(dev_type)).iter()).collect();
// let device = Device::first(platform)?;
// let context = Context::builder()
// .platform(platform)
// .devices(device.clone())
// .build()?;
}
+267
View File
@@ -0,0 +1,267 @@
// union U64orU32 {
// l: u64,
// i: [u32; 2],
// }
fn add_u32_to_u256(a: &[u32; 8], b: u32) -> ([u32; 8], bool) {
let mut res: [u32; 8] = [0; 8];
let mut carry = false;
(res[0], carry) = a[0].carrying_add(b, carry);
for idx in 1..8 {
(res[idx], carry) = a[idx].carrying_add(0, carry);
}
return (res, carry);
}
fn add_u32_to_u256_(a: &mut [u32; 8], b: u32) -> bool {
let mut carry = false;
(a[0], carry) = a[0].carrying_add(b, carry);
for idx in 1..8 {
(a[idx], carry) = a[idx].carrying_add(0, carry);
}
return carry;
}
fn bytes_from_chars(chars_chunk: &[char]) -> [u8; 4] {
let mut res: [u8; 4] = [0; 4];
let mut idx: usize = 0;
chars_chunk.chunks_exact(2).for_each(|b_c| {
if idx < 4 {
match u8::from_str_radix(&b_c.iter().collect::<String>(), 16) {
Ok(n) => res[idx] = n,
Err(_) => (),
}
idx += 1;
}
});
return res;
}
fn bignum_from_hex(hex: &str) -> [u32; 8] {
let mut res: [u32; 8] = [0; 8];
let mut idx: usize = 0;
let chars_hex = hex.chars().collect::<Vec<char>>();
chars_hex
.chunks_exact(8)
.rev()
.map(|chunk| bytes_from_chars(chunk))
.for_each(|b_arr| {
if idx < 8 {
res[idx] = u32::from_be_bytes(b_arr);
idx += 1;
}
});
return res;
}
fn hex_fmt_byte(n: u32) -> String {
let res: String = n
.to_be_bytes()
.iter()
.map(|b| format!("{:02x}", b))
.collect();
return res;
}
fn bignum_to_hex(a: &[u32; 8]) -> String {
let res: String = a
.iter()
.rev()
.map(|n| hex_fmt_byte(*n))
.collect::<Vec<String>>()
.join("");
return res;
}
#[cfg(test)]
mod num_utils_tests {
use std::io::Read;
use super::*;
#[test]
fn test_add() {
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py";
let mut child = Command::new(test_gen_cmd)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let gen_stdout = child
.stdout
.take()
.ok_or("Failed to capture stdout")
.unwrap();
let gen_reader = BufReader::new(gen_stdout);
for r_line in gen_reader.lines() {
let test_line: String = r_line.unwrap(); // Handle any I/O errors
let test_data_line = test_line.split(' ').collect::<Vec<&str>>();
let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap();
let t0 = bignum_from_hex(test_data_line[1]);
let t1_test = add_u32_to_u256(&t0, 1).0;
let t2_test = add_u32_to_u256(&t0, num_to_add).0;
let res_actual = format!(
"{} {} {} {}",
num_to_add,
bignum_to_hex(&t0),
bignum_to_hex(&t1_test),
bignum_to_hex(&t2_test)
);
assert_eq!(test_line, res_actual);
}
let _ = child.wait().unwrap();
}
#[test]
fn test_cl_add() {
extern crate ocl;
use ocl::{
Buffer, Context, Device, DeviceType, Kernel, Platform, Program, Queue, SpatialDims,
flags,
};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
let cl_test_path = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl/test_num_utils.cl";
let cl_include_opt =
"-I /home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl";
let mut cl_src = String::new();
// read ocl source
BufReader::new(File::open(cl_test_path).unwrap()).read_to_string(&mut cl_src);
const G_WORK_SIZE: usize = 4096;
let cl_platform = Platform::default();
let cl_device = Device::first(cl_platform).unwrap();
let cl_context = Context::builder()
.platform(cl_platform)
.devices(cl_device.clone())
.build()
.unwrap();
let cl_program = Program::builder()
.devices(cl_device)
.src(cl_src)
.cmplr_opt(cl_include_opt)
.build(&cl_context)
.unwrap();
let cl_queue = Queue::new(&cl_context, cl_device, None).unwrap();
let cl_buffer_num = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(G_WORK_SIZE)
.fill_val(0u32)
.build()
.unwrap();
let cl_buffer_t0 = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(G_WORK_SIZE * 8)
.fill_val(0u32)
.build()
.unwrap();
let cl_buffer_t1 = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_WRITE_ONLY)
.len(G_WORK_SIZE * 8)
.fill_val(0u32)
.build()
.unwrap();
let cl_buffer_t2 = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_WRITE_ONLY)
.len(G_WORK_SIZE * 8)
.fill_val(0u32)
.build()
.unwrap();
// (3) Create a kernel with arguments matching those in the source above:
let kernel = Kernel::builder()
.program(&cl_program)
.name("test_add")
.queue(cl_queue.clone())
.global_work_size(G_WORK_SIZE)
.arg(&cl_buffer_num)
.arg(&cl_buffer_t0)
.arg(&cl_buffer_t1)
.arg(&cl_buffer_t2)
.build()
.unwrap();
let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py";
let mut child = Command::new(test_gen_cmd)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let gen_stdout = child
.stdout
.take()
.ok_or("Failed to capture stdout")
.unwrap();
let gen_reader = BufReader::new(gen_stdout);
let mut buffer_num: Vec<u32> = vec![0u32; G_WORK_SIZE];
let mut buffer_t0: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut exp_buffer_t1: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut exp_buffer_t2: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut act_buffer_t1: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut act_buffer_t2: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut w_id: usize = 0;
for r_line in gen_reader.lines() {
let test_line: String = r_line.unwrap(); // Handle any I/O errors
let test_data_line = test_line.split(' ').collect::<Vec<&str>>();
let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap();
buffer_num[w_id] = num_to_add;
let slise_id = w_id*8;
buffer_t0[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[1]));
exp_buffer_t1[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[2]));
exp_buffer_t2[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[3]));
w_id += 1;
if w_id >= G_WORK_SIZE {
w_id = 0; // reset counter
cl_buffer_num.cmd().queue(&cl_queue).offset(0).write(&buffer_num).enq().unwrap();
cl_buffer_t0.cmd().queue(&cl_queue).offset(0).write(&buffer_t0).enq().unwrap();
// (4) Run the kernel
unsafe {
kernel
.cmd()
.queue(&cl_queue)
.global_work_size(G_WORK_SIZE)
.enq().unwrap();
}
cl_buffer_t1.cmd().queue(&cl_queue).offset(0).read(&mut act_buffer_t1).enq().unwrap();
cl_buffer_t2.cmd().queue(&cl_queue).offset(0).read(&mut act_buffer_t2).enq().unwrap();
assert_eq!(exp_buffer_t1, act_buffer_t1);
assert_eq!(exp_buffer_t2, act_buffer_t2);
}
}
let _ = child.wait().unwrap();
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,29 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
inline void aes128_InvertKey (uint *ks);
inline void aes128_ExpandKey (uint *ks, const uint *ukey);
inline void aes128_set_encrypt_key (uint *ks, const uint *ukey);
inline void aes128_set_decrypt_key (uint *ks, const uint *ukey);
inline void aes128_encrypt (const uint *ks, const uint *in, uint *out);
inline void aes128_decrypt (const uint *ks, const uint *in, uint *out);
inline void xts_mul2 (uint *in, uint *out);
inline void aes_xts256_gen_tweak (const uint *ks, const uint *sec_n, const uint block_n, uint *out);
inline void aes_xts256_enc_block (const uint *ks, const uint *T, const uint *in, uint *out);
inline void aes_xts256_dec_block (const uint *ks, const uint *T, const uint *in, uint *out);
+174
View File
@@ -0,0 +1,174 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
typedef struct __attribute__((aligned(32))) {
uint carry;
uint carry4;
} bigintRes;
typedef union {
ulong l;
uint i[2];
} __attribute__((aligned(32))) ul_ui_union;
inline uint add_one_to_bigint4_(uint *_n)
{
ul_ui_union t;
t.l = (ulong)_n[0] + 1ul;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
return t.i[1];
}
inline uint add_uint_to_bigint4_ (uint* _n, const uint b)
{
ul_ui_union t;
t.l = (ulong)_n[0] + (ulong)b;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
return t.i[1];
}
inline bigintRes add_one_to_bigint8(const uint *n, uint *out)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)n[0] + 1ul;
out[0] = t.i[0];
t.l = (ulong)n[1] + (ulong)t.i[1];
out[1] = t.i[0];
t.l = (ulong)n[2] + (ulong)t.i[1];
out[2] = t.i[0];
t.l = (ulong)n[3] + (ulong)t.i[1];
out[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)n[4] + (ulong)t.i[1];
out[4] = t.i[0];
t.l = (ulong)n[5] + (ulong)t.i[1];
out[5] = t.i[0];
t.l = (ulong)n[6] + (ulong)t.i[1];
out[6] = t.i[0];
t.l = (ulong)n[7] + (ulong)t.i[1];
out[7] = t.i[0];
res.carry = t.i[1];
return res;
}
inline bigintRes add_one_to_bigint8_(uint *_n)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)_n[0] + 1ul;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)_n[4] + (ulong)t.i[1];
_n[4] = t.i[0];
t.l = (ulong)_n[5] + (ulong)t.i[1];
_n[5] = t.i[0];
t.l = (ulong)_n[6] + (ulong)t.i[1];
_n[6] = t.i[0];
t.l = (ulong)_n[7] + (ulong)t.i[1];
_n[7] = t.i[0];
res.carry = t.i[1];
return res;
}
inline bigintRes add_uint_to_bigint8 (const uint *n, const uint b, uint *out)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)n[0] + (ulong)b;
out[0] = t.i[0];
t.l = (ulong)n[1] + (ulong)t.i[1];
out[1] = t.i[0];
t.l = (ulong)n[2] + (ulong)t.i[1];
out[2] = t.i[0];
t.l = (ulong)n[3] + (ulong)t.i[1];
out[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)n[4] + (ulong)t.i[1];
out[4] = t.i[0];
t.l = (ulong)n[5] + (ulong)t.i[1];
out[5] = t.i[0];
t.l = (ulong)n[6] + (ulong)t.i[1];
out[6] = t.i[0];
t.l = (ulong)n[7] + (ulong)t.i[1];
out[7] = t.i[0];
res.carry = t.i[1];
return res;
}
inline bigintRes add_uint_to_bigint8_ (uint* _n, const uint b)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)_n[0] + (ulong)b;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)_n[4] + (ulong)t.i[1];
_n[4] = t.i[0];
t.l = (ulong)_n[5] + (ulong)t.i[1];
_n[5] = t.i[0];
t.l = (ulong)_n[6] + (ulong)t.i[1];
_n[6] = t.i[0];
t.l = (ulong)_n[7] + (ulong)t.i[1];
_n[7] = t.i[0];
res.carry = t.i[1];
return res;
}
@@ -0,0 +1,101 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "aes256_xts_plain.cl"
#include "num_utils.cl"
// batch_size uint - batch size
// Ti - sector index ulong
// Tj - encryption block number (16 bytes blocks)
// Tk - tweak key uint[4]
// s_Dk - start of data key uint[4]
// t_e_d - target_enc_data uint[4]
// u_d - unencrypted data to be encrypted uint[4]
// g_key_found uint[9] - 0 element - flag that sets to 1 if key found.
// Other 8 elements is found key
__kernel void search_key_test(const uint batch_size, const ulong g_Ti, const uint g_Tj,
__global const uint8* g_start_enc_key,
__global const uint4* g_uenc_data,
__global const uint4* g_target_data,
__global uint* g_key_found)
{
const uint g_id = get_global_id(0);
uint enc_key[8];
uint tweak[4];
uint uenc_data[4];
uint4 target_data = *g_target_data;
uint4 enc_data = (uint4)(0);
uint d_ks[44]; // data expanded key
uint t_ks[44]; // tweak expanded key
uint sec_n[4] = {0};
sec_n[0] = ((uint*)&g_Ti)[0];
sec_n[1] = ((uint*)&g_Ti)[1];
uint Tj = g_Tj;
vstore4(*g_uenc_data, 0, uenc_data);
vstore8(*g_start_enc_key, 0, enc_key);
// Set initial start key for every work thread
uint k_data_carry = add_uint_to_bigint4_ (enc_key, (g_id*batch_size));
uint k_tweak_carry = add_one_to_bigint4_ (&enc_key[4]);
if (k_tweak_carry != 0u) return; // if reached max key value exit thread
// Generate tweak
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
for (uint batch_id = 0u; (batch_id < batch_size); batch_id++)
{
// Data encrypt key always changing because we increment from 0 index to 8
aes128_set_encrypt_key (d_ks, enc_key);
// encrypt data
aes_xts256_enc_block (d_ks, tweak, uenc_data, (uint*)&enc_data);
// check if we found the key!
if (all(enc_data==target_data))
{
g_key_found[0] = 1;
vstore8(vload8(0, enc_key), 0, &g_key_found[1]);
return;
}
// Increment data key part by 1.
k_data_carry = add_one_to_bigint4_ (enc_key);
// Tweak changes only once in 2^128 times
if (k_data_carry != 0u) {
// Increment tweak part
k_tweak_carry = add_one_to_bigint4_ (&enc_key[4]);
if (k_tweak_carry != 0u) return; // if reached max key value exit thread
// Gen new tweak
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
}
}
}
@@ -0,0 +1,98 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "aes256_xts_plain.cl"
__kernel void encrypt_data(__global const ulong* g_Ti, __global const uint* g_Tj,
__global const uint8* g_key,
__global const uint4* g_u_data,
__global uint* g_enc_data)
{
const size_t g_id = get_global_id(0);
uint d_ks[44];
uint t_ks[44];
uint tweak[4];
uint enc_key[8];
uint u_data[4];
uint enc_data[4] = { 0 };
uint sec_n[4];
ulong Ti = g_Ti[g_id];
sec_n[0] = ((uint*)&Ti)[0];
sec_n[1] = ((uint*)&Ti)[1];
sec_n[2] = 0;
sec_n[3] = 0;
uint Tj = g_Tj[g_id];
vstore8(*g_key, 0, enc_key);
vstore4(g_u_data[g_id], 0, u_data);
// printf("Ti: %lu\\n", Ti);
// printf("Tj: %u\\n", Tj);
// printf("enc_key: %v8u\\n", *(uint8*)enc_key);
// printf("uenc_data: %v4u\\n", *(uint4*)uenc_data);
//calculate tweak value
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
// encrypt data
aes128_set_encrypt_key (d_ks, enc_key);
aes_xts256_enc_block (d_ks, tweak, u_data, enc_data);
// printf("enc_data: %v4u\\n", *(uint4*)enc_data);
vstore4(*(uint4*)enc_data, g_id, g_enc_data);
}
__kernel void decrypt_data(__global const ulong* g_Ti, __global const uint* g_Tj,
__global const uint8* g_key,
__global const uint4* g_enc_data,
__global uint* g_u_data)
{
const size_t g_id = get_global_id(0);
uint d_ks[44];
uint t_ks[44];
uint tweak[4];
uint enc_key[8];
uint enc_data[4];
uint u_data[4] = { 0 };
uint sec_n[4];
ulong Ti = g_Ti[g_id];
sec_n[0] = ((uint*)&Ti)[0];
sec_n[1] = ((uint*)&Ti)[1];
sec_n[2] = 0;
sec_n[3] = 0;
uint Tj = g_Tj[g_id];
vstore8(*g_key, 0, enc_key);
vstore4(g_enc_data[g_id], 0, enc_data);
//calculate tweak value
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
// decrypt data
aes128_set_decrypt_key (d_ks, enc_key);
aes_xts256_dec_block (d_ks, tweak, enc_data, u_data);
// printf("enc_data: %v4u\\n", *(uint4*)enc_data);
vstore4(*(uint4*)u_data, g_id, g_u_data);
}
@@ -0,0 +1,27 @@
#include "num_utils.cl"
__kernel void test_add(__global const uint* g_num_to_add,
__global const uint8* g_t0,
__global uint* g_t1,
__global uint* g_t2)
{
const size_t g_id = get_global_id(0);
uint t0[8] = {0};
uint t1[8] = {0};
uint t2[8] = {0};
uint num_to_add = g_num_to_add[g_id];
vstore8(g_t0[g_id], 0, t0);
add_one_to_bigint8 (t0, t1);
add_uint_to_bigint8 (t0, num_to_add, t2);
// save results
vstore8(*(uint8*)t1, 0, &g_t1[g_id*8]);
vstore8(*(uint8*)t2, 0, &g_t2[g_id*8]);
}
+50
View File
@@ -0,0 +1,50 @@
#[cfg(test)]
mod cl_num_utils_tests {
use super::*;
#[test]
fn test_add() {
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py";
let mut child = Command::new(test_gen_cmd)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let gen_stdout = child
.stdout
.take()
.ok_or("Failed to capture stdout")
.unwrap();
let gen_reader = BufReader::new(gen_stdout);
for r_line in gen_reader.lines() {
let test_line: String = r_line.unwrap(); // Handle any I/O errors
let test_data_line = test_line.split(' ').collect::<Vec<&str>>();
let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap();
let t0 = bignum_from_hex(test_data_line[1]);
let t1_test = add_u32_to_u256(&t0, 1).0;
let t2_test = add_u32_to_u256(&t0, num_to_add).0;
let res_actual = format!(
"{} {} {} {}",
num_to_add,
bignum_to_hex(&t0),
bignum_to_hex(&t1_test),
bignum_to_hex(&t2_test)
);
assert_eq!(test_line, res_actual);
}
let _ = child.wait().unwrap();
}
}
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/python
import random
def main(iters: int):
for i in range(iters):
rand_u32 = random.randint(1,0xffffffff)
t0 = int.from_bytes(random.randbytes(32))
t1 = t0 + 1
t2 = t0 + rand_u32
print(f"{rand_u32} {t0.to_bytes(32).hex()} {t1.to_bytes(32).hex()} {t2.to_bytes(32).hex()}")
if __name__ == '__main__':
main(1000000)