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

# beecrypt/elgamal.c

Go to the documentation of this file.

    00001
    00037 /*
    00038  * Copyright (c) 1999, 2000, 2001 Virtual Unlimited B.V.
    00039  *
    00040  * Author: Bob Deblier <bob@virtualunlimited.com>
    00041  *
    00042  * This library is free software; you can redistribute it and/or
    00043  * modify it under the terms of the GNU Lesser General Public
    00044  * License as published by the Free Software Foundation; either
    00045  * version 2.1 of the License, or (at your option) any later version.
    00046  *
    00047  * This library is distributed in the hope that it will be useful,
    00048  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00049  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00050  * Lesser General Public License for more details.
    00051  *
    00052  * You should have received a copy of the GNU Lesser General Public
    00053  * License along with this library; if not, write to the Free Software
    00054  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00055  */
    00056
    00057 #include "system.h"
    00058 #include "elgamal.h"
    00059 #include "dldp.h"
    00060 #include "mp32.h"
    00061 #include "debug.h"
    00062
    00063 int elgv1sign(const mp32barrett* p, const mp32barrett* n, const mp32number* g, randomGeneratorContext* rgc, const mp32number* hm, const mp32number* x, mp32number* r, mp32number* s)
    00064 {
    00065         register uint32  size = p->size;
    00066         register uint32* temp = (uint32*) malloc((13*size+11) * sizeof(*temp));
    00067
    00068         if (temp)
    00069         {
    00070                 /* get a random k, invertible modulo (p-1) */
    00071                 mp32brndinv_w(n, rgc, temp, temp+size, temp+2*size);
    00072
    00073                 /* compute r = g^k mod p */
    00074                 mp32nfree(r);
    00075                 mp32nsize(r, size);
    00076                 mp32bpowmod_w(p, g->size, g->data, size, temp, r->data, temp+2*size);
    00077
    00078                 /* compute x*r mod n */
    00079                 mp32bmulmod_w(n, x->size, x->data, r->size, r->data, temp, temp+2*size);
    00080
    00081                 /* compute -(x*r) mod n */
    00082                 mp32neg(size, temp);
    00083                 (void) mp32add(size, temp, n->modl);
    00084
    00085                 /* compute h(m) - x*r mod n */
    00086                 mp32baddmod_w(n, hm->size, hm->data, size, temp, temp, temp+2*size);
    00087
    00088                 /* compute s = inv(k)*(h(m) - x*r) mod n */
    00089                 mp32nfree(s);
    00090                 mp32nsize(s, size);
    00091                 mp32bmulmod_w(n, size, temp, size, temp+size, s->data, temp+2*size);
    00092
    00093                 free(temp);
    00094
    00095                 return 0;
    00096         }
    00097         return -1;
    00098 }
    00099
    00100 int elgv1vrfy(const mp32barrett* p, const mp32barrett* n, const mp32number* g, const mp32number* hm, const mp32number* y, const mp32number* r, const mp32number* s)
    00101 {
    00102         register uint32  size = p->size;
    00103         register uint32* temp;
    00104
    00105         if (mp32z(r->size, r->data))
    00106                 return 0;
    00107
    00108         if (mp32gex(r->size, r->data, size, p->modl))
    00109                 return 0;
    00110
    00111         if (mp32z(s->size, s->data))
    00112                 return 0;
    00113
    00114         if (mp32gex(s->size, s->data, n->size, n->modl))
    00115                 return 0;
    00116
    00117         temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
    00118
    00119         if (temp)
    00120         {
    00121                 register int rc;
    00122
    00123                 /* compute u1 = y^r mod p */
    00124                 mp32bpowmod_w(p, y->size, y->data, r->size, r->data, temp, temp+2*size);
    00125
    00126                 /* compute u2 = r^s mod p */
    00127                 mp32bpowmod_w(p, r->size, r->data, s->size, s->data, temp+size, temp+2*size);
    00128
    00129                 /* compute v2 = u1*u2 mod p */
    00130                 mp32bmulmod_w(p, size, temp, size, temp+size, temp+size, temp+2*size);
    00131
    00132                 /* compute v1 = g^h(m) mod p */
    00133                 mp32bpowmod_w(p, g->size, g->data, hm->size, hm->data, temp, temp+2*size);
    00134
    00135                 rc = mp32eq(size, temp, temp+size);
    00136
    00137                 free(temp);
    00138
    00139                 return rc;
    00140         }
    00141         return 0;
    00142 }
    00143
    00144 int elgv3sign(const mp32barrett* p, const mp32barrett* n, const mp32number* g, randomGeneratorContext* rgc, const mp32number* hm, const mp32number* x, mp32number* r, mp32number* s)
    00145 {
    00146         register uint32  size = p->size;
    00147         register uint32* temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
    00148
    00149         if (temp)
    00150         {
    00151                 /* get a random k */
    00152                 mp32brnd_w(p, rgc, temp, temp+2*size);
    00153
    00154                 /* compute r = g^k mod p */
    00155                 mp32nfree(r);
    00156                 mp32nsize(r, size);
    00157                 mp32bpowmod_w(p, g->size, g->data, size, temp, r->data, temp+2*size);
    00158
    00159                 /* compute u1 = x*r mod n */
    00160                 mp32bmulmod_w(n, x->size, x->data, size, r->data, temp+size, temp+2*size);
    00161
    00162                 /* compute u2 = k*h(m) mod n */
    00163                 mp32bmulmod_w(n, size, temp, hm->size, hm->data, temp, temp+2*size);
    00164
    00165                 /* compute s = u1+u2 mod n */
    00166                 mp32nfree(s);
    00167                 mp32nsize(s, n->size);
    00168                 mp32baddmod_w(n, size, temp, size, temp+size, s->data, temp+2*size);
    00169
    00170                 free(temp);
    00171
    00172                 return 0;
    00173         }
    00174         return -1;
    00175 }
    00176
    00177 int elgv3vrfy(const mp32barrett* p, const mp32barrett* n, const mp32number* g, const mp32number* hm, const mp32number* y, const mp32number* r, const mp32number* s)
    00178 {
    00179         register uint32  size = p->size;
    00180         register uint32* temp;
    00181
    00182         if (mp32z(r->size, r->data))
    00183                 return 0;
    00184
    00185         if (mp32gex(r->size, r->data, size, p->modl))
    00186                 return 0;
    00187
    00188         if (mp32z(s->size, s->data))
    00189                 return 0;
    00190
    00191         if (mp32gex(s->size, s->data, n->size, n->modl))
    00192                 return 0;
    00193
    00194         temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
    00195
    00196         if (temp)
    00197         {
    00198                 register int rc;
    00199
    00200                 /* compute u1 = y^r mod p */
    00201                 mp32bpowmod_w(p, y->size, y->data, r->size, r->data, temp, temp+2*size);
    00202
    00203                 /* compute u2 = r^h(m) mod p */
    00204                 mp32bpowmod_w(p, r->size, r->data, hm->size, hm->data, temp+size, temp+2*size);
    00205
    00206                 /* compute v2 = u1*u2 mod p */
    00207                 mp32bmulmod_w(p, size, temp, size, temp+size, temp+size, temp+2*size);
    00208
    00209                 /* compute v1 = g^s mod p */
    00210                 mp32bpowmod_w(p, g->size, g->data, s->size, s->data, temp, temp+2*size);
    00211
    00212                 rc = mp32eq(size, temp, temp+size);
    00213
    00214                 free(temp);
    00215
    00216                 return rc;
    00217         }
    00218         return 0;
    00219 }