Dh Chilkat2 Perl Reference Documentation
Chilkat2::Dh
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.Dh
Use
Use
Use
Use
Use
The shared secret is typically used as input to a key derivation step
before being used for encryption or authentication.
For an extended overview, see
Dh Class Overview.
Perform Diffie-Hellman shared-secret key exchange.
Chilkat.Dh provides the core operations needed for
Diffie-Hellman shared-secret exchange. It allows two parties to agree on a
shared secret over an insecure channel by using common Diffie-Hellman
parameters, generating public exchange values, and computing the same shared
secret from the other party's public value.
Choose known parameters
UseKnownPrime when the application wants to use a
predefined Diffie-Hellman prime group.
Generate parameters
GenPG to generate Diffie-Hellman parameters when custom
prime and generator values are required.
Set custom P and G
SetPG when the prime and generator are already known or
have been received from another party or protocol.
Create the exchange value
CreateE to create the public Diffie-Hellman value that
is sent to the other party.
Compute the shared secret
FindK with the other party's public exchange value to
compute the shared secret.
Use the result carefully
CreateE, exchange public values with the
other party, then call FindK to compute the shared secret. The
primary methods are UseKnownPrime, GenPG,
SetPG, CreateE, and FindK.
Object Creation
use Chilkat2;
my $dh = Chilkat2::Dh->new;
# or set properties (and RaiseError) at creation:
# my $dh = Chilkat2::Dh->new(VerboseLogging => 1, RaiseError => 1);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/Dh.pm file holds only this class's documentation (perldoc Chilkat2::Dh).
Perl frees the object when its last reference goes (undef $dh, 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::Dh';): an object is a blessed hash reference whose hash is free for the subclass's own fields.
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::Dh'), 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
DebugLogFilePath
my $strVal = $dh->DebugLogFilePath;
$dh->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.
G
my $intVal = $dh->G;
Returns the current Diffie-Hellman generator, g. The generator is the
small public integer used with the prime modulus p in the modular
exponentiation operations that create the public exchange value and shared secret.
For this class, g is expected to be 2 or 5.
Its value is established by UseKnownPrime, GenPG, or
SetPG.
p and g values for the exchange.
LastErrorHtml
my $strVal = $dh->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.
topLastErrorText
my $strVal = $dh->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.
LastErrorXml
my $strVal = $dh->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.
topLastMethodSuccess
my $boolVal = $dh->LastMethodSuccess;
$dh->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.
P
my $strVal = $dh->P;
Returns the current Diffie-Hellman prime modulus, p, as a
hex-encoded SSH1-format bignum. The value is established by
UseKnownPrime, GenPG, or SetPG.
The modulus is a safe prime: p = 2q + 1, where
q is also prime. Safe-prime groups are commonly used for
finite-field Diffie-Hellman because their subgroup structure is well understood.
p.
p and g.
VerboseLogging
my $boolVal = $dh->VerboseLogging;
$dh->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.
Version
my $strVal = $dh->Version;
Methods
CreateE
# $str is a string or undef
my $str = $dh->CreateE($numBits);
Creates this party's public Diffie-Hellman exchange value, conventionally written
as E = gx mod p. Before calling this method, initialize
the common Diffie-Hellman parameters by calling UseKnownPrime,
GenPG, or SetPG.
($numBitsnumBits) controls the size of the randomly
generated private exponent retained internally by this Dh object.
Chilkat recommends setting it to twice the desired strength of the symmetric key
that will ultimately be derived. For example, use 256 when the
protocol will derive a 128-bit key.
The returned E value is a hex-encoded SSH1-format bignum. It is
public and is sent to the other party. The size of the eventual shared-secret
integer is determined primarily by p; numBits does
not make the returned shared secret a fixed-length symmetric key.
FindK on this same Dh object after receiving the
other party's exchange value. CreateE stores the corresponding
private value internally, and FindK needs it to compute the shared
secret. Use a separate Dh object for each party and generate a
fresh exchange value for each session.
For language bindings where a string return cannot directly communicate failure,
check LastMethodSuccess. If it is FALSE, inspect
LastErrorText.
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.
FindK
# $str is a string or undef
my $str = $dh->FindK($E);
Computes the Diffie-Hellman shared secret from the other party's public exchange
value. is the peer's $EE value, encoded as the
hexadecimal representation of an SSH1-format bignum.
This method must be called on the same Dh object that previously
called CreateE. Both parties must also be using the same
p and g parameters. Internally, the method computes
K = Ex mod p, where x is the private value
generated by CreateE.
The result is the shared-secret integer K, returned as a
hex-encoded SSH1-format bignum. When the exchange is performed correctly, both
parties compute the same value.
For language bindings where a string return cannot directly communicate failure,
check LastMethodSuccess. If it is FALSE, inspect
LastErrorText.
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.
GenPG
# $G is an integer
# $success is true or false
my $success = $dh->GenPG($numBits, $G);
Generates new finite-field Diffie-Hellman parameters and stores them in this
object. specifies the size of the safe prime $numBitsp in
bits, and specifies the generator $Gg, which should
be 2 or 5.
On success, the generated values are available through the P and
G properties. The prime generation and validation process is
computationally expensive, especially for large groups.
UseKnownPrime or parameters mandated
by the surrounding protocol instead of generating a new group. Standardized
groups improve interoperability and avoid the cost and risk of custom parameter
generation.
Returns TRUE if the parameters were generated successfully;
otherwise returns FALSE. Check LastErrorText for
failure details.
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.
SetPG
# $g is an integer
# $success is true or false
my $success = $dh->SetPG($p, $g);
Sets explicit finite-field Diffie-Hellman parameters for this object.
is the prime modulus $pp, supplied as a
hex-encoded SSH1-format bignum, and is the generator
$gg, normally 2 or 5.
The method validates that the supplied values satisfy the requirements expected
by this Diffie-Hellman implementation. It returns TRUE if the
parameters are accepted; otherwise it returns FALSE.
p is not ordinary integer hex. It must contain the hexadecimal
encoding of the SSH1 bignum representation, including its bit-length prefix.
The P property of another Chilkat Dh object is
already in the required form.
p and g.
These values are public, but untrusted or nonstandard parameters should not be
accepted unless the protocol explicitly permits them and their origin is
authenticated.
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.
UseKnownPrime
$dh->UseKnownPrime($index);
Selects one of eight built-in safe-prime MODP groups and sets the object's
P and G properties. is a Chilkat
selector from $index1 through 8; it is not
the IKE group number.
$index |
MODP group | Prime bits | Definition | G |
|---|---|---|---|---|
1 | Group 1 | 768 | RFC 2409, section 6.1 | 2 |
2 | Group 2 | 1024 | RFC 2409, section 6.2 | 2 |
3 | Group 5 | 1536 | RFC 3526, section 2 | 2 |
4 | Group 14 | 2048 | RFC 3526, section 3 | 2 |
5 | Group 15 | 3072 | RFC 3526, section 4 | 2 |
6 | Group 16 | 4096 | RFC 3526, section 5 | 2 |
7 | Group 17 | 6144 | RFC 3526, section 6 | 2 |
8 | Group 18 | 8192 | RFC 3526, section 7 | 2 |
1, 2, and 3 are legacy groups
and should not be chosen for new systems. Selector 4 is the
2048-bit group 14; selector 5 is the 3072-bit group 15 and is
commonly used when approximately 128-bit classical security is required.
Always use the group required by the protocol or the communicating peer.
Returns the object, so calls can be chained.