8b810a45 |
1 | /* -*-c-*- |
8b810a45 |
2 | * |
3 | * DSA signing operation |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
8b810a45 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
8b810a45 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
8b810a45 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
8b810a45 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
92381410 |
30 | #define CATACOMB_DSAIMPL |
8b810a45 |
31 | #include "dsa.h" |
32 | #include "mp.h" |
ef5f4810 |
33 | #include "mpbarrett.h" |
8b810a45 |
34 | #include "mpmont.h" |
35 | |
36 | /*----- Main code ---------------------------------------------------------*/ |
37 | |
38 | /* --- @dsa_mksig@ --- * |
39 | * |
40 | * Arguments: @const dsa_param *dp@ = pointer to DSA parameters |
ef5f4810 |
41 | * @mp *a@ = secret signing key |
42 | * @mp *m@ = message to be signed |
43 | * @mp *k@ = random data |
8b810a45 |
44 | * @mp **rr, **ss@ = where to put output parameters |
45 | * |
46 | * Returns: --- |
47 | * |
48 | * Use: Computes a DSA signature of a message. |
49 | */ |
50 | |
ef5f4810 |
51 | void dsa_mksig(const dsa_param *dp, mp *a, mp *m, mp *k, mp **rr, mp **ss) |
8b810a45 |
52 | { |
ef5f4810 |
53 | mpmont pm; |
54 | mpbarrett qb; |
b817bfc6 |
55 | mp *k1, *r; |
ef5f4810 |
56 | mp *ar; |
8b810a45 |
57 | |
58 | /* --- Compute %$r = (g^k \bmod p) \bmod q$% --- */ |
59 | |
ef5f4810 |
60 | mpmont_create(&pm, dp->p); |
61 | r = mpmont_exp(&pm, MP_NEW, dp->g, k); |
62 | mpmont_destroy(&pm); |
8b810a45 |
63 | mp_div(0, &r, r, dp->q); |
8b810a45 |
64 | |
65 | /* --- Compute %$k^{-1} \bmod q$% --- */ |
66 | |
b817bfc6 |
67 | k1 = mp_modinv(MP_NEW, k, dp->q); |
8b810a45 |
68 | |
69 | /* --- Now for %$k^{-1}(m + ar)$% --- */ |
70 | |
ef5f4810 |
71 | mpbarrett_create(&qb, dp->q); |
72 | ar = mp_mul(MP_NEW, a, r); |
8b810a45 |
73 | ar = mp_add(ar, ar, m); |
ef5f4810 |
74 | ar = mpbarrett_reduce(&qb, ar, ar); |
75 | ar = mp_mul(ar, ar, k1); |
76 | ar = mpbarrett_reduce(&qb, ar, ar); |
77 | mpbarrett_destroy(&qb); |
78 | MP_DROP(k1); |
79 | if (*rr) MP_DROP(*rr); |
80 | if (*ss) MP_DROP(*ss); |
81 | *rr = r; |
8b810a45 |
82 | *ss = ar; |
8b810a45 |
83 | } |
84 | |
85 | /* --- @dsa_sign@ --- * |
86 | * |
87 | * Arguments: @dsa_param *dp@ = pointer to DSA parameters |
88 | * @mp *a@ = pointer to secret signing key |
89 | * @const void *m@ = pointer to message |
90 | * @size_t msz@ = size of the message |
91 | * @const void *k@ = secret random data for securing signature |
92 | * @size_t ksz@ = size of secret data |
93 | * @void *r@ = pointer to output space for @r@ |
94 | * @size_t rsz@ = size of output space for @r@ |
95 | * @void *s@ = pointer to output space for @s@ |
96 | * @size_t ssz@ = size of output space for @s@ |
97 | * |
98 | * Returns: --- |
99 | * |
100 | * Use: Signs a message, storing the results in a big-endian binary |
101 | * form. |
102 | */ |
103 | |
104 | void dsa_sign(dsa_param *dp, mp *a, |
105 | const void *m, size_t msz, const void *k, size_t ksz, |
106 | void *r, size_t rsz, void *s, size_t ssz) |
107 | { |
c97fbcf9 |
108 | mp *mm = dsa_h2n(MP_NEW, dp->q, m, msz); |
8b810a45 |
109 | mp *km = mp_loadb(MP_NEW, k, ksz); |
ef5f4810 |
110 | mp *rm = MP_NEW, *sm = MP_NEW; |
8b810a45 |
111 | dsa_mksig(dp, a, mm, km, &rm, &sm); |
112 | mp_storeb(rm, r, rsz); |
113 | mp_storeb(sm, s, ssz); |
114 | mp_drop(mm); mp_drop(km); |
115 | mp_drop(rm); mp_drop(sm); |
116 | } |
117 | |
118 | /*----- Test rig ----------------------------------------------------------*/ |
119 | |
120 | #ifdef TEST_RIG |
121 | |
122 | #include <mLib/testrig.h> |
123 | |
124 | #include "sha.h" |
125 | |
126 | static int verify(dstr *v) |
127 | { |
128 | dsa_param dp; |
129 | mp *x; |
130 | sha_ctx c; |
131 | octet hash[SHA_HASHSZ]; |
132 | dsa_sig s; |
133 | int ok = 1; |
134 | |
135 | dp.q = *(mp **)v[0].buf; |
136 | dp.p = *(mp **)v[1].buf; |
137 | dp.g = *(mp **)v[2].buf; |
138 | x = *(mp **)v[3].buf; |
139 | |
140 | sha_init(&c); |
141 | sha_hash(&c, v[4].buf, v[4].len); |
142 | sha_done(&c, hash); |
143 | |
144 | dsa_sign(&dp, x, hash, sizeof(hash), v[5].buf, v[5].len, |
145 | s.r, sizeof(s.r), s.s, sizeof(s.s)); |
146 | |
147 | if (v[6].len != sizeof(s.r) || v[7].len != sizeof(s.s) || |
148 | memcmp(s.r, v[6].buf, sizeof(s.r)) != 0 || |
149 | memcmp(s.s, v[7].buf, sizeof(s.s)) != 0) { |
150 | fputs("\n*** signature failed", stderr); |
151 | fputs("\nq = ", stderr); mp_writefile(dp.q, stderr, 16); |
152 | fputs("\np = ", stderr); mp_writefile(dp.p, stderr, 16); |
153 | fputs("\ng = ", stderr); mp_writefile(dp.g, stderr, 16); |
154 | fputs("\nx = ", stderr); mp_writefile(x, stderr, 16); |
155 | fprintf(stderr, "\nmessage = `%s'", v[4].buf); |
156 | fputs("\nk = ", stderr); type_hex.dump(&v[5], stderr); |
157 | fputs("\nR = ", stderr); type_hex.dump(&v[6], stderr); |
158 | fputs("\nS = ", stderr); type_hex.dump(&v[7], stderr); |
159 | |
160 | { |
161 | mp *m = MP_NEW; |
162 | m = mp_loadb(m, hash, sizeof(hash)); |
163 | fputs("\nm = ", stderr); mp_writefile(m, stderr, 16); |
164 | m = mp_loadb(m, s.r, sizeof(s.r)); |
165 | fputs("\nr = ", stderr); mp_writefile(m, stderr, 16); |
166 | m = mp_loadb(m, s.s, sizeof(s.s)); |
167 | fputs("\ns = ", stderr); mp_writefile(m, stderr, 16); |
168 | mp_drop(m); |
169 | } |
45c0fd36 |
170 | |
8b810a45 |
171 | fputc('\n', stderr); |
172 | ok = 0; |
173 | } |
174 | |
175 | mp_drop(dp.p); |
176 | mp_drop(dp.q); |
177 | mp_drop(dp.g); |
178 | mp_drop(x); |
ef5f4810 |
179 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
8b810a45 |
180 | return (ok); |
181 | } |
182 | |
183 | static test_chunk tests[] = { |
184 | { "sign", verify, |
185 | { &type_mp, &type_mp, &type_mp, &type_mp, |
186 | &type_string, &type_hex, &type_hex, &type_hex, 0 } }, |
187 | { 0, 0, { 0 } } |
188 | }; |
189 | |
190 | int main(int argc, char *argv[]) |
191 | { |
192 | sub_init(); |
0f00dc4c |
193 | test_run(argc, argv, tests, SRCDIR "/t/dsa"); |
8b810a45 |
194 | return (0); |
195 | } |
196 | |
197 | #endif |
198 | |
199 | /*----- That's all, folks -------------------------------------------------*/ |