~ubuntu-branches/ubuntu/lucid/openssh/lucid

« back to all changes in this revision

Viewing changes to jpake.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2010-01-26 13:07:40 UTC
  • mfrom: (1.13.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100126130740-d7r70jqrqlbvz3r0
Tags: 1:5.3p1-1ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Add support for registering ConsoleKit sessions on login.
  - Drop openssh-blacklist and openssh-blacklist-extra to Suggests; they
    take up a lot of CD space, and I suspect that rolling them out in
    security updates has covered most affected systems now.
  - Convert to Upstart.  The init script is still here for the benefit of
    people running sshd in chroots.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $OpenBSD: jpake.c,v 1.1 2008/11/04 08:22:12 djm Exp $ */
 
1
/* $OpenBSD: jpake.c,v 1.2 2009/03/05 07:18:19 djm Exp $ */
2
2
/*
3
3
 * Copyright (c) 2008 Damien Miller.  All rights reserved.
4
4
 *
47
47
#include "log.h"
48
48
 
49
49
#include "jpake.h"
 
50
#include "schnorr.h"
50
51
 
51
52
#ifdef JPAKE
52
53
 
60
61
        "98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB" \
61
62
        "9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"
62
63
 
63
 
struct jpake_group *
 
64
struct modp_group *
64
65
jpake_default_group(void)
65
66
{
66
 
        struct jpake_group *ret;
67
 
 
68
 
        ret = xmalloc(sizeof(*ret));
69
 
        ret->p = ret->q = ret->g = NULL;
70
 
        if (BN_hex2bn(&ret->p, JPAKE_GROUP_P) == 0 ||
71
 
            BN_hex2bn(&ret->g, JPAKE_GROUP_G) == 0)
72
 
                fatal("%s: BN_hex2bn", __func__);
73
 
        /* Subgroup order is p/2 (p is a safe prime) */
74
 
        if ((ret->q = BN_new()) == NULL)
75
 
                fatal("%s: BN_new", __func__);
76
 
        if (BN_rshift1(ret->q, ret->p) != 1)
77
 
                fatal("%s: BN_rshift1", __func__);
78
 
 
79
 
        return ret;
80
 
}
81
 
 
82
 
/*
83
 
 * Generate uniformly distributed random number in range (1, high).
84
 
 * Return number on success, NULL on failure.
85
 
 */
86
 
BIGNUM *
87
 
bn_rand_range_gt_one(const BIGNUM *high)
88
 
{
89
 
        BIGNUM *r, *tmp;
90
 
        int success = -1;
91
 
 
92
 
        if ((tmp = BN_new()) == NULL) {
93
 
                error("%s: BN_new", __func__);
94
 
                return NULL;
95
 
        }
96
 
        if ((r = BN_new()) == NULL) {
97
 
                error("%s: BN_new failed", __func__);
98
 
                goto out;
99
 
        }
100
 
        if (BN_set_word(tmp, 2) != 1) {
101
 
                error("%s: BN_set_word(tmp, 2)", __func__);
102
 
                goto out;
103
 
        }
104
 
        if (BN_sub(tmp, high, tmp) == -1) {
105
 
                error("%s: BN_sub failed (tmp = high - 2)", __func__);
106
 
                goto out;
107
 
        }
108
 
        if (BN_rand_range(r, tmp) == -1) {
109
 
                error("%s: BN_rand_range failed", __func__);
110
 
                goto out;
111
 
        }
112
 
        if (BN_set_word(tmp, 2) != 1) {
113
 
                error("%s: BN_set_word(tmp, 2)", __func__);
114
 
                goto out;
115
 
        }
116
 
        if (BN_add(r, r, tmp) == -1) {
117
 
                error("%s: BN_add failed (r = r + 2)", __func__);
118
 
                goto out;
119
 
        }
120
 
        success = 0;
121
 
 out:
122
 
        BN_clear_free(tmp);
123
 
        if (success == 0)
124
 
                return r;
125
 
        BN_clear_free(r);
126
 
        return NULL;
127
 
}
128
 
 
129
 
/*
130
 
 * Hash contents of buffer 'b' with hash 'md'. Returns 0 on success,
131
 
 * with digest via 'digestp' (caller to free) and length via 'lenp'.
132
 
 * Returns -1 on failure.
133
 
 */
134
 
int
135
 
hash_buffer(const u_char *buf, u_int len, const EVP_MD *md,
136
 
    u_char **digestp, u_int *lenp)
137
 
{
138
 
        u_char digest[EVP_MAX_MD_SIZE];
139
 
        u_int digest_len;
140
 
        EVP_MD_CTX evp_md_ctx;
141
 
        int success = -1;
142
 
 
143
 
        EVP_MD_CTX_init(&evp_md_ctx);
144
 
 
145
 
        if (EVP_DigestInit_ex(&evp_md_ctx, md, NULL) != 1) {
146
 
                error("%s: EVP_DigestInit_ex", __func__);
147
 
                goto out;
148
 
        }
149
 
        if (EVP_DigestUpdate(&evp_md_ctx, buf, len) != 1) {
150
 
                error("%s: EVP_DigestUpdate", __func__);
151
 
                goto out;
152
 
        }
153
 
        if (EVP_DigestFinal_ex(&evp_md_ctx, digest, &digest_len) != 1) {
154
 
                error("%s: EVP_DigestFinal_ex", __func__);
155
 
                goto out;
156
 
        }
157
 
        *digestp = xmalloc(digest_len);
158
 
        *lenp = digest_len;
159
 
        memcpy(*digestp, digest, *lenp);
160
 
        success = 0;
161
 
 out:
162
 
        EVP_MD_CTX_cleanup(&evp_md_ctx);
163
 
        bzero(digest, sizeof(digest));
164
 
        digest_len = 0;
165
 
        return success;
166
 
}
167
 
 
168
 
/* print formatted string followed by bignum */
169
 
void
170
 
jpake_debug3_bn(const BIGNUM *n, const char *fmt, ...)
171
 
{
172
 
        char *out, *h;
173
 
        va_list args;
174
 
 
175
 
        out = NULL;
176
 
        va_start(args, fmt);
177
 
        vasprintf(&out, fmt, args);
178
 
        va_end(args);
179
 
        if (out == NULL)
180
 
                fatal("%s: vasprintf failed", __func__);
181
 
 
182
 
        if (n == NULL)
183
 
                debug3("%s(null)", out);
184
 
        else {
185
 
                h = BN_bn2hex(n);
186
 
                debug3("%s0x%s", out, h);
187
 
                free(h);
188
 
        }
189
 
        free(out);
190
 
}
191
 
 
192
 
/* print formatted string followed by buffer contents in hex */
193
 
void
194
 
jpake_debug3_buf(const u_char *buf, u_int len, const char *fmt, ...)
195
 
{
196
 
        char *out, h[65];
197
 
        u_int i, j;
198
 
        va_list args;
199
 
 
200
 
        out = NULL;
201
 
        va_start(args, fmt);
202
 
        vasprintf(&out, fmt, args);
203
 
        va_end(args);
204
 
        if (out == NULL)
205
 
                fatal("%s: vasprintf failed", __func__);
206
 
 
207
 
        debug3("%s length %u%s", out, len, buf == NULL ? " (null)" : "");
208
 
        free(out);
209
 
        if (buf == NULL)
210
 
                return;
211
 
 
212
 
        *h = '\0';
213
 
        for (i = j = 0; i < len; i++) {
214
 
                snprintf(h + j, sizeof(h) - j, "%02x", buf[i]);
215
 
                j += 2;
216
 
                if (j >= sizeof(h) - 1 || i == len - 1) {
217
 
                        debug3("    %s", h);
218
 
                        *h = '\0';
219
 
                        j = 0;
220
 
                }
221
 
        }
 
67
        return modp_group_from_g_and_safe_p(JPAKE_GROUP_G, JPAKE_GROUP_P);
222
68
}
223
69
 
224
70
struct jpake_ctx *
243
89
        return ret;
244
90
}
245
91
 
246
 
 
247
92
void
248
93
jpake_free(struct jpake_ctx *pctx)
249
94
{
344
189
 
345
190
/* Shared parts of step 1 exchange calculation */
346
191
void
347
 
jpake_step1(struct jpake_group *grp,
 
192
jpake_step1(struct modp_group *grp,
348
193
    u_char **id, u_int *id_len,
349
194
    BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
350
195
    u_char **priv1_proof, u_int *priv1_proof_len,
383
228
                fatal("%s: BN_mod_exp", __func__);
384
229
 
385
230
        /* Generate proofs for holding x1/x3 and x2/x4 */
386
 
        if (schnorr_sign(grp->p, grp->q, grp->g,
 
231
        if (schnorr_sign_buf(grp->p, grp->q, grp->g,
387
232
            *priv1, *g_priv1, *id, *id_len,
388
233
            priv1_proof, priv1_proof_len) != 0)
389
234
                fatal("%s: schnorr_sign", __func__);
390
 
        if (schnorr_sign(grp->p, grp->q, grp->g,
 
235
        if (schnorr_sign_buf(grp->p, grp->q, grp->g,
391
236
            *priv2, *g_priv2, *id, *id_len,
392
237
            priv2_proof, priv2_proof_len) != 0)
393
238
                fatal("%s: schnorr_sign", __func__);
397
242
 
398
243
/* Shared parts of step 2 exchange calculation */
399
244
void
400
 
jpake_step2(struct jpake_group *grp, BIGNUM *s,
 
245
jpake_step2(struct modp_group *grp, BIGNUM *s,
401
246
    BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
402
247
    const u_char *theirid, u_int theirid_len,
403
248
    const u_char *myid, u_int myid_len,
415
260
        if (BN_cmp(theirpub2, BN_value_one()) <= 0)
416
261
                fatal("%s: theirpub2 <= 1", __func__);
417
262
 
418
 
        if (schnorr_verify(grp->p, grp->q, grp->g, theirpub1,
 
263
        if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub1,
419
264
            theirid, theirid_len, theirpub1_proof, theirpub1_proof_len) != 1)
420
265
                fatal("%s: schnorr_verify theirpub1 failed", __func__);
421
 
        if (schnorr_verify(grp->p, grp->q, grp->g, theirpub2,
 
266
        if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub2,
422
267
            theirid, theirid_len, theirpub2_proof, theirpub2_proof_len) != 1)
423
268
                fatal("%s: schnorr_verify theirpub2 failed", __func__);
424
269
 
459
304
        JPAKE_DEBUG_BN((exponent, "%s: exponent = ", __func__));
460
305
 
461
306
        /* Note the generator here is 'tmp', not g */
462
 
        if (schnorr_sign(grp->p, grp->q, tmp, exponent, *newpub,
 
307
        if (schnorr_sign_buf(grp->p, grp->q, tmp, exponent, *newpub,
463
308
            myid, myid_len,
464
309
            newpub_exponent_proof, newpub_exponent_proof_len) != 0)
465
310
                fatal("%s: schnorr_sign newpub", __func__);
496
341
 
497
342
/* Shared parts of key derivation and confirmation calculation */
498
343
void
499
 
jpake_key_confirm(struct jpake_group *grp, BIGNUM *s, BIGNUM *step2_val,
 
344
jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
500
345
    BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
501
346
    BIGNUM *theirpub1, BIGNUM *theirpub2,
502
347
    const u_char *my_id, u_int my_id_len,
531
376
 
532
377
        JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__));
533
378
 
534
 
        if (schnorr_verify(grp->p, grp->q, tmp, step2_val, 
 
379
        if (schnorr_verify_buf(grp->p, grp->q, tmp, step2_val, 
535
380
            their_id, their_id_len,
536
381
            theirpriv2_s_proof, theirpriv2_s_proof_len) != 1)
537
382
                fatal("%s: schnorr_verify theirpriv2_s_proof failed", __func__);