Commit | Line | Data |
---|---|---|
8b810a45 | 1 | /* -*-c-*- |
2 | * | |
8b810a45 | 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 | ||
141c1284 | 122 | #include <mLib/macros.h> |
8b810a45 | 123 | #include <mLib/testrig.h> |
124 | ||
125 | #include "sha.h" | |
126 | ||
127 | static int verify(dstr *v) | |
128 | { | |
129 | dsa_param dp; | |
130 | mp *x; | |
131 | sha_ctx c; | |
132 | octet hash[SHA_HASHSZ]; | |
133 | dsa_sig s; | |
134 | int ok = 1; | |
135 | ||
136 | dp.q = *(mp **)v[0].buf; | |
137 | dp.p = *(mp **)v[1].buf; | |
138 | dp.g = *(mp **)v[2].buf; | |
139 | x = *(mp **)v[3].buf; | |
140 | ||
141 | sha_init(&c); | |
142 | sha_hash(&c, v[4].buf, v[4].len); | |
143 | sha_done(&c, hash); | |
144 | ||
145 | dsa_sign(&dp, x, hash, sizeof(hash), v[5].buf, v[5].len, | |
146 | s.r, sizeof(s.r), s.s, sizeof(s.s)); | |
147 | ||
148 | if (v[6].len != sizeof(s.r) || v[7].len != sizeof(s.s) || | |
141c1284 MW |
149 | MEMCMP(s.r, !=, v[6].buf, sizeof(s.r)) || |
150 | MEMCMP(s.s, !=, v[7].buf, sizeof(s.s))) { | |
8b810a45 | 151 | fputs("\n*** signature failed", stderr); |
152 | fputs("\nq = ", stderr); mp_writefile(dp.q, stderr, 16); | |
153 | fputs("\np = ", stderr); mp_writefile(dp.p, stderr, 16); | |
154 | fputs("\ng = ", stderr); mp_writefile(dp.g, stderr, 16); | |
155 | fputs("\nx = ", stderr); mp_writefile(x, stderr, 16); | |
156 | fprintf(stderr, "\nmessage = `%s'", v[4].buf); | |
157 | fputs("\nk = ", stderr); type_hex.dump(&v[5], stderr); | |
158 | fputs("\nR = ", stderr); type_hex.dump(&v[6], stderr); | |
159 | fputs("\nS = ", stderr); type_hex.dump(&v[7], stderr); | |
160 | ||
161 | { | |
162 | mp *m = MP_NEW; | |
163 | m = mp_loadb(m, hash, sizeof(hash)); | |
164 | fputs("\nm = ", stderr); mp_writefile(m, stderr, 16); | |
165 | m = mp_loadb(m, s.r, sizeof(s.r)); | |
166 | fputs("\nr = ", stderr); mp_writefile(m, stderr, 16); | |
167 | m = mp_loadb(m, s.s, sizeof(s.s)); | |
168 | fputs("\ns = ", stderr); mp_writefile(m, stderr, 16); | |
169 | mp_drop(m); | |
170 | } | |
45c0fd36 | 171 | |
8b810a45 | 172 | fputc('\n', stderr); |
173 | ok = 0; | |
174 | } | |
175 | ||
176 | mp_drop(dp.p); | |
177 | mp_drop(dp.q); | |
178 | mp_drop(dp.g); | |
179 | mp_drop(x); | |
ef5f4810 | 180 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
8b810a45 | 181 | return (ok); |
182 | } | |
183 | ||
184 | static test_chunk tests[] = { | |
185 | { "sign", verify, | |
186 | { &type_mp, &type_mp, &type_mp, &type_mp, | |
187 | &type_string, &type_hex, &type_hex, &type_hex, 0 } }, | |
188 | { 0, 0, { 0 } } | |
189 | }; | |
190 | ||
191 | int main(int argc, char *argv[]) | |
192 | { | |
193 | sub_init(); | |
0f00dc4c | 194 | test_run(argc, argv, tests, SRCDIR "/t/dsa"); |
8b810a45 | 195 | return (0); |
196 | } | |
197 | ||
198 | #endif | |
199 | ||
200 | /*----- That's all, folks -------------------------------------------------*/ |