Ecc Lua Reference Documentation
chilkat.Ecc
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.Ecc
Create elliptic-curve private keys for supported curves and use the
resulting key with Chilkat signing, verification, or key-agreement APIs.
Sign hashes or input data using an EC private key, producing ECDSA
signatures in the configured output format.
Verify ECDSA signatures using the matching EC public key and the original
hash or data.
Sign with an ECDSA certificate when the certificate has access to its
associated private key.
Compute an encoded shared secret from compatible EC private and public
keys for key-agreement workflows.
Use
For an extended overview, see
Ecc Class Overview.
Generate EC keys, create ECDSA signatures, verify signatures, and compute shared secrets.
Chilkat.Ecc is the Chilkat class for direct elliptic-curve
cryptographic operations. It can generate EC private keys, sign hashes or
data using ECDSA, verify ECDSA signatures, sign through ECDSA certificates,
and compute encoded shared secrets from compatible EC key pairs. It is used
when an application needs explicit control over elliptic-curve keys,
signatures, signature encoding, or key-agreement output.
Generate EC private keys
ECDSA signing
ECDSA verification
Certificate-based signing
Shared-secret calculation
Signature format control
AsnFormat to control whether ECDSA signatures are
encoded as ASN.1/DER or as raw r || s values.
AsnFormat, then sign or verify using the method that matches the
input form: hash, data, key, or certificate. For shared-secret workflows,
compute the ECDH-style secret and pass the result through the key derivation
or encoding step required by the protocol.
Object Creation
local chilkat = require("chilkat")
local ecc = chilkat.Ecc.new()Creates the object (chilkat.Ecc() does the same). Lua's garbage collector frees it when it is no longer referenced; ecc:dispose() frees it at once, and in Lua 5.4 and later local ecc <close> = chilkat.Ecc.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = ecc:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (ecc.verbose_logging = true) and methods use the colon (ecc:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
AsnFormat
local boolVal = ecc.asn_format
ecc.asn_format = boolVal
Controls the binary layout of ECDSA signatures created by this object. The default is true.
| Value | Signature layout |
|---|---|
true | ASN.1 DER: a SEQUENCE containing the two INTEGER values r and s. Its encoded length can vary because DER INTEGER values may require leading sign-padding bytes. |
false | Raw format: fixed-width, unsigned big-endian r followed immediately by fixed-width s, commonly written r || s. |
r || s signatures.DebugLogFilePath
local strVal = ecc.debug_log_file_path
ecc.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
LastErrorHtml
local strVal = ecc.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = ecc.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = ecc.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = ecc.last_method_success
ecc.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
VerboseLogging
local boolVal = ecc.verbose_logging
ecc.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = ecc.version
Methods
GenKey
-- prng is a chilkat.Prng
-- priv_key is a chilkat.PrivateKey
-- ok is true, or nil and err on failure
local ok, err = ecc:gen_key(curve_name, prng, priv_key)
Generates a new elliptic-curve private key and stores it in priv_key. curve_name selects the named curve, and prng supplies the cryptographically secure random data used to choose the private scalar. Returns true on success and false on failure.
Supported curve names are:
| Curve name | Notes |
|---|---|
secp256r1 | Also known as P-256 and prime256v1. |
secp384r1 | Also known as P-384. |
secp521r1 | Also known as P-521. |
secp256k1 | Widely associated with Bitcoin and related systems. |
secp192r1, secp224r1 | Older, smaller SEC curves retained for compatibility. |
brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1, brainpoolP320r1, brainpoolP384r1, brainpoolP512r1 | Brainpool prime-field curves. |
secp256k1 is commonly required by Bitcoin-related systems. Older and smaller curves should generally be used only for compatibility with an existing format or peer.prng. Use a properly initialized cryptographic Prng.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GenKey2
-- encoded_k is a string
-- encoding is a string
-- priv_key is a chilkat.PrivateKey
-- ok is true, or nil and err on failure
local ok, err = ecc:gen_key2(curve_name, encoded_k, encoding, priv_key)
Constructs an elliptic-curve private key in priv_key from a caller-supplied private scalar. curve_name selects the named curve, encoded_k contains the scalar, and encoding names its representation, such as hex, base64, or decimal. Returns true on success and false on failure.
Supported curve names are:
| Curve name | Notes |
|---|---|
secp256r1 | Also known as P-256 and prime256v1. |
secp384r1 | Also known as P-384. |
secp521r1 | Also known as P-521. |
secp256k1 | Widely associated with Bitcoin and related systems. |
secp192r1, secp224r1 | Older, smaller SEC curves retained for compatibility. |
brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1, brainpoolP320r1, brainpoolP384r1, brainpoolP512r1 | Brainpool prime-field curves. |
encodedK is the private key value: Despite the historical argument name, encoded_k is the long-lived private scalar used to construct the key. It is not the temporary ECDSA signing nonce also commonly denoted by k. The value must be valid for the selected curve, unpredictable when used as a real key, and kept secret.GenKey to create a new random private key.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SharedSecretENC
-- pub_key is a chilkat.PublicKey
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = ecc:shared_secret_enc(priv_key, pub_key, encoding)
Computes an elliptic-curve Diffie-Hellman shared secret from the private key in priv_key and the peer's public key in pub_key. encoding specifies how the resulting bytes are encoded in the returned string, such as hex or base64.
Two parties obtain the same result when each combines its own private key with the other party's corresponding public key. Both keys must use the same named curve and must be suitable EC keys.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SignBd
-- hash_alg is a string
-- encoding is a string
-- priv_key is a chilkat.PrivateKey
-- prng is a chilkat.Prng
-- str is a string, or nil and err on failure
local str, err = ecc:sign_bd(bd_data, hash_alg, encoding, priv_key, prng)
Hashes the bytes currently contained in bd_data and creates an ECDSA signature with the EC private key in priv_key. hash_alg selects the hash algorithm and may be sha256, sha384, or sha512. encoding selects the textual encoding of the returned signature, such as hex or base64. prng supplies the random source used during ECDSA signature generation.
The signature's binary layout is controlled by AsnFormat. The contents of the BinData object are read but not modified.
priv_key.Prng. Reusing or predicting an ECDSA nonce can expose the private key.Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SignBdUsingCert
-- hash_alg is a string
-- encoding is a string
-- cert is a chilkat.Cert
-- str is a string, or nil and err on failure
local str, err = ecc:sign_bd_using_cert(bd_data, hash_alg, encoding, cert)
Hashes the bytes in bd_data and creates an ECDSA signature using the private key associated with the certificate in cert. hash_alg selects sha256, sha384, or sha512; encoding selects the textual encoding of the returned signature.
The certificate must contain an EC public key and must provide access to its corresponding private key. The private key may be embedded with the certificate or supplied through the platform or key-storage mechanism from which the certificate was loaded. Signature layout is controlled by AsnFormat.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SignHashENC
-- encoding is a string
-- privkey is a chilkat.PrivateKey
-- prng is a chilkat.Prng
-- str is a string, or nil and err on failure
local str, err = ecc:sign_hash_enc(encoded_hash, encoding, privkey, prng)
Creates an ECDSA signature over a digest that has already been computed by the application. encoded_hash contains the digest encoded using the binary encoding named by encoding, such as hex or base64. privkey supplies the EC private key, and prng supplies the random source used during signature generation.
The returned signature uses the same textual encoding named by encoding. Its binary layout is controlled by AsnFormat: ASN.1 DER when true, or raw r || s when false.
encoded_hash must be the exact digest required by the protocol. The verifier must use the same digest bytes and the matching public key.Prng.Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SignHashUsingCert
-- encoding is a string
-- cert is a chilkat.Cert
-- str is a string, or nil and err on failure
local str, err = ecc:sign_hash_using_cert(encoded_hash, encoding, cert)
Creates an ECDSA signature over a digest that has already been computed. encoded_hash contains the digest encoded using the binary encoding named by encoding. cert supplies a certificate whose public key is EC and whose associated private key is accessible for signing.
The returned signature uses the same textual encoding named by encoding. Its binary layout is controlled by AsnFormat.
encoded_hash must contain the final digest bytes expected by the receiving protocol. Use SignBdUsingCert when Chilkat should hash the source data before signing.Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
VerifyBd
-- hash_alg is a string
-- encoded_sig is a string
-- encoding is a string
-- pubkey is a chilkat.PublicKey
-- n is an integer
local n = ecc:verify_bd(bd_data, hash_alg, encoded_sig, encoding, pubkey)
Hashes the bytes in bd_data using the algorithm named by hash_alg and verifies the ECDSA signature in encoded_sig with the EC public key in pubkey. encoding identifies the textual encoding of the signature, such as hex or base64. Supported hash algorithms are sha256, sha384, and sha512.
Chilkat automatically recognizes ASN.1 DER and raw r || s signature layouts.
| Return | Meaning |
|---|---|
1 | Valid signature. |
0 | Invalid signature. |
-1 | Operational failure; examine LastErrorText. |
VerifyHashENC
-- encoded_sig is a string
-- encoding is a string
-- pubkey is a chilkat.PublicKey
-- n is an integer
local n = ecc:verify_hash_enc(encoded_hash, encoded_sig, encoding, pubkey)
Verifies an ECDSA signature over a digest that was computed by the application. encoded_hash is the encoded digest, encoded_sig is the encoded signature, encoding names the binary encoding used by both strings, and pubkey supplies the matching EC public key.
Chilkat automatically recognizes both supported ECDSA signature layouts: ASN.1 DER and fixed-width raw r || s. The AsnFormat property therefore does not need to match the signature being verified.
| Return | Meaning |
|---|---|
1 | The signature is valid for the supplied digest and public key. |
0 | The method ran successfully, but the signature is not valid. |
-1 | The verification operation could not be completed because of malformed input, an unsuitable key, an unsupported encoding, or another error. Examine LastErrorText. |
encoded_hash.