]> andersk Git - moira.git/blob - util/gdss/lib/crypto/KeyFromPQ.c
initial import of gdss from the Athena source tree
[moira.git] / util / gdss / lib / crypto / KeyFromPQ.c
1 /*
2  * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
3  * ALL RIGHTS RESERVED
4  *
5  * "Digital Equipment Corporation authorizes the reproduction,
6  * distribution and modification of this software subject to the following
7  * restrictions:
8  * 
9  * 1.  Any partial or whole copy of this software, or any modification
10  * thereof, must include this copyright notice in its entirety.
11  *
12  * 2.  This software is supplied "as is" with no warranty of any kind,
13  * expressed or implied, for any purpose, including any warranty of fitness 
14  * or merchantibility.  DIGITAL assumes no responsibility for the use or
15  * reliability of this software, nor promises to provide any form of 
16  * support for it on any basis.
17  *
18  * 3.  Distribution of this software is authorized only if no profit or
19  * remuneration of any kind is received in exchange for such distribution.
20  * 
21  * 4.  This software produces public key authentication certificates
22  * bearing an expiration date established by DIGITAL and RSA Data
23  * Security, Inc.  It may cease to generate certificates after the expiration
24  * date.  Any modification of this software that changes or defeats
25  * the expiration date or its effect is unauthorized.
26  * 
27  * 5.  Software that will renew or extend the expiration date of
28  * authentication certificates produced by this software may be obtained
29  * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
30  * 94065, (415)595-8782, or from DIGITAL"
31  *
32  */
33
34 #include <stdio.h>
35 #include <sys/types.h>
36
37 #include "BigNum.h"
38 #include "BigRSA.h"
39 #include "bigrsacode.h"
40 #include "bigkeygen.h"
41
42 /* 
43 #ifdef DEBUG
44 #undef DEBUG
45 #endif
46 */
47
48 /* given two primes in msb-lsg order, initialize the key structure */
49
50 int KeyFromPQ ( P, PL, Q, QL, keys )
51 char *P, *Q;
52 int PL, QL;
53 RSAKeyStorage *keys ;
54 {
55     BigNum  p = keys->p, q = keys->q ;
56     int i;
57     char *x, *y;
58     unsigned pl, ql;
59
60     memset(keys,0,sizeof(RSAKeyStorage));
61     BnnInit();
62
63     DecodeBigInteger(P, p, PL);
64     DecodeBigInteger(Q, q, QL);
65  
66     pl = BnnNumDigits(p, (PL+sizeof(BigNumDigit))/sizeof(BigNumDigit));
67     ql = BnnNumDigits(q, (QL+sizeof(BigNumDigit))/sizeof(BigNumDigit));
68
69     if (BnnCompare(p, pl, q, ql) == -1) {
70         unsigned tpl = pl;
71         BigNumDigit tp[DigitLim];
72         BnnSetToZero(tp,DigitLim);
73         BnnAssign(tp,p,pl);
74         BnnSetToZero(p,pl);
75         BnnAssign(p,q,ql);
76         BnnAssign(q,tp,pl);
77         pl = ql;
78         ql = tpl;
79     }
80
81     keys->pl = pl;
82     keys->ql = ql;
83
84     BnnSetDigit(keys->e, (BigNumDigit) ((1<<16)+1));
85     keys->el = 1;
86
87     BnnMultiply(keys->n, pl+ql, p, pl, q, ql),
88     keys->nl = pl+ql;
89
90 #ifdef DEBUG
91 PrintTestKey(keys);
92 #endif
93
94     if (FinishKey(keys)==0) return (0);
95
96     if (TestRSAKeys(keys)!=1) {
97 #ifdef DEBUG
98 printf("\nFailed.\n");
99 #endif
100         return(0);
101     }  
102
103     BnnClose();
104     return(1);
105 }
106
107
This page took 0.044222 seconds and 5 git commands to generate.