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

beecrypt/rsa.c

Go to the documentation of this file.
00001 
00007 /*
00008  * Copyright (c) 2000, 2001 Virtual Unlimited B.V.
00009  *
00010  * Author: Bob Deblier <bob@virtualunlimited.com>
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with this library; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  *
00026  */
00027 
00028 #include "system.h"
00029 #include "rsa.h"
00030 #include "mp32.h"
00031 #include "debug.h"
00032 
00033 int rsapri(const rsakp* kp, const mp32number* m, mp32number* c)
00034 {
00035         register uint32  size = kp->n.size;
00036         register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(*temp));
00037 
00038         if (temp)
00039         {
00040                 mp32nsize(c, size);
00041                 mp32bpowmod_w(&kp->n, m->size, m->data, kp->d.size, kp->d.data, c->data, temp);
00042 
00043                 free(temp);
00044 
00045                 return 0;
00046         }
00047         return -1;
00048 }
00049 
00050 
00051 int rsapricrt(const rsakp* kp, const mp32number* m, mp32number* c)
00052 {
00053         register uint32  nsize = kp->n.size;
00054         register uint32  psize = kp->p.size;
00055         register uint32  qsize = kp->q.size;
00056 
00057         register uint32* ptemp;
00058         register uint32* qtemp;
00059 
00060         /* m must be small enough to be exponentiated modulo p and q */
00061         if (m->size > psize || m->size > qsize)
00062                 return -1;
00063 
00064         ptemp = (uint32*) malloc((6*psize+2)*sizeof(*ptemp));
00065         if (ptemp == NULL)
00066                 return -1;
00067 
00068         qtemp = (uint32*) malloc((6*qsize+2)*sizeof(*qtemp));
00069         if (qtemp == NULL)
00070         {
00071                 free(ptemp);
00072                 return -1;
00073         }
00074 
00075         /* resize m for powmod p */
00076         mp32setx(psize, ptemp+psize, m->size, m->data);
00077 
00078         /* compute j1 = m^d1 mod p, store @ ptemp */
00079 /*@-compdef@*/
00080         mp32bpowmod_w(&kp->p, psize, ptemp+psize, kp->d1.size, kp->d1.data, ptemp, ptemp+2*psize);
00081 
00082         /* resize m for powmod p */
00083         mp32setx(qsize, qtemp+psize, m->size, m->data);
00084 
00085         /* compute j2 = m^d2 mod q, store @ qtemp */
00086         mp32bpowmod_w(&kp->q, qsize, qtemp+psize, kp->d2.size, kp->d2.data, qtemp, qtemp+2*qsize);
00087 /*@=compdef@*/
00088 
00089         /* compute j1-j2 mod p, store @ ptemp */
00090         mp32bsubmod_w(&kp->p, psize, ptemp, qsize, qtemp, ptemp, ptemp+2*psize);
00091 
00092         /* compute h = c*(j1-j2) mod p, store @ ptemp */
00093         mp32bmulmod_w(&kp->p, psize, ptemp, psize, kp->c.data, ptemp, ptemp+2*psize);
00094 
00095         /* make sure the signature gets the proper size */
00096         mp32nsize(c, nsize);
00097 
00098         /* compute s = h*q + j2 */
00099         mp32mul(c->data, psize, ptemp, qsize, kp->q.modl);
00100         (void) mp32addx(nsize, c->data, qsize, qtemp);
00101 
00102         free(ptemp);
00103         free(qtemp);
00104 
00105         return 0;
00106 }
00107 
00111 int rsavrfy(const rsapk* pk, const mp32number* m, const mp32number* c)
00112 {
00113         int rc;
00114         register uint32  size = pk->n.size;
00115         register uint32* temp = (uint32*) malloc((5*size+2) * sizeof(*temp));
00116 
00117         if (temp)
00118         {
00119                 mp32bpowmod_w(&pk->n, c->size, c->data, pk->e.size, pk->e.data, temp, temp+size);
00120 
00121                 rc = mp32eqx(size, temp, m->size, m->data);
00122 
00123                 free(temp);
00124 
00125                 return rc;
00126         }
00127         return 0;
00128 }

Generated on Tue Sep 17 15:56:37 2002 for rpm by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002