| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * DSA signing operation |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include "dsa.h" |
| 31 | #include "mp.h" |
| 32 | #include "mpbarrett.h" |
| 33 | #include "mpmont.h" |
| 34 | |
| 35 | /*----- Main code ---------------------------------------------------------*/ |
| 36 | |
| 37 | /* --- @dsa_mksig@ --- * |
| 38 | * |
| 39 | * Arguments: @const dsa_param *dp@ = pointer to DSA parameters |
| 40 | * @mp *a@ = secret signing key |
| 41 | * @mp *m@ = message to be signed |
| 42 | * @mp *k@ = random data |
| 43 | * @mp **rr, **ss@ = where to put output parameters |
| 44 | * |
| 45 | * Returns: --- |
| 46 | * |
| 47 | * Use: Computes a DSA signature of a message. |
| 48 | */ |
| 49 | |
| 50 | void dsa_mksig(const dsa_param *dp, mp *a, mp *m, mp *k, mp **rr, mp **ss) |
| 51 | { |
| 52 | mpmont pm; |
| 53 | mpbarrett qb; |
| 54 | mp *k1, *r; |
| 55 | mp *ar; |
| 56 | |
| 57 | /* --- Compute %$r = (g^k \bmod p) \bmod q$% --- */ |
| 58 | |
| 59 | mpmont_create(&pm, dp->p); |
| 60 | r = mpmont_exp(&pm, MP_NEW, dp->g, k); |
| 61 | mpmont_destroy(&pm); |
| 62 | mp_div(0, &r, r, dp->q); |
| 63 | |
| 64 | /* --- Compute %$k^{-1} \bmod q$% --- */ |
| 65 | |
| 66 | k1 = mp_modinv(MP_NEW, k, dp->q); |
| 67 | |
| 68 | /* --- Now for %$k^{-1}(m + ar)$% --- */ |
| 69 | |
| 70 | mpbarrett_create(&qb, dp->q); |
| 71 | ar = mp_mul(MP_NEW, a, r); |
| 72 | ar = mp_add(ar, ar, m); |
| 73 | ar = mpbarrett_reduce(&qb, ar, ar); |
| 74 | ar = mp_mul(ar, ar, k1); |
| 75 | ar = mpbarrett_reduce(&qb, ar, ar); |
| 76 | mpbarrett_destroy(&qb); |
| 77 | MP_DROP(k1); |
| 78 | if (*rr) MP_DROP(*rr); |
| 79 | if (*ss) MP_DROP(*ss); |
| 80 | *rr = r; |
| 81 | *ss = ar; |
| 82 | } |
| 83 | |
| 84 | /* --- @dsa_sign@ --- * |
| 85 | * |
| 86 | * Arguments: @dsa_param *dp@ = pointer to DSA parameters |
| 87 | * @mp *a@ = pointer to secret signing key |
| 88 | * @const void *m@ = pointer to message |
| 89 | * @size_t msz@ = size of the message |
| 90 | * @const void *k@ = secret random data for securing signature |
| 91 | * @size_t ksz@ = size of secret data |
| 92 | * @void *r@ = pointer to output space for @r@ |
| 93 | * @size_t rsz@ = size of output space for @r@ |
| 94 | * @void *s@ = pointer to output space for @s@ |
| 95 | * @size_t ssz@ = size of output space for @s@ |
| 96 | * |
| 97 | * Returns: --- |
| 98 | * |
| 99 | * Use: Signs a message, storing the results in a big-endian binary |
| 100 | * form. |
| 101 | */ |
| 102 | |
| 103 | void dsa_sign(dsa_param *dp, mp *a, |
| 104 | const void *m, size_t msz, const void *k, size_t ksz, |
| 105 | void *r, size_t rsz, void *s, size_t ssz) |
| 106 | { |
| 107 | mp *mm = dsa_h2n(MP_NEW, dp->q, m, msz); |
| 108 | mp *km = mp_loadb(MP_NEW, k, ksz); |
| 109 | mp *rm = MP_NEW, *sm = MP_NEW; |
| 110 | dsa_mksig(dp, a, mm, km, &rm, &sm); |
| 111 | mp_storeb(rm, r, rsz); |
| 112 | mp_storeb(sm, s, ssz); |
| 113 | mp_drop(mm); mp_drop(km); |
| 114 | mp_drop(rm); mp_drop(sm); |
| 115 | } |
| 116 | |
| 117 | /*----- Test rig ----------------------------------------------------------*/ |
| 118 | |
| 119 | #ifdef TEST_RIG |
| 120 | |
| 121 | #include <mLib/testrig.h> |
| 122 | |
| 123 | #include "sha.h" |
| 124 | |
| 125 | static int verify(dstr *v) |
| 126 | { |
| 127 | dsa_param dp; |
| 128 | mp *x; |
| 129 | sha_ctx c; |
| 130 | octet hash[SHA_HASHSZ]; |
| 131 | dsa_sig s; |
| 132 | int ok = 1; |
| 133 | |
| 134 | dp.q = *(mp **)v[0].buf; |
| 135 | dp.p = *(mp **)v[1].buf; |
| 136 | dp.g = *(mp **)v[2].buf; |
| 137 | x = *(mp **)v[3].buf; |
| 138 | |
| 139 | sha_init(&c); |
| 140 | sha_hash(&c, v[4].buf, v[4].len); |
| 141 | sha_done(&c, hash); |
| 142 | |
| 143 | dsa_sign(&dp, x, hash, sizeof(hash), v[5].buf, v[5].len, |
| 144 | s.r, sizeof(s.r), s.s, sizeof(s.s)); |
| 145 | |
| 146 | if (v[6].len != sizeof(s.r) || v[7].len != sizeof(s.s) || |
| 147 | memcmp(s.r, v[6].buf, sizeof(s.r)) != 0 || |
| 148 | memcmp(s.s, v[7].buf, sizeof(s.s)) != 0) { |
| 149 | fputs("\n*** signature failed", stderr); |
| 150 | fputs("\nq = ", stderr); mp_writefile(dp.q, stderr, 16); |
| 151 | fputs("\np = ", stderr); mp_writefile(dp.p, stderr, 16); |
| 152 | fputs("\ng = ", stderr); mp_writefile(dp.g, stderr, 16); |
| 153 | fputs("\nx = ", stderr); mp_writefile(x, stderr, 16); |
| 154 | fprintf(stderr, "\nmessage = `%s'", v[4].buf); |
| 155 | fputs("\nk = ", stderr); type_hex.dump(&v[5], stderr); |
| 156 | fputs("\nR = ", stderr); type_hex.dump(&v[6], stderr); |
| 157 | fputs("\nS = ", stderr); type_hex.dump(&v[7], stderr); |
| 158 | |
| 159 | { |
| 160 | mp *m = MP_NEW; |
| 161 | m = mp_loadb(m, hash, sizeof(hash)); |
| 162 | fputs("\nm = ", stderr); mp_writefile(m, stderr, 16); |
| 163 | m = mp_loadb(m, s.r, sizeof(s.r)); |
| 164 | fputs("\nr = ", stderr); mp_writefile(m, stderr, 16); |
| 165 | m = mp_loadb(m, s.s, sizeof(s.s)); |
| 166 | fputs("\ns = ", stderr); mp_writefile(m, stderr, 16); |
| 167 | mp_drop(m); |
| 168 | } |
| 169 | |
| 170 | fputc('\n', stderr); |
| 171 | ok = 0; |
| 172 | } |
| 173 | |
| 174 | mp_drop(dp.p); |
| 175 | mp_drop(dp.q); |
| 176 | mp_drop(dp.g); |
| 177 | mp_drop(x); |
| 178 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 179 | return (ok); |
| 180 | } |
| 181 | |
| 182 | static test_chunk tests[] = { |
| 183 | { "sign", verify, |
| 184 | { &type_mp, &type_mp, &type_mp, &type_mp, |
| 185 | &type_string, &type_hex, &type_hex, &type_hex, 0 } }, |
| 186 | { 0, 0, { 0 } } |
| 187 | }; |
| 188 | |
| 189 | int main(int argc, char *argv[]) |
| 190 | { |
| 191 | sub_init(); |
| 192 | test_run(argc, argv, tests, SRCDIR "/t/dsa"); |
| 193 | return (0); |
| 194 | } |
| 195 | |
| 196 | #endif |
| 197 | |
| 198 | /*----- That's all, folks -------------------------------------------------*/ |