]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/ctaocrypt/src/dsa.c
Rename the CyaSSL directory to WolfSSL
[freertos] / FreeRTOS-Plus / Source / WolfSSL / ctaocrypt / src / dsa.c
1 /* dsa.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/ctaocrypt/settings.h>
27
28 #ifndef NO_DSA
29
30 #include <cyassl/ctaocrypt/dsa.h>
31 #include <cyassl/ctaocrypt/sha.h>
32 #include <cyassl/ctaocrypt/random.h>
33 #include <cyassl/ctaocrypt/error-crypt.h>
34
35
36 enum {
37     DSA_HALF_SIZE = 20,   /* r and s size  */
38     DSA_SIG_SIZE  = 40    /* signature size */
39 };
40
41
42 #ifndef min
43
44     static INLINE word32 min(word32 a, word32 b)
45     {
46         return a > b ? b : a;
47     }
48
49 #endif /* min */
50
51
52 void InitDsaKey(DsaKey* key)
53 {
54     key->type = -1;  /* haven't decided yet */
55
56 /* TomsFastMath doesn't use memory allocation */
57 #ifndef USE_FAST_MATH
58     key->p.dp = 0;   /* public  alloc parts */
59     key->q.dp = 0;    
60     key->g.dp = 0;    
61     key->y.dp = 0;    
62
63     key->x.dp = 0;   /* private alloc parts */
64 #endif
65 }
66
67
68 void FreeDsaKey(DsaKey* key)
69 {
70     (void)key;
71 /* TomsFastMath doesn't use memory allocation */
72 #ifndef USE_FAST_MATH
73     if (key->type == DSA_PRIVATE)
74         mp_clear(&key->x);
75     mp_clear(&key->y);
76     mp_clear(&key->g);
77     mp_clear(&key->q);
78     mp_clear(&key->p);
79 #endif
80 }
81
82
83 int DsaSign(const byte* digest, byte* out, DsaKey* key, RNG* rng)
84 {
85     mp_int k, kInv, r, s, H;
86     int    ret, sz;
87     byte   buffer[DSA_HALF_SIZE];
88
89     sz = min(sizeof(buffer), mp_unsigned_bin_size(&key->q)); 
90
91     /* generate k */
92     ret = RNG_GenerateBlock(rng, buffer, sz);
93     if (ret != 0)
94         return ret;
95
96     buffer[0] |= 0x0C;
97
98     if (mp_init_multi(&k, &kInv, &r, &s, &H, 0) != MP_OKAY)
99         return MP_INIT_E;
100
101     if (mp_read_unsigned_bin(&k, buffer, sz) != MP_OKAY)
102         ret = MP_READ_E;
103
104     if (ret == 0 && mp_cmp_d(&k, 1) != MP_GT)
105         ret = MP_CMP_E;
106
107     /* inverse k mod q */
108     if (ret == 0 && mp_invmod(&k, &key->q, &kInv) != MP_OKAY)
109         ret = MP_INVMOD_E;
110
111     /* generate r, r = (g exp k mod p) mod q */
112     if (ret == 0 && mp_exptmod(&key->g, &k, &key->p, &r) != MP_OKAY)
113         ret = MP_EXPTMOD_E;
114
115     if (ret == 0 && mp_mod(&r, &key->q, &r) != MP_OKAY)
116         ret = MP_MOD_E;
117
118     /* generate H from sha digest */
119     if (ret == 0 && mp_read_unsigned_bin(&H, digest,SHA_DIGEST_SIZE) != MP_OKAY)
120         ret = MP_READ_E;
121
122     /* generate s, s = (kInv * (H + x*r)) % q */
123     if (ret == 0 && mp_mul(&key->x, &r, &s) != MP_OKAY)
124         ret = MP_MUL_E;
125
126     if (ret == 0 && mp_add(&s, &H, &s) != MP_OKAY)
127         ret = MP_ADD_E;
128
129     if (ret == 0 && mp_mulmod(&s, &kInv, &key->q, &s) != MP_OKAY)
130         ret = MP_MULMOD_E;
131
132     /* write out */
133     if (ret == 0)  {
134         int rSz = mp_unsigned_bin_size(&r);
135         int sSz = mp_unsigned_bin_size(&s);
136
137         if (rSz == DSA_HALF_SIZE - 1) {
138             out[0] = 0;
139             out++;
140         }
141
142         if (mp_to_unsigned_bin(&r, out) != MP_OKAY)
143             ret = MP_TO_E;
144         else {
145             if (sSz == DSA_HALF_SIZE - 1) {
146                 out[rSz] = 0;
147                 out++;
148             }    
149             ret = mp_to_unsigned_bin(&s, out + rSz);
150         }
151     }
152
153     mp_clear(&H);
154     mp_clear(&s);
155     mp_clear(&r);
156     mp_clear(&kInv);
157     mp_clear(&k);
158
159     return ret;
160 }
161
162
163 int DsaVerify(const byte* digest, const byte* sig, DsaKey* key, int* answer)
164 {
165     mp_int w, u1, u2, v, r, s;
166     int    ret = 0;
167
168     if (mp_init_multi(&w, &u1, &u2, &v, &r, &s) != MP_OKAY)
169         return MP_INIT_E;
170
171     /* set r and s from signature */
172     if (mp_read_unsigned_bin(&r, sig, DSA_HALF_SIZE) != MP_OKAY ||
173         mp_read_unsigned_bin(&s, sig + DSA_HALF_SIZE, DSA_HALF_SIZE) != MP_OKAY)
174         ret = MP_READ_E;
175
176     /* sanity checks */
177
178
179     /* put H into u1 from sha digest */
180     if (ret == 0 && mp_read_unsigned_bin(&u1,digest,SHA_DIGEST_SIZE) != MP_OKAY)
181         ret = MP_READ_E;
182
183     /* w = s invmod q */
184     if (ret == 0 && mp_invmod(&s, &key->q, &w) != MP_OKAY)
185         ret = MP_INVMOD_E;
186
187     /* u1 = (H * w) % q */
188     if (ret == 0 && mp_mulmod(&u1, &w, &key->q, &u1) != MP_OKAY)
189         ret = MP_MULMOD_E;
190
191     /* u2 = (r * w) % q */
192     if (ret == 0 && mp_mulmod(&r, &w, &key->q, &u2) != MP_OKAY)
193         ret = MP_MULMOD_E;
194
195     /* verify v = ((g^u1 * y^u2) mod p) mod q */
196     if (ret == 0 && mp_exptmod(&key->g, &u1, &key->p, &u1) != MP_OKAY)
197         ret = MP_EXPTMOD_E;
198
199     if (ret == 0 && mp_exptmod(&key->y, &u2, &key->p, &u2) != MP_OKAY)
200         ret = MP_EXPTMOD_E;
201
202     if (ret == 0 && mp_mulmod(&u1, &u2, &key->p, &v) != MP_OKAY)
203         ret = MP_MULMOD_E;
204
205     if (ret == 0 && mp_mod(&v, &key->q, &v) != MP_OKAY)
206         ret = MP_MULMOD_E;
207
208     /* do they match */
209     if (ret == 0 && mp_cmp(&r, &v) == MP_EQ)
210         *answer = 1;
211     else
212         *answer = 0;
213
214     mp_clear(&s);
215     mp_clear(&r);
216     mp_clear(&u1);
217     mp_clear(&u2);
218     mp_clear(&w);
219     mp_clear(&v);
220
221     return ret;
222 }
223
224
225 #endif /* NO_DSA */
226