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