Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7e8dec91 authored by Dmitry Kasatkin's avatar Dmitry Kasatkin
Browse files

crypto: GnuPG based MPI lib - additional sources (part 4)



Adds the multi-precision-integer maths library which was originally taken
from GnuPG and ported to the kernel by (among others) David Howells.
This version is taken from Fedora kernel 2.6.32-71.14.1.el6.
The difference is that checkpatch reported errors and warnings have been fixed.

This library is used to implemenet RSA digital signature verification
used in IMA/EVM integrity protection subsystem.

Due to patch size limitation, the patch is divided into 4 parts.

This code is unnecessary for RSA digital signature verification,
but for completeness it is included here and can be compiled,
if CONFIG_MPILIB_EXTRA is enabled.

Signed-off-by: default avatarDmitry Kasatkin <dmitry.kasatkin@intel.com>
parent d9c46b18
Loading
Loading
Loading
Loading
+10 −0
Original line number Original line Diff line number Diff line
@@ -283,4 +283,14 @@ config MPILIB
	  It is used to implement RSA digital signature verification,
	  It is used to implement RSA digital signature verification,
	  which is used by IMA/EVM digital signature extension.
	  which is used by IMA/EVM digital signature extension.


config MPILIB_EXTRA
	bool "Multiprecision maths library - additional sources"
	depends on MPILIB
	help
	  Multiprecision maths library from GnuPG.
	  It is used to implement RSA digital signature verification,
	  which is used by IMA/EVM digital signature extension.
	  This code in unnecessary for RSA digital signature verification,
	  and can be compiled if needed.

endmenu
endmenu
+11 −0
Original line number Original line Diff line number Diff line
@@ -19,3 +19,14 @@ mpi-y = \
	mpih-mul.o			\
	mpih-mul.o			\
	mpi-pow.o			\
	mpi-pow.o			\
	mpiutil.o
	mpiutil.o

mpi-$(CONFIG_MPILIB_EXTRA) += \
	mpi-add.o			\
	mpi-div.o			\
	mpi-cmp.o			\
	mpi-gcd.o			\
	mpi-inline.o			\
	mpi-inv.o			\
	mpi-mpow.o			\
	mpi-mul.o			\
	mpi-scan.o
+4 −0
Original line number Original line Diff line number Diff line
/* This file defines some basic constants for the MPI machinery.  We
 * need to define the types on a per-CPU basis, so it is done with
 * this file here.  */
#define BYTES_PER_MPI_LIMB  (SIZEOF_UNSIGNED_LONG)

lib/mpi/mpi-add.c

0 → 100644
+234 −0
Original line number Original line Diff line number Diff line
/* mpi-add.c  -  MPI functions
 *	Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 *	Copyright (C) 1994, 1996 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * 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"

/****************
 * Add the unsigned integer V to the mpi-integer U and store the
 * result in W. U and V may be the same.
 */
int mpi_add_ui(MPI w, const 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)
		if (mpi_resize(w, wsize) < 0)
			return -ENOMEM;

	/* 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;
	return 0;
}

int mpi_add(MPI w, MPI u, MPI v)
{
	mpi_ptr_t wp, up, vp;
	mpi_size_t usize, vsize, wsize;
	int usign, vsign, wsign;

	if (u->nlimbs < v->nlimbs) {	/* Swap U and V. */
		usize = v->nlimbs;
		usign = v->sign;
		vsize = u->nlimbs;
		vsign = u->sign;
		wsize = usize + 1;
		if (RESIZE_IF_NEEDED(w, wsize) < 0)
			return -ENOMEM;
		/* These must be after realloc (u or v may be the same as w).  */
		up = v->d;
		vp = u->d;
	} else {
		usize = u->nlimbs;
		usign = u->sign;
		vsize = v->nlimbs;
		vsign = v->sign;
		wsize = usize + 1;
		if (RESIZE_IF_NEEDED(w, wsize) < 0)
			return -ENOMEM;
		/* These must be after realloc (u or v may be the same as w).  */
		up = u->d;
		vp = v->d;
	}
	wp = w->d;
	wsign = 0;

	if (!vsize) {		/* simple */
		MPN_COPY(wp, up, usize);
		wsize = usize;
		wsign = usign;
	} else if (usign != vsign) {	/* different sign */
		/* This test is right since USIZE >= VSIZE */
		if (usize != vsize) {
			mpihelp_sub(wp, up, usize, vp, vsize);
			wsize = usize;
			MPN_NORMALIZE(wp, wsize);
			wsign = usign;
		} else if (mpihelp_cmp(up, vp, usize) < 0) {
			mpihelp_sub_n(wp, vp, up, usize);
			wsize = usize;
			MPN_NORMALIZE(wp, wsize);
			if (!usign)
				wsign = 1;
		} else {
			mpihelp_sub_n(wp, up, vp, usize);
			wsize = usize;
			MPN_NORMALIZE(wp, wsize);
			if (usign)
				wsign = 1;
		}
	} else {		/* U and V have same sign. Add them. */
		mpi_limb_t cy = mpihelp_add(wp, up, usize, vp, vsize);
		wp[usize] = cy;
		wsize = usize + cy;
		if (usign)
			wsign = 1;
	}

	w->nlimbs = wsize;
	w->sign = wsign;
	return 0;
}

/****************
 * Subtract the unsigned integer V from the mpi-integer U and store the
 * result in W.
 */
int mpi_sub_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)
		if (mpi_resize(w, wsize) < 0)
			return -ENOMEM;

	/* 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;
		wsign = 1;
	} else if (usign) {	/* mpi and v are 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;
			wsign = 1;
		} else {
			mpihelp_sub_1(wp, up, usize, v);
			/* Size can decrease with at most one limb. */
			wsize = usize - (wp[usize - 1] == 0);
		}
	}

	w->nlimbs = wsize;
	w->sign = wsign;
	return 0;
}

int mpi_sub(MPI w, MPI u, MPI v)
{
	int rc;

	if (w == v) {
		MPI vv;
		if (mpi_copy(&vv, v) < 0)
			return -ENOMEM;
		vv->sign = !vv->sign;
		rc = mpi_add(w, u, vv);
		mpi_free(vv);
	} else {
		/* fixme: this is not thread-save (we temp. modify v) */
		v->sign = !v->sign;
		rc = mpi_add(w, u, v);
		v->sign = !v->sign;
	}
	return rc;
}

int mpi_addm(MPI w, MPI u, MPI v, MPI m)
{
	if (mpi_add(w, u, v) < 0 || mpi_fdiv_r(w, w, m) < 0)
		return -ENOMEM;
	return 0;
}

int mpi_subm(MPI w, MPI u, MPI v, MPI m)
{
	if (mpi_sub(w, u, v) < 0 || mpi_fdiv_r(w, w, m) < 0)
		return -ENOMEM;
	return 0;
}

lib/mpi/mpi-cmp.c

0 → 100644
+68 −0
Original line number Original line Diff line number Diff line
/* mpi-cmp.c  -  MPI functions
 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "mpi-internal.h"

int mpi_cmp_ui(MPI u, unsigned long v)
{
	mpi_limb_t limb = v;

	mpi_normalize(u);
	if (!u->nlimbs && !limb)
		return 0;
	if (u->sign)
		return -1;
	if (u->nlimbs > 1)
		return 1;

	if (u->d[0] == limb)
		return 0;
	else if (u->d[0] > limb)
		return 1;
	else
		return -1;
}

int mpi_cmp(MPI u, MPI v)
{
	mpi_size_t usize, vsize;
	int cmp;

	mpi_normalize(u);
	mpi_normalize(v);
	usize = u->nlimbs;
	vsize = v->nlimbs;
	if (!u->sign && v->sign)
		return 1;
	if (u->sign && !v->sign)
		return -1;
	if (usize != vsize && !u->sign && !v->sign)
		return usize - vsize;
	if (usize != vsize && u->sign && v->sign)
		return vsize + usize;
	if (!usize)
		return 0;
	cmp = mpihelp_cmp(u->d, v->d, usize);
	if (!cmp)
		return 0;
	if ((cmp < 0 ? 1 : 0) == (u->sign ? 1 : 0))
		return 1;
	return -1;
}
Loading