~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/mycrypt_pk.h

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ---- NUMBER THEORY ---- */
2
 
#ifdef MPI
3
 
 
4
 
#include "ltc_tommath.h"
5
 
 
6
 
/* in/out macros */
7
 
#define OUTPUT_BIGNUM(num, out, y, z)                                                             \
8
 
{                                                                                                 \
9
 
      if ((y + 4) > *outlen) { return CRYPT_BUFFER_OVERFLOW; }                                    \
10
 
      z = (unsigned long)mp_unsigned_bin_size(num);                                               \
11
 
      STORE32L(z, out+y);                                                                         \
12
 
      y += 4;                                                                                     \
13
 
      if ((y + z) > *outlen) { return CRYPT_BUFFER_OVERFLOW; }                                    \
14
 
      if ((err = mp_to_unsigned_bin(num, out+y)) != MP_OKAY) { return mpi_to_ltc_error(err); }    \
15
 
      y += z;                                                                                     \
16
 
}
17
 
 
18
 
 
19
 
#define INPUT_BIGNUM(num, in, x, y, inlen)                       \
20
 
{                                                                \
21
 
     /* load value */                                            \
22
 
     if ((y + 4) > inlen) {                                      \
23
 
        err = CRYPT_INVALID_PACKET;                              \
24
 
        goto error;                                              \
25
 
     }                                                           \
26
 
     LOAD32L(x, in+y);                                           \
27
 
     y += 4;                                                     \
28
 
                                                                 \
29
 
     /* sanity check... */                                       \
30
 
     if ((x+y) > inlen) {                                        \
31
 
        err = CRYPT_INVALID_PACKET;                              \
32
 
        goto error;                                              \
33
 
     }                                                           \
34
 
                                                                 \
35
 
     /* load it */                                               \
36
 
     if ((err = mp_read_unsigned_bin(num, (unsigned char *)in+y, (int)x)) != MP_OKAY) {\
37
 
        err = mpi_to_ltc_error(err);                             \
38
 
        goto error;                                              \
39
 
     }                                                           \
40
 
     y += x;                                                     \
41
 
     if ((err = mp_shrink(num)) != MP_OKAY) {                    \
42
 
        err = mpi_to_ltc_error(err);                             \
43
 
        goto error;                                              \
44
 
     }                                                           \
45
 
}
46
 
 
47
 
 int is_prime(mp_int *, int *);
48
 
 int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
49
 
 
50
 
#else
51
 
   #ifdef MRSA
52
 
      #error RSA requires the big int library 
53
 
   #endif
54
 
   #ifdef MECC
55
 
      #error ECC requires the big int library 
56
 
   #endif
57
 
   #ifdef MDH
58
 
      #error DH requires the big int library 
59
 
   #endif
60
 
   #ifdef MDSA
61
 
      #error DSA requires the big int library 
62
 
   #endif
63
 
#endif /* MPI */
64
 
 
65
 
 
66
 
/* ---- PUBLIC KEY CRYPTO ---- */
67
 
 
68
 
#define PK_PRIVATE            0        /* PK private keys */
69
 
#define PK_PUBLIC             1        /* PK public keys */
70
 
 
71
 
/* ---- PACKET ---- */
72
 
#ifdef PACKET
73
 
 
74
 
 void packet_store_header(unsigned char *dst, int section, int subsection);
75
 
 int packet_valid_header(unsigned char *src, int section, int subsection);
76
 
 
77
 
#endif
78
 
 
79
 
 
80
 
/* ---- RSA ---- */
81
 
#ifdef MRSA
82
 
 
83
 
/* Min and Max RSA key sizes (in bits) */
84
 
#define MIN_RSA_SIZE 1024
85
 
#define MAX_RSA_SIZE 4096
86
 
 
87
 
/* Stack required for temps (plus padding) */
88
 
// #define RSA_STACK    (8 + (MAX_RSA_SIZE/8))
89
 
 
90
 
typedef struct Rsa_key {
91
 
    int type;
92
 
    mp_int e, d, N, p, q, qP, dP, dQ;
93
 
} rsa_key;
94
 
 
95
 
 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
96
 
 
97
 
 int rsa_exptmod(const unsigned char *in,   unsigned long inlen,
98
 
                      unsigned char *out,  unsigned long *outlen, int which,
99
 
                      prng_state    *prng, int           prng_idx,
100
 
                      rsa_key *key);
101
 
 
102
 
#ifdef RSA_TIMING
103
 
 
104
 
 int tim_exptmod(prng_state *prng, int prng_idx, 
105
 
                       mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m);
106
 
 
107
 
#else
108
 
 
109
 
#define tim_exptmod(prng, prng_idx, c, e, d, n, m) mpi_to_ltc_error(mp_exptmod(c, d, n, m))
110
 
 
111
 
#endif
112
 
 
113
 
 void rsa_free(rsa_key *key);
114
 
 
115
 
/* These use PKCS #1 v2.0 padding */
116
 
int rsa_encrypt_key(const unsigned char *inkey,  unsigned long inlen,
117
 
                          unsigned char *outkey, unsigned long *outlen,
118
 
                    const unsigned char *lparam, unsigned long lparamlen,
119
 
                    prng_state *prng, int prng_idx, int hash_idx, rsa_key *key);
120
 
                                        
121
 
int rsa_decrypt_key(const unsigned char *in,     unsigned long inlen,
122
 
                          unsigned char *outkey, unsigned long *keylen, 
123
 
                    const unsigned char *lparam, unsigned long lparamlen,
124
 
                          prng_state    *prng,   int           prng_idx,
125
 
                          int            hash_idx, int *res,
126
 
                          rsa_key       *key);
127
 
 
128
 
int rsa_sign_hash(const unsigned char *msghash,  unsigned long  msghashlen, 
129
 
                        unsigned char *sig,      unsigned long *siglen, 
130
 
                        prng_state    *prng,     int            prng_idx,
131
 
                        int            hash_idx, unsigned long  saltlen,
132
 
                        rsa_key *key);
133
 
 
134
 
int rsa_verify_hash(const unsigned char *sig,      unsigned long siglen,
135
 
                    const unsigned char *msghash,  unsigned long msghashlen,
136
 
                          prng_state    *prng,     int           prng_idx,
137
 
                          int            hash_idx, unsigned long saltlen,
138
 
                          int           *stat,     rsa_key      *key);
139
 
 
140
 
/* these use PKCS #1 v1.5 padding */
141
 
int rsa_v15_encrypt_key(const unsigned char *inkey,    unsigned long  inlen,
142
 
                              unsigned char *outkey,   unsigned long *outlen,
143
 
                              prng_state    *prng,     int            prng_idx, 
144
 
                              rsa_key       *key);
145
 
                              
146
 
int rsa_v15_decrypt_key(const unsigned char *in,     unsigned long  inlen,
147
 
                              unsigned char *outkey, unsigned long keylen, 
148
 
                              prng_state    *prng,   int            prng_idx,
149
 
                              int           *res,    rsa_key       *key);
150
 
 
151
 
int rsa_v15_sign_hash(const unsigned char *msghash,  unsigned long  msghashlen, 
152
 
                            unsigned char *sig,      unsigned long *siglen, 
153
 
                            prng_state    *prng,     int            prng_idx,
154
 
                            int            hash_idx, rsa_key       *key);
155
 
 
156
 
int rsa_v15_verify_hash(const unsigned char *sig,      unsigned long siglen,
157
 
                        const unsigned char *msghash,  unsigned long msghashlen,
158
 
                              prng_state    *prng,     int           prng_idx,
159
 
                              int            hash_idx, int          *stat,     
160
 
                              rsa_key       *key);
161
 
 
162
 
 
163
 
/* PKCS #1 import/export */
164
 
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
165
 
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
166
 
                        
167
 
#endif
168
 
 
169
 
/* ---- DH Routines ---- */
170
 
#ifdef MDH 
171
 
 
172
 
typedef struct Dh_key {
173
 
    int idx, type;
174
 
    mp_int x, y;
175
 
} dh_key;
176
 
 
177
 
 int dh_test(void);
178
 
 void dh_sizes(int *low, int *high);
179
 
 int dh_get_size(dh_key *key);
180
 
 
181
 
 int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
182
 
 void dh_free(dh_key *key);
183
 
 
184
 
 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
185
 
 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
186
 
 
187
 
 int dh_shared_secret(dh_key *private_key, dh_key *public_key,
188
 
                            unsigned char *out, unsigned long *outlen);
189
 
 
190
 
 int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
191
 
                                unsigned char *out,  unsigned long *len, 
192
 
                                prng_state *prng, int wprng, int hash, 
193
 
                                dh_key *key);
194
 
 
195
 
 int dh_decrypt_key(const unsigned char *in,  unsigned long inlen, 
196
 
                                unsigned char *outkey, unsigned long *keylen, 
197
 
                                dh_key *key);
198
 
 
199
 
 int dh_sign_hash(const unsigned char *in,  unsigned long inlen,
200
 
                              unsigned char *out, unsigned long *outlen,
201
 
                              prng_state *prng, int wprng, dh_key *key);
202
 
 
203
 
 int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
204
 
                          const unsigned char *hash, unsigned long hashlen, 
205
 
                                int *stat, dh_key *key);
206
 
 
207
 
 
208
 
#endif
209
 
 
210
 
/* ---- ECC Routines ---- */
211
 
#ifdef MECC
212
 
typedef struct {
213
 
    mp_int x, y;
214
 
} ecc_point;
215
 
 
216
 
typedef struct {
217
 
    int type, idx;
218
 
    ecc_point pubkey;
219
 
    mp_int k;
220
 
} ecc_key;
221
 
 
222
 
 int ecc_test(void);
223
 
 void ecc_sizes(int *low, int *high);
224
 
 int ecc_get_size(ecc_key *key);
225
 
 
226
 
 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
227
 
 void ecc_free(ecc_key *key);
228
 
 
229
 
 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
230
 
 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
231
 
 
232
 
 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, 
233
 
                             unsigned char *out, unsigned long *outlen);
234
 
 
235
 
 int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
236
 
                                 unsigned char *out,  unsigned long *len, 
237
 
                                 prng_state *prng, int wprng, int hash, 
238
 
                                 ecc_key *key);
239
 
 
240
 
 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
241
 
                                 unsigned char *outkey, unsigned long *keylen, 
242
 
                                 ecc_key *key);
243
 
 
244
 
 int ecc_sign_hash(const unsigned char *in,  unsigned long inlen,
245
 
                               unsigned char *out, unsigned long *outlen,
246
 
                               prng_state *prng, int wprng, ecc_key *key);
247
 
 
248
 
 int ecc_verify_hash(const unsigned char *sig,  unsigned long siglen,
249
 
                           const unsigned char *hash, unsigned long hashlen, 
250
 
                                 int *stat, ecc_key *key);
251
 
#endif
252
 
 
253
 
#ifdef MDSA
254
 
 
255
 
typedef struct {
256
 
   int type, qord;
257
 
   mp_int g, q, p, x, y;
258
 
} dsa_key;
259
 
 
260
 
 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
261
 
 void dsa_free(dsa_key *key);
262
 
 
263
 
 int dsa_sign_hash(const unsigned char *in,  unsigned long inlen,
264
 
                        unsigned char *out, unsigned long *outlen,
265
 
                        prng_state *prng, int wprng, dsa_key *key);
266
 
 
267
 
 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
268
 
                           const unsigned char *hash, unsigned long inlen, 
269
 
                           int *stat, dsa_key *key);
270
 
 
271
 
 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
272
 
 
273
 
 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
274
 
 
275
 
 int dsa_verify_key(dsa_key *key, int *stat);
276
 
 
277
 
#endif
278
 
 
279
 
/* ifdef added by matt - a bit of a hack */
280
 
#ifdef MPI
281
 
/* DER handling */
282
 
int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen);
283
 
int der_decode_integer(const unsigned char *in, unsigned long *inlen, mp_int *num);
284
 
int der_length_integer(mp_int *num, unsigned long *len);
285
 
int der_put_multi_integer(unsigned char *dst, unsigned long *outlen, mp_int *num, ...);
286
 
int der_get_multi_integer(const unsigned char *src, unsigned long *inlen,  mp_int *num, ...);
287
 
#endif
288