spec/util_crypto_spec.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 24 Jun 2022 16:56:16 +0100
changeset 12697 7c5afbdcbc77
child 12704 899c057781cd
permissions -rw-r--r--
util.crypto: New wrapper for some operations in OpenSSL's libcrypto Specifically, ED25519 key generation/import/export, sign/verify operations, and AES encrypt/decrypt.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12697
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local test_keys = {
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
	ecdsa_private_pem = [[
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
-----BEGIN PRIVATE KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg7taVK6bPtPz4ah32
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
aD9CfvOah5omBxRVtzypwQXvZeahRANCAAQpKFeNIy27+lVo6bJslO6r2ty5rlb5
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
xEiCx8GrrbJ8S7b5IPZCS7OrBaO2iqgOf7NMsgO12eLCfMZRnA+gCC34
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
-----END PRIVATE KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
]];
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	ecdsa_public_pem = [[
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
-----BEGIN PUBLIC KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEKShXjSMtu/pVaOmybJTuq9rcua5W
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
+cRIgsfBq62yfEu2+SD2QkuzqwWjtoqoDn+zTLIDtdniwnzGUZwPoAgt+A==
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
-----END PUBLIC KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
]];
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
	eddsa_private_pem = [[
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
-----BEGIN PRIVATE KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
MC4CAQAwBQYDK2VwBCIEIOmrajEfnqdzdJzkJ4irQMCGbYRqrl0RlwPHIw+a5b7M
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
-----END PRIVATE KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
]];
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	eddsa_public_pem = [[
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
-----BEGIN PUBLIC KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
MCowBQYDK2VwAyEAFipbSXeGvPVK7eA4+hIOdutZTUUyXswVSbMGi0j1QKE=
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
-----END PUBLIC KEY-----
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
]];
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
};
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
describe("util.crypto", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
	local crypto = require "util.crypto";
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
	local random = require "util.random";
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	describe("generate_ed25519_keypair", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
		local keypair = crypto.generate_ed25519_keypair();
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
		assert.is_not_nil(keypair);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
		assert.equal("ED25519", keypair:get_type());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
	end)
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	describe("import_private_pem", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		it("can import ECDSA keys", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
			local ecdsa_key = crypto.import_private_pem(test_keys.ecdsa_private_pem);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
			assert.equal("id-ecPublicKey", ecdsa_key:get_type());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
		it("can import EdDSA (Ed25519) keys", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
			local ed25519_key = crypto.import_private_pem(crypto.generate_ed25519_keypair():private_pem());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
			assert.equal("ED25519", ed25519_key:get_type());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
		it("can import RSA keys", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
			-- TODO
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
		it("rejects invalid keys", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
			assert.is_nil(crypto.import_private_pem(test_keys.eddsa_public_pem));
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
			assert.is_nil(crypto.import_private_pem(test_keys.ecdsa_public_pem));
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
			assert.is_nil(crypto.import_private_pem("foo"));
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
			assert.is_nil(crypto.import_private_pem(""));
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
	describe("import_public_pem", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
		it("can import ECDSA public keys", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
			local ecdsa_key = crypto.import_public_pem(test_keys.ecdsa_public_pem);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
			assert.equal("id-ecPublicKey", ecdsa_key:get_type());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		it("can import EdDSA (Ed25519) public keys", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
			local ed25519_key = crypto.import_public_pem(test_keys.eddsa_public_pem);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
			assert.equal("ED25519", ed25519_key:get_type());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		it("can import RSA public keys", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
			-- TODO
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	describe("PEM export", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		it("works", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
			local ecdsa_key = crypto.import_public_pem(test_keys.ecdsa_public_pem);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
			assert.equal("id-ecPublicKey", ecdsa_key:get_type());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
			assert.equal(test_keys.ecdsa_public_pem, ecdsa_key:public_pem());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
			assert.has_error(function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
				-- Fails because private key is not available
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
				ecdsa_key:private_pem();
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
			end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
			local ecdsa_private_key = crypto.import_private_pem(test_keys.ecdsa_private_pem);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
			assert.equal(test_keys.ecdsa_private_pem, ecdsa_private_key:private_pem());
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
	end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
	describe("sign/verify with", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
		local test_cases = {
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
			ed25519 = {
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
				crypto.ed25519_sign, crypto.ed25519_verify;
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
				key = crypto.import_private_pem(test_keys.eddsa_private_pem);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
				sig_length = 64;
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
			};
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
			ecdsa = {
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
				crypto.ecdsa_sha256_sign, crypto.ecdsa_sha256_verify;
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
				key = crypto.import_private_pem(test_keys.ecdsa_private_pem);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
			};
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
		};
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
		for test_name, test in pairs(test_cases) do
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
			local key = test.key;
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
			describe(test_name, function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
				it("works", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
					local sign, verify = test[1], test[2];
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
					local sig = assert(sign(key, "Hello world"));
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
					assert.is_string(sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
					if test.sig_length then
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
						assert.equal(test.sig_length, #sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
					end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
					do
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
						local ok = verify(key, "Hello world", sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
						assert.is_truthy(ok);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
					end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
					do -- Incorrect signature
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
						local ok = verify(key, "Hello world", sig:sub(1, -2)..string.char((sig:byte(-1)+1)%255));
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
						assert.is_falsy(ok);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
					end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
					do -- Incorrect message
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
						local ok = verify(key, "Hello earth", sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
						assert.is_falsy(ok);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
					end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
					do -- Incorrect message (embedded NUL)
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
						local ok = verify(key, "Hello world\0foo", sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
						assert.is_falsy(ok);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
					end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
				end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
			end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
		end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
	end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
	describe("ECDSA signatures", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
		local hex = require "util.hex";
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
		local sig = hex.decode((([[
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
			304402203e936e7b0bc62887e0e9d675afd08531a930384cfcf301
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
			f25d13053a2ebf141d02205a5a7c7b7ac5878d004cb79b17b39346
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
			6b0cd1043718ffc31c153b971d213a8e
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
		]]):gsub("%s+", "")));
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
		it("can be parsed", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
			local r, s = crypto.parse_ecdsa_signature(sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
			assert.is_string(r);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
			assert.is_string(s);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
			assert.equal(32, #r);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
			assert.equal(32, #s);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
		it("fails to parse invalid signatures", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
			local invalid_sigs = {
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
				"";
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
				"\000";
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
				string.rep("\000", 64);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
				string.rep("\000", 72);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
				string.rep("\000", 256);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
				string.rep("\255", 72);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
				string.rep("\255", 3);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
			};
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
			for _, sig in ipairs(invalid_sigs) do
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
				local r, s = crypto.parse_ecdsa_signature("");
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
				assert.is_nil(r);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
				assert.is_nil(s);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
			end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
			
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
		it("can be built", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
			local r, s = crypto.parse_ecdsa_signature(sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
			local rebuilt_sig = crypto.build_ecdsa_signature(r, s);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
			assert.equal(sig, rebuilt_sig);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
	end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
	describe("AES-GCM encryption", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
		it("works", function ()
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
			local message = "foo\0bar";
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
			local key_128_bit = random.bytes(16);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
			local key_256_bit = random.bytes(32);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
			local test_cases = {
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
				{ crypto.aes_128_gcm_encrypt, crypto.aes_128_gcm_decrypt, key = key_128_bit };
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
				{ crypto.aes_256_gcm_encrypt, crypto.aes_256_gcm_decrypt, key = key_256_bit };
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
			};
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
			for _, params in pairs(test_cases) do
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
				local iv = params.iv or random.bytes(12);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
				local encrypted = params[1](params.key, iv, message);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
				assert.not_equal(message, encrypted);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
				local decrypted = params[2](params.key, iv, encrypted);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
				assert.equal(message, decrypted);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
			end
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
		end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
	end);
7c5afbdcbc77 util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   196
end);