-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathlib.rs
More file actions
153 lines (134 loc) · 3.85 KB
/
lib.rs
File metadata and controls
153 lines (134 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! Pure Rust implementation of the [Gift] block cipher.
//!
//! # ⚠️ Security Warning: Hazmat!
//!
//! This crate implements only the low-level block cipher function, and is intended
//! for use for implementing higher-level constructions *only*. It is NOT
//! intended for direct use in applications.
//!
//! USE AT YOUR OWN RISK!
//!
//! # Examples
//! ```
//! use gift_cipher::cipher::{Array, BlockCipherDecrypt, BlockCipherEncrypt, KeyInit};
//! use gift_cipher::Gift128;
//!
//! let key = Array::from([0u8; 16]);
//! let mut block = Array::from([0u8; 16]);
//!
//! // Initialize cipher
//! let cipher = Gift128::new(&key);
//!
//! let block_copy = block;
//!
//! // Encrypt block in-place
//! cipher.encrypt_block(&mut block);
//!
//! // And decrypt it back
//! cipher.decrypt_block(&mut block);
//!
//! assert_eq!(block, block_copy);
//! ```
//!
//! [Gift]: https://eprint.iacr.org/2017/622.pdf
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg"
)]
#![deny(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs, rust_2018_idioms)]
use cipher::{
AlgorithmName, Block, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt,
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut, Key,
KeyInit, KeySizeUser, ParBlocksSizeUser,
consts::{U1, U16},
};
use core::fmt;
#[cfg(feature = "zeroize")]
use cipher::zeroize::{Zeroize, ZeroizeOnDrop};
pub use cipher;
mod consts;
mod key_schedule;
mod primitives;
use consts::GIFT_RC;
use primitives::{inv_quintuple_round, packing, quintuple_round, unpacking};
/// Gift-128 block cipher instance.
#[derive(Clone)]
pub struct Gift128 {
k: [u32; 80],
}
impl KeySizeUser for Gift128 {
type KeySize = U16;
}
impl KeyInit for Gift128 {
fn new(key: &Key<Self>) -> Self {
Self {
k: key_schedule::precompute_rkeys(key.into()),
}
}
}
impl BlockSizeUser for Gift128 {
type BlockSize = U16;
}
impl ParBlocksSizeUser for Gift128 {
type ParBlocksSize = U1;
}
impl BlockCipherEncrypt for Gift128 {
#[inline]
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = Self::BlockSize>) {
f.call(self)
}
}
impl BlockCipherEncBackend for Gift128 {
#[inline]
fn encrypt_block(&self, mut block: InOut<'_, '_, Block<Self>>) {
let b = block.get_in();
let mut state = [0u32; 4];
packing(&mut state, b.into());
for i in (0..40).step_by(5) {
quintuple_round(&mut state, &self.k[i * 2..], &GIFT_RC[i..]);
}
unpacking(&state, block.get_out().into());
}
}
impl BlockCipherDecrypt for Gift128 {
#[inline]
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = Self::BlockSize>) {
f.call(self)
}
}
impl BlockCipherDecBackend for Gift128 {
#[inline]
fn decrypt_block(&self, mut block: InOut<'_, '_, Block<Self>>) {
let b = block.get_in();
let mut state = [0u32; 4];
packing(&mut state, b.into());
let mut i: usize = 35;
while i > 0 {
inv_quintuple_round(&mut state, &self.k[i * 2..], &GIFT_RC[i..]);
i -= 5;
}
inv_quintuple_round(&mut state, &self.k[i * 2..], &GIFT_RC[i..]);
unpacking(&state, block.get_out().into());
}
}
impl AlgorithmName for Gift128 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Gift128")
}
}
impl fmt::Debug for Gift128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Gift128 { ... }")
}
}
impl Drop for Gift128 {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
self.k.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl ZeroizeOnDrop for Gift128 {}