Next: , Previous: Public key modules, Up: Public Key cryptography


6.4 Cryptographic Functions

Note that we will in future allow to use keys without p,q and u specified and may also support other parameters for performance reasons.

Some functions operating on S-expressions support `flags', that influence the operation. These flags have to be listed in a sub-S-expression named `flags'; the following flags are known:

pkcs1
Use PKCS#1 block type 2 padding for encryption, block type 1 padding for signing.
oaep
Use RSA-OAEP padding for encryption.
pss
Use RSA-PSS padding for signing.
no-blinding
Do not use a technique called `blinding', which is used by default in order to prevent leaking of secret information. Blinding is only implemented by RSA, but it might be implemented by other algorithms in the future as well, when necessary.

Now that we know the key basics, we can carry on and explain how to encrypt and decrypt data. In almost all cases the data is a random session key which is in turn used for the actual encryption of the real data. There are 2 functions to do this:

— Function: gcry_error_t gcry_pk_encrypt (gcry_sexp_t *r_ciph, gcry_sexp_t data, gcry_sexp_t pkey)

Obviously a public key must be provided for encryption. It is expected as an appropriate S-expression (see above) in pkey. The data to be encrypted can either be in the simple old format, which is a very simple S-expression consisting only of one MPI, or it may be a more complex S-expression which also allows to specify flags for operation, like e.g. padding rules.

If you don't want to let Libgcrypt handle the padding, you must pass an appropriate MPI using this expression for data:

          (data
            (flags raw)
            (value mpi))

This has the same semantics as the old style MPI only way. MPI is the actual data, already padded appropriate for your protocol. Most RSA based systems however use PKCS#1 padding and so you can use this S-expression for data:

          (data
            (flags pkcs1)
            (value block))

Here, the "flags" list has the "pkcs1" flag which let the function know that it should provide PKCS#1 block type 2 padding. The actual data to be encrypted is passed as a string of octets in block. The function checks that this data actually can be used with the given key, does the padding and encrypts it.

If the function could successfully perform the encryption, the return value will be 0 and a new S-expression with the encrypted result is allocated and assigned to the variable at the address of r_ciph. The caller is responsible to release this value using gcry_sexp_release. In case of an error, an error code is returned and r_ciph will be set to NULL.

The returned S-expression has this format when used with RSA:

          (enc-val
            (rsa
              (a a-mpi)))

Where a-mpi is an MPI with the result of the RSA operation. When using the Elgamal algorithm, the return value will have this format:

          (enc-val
            (elg
              (a a-mpi)
              (b b-mpi)))

Where a-mpi and b-mpi are MPIs with the result of the Elgamal encryption operation.

— Function: gcry_error_t gcry_pk_decrypt (gcry_sexp_t *r_plain, gcry_sexp_t data, gcry_sexp_t skey)

Obviously a private key must be provided for decryption. It is expected as an appropriate S-expression (see above) in skey. The data to be decrypted must match the format of the result as returned by gcry_pk_encrypt, but should be enlarged with a flags element:

          (enc-val
            (flags)
            (elg
              (a a-mpi)
              (b b-mpi)))

This function does not remove padding from the data by default. To let Libgcrypt remove padding, give a hint in `flags' telling which padding method was used when encrypting:

          (flags padding-method)

Currently padding-method is either pkcs1 for PKCS#1 block type 2 padding, or oaep for RSA-OAEP padding.

The function returns 0 on success or an error code. The variable at the address of r_plain will be set to NULL on error or receive the decrypted value on success. The format of r_plain is a simple S-expression part (i.e. not a valid one) with just one MPI if there was no flags element in data; if at least an empty flags is passed in data, the format is:

          (value plaintext)

Another operation commonly performed using public key cryptography is signing data. In some sense this is even more important than encryption because digital signatures are an important instrument for key management. Libgcrypt supports digital signatures using 2 functions, similar to the encryption functions:

— Function: gcry_error_t gcry_pk_sign (gcry_sexp_t *r_sig, gcry_sexp_t data, gcry_sexp_t skey)

This function creates a digital signature for data using the private key skey and place it into the variable at the address of r_sig. data may either be the simple old style S-expression with just one MPI or a modern and more versatile S-expression which allows to let Libgcrypt handle padding:

           (data
            (flags pkcs1)
            (hash hash-algo block))

This example requests to sign the data in block after applying PKCS#1 block type 1 style padding. hash-algo is a string with the hash algorithm to be encoded into the signature, this may be any hash algorithm name as supported by Libgcrypt. Most likely, this will be "sha256" or "sha1". It is obvious that the length of block must match the size of that message digests; the function checks that this and other constraints are valid.

If PKCS#1 padding is not required (because the caller does already provide a padded value), either the old format or better the following format should be used:

          (data
            (flags raw)
            (value mpi))

Here, the data to be signed is directly given as an MPI.

The signature is returned as a newly allocated S-expression in r_sig using this format for RSA:

          (sig-val
            (rsa
              (s s-mpi)))

Where s-mpi is the result of the RSA sign operation. For DSA the S-expression returned is:

          (sig-val
            (dsa
              (r r-mpi)
              (s s-mpi)))

Where r-mpi and s-mpi are the result of the DSA sign operation. For Elgamal signing (which is slow, yields large numbers and probably is not as secure as the other algorithms), the same format is used with "elg" replacing "dsa".

The operation most commonly used is definitely the verification of a signature. Libgcrypt provides this function:

— Function: gcry_error_t gcry_pk_verify (gcry_sexp_t sig, gcry_sexp_t data, gcry_sexp_t pkey)

This is used to check whether the signature sig matches the data. The public key pkey must be provided to perform this verification. This function is similar in its parameters to gcry_pk_sign with the exceptions that the public key is used instead of the private key and that no signature is created but a signature, in a format as created by gcry_pk_sign, is passed to the function in sig.

The result is 0 for success (i.e. the data matches the signature), or an error code where the most relevant code is GCRYERR_BAD_SIGNATURE to indicate that the signature does not match the provided data.