it works!
This commit is contained in:
commit
436fd22a51
3 changed files with 166 additions and 0 deletions
80
Cargo.lock
generated
Normal file
80
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crypto-bigint"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "96272c2ff28b807e09250b180ad1fb7889a3258f7455759b5c3c58b719467130"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
"rand_core",
|
||||||
|
"subtle",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.177"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "poly1305"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-bigint",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "subtle"
|
||||||
|
version = "2.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.11.1+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||||
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "poly1305"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
crypto-bigint = "0.6.1"
|
||||||
79
src/lib.rs
Normal file
79
src/lib.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
use std::os::unix::raw::mode_t;
|
||||||
|
use crypto_bigint::{BitOps, ConstZero, Encoding, Integer, Limb, MulMod, NonZero, Pow, Uint, U192};
|
||||||
|
use crypto_bigint::subtle::ConstantTimeEq;
|
||||||
|
|
||||||
|
pub fn oneoff_authenticate(message: &[u8], key: &[u8; 32]) -> [u8; 16] {
|
||||||
|
let r = u128::from_le_bytes(key[0..16].try_into().unwrap());
|
||||||
|
let s = u128::from_le_bytes(key[16..32].try_into().unwrap());
|
||||||
|
|
||||||
|
let poly = poly1305(message, clamp(r)).wrapping_add(s).to_le_bytes();
|
||||||
|
|
||||||
|
poly
|
||||||
|
}
|
||||||
|
|
||||||
|
/// the prime $2^130 - 5$
|
||||||
|
const MODULO_PRIME: NonZero<Uint<3>> = NonZero::<U192>::new_unwrap(U192::ONE.shl(130).sub_mod(&U192::from_u8(5), &U192::MAX));
|
||||||
|
|
||||||
|
fn clamp(secret: u128) -> u128 {
|
||||||
|
secret & 0x0ffffffc0ffffffc0ffffffc0fffffff
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poly1305(data: &[u8], secret: u128) -> u128 {
|
||||||
|
let secret = U192::from_u128(secret);
|
||||||
|
|
||||||
|
let blocks = data.chunks(16);
|
||||||
|
|
||||||
|
let mut sum = U192::ZERO;
|
||||||
|
for block in blocks {
|
||||||
|
let c = block_to_coefficient(block);
|
||||||
|
|
||||||
|
sum = sum.add_mod(&c, &MODULO_PRIME).mul_mod(&secret, &MODULO_PRIME);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut out_bytes = [0u8; 16];
|
||||||
|
for i in 0..16 {
|
||||||
|
out_bytes[i] = sum.to_le_bytes()[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
u128::from_le_bytes(out_bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn block_to_coefficient(block: &[u8]) -> U192 {
|
||||||
|
let mut bytes = [0u8; 192/8];
|
||||||
|
|
||||||
|
let mut i = 0;
|
||||||
|
for byte in block {
|
||||||
|
bytes[i] = *byte;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes[i] = 1;
|
||||||
|
|
||||||
|
U192::from_le_bytes(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crypto_bigint::U256;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
|
||||||
|
const MESSAGE: &[u8] = b"Cryptographic Forum Research Group";
|
||||||
|
const KEY: [u8; 32] = U256::from_be_hex("1bf54941aff6bf4afdb20dfb8a800301a806d542fe52447f336d555778bed685").to_le_bytes();
|
||||||
|
const EXPECTED_TAG: [u8; 16] = 0xa927010caf8b2bc2c6365130c11d06a8_u128.to_le_bytes();
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let tag = oneoff_authenticate(MESSAGE, &KEY);
|
||||||
|
|
||||||
|
assert_eq!(tag, EXPECTED_TAG);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn single_block() {
|
||||||
|
let secret = clamp(0xa806_d542_fe52_447f_336d_5557_78be_d685_u128);
|
||||||
|
let block = poly1305(b"Cryptographic Fo", secret);
|
||||||
|
|
||||||
|
assert_eq!(block, 0xc88c77849d64ae9147ddeb88e69c83fc)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue