Skip to content

Commit cdec9cb

Browse files
author
Dmitry Kasatkin
committed
crypto: GnuPG based MPI lib - source files (part 1)
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. Signed-off-by: Dmitry Kasatkin <[email protected]>
1 parent 1ea6b8f commit cdec9cb

14 files changed

+2681
-0
lines changed

lib/mpi/generic_mpih-add1.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* mpihelp-add_1.c - MPI helper functions
2+
* Copyright (C) 1994, 1996, 1997, 1998,
3+
* 2000 Free Software Foundation, Inc.
4+
*
5+
* This file is part of GnuPG.
6+
*
7+
* GnuPG is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* GnuPG is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20+
*
21+
* Note: This code is heavily based on the GNU MP Library.
22+
* Actually it's the same code with only minor changes in the
23+
* way the data is stored; this is to support the abstraction
24+
* of an optional secure memory allocation which may be used
25+
* to avoid revealing of sensitive data due to paging etc.
26+
* The GNU MP Library itself is published under the LGPL;
27+
* however I decided to publish this code under the plain GPL.
28+
*/
29+
30+
#include "mpi-internal.h"
31+
#include "longlong.h"
32+
33+
mpi_limb_t
34+
mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
35+
mpi_ptr_t s2_ptr, mpi_size_t size)
36+
{
37+
mpi_limb_t x, y, cy;
38+
mpi_size_t j;
39+
40+
/* The loop counter and index J goes from -SIZE to -1. This way
41+
the loop becomes faster. */
42+
j = -size;
43+
44+
/* Offset the base pointers to compensate for the negative indices. */
45+
s1_ptr -= j;
46+
s2_ptr -= j;
47+
res_ptr -= j;
48+
49+
cy = 0;
50+
do {
51+
y = s2_ptr[j];
52+
x = s1_ptr[j];
53+
y += cy; /* add previous carry to one addend */
54+
cy = y < cy; /* get out carry from that addition */
55+
y += x; /* add other addend */
56+
cy += y < x; /* get out carry from that add, combine */
57+
res_ptr[j] = y;
58+
} while (++j);
59+
60+
return cy;
61+
}

lib/mpi/generic_mpih-lshift.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* mpihelp-lshift.c - MPI helper functions
2+
* Copyright (C) 1994, 1996, 1998, 2001 Free Software Foundation, Inc.
3+
*
4+
* This file is part of GnuPG.
5+
*
6+
* GnuPG is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* GnuPG is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19+
*
20+
* Note: This code is heavily based on the GNU MP Library.
21+
* Actually it's the same code with only minor changes in the
22+
* way the data is stored; this is to support the abstraction
23+
* of an optional secure memory allocation which may be used
24+
* to avoid revealing of sensitive data due to paging etc.
25+
* The GNU MP Library itself is published under the LGPL;
26+
* however I decided to publish this code under the plain GPL.
27+
*/
28+
29+
#include "mpi-internal.h"
30+
31+
/* Shift U (pointed to by UP and USIZE digits long) CNT bits to the left
32+
* and store the USIZE least significant digits of the result at WP.
33+
* Return the bits shifted out from the most significant digit.
34+
*
35+
* Argument constraints:
36+
* 1. 0 < CNT < BITS_PER_MP_LIMB
37+
* 2. If the result is to be written over the input, WP must be >= UP.
38+
*/
39+
40+
mpi_limb_t
41+
mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, unsigned int cnt)
42+
{
43+
mpi_limb_t high_limb, low_limb;
44+
unsigned sh_1, sh_2;
45+
mpi_size_t i;
46+
mpi_limb_t retval;
47+
48+
sh_1 = cnt;
49+
wp += 1;
50+
sh_2 = BITS_PER_MPI_LIMB - sh_1;
51+
i = usize - 1;
52+
low_limb = up[i];
53+
retval = low_limb >> sh_2;
54+
high_limb = low_limb;
55+
while (--i >= 0) {
56+
low_limb = up[i];
57+
wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
58+
high_limb = low_limb;
59+
}
60+
wp[i] = high_limb << sh_1;
61+
62+
return retval;
63+
}

lib/mpi/generic_mpih-mul1.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* mpihelp-mul_1.c - MPI helper functions
2+
* Copyright (C) 1994, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
3+
*
4+
* This file is part of GnuPG.
5+
*
6+
* GnuPG is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* GnuPG is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19+
*
20+
* Note: This code is heavily based on the GNU MP Library.
21+
* Actually it's the same code with only minor changes in the
22+
* way the data is stored; this is to support the abstraction
23+
* of an optional secure memory allocation which may be used
24+
* to avoid revealing of sensitive data due to paging etc.
25+
* The GNU MP Library itself is published under the LGPL;
26+
* however I decided to publish this code under the plain GPL.
27+
*/
28+
29+
#include "mpi-internal.h"
30+
#include "longlong.h"
31+
32+
mpi_limb_t
33+
mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
34+
mpi_limb_t s2_limb)
35+
{
36+
mpi_limb_t cy_limb;
37+
mpi_size_t j;
38+
mpi_limb_t prod_high, prod_low;
39+
40+
/* The loop counter and index J goes from -S1_SIZE to -1. This way
41+
* the loop becomes faster. */
42+
j = -s1_size;
43+
44+
/* Offset the base pointers to compensate for the negative indices. */
45+
s1_ptr -= j;
46+
res_ptr -= j;
47+
48+
cy_limb = 0;
49+
do {
50+
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
51+
prod_low += cy_limb;
52+
cy_limb = (prod_low < cy_limb ? 1 : 0) + prod_high;
53+
res_ptr[j] = prod_low;
54+
} while (++j);
55+
56+
return cy_limb;
57+
}

lib/mpi/generic_mpih-mul2.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* mpihelp-mul_2.c - MPI helper functions
2+
* Copyright (C) 1994, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
3+
*
4+
* This file is part of GnuPG.
5+
*
6+
* GnuPG is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* GnuPG is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19+
*
20+
* Note: This code is heavily based on the GNU MP Library.
21+
* Actually it's the same code with only minor changes in the
22+
* way the data is stored; this is to support the abstraction
23+
* of an optional secure memory allocation which may be used
24+
* to avoid revealing of sensitive data due to paging etc.
25+
* The GNU MP Library itself is published under the LGPL;
26+
* however I decided to publish this code under the plain GPL.
27+
*/
28+
29+
#include "mpi-internal.h"
30+
#include "longlong.h"
31+
32+
mpi_limb_t
33+
mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
34+
mpi_size_t s1_size, mpi_limb_t s2_limb)
35+
{
36+
mpi_limb_t cy_limb;
37+
mpi_size_t j;
38+
mpi_limb_t prod_high, prod_low;
39+
mpi_limb_t x;
40+
41+
/* The loop counter and index J goes from -SIZE to -1. This way
42+
* the loop becomes faster. */
43+
j = -s1_size;
44+
res_ptr -= j;
45+
s1_ptr -= j;
46+
47+
cy_limb = 0;
48+
do {
49+
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
50+
51+
prod_low += cy_limb;
52+
cy_limb = (prod_low < cy_limb ? 1 : 0) + prod_high;
53+
54+
x = res_ptr[j];
55+
prod_low = x + prod_low;
56+
cy_limb += prod_low < x ? 1 : 0;
57+
res_ptr[j] = prod_low;
58+
} while (++j);
59+
return cy_limb;
60+
}

lib/mpi/generic_mpih-mul3.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* mpihelp-mul_3.c - MPI helper functions
2+
* Copyright (C) 1994, 1996, 1997, 1998, 2001 Free Software Foundation, Inc.
3+
*
4+
* This file is part of GnuPG.
5+
*
6+
* GnuPG is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* GnuPG is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19+
*
20+
* Note: This code is heavily based on the GNU MP Library.
21+
* Actually it's the same code with only minor changes in the
22+
* way the data is stored; this is to support the abstraction
23+
* of an optional secure memory allocation which may be used
24+
* to avoid revealing of sensitive data due to paging etc.
25+
* The GNU MP Library itself is published under the LGPL;
26+
* however I decided to publish this code under the plain GPL.
27+
*/
28+
29+
#include "mpi-internal.h"
30+
#include "longlong.h"
31+
32+
mpi_limb_t
33+
mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
34+
mpi_size_t s1_size, mpi_limb_t s2_limb)
35+
{
36+
mpi_limb_t cy_limb;
37+
mpi_size_t j;
38+
mpi_limb_t prod_high, prod_low;
39+
mpi_limb_t x;
40+
41+
/* The loop counter and index J goes from -SIZE to -1. This way
42+
* the loop becomes faster. */
43+
j = -s1_size;
44+
res_ptr -= j;
45+
s1_ptr -= j;
46+
47+
cy_limb = 0;
48+
do {
49+
umul_ppmm(prod_high, prod_low, s1_ptr[j], s2_limb);
50+
51+
prod_low += cy_limb;
52+
cy_limb = (prod_low < cy_limb ? 1 : 0) + prod_high;
53+
54+
x = res_ptr[j];
55+
prod_low = x - prod_low;
56+
cy_limb += prod_low > x ? 1 : 0;
57+
res_ptr[j] = prod_low;
58+
} while (++j);
59+
60+
return cy_limb;
61+
}

lib/mpi/generic_mpih-rshift.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* mpih-rshift.c - MPI helper functions
2+
* Copyright (C) 1994, 1996, 1998, 1999,
3+
* 2000, 2001 Free Software Foundation, Inc.
4+
*
5+
* This file is part of GNUPG
6+
*
7+
* GNUPG is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* GNUPG is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20+
*
21+
* Note: This code is heavily based on the GNU MP Library.
22+
* Actually it's the same code with only minor changes in the
23+
* way the data is stored; this is to support the abstraction
24+
* of an optional secure memory allocation which may be used
25+
* to avoid revealing of sensitive data due to paging etc.
26+
* The GNU MP Library itself is published under the LGPL;
27+
* however I decided to publish this code under the plain GPL.
28+
*/
29+
30+
#include "mpi-internal.h"
31+
32+
/* Shift U (pointed to by UP and USIZE limbs long) CNT bits to the right
33+
* and store the USIZE least significant limbs of the result at WP.
34+
* The bits shifted out to the right are returned.
35+
*
36+
* Argument constraints:
37+
* 1. 0 < CNT < BITS_PER_MP_LIMB
38+
* 2. If the result is to be written over the input, WP must be <= UP.
39+
*/
40+
41+
mpi_limb_t
42+
mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, unsigned cnt)
43+
{
44+
mpi_limb_t high_limb, low_limb;
45+
unsigned sh_1, sh_2;
46+
mpi_size_t i;
47+
mpi_limb_t retval;
48+
49+
sh_1 = cnt;
50+
wp -= 1;
51+
sh_2 = BITS_PER_MPI_LIMB - sh_1;
52+
high_limb = up[0];
53+
retval = high_limb << sh_2;
54+
low_limb = high_limb;
55+
for (i = 1; i < usize; i++) {
56+
high_limb = up[i];
57+
wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
58+
low_limb = high_limb;
59+
}
60+
wp[i] = low_limb >> sh_1;
61+
62+
return retval;
63+
}

0 commit comments

Comments
 (0)