| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Reading Diffie-Hellman parameters |
| 6 | * |
| 7 | * (c) 2004 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 | /*----- Header files ------------------------------------------------------*/ |
| 31 | |
| 32 | #include "dh.h" |
| 33 | #include "ptab.h" |
| 34 | #include "bintab.h" |
| 35 | |
| 36 | /*----- Main code ---------------------------------------------------------*/ |
| 37 | |
| 38 | /* --- @dh_parse@, @dhbin_parse@ --- * |
| 39 | * |
| 40 | * Arguments: @qd_parse *qd@ = parser context |
| 41 | * @dh_param *dp@ = parameters to fill in |
| 42 | * |
| 43 | * Returns: Zero if OK, nonzero on error. |
| 44 | * |
| 45 | * Use: Parses a prime group string. This is either one of the |
| 46 | * standard group strings, or a %$p$%, %$q$%, %$g$% triple |
| 47 | * separated by commas. |
| 48 | */ |
| 49 | |
| 50 | static void getinfo(dh_param *dp, pdata *pd) |
| 51 | { dp->p = &pd->p; dp->q = &pd->q; dp->g = &pd->g; } |
| 52 | |
| 53 | static int parse(qd_parse *qd, gprime_param *dp) |
| 54 | { |
| 55 | mp *p = MP_NEW, *q = MP_NEW, *g = MP_NEW; |
| 56 | |
| 57 | if ((p = qd_getmp(qd)) == 0) goto fail; |
| 58 | qd_delim(qd, ','); if ((q = qd_getmp(qd)) == 0) goto fail; |
| 59 | qd_delim(qd, ','); if ((g = qd_getmp(qd)) == 0) goto fail; |
| 60 | dp->p = p; dp->q = q; dp->g = g; |
| 61 | return (0); |
| 62 | fail: |
| 63 | mp_drop(p); mp_drop(q); mp_drop(g); |
| 64 | return (-1); |
| 65 | } |
| 66 | |
| 67 | int dh_parse(qd_parse *qd, dh_param *dp) |
| 68 | { |
| 69 | const pentry *pe; |
| 70 | |
| 71 | for (pe = ptab; pe->name; pe++) { |
| 72 | if (qd_enum(qd, pe->name) >= 0) { |
| 73 | getinfo(dp, pe->data); |
| 74 | goto found; |
| 75 | } |
| 76 | } |
| 77 | if (parse(qd, dp)) |
| 78 | return (-1); |
| 79 | found: |
| 80 | return (0); |
| 81 | } |
| 82 | |
| 83 | int dhbin_parse(qd_parse *qd, gbin_param *gb) |
| 84 | { |
| 85 | const binentry *be; |
| 86 | |
| 87 | for (be = bintab; be->name; be++) { |
| 88 | if (qd_enum(qd, be->name) >= 0) { |
| 89 | getinfo(gb, be->data); |
| 90 | goto found; |
| 91 | } |
| 92 | } |
| 93 | if (parse(qd, gb)) |
| 94 | return (-1); |
| 95 | found: |
| 96 | return (0); |
| 97 | } |
| 98 | |
| 99 | /*----- Test rig ----------------------------------------------------------*/ |
| 100 | |
| 101 | #ifdef TEST_RIG |
| 102 | |
| 103 | #include "fibrand.h" |
| 104 | |
| 105 | int main(int argc, char *argv[]) |
| 106 | { |
| 107 | const pentry *pe; |
| 108 | const binentry *be; |
| 109 | const char *e; |
| 110 | int ok = 1; |
| 111 | grand *gr; |
| 112 | |
| 113 | gr = fibrand_create(0); |
| 114 | fputs("checking standard prime groups...\n", stdout); |
| 115 | for (pe = ptab; pe->name; pe++) { |
| 116 | dh_param dp; |
| 117 | group *g; |
| 118 | getinfo(&dp, pe->data); |
| 119 | printf(" %s: ", pe->name); |
| 120 | g = group_prime(&dp); |
| 121 | if (mp_bits(dp.q) > 2048 && |
| 122 | (!argv[1] || strcmp(argv[1], "keen") != 0)) { |
| 123 | fputs("skipping\n", stdout); |
| 124 | continue; |
| 125 | } |
| 126 | fflush(stdout); |
| 127 | e = G_CHECK(g, gr); |
| 128 | G_DESTROYGROUP(g); |
| 129 | dh_paramfree(&dp); |
| 130 | if (e) { |
| 131 | printf("fails: %s\n", e); |
| 132 | ok = 0; |
| 133 | } else |
| 134 | fputs("ok\n", stdout); |
| 135 | } |
| 136 | fputs("checking standard binary groups...\n", stdout); |
| 137 | for (be = bintab; be->name; be++) { |
| 138 | gbin_param gb; |
| 139 | group *g; |
| 140 | getinfo(&gb, be->data); |
| 141 | printf(" %s: ", be->name); |
| 142 | g = group_binary(&gb); |
| 143 | fflush(stdout); |
| 144 | e = G_CHECK(g, gr); |
| 145 | G_DESTROYGROUP(g); |
| 146 | dh_paramfree(&gb); |
| 147 | if (e) { |
| 148 | printf("fails: %s\n", e); |
| 149 | ok = 0; |
| 150 | } else |
| 151 | fputs("ok\n", stdout); |
| 152 | } |
| 153 | gr->ops->destroy(gr); |
| 154 | if (ok) |
| 155 | fputs("all ok\n", stdout); |
| 156 | return (!ok); |
| 157 | } |
| 158 | |
| 159 | #endif |
| 160 | |
| 161 | /*----- That's all, folks -------------------------------------------------*/ |