1*ebfedea0SLionel Sambuc /*- 2*ebfedea0SLionel Sambuc * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> 3*ebfedea0SLionel Sambuc * All rights reserved. 4*ebfedea0SLionel Sambuc * 5*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 6*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 7*ebfedea0SLionel Sambuc * are met: 8*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 9*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 10*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 11*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 12*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution. 13*ebfedea0SLionel Sambuc * 14*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16*ebfedea0SLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17*ebfedea0SLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18*ebfedea0SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19*ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20*ebfedea0SLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21*ebfedea0SLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22*ebfedea0SLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23*ebfedea0SLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24*ebfedea0SLionel Sambuc */ 25*ebfedea0SLionel Sambuc #ifndef RSA_H_ 26*ebfedea0SLionel Sambuc #define RSA_H_ 20120325 27*ebfedea0SLionel Sambuc 28*ebfedea0SLionel Sambuc #include "bn.h" 29*ebfedea0SLionel Sambuc 30*ebfedea0SLionel Sambuc #ifndef __BEGIN_DECLS 31*ebfedea0SLionel Sambuc # if defined(__cplusplus) 32*ebfedea0SLionel Sambuc # define __BEGIN_DECLS extern "C" { 33*ebfedea0SLionel Sambuc # define __END_DECLS } 34*ebfedea0SLionel Sambuc # else 35*ebfedea0SLionel Sambuc # define __BEGIN_DECLS 36*ebfedea0SLionel Sambuc # define __END_DECLS 37*ebfedea0SLionel Sambuc # endif 38*ebfedea0SLionel Sambuc #endif 39*ebfedea0SLionel Sambuc 40*ebfedea0SLionel Sambuc __BEGIN_DECLS 41*ebfedea0SLionel Sambuc 42*ebfedea0SLionel Sambuc typedef struct rsa_pubkey_t { 43*ebfedea0SLionel Sambuc BIGNUM *n; /* RSA public modulus n */ 44*ebfedea0SLionel Sambuc BIGNUM *e; /* RSA public encryption exponent e */ 45*ebfedea0SLionel Sambuc } rsa_pubkey_t; 46*ebfedea0SLionel Sambuc 47*ebfedea0SLionel Sambuc typedef struct mpi_rsa_t { 48*ebfedea0SLionel Sambuc int f1; /* openssl pad */ 49*ebfedea0SLionel Sambuc long f2; /* openssl version */ 50*ebfedea0SLionel Sambuc const void *f3; /* openssl method */ 51*ebfedea0SLionel Sambuc void *f4; /* openssl engine */ 52*ebfedea0SLionel Sambuc BIGNUM *n; 53*ebfedea0SLionel Sambuc BIGNUM *e; 54*ebfedea0SLionel Sambuc BIGNUM *d; 55*ebfedea0SLionel Sambuc BIGNUM *p; 56*ebfedea0SLionel Sambuc BIGNUM *q; 57*ebfedea0SLionel Sambuc BIGNUM *dmp1; 58*ebfedea0SLionel Sambuc BIGNUM *dmq1; 59*ebfedea0SLionel Sambuc BIGNUM *iqmp; 60*ebfedea0SLionel Sambuc } mpi_rsa_t; 61*ebfedea0SLionel Sambuc 62*ebfedea0SLionel Sambuc #define RSA mpi_rsa_t 63*ebfedea0SLionel Sambuc 64*ebfedea0SLionel Sambuc typedef struct dsa_pubkey_t { 65*ebfedea0SLionel Sambuc BIGNUM *p; /* DSA public modulus n */ 66*ebfedea0SLionel Sambuc BIGNUM *q; /* DSA public encryption exponent e */ 67*ebfedea0SLionel Sambuc BIGNUM *g; 68*ebfedea0SLionel Sambuc BIGNUM *y; 69*ebfedea0SLionel Sambuc } dsa_pubkey_t; 70*ebfedea0SLionel Sambuc 71*ebfedea0SLionel Sambuc typedef struct mpi_dsa_t { 72*ebfedea0SLionel Sambuc BIGNUM *p; 73*ebfedea0SLionel Sambuc BIGNUM *q; 74*ebfedea0SLionel Sambuc BIGNUM *g; 75*ebfedea0SLionel Sambuc BIGNUM *y; 76*ebfedea0SLionel Sambuc BIGNUM *x; 77*ebfedea0SLionel Sambuc BIGNUM *pub_key; 78*ebfedea0SLionel Sambuc BIGNUM *priv_key; 79*ebfedea0SLionel Sambuc } mpi_dsa_t; 80*ebfedea0SLionel Sambuc 81*ebfedea0SLionel Sambuc #define DSA mpi_dsa_t 82*ebfedea0SLionel Sambuc 83*ebfedea0SLionel Sambuc typedef struct rsasig_t { 84*ebfedea0SLionel Sambuc BIGNUM *sig; /* mpi which is actual signature */ 85*ebfedea0SLionel Sambuc } rsasig_t; 86*ebfedea0SLionel Sambuc 87*ebfedea0SLionel Sambuc typedef struct dsasig_t { 88*ebfedea0SLionel Sambuc BIGNUM *r; /* mpi which is actual signature */ 89*ebfedea0SLionel Sambuc BIGNUM *s; /* mpi which is actual signature */ 90*ebfedea0SLionel Sambuc } dsasig_t; 91*ebfedea0SLionel Sambuc 92*ebfedea0SLionel Sambuc #define DSA_SIG dsasig_t 93*ebfedea0SLionel Sambuc 94*ebfedea0SLionel Sambuc /* misc defs */ 95*ebfedea0SLionel Sambuc #define RSA_NO_PADDING 3 96*ebfedea0SLionel Sambuc 97*ebfedea0SLionel Sambuc #define SIGNETBSD_ID_SIZE 8 98*ebfedea0SLionel Sambuc #define SIGNETBSD_NAME_SIZE 128 99*ebfedea0SLionel Sambuc 100*ebfedea0SLionel Sambuc #define RSA_PUBKEY_ALG 1 101*ebfedea0SLionel Sambuc #define DSA_PUBKEY_ALG 17 102*ebfedea0SLionel Sambuc 103*ebfedea0SLionel Sambuc /* the public part of the key */ 104*ebfedea0SLionel Sambuc typedef struct pubkey_t { 105*ebfedea0SLionel Sambuc uint32_t version; /* key version - usually 4 */ 106*ebfedea0SLionel Sambuc uint8_t id[SIGNETBSD_ID_SIZE]; /* binary id */ 107*ebfedea0SLionel Sambuc char name[SIGNETBSD_NAME_SIZE]; /* name of identity - not necessary, but looks better */ 108*ebfedea0SLionel Sambuc int64_t birthtime; /* time of creation of key */ 109*ebfedea0SLionel Sambuc int64_t expiry; /* expiration time of the key */ 110*ebfedea0SLionel Sambuc uint32_t validity; /* validity in days */ 111*ebfedea0SLionel Sambuc uint32_t alg; /* pubkey algorithm - rsa/dss etc */ 112*ebfedea0SLionel Sambuc rsa_pubkey_t rsa; /* specific RSA keys */ 113*ebfedea0SLionel Sambuc dsa_pubkey_t dsa; /* specific DSA keys */ 114*ebfedea0SLionel Sambuc } pubkey_t; 115*ebfedea0SLionel Sambuc 116*ebfedea0SLionel Sambuc /* signature details (for a specific file) */ 117*ebfedea0SLionel Sambuc typedef struct signature_t { 118*ebfedea0SLionel Sambuc uint32_t version; /* signature version number */ 119*ebfedea0SLionel Sambuc uint32_t type; /* signature type value */ 120*ebfedea0SLionel Sambuc int64_t birthtime; /* creation time of the signature */ 121*ebfedea0SLionel Sambuc int64_t expiry; /* expiration time of the signature */ 122*ebfedea0SLionel Sambuc uint8_t id[SIGNETBSD_ID_SIZE]; /* binary id */ 123*ebfedea0SLionel Sambuc uint32_t key_alg; /* public key algorithm number */ 124*ebfedea0SLionel Sambuc uint32_t hash_alg; /* hashing algorithm number */ 125*ebfedea0SLionel Sambuc rsasig_t rsa; /* RSA signature */ 126*ebfedea0SLionel Sambuc dsasig_t dsa; /* DSA signature */ 127*ebfedea0SLionel Sambuc size_t v4_hashlen; /* length of hashed info */ 128*ebfedea0SLionel Sambuc uint8_t *v4_hashed; /* hashed info */ 129*ebfedea0SLionel Sambuc uint8_t hash2[2]; /* high 2 bytes of hashed value - for quick test */ 130*ebfedea0SLionel Sambuc pubkey_t *signer; /* pubkey of signer */ 131*ebfedea0SLionel Sambuc } signature_t; 132*ebfedea0SLionel Sambuc 133*ebfedea0SLionel Sambuc unsigned dsa_verify(const signature_t */*sig*/, const dsa_pubkey_t */*pubdsa*/, const uint8_t */*calc*/, size_t /*hashlen*/); 134*ebfedea0SLionel Sambuc 135*ebfedea0SLionel Sambuc RSA *RSA_new(void); 136*ebfedea0SLionel Sambuc int RSA_size(const RSA */*rsa*/); 137*ebfedea0SLionel Sambuc void RSA_free(RSA */*rsa*/); 138*ebfedea0SLionel Sambuc int RSA_check_key(RSA */*rsa*/); 139*ebfedea0SLionel Sambuc RSA *RSA_generate_key(int /*num*/, unsigned long /*e*/, void (*callback)(int,int,void *), void */*cb_arg*/); 140*ebfedea0SLionel Sambuc int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); 141*ebfedea0SLionel Sambuc int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); 142*ebfedea0SLionel Sambuc int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); 143*ebfedea0SLionel Sambuc int RSA_public_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa, int padding); 144*ebfedea0SLionel Sambuc 145*ebfedea0SLionel Sambuc DSA *DSA_new(void); 146*ebfedea0SLionel Sambuc int DSA_size(const DSA */*rsa*/); 147*ebfedea0SLionel Sambuc void DSA_free(DSA */*dsa*/); 148*ebfedea0SLionel Sambuc DSA_SIG *DSA_SIG_new(void); 149*ebfedea0SLionel Sambuc void DSA_SIG_free(DSA_SIG */*sig*/); 150*ebfedea0SLionel Sambuc int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa); 151*ebfedea0SLionel Sambuc DSA_SIG *DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); 152*ebfedea0SLionel Sambuc 153*ebfedea0SLionel Sambuc __END_DECLS 154*ebfedea0SLionel Sambuc 155*ebfedea0SLionel Sambuc #endif 156