Main Page Modules Data Structures File List Data Fields Globals Related Pages

# beecrypt/rsakp.c

Go to the documentation of this file.

    00001
    00007 /*
    00008  * <conformance statement for IEEE P1363 needed here>
    00009  *
    00010  * Copyright (c) 2000 Virtual Unlimited B.V.
    00011  *
    00012  * Author: Bob Deblier <bob@virtualunlimited.com>
    00013  *
    00014  * This library is free software; you can redistribute it and/or
    00015  * modify it under the terms of the GNU Lesser General Public
    00016  * License as published by the Free Software Foundation; either
    00017  * version 2.1 of the License, or (at your option) any later version.
    00018  *
    00019  * This library is distributed in the hope that it will be useful,
    00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00022  * Lesser General Public License for more details.
    00023  *
    00024  * You should have received a copy of the GNU Lesser General Public
    00025  * License along with this library; if not, write to the Free Software
    00026  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00027  *
    00028  */
    00029
    00030 #include "system.h"
    00031 #include "rsakp.h"
    00032 #include "mp32prime.h"
    00033 #include "mp32.h"
    00034 #include "debug.h"
    00035
    00036 /*@-boundswrite@*/
    00037 int rsakpMake(rsakp* kp, randomGeneratorContext* rgc, int nsize)
    00038 {
    00039         /*
    00040          * Generates an RSA Keypair for use with the Chinese Remainder Theorem
    00041          */
    00042
    00043         register uint32  pqsize = ((uint32)(nsize+1)) >> 1;
    00044         register uint32* temp = (uint32*) malloc((16*pqsize+6) * sizeof(*temp));
    00045         register uint32  newn = 1;
    00046
    00047         if (temp)
    00048         {
    00049                 mp32barrett r, psubone, qsubone, phi;
    00050
    00051                 nsize = pqsize << 1;
    00052
    00053                 /* set e */
    00054                 mp32nsetw(&kp->e, 65535);
    00055
    00056                 /* generate a random prime p and q */
    00057                 /*@-globs@*/
    00058                 mp32prnd_w(&kp->p, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
    00059                 mp32prnd_w(&kp->q, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
    00060                 /*@-globs@*/
    00061
    00062                 /* if p <= q, perform a swap to make p larger than q */
    00063                 if (mp32le(pqsize, kp->p.modl, kp->q.modl))
    00064                 {
    00065                         /*@-sizeoftype@*/
    00066                         memcpy(&r, &kp->q, sizeof(mp32barrett));
    00067                         memcpy(&kp->q, &kp->p, sizeof(mp32barrett));
    00068                         memcpy(&kp->p, &r, sizeof(mp32barrett));
    00069                         /*@=sizeoftype@*/
    00070                 }
    00071
    00072                 mp32bzero(&r);
    00073                 mp32bzero(&psubone);
    00074                 mp32bzero(&qsubone);
    00075                 mp32bzero(&phi);
    00076
    00077                 while (1)
    00078                 {
    00079                         mp32mul(temp, pqsize, kp->p.modl, pqsize, kp->q.modl);
    00080
    00081                         if (newn && mp32msbset(nsize, temp))
    00082                                 break;
    00083
    00084                         /* product of p and q doesn't have the required size (one bit short) */
    00085
    00086                         /*@-globs@*/
    00087                         mp32prnd_w(&r, rgc, pqsize, mp32ptrials(pqsize << 5), &kp->e, temp);
    00088                         /*@=globs@*/
    00089
    00090                         /*@-usedef -branchstate @*/ /* r is set */
    00091                         if (mp32le(pqsize, kp->p.modl, r.modl))
    00092                         {
    00093                                 mp32bfree(&kp->q);
    00094                                 /*@-sizeoftype@*/
    00095                                 memcpy(&kp->q, &kp->p, sizeof(mp32barrett));
    00096                                 memcpy(&kp->p, &r, sizeof(mp32barrett));
    00097                                 /*@=sizeoftype@*/
    00098                                 mp32bzero(&r);
    00099                                 newn = 1;
    00100                         }
    00101                         else if (mp32le(pqsize, kp->q.modl, r.modl))
    00102                         {
    00103                                 mp32bfree(&kp->q);
    00104                                 /*@-sizeoftype@*/
    00105                                 memcpy(&kp->q, &r, sizeof(mp32barrett));
    00106                                 /*@=sizeoftype@*/
    00107                                 mp32bzero(&r);
    00108                                 newn = 1;
    00109                         }
    00110                         else
    00111                         {
    00112                                 mp32bfree(&r);
    00113                                 newn = 0;
    00114                         }
    00115                         /*@=usedef =branchstate @*/
    00116                 }
    00117
    00118                 mp32bset(&kp->n, nsize, temp);
    00119
    00120                 /* compute p-1 */
    00121                 mp32bsubone(&kp->p, temp);
    00122                 mp32bset(&psubone, pqsize, temp);
    00123
    00124                 /* compute q-1 */
    00125                 mp32bsubone(&kp->q, temp);
    00126                 mp32bset(&qsubone, pqsize, temp);
    00127
    00128                 /*@-usedef@*/   /* psubone/qsubone are set */
    00129                 /* compute phi = (p-1)*(q-1) */
    00130                 mp32mul(temp, pqsize, psubone.modl, pqsize, qsubone.modl);
    00131                 mp32bset(&phi, nsize, temp);
    00132
    00133                 /* compute d = inv(e) mod phi */
    00134                 mp32nsize(&kp->d, nsize);
    00135                 (void) mp32binv_w(&phi, kp->e.size, kp->e.data, kp->d.data, temp);
    00136
    00137                 /* compute d1 = d mod (p-1) */
    00138                 mp32nsize(&kp->d1, pqsize);
    00139                 mp32bmod_w(&psubone, kp->d.data, kp->d1.data, temp);
    00140
    00141                 /* compute d2 = d mod (q-1) */
    00142                 mp32nsize(&kp->d2, pqsize);
    00143                 mp32bmod_w(&qsubone, kp->d.data, kp->d2.data, temp);
    00144
    00145                 /* compute c = inv(q) mod p */
    00146                 mp32nsize(&kp->c, pqsize);
    00147                 (void) mp32binv_w(&kp->p, pqsize, kp->q.modl, kp->c.data, temp);
    00148
    00149                 free(temp);
    00150                 /*@=usedef@*/
    00151
    00152                 return 0;
    00153         }
    00154         return -1;
    00155 }
    00156 /*@=boundswrite@*/
    00157
    00158 /*@-boundswrite@*/
    00159 int rsakpInit(rsakp* kp)
    00160 {
    00161         memset(kp, 0, sizeof(*kp));
    00162         /* or
    00163         mp32bzero(&kp->n);
    00164         mp32nzero(&kp->e);
    00165         mp32nzero(&kp->d);
    00166         mp32bzero(&kp->p);
    00167         mp32bzero(&kp->q);
    00168         mp32nzero(&kp->d1);
    00169         mp32nzero(&kp->d2);
    00170         mp32nzero(&kp->c);
    00171         */
    00172
    00173         return 0;
    00174 }
    00175 /*@=boundswrite@*/
    00176
    00177 int rsakpFree(rsakp* kp)
    00178 {
    00179         /*@-usereleased -compdef @*/ /* kp->param.{n,p,q}.modl is OK */
    00180         mp32bfree(&kp->n);
    00181         mp32nfree(&kp->e);
    00182         mp32nfree(&kp->d);
    00183         mp32bfree(&kp->p);
    00184         mp32bfree(&kp->q);
    00185         mp32nfree(&kp->d1);
    00186         mp32nfree(&kp->d2);
    00187         mp32nfree(&kp->c);
    00188
    00189         return 0;
    00190         /*@=usereleased =compdef @*/
    00191 }
    00192
    00193 int rsakpCopy(rsakp* dst, const rsakp* src)
    00194 {
    00195         mp32bcopy(&dst->n, &src->n);
    00196         mp32ncopy(&dst->e, &src->e);
    00197         mp32ncopy(&dst->d, &src->d);
    00198         mp32bcopy(&dst->p, &src->p);
    00199         mp32bcopy(&dst->q, &src->q);
    00200         mp32ncopy(&dst->d1, &src->d1);
    00201         mp32ncopy(&dst->d2, &src->d2);
    00202         mp32ncopy(&dst->c, &src->c);
    00203
    00204         return 0;
    00205 }