diff --git a/sm2/src/pke.rs b/sm2/src/pke.rs index 1f595648..61875511 100644 --- a/sm2/src/pke.rs +++ b/sm2/src/pke.rs @@ -28,11 +28,11 @@ //! let decrypting_key = DecryptingKey::new_with_mode(secret_key.to_nonzero_scalar(), Mode::C1C2C3); //! assert_eq!(decrypting_key.decrypt(&ciphertext)?, plaintext); //! -//! // Encrypting asn.1 -//! let ciphertext = encrypting_key.encrypt_asna1(plaintext)?; +//! // Encrypting ASN.1 DER +//! let ciphertext = encrypting_key.encrypt_der(plaintext)?; //! -//! // Decrypting asn.1 -//! assert_eq!(decrypting_key.decrypt_asna1(&ciphertext)?, plaintext); +//! // Decrypting ASN.1 DER +//! assert_eq!(decrypting_key.decrypt_der(&ciphertext)?, plaintext); //! //! Ok(()) //! # } @@ -121,8 +121,8 @@ impl<'a> DecodeValue<'a> for Cipher<'a> { let digest = OctetStringRef::decode(nr)?.into(); let cipher = OctetStringRef::decode(nr)?.into(); Ok(Cipher { - x: Uint::from_be_bytes(zero_byte_slice(x)?), - y: Uint::from_be_bytes(zero_byte_slice(y)?), + x: Uint::from_be_bytes(zero_pad_byte_slice(x)?), + y: Uint::from_be_bytes(zero_pad_byte_slice(y)?), digest, cipher, }) @@ -164,7 +164,7 @@ fn xor(c2: &mut [u8], ha: &[u8], offset: usize, xor_len: usize) { } /// Converts a byte slice to a fixed-size array, padding with leading zeroes if necessary. -pub(crate) fn zero_byte_slice( +pub(crate) fn zero_pad_byte_slice( bytes: &[u8], ) -> elliptic_curve::pkcs8::der::Result<[u8; N]> { let num_zeroes = N diff --git a/sm2/src/pke/encrypting.rs b/sm2/src/pke/encrypting.rs index b877717f..82d274e7 100644 --- a/sm2/src/pke/encrypting.rs +++ b/sm2/src/pke/encrypting.rs @@ -191,7 +191,7 @@ fn encrypt( fn next_k(bit_length: u32) -> U256 { loop { let k = U256::random_bits(&mut rand_core::OsRng, bit_length); - if k.is_zero().unwrap_u8() == 0 && k <= Sm2::ORDER { + if !>::into(k.is_zero()) && k < Sm2::ORDER { return k; } }