~andersk/ubuntu/oneiric/openssl/spurious-reboot

« back to all changes in this revision

Viewing changes to crypto/dsa/dsa_ossl.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2010-12-12 15:37:21 UTC
  • mto: (1.2.1 upstream) (11.2.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 55.
  • Revision ID: james.westby@ubuntu.com-20101212153721-mfw51stum5hwztpd
Tags: upstream-1.0.0c
ImportĀ upstreamĀ versionĀ 1.0.0c

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
#include <stdio.h>
62
62
#include "cryptlib.h"
63
63
#include <openssl/bn.h>
 
64
#include <openssl/sha.h>
64
65
#include <openssl/dsa.h>
65
66
#include <openssl/rand.h>
66
67
#include <openssl/asn1.h>
67
68
 
68
 
#ifndef OPENSSL_FIPS
69
 
 
70
69
static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
71
70
static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp);
72
71
static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
73
 
                  DSA *dsa);
 
72
                         DSA *dsa);
74
73
static int dsa_init(DSA *dsa);
75
74
static int dsa_finish(DSA *dsa);
76
75
 
135
134
        BIGNUM m;
136
135
        BIGNUM xr;
137
136
        BN_CTX *ctx=NULL;
138
 
        int i,reason=ERR_R_BN_LIB;
 
137
        int reason=ERR_R_BN_LIB;
139
138
        DSA_SIG *ret=NULL;
140
139
 
141
140
        BN_init(&m);
150
149
        s=BN_new();
151
150
        if (s == NULL) goto err;
152
151
 
153
 
        i=BN_num_bytes(dsa->q); /* should be 20 */
154
 
        if ((dlen > i) || (dlen > 50))
 
152
        /* reject a excessive digest length (currently at most
 
153
         * dsa-with-SHA256 is supported) */
 
154
        if (dlen > SHA256_DIGEST_LENGTH)
155
155
                {
156
156
                reason=DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE;
157
157
                goto err;
172
172
                dsa->r=NULL;
173
173
                }
174
174
 
175
 
        if (BN_bin2bn(dgst,dlen,&m) == NULL) goto err;
 
175
        
 
176
        if (dlen > BN_num_bytes(dsa->q))
 
177
                /* if the digest length is greater than the size of q use the
 
178
                 * BN_num_bits(dsa->q) leftmost bits of the digest, see
 
179
                 * fips 186-3, 4.2 */
 
180
                dlen = BN_num_bytes(dsa->q);
 
181
        if (BN_bin2bn(dgst,dlen,&m) == NULL)
 
182
                goto err;
176
183
 
177
184
        /* Compute  s = inv(k) (m + xr) mod q */
178
185
        if (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;/* s = xr */
179
186
        if (!BN_add(s, &xr, &m)) goto err;              /* s = m + xr */
180
187
        if (BN_cmp(s,dsa->q) > 0)
181
 
                BN_sub(s,s,dsa->q);
 
188
                if (!BN_sub(s,s,dsa->q)) goto err;
182
189
        if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err;
183
190
 
184
191
        ret=DSA_SIG_new();
283
290
        if (!ret)
284
291
                {
285
292
                DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB);
286
 
                if (kinv != NULL) BN_clear_free(kinv);
287
 
                if (r != NULL) BN_clear_free(r);
 
293
                if (r != NULL)
 
294
                        BN_clear_free(r);
288
295
                }
289
296
        if (ctx_in == NULL) BN_CTX_free(ctx);
290
 
        if (kinv != NULL) BN_clear_free(kinv);
291
297
        BN_clear_free(&k);
292
298
        BN_clear_free(&kq);
293
299
        return(ret);
294
300
        }
295
301
 
296
302
static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
297
 
                  DSA *dsa)
 
303
                         DSA *dsa)
298
304
        {
299
305
        BN_CTX *ctx;
300
306
        BIGNUM u1,u2,t1;
301
307
        BN_MONT_CTX *mont=NULL;
302
 
        int ret = -1;
 
308
        int ret = -1, i;
303
309
        if (!dsa->p || !dsa->q || !dsa->g)
304
310
                {
305
311
                DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MISSING_PARAMETERS);
306
312
                return -1;
307
313
                }
308
314
 
309
 
        if (BN_num_bits(dsa->q) != 160)
 
315
        i = BN_num_bits(dsa->q);
 
316
        /* fips 186-3 allows only different sizes for q */
 
317
        if (i != 160 && i != 224 && i != 256)
310
318
                {
311
319
                DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_BAD_Q_VALUE);
312
320
                return -1;
318
326
                return -1;
319
327
                }
320
328
 
 
329
        /* reject a excessive digest length (currently at most
 
330
         * dsa-with-SHA256 is supported) */
 
331
        if (dgst_len > SHA256_DIGEST_LENGTH)
 
332
                {
 
333
                DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
 
334
                return -1;
 
335
                }
 
336
 
321
337
        BN_init(&u1);
322
338
        BN_init(&u2);
323
339
        BN_init(&t1);
342
358
        if ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err;
343
359
 
344
360
        /* save M in u1 */
 
361
        if (dgst_len > (i >> 3))
 
362
                /* if the digest length is greater than the size of q use the
 
363
                 * BN_num_bits(dsa->q) leftmost bits of the digest, see
 
364
                 * fips 186-3, 4.2 */
 
365
                dgst_len = (i >> 3);
345
366
        if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err;
346
367
 
347
368
        /* u1 = M * w mod q */
393
414
        return(1);
394
415
}
395
416
 
396
 
#endif