(rsa_init_public_key): New function.
[gd/nettle] / rsa.h
1 /* rsa.h
2  *
3  * The RSA publickey algorithm.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25  
26 #ifndef NETTLE_RSA_H_INCLUDED
27 #define NETTLE_RSA_H_INCLUDED
28
29 #include <inttypes.h>
30 #include <gmp.h>
31
32 #include "md5.h"
33 #include "sha.h"
34
35 struct rsa_public_key
36 {
37   /* Size of the modulo, in octets. This is also the size of all
38    * signatures that are created or verified with this key. */
39   unsigned size;
40   
41   /* Modulo */
42   mpz_t n;
43
44   /* Public exponent */
45   mpz_t e;
46 };
47
48 struct rsa_private_key
49 {
50   struct rsa_public_key pub;
51   
52   /* Secret exponent */
53   mpz_t d;
54
55   /* The two factors */
56   mpz_t p; mpz_t q;
57
58   /* d % (p-1), i.e. a e = 1 (mod (p-1)) */
59   mpz_t a;
60
61   /* d % (q-1), i.e. b e = 1 (mod (q-1)) */
62   mpz_t b;
63
64   /* modular inverse of q , i.e. c q = 1 (mod p) */
65   mpz_t c;
66 };
67
68 /* Signing a message works as follows:
69  *
70  * Store the private key in a rsa_private_key struct.
71  *
72  * Call rsa_prepare_private_key. This initializes the size attribute
73  * to the length of a signature.
74  *
75  * Initialize a hashing context, by callling
76  *   md5_init
77  *
78  * Hash the message by calling
79  *   md5_update
80  *
81  * Create the signature by calling
82  *   rsa_md5_sign
83  *
84  * The signature is represented as a mpz_t bignum. This call also
85  * resets the hashing context.
86  *
87  * When done with the key and signature, don't forget to call
88  * mpz_clear.
89  */
90
91 /* Calls mpz_init to initialize bignum storage. */
92 void
93 rsa_init_public_key(struct rsa_public_key *key);
94
95 /* Calls mpz_clear to deallocate bignum storage. */
96 void
97 rsa_clear_public_key(struct rsa_public_key *key);
98
99 int
100 rsa_prepare_public_key(struct rsa_public_key *key);
101
102 /* Calls mpz_init to initialize bignum storage. */
103 void
104 rsa_init_private_key(struct rsa_private_key *key);
105
106 /* Calls mpz_clear to deallocate bignum storage. */
107 void
108 rsa_clear_private_key(struct rsa_private_key *key);
109
110 int
111 rsa_prepare_private_key(struct rsa_private_key *key);
112
113
114 /* PKCS#1 style signatures */
115 void
116 rsa_md5_sign(struct rsa_private_key *key,
117              struct md5_ctx *hash,
118              mpz_t signature);
119
120
121 int
122 rsa_md5_verify(struct rsa_public_key *key,
123                struct md5_ctx *hash,
124                const mpz_t signature);
125
126 void
127 rsa_sha1_sign(struct rsa_private_key *key,
128               struct sha1_ctx *hash,
129               mpz_t signature);
130
131 int
132 rsa_sha1_verify(struct rsa_public_key *key,
133                 struct sha1_ctx *hash,
134                 const mpz_t signature);
135
136 /* Compute x, the d:th root of m. Calling it with x == m is allowed. */
137 void
138 rsa_compute_root(struct rsa_private_key *key, mpz_t x, const mpz_t m);
139
140
141 #endif /* NETTLE_RSA_H_INCLUDED */