Ecc Chilkat2 Perl Reference Documentation

Chilkat2::Ecc

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.Ecc

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

Create elliptic-curve private keys for supported curves and use the resulting key with Chilkat signing, verification, or key-agreement APIs.

ECDSA signing

Sign hashes or input data using an EC private key, producing ECDSA signatures in the configured output format.

ECDSA verification

Verify ECDSA signatures using the matching EC public key and the original hash or data.

Certificate-based signing

Sign with an ECDSA certificate when the certificate has access to its associated private key.

Shared-secret calculation

Compute an encoded shared secret from compatible EC private and public keys for key-agreement workflows.

Signature format control

Use AsnFormat to control whether ECDSA signatures are encoded as ASN.1/DER or as raw r || s values.

Common pattern: Generate or load an EC key, choose the required signature format with 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

use Chilkat2;

my $ecc = Chilkat2::Ecc->new;
# or set properties (and RaiseError) at creation:
# my $ecc = Chilkat2::Ecc->new(VerboseLogging => 1, RaiseError => 1);
my $ecc = Chilkat2::Ecc->new(Property => $value, ...);

Creates the object. The optional pairs set writable properties (and RaiseError) in order, as if set one by one; an unknown or read-only name croaks. use Chilkat2; loads every class; the Chilkat2/Ecc.pm file holds only this class's documentation (perldoc Chilkat2::Ecc).

Perl frees the object when its last reference goes (undef $ecc, or the variable going out of scope); there is nothing to close. Objects returned by methods are independent and freed the same way. The class may be subclassed (use parent -norequire, 'Chilkat2::Ecc';): an object is a blessed hash reference whose hash is free for the subclass's own fields.

my $boolVal = $ecc->RaiseError;
$ecc->RaiseError($boolVal);

When false (the default), a method that fails returns false (or undef), and the reason is in LastErrorText. When true, it dies with a Chilkat2::Error instead, as DBI does with its RaiseError. Objects returned by a method inherit the value of the object that returned them.

Errors

A Chilkat2::Error stringifies to the failed call's full LastErrorText, and has the methods text, class ('Chilkat2::Ecc'), method (the method name) and object. Methods that look something up and find nothing return undef without dying (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain boolean, and property reads and writes never fail for Chilkat reasons.

Misuse always croaks, with your file and line, whatever RaiseError is: a wrong number of arguments, a reference or undef where a string or number is expected, an object of the wrong class, an out-of-range integer, a string containing a NUL character, a wide character in a byte string, or a write to a read-only property.

Strings are Perl character strings in both directions (length, regexes and :encoding layers see characters); binary data (BinData) is a byte string. Unlocking: call Chilkat2::Global->new->UnlockBundle('...') once at startup.

Properties

AsnFormat
# $boolVal is a boolean
my $boolVal = $ecc->AsnFormat;
$ecc->AsnFormat($boolVal);
Introduced in version 9.5.0.97

Controls the binary layout of ECDSA signatures created by this object. The default is 1.

ValueSignature layout
1ASN.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.
0Raw format: fixed-width, unsigned big-endian r followed immediately by fixed-width s, commonly written r || s.
Signing versus verification: This property controls generated signatures. Chilkat verification methods automatically recognize both ASN.1 DER and raw r || s signatures.
Protocol compatibility: Neither format is universally interchangeable. Use the layout required by the receiving protocol or file format.

top
DebugLogFilePath
# $strVal is a string
my $strVal = $ecc->DebugLogFilePath;
$ecc->DebugLogFilePath($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.

More Information and Examples
top
LastErrorHtml
# $strVal is a string (read-only)
my $strVal = $ecc->LastErrorHtml;

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.

top
LastErrorText
# $strVal is a string (read-only)
my $strVal = $ecc->LastErrorText;

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.

top
LastErrorXml
# $strVal is a string (read-only)
my $strVal = $ecc->LastErrorXml;

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.

top
LastMethodSuccess
# $boolVal is a boolean
my $boolVal = $ecc->LastMethodSuccess;
$ecc->LastMethodSuccess($boolVal);

Indicates the success or failure of the most recent method call: 1 means success, 0 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.

top
VerboseLogging
# $boolVal is a boolean
my $boolVal = $ecc->VerboseLogging;
$ecc->VerboseLogging($boolVal);

If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
# $strVal is a string (read-only)
my $strVal = $ecc->Version;

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

GenKey
# $curveName is a string
# $prng is a Chilkat2::Prng
# $privKey is a Chilkat2::PrivateKey
# $success is true or false
my $success = $ecc->GenKey($curveName, $prng, $privKey);
Introduced in version 11.0.0

Generates a new elliptic-curve private key and stores it in $privKey. $curveName selects the named curve, and $prng supplies the cryptographically secure random data used to choose the private scalar. Returns 1 on success and 0 on failure.

Supported curve names are:

Curve nameNotes
secp256r1Also known as P-256 and prime256v1.
secp384r1Also known as P-384.
secp521r1Also known as P-521.
secp256k1Widely associated with Bitcoin and related systems.
secp192r1, secp224r1Older, smaller SEC curves retained for compatibility.
brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1, brainpoolP320r1, brainpoolP384r1, brainpoolP512r1Brainpool prime-field curves.
Choose the curve required by the protocol: The curve name determines the key size, signature size, and interoperability. P-256, P-384, and P-521 are broadly standardized; 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.
Random generation matters: The security of the private key depends on the randomness supplied by $prng. Use a properly initialized cryptographic Prng.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
GenKey2
# $curveName is a string
# $encodedK is a string
# $encoding is a string
# $privKey is a Chilkat2::PrivateKey
# $success is true or false
my $success = $ecc->GenKey2($curveName, $encodedK, $encoding, $privKey);
Introduced in version 11.0.0

Constructs an elliptic-curve private key in $privKey from a caller-supplied private scalar. $curveName selects the named curve, $encodedK contains the scalar, and $encoding names its representation, such as hex, base64, or decimal. Returns 1 on success and 0 on failure.

Supported curve names are:

Curve nameNotes
secp256r1Also known as P-256 and prime256v1.
secp384r1Also known as P-384.
secp521r1Also known as P-521.
secp256k1Widely associated with Bitcoin and related systems.
secp192r1, secp224r1Older, smaller SEC curves retained for compatibility.
brainpoolP160r1, brainpoolP192r1, brainpoolP224r1, brainpoolP256r1, brainpoolP320r1, brainpoolP384r1, brainpoolP512r1Brainpool prime-field curves.
encodedK is the private key value: Despite the historical argument name, $encodedK 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.
Typical use: Use this method to reproduce a known key for a test vector or to import a scalar supplied by another system. Use GenKey to create a new random private key.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
SharedSecretENC
# $privKey is a Chilkat2::PrivateKey
# $pubKey is a Chilkat2::PublicKey
# $encoding is a string
# $str is a string or undef
my $str = $ecc->SharedSecretENC($privKey, $pubKey, $encoding);
Introduced in version 9.5.0.52

Computes an elliptic-curve Diffie-Hellman shared secret from the private key in $privKey and the peer's public key in $pubKey. $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.

Do not use the raw result directly as a long-term encryption key: A shared secret is normally passed through the key-derivation function required by the surrounding protocol, together with any required context, salt, or transcript information. Key agreement by itself also does not authenticate the peer; an unauthenticated exchange is vulnerable to a man-in-the-middle attack.
ECDH, not ECDSA: ECDSA is a signature algorithm. This method performs ECDH-style key agreement.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
SignBd
# $bdData is a Chilkat2::BinData
# $hashAlg is a string
# $encoding is a string
# $privKey is a Chilkat2::PrivateKey
# $prng is a Chilkat2::Prng
# $str is a string or undef
my $str = $ecc->SignBd($bdData, $hashAlg, $encoding, $privKey, $prng);
Introduced in version 9.5.0.85

Hashes the bytes currently contained in $bdData and creates an ECDSA signature with the EC private key in $privKey. $hashAlg 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.

Sign the exact bytes: Verification succeeds only when the verifier hashes the same byte sequence with the same hash algorithm and uses the public key corresponding to $privKey.
Protect the signing nonce: Use a cryptographically secure Prng. Reusing or predicting an ECDSA nonce can expose the private key.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
SignBdUsingCert
# $bdData is a Chilkat2::BinData
# $hashAlg is a string
# $encoding is a string
# $cert is a Chilkat2::Cert
# $str is a string or undef
my $str = $ecc->SignBdUsingCert($bdData, $hashAlg, $encoding, $cert);
Introduced in version 9.5.0.91

Hashes the bytes in $bdData and creates an ECDSA signature using the private key associated with the certificate in $cert. $hashAlg 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.

Certificate versus key: The certificate identifies the public key and may describe the key owner, but signing requires actual access to the associated private key. A public-only certificate cannot sign.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
SignHashENC
# $encodedHash is a string
# $encoding is a string
# $privkey is a Chilkat2::PrivateKey
# $prng is a Chilkat2::Prng
# $str is a string or undef
my $str = $ecc->SignHashENC($encodedHash, $encoding, $privkey, $prng);
Introduced in version 9.5.0.52

Creates an ECDSA signature over a digest that has already been computed by the application. $encodedHash 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 1, or raw r || s when 0.

No hashing is performed: $encodedHash must be the exact digest required by the protocol. The verifier must use the same digest bytes and the matching public key.
ECDSA nonce safety: Every signature requires a secret per-signature value. It must be generated securely and must never be reused with the same private key, because nonce reuse or predictability can reveal the private key. Supply a properly initialized cryptographic Prng.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
SignHashUsingCert
# $encodedHash is a string
# $encoding is a string
# $cert is a Chilkat2::Cert
# $str is a string or undef
my $str = $ecc->SignHashUsingCert($encodedHash, $encoding, $cert);
Introduced in version 10.1.0

Creates an ECDSA signature over a digest that has already been computed. $encodedHash 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.

No hashing is performed: $encodedHash must contain the final digest bytes expected by the receiving protocol. Use SignBdUsingCert when Chilkat should hash the source data before signing.
Private-key requirement: A certificate containing only a public key is not sufficient. The associated EC private key must be present and usable.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
VerifyBd
# $bdData is a Chilkat2::BinData
# $hashAlg is a string
# $encodedSig is a string
# $encoding is a string
# $pubkey is a Chilkat2::PublicKey
# $n is an integer
my $n = $ecc->VerifyBd($bdData, $hashAlg, $encodedSig, $encoding, $pubkey);
Introduced in version 9.5.0.85

Hashes the bytes in $bdData using the algorithm named by $hashAlg and verifies the ECDSA signature in $encodedSig 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.

ReturnMeaning
1Valid signature.
0Invalid signature.
-1Operational failure; examine LastErrorText.
Exact-data requirement: A valid signature becomes invalid if any signed byte changes or if a different hash algorithm is selected.
top
VerifyHashENC
# $encodedHash is a string
# $encodedSig is a string
# $encoding is a string
# $pubkey is a Chilkat2::PublicKey
# $n is an integer
my $n = $ecc->VerifyHashENC($encodedHash, $encodedSig, $encoding, $pubkey);
Introduced in version 9.5.0.52

Verifies an ECDSA signature over a digest that was computed by the application. $encodedHash is the encoded digest, $encodedSig 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.

ReturnMeaning
1The signature is valid for the supplied digest and public key.
0The method ran successfully, but the signature is not valid.
-1The verification operation could not be completed because of malformed input, an unsuitable key, an unsupported encoding, or another error. Examine LastErrorText.
Digest-level verification: This method does not hash the original message. It verifies only the digest bytes supplied in $encodedHash.
top