Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
309972028d
|
|||
|
4e1b96a38d
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "sm4-gcm"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
edition = "2021"
|
||||
authors = ["Hatter Jiang"]
|
||||
repository = "https://git.hatter.ink/hatter/sm4-gcm"
|
||||
|
||||
@@ -13,7 +13,7 @@ pub fn sm4_gcm_decrypt(key: &Sm4Key, nonce: &[u8], ciphertext: &[u8]) -> Result<
|
||||
|
||||
pub fn sm4_gcm_aad_decrypt(key: &Sm4Key, nonce: &[u8], aad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, String> {
|
||||
let mut decryptor = Sm4GcmStreamDecryptor::new(key, nonce);
|
||||
if aad.len() > 0 {
|
||||
if !aad.is_empty() {
|
||||
decryptor.init_adata(aad);
|
||||
}
|
||||
let mut msg1 = decryptor.update(ciphertext);
|
||||
@@ -59,7 +59,7 @@ impl Sm4GcmStreamDecryptor {
|
||||
}
|
||||
|
||||
pub fn init_adata(&mut self, adata: &[u8]) {
|
||||
if adata.len() > 0 {
|
||||
if !adata.is_empty() {
|
||||
self.adata_len += adata.len();
|
||||
self.ghash.update_padded(adata);
|
||||
}
|
||||
@@ -123,6 +123,9 @@ impl Sm4GcmStreamDecryptor {
|
||||
self.ghash.update_padded(&adata_and_message_len);
|
||||
|
||||
let tag = self.calculate_tag();
|
||||
if self.message_buffer.len() < 16 {
|
||||
return Err(format!("Tag missing, message length is: {} < 16 bytes", self.message_buffer.len()));
|
||||
}
|
||||
let message_tag = &self.message_buffer[message_buffer_len - 16..];
|
||||
|
||||
if message_tag != tag.as_slice() {
|
||||
@@ -134,12 +137,12 @@ impl Sm4GcmStreamDecryptor {
|
||||
}
|
||||
|
||||
fn calculate_tag(&mut self) -> Vec<u8> {
|
||||
let mut bs = self.init_nonce.to_be_bytes().clone();
|
||||
let mut bs = self.init_nonce.to_be_bytes();
|
||||
let block = Block::<Sm4Block>::from_mut_slice(&mut bs);
|
||||
self.cipher.encrypt_block(block);
|
||||
let ghash = self.ghash.clone().finalize();
|
||||
let tag_trunk = ghash.as_slice();
|
||||
let y = u8to128(&tag_trunk) ^ u8to128(&block.as_slice());
|
||||
let y = u8to128(tag_trunk) ^ u8to128(block.as_slice());
|
||||
y.to_be_bytes().to_vec()
|
||||
}
|
||||
|
||||
@@ -147,7 +150,7 @@ impl Sm4GcmStreamDecryptor {
|
||||
let mut block = [0u8; BLOCK_SIZE];
|
||||
let block = Block::<Sm4Block>::from_mut_slice(&mut block);
|
||||
self.cipher.encrypt_block(block);
|
||||
u8to128(&block.as_slice())
|
||||
u8to128(block.as_slice())
|
||||
}
|
||||
|
||||
fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) {
|
||||
|
||||
@@ -13,7 +13,7 @@ pub fn sm4_gcm_encrypt(key: &Sm4Key, nonce: &[u8], message: &[u8]) -> Vec<u8> {
|
||||
|
||||
pub fn sm4_gcm_aad_encrypt(key: &Sm4Key, nonce: &[u8], aad: &[u8], message: &[u8]) -> Vec<u8> {
|
||||
let mut encryptor = Sm4GcmStreamEncryptor::new(key, nonce);
|
||||
if aad.len() > 0 {
|
||||
if !aad.is_empty() {
|
||||
encryptor.init_adata(aad);
|
||||
}
|
||||
let mut enc1 = encryptor.update(message);
|
||||
@@ -60,7 +60,7 @@ impl Sm4GcmStreamEncryptor {
|
||||
}
|
||||
|
||||
pub fn init_adata(&mut self, adata: &[u8]) {
|
||||
if adata.len() > 0 {
|
||||
if !adata.is_empty() {
|
||||
self.adata_len += adata.len();
|
||||
self.ghash.update_padded(adata);
|
||||
}
|
||||
@@ -126,12 +126,12 @@ impl Sm4GcmStreamEncryptor {
|
||||
}
|
||||
|
||||
fn compute_tag(&mut self) -> Vec<u8> {
|
||||
let mut bs = self.init_nonce.to_be_bytes().clone();
|
||||
let mut bs = self.init_nonce.to_be_bytes();
|
||||
let block = Block::<Sm4Block>::from_mut_slice(&mut bs);
|
||||
self.cipher.encrypt_block(block);
|
||||
let ghash = self.ghash.clone().finalize();
|
||||
let tag_trunk = ghash.as_slice();
|
||||
let y = u8to128(&tag_trunk) ^ u8to128(&block.as_slice());
|
||||
let y = u8to128(tag_trunk) ^ u8to128(block.as_slice());
|
||||
y.to_be_bytes().to_vec()
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ impl Sm4GcmStreamEncryptor {
|
||||
let mut block = [0u8; BLOCK_SIZE];
|
||||
let block = Block::<Sm4Block>::from_mut_slice(&mut block);
|
||||
self.cipher.encrypt_block(block);
|
||||
u8to128(&block.as_slice())
|
||||
u8to128(block.as_slice())
|
||||
}
|
||||
|
||||
fn normalize_nonce(&mut self, nonce_bytes: &[u8]) -> (u128, u128) {
|
||||
|
||||
Reference in New Issue
Block a user