5 /* nettle, low-level cryptographics library
7 * Copyright (C) 2002 Niels Möller
9 * The nettle library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
14 * The nettle library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the nettle library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
36 #define GET(x, l, v) \
38 if (!nettle_mpz_set_sexp((x), (l), (v)) \
43 /* Iterator should point past the algorithm tag, e.g.
45 * (public-key (dsa (p |xxxx|) ...)
50 dsa_keypair_from_sexp_alist(struct dsa_public_key *pub,
51 struct dsa_private_key *priv,
54 struct sexp_iterator *i)
56 static const uint8_t * const names[5]
57 = { "p", "q", "g", "y", "x" };
58 struct sexp_iterator values[5];
59 unsigned nvalues = priv ? 5 : 4;
61 if (!sexp_iterator_assoc(i, nvalues, names, values))
65 GET(priv->x, q_bits, &values[4]);
67 GET(pub->p, p_max_bits, &values[0]);
68 GET(pub->q, q_bits, &values[1]);
69 if (mpz_sizeinbase(pub->q, 2) != q_bits)
71 GET(pub->g, p_max_bits, &values[2]);
72 GET(pub->y, p_max_bits, &values[3]);
78 dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub,
79 struct dsa_private_key *priv,
81 unsigned length, const uint8_t *expr)
83 struct sexp_iterator i;
85 return sexp_iterator_first(&i, length, expr)
86 && sexp_iterator_check_type(&i, priv ? "private-key" : "public-key")
87 && sexp_iterator_check_type(&i, "dsa")
88 && dsa_keypair_from_sexp_alist(pub, priv, p_max_bits, DSA_SHA1_Q_BITS, &i);
92 dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub,
93 struct dsa_private_key *priv,
95 unsigned length, const uint8_t *expr)
97 struct sexp_iterator i;
99 return sexp_iterator_first(&i, length, expr)
100 && sexp_iterator_check_type(&i, priv ? "private-key" : "public-key")
101 && sexp_iterator_check_type(&i, "dsa-sha256")
102 && dsa_keypair_from_sexp_alist(pub, priv, p_max_bits, DSA_SHA256_Q_BITS, &i);
106 dsa_signature_from_sexp(struct dsa_signature *rs,
107 struct sexp_iterator *i,
110 static const uint8_t * const names[2] = { "r", "s" };
111 struct sexp_iterator values[2];
113 if (!sexp_iterator_assoc(i, 2, names, values))
116 GET(rs->r, q_bits, &values[0]);
117 GET(rs->s, q_bits, &values[1]);