Mercurial > prosody-hg
annotate util-src/crypto.c @ 14063:e73fdb09f080
Merge 13.0->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 09 Feb 2026 01:57:07 +0100 |
| parents | fbfc06c5f3a8 |
| children | d975d280baec |
| rev | line source |
|---|---|
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 /* Prosody IM |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2022 Matthew Wild |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 -- |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 */ |
|
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 * crypto.c |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 * Lua library for cryptographic operations using OpenSSL |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 #include <string.h> |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 #include <stdlib.h> |
|
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 #ifdef _MSC_VER |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 typedef unsigned __int32 uint32_t; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 #else |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 #include <inttypes.h> |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 #endif |
|
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 #include "lua.h" |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 #include "lauxlib.h" |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 #include <openssl/crypto.h> |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 #include <openssl/ecdsa.h> |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 #include <openssl/err.h> |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 #include <openssl/evp.h> |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 #include <openssl/obj_mac.h> |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
30 #include <openssl/param_build.h> |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 #include <openssl/pem.h> |
|
14013
fbfc06c5f3a8
util.crypto: Add missing include for openssl/rsa.h
Matthew Wild <mwild1@gmail.com>
parents:
13537
diff
changeset
|
32 #include <openssl/rsa.h> |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 #if (LUA_VERSION_NUM == 501) |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 #endif |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
|
12735
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
38 /* The max size of an encoded 'R' or 'S' value. P-521 = 521 bits = 66 bytes */ |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
39 #define MAX_ECDSA_SIG_INT_BYTES 66 |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
40 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 #include "managed_pointer.h" |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 #define PKEY_MT_TAG "util.crypto key" |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
|
12876
0ed24f48b6a6
util.crypto: Preemptively silence 'strict-prototypes' warning
Kim Alvefur <zash@zash.se>
parents:
12837
diff
changeset
|
45 static BIO* new_memory_BIO(void) { |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 return BIO_new(BIO_s_mem()); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 MANAGED_POINTER_ALLOCATOR(new_managed_EVP_MD_CTX, EVP_MD_CTX*, EVP_MD_CTX_new, EVP_MD_CTX_free) |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 MANAGED_POINTER_ALLOCATOR(new_managed_BIO_s_mem, BIO*, new_memory_BIO, BIO_free) |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 MANAGED_POINTER_ALLOCATOR(new_managed_EVP_CIPHER_CTX, EVP_CIPHER_CTX*, EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_free) |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
|
12698
999663b4e39d
util.crypto: Friendlier error message on incorrect key types
Matthew Wild <mwild1@gmail.com>
parents:
12697
diff
changeset
|
53 #define CRYPTO_KEY_TYPE_ERR "unexpected key type: got '%s', expected '%s'" |
|
999663b4e39d
util.crypto: Friendlier error message on incorrect key types
Matthew Wild <mwild1@gmail.com>
parents:
12697
diff
changeset
|
54 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 static EVP_PKEY* pkey_from_arg(lua_State *L, int idx, const int type, const int require_private) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 EVP_PKEY *pkey = *(EVP_PKEY**)luaL_checkudata(L, idx, PKEY_MT_TAG); |
|
12697
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
57 int got_type; |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 if(type || require_private) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 lua_getuservalue(L, idx); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 if(type != 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 lua_getfield(L, -1, "type"); |
|
12697
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
62 got_type = lua_tointeger(L, -1); |
|
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
63 if(got_type != type) { |
|
12698
999663b4e39d
util.crypto: Friendlier error message on incorrect key types
Matthew Wild <mwild1@gmail.com>
parents:
12697
diff
changeset
|
64 const char *got_key_type_name = OBJ_nid2sn(got_type); |
|
999663b4e39d
util.crypto: Friendlier error message on incorrect key types
Matthew Wild <mwild1@gmail.com>
parents:
12697
diff
changeset
|
65 const char *want_key_type_name = OBJ_nid2sn(type); |
|
999663b4e39d
util.crypto: Friendlier error message on incorrect key types
Matthew Wild <mwild1@gmail.com>
parents:
12697
diff
changeset
|
66 lua_pushfstring(L, CRYPTO_KEY_TYPE_ERR, got_key_type_name, want_key_type_name); |
|
999663b4e39d
util.crypto: Friendlier error message on incorrect key types
Matthew Wild <mwild1@gmail.com>
parents:
12697
diff
changeset
|
67 luaL_argerror(L, idx, lua_tostring(L, -1)); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 lua_pop(L, 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if(require_private != 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 lua_getfield(L, -1, "private"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 if(lua_toboolean(L, -1) != 1) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 luaL_argerror(L, idx, "private key expected, got public key only"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 lua_pop(L, 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 lua_pop(L, 1); |
|
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 return pkey; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 static int Lpkey_finalizer(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 EVP_PKEY *pkey = pkey_from_arg(L, 1, 0, 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 EVP_PKEY_free(pkey); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 return 0; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 static int Lpkey_meth_get_type(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 EVP_PKEY *pkey = pkey_from_arg(L, 1, 0, 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 int key_type = EVP_PKEY_id(pkey); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 lua_pushstring(L, OBJ_nid2sn(key_type)); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 return 1; |
|
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 |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
97 static int Lpkey_meth_derive(lua_State *L) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
98 size_t outlen; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
99 EVP_PKEY *key = pkey_from_arg(L, 1, 0, 0); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
100 EVP_PKEY *peer = pkey_from_arg(L, 2, 0, 0); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
101 EVP_PKEY_CTX *ctx; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
102 BUF_MEM *buf; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
103 BIO *bio = new_managed_BIO_s_mem(L); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
104 BIO_get_mem_ptr(bio, &buf); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
105 if (!(ctx = EVP_PKEY_CTX_new(key, NULL))) |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
106 goto sslerr; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
107 if (EVP_PKEY_derive_init(ctx) <= 0) |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
108 goto sslerr; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
109 if (EVP_PKEY_derive_set_peer(ctx, peer) <= 0) |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
110 goto sslerr; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
111 if (EVP_PKEY_derive(ctx, NULL, &outlen) <= 0) |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
112 goto sslerr; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
113 if (!BUF_MEM_grow_clean(buf, outlen)) |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
114 goto sslerr; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
115 if (EVP_PKEY_derive(ctx, (unsigned char*)buf->data, &outlen) <= 0) |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
116 goto sslerr; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
117 EVP_PKEY_CTX_free(ctx); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
118 ctx = NULL; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
119 lua_pushlstring(L, buf->data, outlen); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
120 BIO_reset(bio); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
121 return 1; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
122 sslerr: |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
123 if (ctx) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
124 EVP_PKEY_CTX_free(ctx); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
125 ctx = NULL; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
126 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
127 BIO_reset(bio); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
128 return luaL_error(L, "pkey:derive failed"); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
129 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
130 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 static int base_evp_sign(lua_State *L, const int key_type, const EVP_MD *digest_type) { |
|
12697
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
132 EVP_PKEY *pkey = pkey_from_arg(L, 1, (key_type!=NID_rsassaPss)?key_type:NID_rsaEncryption, 1); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 luaL_Buffer sigbuf; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 size_t msg_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 const unsigned char* msg = (unsigned char*)lua_tolstring(L, 2, &msg_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 size_t sig_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 unsigned char *sig = NULL; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 EVP_MD_CTX *md_ctx = new_managed_EVP_MD_CTX(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 if(EVP_DigestSignInit(md_ctx, NULL, digest_type, NULL, pkey) != 1) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 } |
|
12697
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
146 if(key_type == NID_rsassaPss) { |
|
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
147 EVP_PKEY_CTX_set_rsa_padding(EVP_MD_CTX_pkey_ctx(md_ctx), RSA_PKCS1_PSS_PADDING); |
|
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
148 } |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 if(EVP_DigestSign(md_ctx, NULL, &sig_len, msg, msg_len) != 1) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 // COMPAT w/ Lua 5.1 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 luaL_buffinit(L, &sigbuf); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 sig = memset(luaL_prepbuffer(&sigbuf), 0, sig_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 if(EVP_DigestSign(md_ctx, sig, &sig_len, msg, msg_len) != 1) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 luaL_addsize(&sigbuf, sig_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 luaL_pushresult(&sigbuf); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 } |
|
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 static int base_evp_verify(lua_State *L, const int key_type, const EVP_MD *digest_type) { |
|
12697
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
171 EVP_PKEY *pkey = pkey_from_arg(L, 1, (key_type!=NID_rsassaPss)?key_type:NID_rsaEncryption, 0); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 size_t msg_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 const unsigned char *msg = (unsigned char*)luaL_checklstring(L, 2, &msg_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 size_t sig_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 const unsigned char *sig = (unsigned char*)luaL_checklstring(L, 3, &sig_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 if(EVP_DigestVerifyInit(md_ctx, NULL, digest_type, NULL, pkey) != 1) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 goto cleanup; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 } |
|
12697
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
185 if(key_type == NID_rsassaPss) { |
|
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
186 EVP_PKEY_CTX_set_rsa_padding(EVP_MD_CTX_pkey_ctx(md_ctx), RSA_PKCS1_PSS_PADDING); |
|
916871447b2f
util.crypto: Add support for RSA signatures (PKCS1-v1.5 + PSS)
Matthew Wild <mwild1@gmail.com>
parents:
12693
diff
changeset
|
187 } |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 int result = EVP_DigestVerify(md_ctx, sig, sig_len, msg, msg_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 if(result == 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 lua_pushboolean(L, 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 } else if(result != 1) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 lua_pushboolean(L, 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 cleanup: |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 EVP_MD_CTX_free(md_ctx); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
202 static int Lpkey_meth_public_raw(lua_State *L) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
203 OSSL_PARAM *params; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
204 EVP_PKEY *pkey = pkey_from_arg(L, 1, 0, 0); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
205 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
206 if (EVP_PKEY_todata(pkey, EVP_PKEY_PUBLIC_KEY, ¶ms)) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
207 OSSL_PARAM *item = params; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
208 while (item->key) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
209 if (!strcmp("pub", item->key)) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
210 lua_pushlstring(L, item->data, item->data_size); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
211 break; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
212 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
213 item++; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
214 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
215 if (!item->key) lua_pushnil(L); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
216 OSSL_PARAM_free(params); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
217 } else { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
218 lua_pushnil(L); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
219 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
220 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
221 return 1; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
222 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
223 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 static int Lpkey_meth_public_pem(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 char *data; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 size_t bytes; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 EVP_PKEY *pkey = pkey_from_arg(L, 1, 0, 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 BIO *bio = new_managed_BIO_s_mem(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 if(PEM_write_bio_PUBKEY(bio, pkey)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 bytes = BIO_get_mem_data(bio, &data); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 if (bytes > 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 lua_pushlstring(L, data, bytes); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 static int Lpkey_meth_private_pem(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 char *data; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 size_t bytes; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 EVP_PKEY *pkey = pkey_from_arg(L, 1, 0, 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 BIO *bio = new_managed_BIO_s_mem(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 if(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 bytes = BIO_get_mem_data(bio, &data); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 if (bytes > 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 lua_pushlstring(L, data, bytes); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 static int push_pkey(lua_State *L, EVP_PKEY *pkey, const int type, const int privkey) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 EVP_PKEY **ud = lua_newuserdata(L, sizeof(EVP_PKEY*)); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 *ud = pkey; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 luaL_newmetatable(L, PKEY_MT_TAG); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 lua_setmetatable(L, -2); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 /* Set some info about the key and attach it as a user value */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 lua_newtable(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 if(type != 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 lua_pushinteger(L, type); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 lua_setfield(L, -2, "type"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 if(privkey != 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 lua_pushboolean(L, 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
279 lua_setfield(L, -2, "private"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 lua_setuservalue(L, -2); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 static int Lgenerate_ed25519_keypair(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 EVP_PKEY *pkey = NULL; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 /* Generate key */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 EVP_PKEY_keygen_init(pctx); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 EVP_PKEY_keygen(pctx, &pkey); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 EVP_PKEY_CTX_free(pctx); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 push_pkey(L, pkey, NID_ED25519, 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
296 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
297 |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
298 static int Lgenerate_p256_keypair(lua_State *L) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
299 EVP_PKEY *pkey = NULL; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
300 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
301 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
302 /* Generate key */ |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
303 if (EVP_PKEY_keygen_init(pctx) <= 0) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
304 if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1) <= 0) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
305 if (EVP_PKEY_keygen(pctx, &pkey) <= 0) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
306 EVP_PKEY_CTX_free(pctx); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
307 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
308 push_pkey(L, pkey, NID_X9_62_prime256v1, 1); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
309 return 1; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
310 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
311 err: |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
312 if (pctx) EVP_PKEY_CTX_free(pctx); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
313 lua_pushnil(L); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
314 return 1; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
315 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
316 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
317 static int Limport_private_pem(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
318 EVP_PKEY *pkey = NULL; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
319 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
320 size_t privkey_bytes; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
321 const char* privkey_data; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 BIO *bio = new_managed_BIO_s_mem(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
324 privkey_data = luaL_checklstring(L, 1, &privkey_bytes); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 BIO_write(bio, privkey_data, privkey_bytes); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 if (pkey) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
328 push_pkey(L, pkey, EVP_PKEY_id(pkey), 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
330 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
331 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
332 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
335 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
336 |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
337 static int Limport_public_ec_raw(lua_State *L) { |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
338 OSSL_PARAM_BLD *param_bld = NULL; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
339 OSSL_PARAM *params = NULL; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
340 EVP_PKEY_CTX *ctx = NULL; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
341 EVP_PKEY *pkey = NULL; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
342 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
343 size_t pubkey_bytes; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
344 const char* pubkey_data = luaL_checklstring(L, 1, &pubkey_bytes); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
345 const char* curve = luaL_checkstring(L, 2); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
346 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
347 param_bld = OSSL_PARAM_BLD_new(); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
348 if (!param_bld) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
349 if (!OSSL_PARAM_BLD_push_utf8_string(param_bld, "group", curve, 0)) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
350 if (!OSSL_PARAM_BLD_push_octet_string(param_bld, "pub", pubkey_data, pubkey_bytes)) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
351 params = OSSL_PARAM_BLD_to_param(param_bld); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
352 if (!params) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
353 ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
354 if (!ctx) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
355 if (!EVP_PKEY_fromdata_init(ctx)) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
356 if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0) goto err; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
357 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
358 push_pkey(L, pkey, EVP_PKEY_id(pkey), 0); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
359 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
360 EVP_PKEY_CTX_free(ctx); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
361 OSSL_PARAM_free(params); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
362 OSSL_PARAM_BLD_free(param_bld); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
363 |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
364 return 1; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
365 err: |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
366 if (ctx) EVP_PKEY_CTX_free(ctx); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
367 if (params) OSSL_PARAM_free(params); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
368 if (param_bld) OSSL_PARAM_BLD_free(param_bld); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
369 lua_pushnil(L); |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
370 return 1; |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
371 } |
|
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
372 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
373 static int Limport_public_pem(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
374 EVP_PKEY *pkey = NULL; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
375 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
376 size_t pubkey_bytes; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
377 const char* pubkey_data; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
378 BIO *bio = new_managed_BIO_s_mem(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
379 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
380 pubkey_data = luaL_checklstring(L, 1, &pubkey_bytes); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
381 BIO_write(bio, pubkey_data, pubkey_bytes); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
382 pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
383 if (pkey) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
384 push_pkey(L, pkey, EVP_PKEY_id(pkey), 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
385 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
386 else { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
387 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
388 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
389 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
390 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
391 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
392 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
393 static int Led25519_sign(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
394 return base_evp_sign(L, NID_ED25519, NULL); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
395 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
396 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
397 static int Led25519_verify(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
398 return base_evp_verify(L, NID_ED25519, NULL); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
399 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
400 |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
401 /* encrypt(key, iv, plaintext) */ |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
402 static int Levp_encrypt(lua_State *L, const EVP_CIPHER *cipher, const unsigned char expected_key_len, const unsigned char expected_iv_len, const size_t tag_len) { |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
403 EVP_CIPHER_CTX *ctx; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
404 luaL_Buffer ciphertext_buffer; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
405 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
406 size_t key_len, iv_len, plaintext_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
407 int ciphertext_len, final_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
408 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
409 const unsigned char *key = (unsigned char*)luaL_checklstring(L, 1, &key_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
410 const unsigned char *iv = (unsigned char*)luaL_checklstring(L, 2, &iv_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
411 const unsigned char *plaintext = (unsigned char*)luaL_checklstring(L, 3, &plaintext_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
412 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
413 if(key_len != expected_key_len) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
414 return luaL_error(L, "key must be %d bytes", expected_key_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
415 } |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
416 if(iv_len != expected_iv_len) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
417 return luaL_error(L, "iv must be %d bytes", expected_iv_len); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
418 } |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
419 if(lua_gettop(L) > 3) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
420 return luaL_error(L, "Expected 3 arguments, got %d", lua_gettop(L)); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
421 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
422 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
423 // Create and initialise the context |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
424 ctx = new_managed_EVP_CIPHER_CTX(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
425 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
426 // Initialise the encryption operation |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
427 if(1 != EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
428 return luaL_error(L, "Error while initializing encryption engine"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
429 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
430 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
431 // Initialise key and IV |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
432 if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
433 return luaL_error(L, "Error while initializing key/iv"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
434 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
435 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
436 luaL_buffinit(L, &ciphertext_buffer); |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
437 unsigned char *ciphertext = (unsigned char*)luaL_prepbuffsize(&ciphertext_buffer, plaintext_len+tag_len); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
438 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
439 if(1 != EVP_EncryptUpdate(ctx, ciphertext, &ciphertext_len, plaintext, plaintext_len)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
440 return luaL_error(L, "Error while encrypting data"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
441 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
442 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
443 /* |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
444 * Finalise the encryption. Normally ciphertext bytes may be written at |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
445 * this stage, but this does not occur in GCM mode |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
446 */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
447 if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + ciphertext_len, &final_len)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
448 return luaL_error(L, "Error while encrypting final data"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
449 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
450 if(final_len != 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
451 return luaL_error(L, "Non-zero final data"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
452 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
453 |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
454 if(tag_len > 0) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
455 /* Get the tag */ |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
456 if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_len, ciphertext + ciphertext_len)) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
457 return luaL_error(L, "Unable to read AEAD tag of encrypted data"); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
458 } |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
459 /* Append tag */ |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
460 luaL_addsize(&ciphertext_buffer, ciphertext_len + tag_len); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
461 } else { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
462 luaL_addsize(&ciphertext_buffer, ciphertext_len); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
463 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
464 luaL_pushresult(&ciphertext_buffer); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
465 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
466 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
467 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
468 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
469 static int Laes_128_gcm_encrypt(lua_State *L) { |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
470 return Levp_encrypt(L, EVP_aes_128_gcm(), 16, 12, 16); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
471 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
472 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
473 static int Laes_256_gcm_encrypt(lua_State *L) { |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
474 return Levp_encrypt(L, EVP_aes_256_gcm(), 32, 12, 16); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
475 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
476 |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
477 static int Laes_256_ctr_encrypt(lua_State *L) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
478 return Levp_encrypt(L, EVP_aes_256_ctr(), 32, 16, 0); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
479 } |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
480 |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
481 /* decrypt(key, iv, ciphertext) */ |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
482 static int Levp_decrypt(lua_State *L, const EVP_CIPHER *cipher, const unsigned char expected_key_len, const unsigned char expected_iv_len, const size_t tag_len) { |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
483 EVP_CIPHER_CTX *ctx; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
484 luaL_Buffer plaintext_buffer; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
485 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
486 size_t key_len, iv_len, ciphertext_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
487 int plaintext_len, final_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
488 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
489 const unsigned char *key = (unsigned char*)luaL_checklstring(L, 1, &key_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
490 const unsigned char *iv = (unsigned char*)luaL_checklstring(L, 2, &iv_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
491 const unsigned char *ciphertext = (unsigned char*)luaL_checklstring(L, 3, &ciphertext_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
492 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
493 if(key_len != expected_key_len) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
494 return luaL_error(L, "key must be %d bytes", expected_key_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
495 } |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
496 if(iv_len != expected_iv_len) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
497 return luaL_error(L, "iv must be %d bytes", expected_iv_len); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
498 } |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
499 if(ciphertext_len <= tag_len) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
500 return luaL_error(L, "ciphertext must be at least %d bytes (including tag)", tag_len); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
501 } |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
502 if(lua_gettop(L) > 3) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
503 return luaL_error(L, "Expected 3 arguments, got %d", lua_gettop(L)); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
504 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
505 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
506 /* Create and initialise the context */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
507 ctx = new_managed_EVP_CIPHER_CTX(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
508 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
509 /* Initialise the decryption operation. */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
510 if(!EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
511 return luaL_error(L, "Error while initializing decryption engine"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
512 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
513 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
514 /* Initialise key and IV */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
515 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv)) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
516 return luaL_error(L, "Error while initializing key/iv"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
517 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
518 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
519 luaL_buffinit(L, &plaintext_buffer); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
520 unsigned char *plaintext = (unsigned char*)luaL_prepbuffsize(&plaintext_buffer, ciphertext_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
521 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
522 /* |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
523 * Provide the message to be decrypted, and obtain the plaintext output. |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
524 * EVP_DecryptUpdate can be called multiple times if necessary |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
525 */ |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
526 if(!EVP_DecryptUpdate(ctx, plaintext, &plaintext_len, ciphertext, ciphertext_len-tag_len)) { |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
527 return luaL_error(L, "Error while decrypting data"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
528 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
529 |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
530 if(tag_len > 0) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
531 /* Set expected tag value. Works in OpenSSL 1.0.1d and later */ |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
532 if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, (unsigned char*)ciphertext + (ciphertext_len-tag_len))) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
533 return luaL_error(L, "Error while processing authentication tag"); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
534 } |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
535 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
536 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
537 /* |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
538 * Finalise the decryption. A positive return value indicates success, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
539 * anything else is a failure - the plaintext is not trustworthy. |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
540 */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
541 int ret = EVP_DecryptFinal_ex(ctx, plaintext + plaintext_len, &final_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
542 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
543 if(ret <= 0) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
544 /* Verify failed */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
545 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
546 lua_pushliteral(L, "verify-failed"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
547 return 2; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
548 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
549 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
550 luaL_addsize(&plaintext_buffer, plaintext_len + final_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
551 luaL_pushresult(&plaintext_buffer); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
552 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
553 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
554 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
555 static int Laes_128_gcm_decrypt(lua_State *L) { |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
556 return Levp_decrypt(L, EVP_aes_128_gcm(), 16, 12, 16); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
557 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
558 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
559 static int Laes_256_gcm_decrypt(lua_State *L) { |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
560 return Levp_decrypt(L, EVP_aes_256_gcm(), 32, 12, 16); |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
561 } |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
562 |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
563 static int Laes_256_ctr_decrypt(lua_State *L) { |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
564 return Levp_decrypt(L, EVP_aes_256_ctr(), 32, 16, 0); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
565 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
566 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
567 /* r, s = parse_ecdsa_sig(sig_der) */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
568 static int Lparse_ecdsa_signature(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
569 ECDSA_SIG *sig; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
570 size_t sig_der_len; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
571 const unsigned char *sig_der = (unsigned char*)luaL_checklstring(L, 1, &sig_der_len); |
|
12735
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
572 const size_t sig_int_bytes = luaL_checkinteger(L, 2); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
573 const BIGNUM *r, *s; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
574 int rlen, slen; |
|
12735
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
575 unsigned char rb[MAX_ECDSA_SIG_INT_BYTES]; |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
576 unsigned char sb[MAX_ECDSA_SIG_INT_BYTES]; |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
577 |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
578 if(sig_int_bytes > MAX_ECDSA_SIG_INT_BYTES) { |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
579 luaL_error(L, "requested signature size exceeds supported limit"); |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
580 } |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
581 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
582 sig = d2i_ECDSA_SIG(NULL, &sig_der, sig_der_len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
583 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
584 if(sig == NULL) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
585 lua_pushnil(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
586 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
587 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
588 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
589 ECDSA_SIG_get0(sig, &r, &s); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
590 |
|
12735
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
591 rlen = BN_bn2binpad(r, rb, sig_int_bytes); |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
592 slen = BN_bn2binpad(s, sb, sig_int_bytes); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
593 |
|
12735
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
594 if (rlen == -1 || slen == -1) { |
|
12714
82bca7191f13
util.crypto: Use stack space buffers
Kim Alvefur <zash@zash.se>
parents:
12702
diff
changeset
|
595 ECDSA_SIG_free(sig); |
|
12735
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
596 luaL_error(L, "encoded integers exceed requested size"); |
|
12714
82bca7191f13
util.crypto: Use stack space buffers
Kim Alvefur <zash@zash.se>
parents:
12702
diff
changeset
|
597 } |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
598 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
599 ECDSA_SIG_free(sig); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
600 |
|
12714
82bca7191f13
util.crypto: Use stack space buffers
Kim Alvefur <zash@zash.se>
parents:
12702
diff
changeset
|
601 lua_pushlstring(L, (const char*)rb, rlen); |
|
82bca7191f13
util.crypto: Use stack space buffers
Kim Alvefur <zash@zash.se>
parents:
12702
diff
changeset
|
602 lua_pushlstring(L, (const char*)sb, slen); |
|
82bca7191f13
util.crypto: Use stack space buffers
Kim Alvefur <zash@zash.se>
parents:
12702
diff
changeset
|
603 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
604 return 2; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
605 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
606 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
607 /* sig_der = 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
|
608 static int Lbuild_ecdsa_signature(lua_State *L) { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
609 ECDSA_SIG *sig = ECDSA_SIG_new(); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
610 BIGNUM *r, *s; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
611 luaL_Buffer sigbuf; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
612 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
613 size_t rlen, slen; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
614 const unsigned char *rbin, *sbin; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
615 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
616 rbin = (unsigned char*)luaL_checklstring(L, 1, &rlen); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
617 sbin = (unsigned char*)luaL_checklstring(L, 2, &slen); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
618 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
619 r = BN_bin2bn(rbin, (int)rlen, NULL); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
620 s = BN_bin2bn(sbin, (int)slen, NULL); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
621 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
622 ECDSA_SIG_set0(sig, r, s); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
623 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
624 luaL_buffinit(L, &sigbuf); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
625 |
|
12735
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
626 /* DER structure of an ECDSA signature has 7 bytes plus the integers themselves, |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
627 which may gain an extra byte once encoded */ |
|
445f7bd6ffc4
util.crypto, util.jwt: Generate consistent signature sizes (via padding)
Matthew Wild <mwild1@gmail.com>
parents:
12715
diff
changeset
|
628 unsigned char *buffer = (unsigned char*)luaL_prepbuffsize(&sigbuf, (rlen+1)+(slen+1)+7); |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
629 int len = i2d_ECDSA_SIG(sig, &buffer); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
630 luaL_addsize(&sigbuf, len); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
631 luaL_pushresult(&sigbuf); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
632 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
633 ECDSA_SIG_free(sig); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
634 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
635 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
636 } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
637 |
|
12702
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
638 #define REG_SIGN_VERIFY(algorithm, digest) \ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
639 { #algorithm "_" #digest "_sign", L ## algorithm ## _ ## digest ## _sign },\ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
640 { #algorithm "_" #digest "_verify", L ## algorithm ## _ ## digest ## _verify }, |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
641 |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
642 #define IMPL_SIGN_VERIFY(algorithm, key_type, digest) \ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
643 static int L ## algorithm ## _ ## digest ## _sign(lua_State *L) { \ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
644 return base_evp_sign(L, key_type, EVP_ ## digest()); \ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
645 } \ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
646 static int L ## algorithm ## _ ## digest ## _verify(lua_State *L) { \ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
647 return base_evp_verify(L, key_type, EVP_ ## digest()); \ |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
648 } |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
649 |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
650 IMPL_SIGN_VERIFY(ecdsa, NID_X9_62_id_ecPublicKey, sha256) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
651 IMPL_SIGN_VERIFY(ecdsa, NID_X9_62_id_ecPublicKey, sha384) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
652 IMPL_SIGN_VERIFY(ecdsa, NID_X9_62_id_ecPublicKey, sha512) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
653 |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
654 IMPL_SIGN_VERIFY(rsassa_pkcs1, NID_rsaEncryption, sha256) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
655 IMPL_SIGN_VERIFY(rsassa_pkcs1, NID_rsaEncryption, sha384) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
656 IMPL_SIGN_VERIFY(rsassa_pkcs1, NID_rsaEncryption, sha512) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
657 |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
658 IMPL_SIGN_VERIFY(rsassa_pss, NID_rsassaPss, sha256) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
659 IMPL_SIGN_VERIFY(rsassa_pss, NID_rsassaPss, sha384) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
660 IMPL_SIGN_VERIFY(rsassa_pss, NID_rsassaPss, sha512) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
661 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
662 static const luaL_Reg Reg[] = { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
663 { "ed25519_sign", Led25519_sign }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
664 { "ed25519_verify", Led25519_verify }, |
|
12702
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
665 |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
666 REG_SIGN_VERIFY(ecdsa, sha256) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
667 REG_SIGN_VERIFY(ecdsa, sha384) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
668 REG_SIGN_VERIFY(ecdsa, sha512) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
669 |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
670 REG_SIGN_VERIFY(rsassa_pkcs1, sha256) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
671 REG_SIGN_VERIFY(rsassa_pkcs1, sha384) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
672 REG_SIGN_VERIFY(rsassa_pkcs1, sha512) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
673 |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
674 REG_SIGN_VERIFY(rsassa_pss, sha256) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
675 REG_SIGN_VERIFY(rsassa_pss, sha384) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
676 REG_SIGN_VERIFY(rsassa_pss, sha512) |
|
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
677 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
678 { "aes_128_gcm_encrypt", Laes_128_gcm_encrypt }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
679 { "aes_128_gcm_decrypt", Laes_128_gcm_decrypt }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
680 { "aes_256_gcm_encrypt", Laes_256_gcm_encrypt }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
681 { "aes_256_gcm_decrypt", Laes_256_gcm_decrypt }, |
|
12702
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
682 |
|
12837
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
683 { "aes_256_ctr_encrypt", Laes_256_ctr_encrypt }, |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
684 { "aes_256_ctr_decrypt", Laes_256_ctr_decrypt }, |
|
d3ae47d8a7a7
util.crypto: Add support for AES-256-CTR
Matthew Wild <mwild1@gmail.com>
parents:
12735
diff
changeset
|
685 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
686 { "generate_ed25519_keypair", Lgenerate_ed25519_keypair }, |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
687 { "generate_p256_keypair", Lgenerate_p256_keypair }, |
|
12702
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
688 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
689 { "import_private_pem", Limport_private_pem }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
690 { "import_public_pem", Limport_public_pem }, |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
691 { "import_public_ec_raw", Limport_public_ec_raw }, |
|
12702
f63176781940
util.crypto: More digests for sign/verify, use macros for clarity/consistency
Matthew Wild <mwild1@gmail.com>
parents:
12698
diff
changeset
|
692 |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
693 { "parse_ecdsa_signature", Lparse_ecdsa_signature }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
694 { "build_ecdsa_signature", Lbuild_ecdsa_signature }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
695 { NULL, NULL } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
696 }; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
697 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
698 static const luaL_Reg KeyMethods[] = { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
699 { "private_pem", Lpkey_meth_private_pem }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
700 { "public_pem", Lpkey_meth_public_pem }, |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
701 { "public_raw", Lpkey_meth_public_raw }, |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
702 { "get_type", Lpkey_meth_get_type }, |
|
13537
fb970df95374
util.crypto: Add more ECC methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
12976
diff
changeset
|
703 { "derive", Lpkey_meth_derive }, |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
704 { NULL, NULL } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
705 }; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
706 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
707 static const luaL_Reg KeyMetatable[] = { |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
708 { "__gc", Lpkey_finalizer }, |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
709 { NULL, NULL } |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
710 }; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
711 |
|
12976
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12876
diff
changeset
|
712 LUALIB_API int luaopen_prosody_util_crypto(lua_State *L) { |
|
12693
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
713 #if (LUA_VERSION_NUM > 501) |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
714 luaL_checkversion(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
715 #endif |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
716 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
717 /* Initialize pkey metatable */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
718 luaL_newmetatable(L, PKEY_MT_TAG); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
719 luaL_setfuncs(L, KeyMetatable, 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
720 lua_newtable(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
721 luaL_setfuncs(L, KeyMethods, 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
722 lua_setfield(L, -2, "__index"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
723 lua_pop(L, 1); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
724 |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
725 /* Initialize lib table */ |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
726 lua_newtable(L); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
727 luaL_setfuncs(L, Reg, 0); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
728 lua_pushliteral(L, "-3.14"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
729 lua_setfield(L, -2, "version"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
730 #ifdef OPENSSL_VERSION |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
731 lua_pushstring(L, OpenSSL_version(OPENSSL_VERSION)); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
732 lua_setfield(L, -2, "_LIBCRYPTO_VERSION"); |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
733 #endif |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
734 return 1; |
|
7c5afbdcbc77
util.crypto: New wrapper for some operations in OpenSSL's libcrypto
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
735 } |
|
12976
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12876
diff
changeset
|
736 |
|
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12876
diff
changeset
|
737 LUALIB_API int luaopen_util_crypto(lua_State *L) { |
|
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12876
diff
changeset
|
738 return luaopen_prosody_util_crypto(L); |
|
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12876
diff
changeset
|
739 } |
