Skip to content

Commit

Permalink
[poly1305] make key size apparent at type level
Browse files Browse the repository at this point in the history
also switch initialization to be less mutable
  • Loading branch information
vincenthz committed Mar 15, 2023
1 parent e78b9e4 commit ee7d5fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ use crate::constant_time::{Choice, CtEqual};
use crate::cryptoutil::write_u64_le;
use crate::mac::Mac;
use crate::poly1305::Poly1305;
use core::convert::TryFrom;

/// Chacha20Poly1305 Incremental Context for Authenticated Data (AAD)
///
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<const ROUNDS: usize> Context<ROUNDS> {
let zero_key = [0u8; 64];
cipher.process(&zero_key, &mut mac_key);

let mac = Poly1305::new(&mac_key[..32]);
let mac = Poly1305::new(<&[u8; 32]>::try_from(&mac_key[..32]).unwrap());
Context {
cipher: cipher,
mac: mac,
Expand Down
47 changes: 24 additions & 23 deletions src/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::mac::{Mac, MacResult};
/// `Poly1305` Context
///
/// Use the `Mac` traits for interaction
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct Poly1305 {
r: [u32; 5],
h: [u32; 5],
Expand All @@ -40,30 +40,31 @@ fn mul64(a: u32, b: u32) -> u64 {

impl Poly1305 {
/// Create a new `Poly1305` context using the key (32 bytes)
pub fn new(key: &[u8]) -> Self {
assert!(key.len() == 32);
let mut poly = Poly1305 {
r: [0u32; 5],
pub fn new(key: &[u8; 32]) -> Self {
// r &= 0xffffffc0ffffffc0ffffffc0fffffff
let r = [
(read_u32_le(&key[0..4])) & 0x3ffffff,
(read_u32_le(&key[3..7]) >> 2) & 0x3ffff03,
(read_u32_le(&key[6..10]) >> 4) & 0x3ffc0ff,
(read_u32_le(&key[9..13]) >> 6) & 0x3f03fff,
(read_u32_le(&key[12..16]) >> 8) & 0x00fffff,
];

let pad = [
read_u32_le(&key[16..20]),
read_u32_le(&key[20..24]),
read_u32_le(&key[24..28]),
read_u32_le(&key[28..32]),
];

Poly1305 {
r,
h: [0u32; 5],
pad: [0u32; 4],
pad,
leftover: 0,
buffer: [0u8; 16],
finalized: false,
};

// r &= 0xffffffc0ffffffc0ffffffc0fffffff
poly.r[0] = (read_u32_le(&key[0..4])) & 0x3ffffff;
poly.r[1] = (read_u32_le(&key[3..7]) >> 2) & 0x3ffff03;
poly.r[2] = (read_u32_le(&key[6..10]) >> 4) & 0x3ffc0ff;
poly.r[3] = (read_u32_le(&key[9..13]) >> 6) & 0x3f03fff;
poly.r[4] = (read_u32_le(&key[12..16]) >> 8) & 0x00fffff;

poly.pad[0] = read_u32_le(&key[16..20]);
poly.pad[1] = read_u32_le(&key[20..24]);
poly.pad[2] = read_u32_le(&key[24..28]);
poly.pad[3] = read_u32_le(&key[28..32]);

poly
}
}

#[rustfmt::skip]
Expand Down Expand Up @@ -255,7 +256,7 @@ mod test {
use crate::mac::Mac;
use crate::poly1305::Poly1305;

fn poly1305(key: &[u8], msg: &[u8], mac: &mut [u8]) {
fn poly1305(key: &[u8; 32], msg: &[u8], mac: &mut [u8]) {
let mut poly = Poly1305::new(key);
poly.input(msg);
poly.raw_result(mac);
Expand Down Expand Up @@ -345,7 +346,7 @@ mod test {
let key = [i as u8; 32];
let msg = [i as u8; 256];
let mut mac = [0u8; 16];
poly1305(&key[..], &msg[0..i], &mut mac);
poly1305(&key, &msg[0..i], &mut mac);
tpoly.input(&mac);
}
tpoly.raw_result(&mut mac);
Expand Down

0 comments on commit ee7d5fd

Please sign in to comment.