#include <pk.h>
Inheritance diagram for Key:


Key() // instantiate a Key object loadoid() // specify key type and size loadprv() // load ASN.1 DER-encoded private key components loadpub() // load ASN.1 DER-encoded public key components
Important Note: Loading a private key clears the public key in the Key object if one is present, so when loading a key pair, you should always load the private components first, then the public.
Once generated or loaded, key components may be accessed using the following member functions: asn1parameters() // get ASN.1 DER-encoded parameters (if any) asn1private() // get ASN.1 DER-encoded private key asn1public() // get ASN.1 DER-encoded public key
PHASE I: Alice instantiates a Key object, privately generates her (static or ephemeral) key pair in the specified group, and extracts her public key:
PRNG rand; Key dhAlice; dhAlice.DLkeygen(rand.gens(20),strOID); str strAlicePK = dhAlice.asn1public();
while Bob privately does the same:
PRNG rand; Key dhBob; dhBob.DLkeygen(rand.gens(20),strOID); str strBobPK = dhBob.asn1public();
EXCHANGE: Alice and Bob now exchange their public keys over the insecure channel. Allice sends strAlicePK to Bob, and Bob sends strBobPK to Alice. (Typically, Alice and Bob obtain certificates for their public keys from a trusted third party and make those certificates available to each other by publishing them in a public repository.)
PHASE II: Alice now computes the common Diffie-Hellman secret by loading Bob's public key into a Key object and raising it to her private exponent (using the modular exponeniation function provided by Point::operator*()).
Key Bob; Bob.loadoid(strOID); Bob.loadpub(strBobPK); Point DHsecret = Bob.pub * kAlice.getPrivate();
while Bob does a similar thing on his side:
Key Alice; Alice.loadoid(strOID); Alice.loadpub(strAlicePK); Point DHsecret = Alice.pub * kBob.getPrivate();
Now Alice and Bob both possess the (full) Diffie-Hellman secret and can apply a previously agreed upon key derivation function to derive from it any type of (symmetric) key they desire.
See also: Diffie-Hellman Key Agreement in the Cookbook.
Public Types | |
| digitalSignature = 0x80 | |
| = 0x80 | |
| nonRepudiation = 0x40 | |
| = 0x40 | |
| keyEncipherment = 0x20 | |
| = 0x20 | |
| dataEncipherment = 0x10 | |
| = 0x10 | |
| keyAgreement = 0x08 | |
| = 0x08 | |
| keyCertSign = 0x04 | |
| = 0x04 | |
| crlSign = 0x02 | |
| = 0x02 | |
| encipherOnly = 0x01 | |
| = 0x01 | |
| usageAll = 0xFF | |
| = 0xFF | |
| enum | { digitalSignature = 0x80, nonRepudiation = 0x40, keyEncipherment = 0x20, dataEncipherment = 0x10, keyAgreement = 0x08, keyCertSign = 0x04, crlSign = 0x02, encipherOnly = 0x01, usageAll = 0xFF } |
| keyUsage bit masks More... | |
Public Member Functions | |
Constructor and Destructor | |
| Key () | |
| Constructor. | |
| virtual | ~Key () |
| Destructor. | |
Object Reuse and Initialization | |
| void | clear () |
| Clear this object. | |
| void | settype (const str &kyp) |
| Set this object's key type and size. | |
| int | setorder (const num &q) |
| Set the order of this key's underlying Abelian group. | |
| void | setdefaulthash () |
| Set the hashtype of this object to SHA-1. | |
| int | DLLoadPublic (const num &y) |
| Load a (raw) DSA or ECDSA public key into this object. | |
| int | setPrivate (const str &p) |
| Load a (raw) DSA or ECDSA private key into this object. | |
| int | RSALoadPublic (const num &expo1, const num &pq1) |
| Load a raw RSA public key (exponent and modulus) into this object. | |
| void | loadprivate (const num &x) |
| Load a raw RSA private key (a single factor of the modulus) into this object. | |
| int | genpub () |
| Generate a public key based on the private key components in this object. | |
| int | loadseed (const str &seed, int np, int &counter) |
| Load DSA parameters based on a specified SEED. | |
| int | loadoid (const str &oid) |
| Load ASN.1 encoded parameters. | |
| int | loadprv (const str &prv) |
| Load an ASN.1 encoded private key (in the clear). | |
| int | loadpub (const str &pubk) |
| Load an ASN.1 encoded public key. | |
Inspectors | |
| str | id () const |
| Get an ASCII description of this object's key type and size. | |
| int | DLGetRawPublic (num &x, num &y) const |
| Get the raw DL public key from this object. | |
| int | RSAGetRawPublic (num &expo1, num &pq1) |
| Get the raw RSA public key from this object. | |
| int | GetRawPrivate (num &x) |
| Get the raw DL private key from this object. | |
| int | alg () const |
| Get this key's "type.". | |
| int | bits () const |
| Get the "size" of the key in this object. | |
| int | rawsiglength () const |
| Get the length in bytes of a raw signature associated with this key. | |
| num | getPrivate () const |
| Get the (raw) private key in this object. | |
| str | asn1private () const |
| Get the ASN.1 encoded private key in this object. | |
| str | asn1public () const |
| Get the ASN.1 encoded public key in this object. | |
| str | asn1parameters (int full=1, int withhash=1) const |
| Get the ASN.1 encoded parameters in this Key object. | |
| num | order () const |
| Get the order of this key's underlying Abelian group. | |
| num | A () const |
| Get the first coefficient in the equation for this object's underlying elliptic curve. | |
| num | B () const |
| Get the second coefficient in the equation for this object's underlying elliptic curve. | |
Sanity Checks | |
| int | check () const |
| Check the consistency of the keypair in this object. | |
| int | checkSeed (const str &seed, int start=0, int v=1, int h=2) |
| Check that this object's key parameters were correctly generated as per NIST FIPS 186-2. | |
Key Generation | |
| int | RSAkeygen (const str &seed, int nbits=1024, const num &exponent=65537, int factors=2) |
| Generate an RSA keypair. | |
| int | DLkeygen (const str &seed, const str &strOID) |
| Generate a DL keypair. | |
| int | power (const num &a, num &x) const |
| Perform modular exponentiation with this object's private key (for DH or ElGamal). | |
Encryption and Decryption | |
| int | Encrypt (const str &a, str &x) const |
| Encrypt (or wrap) a specified buffer using this object's public key. | |
| int | Encrypt (const str &a, const str &seed, str &x) const |
| Encrypt (or wrap) a specified buffer using this object's public key. | |
| int | Decrypt (const str &a, str &x) const |
| Decrypt (or unwrap) a specified buffer using this object's private key. | |
Signatures and Validation | |
| void | SetPadding (int nPadding) |
| Set the RSA padding scheme to use when signing. | |
| int | Sign (const num &hash, const num &random, Signature &sig) const |
| Sign the specified message digest using this object's private key. | |
| int | asn1sign (const str &msg, const num &krand, str &sig) const |
| Produce an ASN.1 DER-encoded signature over a specified message. | |
| int | SignCheck (const num &hash, const Signature &sig) const |
| Check the validity of a specified signature against this object's public key. | |
| int | SignCheck (const num &hash, const str &sig) const |
| Check the validity of a specified signature against this object's public key. | |
Useful Predicates | |
| bool | hasPublic () const |
| Predicate used to determine whether this object contains a public key. | |
| bool | isRSA () const |
| Predicate used to determine whether this object contains an RSA key. | |
| bool | isDH () const |
| Predicate used to determine whether this object contains an DH/DSA key. | |
| bool | isEC () const |
| Predicate used to determine whether this object contains an elliptic curve key. | |
| bool | isChar2 () const |
| Predicate used to test whether this object is an elliptic curve key over a field of characteristic 2. | |
| bool | permit (int flag) const |
| Predicate used to test keyUsage bit settings. | |
Conversion Function | |
| template<class T> | |
| T | to () const |
| Convert this key object into an object of type T. | |
Data Fields | |
| int | usage |
| keyUsage extension | |
| str | keytype |
| key type (e.g., gRSA, gDSA, gECP, or gEC2) | |
| int | hashtype |
| hash type (e.g., hSHA1) | |
| Point | gen |
| group generator (base point) | |
| Point | pub |
| public key | |
| int | cofactor |
| cofactor (EC only) | |
| RSA | rsai |
| RSA key components. | |
| anonymous enum |
| Key | ( | ) | [inline, explicit] |
Constructor.
| virtual ~Key | ( | ) | [inline, virtual] |
Destructor.
| num A | ( | ) | const [inline] |
Get the first coefficient in the equation for this object's underlying elliptic curve.
| int alg | ( | ) | const |
Get this key's "type.".
| str asn1parameters | ( | int | full = 1, |
|
| int | withhash = 1 | |||
| ) | const |
Get the ASN.1 encoded parameters in this Key object.
| full | parameter indicator: 1 to include parameters, 0 to produce OID only | |
| withhash | digest indicator: 1 to include digest OID, 0 to omit digest OID |
| str asn1private | ( | ) | const |
Get the ASN.1 encoded private key in this object.
| str asn1public | ( | ) | const |
Get the ASN.1 encoded public key in this object.
Produce an ASN.1 DER-encoded signature over a specified message.
| msg | the message data to be hashed and signed | |
| krand | a random number (can be generated with num(gens(20)) | |
| sig | the output buffer that is to receive the ASN.1 DER-encoded signature |
| num B | ( | ) | const [inline] |
Get the second coefficient in the equation for this object's underlying elliptic curve.
| int bits | ( | ) | const [inline] |
Get the "size" of the key in this object.
| int check | ( | ) | const |
Check the consistency of the keypair in this object.
| int checkSeed | ( | const str & | seed, | |
| int | start = 0, |
|||
| int | v = 1, |
|||
| int | h = 2 | |||
| ) |
Check that this object's key parameters were correctly generated as per NIST FIPS 186-2.
| seed | the initial SEED value | |
| start | a starting value for the counter | |
| v | an algorithm indicator: for DSA, use v=0 for SHA, v=1 for SHA-1; for ECP, use v=0; for EC2, v should be the degree of extension field over Z2. | |
| h | the value used to obtain the generator (i.e., g = h^[(p-1)/q]; for DSA only) |
Decrypt (or unwrap) a specified buffer using this object's private key.
| a | the input ciphertext buffer | |
| x | an output buffer for the plaintext |
Get the raw DL public key from this object.
| x | an output buffer for the DSA/DH public key (or x coordinate of the EC public key) | |
| y | an output buffer for the y coordinate of the EC public key (receives 0 in the DSA/DH case) |
Generate a DL keypair.
| seed | a random number (at least 20 bytes, preferably 40; can be generated with PRNG::gens(40)) | |
| strOID | an ASN.1 DER-encoded OID specifying the DSA/ECDSA parameters to be used. |
| int DLLoadPublic | ( | const num & | y | ) |
Load a (raw) DSA or ECDSA public key into this object.
| y | the raw DL public key (private exponent) to be loaded |
Encrypt (or wrap) a specified buffer using this object's public key.
| a | the input plaintext buffer | |
| seed | an input for pseudorandom number generation | |
| x | an output buffer for the ciphertext |
Encrypt (or wrap) a specified buffer using this object's public key.
| a | the input plaintext buffer | |
| x | an output buffer for the ciphertext |
| int genpub | ( | ) |
Generate a public key based on the private key components in this object.
| num getPrivate | ( | ) | const [inline] |
Get the (raw) private key in this object.
| int GetRawPrivate | ( | num & | x | ) |
Get the raw DL private key from this object.
| x | an buffer for the private key |
| bool hasPublic | ( | ) | const [inline] |
Predicate used to determine whether this object contains a public key.
| str id | ( | ) | const |
Get an ASCII description of this object's key type and size.
| bool isChar2 | ( | ) | const [inline] |
Predicate used to test whether this object is an elliptic curve key over a field of characteristic 2.
| bool isDH | ( | ) | const [inline] |
Predicate used to determine whether this object contains an DH/DSA key.
| bool isEC | ( | ) | const [inline] |
Predicate used to determine whether this object contains an elliptic curve key.
| bool isRSA | ( | ) | const [inline] |
Predicate used to determine whether this object contains an RSA key.
| int loadoid | ( | const str & | oid | ) |
Load ASN.1 encoded parameters.
| oid | a str containing an ASN.1 encoded OID providing RSA/DSA/ECDSA parameters Modifies: hashtype, rsai, cofactor, priv, gen, pub |
| void loadprivate | ( | const num & | x | ) |
Load a raw RSA private key (a single factor of the modulus) into this object.
| x | the raw private key to be loaded |
| int loadprv | ( | const str & | prv | ) |
Load an ASN.1 encoded private key (in the clear).
| prv | the ASN.1 encoded private key to be loaded |
| int loadpub | ( | const str & | pubk | ) |
Load an ASN.1 encoded public key.
| pubk | the ASN.1 encoded public key to be loaded |
| int loadseed | ( | const str & | seed, | |
| int | np, | |||
| int & | counter | |||
| ) |
Load DSA parameters based on a specified SEED.
| seed | the initial SEED value | |
| np | the length of the desired prime modulus in bits | |
| counter | an output buffer to receive the final counter value |
| num order | ( | ) | const [inline] |
Get the order of this key's underlying Abelian group.
| bool permit | ( | int | flag | ) | const [inline] |
Predicate used to test keyUsage bit settings.
| flag | the bit setting (enum value) to be tested against this object's keyUsage |
Perform modular exponentiation with this object's private key (for DH or ElGamal).
| a | the num to be raised to the private exponent | |
| x | an output buffer for the result (a raised to the private exponent in the underlying group) |
| int rawsiglength | ( | ) | const [inline] |
Get the length in bytes of a raw signature associated with this key.
Get the raw RSA public key from this object.
| expo1 | an output buffer for the public exponent | |
| pq1 | an output buffer for the public modulus |
Generate an RSA keypair.
| seed | a random number to be used as a seed for the prime searches. | |
| nbits | the desired length of the modulus in bits | |
| exponent | the public exponent | |
| factors | the number of prime factors to use: 2, 3, or 4 |
Load a raw RSA public key (exponent and modulus) into this object.
| expo1 | the exponent of the RSA public key to be loaded | |
| pq1 | the modulus of the RSA public key to be loaded |
| void setdefaulthash | ( | ) |
Set the hashtype of this object to SHA-1.
| int setorder | ( | const num & | q | ) |
Set the order of this key's underlying Abelian group.
| q | the order of the group |
| void SetPadding | ( | int | nPadding | ) | [inline] |
Set the RSA padding scheme to use when signing.
| nPadding | padding indicator: pkcs1 or x931 |
| int setPrivate | ( | const str & | p | ) | [inline] |
Load a (raw) DSA or ECDSA private key into this object.
| p | the raw private key to be loaded |
| void settype | ( | const str & | kyp | ) |
Set this object's key type and size.
| kyp | an ASCII description of the key type and size (e.g., "RSA-1024", "EC2-163", etc.) |
Sign the specified message digest using this object's private key.
| hash | the message digest to be signed | |
| random | a random number required for DL signatures (can be generated with num(gens(20)) | |
| sig | the Signature object that is to receive the result |
Check the validity of a specified signature against this object's public key.
| hash | the message digest that was purportedly signed | |
| sig | a str containing the signature PDU (raw binary or ASN.1 DER-encoded) to be verified |
Check the validity of a specified signature against this object's public key.
| hash | the message digest that was purportedly signed | |
| sig | a Signature object containing the signature to be verified |
| T to | ( | ) | const |
Convert this key object into an object of type T.
| ISC Cryptographic Development Kit - User's Guide | |
| Questions? E-mail ISC technical support | |
| Copyright© 2002-2006 Information Security Corp. All rights reserved. |