GithubHelp home page GithubHelp logo

citahub / libsm Goto Github PK

View Code? Open in Web Editor NEW
224.0 12.0 72.0 189 KB

A Rust Library of China's Standards of Encryption Algorithms (SM2/3/4)

License: Apache License 2.0

Rust 100.00%
cipher cryptography sm2 sm3 sm4

libsm's Issues

sm2加解密时klen怎么设置?

libsm/src/sm2/encrypt.rs

Lines 142 to 153 in 3ead619

fn sm2_encrypt_decrypt_test() {
let msg = "hello world".as_bytes();
let klen = msg.len();
let ctx = SigCtx::new();
let (pk_b, sk_b) = ctx.new_keypair().unwrap();
let encrypt_ctx = EncryptCtx::new(klen, pk_b);
let cipher = encrypt_ctx.encrypt(msg).unwrap();
let decrypt_ctx = DecryptCtx::new(klen, sk_b);
let plain = decrypt_ctx.decrypt(&cipher).unwrap();
assert_eq!(msg, plain);

如上代码示例, 加解密时 klen 为要加密码信息的长度,我尝试使用固定值(E.g. 1024)会报错。 由于加密与解密可能在不同流程中使用,加密时可以知道原始数据的长度,但解密时只有密文,无法获取到原始数据的长度。请问要如何处理?

sm2 解密时,如果密文格式不正确,会导致 panic

pub fn decrypt(&self, cipher: &[u8]) -> Sm2Result<Vec<u8>> 方法

转 c_1_point 时,如果遇到异常能否“透传”出来?
最好 let c_1_point = self.curve.bytes_to_point(c_1_bytes).unwrap(); 这里可以不要直接 unwrap(),
能把这里的异常也 return 出来。

现在,这里面的 Sm2Error::InvalidPublic Sm2Error::NotOnCurve 等,外层好像没法处理。

expected `Result<(Point, BigUint), Sm2Error>`, found `(_, _)`

1.配置:

[dependencies]
libsm="0.6.0"

  1. 然后在main.rs中调用的时候
    use libsm::sm2::signature::{Pubkey, Seckey, SigCtx, Signature};
    fn main() {
    let ctx = SigCtx::new();
    let (pk, sk) = ctx.new_keypair();

}

报错信息如下
error[E0308]: mismatched types
--> src/main.rs:4:9
|
4 | let (pk, sk) = ctx.new_keypair();
| ^^^^^^^^ ----------------- this expression has type Result<(Point, num_bigint::biguint::BigUint), sm2::error::Sm2Error>
| |
| expected Result<(Point, BigUint), Sm2Error>, found (_, _)
|
= note: expected enum Result<(Point, num_bigint::biguint::BigUint), sm2::error::Sm2Error>

how can i use encode and decode with sm2

i see the test code for encode and decode like this

#[test]
    fn test_sig_encode_and_decode() {
        let string = String::from("abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
        let msg = string.as_bytes();

        let ctx = SigCtx::new();
        let (pk, sk) = ctx.new_keypair();

        let signature = ctx.sign(msg, &sk, &pk);
        let der = signature.der_encode();
        let sig = Signature::der_decode(&der[..]).unwrap();
        assert!(ctx.verify(msg, &pk, &sig));

        let signature = ctx.sign(msg, &sk, &pk);
        let der = signature.der_encode();
        let sig = Signature::der_decode_raw(&der[2..]).unwrap();
        assert!(ctx.verify(msg, &pk, &sig));
    }

but i need a methods to encrypt plaintext.

add error return

对于 decrypt 无法返回错误,导致错误处理使用 panic 的方式,会导致用户错误输入而造成进程 panic 的现象,由于涉及接口次更新为非兼容更新

升级到最新0.51版后sm2速度变慢很多

原来0.50时相对慢 #55 ,代码相同,今天升级到0.51后,直接开发模式时卡住好久,release后运行,time ./target/release/demo,如下

real	0m2.339s
user	0m2.311s
sys	0m0.011s

debug 下时间

real	1m0.366s
user	0m58.938s
sys	0m0.466s

ecc曲线point add 和point double 计算结果不同

我尝试为sm2算法中的ecc.rs代码内,为测试test_add_double_neg增加以下测试,该测试希望验证点加法和点倍增的结果是否相同:

        let double_g = curve.double(&g);  //  2 * g
        let add_g = curve.add(&g,&g);     // g + g
        assert!(curve.eq(&add_g,&double_g));

但是测试结果告诉我没有通过测试:

thread 'sm2::ecc::tests::test_add_double_neg' panicked at 'assertion failed: curve.eq(&add_g, &double_g)', src\sm2\ecc.rs:595:

查看了一下实现点加法和点倍增的算法,发现算法和我之前学习的算法有所出入。例如,点倍增算法中引入了参数a:
而论文中实现点倍增并没有参数a的参与(雅可比坐标系下)
经过学习,了解到此处点倍增利用的似乎是dbl-1998-cmo-2中的方法,抱歉。点加法似乎是使用了add-2007-bl中的算法?

想请问点加法和点倍增计算结果为何不一致的原因,以及计算点加法和点乘法时使用到的算法名称。个人水平有限,难免在理解上会出现错误,烦请多多指教

同样sm2签名和验签,为什么比go的慢了好多

用github.com/tjfoc/gmsm/sm2 sm2签名和验签,用time看了下运行时间,发现libsm用的时间比go的多好多,而且在开发时,运行更慢。下面是release后的

❯ rustc --version
rustc 1.71.0-nightly (5cdb7886a 2023-04-15)
❯ go version
go version go1.18beta1 darwin/amd64

rust 的

    let msg = "test".as_bytes();
    let ctx = SigCtx::new();
    let (pk, sk) = ctx.new_keypair().unwrap();

    let signature = ctx.sign(&msg, &sk, &pk).unwrap();

    println!("signed is : {:?}", signature.to_string());

cargo build --release后运行,time ./target/release/demo看时间为

real	0m0.221s
user	0m0.216s
sys	0m0.003s

go 的

package main

import (
	"crypto/rand"
	"fmt"
	"log"

	"github.com/tjfoc/gmsm/sm2"
)

func main() {
	priv, err := sm2.GenerateKey(rand.Reader) // 生成密钥对
	if err != nil {
		log.Fatal(err)
	}
	pub := &priv.PublicKey
	msg := []byte("test")
	sign, err := priv.Sign(rand.Reader, msg, nil) // sm2签名
	if err != nil {
		log.Fatal(err)
	}
	isok := pub.Verify(msg, sign) // sm2验签
	fmt.Printf("Verified: %v\n", isok)
}

go build后运行两次,time ./demo

Verified: true

real	0m0.370s
user	0m0.004s
sys	0m0.002s
Verified: true

real	0m0.009s
user	0m0.005s
sys	0m0.003s

可以看出go用的时间更少,而且go还做了加密解密两件事

Invalid private key length, sometimes

First. I created a private key and return hex.

let ctx = SigCtx::new();
let (pk, sk) = ctx.new_keypair();
format!("0x{}", hex::encode(sk.to_bytes_be()))
// 0x11a8571fe8ee79990db44379aac8d67606a783ac73f52113d563dce7f38fdf

Next, the data is signed with the private key

let ctx = SigCtx::new();
let privk = hex::decode("0x11a8571fe8ee79990db44379aac8d67606a783ac73f52113d563dce7f38fdf")
let sk = ctx.load_seckey(&privk).unwrap();
// this painic. result returns Err(true)

The length of a valid private key that doesn't include 0x should be 64, it's only 62.

version: git commit 5cc6f4d
rust: rustc 1.31.0-nightly (8c4ad4e9e 2018-10-04)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.