diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2024-11-18 18:35:26 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2024-11-22 12:59:13 +0100 |
commit | 0badc88d7a39f431cbc53ba6be61113b1f2d638d (patch) | |
tree | ef04e31b801c36b0396d2b6683f6ecf675957c18 | |
parent | c626e488ba7c1b3c5932dc887402b9ba4c320f29 (diff) |
lib: add crypto.uccrypto
The module crypto.uc uses one of crypto_openssl and crypto_mbedtls
to provide a cryptographic implementation to the user. The crypto_openssl
module is preferred since it supports EdDSA in addition to RSA and ECDSA,
which are also supported by crypto_mbedtls.
Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | jsdoc/conf.json | 4 | ||||
-rw-r--r-- | lib/crypto.uc | 146 |
3 files changed, 152 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 727a9b8..e5a5d27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -318,6 +318,10 @@ if(CRYPTO_OPENSSL_SUPPORT) target_link_libraries(crypto_openssl_lib crypto) endif() +if(CRYPTO_MBEDTLS_SUPPORT OR CRYPTO_OPENSSL_SUPPORT) + install(FILES lib/crypto.uc DESTINATION share/ucode) +endif() + if(UNIT_TESTING) enable_testing() add_definitions(-DUNIT_TESTING) diff --git a/jsdoc/conf.json b/jsdoc/conf.json index 9d3c995..523243b 100644 --- a/jsdoc/conf.json +++ b/jsdoc/conf.json @@ -5,8 +5,8 @@ }, "source": { "include": ["."], - "exclude": ["CMakeFiles"], - "includePattern": ".+\\.c$" + "exclude": ["CMakeFiles", "tests"], + "includePattern": ".+\\.(c|uc)$" }, "plugins": [ "plugins/markdown", diff --git a/lib/crypto.uc b/lib/crypto.uc new file mode 100644 index 0000000..43581c5 --- /dev/null +++ b/lib/crypto.uc @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2024 Mikael Magnusson <mikma@users.sourceforge.net> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/** + * # Crypto + * + * The `crypto` module provides message digest and message signing and + * verification. There are two alternative implementations, `crypto_openssl` + * and `crypto_mbedtls`. The `crypto_openssl` is preferred if available since + * it supports EdDSA in addition to all algorithms supported + * by `crypto_mbedtls`. + * + * @module crypto + */ + +/** + * Represents a public key context. + * + * @class module:crypto.pk + * @hideconstructor + */ + +/** + * @function module:crypto#md_digest + * + * @param {string} alg + * Message digest algorithm. + * + * @param {string} input + * Input to the message digest algorithm. + * + * @returns {string} + */ + +/** + * @function module:crypto#pk + * + * @returns {crypto.pk} + */ + +/** + * @function module:crypto.pk#get_public_key + * + * @returns {?string} - Public key in DER format. + */ + +/** + * @function module:crypto.pk#keygen + * + * @param {('EC'|'RSA'|'ED25519')} type + * Public key type. + * + * @param {('P-192'|'P-224'|'P-256'|'P-384'|'P-521'|'brainpoolP256r1'|'brainpoolP384r1'|'brainpoolP512r1'|number)} [param] + * EC curve name (`string`), or RSA key length (`number`). + * + * @returns {string} + */ + +/** + * @function module:crypto.pk#set_public_key + * + * @param {?string} key + * A public key in DER format. + */ + +/** + * Available only if the `crypto_openssl` module is installed. + * + * @function module:crypto.pk#set_raw_public_key + * + * @param {('ED25519'|'ED448')} type + * @param {string} key + * Public key in raw format. + */ + +/** + * @function module:crypto.pk#sign + * + * @param {?('SHA1'|'SHA224'|'SHA256'|'SHA384'|'SHA512'|string)} alg + * The message digest algorithm. + * + * @param {string} input + * The message to be signed. + * + * @returns {string} + */ + +/** + * @function module:crypto.pk#verify + * + * @param {?('SHA1'|'SHA224'|'SHA256'|'SHA384'|'SHA512'|string)} alg + * The message digest algorithm. + * + * @param {string} input + * The message to be verified. + * + * @param {string} sig + * The signature to be verified. + * + * @returns {boolean} + */ + +let crypto; + +try { + crypto = require('crypto_openssl'); +} catch { + try { + crypto = require('crypto_mbedtls'); + } catch { + die(`No module named 'crypto_openssl' or 'crypto_mbedtls' could be found`); + } +} + +export +function md_digest(...args) { + return crypto.md_digest(...args); +}; + +export +function md_list(...args) { + return crypto.md_list(...args); +}; + +export +function pk_list(...args) { + return crypto.pk_list(...args); +}; + +export +function pk(...args) { + return crypto.pk(...args); +}; |