os/ossrv/ssl/libcrypto/src/crypto/rsa/rsa_gen.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/rsa/rsa_gen.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
sl@0
    60
/* NB: these functions have been "upgraded", the deprecated versions (which are
sl@0
    61
 * compatibility wrappers using these functions) are in rsa_depr.c.
sl@0
    62
 * - Geoff
sl@0
    63
 */
sl@0
    64
sl@0
    65
#include <stdio.h>
sl@0
    66
#include <time.h>
sl@0
    67
#include "cryptlib.h"
sl@0
    68
#include <openssl/bn.h>
sl@0
    69
#include <openssl/rsa.h>
sl@0
    70
sl@0
    71
static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb);
sl@0
    72
sl@0
    73
/* NB: this wrapper would normally be placed in rsa_lib.c and the static
sl@0
    74
 * implementation would probably be in rsa_eay.c. Nonetheless, is kept here so
sl@0
    75
 * that we don't introduce a new linker dependency. Eg. any application that
sl@0
    76
 * wasn't previously linking object code related to key-generation won't have to
sl@0
    77
 * now just because key-generation is part of RSA_METHOD. */
sl@0
    78
EXPORT_C int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
sl@0
    79
	{
sl@0
    80
	if(rsa->meth->rsa_keygen)
sl@0
    81
		return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
sl@0
    82
	return rsa_builtin_keygen(rsa, bits, e_value, cb);
sl@0
    83
	}
sl@0
    84
sl@0
    85
static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
sl@0
    86
	{
sl@0
    87
	BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp;
sl@0
    88
	BIGNUM local_r0,local_d,local_p;
sl@0
    89
	BIGNUM *pr0,*d,*p;
sl@0
    90
	int bitsp,bitsq,ok= -1,n=0;
sl@0
    91
	BN_CTX *ctx=NULL;
sl@0
    92
sl@0
    93
	ctx=BN_CTX_new();
sl@0
    94
	if (ctx == NULL) goto err;
sl@0
    95
	BN_CTX_start(ctx);
sl@0
    96
	r0 = BN_CTX_get(ctx);
sl@0
    97
	r1 = BN_CTX_get(ctx);
sl@0
    98
	r2 = BN_CTX_get(ctx);
sl@0
    99
	r3 = BN_CTX_get(ctx);
sl@0
   100
	if (r3 == NULL) goto err;
sl@0
   101
sl@0
   102
	bitsp=(bits+1)/2;
sl@0
   103
	bitsq=bits-bitsp;
sl@0
   104
sl@0
   105
	/* We need the RSA components non-NULL */
sl@0
   106
	if(!rsa->n && ((rsa->n=BN_new()) == NULL)) goto err;
sl@0
   107
	if(!rsa->d && ((rsa->d=BN_new()) == NULL)) goto err;
sl@0
   108
	if(!rsa->e && ((rsa->e=BN_new()) == NULL)) goto err;
sl@0
   109
	if(!rsa->p && ((rsa->p=BN_new()) == NULL)) goto err;
sl@0
   110
	if(!rsa->q && ((rsa->q=BN_new()) == NULL)) goto err;
sl@0
   111
	if(!rsa->dmp1 && ((rsa->dmp1=BN_new()) == NULL)) goto err;
sl@0
   112
	if(!rsa->dmq1 && ((rsa->dmq1=BN_new()) == NULL)) goto err;
sl@0
   113
	if(!rsa->iqmp && ((rsa->iqmp=BN_new()) == NULL)) goto err;
sl@0
   114
sl@0
   115
	BN_copy(rsa->e, e_value);
sl@0
   116
sl@0
   117
	/* generate p and q */
sl@0
   118
	for (;;)
sl@0
   119
		{
sl@0
   120
		if(!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb))
sl@0
   121
			goto err;
sl@0
   122
		if (!BN_sub(r2,rsa->p,BN_value_one())) goto err;
sl@0
   123
		if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err;
sl@0
   124
		if (BN_is_one(r1)) break;
sl@0
   125
		if(!BN_GENCB_call(cb, 2, n++))
sl@0
   126
			goto err;
sl@0
   127
		}
sl@0
   128
	if(!BN_GENCB_call(cb, 3, 0))
sl@0
   129
		goto err;
sl@0
   130
	for (;;)
sl@0
   131
		{
sl@0
   132
		/* When generating ridiculously small keys, we can get stuck
sl@0
   133
		 * continually regenerating the same prime values. Check for
sl@0
   134
		 * this and bail if it happens 3 times. */
sl@0
   135
		unsigned int degenerate = 0;
sl@0
   136
		do
sl@0
   137
			{
sl@0
   138
			if(!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))
sl@0
   139
				goto err;
sl@0
   140
			} while((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
sl@0
   141
		if(degenerate == 3)
sl@0
   142
			{
sl@0
   143
			ok = 0; /* we set our own err */
sl@0
   144
			RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,RSA_R_KEY_SIZE_TOO_SMALL);
sl@0
   145
			goto err;
sl@0
   146
			}
sl@0
   147
		if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;
sl@0
   148
		if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err;
sl@0
   149
		if (BN_is_one(r1))
sl@0
   150
			break;
sl@0
   151
		if(!BN_GENCB_call(cb, 2, n++))
sl@0
   152
			goto err;
sl@0
   153
		}
sl@0
   154
	if(!BN_GENCB_call(cb, 3, 1))
sl@0
   155
		goto err;
sl@0
   156
	if (BN_cmp(rsa->p,rsa->q) < 0)
sl@0
   157
		{
sl@0
   158
		tmp=rsa->p;
sl@0
   159
		rsa->p=rsa->q;
sl@0
   160
		rsa->q=tmp;
sl@0
   161
		}
sl@0
   162
sl@0
   163
	/* calculate n */
sl@0
   164
	if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err;
sl@0
   165
sl@0
   166
	/* calculate d */
sl@0
   167
	if (!BN_sub(r1,rsa->p,BN_value_one())) goto err;	/* p-1 */
sl@0
   168
	if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;	/* q-1 */
sl@0
   169
	if (!BN_mul(r0,r1,r2,ctx)) goto err;	/* (p-1)(q-1) */
sl@0
   170
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   171
		{
sl@0
   172
		  pr0 = &local_r0;
sl@0
   173
		  BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
sl@0
   174
		}
sl@0
   175
	else
sl@0
   176
	  pr0 = r0;
sl@0
   177
	if (!BN_mod_inverse(rsa->d,rsa->e,pr0,ctx)) goto err;	/* d */
sl@0
   178
sl@0
   179
	/* set up d for correct BN_FLG_CONSTTIME flag */
sl@0
   180
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   181
		{
sl@0
   182
		d = &local_d;
sl@0
   183
		BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
sl@0
   184
		}
sl@0
   185
	else
sl@0
   186
		d = rsa->d;
sl@0
   187
	/* calculate d mod (p-1) */
sl@0
   188
	if (!BN_mod(rsa->dmp1,d,r1,ctx)) goto err;
sl@0
   189
sl@0
   190
	/* calculate d mod (q-1) */
sl@0
   191
	if (!BN_mod(rsa->dmq1,d,r2,ctx)) goto err;
sl@0
   192
sl@0
   193
	/* calculate inverse of q mod p */
sl@0
   194
	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
sl@0
   195
		{
sl@0
   196
		p = &local_p;
sl@0
   197
		BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
sl@0
   198
		}
sl@0
   199
	else
sl@0
   200
		p = rsa->p;
sl@0
   201
	if (!BN_mod_inverse(rsa->iqmp,rsa->q,p,ctx)) goto err;
sl@0
   202
sl@0
   203
	ok=1;
sl@0
   204
err:
sl@0
   205
	if (ok == -1)
sl@0
   206
		{
sl@0
   207
		RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,ERR_LIB_BN);
sl@0
   208
		ok=0;
sl@0
   209
		}
sl@0
   210
	if (ctx != NULL)
sl@0
   211
		{
sl@0
   212
		BN_CTX_end(ctx);
sl@0
   213
		BN_CTX_free(ctx);
sl@0
   214
		}
sl@0
   215
sl@0
   216
	return ok;
sl@0
   217
	}
sl@0
   218