GCM (Galois Counter Mode) is an encryption method with authentication for symmetric key encryption. It is fast and allows for parallel processing. The GF(\(2^{128}\)) ) field is used (\(x^{128}+x^{7}+x^2+x+1\)). An authentication tag is created by inputing the data into a GHASH function and encrypting the result. This function is \(GHASH(H,A,C)=𝑋_{m+n+1}\) and where where \(H = E_k(0,128)\) is the Hash Key - and which all 128 bits of zero, \(A\) defines the authenicated data, \(C\) defines the ciphertext, \(m\) related to the number of blocks, and \(n\) the number of blocks in \(C\) (rounded up). and the variable \(X_i\) for \(i\) = 0, ..., \(m\) + \(n\) + 1 i.
GHash in Rust |
An outline of the Rust code is:
extern crate crypto; use crate::crypto::mac::Mac; extern crate base64; use std::env; fn main() { let mut mykey="0123456789ABCDEF"; let mut bytes="0123456789ABCDEF1011"; let args: Vec<String> = env::args().collect(); if args.len() >1 { mykey = args[1].as_str();} if args.len() >2 { bytes = args[2].as_str();} println!("== GHash=="); println!("Key: {:?}",mykey); println!("Bytes: {:?}",bytes); let key=mykey.as_bytes(); let b=bytes.as_bytes(); let mut mac = [0u8; 16]; let mut gh = crypto::ghash::Ghash::new(&key); gh.input(&b); gh.raw_result(&mut mac); println!("\nMAC: {}",hex::encode(mac)); println!("\nMAC: {}",base64::encode(mac)); }
Finally we simply build with:
cargo build
A sample run is:
== GHash== Key: "0123456789ABCDEF" Bytes: "0123456789ABCDEF1011" MAC: 39076d2a3477c873f4ab55e05d4e7ca4 MAC: OQdtKjR3yHP0q1XgXU58pA==