gcry_mpi_t libgcrypt-1.8.2 中的类型定义

gcry_mpi_t type definition in libgcrypt-1.8.2

我正在寻找 gcry_mpi_t 类型的定义。我正在研究 GnuPG 的代码,它使用 libgcrypt,后者又使用后者作为负责存储模数、RSA 密钥的素数等的类型。通常,在 /libgcrypt-1.8.2/cipher/rsa.c 中你可以找到:

typedef struct
    {
      gcry_mpi_t n;     /* modulus */
      gcry_mpi_t e;     /* exponent */
    } RSA_public_key;


typedef struct
    {
      gcry_mpi_t n;     /* public modulus */
      gcry_mpi_t e;     /* public exponent */
      gcry_mpi_t d;     /* exponent */
      gcry_mpi_t p;     /* prime  p. */
      gcry_mpi_t q;     /* prime  q. */
      gcry_mpi_t u;     /* inverse of p mod q. */
    } RSA_secret_key;

我发现 this SO post 提到了我要定义的特定类型,但没有说明它是如何定义的。

我的目标是使用基本 CS 中的定义 class 介绍 RSA 及其实现方式。因此,我希望展示如何通过专门设计的 struct 来处理特定的 RSA 变量,以实现高效的内存管理。

但是,直到现在,我都找不到在 libgcrypt 代码中定义它的正确代码段。谢谢!

src/gcrypt.h.in(用于生成 <gcrypt.h> header:

/* The data objects used to hold multi precision integers.  */
struct gcry_mpi;
typedef struct gcry_mpi *gcry_mpi_t;

所以公开 gcry_mpi_t 被定义为指向不完整结构的指针,允许实现保密。如果您只是查看已安装的 headers,您将找不到完整的定义。但是,对于内部使用,src/mpi.hstruct gcry_mpi 定义为:

struct gcry_mpi
{
   int alloced;         /* Array size (# of allocated limbs). */
   int nlimbs;          /* Number of valid limbs. */
   int sign;            /* Indicates a negative number and is also used
                           for opaque MPIs to store the length.  */
   unsigned int flags; /* Bit 0: Array to be allocated in secure memory space.*/
                       /* Bit 2: The limb is a pointer to some m_alloced data.*/
                       /* Bit 4: Immutable MPI - the MPI may not be modified.  */
                       /* Bit 5: Constant MPI - the MPI will not be freed.  */
   mpi_limb_t *d;      /* Array with the limbs */
};