~ubuntu-branches/ubuntu/saucy/nettle/saucy-proposed

« back to all changes in this revision

Viewing changes to dsa.h

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* dsa.h
 
2
 *
 
3
 * The DSA publickey algorithm.
 
4
 */
 
5
 
 
6
/* nettle, low-level cryptographics library
 
7
 *
 
8
 * Copyright (C) 2002 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_DSA_H_INCLUDED
 
27
#define NETTLE_DSA_H_INCLUDED
 
28
 
 
29
#include <gmp.h>
 
30
 
 
31
#include "nettle-types.h"
 
32
 
 
33
#include "sha.h"
 
34
 
 
35
/* For nettle_random_func */
 
36
#include "nettle-meta.h"
 
37
 
 
38
/* Name mangling */
 
39
#define dsa_public_key_init nettle_dsa_public_key_init
 
40
#define dsa_public_key_clear nettle_dsa_public_key_clear
 
41
#define dsa_private_key_init nettle_dsa_private_key_init
 
42
#define dsa_private_key_clear nettle_dsa_private_key_clear
 
43
#define dsa_signature_init nettle_dsa_signature_init
 
44
#define dsa_signature_clear nettle_dsa_signature_clear
 
45
#define dsa_sign nettle_dsa_sign
 
46
#define dsa_verify nettle_dsa_verify
 
47
#define dsa_sign_digest nettle_dsa_sign_digest
 
48
#define dsa_verify_digest nettle_dsa_verify_digest
 
49
#define dsa_generate_keypair nettle_dsa_generate_keypair
 
50
#define dsa_signature_from_sexp nettle_dsa_signature_from_sexp
 
51
#define dsa_keypair_from_sexp_alist nettle_dsa_keypair_from_sexp_alist
 
52
#define dsa_keypair_from_sexp nettle_dsa_keypair_from_sexp
 
53
 
 
54
#define DSA_MIN_P_BITS 512
 
55
#define DSA_Q_OCTETS 20
 
56
#define DSA_Q_BITS 160
 
57
 
 
58
struct dsa_public_key
 
59
{  
 
60
  /* Modulo */
 
61
  mpz_t p;
 
62
 
 
63
  /* Group order */
 
64
  mpz_t q;
 
65
 
 
66
  /* Generator */
 
67
  mpz_t g;
 
68
  
 
69
  /* Public value */
 
70
  mpz_t y;
 
71
};
 
72
 
 
73
struct dsa_private_key
 
74
{
 
75
  /* Unlike an rsa public key, private key operations will need both
 
76
   * the private and the public information. */
 
77
  mpz_t x;
 
78
};
 
79
 
 
80
struct dsa_signature
 
81
{
 
82
  mpz_t r;
 
83
  mpz_t s;
 
84
};
 
85
 
 
86
/* Signing a message works as follows:
 
87
 *
 
88
 * Store the private key in a dsa_private_key struct.
 
89
 *
 
90
 * Initialize a hashing context, by callling
 
91
 *   sha1_init
 
92
 *
 
93
 * Hash the message by calling
 
94
 *   sha1_update
 
95
 *
 
96
 * Create the signature by calling
 
97
 *   dsa_sign
 
98
 *
 
99
 * The signature is represented as a struct dsa_signature. This call also
 
100
 * resets the hashing context.
 
101
 *
 
102
 * When done with the key and signature, don't forget to call
 
103
 * dsa_signature_clear.
 
104
 */
 
105
 
 
106
/* Calls mpz_init to initialize bignum storage. */
 
107
void
 
108
dsa_public_key_init(struct dsa_public_key *key);
 
109
 
 
110
/* Calls mpz_clear to deallocate bignum storage. */
 
111
void
 
112
dsa_public_key_clear(struct dsa_public_key *key);
 
113
 
 
114
 
 
115
/* Calls mpz_init to initialize bignum storage. */
 
116
void
 
117
dsa_private_key_init(struct dsa_private_key *key);
 
118
 
 
119
/* Calls mpz_clear to deallocate bignum storage. */
 
120
void
 
121
dsa_private_key_clear(struct dsa_private_key *key);
 
122
 
 
123
/* Calls mpz_init to initialize bignum storage. */
 
124
void
 
125
dsa_signature_init(struct dsa_signature *signature);
 
126
 
 
127
/* Calls mpz_clear to deallocate bignum storage. */
 
128
void
 
129
dsa_signature_clear(struct dsa_signature *signature);
 
130
 
 
131
 
 
132
void
 
133
dsa_sign(const struct dsa_public_key *pub,
 
134
         const struct dsa_private_key *key,
 
135
         void *random_ctx, nettle_random_func random,
 
136
         struct sha1_ctx *hash,
 
137
         struct dsa_signature *signature);
 
138
 
 
139
 
 
140
int
 
141
dsa_verify(const struct dsa_public_key *key,
 
142
           struct sha1_ctx *hash,
 
143
           const struct dsa_signature *signature);
 
144
 
 
145
void
 
146
dsa_sign_digest(const struct dsa_public_key *pub,
 
147
                const struct dsa_private_key *key,
 
148
                void *random_ctx, nettle_random_func random,
 
149
                const uint8_t *digest,
 
150
                struct dsa_signature *signature);
 
151
 
 
152
int
 
153
dsa_verify_digest(const struct dsa_public_key *key,
 
154
                  const uint8_t *digest,
 
155
                  const struct dsa_signature *signature);
 
156
 
 
157
/* Key generation */
 
158
 
 
159
int
 
160
dsa_generate_keypair(struct dsa_public_key *pub,
 
161
                     struct dsa_private_key *key,
 
162
 
 
163
                     void *random_ctx, nettle_random_func random,
 
164
                     void *progress_ctx, nettle_progress_func progress,
 
165
 
 
166
                     /* Size of key, in bits.
 
167
                      * Use size = 512 + 64 * l for the official
 
168
                      * NIS key sizes. */
 
169
                     unsigned bits);
 
170
 
 
171
struct sexp_iterator;
 
172
 
 
173
int
 
174
dsa_signature_from_sexp(struct dsa_signature *rs,
 
175
                        struct sexp_iterator *i);
 
176
 
 
177
int
 
178
dsa_keypair_from_sexp_alist(struct dsa_public_key *pub,
 
179
                            struct dsa_private_key *priv,
 
180
                            unsigned limit,
 
181
                            struct sexp_iterator *i);
 
182
 
 
183
/* If PRIV is NULL, expect a public-key expression. If PUB is NULL,
 
184
 * expect a private key expression and ignore the parts not needed for
 
185
 * the public key. */
 
186
/* Keys must be initialized before calling this function, as usual. */
 
187
int
 
188
dsa_keypair_from_sexp(struct dsa_public_key *pub,
 
189
                      struct dsa_private_key *priv,
 
190
                      unsigned limit,
 
191
                      unsigned length, const uint8_t *expr);
 
192
 
 
193
 
 
194
#endif /* NETTLE_DSA_H_INCLUDED */