]> git.ipfire.org Git - thirdparty/nettle.git/blob - sexp2dsa.c
Use NETTLE_OCTET_SIZE_TO_LIMB_SIZE macro.
[thirdparty/nettle.git] / sexp2dsa.c
1 /* sexp2dsa.c
2
3 Copyright (C) 2002 Niels Möller
4
5 This file is part of GNU Nettle.
6
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
9
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.
13
14 or
15
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.
19
20 or both in parallel, as here.
21
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.
26
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/.
30 */
31
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <string.h>
37
38 #include "dsa.h"
39
40 #include "bignum.h"
41 #include "sexp.h"
42
43 #define GET(x, l, v) \
44 do { \
45 if (!nettle_mpz_set_sexp((x), (l), (v)) \
46 || mpz_sgn(x) <= 0) \
47 return 0; \
48 } while(0)
49
50 /* Iterator should point past the algorithm tag, e.g.
51 *
52 * (public-key (dsa (p |xxxx|) ...)
53 * ^ here
54 */
55
56 int
57 dsa_keypair_from_sexp_alist(struct dsa_params *params,
58 mpz_t pub,
59 mpz_t priv,
60 unsigned p_max_bits,
61 unsigned q_bits,
62 struct sexp_iterator *i)
63 {
64 static const char * const names[5]
65 = { "p", "q", "g", "y", "x" };
66 struct sexp_iterator values[5];
67 unsigned nvalues = priv ? 5 : 4;
68 unsigned p_bits;
69
70 if (!sexp_iterator_assoc(i, nvalues, names, values))
71 return 0;
72
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)
77 return 0;
78 if (mpz_cmp (params->q, params->p) >= 0)
79 return 0;
80 GET(params->g, p_bits, &values[2]);
81 if (mpz_cmp (params->g, params->p) >= 0)
82 return 0;
83 GET(pub, p_bits, &values[3]);
84 if (mpz_cmp (pub, params->p) >= 0)
85 return 0;
86
87 if (priv)
88 {
89 GET(priv, mpz_sizeinbase (params->q, 2), &values[4]);
90 if (mpz_cmp (priv, params->q) >= 0)
91 return 0;
92 }
93
94 return 1;
95 }
96
97 int
98 dsa_sha1_keypair_from_sexp(struct dsa_params *params,
99 mpz_t pub,
100 mpz_t priv,
101 unsigned p_max_bits,
102 size_t length, const uint8_t *expr)
103 {
104 struct sexp_iterator i;
105
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);
111 }
112
113 int
114 dsa_sha256_keypair_from_sexp(struct dsa_params *params,
115 mpz_t pub,
116 mpz_t priv,
117 unsigned p_max_bits,
118 size_t length, const uint8_t *expr)
119 {
120 struct sexp_iterator i;
121
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);
127 }
128
129 int
130 dsa_signature_from_sexp(struct dsa_signature *rs,
131 struct sexp_iterator *i,
132 unsigned q_bits)
133 {
134 static const char * const names[2] = { "r", "s" };
135 struct sexp_iterator values[2];
136
137 if (!sexp_iterator_assoc(i, 2, names, values))
138 return 0;
139
140 GET(rs->r, q_bits, &values[0]);
141 GET(rs->s, q_bits, &values[1]);
142
143 return 1;
144 }