Skip to main content

cryptocol/symmetric/
trait_ofb_impl_for_rijndael.rs

1// Copyright 2025 PARK Youngho.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed
7// except according to those terms.
8
9
10#![allow(missing_docs)]
11#![allow(unused_must_use)]
12#![allow(dead_code)]
13#![allow(unused_variables)]
14// #![warn(rustdoc::missing_doc_code_examples)]
15
16
17use std::ptr::copy_nonoverlapping;
18
19use crate::number::{ IntUnion, SmallUInt };
20use crate::symmetric::{ Rijndael_Generic, OFB };
21use crate::symmetric::{ crypt_into_something_without_padding,
22                        pre_encrypt_into_array, pre_encrypt_into_vec,
23                        pre_decrypt_into_array_without_padding,
24                        encrypt_into_vec, encrypt_into_array_without_padding,
25                        decrypt_into_array_without_padding };
26
27
28
29impl <const ROUND: usize, const NB: usize, const NK: usize, const IRREDUCIBLE: u8,
30        const AFFINE_MUL: u64, const AFFINE_ADD: u8,
31        const SR0: usize, const SR1: usize, const SR2: usize, const SR3: usize,
32        const MC00: u8, const MC01: u8, const MC02: u8, const MC03: u8,
33        const MC10: u8, const MC11: u8, const MC12: u8, const MC13: u8,
34        const MC20: u8, const MC21: u8, const MC22: u8, const MC23: u8,
35        const MC30: u8, const MC31: u8, const MC32: u8, const MC33: u8,
36        const RC0: u32, const RC1: u32, const RC2: u32, const RC3: u32,
37        const RC4: u32, const RC5: u32, const RC6: u32, const RC7: u32,
38        const RC8: u32, const RC9: u32, const ROT: u32>
39OFB<[u32; NB]> for Rijndael_Generic<ROUND, NB, NK, IRREDUCIBLE,
40                                            AFFINE_MUL, AFFINE_ADD, SR0, SR1, SR2, SR3,
41                                            MC00, MC01, MC02, MC03, MC10, MC11, MC12, MC13,
42                                            MC20, MC21, MC22, MC23, MC30, MC31, MC32, MC33,
43                                            RC0, RC1, RC2, RC3, RC4, RC5, RC6, RC7, RC8, RC9, ROT>
44{
45    fn encrypt(&mut self, iv: [u32; NB], message: *const u8, length_in_bytes: u64, cipher: *mut u8) -> u64
46    {
47        let mut progress = 0_usize;
48        let mut block = [IntUnion::new(); NB];
49        let mut iivv = [IntUnion::new(); NB];
50        let mut coded = [IntUnion::new(); NB];
51        for i in 0..NB
52            { iivv[i].set(iv[i]); }
53        for _ in 0..length_in_bytes / Self::BLOCK_SIZE as u64
54        {
55            unsafe { copy_nonoverlapping(message.add(progress as usize), block.as_mut_ptr() as *mut u8, Self::BLOCK_SIZE); }
56            iivv = self.encrypt_unit(&iivv);
57            for i in 0..NB
58                { coded[i] = block[i] ^ iivv[i]; }
59            unsafe { copy_nonoverlapping(coded.as_ptr() as *const u8, cipher.add(progress as usize), Self::BLOCK_SIZE); }
60            progress += Self::BLOCK_SIZE;
61        }
62
63        if progress as u64 == length_in_bytes
64        {
65            self.set_successful();
66            progress as u64
67        }
68        else
69        {
70            block = [IntUnion::new(); NB];
71            let tail = (length_in_bytes - progress as u64) as usize;
72            let addr = unsafe { message.add(progress) as *const u8 };
73            unsafe { copy_nonoverlapping(addr, block.as_mut_ptr() as *mut u8, tail); }
74            coded = self.encrypt_unit(&iivv);
75            for i in 0..NB
76                { coded[i] ^= block[i]; }
77            unsafe { copy_nonoverlapping(coded.as_ptr() as *const u8, cipher.add(progress as usize), tail); }
78            self.set_successful();
79            progress as u64 + tail as u64
80        }
81    }
82
83    crypt_into_something_without_padding! {[u32; NB]}
84}