2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #define RSA_H_ 20120325
31 # if defined(__cplusplus)
32 # define __BEGIN_DECLS extern "C" {
33 # define __END_DECLS }
35 # define __BEGIN_DECLS
42 typedef struct rsa_pubkey_t
{
43 BIGNUM
*n
; /* RSA public modulus n */
44 BIGNUM
*e
; /* RSA public encryption exponent e */
47 typedef struct mpi_rsa_t
{
48 int f1
; /* openssl pad */
49 long f2
; /* openssl version */
50 const void *f3
; /* openssl method */
51 void *f4
; /* openssl engine */
64 typedef struct dsa_pubkey_t
{
65 BIGNUM
*p
; /* DSA public modulus n */
66 BIGNUM
*q
; /* DSA public encryption exponent e */
71 typedef struct mpi_dsa_t
{
83 typedef struct rsasig_t
{
84 BIGNUM
*sig
; /* mpi which is actual signature */
87 typedef struct dsasig_t
{
88 BIGNUM
*r
; /* mpi which is actual signature */
89 BIGNUM
*s
; /* mpi which is actual signature */
92 #define DSA_SIG dsasig_t
95 #define RSA_NO_PADDING 3
97 #define SIGNETBSD_ID_SIZE 8
98 #define SIGNETBSD_NAME_SIZE 128
100 #define RSA_PUBKEY_ALG 1
101 #define DSA_PUBKEY_ALG 17
103 /* the public part of the key */
104 typedef 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 rsa_pubkey_t rsa
; /* specific RSA keys */
113 dsa_pubkey_t dsa
; /* specific DSA keys */
116 /* signature details (for a specific file) */
117 typedef 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 rsasig_t rsa
; /* RSA signature */
126 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 */
133 unsigned dsa_verify(const signature_t */
*sig*/
, const dsa_pubkey_t */
*pubdsa*/
, const uint8_t */
*calc*/
, size_t /*hashlen*/);
136 int RSA_size(const RSA */
*rsa*/
);
137 void RSA_free(RSA */
*rsa*/
);
138 int RSA_check_key(RSA */
*rsa*/
);
139 RSA
*RSA_generate_key(int /*num*/, unsigned long /*e*/, void (*callback
)(int,int,void *), void */
*cb_arg*/
);
140 int RSA_public_encrypt(int flen
, const unsigned char *from
, unsigned char *to
, RSA
*rsa
, int padding
);
141 int RSA_private_decrypt(int flen
, const unsigned char *from
, unsigned char *to
, RSA
*rsa
, int padding
);
142 int RSA_private_encrypt(int flen
, const unsigned char *from
, unsigned char *to
, RSA
*rsa
, int padding
);
143 int RSA_public_decrypt(int flen
, const uint8_t *from
, uint8_t *to
, RSA
*rsa
, int padding
);
146 int DSA_size(const DSA */
*rsa*/
);
147 void DSA_free(DSA */
*dsa*/
);
148 DSA_SIG
*DSA_SIG_new(void);
149 void DSA_SIG_free(DSA_SIG */
*sig*/
);
150 int DSA_do_verify(const unsigned char *dgst
, int dgst_len
, DSA_SIG
*sig
, DSA
*dsa
);
151 DSA_SIG
*DSA_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
);