1 /*- 2 * Copyright (c) 2018 Ribose Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef RNP_MPI_H_ 28 #define RNP_MPI_H_ 29 30 #include <stdint.h> 31 #include <stdbool.h> 32 #include <ctype.h> 33 #include "bn.h" 34 35 /* 16384 bits should be pretty enough for now */ 36 #define PGP_MPINT_BITS (16384) 37 #define PGP_MPINT_SIZE (PGP_MPINT_BITS >> 3) 38 39 typedef struct pgp_hash_t pgp_hash_t; 40 41 /** multi-precision integer, used in signatures and public/secret keys */ 42 typedef struct pgp_mpi_t { 43 uint8_t mpi[PGP_MPINT_SIZE]; 44 size_t len; 45 } pgp_mpi_t; 46 47 bignum_t *mpi2bn(const pgp_mpi_t *val); 48 49 bool bn2mpi(bignum_t *bn, pgp_mpi_t *val); 50 51 bool mem2mpi(pgp_mpi_t *val, const void *mem, size_t len); 52 53 void mpi2mem(const pgp_mpi_t *val, void *mem); 54 55 char *mpi2hex(const pgp_mpi_t *val); 56 57 size_t mpi_bits(const pgp_mpi_t *val); 58 59 size_t mpi_bytes(const pgp_mpi_t *val); 60 61 bool mpi_hash(const pgp_mpi_t *val, pgp_hash_t *hash); 62 63 bool mpi_equal(const pgp_mpi_t *val1, const pgp_mpi_t *val2); 64 65 void mpi_forget(pgp_mpi_t *val); 66 67 #endif // MPI_H_ 68