Mercurial > prosody-hg
comparison spec/util_crypto_spec.lua @ 13537:fb970df95374
util.crypto: Add more ECC methods
pkey_meth_derive: to derive a shared symmetric key from two ECC keys
pkey_meth_public_raw: to get the raw form of the public key
import_public_ec_raw: to import the raw form of the public key
generate_p256_keypair: key generation for the P-256 curve
| author | Stephen Paul Weber <singpolyma@singpolyma.net> |
|---|---|
| date | Tue, 29 Oct 2024 09:15:50 -0500 |
| parents | d3ae47d8a7a7 |
| children |
comparison
equal
deleted
inserted
replaced
| 13536:272ea65c3087 | 13537:fb970df95374 |
|---|---|
| 1 local test_keys = require "spec.inputs.test_keys"; | 1 local test_keys = require "spec.inputs.test_keys"; |
| 2 | 2 |
| 3 describe("util.crypto", function () | 3 describe("util.crypto", function () |
| 4 local crypto = require "util.crypto"; | 4 local crypto = require "util.crypto"; |
| 5 local random = require "util.random"; | 5 local random = require "util.random"; |
| 6 local encodings = require "util.encodings"; | |
| 6 | 7 |
| 7 describe("generate_ed25519_keypair", function () | 8 describe("generate_ed25519_keypair", function () |
| 8 local keypair = crypto.generate_ed25519_keypair(); | 9 local keypair = crypto.generate_ed25519_keypair(); |
| 9 assert.is_not_nil(keypair); | 10 assert.is_not_nil(keypair); |
| 10 assert.equal("ED25519", keypair:get_type()); | 11 assert.equal("ED25519", keypair:get_type()); |
| 12 end) | |
| 13 | |
| 14 describe("generate_p256_keypair", function () | |
| 15 local keypair = crypto.generate_p256_keypair(); | |
| 16 assert.is_not_nil(keypair); | |
| 17 assert.equal("id-ecPublicKey", keypair:get_type()); | |
| 18 end) | |
| 19 | |
| 20 describe("export/import raw", function () | |
| 21 local keypair = crypto.generate_p256_keypair(); | |
| 22 assert.is_not_nil(keypair); | |
| 23 local raw = keypair:public_raw() | |
| 24 local imported = crypto.import_public_ec_raw(raw, "P-256") | |
| 25 assert.equal(keypair:public_pem(), imported:public_pem()); | |
| 26 end) | |
| 27 | |
| 28 describe("derive", function () | |
| 29 local key = crypto.import_private_pem(test_keys.ecdsa_private_pem); | |
| 30 local peer_key = crypto.import_public_pem(test_keys.ecdsa_public_pem); | |
| 31 assert.equal("n1v4KeKmOVwjC67fiKtjJnqcEaasbpZa2fLPNHW51co=", encodings.base64.encode(key:derive(peer_key))) | |
| 11 end) | 32 end) |
| 12 | 33 |
| 13 describe("import_private_pem", function () | 34 describe("import_private_pem", function () |
| 14 it("can import ECDSA keys", function () | 35 it("can import ECDSA keys", function () |
| 15 local ecdsa_key = crypto.import_private_pem(test_keys.ecdsa_private_pem); | 36 local ecdsa_key = crypto.import_private_pem(test_keys.ecdsa_private_pem); |
