1/*- 2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25#ifndef RSA_H_ 26#define RSA_H_ 20120325 27 28#include "bn.h" 29 30#ifndef __BEGIN_DECLS 31# if defined(__cplusplus) 32# define __BEGIN_DECLS extern "C" { 33# define __END_DECLS } 34# else 35# define __BEGIN_DECLS 36# define __END_DECLS 37# endif 38#endif 39 40__BEGIN_DECLS 41 42typedef struct netpgpv_rsa_pubkey_t { 43 PGPV_BIGNUM *n; /* RSA public modulus n */ 44 PGPV_BIGNUM *e; /* RSA public encryption exponent e */ 45} netpgpv_rsa_pubkey_t; 46 47typedef struct netpgpv_mpi_rsa_t { 48 int f1; /* openssl pad */ 49 long f2; /* openssl version */ 50 const void *f3; /* openssl method */ 51 void *f4; /* openssl engine */ 52 PGPV_BIGNUM *n; 53 PGPV_BIGNUM *e; 54 PGPV_BIGNUM *d; 55 PGPV_BIGNUM *p; 56 PGPV_BIGNUM *q; 57 PGPV_BIGNUM *dmp1; 58 PGPV_BIGNUM *dmq1; 59 PGPV_BIGNUM *iqmp; 60} netpgpv_mpi_rsa_t; 61 62#define NETPGPV_RSA netpgpv_mpi_rsa_t 63 64typedef struct netpgpv_dsa_pubkey_t { 65 PGPV_BIGNUM *p; /* DSA public modulus n */ 66 PGPV_BIGNUM *q; /* DSA public encryption exponent e */ 67 PGPV_BIGNUM *g; 68 PGPV_BIGNUM *y; 69} netpgpv_dsa_pubkey_t; 70 71typedef struct netpgpv_mpi_dsa_t { 72 PGPV_BIGNUM *p; 73 PGPV_BIGNUM *q; 74 PGPV_BIGNUM *g; 75 PGPV_BIGNUM *y; 76 PGPV_BIGNUM *x; 77 PGPV_BIGNUM *pub_key; 78 PGPV_BIGNUM *priv_key; 79} netpgpv_mpi_dsa_t; 80 81#define NETPGPV_DSA netpgpv_mpi_dsa_t 82 83typedef struct netpgpv_rsasig_t { 84 PGPV_BIGNUM *sig; /* mpi which is actual signature */ 85} netpgpv_rsasig_t; 86 87typedef struct netpgpv_dsasig_t { 88 PGPV_BIGNUM *r; /* mpi which is actual signature */ 89 PGPV_BIGNUM *s; /* mpi which is actual signature */ 90} netpgpv_dsasig_t; 91 92#define NETPGPV_DSA_SIG netpgpv_dsasig_t 93 94/* misc defs */ 95#define NETPGPV_RSA_NO_PADDING 3 96 97#define SIGNETBSD_ID_SIZE 8 98#define SIGNETBSD_NAME_SIZE 128 99 100#define NETPGPV_RSA_PUBKEY_ALG 1 101#define NETPGPV_DSA_PUBKEY_ALG 17 102 103/* the public part of the key */ 104typedef struct pubkey_t { 105 uint32_t version; /* key version - usually 4 */ 106 uint8_t id[SIGNETBSD_ID_SIZE]; /* binary id */ 107 char name[SIGNETBSD_NAME_SIZE]; /* name of identity - not necessary, but looks better */ 108 int64_t birthtime; /* time of creation of key */ 109 int64_t expiry; /* expiration time of the key */ 110 uint32_t validity; /* validity in days */ 111 uint32_t alg; /* pubkey algorithm - rsa/dss etc */ 112 netpgpv_rsa_pubkey_t rsa; /* specific RSA keys */ 113 netpgpv_dsa_pubkey_t dsa; /* specific DSA keys */ 114} pubkey_t; 115 116/* signature details (for a specific file) */ 117typedef struct signature_t { 118 uint32_t version; /* signature version number */ 119 uint32_t type; /* signature type value */ 120 int64_t birthtime; /* creation time of the signature */ 121 int64_t expiry; /* expiration time of the signature */ 122 uint8_t id[SIGNETBSD_ID_SIZE]; /* binary id */ 123 uint32_t key_alg; /* public key algorithm number */ 124 uint32_t hash_alg; /* hashing algorithm number */ 125 netpgpv_rsasig_t rsa; /* RSA signature */ 126 netpgpv_dsasig_t dsa; /* DSA signature */ 127 size_t v4_hashlen; /* length of hashed info */ 128 uint8_t *v4_hashed; /* hashed info */ 129 uint8_t hash2[2]; /* high 2 bytes of hashed value - for quick test */ 130 pubkey_t *signer; /* pubkey of signer */ 131} signature_t; 132 133unsigned netpgpv_dsa_verify(const signature_t */*sig*/, 134 const netpgpv_dsa_pubkey_t */*pubdsa*/, const uint8_t */*calc*/, 135 size_t /*hashlen*/); 136 137NETPGPV_RSA *netpgpv_RSA_new(void); 138int netpgpv_RSA_size(const NETPGPV_RSA */*rsa*/); 139void netpgpv_RSA_free(NETPGPV_RSA */*rsa*/); 140int netpgpv_RSA_check_key(NETPGPV_RSA */*rsa*/); 141NETPGPV_RSA *netpgpv_RSA_generate_key(int /*num*/, unsigned long /*e*/, 142 void (*callback)(int,int,void *), void */*cb_arg*/); 143int netpgpv_RSA_public_encrypt(int /*flen*/, const unsigned char */*from*/, 144 unsigned char */*to*/, NETPGPV_RSA */*rsa*/, int /*padding*/); 145int netpgpv_RSA_private_decrypt(int /*flen*/, const unsigned char */*from*/, 146 unsigned char */*to*/, NETPGPV_RSA */*rsa*/, int /*padding*/); 147int netpgpv_RSA_private_encrypt(int /*flen*/, const unsigned char */*from*/, 148 unsigned char */*to*/, NETPGPV_RSA */*rsa*/, int /*padding*/); 149int netpgpv_RSA_public_decrypt(int /*flen*/, const uint8_t */*from*/, 150 uint8_t */*to*/, NETPGPV_RSA */*rsa*/, int /*padding*/); 151 152NETPGPV_DSA *netpgpv_DSA_new(void); 153int netpgpv_DSA_size(const NETPGPV_DSA */*rsa*/); 154void netpgpv_DSA_free(NETPGPV_DSA */*dsa*/); 155NETPGPV_DSA_SIG *netpgpv_DSA_SIG_new(void); 156void netpgpv_DSA_SIG_free(NETPGPV_DSA_SIG */*sig*/); 157int netpgpv_DSA_do_verify(const unsigned char */*dgst*/, int /*dgst_len*/, 158 NETPGPV_DSA_SIG */*sig*/, NETPGPV_DSA */*dsa*/); 159NETPGPV_DSA_SIG *netpgpv_DSA_do_sign(const unsigned char */*dgst*/, 160 int /*dlen*/, NETPGPV_DSA */*dsa*/); 161 162__END_DECLS 163 164#endif 165