commit caf4480811fffdf3b8677864e8d663a68f210e5c
Author: Werner Koch <wk@gnupg.org>
Date:   Fri Jun 10 10:52:18 2011 +0200

    Fixed leading zero problems in PSS and OAEP.

Index: libgcrypt-1.5.0/cipher/pubkey.c
===================================================================
--- libgcrypt-1.5.0.orig/cipher/pubkey.c	2011-06-13 12:23:50.000000000 +0200
+++ libgcrypt-1.5.0/cipher/pubkey.c	2011-09-05 13:47:08.771881919 +0200
@@ -2773,7 +2773,7 @@
             rc = GPG_ERR_DIGEST_ALGO;
 	  else
 	    {
-	      *ret_mpi = gcry_sexp_nth_mpi (lhash, 2, GCRYMPI_FMT_USG);
+	      *ret_mpi = gcry_sexp_nth_mpi (lhash, 2, 0);
 	      if (!*ret_mpi)
 		rc = GPG_ERR_INV_OBJ;
 	      ctx->verify_cmp = pss_verify_cmp;
@@ -2892,71 +2892,50 @@
     goto leave;
 
   /* We did it.  Now build the return list */
-  if (ctx.encoding == PUBKEY_ENC_OAEP
-      || ctx.encoding == PUBKEY_ENC_PKCS1)
-    {
-      /* We need to make sure to return the correct length to avoid
-         problems with missing leading zeroes.  We know that this
-         encoding does only make sense with RSA thus we don't need to
-         build the S-expression on the fly.  */
-      unsigned char *em;
-      size_t emlen = (ctx.nbits+7)/8;
-
-      rc = octet_string_from_mpi (&em, NULL, ciph[0], emlen);
-      if (rc)
+  {
+    char *string, *p;
+    int i;
+    size_t nelem = strlen (algo_elems);
+    size_t needed = 19 + strlen (algo_name) + (nelem * 5);
+    void **arg_list;
+
+    /* Build the string.  */
+    string = p = gcry_malloc (needed);
+    if (!string)
+      {
+        rc = gpg_err_code_from_syserror ();
         goto leave;
-      rc = gcry_err_code (gcry_sexp_build (r_ciph, NULL,
-                                           "(enc-val(%s(a%b)))",
-                                           algo_name, (int)emlen, em));
-      gcry_free (em);
-      if (rc)
+      }
+    p = stpcpy ( p, "(enc-val(" );
+    p = stpcpy ( p, algo_name );
+    for (i=0; algo_elems[i]; i++ )
+      {
+        *p++ = '(';
+        *p++ = algo_elems[i];
+        p = stpcpy ( p, "%m)" );
+      }
+    strcpy ( p, "))" );
+
+    /* And now the ugly part: We don't have a function to pass an
+     * array to a format string, so we have to do it this way :-(.  */
+    /* FIXME: There is now such a format specifier, so we can
+       change the code to be more clear. */
+    arg_list = malloc (nelem * sizeof *arg_list);
+    if (!arg_list)
+      {
+        rc = gpg_err_code_from_syserror ();
         goto leave;
-    }
-  else
-    {
-      char *string, *p;
-      int i;
-      size_t nelem = strlen (algo_elems);
-      size_t needed = 19 + strlen (algo_name) + (nelem * 5);
-      void **arg_list;
-
-      /* Build the string.  */
-      string = p = gcry_malloc (needed);
-      if (!string)
-        {
-          rc = gpg_err_code_from_syserror ();
-          goto leave;
-        }
-      p = stpcpy ( p, "(enc-val(" );
-      p = stpcpy ( p, algo_name );
-      for (i=0; algo_elems[i]; i++ )
-        {
-          *p++ = '(';
-          *p++ = algo_elems[i];
-          p = stpcpy ( p, "%m)" );
-        }
-      strcpy ( p, "))" );
-
-      /* And now the ugly part: We don't have a function to pass an
-       * array to a format string, so we have to do it this way :-(.  */
-      /* FIXME: There is now such a format specifier, so we can
-         change the code to be more clear. */
-      arg_list = malloc (nelem * sizeof *arg_list);
-      if (!arg_list)
-        {
-          rc = gpg_err_code_from_syserror ();
-          goto leave;
-        }
-
-      for (i = 0; i < nelem; i++)
-        arg_list[i] = ciph + i;
-
-      rc = gcry_sexp_build_array (r_ciph, NULL, string, arg_list);
-      free (arg_list);
-      if (rc)
-        BUG ();
-      gcry_free (string);
-    }
+      }
+
+    for (i = 0; i < nelem; i++)
+      arg_list[i] = ciph + i;
+
+    rc = gcry_sexp_build_array (r_ciph, NULL, string, arg_list);
+    free (arg_list);
+    if (rc)
+      BUG ();
+    gcry_free (string);
+  }
 
  leave:
   if (pkey)
@@ -3184,71 +3163,49 @@
   if (rc)
     goto leave;
 
-  if (ctx.encoding == PUBKEY_ENC_PSS
-      || ctx.encoding == PUBKEY_ENC_PKCS1)
-    {
-      /* We need to make sure to return the correct length to avoid
-         problems with missing leading zeroes.  We know that this
-         encoding does only make sense with RSA thus we don't need to
-         build the S-expression on the fly.  */
-      unsigned char *em;
-      size_t emlen = (ctx.nbits+7)/8;
-
-      rc = octet_string_from_mpi (&em, NULL, result[0], emlen);
-      if (rc)
+  {
+    char *string, *p;
+    size_t nelem, needed = strlen (algo_name) + 20;
+    void **arg_list;
+
+    nelem = strlen (algo_elems);
+
+    /* Count elements, so that we can allocate enough space. */
+    needed += 10 * nelem;
+
+    /* Build the string. */
+    string = p = gcry_malloc (needed);
+    if (!string)
+      {
+        rc = gpg_err_code_from_syserror ();
         goto leave;
-      rc = gcry_err_code (gcry_sexp_build (r_sig, NULL,
-                                           "(sig-val(%s(s%b)))",
-                                           algo_name, (int)emlen, em));
-      gcry_free (em);
-      if (rc)
+      }
+    p = stpcpy (p, "(sig-val(");
+    p = stpcpy (p, algo_name);
+    for (i = 0; algo_elems[i]; i++)
+      {
+        *p++ = '(';
+        *p++ = algo_elems[i];
+        p = stpcpy (p, "%m)");
+      }
+    strcpy (p, "))");
+
+    arg_list = malloc (nelem * sizeof *arg_list);
+    if (!arg_list)
+      {
+        rc = gpg_err_code_from_syserror ();
         goto leave;
-    }
-  else
-    {
-      /* General purpose output encoding.  Do it on the fly.  */
-      char *string, *p;
-      size_t nelem, needed = strlen (algo_name) + 20;
-      void **arg_list;
-
-      nelem = strlen (algo_elems);
-
-      /* Count elements, so that we can allocate enough space. */
-      needed += 10 * nelem;
-
-      /* Build the string. */
-      string = p = gcry_malloc (needed);
-      if (!string)
-        {
-          rc = gpg_err_code_from_syserror ();
-          goto leave;
-        }
-      p = stpcpy (p, "(sig-val(");
-      p = stpcpy (p, algo_name);
-      for (i = 0; algo_elems[i]; i++)
-        {
-          *p++ = '(';
-          *p++ = algo_elems[i];
-          p = stpcpy (p, "%M)");
-        }
-      strcpy (p, "))");
-
-      arg_list = malloc (nelem * sizeof *arg_list);
-      if (!arg_list)
-        {
-          rc = gpg_err_code_from_syserror ();
-          goto leave;
-        }
-
-      for (i = 0; i < nelem; i++)
-        arg_list[i] = result + i;
-
-      rc = gcry_sexp_build_array (r_sig, NULL, string, arg_list);
-      free (arg_list);
-      if (rc)
-        BUG ();
-      gcry_free (string);
-    }
+      }
+
+    for (i = 0; i < nelem; i++)
+      arg_list[i] = result + i;
+
+    rc = gcry_sexp_build_array (r_sig, NULL, string, arg_list);
+    free (arg_list);
+    if (rc)
+      BUG ();
+    gcry_free (string);
+  }
 
  leave:
   if (skey)
Index: libgcrypt-1.5.0/src/sexp.c
===================================================================
--- libgcrypt-1.5.0.orig/src/sexp.c	2011-06-10 10:53:41.000000000 +0200
+++ libgcrypt-1.5.0/src/sexp.c	2011-09-05 13:39:12.696017117 +0200
@@ -1,6 +1,6 @@
 /* sexp.c  -  S-Expression handling
  * Copyright (C) 1999, 2000, 2001, 2002, 2003,
- *               2004, 2006, 2007, 2008, 2011  Free Software Foundation, Inc.
+ *               2004, 2006, 2007, 2008  Free Software Foundation, Inc.
  *
  * This file is part of Libgcrypt.
  *
@@ -1249,12 +1249,11 @@
 	}
       else if (percent)
 	{
-	  if (*p == 'm' || *p == 'M')
+	  if (*p == 'm')
 	    {
 	      /* Insert an MPI.  */
 	      gcry_mpi_t m;
 	      size_t nm = 0;
-              int mpifmt = *p == 'm'? GCRYMPI_FMT_STD: GCRYMPI_FMT_USG;
 
 	      ARG_NEXT (m, gcry_mpi_t);
 
@@ -1297,7 +1296,7 @@
                 }
               else
                 {
-                  if (gcry_mpi_print (mpifmt, NULL, 0, &nm, m))
+                  if (gcry_mpi_print (GCRYMPI_FMT_STD, NULL, 0, &nm, m))
                     BUG ();
 
                   MAKE_SPACE (nm);
@@ -1324,7 +1323,7 @@
 
                   *c.pos++ = ST_DATA;
                   STORE_LEN (c.pos, nm);
-                  if (gcry_mpi_print (mpifmt, c.pos, nm, &nm, m))
+                  if (gcry_mpi_print (GCRYMPI_FMT_STD, c.pos, nm, &nm, m))
                     BUG ();
                   c.pos += nm;
                 }
@@ -1386,29 +1385,13 @@
 	      /* Insert an integer as string.  */
 	      int aint;
 	      size_t alen;
-	      char buf[35];
+	      char buf[20];
 
 	      ARG_NEXT (aint, int);
 	      sprintf (buf, "%d", aint);
 	      alen = strlen (buf);
 	      MAKE_SPACE (alen);
 	      *c.pos++ = ST_DATA;
-	      STORE_LEN (c.pos, alen);
-	      memcpy (c.pos, buf, alen);
-	      c.pos += alen;
-	    }
-	  else if (*p == 'u')
-	    {
-	      /* Insert an unsigned integer as string.  */
-	      unsigned int aint;
-	      size_t alen;
-	      char buf[35];
-
-	      ARG_NEXT (aint, unsigned int);
-	      sprintf (buf, "%u", aint);
-	      alen = strlen (buf);
-	      MAKE_SPACE (alen);
-	      *c.pos++ = ST_DATA;
 	      STORE_LEN (c.pos, alen);
 	      memcpy (c.pos, buf, alen);
 	      c.pos += alen;
