1 /*
2 * Digital Signature Standard (DSS)
3 *
4 * Elliptic Curve Variation GF(2^m) - See Dr. Dobbs Journal April 1997
5 *
6 * This program verifies the signature given to a <file> in
7 * <file>.ecs generated by program ecsign2
8 *
9 * The curve is y^2+xy = x^3+Ax^2+B over GF(2^m) using a trinomial or
10 * pentanomial basis (t^m+t^a+1 or t^m+t^a+t^b+t^c+1), These parameters
11 * can be generated using the findbase.cpp example program, or taken from tables
12 * provided, for example in IEEE-P1363 Annex A
13 *
14 * The file common2.ecs is presumed to exist and contain
15 * {m,A,B,q,x,y,a,b,c} where A and B are parameters of the equation
16 * above, (x,y) is an initial point on the curve, {m,a,b,c} are the field
17 * parameters, (b is zero for a trinomial) and q is the order of the
18 * (x,y) point, itself a large prime. The number of points on the curve is
19 * cf.q where cf is the "co-factor", normally 2 or 4.
20 *
21 * This program is written for static mode.
22 * For a 163-bit modulus p, MR_STATIC could be defined as 6 in mirdef.h
23 * for a 32-bit processor, or 11 for a 16-bit processor (11*16 > 163).
24 * The system parameters can be found in the file common2.ecs
25 * Assumes MR_GENERIC_MT is defined in mirdef.h
26 */
27
28 #include <stdio.h>
29 #include "miracl.h"
30 #include <stdlib.h>
31 #include <string.h>
32
strip(char * name)33 void strip(char *name)
34 { /* strip off filename extension */
35 int i;
36 for (i=0;name[i]!='\0';i++)
37 {
38 if (name[i]!='.') continue;
39 name[i]='\0';
40 break;
41 }
42 }
43
hashing(miracl * mip,FILE * fp,big hash)44 static void hashing(miracl *mip,FILE *fp,big hash)
45 { /* compute hash function */
46 char h[20];
47 sha sh;
48 int i,ch;
49 shs_init(&sh);
50 while ((ch=fgetc(fp))!=EOF) shs_process(&sh,ch);
51 shs_hash(&sh,h);
52 bytes_to_big(mip,20,h,hash);
53 }
54
main()55 int main()
56 {
57 FILE *fp;
58 int ep,m,a,b,c;
59 epoint *g,*public;
60 char ifname[50],ofname[50];
61 big a2,a6,q,x,y,v,u1,u2,r,s,hash;
62 miracl instance;
63 miracl *mip=&instance;
64 char mem[MR_BIG_RESERVE(11)]; /* reserve space on the stack for 11 bigs */
65 char mem1[MR_ECP_RESERVE(2)]; /* and two elliptic curve points */
66 memset(mem,0,MR_BIG_RESERVE(11));
67 memset(mem1,0,MR_ECP_RESERVE(2));
68
69 /* get public data */
70 fp=fopen("common2.ecs","rt");
71 if (fp==NULL)
72 {
73 printf("file common2.ecs does not exist\n");
74 return 0;
75 }
76 fscanf(fp,"%d\n",&m);
77
78 mip=mirsys(mip,MR_ROUNDUP(abs(m),4),16);
79 a2=mirvar_mem(mip,mem,0);
80 a6=mirvar_mem(mip,mem,1);
81 q=mirvar_mem(mip,mem,2);
82 x=mirvar_mem(mip,mem,3);
83 y=mirvar_mem(mip,mem,4);
84 v=mirvar_mem(mip,mem,5);
85 u1=mirvar_mem(mip,mem,6);
86 u2=mirvar_mem(mip,mem,7);
87 s=mirvar_mem(mip,mem,8);
88 r=mirvar_mem(mip,mem,9);
89 hash=mirvar_mem(mip,mem,10);
90
91 innum(mip,a2,fp);
92 innum(mip,a6,fp);
93 innum(mip,q,fp);
94 innum(mip,x,fp);
95 innum(mip,y,fp);
96
97 fscanf(fp,"%d\n",&a);
98 fscanf(fp,"%d\n",&b);
99 fscanf(fp,"%d\n",&c);
100
101 fclose(fp);
102
103 ecurve2_init(mip,m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE); /* initialise curve */
104 g=epoint_init_mem(mip,mem1,0);
105 epoint2_set(mip,x,y,0,g); /* initialise point of order q */
106
107 /* get public key of signer */
108 fp=fopen("public.ecs","rt");
109 if (fp==NULL)
110 {
111 printf("file public.ecs does not exist\n");
112 return 0;
113 }
114 fscanf(fp,"%d",&ep);
115 innum(mip,x,fp);
116 fclose(fp);
117
118 public=epoint_init_mem(mip,mem1,1);
119 epoint2_set(mip,x,x,ep,public); /* decompress */
120
121 /* get message */
122 printf("signed file = ");
123 gets(ifname);
124 strcpy(ofname,ifname);
125 strip(ofname);
126 strcat(ofname,".ecs");
127 if ((fp=fopen(ifname,"rb"))==NULL)
128 { /* no message */
129 printf("Unable to open file %s\n",ifname);
130 return 0;
131 }
132 hashing(mip,fp,hash);
133 fclose(fp);
134 fp=fopen(ofname,"rt");
135 if (fp==NULL)
136 { /* no signature */
137 printf("signature file %s does not exist\n",ofname);
138 return 0;
139 }
140 innum(mip,r,fp);
141 innum(mip,s,fp);
142 fclose(fp);
143 if (compare(r,q)>=0 || compare(s,q)>=0)
144 {
145 printf("Signature is NOT verified\n");
146 return 0;
147 }
148 xgcd(mip,s,q,s,s,s);
149 mad(mip,hash,s,s,q,q,u1);
150 mad(mip,r,s,s,q,q,u2);
151
152 ecurve2_mult2(mip,u2,public,u1,g,g);
153 epoint2_get(mip,g,v,v);
154 divide(mip,v,q,q);
155 if (compare(v,r)==0) printf("Signature is verified\n");
156 else printf("Signature is NOT verified\n");
157 /* clear all memory used */
158 memset(mem,0,MR_BIG_RESERVE(11));
159 memset(mem1,0,MR_ECP_RESERVE(2));
160
161 return 0;
162 }
163
164