citahub / libsm Goto Github PK
View Code? Open in Web Editor NEWA Rust Library of China's Standards of Encryption Algorithms (SM2/3/4)
License: Apache License 2.0
A Rust Library of China's Standards of Encryption Algorithms (SM2/3/4)
License: Apache License 2.0
Lines 142 to 153 in 3ead619
如上代码示例, 加解密时 klen
为要加密码信息的长度,我尝试使用固定值(E.g. 1024)会报错。 由于加密与解密可能在不同流程中使用,加密时可以知道原始数据的长度,但解密时只有密文,无法获取到原始数据的长度。请问要如何处理?
是否可以实现对 sm4 的 cbc 模式的支持?
1.配置:
[dependencies]
libsm="0.6.0"
}
报错信息如下
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>
sm2还没有实现非对称加密解密么? 我看现在好像只能进行数字签名?
有加密解密方案或文档么?
rsa的公钥、私钥可以是那种pem的。请问下libsm可以生成类似的吗?没找到方法?
我尝试为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中的算法?
想请问点加法和点倍增计算结果为何不一致的原因,以及计算点加法和点乘法时使用到的算法名称。个人水平有限,难免在理解上会出现错误,烦请多多指教
Hi,想问下作者是否介意我将本项目的代码直接拷贝到其它项目呢(应该不会有什么太大的修改)?
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
等,外层好像没法处理。
用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还做了加密解密两件事
对于 decrypt 无法返回错误,导致错误处理使用 panic 的方式,会导致用户错误输入而造成进程 panic 的现象,由于涉及接口次更新为非兼容更新
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.
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)
原来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
You can use this crates in WebAssembly. Currently, you only need to add dependencies:
# Cargo.toml
getrandom = { version = "^0.2", features = ["js"] }
See getrandom#WebAssembly support.
But it still needs improvement for the first time #47
0.5.1相比0.5.0的SM2签名性能有严重的下降
pub fn verify_raw(&self, digest: &[u8], pk: &Point, sig: &Signature) -> Sm2Result {
if digest.len() != 32 {
return Err(Sm2Error::InvalidDigestLen);
}
()
to replace needless types (for example, bool
in Result::Err
).I will do these in few weeks.
I found user id is 1234567812345678, but sometimes I have to modify it, is there any solutions?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.