3 Copyright (C) 2002 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
43 #define GET(x, l, v) \
45 if (!nettle_mpz_set_sexp((x), (l), (v)) \
50 /* Iterator should point past the algorithm tag, e.g.
52 * (public-key (dsa (p |xxxx|) ...)
57 dsa_keypair_from_sexp_alist(struct dsa_params *params,
62 struct sexp_iterator *i)
64 static const uint8_t * const names[5]
65 = { "p", "q", "g", "y", "x" };
66 struct sexp_iterator values[5];
67 unsigned nvalues = priv ? 5 : 4;
70 if (!sexp_iterator_assoc(i, nvalues, names, values))
73 GET(params->p, p_max_bits, &values[0]);
74 p_bits = mpz_sizeinbase (params->p, 2);
75 GET(params->q, q_bits ? q_bits : p_bits, &values[1]);
76 if (q_bits > 0 && mpz_sizeinbase(params->q, 2) != q_bits)
78 if (mpz_cmp (params->q, params->p) >= 0)
80 GET(params->g, p_bits, &values[2]);
81 if (mpz_cmp (params->g, params->p) >= 0)
83 GET(pub, p_bits, &values[3]);
84 if (mpz_cmp (pub, params->p) >= 0)
89 GET(priv, mpz_sizeinbase (params->q, 2), &values[4]);
90 if (mpz_cmp (priv, params->q) >= 0)
98 dsa_sha1_keypair_from_sexp(struct dsa_params *params,
102 size_t length, const uint8_t *expr)
104 struct sexp_iterator i;
106 return sexp_iterator_first(&i, length, expr)
107 && sexp_iterator_check_type(&i, priv ? "private-key" : "public-key")
108 && sexp_iterator_check_type(&i, "dsa")
109 && dsa_keypair_from_sexp_alist(params, pub, priv,
110 p_max_bits, DSA_SHA1_Q_BITS, &i);
114 dsa_sha256_keypair_from_sexp(struct dsa_params *params,
118 size_t length, const uint8_t *expr)
120 struct sexp_iterator i;
122 return sexp_iterator_first(&i, length, expr)
123 && sexp_iterator_check_type(&i, priv ? "private-key" : "public-key")
124 && sexp_iterator_check_type(&i, "dsa-sha256")
125 && dsa_keypair_from_sexp_alist(params, pub, priv,
126 p_max_bits, DSA_SHA256_Q_BITS, &i);
130 dsa_signature_from_sexp(struct dsa_signature *rs,
131 struct sexp_iterator *i,
134 static const uint8_t * const names[2] = { "r", "s" };
135 struct sexp_iterator values[2];
137 if (!sexp_iterator_assoc(i, 2, names, values))
140 GET(rs->r, q_bits, &values[0]);
141 GET(rs->s, q_bits, &values[1]);