Re: [PATCH v5 2/8] lib/mpi: Extend the MPI library

2020-07-12 Thread Tianjia Zhang




On 2020/7/10 21:12, Marcelo Henrique Cerri wrote:

Hi, Tianjia.

On Thu, Jul 09, 2020 at 04:40:09PM +0800, Tianjia Zhang wrote:

Expand the mpi library based on libgcrypt, and the ECC algorithm of
mpi based on libgcrypt requires these functions.
Some other algorithms will be developed based on mpi ecc, such as SM2.

Signed-off-by: Tianjia Zhang 
---
  include/linux/mpi.h|  88 +++
  lib/mpi/Makefile   |   5 +
  lib/mpi/mpi-add.c  | 207 +
  lib/mpi/mpi-bit.c  | 251 ++
  lib/mpi/mpi-cmp.c  |  46 --
  lib/mpi/mpi-div.c  | 238 +
  lib/mpi/mpi-internal.h |  53 +++
  lib/mpi/mpi-inv.c  | 143 ++
  lib/mpi/mpi-mod.c  | 155 +++
  lib/mpi/mpi-mul.c  |  94 
  lib/mpi/mpicoder.c | 336 +
  lib/mpi/mpih-div.c | 294 
  lib/mpi/mpih-mul.c |  25 +++
  lib/mpi/mpiutil.c  | 204 +
  14 files changed, 2129 insertions(+), 10 deletions(-)
  create mode 100644 lib/mpi/mpi-add.c
  create mode 100644 lib/mpi/mpi-div.c
  create mode 100644 lib/mpi/mpi-inv.c
  create mode 100644 lib/mpi/mpi-mod.c
  create mode 100644 lib/mpi/mpi-mul.c

diff --git a/lib/mpi/mpi-add.c b/lib/mpi/mpi-add.c
new file mode 100644
index ..9afad7832737
--- /dev/null
+++ b/lib/mpi/mpi-add.c
@@ -0,0 +1,207 @@
+/* mpi-add.c  -  MPI functions
+ * Copyright (C) 1994, 1996, 1998, 2001, 2002,
+ *   2003 Free Software Foundation, Inc.
+ *
+ * This file is part of Libgcrypt.
+ *
+ * Note: This code is heavily based on the GNU MP Library.
+ *  Actually it's the same code with only minor changes in the
+ *  way the data is stored; this is to support the abstraction
+ *  of an optional secure memory allocation which may be used
+ *  to avoid revealing of sensitive data due to paging etc.
+ */
+
+#include "mpi-internal.h"
+
+/
+ * Add the unsigned integer V to the mpi-integer U and store the
+ * result in W. U and V may be the same.
+ */
+void mpi_add_ui(MPI w, MPI u, unsigned long v)
+{
+   mpi_ptr_t wp, up;
+   mpi_size_t usize, wsize;
+   int usign, wsign;
+
+   usize = u->nlimbs;
+   usign = u->sign;
+   wsign = 0;
+
+   /* If not space for W (and possible carry), increase space.  */
+   wsize = usize + 1;
+   if (w->alloced < wsize)
+   mpi_resize(w, wsize);


You are ignoring the mpi_resize() return. I believe these new functions
need to return an int to indicate errors as mpi_powm() does.



Yes, of course.  Thanks for pointing it out.

Thanks,
Tianjia




+
+   /* These must be after realloc (U may be the same as W).  */
+   up = u->d;
+   wp = w->d;
+
+   if (!usize) {  /* simple */
+   wp[0] = v;
+   wsize = v ? 1:0;
+   } else if (!usign) {  /* mpi is not negative */
+   mpi_limb_t cy;
+   cy = mpihelp_add_1(wp, up, usize, v);
+   wp[usize] = cy;
+   wsize = usize + cy;
+   } else {
+   /* The signs are different.  Need exact comparison to determine
+* which operand to subtract from which.
+*/
+   if (usize == 1 && up[0] < v) {
+   wp[0] = v - up[0];
+   wsize = 1;
+   } else {
+   mpihelp_sub_1(wp, up, usize, v);
+   /* Size can decrease with at most one limb. */
+   wsize = usize - (wp[usize-1] == 0);
+   wsign = 1;
+   }
+   }
+
+   w->nlimbs = wsize;
+   w->sign   = wsign;
+}
+




Re: [PATCH v5 2/8] lib/mpi: Extend the MPI library

2020-07-10 Thread Marcelo Henrique Cerri
Hi, Tianjia.

On Thu, Jul 09, 2020 at 04:40:09PM +0800, Tianjia Zhang wrote:
> Expand the mpi library based on libgcrypt, and the ECC algorithm of
> mpi based on libgcrypt requires these functions.
> Some other algorithms will be developed based on mpi ecc, such as SM2.
> 
> Signed-off-by: Tianjia Zhang 
> ---
>  include/linux/mpi.h|  88 +++
>  lib/mpi/Makefile   |   5 +
>  lib/mpi/mpi-add.c  | 207 +
>  lib/mpi/mpi-bit.c  | 251 ++
>  lib/mpi/mpi-cmp.c  |  46 --
>  lib/mpi/mpi-div.c  | 238 +
>  lib/mpi/mpi-internal.h |  53 +++
>  lib/mpi/mpi-inv.c  | 143 ++
>  lib/mpi/mpi-mod.c  | 155 +++
>  lib/mpi/mpi-mul.c  |  94 
>  lib/mpi/mpicoder.c | 336 +
>  lib/mpi/mpih-div.c | 294 
>  lib/mpi/mpih-mul.c |  25 +++
>  lib/mpi/mpiutil.c  | 204 +
>  14 files changed, 2129 insertions(+), 10 deletions(-)
>  create mode 100644 lib/mpi/mpi-add.c
>  create mode 100644 lib/mpi/mpi-div.c
>  create mode 100644 lib/mpi/mpi-inv.c
>  create mode 100644 lib/mpi/mpi-mod.c
>  create mode 100644 lib/mpi/mpi-mul.c
> 
> diff --git a/include/linux/mpi.h b/include/linux/mpi.h
> index 7bd6d8af0004..2dddf4c6e011 100644
> --- a/include/linux/mpi.h
> +++ b/include/linux/mpi.h
> @@ -40,21 +40,79 @@ struct gcry_mpi {
>  typedef struct gcry_mpi *MPI;
>  
>  #define mpi_get_nlimbs(a) ((a)->nlimbs)
> +#define mpi_has_sign(a)   ((a)->sign)
>  
>  /*-- mpiutil.c --*/
>  MPI mpi_alloc(unsigned nlimbs);
> +void mpi_clear(MPI a);
>  void mpi_free(MPI a);
>  int mpi_resize(MPI a, unsigned nlimbs);
>  
> +static inline MPI mpi_new(unsigned int nbits)
> +{
> + return mpi_alloc((nbits + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB);
> +}
> +
> +MPI mpi_copy(MPI a);
> +MPI mpi_alloc_like(MPI a);
> +void mpi_snatch(MPI w, MPI u);
> +MPI mpi_set(MPI w, MPI u);
> +MPI mpi_set_ui(MPI w, unsigned long u);
> +MPI mpi_alloc_set_ui(unsigned long u);
> +void mpi_swap_cond(MPI a, MPI b, unsigned long swap);
> +
> +/* Constants used to return constant MPIs.  See mpi_init if you
> + * want to add more constants.
> + */
> +#define MPI_NUMBER_OF_CONSTANTS 6
> +enum gcry_mpi_constants {
> + MPI_C_ZERO,
> + MPI_C_ONE,
> + MPI_C_TWO,
> + MPI_C_THREE,
> + MPI_C_FOUR,
> + MPI_C_EIGHT
> +};
> +
> +MPI mpi_const(enum gcry_mpi_constants no);
> +
>  /*-- mpicoder.c --*/
> +
> +/* Different formats of external big integer representation. */
> +enum gcry_mpi_format {
> + GCRYMPI_FMT_NONE = 0,
> + GCRYMPI_FMT_STD = 1,/* Twos complement stored without length. */
> + GCRYMPI_FMT_PGP = 2,/* As used by OpenPGP (unsigned only). */
> + GCRYMPI_FMT_SSH = 3,/* As used by SSH (like STD but with length). */
> + GCRYMPI_FMT_HEX = 4,/* Hex format. */
> + GCRYMPI_FMT_USG = 5,/* Like STD but unsigned. */
> + GCRYMPI_FMT_OPAQUE = 8  /* Opaque format (some functions only). */
> +};
> +
>  MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
>  MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
> +int mpi_fromstr(MPI val, const char *str);
> +MPI mpi_scanval(const char *string);
>  MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
>  void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
>  int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
>   int *sign);
>  int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
>int *sign);
> +int mpi_print(enum gcry_mpi_format format, unsigned char *buffer,
> + size_t buflen, size_t *nwritten, MPI a);
> +
> +/*-- mpi-mod.c --*/
> +void mpi_mod(MPI rem, MPI dividend, MPI divisor);
> +
> +/* Context used with Barrett reduction.  */
> +struct barrett_ctx_s;
> +typedef struct barrett_ctx_s *mpi_barrett_t;
> +
> +mpi_barrett_t mpi_barrett_init(MPI m, int copy);
> +void mpi_barrett_free(mpi_barrett_t ctx);
> +void mpi_mod_barrett(MPI r, MPI x, mpi_barrett_t ctx);
> +void mpi_mul_barrett(MPI w, MPI u, MPI v, mpi_barrett_t ctx);
>  
>  /*-- mpi-pow.c --*/
>  int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
> @@ -62,10 +120,40 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
>  /*-- mpi-cmp.c --*/
>  int mpi_cmp_ui(MPI u, ulong v);
>  int mpi_cmp(MPI u, MPI v);
> +int mpi_cmpabs(MPI u, MPI v);
>  
>  /*-- mpi-bit.c --*/
>  void mpi_normalize(MPI a);
>  unsigned mpi_get_nbits(MPI a);
> +int mpi_test_bit(MPI a, unsigned int n);
> +void mpi_set_bit(MPI a, unsigned int n);
> +void mpi_set_highbit(MPI a, unsigned int n);
> +void mpi_clear_highbit(MPI a, unsigned int n);
> +void mpi_clear_bit(MPI a, unsigned int n);
> +void mpi_rshift_limbs(MPI a, unsigned int count);
> +void mpi_rshift(MPI x, MPI a, unsigned int n);
> +void mpi_lshift_limbs(MPI a, unsigned int co

[PATCH v5 2/8] lib/mpi: Extend the MPI library

2020-07-09 Thread Tianjia Zhang
Expand the mpi library based on libgcrypt, and the ECC algorithm of
mpi based on libgcrypt requires these functions.
Some other algorithms will be developed based on mpi ecc, such as SM2.

Signed-off-by: Tianjia Zhang 
---
 include/linux/mpi.h|  88 +++
 lib/mpi/Makefile   |   5 +
 lib/mpi/mpi-add.c  | 207 +
 lib/mpi/mpi-bit.c  | 251 ++
 lib/mpi/mpi-cmp.c  |  46 --
 lib/mpi/mpi-div.c  | 238 +
 lib/mpi/mpi-internal.h |  53 +++
 lib/mpi/mpi-inv.c  | 143 ++
 lib/mpi/mpi-mod.c  | 155 +++
 lib/mpi/mpi-mul.c  |  94 
 lib/mpi/mpicoder.c | 336 +
 lib/mpi/mpih-div.c | 294 
 lib/mpi/mpih-mul.c |  25 +++
 lib/mpi/mpiutil.c  | 204 +
 14 files changed, 2129 insertions(+), 10 deletions(-)
 create mode 100644 lib/mpi/mpi-add.c
 create mode 100644 lib/mpi/mpi-div.c
 create mode 100644 lib/mpi/mpi-inv.c
 create mode 100644 lib/mpi/mpi-mod.c
 create mode 100644 lib/mpi/mpi-mul.c

diff --git a/include/linux/mpi.h b/include/linux/mpi.h
index 7bd6d8af0004..2dddf4c6e011 100644
--- a/include/linux/mpi.h
+++ b/include/linux/mpi.h
@@ -40,21 +40,79 @@ struct gcry_mpi {
 typedef struct gcry_mpi *MPI;
 
 #define mpi_get_nlimbs(a) ((a)->nlimbs)
+#define mpi_has_sign(a)   ((a)->sign)
 
 /*-- mpiutil.c --*/
 MPI mpi_alloc(unsigned nlimbs);
+void mpi_clear(MPI a);
 void mpi_free(MPI a);
 int mpi_resize(MPI a, unsigned nlimbs);
 
+static inline MPI mpi_new(unsigned int nbits)
+{
+   return mpi_alloc((nbits + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB);
+}
+
+MPI mpi_copy(MPI a);
+MPI mpi_alloc_like(MPI a);
+void mpi_snatch(MPI w, MPI u);
+MPI mpi_set(MPI w, MPI u);
+MPI mpi_set_ui(MPI w, unsigned long u);
+MPI mpi_alloc_set_ui(unsigned long u);
+void mpi_swap_cond(MPI a, MPI b, unsigned long swap);
+
+/* Constants used to return constant MPIs.  See mpi_init if you
+ * want to add more constants.
+ */
+#define MPI_NUMBER_OF_CONSTANTS 6
+enum gcry_mpi_constants {
+   MPI_C_ZERO,
+   MPI_C_ONE,
+   MPI_C_TWO,
+   MPI_C_THREE,
+   MPI_C_FOUR,
+   MPI_C_EIGHT
+};
+
+MPI mpi_const(enum gcry_mpi_constants no);
+
 /*-- mpicoder.c --*/
+
+/* Different formats of external big integer representation. */
+enum gcry_mpi_format {
+   GCRYMPI_FMT_NONE = 0,
+   GCRYMPI_FMT_STD = 1,/* Twos complement stored without length. */
+   GCRYMPI_FMT_PGP = 2,/* As used by OpenPGP (unsigned only). */
+   GCRYMPI_FMT_SSH = 3,/* As used by SSH (like STD but with length). */
+   GCRYMPI_FMT_HEX = 4,/* Hex format. */
+   GCRYMPI_FMT_USG = 5,/* Like STD but unsigned. */
+   GCRYMPI_FMT_OPAQUE = 8  /* Opaque format (some functions only). */
+};
+
 MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
 MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
+int mpi_fromstr(MPI val, const char *str);
+MPI mpi_scanval(const char *string);
 MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
 int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
int *sign);
 int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
 int *sign);
+int mpi_print(enum gcry_mpi_format format, unsigned char *buffer,
+   size_t buflen, size_t *nwritten, MPI a);
+
+/*-- mpi-mod.c --*/
+void mpi_mod(MPI rem, MPI dividend, MPI divisor);
+
+/* Context used with Barrett reduction.  */
+struct barrett_ctx_s;
+typedef struct barrett_ctx_s *mpi_barrett_t;
+
+mpi_barrett_t mpi_barrett_init(MPI m, int copy);
+void mpi_barrett_free(mpi_barrett_t ctx);
+void mpi_mod_barrett(MPI r, MPI x, mpi_barrett_t ctx);
+void mpi_mul_barrett(MPI w, MPI u, MPI v, mpi_barrett_t ctx);
 
 /*-- mpi-pow.c --*/
 int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
@@ -62,10 +120,40 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
 /*-- mpi-cmp.c --*/
 int mpi_cmp_ui(MPI u, ulong v);
 int mpi_cmp(MPI u, MPI v);
+int mpi_cmpabs(MPI u, MPI v);
 
 /*-- mpi-bit.c --*/
 void mpi_normalize(MPI a);
 unsigned mpi_get_nbits(MPI a);
+int mpi_test_bit(MPI a, unsigned int n);
+void mpi_set_bit(MPI a, unsigned int n);
+void mpi_set_highbit(MPI a, unsigned int n);
+void mpi_clear_highbit(MPI a, unsigned int n);
+void mpi_clear_bit(MPI a, unsigned int n);
+void mpi_rshift_limbs(MPI a, unsigned int count);
+void mpi_rshift(MPI x, MPI a, unsigned int n);
+void mpi_lshift_limbs(MPI a, unsigned int count);
+void mpi_lshift(MPI x, MPI a, unsigned int n);
+
+/*-- mpi-add.c --*/
+void mpi_add_ui(MPI w, MPI u, unsigned long v);
+void mpi_add(MPI w, MPI u, MPI v);
+void mpi_sub_ui(MPI w, MPI u, unsigned long v);
+void mpi_sub(MPI w, MPI u, MPI v);
+void mpi_addm(MPI w, MPI u, MPI v, MPI m);
+void mp