2.1. Manage EC Keys
该文章根据 CC-BY-4.0 协议发表,转载请遵循该协议。
本文地址:https://fenying.net/en/book/pki-tutorials/2.1.manage-ec-keys/
This chapter introduces how to generate, convert, encode, encrypt, decrypt EC keys using OpenSSL command-line tools.
Generate EC Keys
According to the key length, there are three types of keys:
1# NIST P-256
2openssl ecparam -genkey -name prime256v1 -noout -out es256-raw.pem
3# NIST P-384
4openssl ecparam -genkey -name secp384r1 -noout -out es384-raw.pem
5# NIST P-521
6openssl ecparam -genkey -name secp521r1 -noout -out es521-raw.pem
However, if you choose to use curve25519
, you must use the genpkey
subcommand, and the key must be in PKCS#8 format.
1openssl genpkey -algorithm ed25519 -out ed25519-raw.pem # The generated key file is in PKCS#8 format
2
3openssl genpkey -algorithm x25519 -out x25519-raw.pem # The generated key file is in PKCS#8 format
Note:
- The key is generated without encryption protection.
- If you want to use
curve25519
in a certificate, please useed25519
instead ofx25519
, because the public key inX.509
certificates is used for signing, not for key exchange.
Encryption and Decryption of Keys
Encrypt Keys
1# PKCS#1
2openssl ec -aes-256-cfb -in es384-raw.pem -out es384.pem
3
4# PKCS#8
5openssl pkcs8 \
6 -topk8 \
7 -inform PEM \
8 -v2 aes256 \
9 -in ed25519-p8-raw.pem \
10 -out ed25519-p8.pem
Decrypt Keys
1openssl ec -in es384-raw.pem -out es384.pem
2
3# PKCS#8
4openssl pkcs8 \
5 -inform PEM \
6 -in ed25519-p8.pem \
7 -out ed25519-p8-raw.pem
Extract Public Key
1# This command accepts both PKCS#1 and PKCS#8 encoded private keys
2openssl ec -in es384.pem -pubout -out es384.pub
3openssl ec -in ed25519.pem -pubout -out ed25519.pub
View Private Key Information
1# This command accepts both PKCS#1 and PKCS#8 encoded private keys
2openssl ec -text -noout -in es384-raw.pem
View Public Key Information
1openssl ec -pubin -text -noout -in es384.pub
2openssl ec -pubin -text -noout -in ed25519.pub
Convert Keys
The conversion between EC keys and PKCS#8 is similar to RSA.
openssl ec
command accepts-inform
and-outform
too, so the DER/PEM conversion of EC keys is the same as RSA keys.
Test Keys
1openssl rand -out tmp.dat 4096
2
3# Sign the random file with the private key using ECDSA-SHA-256
4
5openssl dgst -sha256 -sign es384-raw.pem -out tmp.dat.sig tmp.dat
6openssl dgst -sha256 -verify es384.pub -signature tmp.dat.sig tmp.dat
7
8# Test ECDH (curve25519)
9
10openssl genpkey -algorithm x25519 -out x25519-a.pem
11openssl ec -in x25519-a.pem -pubout -out x25519-a.pub
12
13openssl genpkey -algorithm x25519 -out x25519-b.pem
14openssl ec -in x25519-b.pem -pubout -out x25519-b.pub
15
16openssl pkeyutl -derive -inkey x25519-a.pem -peerkey x25519-b.pub -out x25519-axb.bin
17openssl pkeyutl -derive -inkey x25519-b.pem -peerkey x25519-a.pub -out x25519-bxa.bin
18
19base64 x25519-axb.bin
20base64 x25519-bxa.bin
21
22rm x25519-*.*
23
24# Test EdDSA (Ed25519) Signing
25
26openssl genpkey -algorithm ed25519 -out ed25519.pem
27openssl ec -in ed25519.pem -pubout -out ed25519.pub
28
29openssl pkeyutl -sign -inkey ed25519.pem -rawin -in tmp.dat -out tmp.dat.sig
30openssl pkeyutl -verify -pubin -inkey ed25519.pub -sigfile tmp.dat.sig -rawin -in tmp.dat
31
32rm ed25519.*
33
34rm tmp.*
Ok, all above are the common operations of EC keys.
comments powered by Disqus