1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* mpi.h - Multi Precision Integers
3 * Copyright (C) 1994, 1996, 1998, 1999,
4 * 2000, 2001 Free Software Foundation, Inc.
6 * This file is part of GNUPG.
8 * Note: This code is heavily based on the GNU MP Library.
9 * Actually it's the same code with only minor changes in the
10 * way the data is stored; this is to support the abstraction
11 * of an optional secure memory allocation which may be used
12 * to avoid revealing of sensitive data due to paging etc.
13 * The GNU MP Library itself is published under the LGPL;
14 * however I decided to publish this code under the plain GPL.
20 #include <linux/types.h>
21 #include <linux/scatterlist.h>
23 #define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8)
24 #define BITS_PER_MPI_LIMB BITS_PER_LONG
26 typedef unsigned long int mpi_limb_t
;
27 typedef signed long int mpi_limb_signed_t
;
30 int alloced
; /* array size (# of allocated limbs) */
31 int nlimbs
; /* number of valid limbs */
32 int nbits
; /* the real number of valid bits (info only) */
33 int sign
; /* indicates a negative number */
34 unsigned flags
; /* bit 0: array must be allocated in secure memory space */
36 /* bit 2: the limb is a pointer to some m_alloced data */
37 mpi_limb_t
*d
; /* array with the limbs */
40 typedef struct gcry_mpi
*MPI
;
42 #define mpi_get_nlimbs(a) ((a)->nlimbs)
45 MPI
mpi_alloc(unsigned nlimbs
);
47 int mpi_resize(MPI a
, unsigned nlimbs
);
52 MPI
mpi_read_raw_data(const void *xbuffer
, size_t nbytes
);
53 MPI
mpi_read_from_buffer(const void *buffer
, unsigned *ret_nread
);
54 MPI
mpi_read_raw_from_sgl(struct scatterlist
*sgl
, unsigned int len
);
55 void *mpi_get_buffer(MPI a
, unsigned *nbytes
, int *sign
);
56 int mpi_read_buffer(MPI a
, uint8_t *buf
, unsigned buf_len
, unsigned *nbytes
,
58 int mpi_write_to_sgl(MPI a
, struct scatterlist
*sg
, unsigned nbytes
,
62 int mpi_mod(MPI rem
, MPI dividend
, MPI divisor
);
65 int mpi_powm(MPI res
, MPI base
, MPI exp
, MPI mod
);
68 int mpi_cmp_ui(MPI u
, ulong v
);
69 int mpi_cmp(MPI u
, MPI v
);
71 /*-- mpi-sub-ui.c --*/
72 int mpi_sub_ui(MPI w
, MPI u
, unsigned long vval
);
75 void mpi_normalize(MPI a
);
76 unsigned mpi_get_nbits(MPI a
);
77 int mpi_test_bit(MPI a
, unsigned int n
);
78 int mpi_set_bit(MPI a
, unsigned int n
);
79 int mpi_rshift(MPI x
, MPI a
, unsigned int n
);
82 int mpi_add(MPI w
, MPI u
, MPI v
);
83 int mpi_sub(MPI w
, MPI u
, MPI v
);
84 int mpi_addm(MPI w
, MPI u
, MPI v
, MPI m
);
85 int mpi_subm(MPI w
, MPI u
, MPI v
, MPI m
);
88 int mpi_mul(MPI w
, MPI u
, MPI v
);
89 int mpi_mulm(MPI w
, MPI u
, MPI v
, MPI m
);
92 int mpi_tdiv_r(MPI rem
, MPI num
, MPI den
);
93 int mpi_fdiv_r(MPI rem
, MPI dividend
, MPI divisor
);
95 /* inline functions */
98 * mpi_get_size() - returns max size required to store the number
100 * @a: A multi precision integer for which we want to allocate a buffer
102 * Return: size required to store the number
104 static inline unsigned int mpi_get_size(MPI a
)
106 return a
->nlimbs
* BYTES_PER_MPI_LIMB
;
108 #endif /*G10_MPI_H */