2020-07-10 10:17:02

by Stephan Müller

[permalink] [raw]
Subject: [PATCH 2/3] lib/mpi: Add mpi_sub_ui()

Add mpi_sub_ui() based on Gnu PG mpz_sub_ui() from mpz/aors_ui.h
adapting the code to the kernel's structures and coding style and also
removing the defines used to produce mpz_sub_ui() and mpz_add_ui()
from the same code.

Signed-off-by: Marcelo Henrique Cerri <[email protected]>
Signed-off-by: Stephan Mueller <[email protected]>
---
include/linux/mpi.h | 3 +++
lib/mpi/Makefile | 1 +
lib/mpi/mpi-sub-ui.c | 60 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 64 insertions(+)
create mode 100644 lib/mpi/mpi-sub-ui.c

diff --git a/include/linux/mpi.h b/include/linux/mpi.h
index 7bd6d8af0004..5d906dfbf3ed 100644
--- a/include/linux/mpi.h
+++ b/include/linux/mpi.h
@@ -63,6 +63,9 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
int mpi_cmp_ui(MPI u, ulong v);
int mpi_cmp(MPI u, MPI v);

+/*-- mpi-sub-ui.c --*/
+int mpi_sub_ui(MPI w, MPI u, unsigned long vval);
+
/*-- mpi-bit.c --*/
void mpi_normalize(MPI a);
unsigned mpi_get_nbits(MPI a);
diff --git a/lib/mpi/Makefile b/lib/mpi/Makefile
index d5874a7f5ff9..43b8fce14079 100644
--- a/lib/mpi/Makefile
+++ b/lib/mpi/Makefile
@@ -16,6 +16,7 @@ mpi-y = \
mpicoder.o \
mpi-bit.o \
mpi-cmp.o \
+ mpi-sub-ui.o \
mpih-cmp.o \
mpih-div.o \
mpih-mul.o \
diff --git a/lib/mpi/mpi-sub-ui.c b/lib/mpi/mpi-sub-ui.c
new file mode 100644
index 000000000000..fa6b085bac36
--- /dev/null
+++ b/lib/mpi/mpi-sub-ui.c
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* mpi-sub-ui.c - MPI functions
+ * Copyright 1991, 1993, 1994, 1996, 1999-2002, 2004, 2012, 2013, 2015
+ * Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * 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.
+ * The GNU MP Library itself is published under the LGPL;
+ * however I decided to publish this code under the plain GPL.
+ */
+
+#include "mpi-internal.h"
+
+int mpi_sub_ui(MPI w, MPI u, unsigned long vval)
+{
+ if (u->nlimbs == 0) {
+ if (mpi_resize(w, 1) < 0)
+ return -ENOMEM;
+ w->d[0] = vval;
+ w->nlimbs = (vval != 0);
+ w->sign = (vval != 0);
+ return 0;
+ }
+
+ /* If not space for W (and possible carry), increase space. */
+ if (mpi_resize(w, u->nlimbs + 1))
+ return -ENOMEM;
+
+ if (u->sign) {
+ mpi_limb_t cy;
+
+ cy = mpihelp_add_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);
+ w->d[u->nlimbs] = cy;
+ w->nlimbs = u->nlimbs + cy;
+ w->sign = 1;
+ } else {
+ /* The signs are different. Need exact comparison to determine
+ * which operand to subtract from which.
+ */
+ if (u->nlimbs == 1 && u->d[0] < vval) {
+ w->d[0] = vval - u->d[0];
+ w->nlimbs = 1;
+ w->sign = 1;
+ } else {
+ mpihelp_sub_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);
+ /* Size can decrease with at most one limb. */
+ w->nlimbs = (u->nlimbs - (w->d[u->nlimbs - 1] == 0));
+ w->sign = 0;
+ }
+ }
+
+ mpi_normalize(w);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(mpi_sub_ui);
--
2.26.2





2020-07-10 14:43:26

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH 2/3] lib/mpi: Add mpi_sub_ui()

On Fri, 10 Jul 2020 at 13:16, Stephan Müller <[email protected]> wrote:
>
> Add mpi_sub_ui() based on Gnu PG mpz_sub_ui() from mpz/aors_ui.h
> adapting the code to the kernel's structures and coding style and also
> removing the defines used to produce mpz_sub_ui() and mpz_add_ui()
> from the same code.
>

Isn't GnuPG GPLv3 ?


> Signed-off-by: Marcelo Henrique Cerri <[email protected]>
> Signed-off-by: Stephan Mueller <[email protected]>
> ---
> include/linux/mpi.h | 3 +++
> lib/mpi/Makefile | 1 +
> lib/mpi/mpi-sub-ui.c | 60 ++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 64 insertions(+)
> create mode 100644 lib/mpi/mpi-sub-ui.c
>
> diff --git a/include/linux/mpi.h b/include/linux/mpi.h
> index 7bd6d8af0004..5d906dfbf3ed 100644
> --- a/include/linux/mpi.h
> +++ b/include/linux/mpi.h
> @@ -63,6 +63,9 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
> int mpi_cmp_ui(MPI u, ulong v);
> int mpi_cmp(MPI u, MPI v);
>
> +/*-- mpi-sub-ui.c --*/
> +int mpi_sub_ui(MPI w, MPI u, unsigned long vval);
> +
> /*-- mpi-bit.c --*/
> void mpi_normalize(MPI a);
> unsigned mpi_get_nbits(MPI a);
> diff --git a/lib/mpi/Makefile b/lib/mpi/Makefile
> index d5874a7f5ff9..43b8fce14079 100644
> --- a/lib/mpi/Makefile
> +++ b/lib/mpi/Makefile
> @@ -16,6 +16,7 @@ mpi-y = \
> mpicoder.o \
> mpi-bit.o \
> mpi-cmp.o \
> + mpi-sub-ui.o \
> mpih-cmp.o \
> mpih-div.o \
> mpih-mul.o \
> diff --git a/lib/mpi/mpi-sub-ui.c b/lib/mpi/mpi-sub-ui.c
> new file mode 100644
> index 000000000000..fa6b085bac36
> --- /dev/null
> +++ b/lib/mpi/mpi-sub-ui.c
> @@ -0,0 +1,60 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/* mpi-sub-ui.c - MPI functions
> + * Copyright 1991, 1993, 1994, 1996, 1999-2002, 2004, 2012, 2013, 2015
> + * Free Software Foundation, Inc.
> + *
> + * This file is part of GnuPG.
> + *
> + * 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.
> + * The GNU MP Library itself is published under the LGPL;
> + * however I decided to publish this code under the plain GPL.
> + */
> +
> +#include "mpi-internal.h"
> +
> +int mpi_sub_ui(MPI w, MPI u, unsigned long vval)
> +{
> + if (u->nlimbs == 0) {
> + if (mpi_resize(w, 1) < 0)
> + return -ENOMEM;
> + w->d[0] = vval;
> + w->nlimbs = (vval != 0);
> + w->sign = (vval != 0);
> + return 0;
> + }
> +
> + /* If not space for W (and possible carry), increase space. */
> + if (mpi_resize(w, u->nlimbs + 1))
> + return -ENOMEM;
> +
> + if (u->sign) {
> + mpi_limb_t cy;
> +
> + cy = mpihelp_add_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);
> + w->d[u->nlimbs] = cy;
> + w->nlimbs = u->nlimbs + cy;
> + w->sign = 1;
> + } else {
> + /* The signs are different. Need exact comparison to determine
> + * which operand to subtract from which.
> + */
> + if (u->nlimbs == 1 && u->d[0] < vval) {
> + w->d[0] = vval - u->d[0];
> + w->nlimbs = 1;
> + w->sign = 1;
> + } else {
> + mpihelp_sub_1(w->d, u->d, u->nlimbs, (mpi_limb_t) vval);
> + /* Size can decrease with at most one limb. */
> + w->nlimbs = (u->nlimbs - (w->d[u->nlimbs - 1] == 0));
> + w->sign = 0;
> + }
> + }
> +
> + mpi_normalize(w);
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(mpi_sub_ui);
> --
> 2.26.2
>
>
>
>

2020-07-10 15:11:16

by Stephan Müller

[permalink] [raw]
Subject: Re: [PATCH 2/3] lib/mpi: Add mpi_sub_ui()

Am Freitag, 10. Juli 2020, 16:42:39 CEST schrieb Ard Biesheuvel:

Hi Ard,

> On Fri, 10 Jul 2020 at 13:16, Stephan M?ller <[email protected]> wrote:
> > Add mpi_sub_ui() based on Gnu PG mpz_sub_ui() from mpz/aors_ui.h
> > adapting the code to the kernel's structures and coding style and also
> > removing the defines used to produce mpz_sub_ui() and mpz_add_ui()
> > from the same code.
>
> Isn't GnuPG GPLv3 ?

Thanks for pointing that out. It is actually a mix-up - the function is from
Gnu MP and not GnuPG.

Gnu MP is GPLv2 and GPLv3.

I will have that corrected with a 2nd patch.

Thanks

Ciao
Stephan