write: Improve help text of --writer.
[paraslash.git] / gcrypt.c
index 49e27f2d0c2002c03b532d145ed395cdaf6d5f47..45a1c67d20da0b6d229180de92be10644a63d082 100644 (file)
--- a/gcrypt.c
+++ b/gcrypt.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -7,7 +7,6 @@
 /** \file gcrypt.c Libgrcypt-based encryption/decryption routines. */
 
 #include <regex.h>
-#include <stdbool.h>
 #include <gcrypt.h>
 
 #include "para.h"
 
 //#define GCRYPT_DEBUG 1
 
+static bool libgcrypt_has_oaep;
+static const char *rsa_decrypt_sexp;
+
 #ifdef GCRYPT_DEBUG
 static void dump_buffer(const char *msg, unsigned char *buf, int len)
 {
        int i;
 
-       fprintf(stderr, "%s (%u bytes): ", msg, len);
+       fprintf(stderr, "%s (%d bytes): ", msg, len);
        for (i = 0; i < len; i++)
                fprintf(stderr, "%02x ", buf[i]);
        fprintf(stderr, "\n");
@@ -56,18 +58,36 @@ void get_random_bytes_or_die(unsigned char *buf, int num)
 }
 
 /*
- * This is called at the beginning of every program that uses libgcrypt.  We
- * don't have to initialize any random seed here, but the call to gcry_control
- * is necessary to avoid warnings of the form "missing initialization - please
- * fix the application".
+ * This is called at the beginning of every program that uses libgcrypt. We
+ * don't have to initialize any random seed here, but we must initialize the
+ * gcrypt library. This task is performed by gcry_check_version() which can
+ * also check that the gcrypt library version is at least the minimal required
+ * version. This function also tells us whether we have to use our own OAEP
+ * padding code.
  */
 void init_random_seed_or_die(void)
 {
-       gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
+       const char *ver, *req_ver;
+
+       ver = gcry_check_version(NULL);
+       req_ver = "1.4.0";
+       if (!gcry_check_version(req_ver)) {
+               PARA_EMERG_LOG("fatal: need at least libgcrypt-%s, have: %s\n",
+                       req_ver, ver);
+               exit(EXIT_FAILURE);
+       }
+       req_ver = "1.5.0";
+       if (gcry_check_version(req_ver)) {
+               libgcrypt_has_oaep = true;
+               rsa_decrypt_sexp = "(enc-val(flags oaep)(rsa(a %m)))";
+       } else {
+               libgcrypt_has_oaep = false;
+               rsa_decrypt_sexp = "(enc-val(rsa(a %m)))";
+       }
 }
 
 /** S-expression for the public part of an RSA key. */
-#define RSA_PUBKEY_SEXP  "(public-key (rsa (n %m) (e %m)))"
+#define RSA_PUBKEY_SEXP "(public-key (rsa (n %m) (e %m)))"
 /** S-expression for a private RSA key. */
 #define RSA_PRIVKEY_SEXP "(private-key (rsa (n %m) (e %m) (d %m) (p %m) (q %m) (u %m)))"
 
@@ -77,7 +97,7 @@ static void mgf1(unsigned char *seed, size_t seed_len, unsigned result_len,
 {
        gcry_error_t gret;
        gcry_md_hd_t handle;
-       size_t n;;
+       size_t n;
        unsigned char *md;
        unsigned char octet_string[4], *rp = result, *end = rp + result_len;
 
@@ -143,7 +163,7 @@ static void pad_oaep(unsigned char *in, size_t in_len, unsigned char *out,
 /* rfc 3447, section 7.1.2 */
 static int unpad_oaep(unsigned char *in, size_t in_len, unsigned char *out,
                size_t *out_len)
-{      int ret;
+{
        unsigned char *masked_seed = in + 1;
        unsigned char *db = in + 1 + HASH_SIZE;
        unsigned char seed[HASH_SIZE], seed_mask[HASH_SIZE];
@@ -168,7 +188,7 @@ static int unpad_oaep(unsigned char *in, size_t in_len, unsigned char *out,
        p++;
        *out_len = in + in_len - p;
        memcpy(out, p, *out_len);
-       return ret;
+       return 1;
 }
 
 struct asymmetric_key {
@@ -219,7 +239,6 @@ static int decode_key(const char *key_file, const char *header_str,
                key[j++] = begin[i];
        }
        key[j] = '\0';
-       //PARA_CRIT_LOG("key: %s\n", key);
        blob_size = key_size * 2;
        blob = para_malloc(blob_size);
        ret = base64_decode(key, blob, blob_size);
@@ -255,14 +274,14 @@ enum asn1_types {
 /* bit 6 has value 0 */
 static inline bool is_primitive(unsigned char c)
 {
-       return ((c & (1<<6)) == 0);
+       return (c & (1<<6)) == 0;
 }
 
 static inline bool is_primitive_integer(unsigned char c)
 {
        if (!is_primitive(c))
                return false;
-       return ((c & 0x1f) == ASN1_TYPE_INTEGER);
+       return (c & 0x1f) == ASN1_TYPE_INTEGER;
 }
 
 /* Bit 8 is zero (and bits 7-1 give the length) */
@@ -285,7 +304,7 @@ static int find_pubkey_bignum_offset(const unsigned char *data, int len)
 {
        const unsigned char *p = data, *end = data + len;
 
-       /* the whole thing istarts with one sequence */
+       /* the whole thing starts with one sequence */
        if (*p != ASN1_TYPE_SEQUENCE)
                return -E_ASN1_PARSE;
        p++;
@@ -409,7 +428,7 @@ static int find_privkey_bignum_offset(const unsigned char *data, int len)
        if (p >= end)
                return -E_ASN1_PARSE;
 
-       /* Skip next integer  */
+       /* skip next integer */
        if (*p != ASN1_TYPE_INTEGER)
                return -E_ASN1_PARSE;
        p++;
@@ -479,7 +498,6 @@ static int get_private_key(const char *key_file, struct asymmetric_key **result)
        ret = read_bignum(cp, end, &u, NULL);
        if (ret < 0)
                goto release_q;
-       cp += ret;
        /*
         * OpenSSL uses slightly different parameters than gcrypt. To use these
         * parameters we need to swap the values of p and q and recompute u.
@@ -554,7 +572,6 @@ static int get_asn_public_key(const char *key_file, struct asymmetric_key **resu
        ret = read_bignum(cp, end, &e, NULL);
        if (ret < 0)
                goto release_n;
-       cp += ret;
 
        gret = gcry_sexp_build(&sexp, &erroff, RSA_PUBKEY_SEXP, n, e);
        if (gret) {
@@ -565,8 +582,9 @@ static int get_asn_public_key(const char *key_file, struct asymmetric_key **resu
        }
        key = para_malloc(sizeof(*key));
        key->sexp = sexp;
+       key->num_bytes = n_size;
        *result = key;
-       ret = n_size * 8;
+       ret = n_size;
        PARA_INFO_LOG("successfully read %u bit asn public key\n", n_size * 8);
 
 release_e:
@@ -586,7 +604,7 @@ static int get_ssh_public_key(unsigned char *data, int size, gcry_sexp_t *result
        size_t nr_scanned, erroff, decoded_size;
        gcry_mpi_t e = NULL, n = NULL;
 
-       PARA_DEBUG_LOG("decoding %d byte  public rsa-ssh key\n", size);
+       PARA_DEBUG_LOG("decoding %d byte public rsa-ssh key\n", size);
        if (size > INT_MAX / 4)
                return -ERRNO_TO_PARA_ERROR(EOVERFLOW);
        blob = para_malloc(2 * size);
@@ -677,7 +695,6 @@ int get_asymmetric_key(const char *key_file, int private,
        key->num_bytes = ret;
        key->sexp = sexp;
        *result = key;
-       ret = key->num_bytes;
 unmap:
        ret2 = para_munmap(map, map_size);
        if (ret >= 0 && ret2 < 0)
@@ -693,16 +710,76 @@ void free_asymmetric_key(struct asymmetric_key *key)
        free(key);
 }
 
+static int decode_rsa(gcry_sexp_t sexp, int key_size, unsigned char *outbuf,
+               size_t *nbytes)
+{
+       int ret;
+       gcry_error_t gret;
+       unsigned char oaep_buf[512];
+       gcry_mpi_t out_mpi;
+
+       if (libgcrypt_has_oaep) {
+               const char *p = gcry_sexp_nth_data(sexp, 1, nbytes);
+
+               if (!p) {
+                       PARA_ERROR_LOG("could not get data from list\n");
+                       return -E_OEAP;
+               }
+               memcpy(outbuf, p, *nbytes);
+               return 1;
+       }
+       out_mpi = gcry_sexp_nth_mpi(sexp, 0, GCRYMPI_FMT_USG);
+       if (!out_mpi)
+               return -E_SEXP_FIND;
+       gret = gcry_mpi_print(GCRYMPI_FMT_USG, oaep_buf, sizeof(oaep_buf),
+               nbytes, out_mpi);
+       if (gret) {
+               PARA_ERROR_LOG("mpi_print: %s\n", gcrypt_strerror(gret));
+               ret = -E_MPI_PRINT;
+               goto out_mpi_release;
+       }
+       /*
+        * An oaep-encoded buffer always starts with at least one zero byte.
+        * However, leading zeroes in an mpi are omitted in the output of
+        * gcry_mpi_print() when using the GCRYMPI_FMT_USG format. The
+        * alternative, GCRYMPI_FMT_STD, does not work either because here the
+        * leading zero(es) might also be omitted, depending on the value of
+        * the second byte.
+        *
+        * To circumvent this, we shift the oaep buffer to the right. But first
+        * we check that the buffer actually started with a zero byte, i.e. that
+        * nbytes < key_size. Otherwise a decoding error occurred.
+        */
+       ret = -E_SEXP_DECRYPT;
+       if (*nbytes >= key_size)
+               goto out_mpi_release;
+       memmove(oaep_buf + key_size - *nbytes, oaep_buf, *nbytes);
+       memset(oaep_buf, 0, key_size - *nbytes);
+
+       PARA_DEBUG_LOG("decrypted buffer before unpad (%d bytes):\n",
+               key_size);
+       dump_buffer("non-unpadded decrypted buffer", oaep_buf, key_size);
+       ret = unpad_oaep(oaep_buf, key_size, outbuf, nbytes);
+       if (ret < 0)
+               goto out_mpi_release;
+       PARA_DEBUG_LOG("decrypted buffer after unpad (%zu bytes):\n",
+               *nbytes);
+       dump_buffer("unpadded decrypted buffer", outbuf, *nbytes);
+       ret = 1;
+out_mpi_release:
+       gcry_mpi_release(out_mpi);
+       return ret;
+}
+
 int priv_decrypt(const char *key_file, unsigned char *outbuf,
                unsigned char *inbuf, int inlen)
 {
        gcry_error_t gret;
        int ret, key_size;
        struct asymmetric_key *priv;
-       gcry_mpi_t in_mpi = NULL, out_mpi = NULL;
+       gcry_mpi_t in_mpi = NULL;
        gcry_sexp_t in, out, priv_key;
        size_t nbytes;
-       unsigned char oaep_buf[512];
 
        PARA_INFO_LOG("decrypting %d byte input\n", inlen);
        /* key_file -> asymmetric key priv */
@@ -725,9 +802,8 @@ int priv_decrypt(const char *key_file, unsigned char *outbuf,
                ret = -E_MPI_SCAN;
                goto key_release;
        }
-
        /* in_mpi -> in sexp */
-       gret = gcry_sexp_build(&in, NULL, "(enc-val(rsa(a %m)))", in_mpi);
+       gret = gcry_sexp_build(&in, NULL, rsa_decrypt_sexp, in_mpi);
        if (gret) {
                PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
                ret = -E_SEXP_BUILD;
@@ -741,47 +817,11 @@ int priv_decrypt(const char *key_file, unsigned char *outbuf,
                ret = -E_SEXP_DECRYPT;
                goto in_release;
        }
-       out_mpi = gcry_sexp_nth_mpi(out, 0, GCRYMPI_FMT_USG);
-       if (!out_mpi) {
-               ret = -E_SEXP_FIND;
+       ret = decode_rsa(out, key_size, outbuf, &nbytes);
+       if (ret < 0)
                goto out_release;
-       }
-       gret = gcry_mpi_print(GCRYMPI_FMT_USG, oaep_buf, sizeof(oaep_buf),
-               &nbytes, out_mpi);
-       if (gret) {
-               PARA_ERROR_LOG("mpi_print: %s\n", gcrypt_strerror(gret));
-               ret = -E_MPI_PRINT;
-               goto out_mpi_release;
-       }
-       /*
-        * An oaep-encoded buffer always starts with at least one zero byte.
-        * However, leading zeroes in an mpi are omitted in the output of
-        * gcry_mpi_print() when using the GCRYMPI_FMT_USG format. The
-        * alternative, GCRYMPI_FMT_STD, does not work either because here the
-        * leading zero(es) might also be omitted, depending on the value of
-        * the second byte.
-        *
-        * To circumvent this, we shift the oaep buffer to the right. But first
-        * we check that the buffer actually started with a zero byte, i.e. that
-        * nbytes < key_size. Otherwise a decoding error occurred.
-        */
-       ret = -E_SEXP_DECRYPT;
-       if (nbytes >= key_size)
-               goto out_mpi_release;
-       memmove(oaep_buf + key_size - nbytes, oaep_buf, nbytes);
-       memset(oaep_buf, 0, key_size - nbytes);
-
-       PARA_DEBUG_LOG("decrypted buffer before unpad (%d bytes):\n",
-               key_size);
-       dump_buffer("non-unpadded decrypted buffer", oaep_buf, key_size);;
-       unpad_oaep(oaep_buf, key_size, outbuf, &nbytes);
-       PARA_DEBUG_LOG("decrypted buffer after unpad (%zu bytes):\n",
-               nbytes);
-       dump_buffer("unpadded decrypted buffer", outbuf, nbytes);;
+       PARA_INFO_LOG("successfully decrypted %zu byte message\n", nbytes);
        ret = nbytes;
-       PARA_INFO_LOG("successfully decrypted %u byte message\n", ret);
-out_mpi_release:
-       gcry_mpi_release(out_mpi);
 out_release:
        gcry_sexp_release(out);
 in_release:
@@ -799,12 +839,10 @@ int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
                unsigned len, unsigned char *outbuf)
 {
        gcry_error_t gret;
-       const size_t pad_size = 256;
        gcry_sexp_t pub_key, in, out, out_a;
        gcry_mpi_t out_mpi = NULL;
        size_t nbytes;
        int ret;
-       unsigned char padded_input[256];
 
        PARA_INFO_LOG("encrypting %u byte input with %d-byte key\n", len, pub->num_bytes);
 
@@ -812,13 +850,18 @@ int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
        pub_key = gcry_sexp_find_token(pub->sexp, "public-key", 0);
        if (!pub_key)
                return -E_SEXP_FIND;
-
-       /* inbuf -> padded inbuf */
-       pad_oaep(inbuf, len, padded_input, pad_size);
-
-       /* padded inbuf -> in sexp */
-       gret = gcry_sexp_build(&in, NULL, "(data(flags raw)(value %b))",
-               pad_size, padded_input);
+       if (libgcrypt_has_oaep) {
+               gret = gcry_sexp_build(&in, NULL,
+                       "(data(flags oaep)(value %b))", len, inbuf);
+       } else {
+               unsigned char padded_input[256];
+               const size_t pad_size = 256;
+               /* inbuf -> padded inbuf */
+               pad_oaep(inbuf, len, padded_input, pad_size);
+               /* padded inbuf -> in sexp */
+               gret = gcry_sexp_build(&in, NULL,
+                       "(data(flags raw)(value %b))", pad_size, padded_input);
+       }
        if (gret) {
                PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
                ret = -E_SEXP_BUILD;
@@ -869,11 +912,25 @@ struct stream_cipher {
        gcry_cipher_hd_t handle;
 };
 
-struct stream_cipher *sc_new(const unsigned char *data, int len)
+struct stream_cipher *sc_new(const unsigned char *data, int len,
+               bool use_aes)
 {
        gcry_error_t gret;
-
        struct stream_cipher *sc = para_malloc(sizeof(*sc));
+
+       if (use_aes) {
+               assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
+               gret = gcry_cipher_open(&sc->handle, GCRY_CIPHER_AES128,
+                       GCRY_CIPHER_MODE_CTR, 0);
+               assert(gret == 0);
+               gret = gcry_cipher_setkey(sc->handle, data,
+                       AES_CRT128_BLOCK_SIZE);
+               assert(gret == 0);
+               gret = gcry_cipher_setctr(sc->handle,
+                       data + AES_CRT128_BLOCK_SIZE, AES_CRT128_BLOCK_SIZE);
+               assert(gret == 0);
+               return sc;
+       }
        gret = gcry_cipher_open(&sc->handle, GCRY_CIPHER_ARCFOUR,
                GCRY_CIPHER_MODE_STREAM, 0);
        if (gret) {
@@ -894,35 +951,14 @@ void sc_free(struct stream_cipher *sc)
        free(sc);
 }
 
-int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf,
-               size_t size)
-{
-       gcry_error_t gret;
-       int ret;
-       unsigned char *tmp = para_malloc(size);
-
-       assert(size);
-       gret = gcry_cipher_encrypt(scc->send->handle, tmp, size,
-               (unsigned char *)buf, size);
-       assert(gret == 0);
-       ret = write_all(scc->fd, (char *)tmp, &size);
-       free(tmp);
-       return ret;
-}
-
-int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
-               size_t size)
+void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
 {
+       gcry_cipher_hd_t handle = sc->handle;
        gcry_error_t gret;
-       ssize_t ret = recv(scc->fd, buf, size, 0);
 
-       if (ret < 0)
-               ret = -ERRNO_TO_PARA_ERROR(errno);
-       if (ret <= 0)
-               return ret;
        /* perform in-place encryption */
-       gret = gcry_cipher_encrypt(scc->recv->handle, (unsigned char *)buf, ret,
+       *dst = *src;
+       gret = gcry_cipher_encrypt(handle, src->iov_base, src->iov_len,
                NULL, 0);
        assert(gret == 0);
-       return ret;
 }