简体   繁体   English

将数据块签名为erlang中的哈希不起作用。 基于ec的密钥推导

[英]signing of datablock as hash in erlang not working. Key derivation based on ec

Erlang version: 9.2 Erlang版本:9.2

I am trying to sign one datablock with generated keys on ecdh-base. 我正在尝试在ecdh-base上使用生成的密钥对一个数据块进行签名。

Here is my workflow: 这是我的工作流程:

86> {PublicKey, PrivKeyOut} = crypto:generate_key(ecdh, crypto:ec_curve(secp521r1)).
{<<4,0,196,6,85,178,189,234,231,13,82,152,96,162,92,163,
   133,81,42,147,168,146,138,226,15,80,127,228,...>>,
 <<1,33,215,135,89,40,35,40,104,14,217,153,78,62,53,83,
   198,165,84,30,135,159,218,82,47,102,204,...>>}
87> Mesage = "testmessage".
"testmessage"
88> Hash = crypto:hash(sha512, Mesage).
<<1,216,98,78,245,111,176,233,114,224,249,27,118,114,49,
  189,40,144,90,249,175,108,79,235,186,247,247,40,131,...>>
89> Signature = crypto:sign(ecdsa, sha512, Hash, PrivKeyOut).
** exception error: bad argument
     in function  crypto:pkey_sign_nif/5
        called as crypto:pkey_sign_nif(ecdsa,sha512,
                                       <<1,216,98,78,245,111,176,233,114,224,
                                         249,27,118,114,49,189,40,144,90,249,
                                         175,108,79,235,186,247,...>>,
                                       <<1,33,215,135,89,40,35,40,104,14,217,
                                         153,78,62,53,83,198,165,84,30,135,159,
                                         218,82,47,...>>,
                                       [])
     in call from crypto:sign/5 (crypto.erl, line 433)

What am I doing wrong? 我究竟做错了什么?

There are two problems with your code: 您的代码有两个问题:

  • The 4th argument of crypto:sign/4 is called Key , but if you check the type spec , it in fact takes an [ecdh_private(), ecdh_params()] list (in case of using the ecdsa algorithm at least). crypto:sign/4的第四个参数称为Key ,但是如果检查类型spec ,实际上它会使用[ecdh_private(), ecdh_params()]列表(至少在使用ecdsa算法的情况下)。
  • If you calculate the Hash yourself, the third argument shall be {digest, Hash} . 如果您自己计算Hash ,则第三个参数应为{digest, Hash} Otherwise you will sign the hash of the message hash. 否则,您将在消息哈希中签名哈希。 You can also pass the plain message to the function, but in that case it has to be a binary, not a string. 您也可以将普通消息传递给函数,但是在这种情况下,它必须是二进制文件,而不是字符串。

This is how to fix these problems: 这是解决这些问题的方法:

EcdhParams = crypto:ec_curve(secp521r1),
{PublicKey, PrivKeyOut} = crypto:generate_key(ecdh, EcdhParams),
Message = <<"testmessage">>,
crypto:sign(ecdsa, sha512, Message, [PrivKeyOut, EcdhParams]).

Or, in case you need the Hash later and/or you get Message as a string, this would also work: 或者,如果以后需要Hash和/或将Message作为字符串获取,这也可以工作:

EcdhParams = crypto:ec_curve(secp521r1),
{PublicKey, PrivKeyOut} = crypto:generate_key(ecdh, EcdhParams),
Message = "testmessage",
Hash = crypto:hash(sha512, Message),
crypto:sign(ecdsa, sha512, {digest, Hash}, [PrivKeyOut, EcdhParams]).

I found old erlang bugs, these show that elliptic curve cipher suites are no completely implemented. 我发现了旧的erlang错误,这些错误表明椭圆曲线密码套件尚未完全实现。 Here URLs: 此处的网址:

http://erlang.2086793.n4.nabble.com/Incomplete-Elliptic-Curve-Cipher-Suites-in-R16B01-and-R16B02-td4692857.html http://erlang-bugs.erlang.narkive.com/YNWGZ1F2/incomplete-elliptic-curve-cipher-suites-in-r16b01-and-r16b02 http://erlang.2086793.n4.nabble.com/Incomplete-Elliptic-Curve-Cipher-Suites-in-R16B01-and-R16B02-td4692857.html http://erlang-bugs.erlang.narkive.com/YNWGZ1F2 /不完整的椭圆曲线加密法套房合r16b01和- r16b02

I would use the libsodium library for my tests: 我将使用libsodium库进行测试:

https://github.com/jlouis/enacl https://github.com/jlouis/enacl

Thx 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM