参考:Elliptic Curve Digital Signature Algorithm
ECDSA,椭圆曲线数字签名算法,也即使用了椭圆曲线密码学
的数字签名算法
。
wikipeida 的定义如下:
In cryptography, the Elliptic Curve Digital Signature Algorithm (ECDSA) offers a variant of the Digital Signature Algorithm (DSA) which uses elliptic curve cryptography.
可以把 ECDSA 可以拆解成两部分:ECC 和 DSA。
ECDSA
Fundamentals
The Fundamentals of an ECDSA Authentication System - ==强力推荐!==
虽然椭圆曲线有无数个,但只有很小一部分适合 ECC,NIST推荐使用这些椭圆曲线并将其写入标准,每个曲线都有一个名称并由一个参数集来定义,参数集由 Prime Modulus p, the Prime Order n, the Coefficient a, the Coefficient b, and the x and y coordinates of the Base Point G(x,y) on the curve 组成。如 Curve P-192 的参数如下:
私钥是个随机数,公钥由私钥和椭圆曲线产生,公钥由 x 和 y 值组成:
数字签名由私钥,椭圆曲线,随机数和消息的摘要产生:
验证结果由公钥,椭圆曲线,数字签名和消息的摘要产生:
Digital signature
参考:Can I extract R and S from an ECDSA signature in bit form, and vica versa?
ECDSA 数字签名由 r 和 s 组成,r 和 s 的长度和私钥 d 长度相同。
实际使用的 ECDSA 一般是 DER 格式的文件,除了 r 和 s 外还有其他信息。
以一个例子来说明 DER 格式 ECDSA 数字签名的格式:
1 | 0x30, 0x45, // Sequence of length 69 to follow |
0x30 代表着下面是个 Sequence,长度为 0x45,0x02 代表下面是个 Integer,长度是 0x21,然后是另一个 Integer,长度是 0x20。
为什么第一个 integer 的长度是 0x21 呢,因为 integer 是带符号类型的,所以如果第一个字节的 MSB 是 1( > 0x7F),就要额外在前面补个 0x00。
工具
参考:
https://github.com/warner/python-ecdsa
openssl 提供了 ECDSA 的一些命令。
生成 EC 私钥
1 | openssl ecparam -name secp224r1 -genkey -out sk.pem |
https://www.openssl.org/docs/manmaster/man1/ecparam.html
其中,-name secp2241r1 是椭圆曲线参数,使用 openssl ecparam -list_curves 可以查看所有的椭圆曲线参数。
从 EC 私钥中提取公钥
1 | openssl ec -in sk.pem -pubout -out vk.pem |
https://www.openssl.org/docs/manmaster/man1/ec.html
签名
1 | openssl dgst -sha256 -sign sk.pem -out data.sig data |
https://www.openssl.org/docs/manmaster/man1/dgst.html
其中,-sha256 是消息摘要算法。
验签
1 | openssl dgst -sha256 -verify vk.pem -signature data.sig data |
从证书中提取公钥
1 | openssl x509 -in cert.pem -noout -pubkey > vk.pem |
https://www.cnblogs.com/pixy/p/4722381.html
查看私钥
1 | openssl ec -in sk.pem -text -noout |
查看公钥
1 | openssl ec -in vk.pem -pubin -text -noout |
SHA256 计算
1 | openssl dgst -sha256 file |
https://www.cnblogs.com/gordon0918/p/5382541.html
由 R 和 S 生成 .DER 文件
1 | #! /usr/bin/env python |