[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / sexp2dsa.c
1 /* sexp2dsa.c
2  *
3  */
4
5 /* nettle, low-level cryptographics library
6  *
7  * Copyright (C) 2002 Niels Möller
8  *  
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.
13  * 
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.
18  * 
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,
22  * MA 02111-1307, USA.
23  */
24
25 #if HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include "dsa.h"
32
33 #include "bignum.h"
34 #include "sexp.h"
35
36 #define GET(x, l, v)                            \
37 do {                                            \
38   if (!nettle_mpz_set_sexp((x), (l), (v))       \
39       || mpz_sgn(x) <= 0)                       \
40     return 0;                                   \
41 } while(0)
42
43 /* Iterator should point past the algorithm tag, e.g.
44  *
45  *   (public-key (dsa (p |xxxx|) ...)
46  *                    ^ here
47  */
48
49 int
50 dsa_keypair_from_sexp_alist(struct dsa_public_key *pub,
51                             struct dsa_private_key *priv,
52                             unsigned p_max_bits,
53                             unsigned q_bits,
54                             struct sexp_iterator *i)
55 {
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;
60   
61   if (!sexp_iterator_assoc(i, nvalues, names, values))
62     return 0;
63
64   if (priv)
65     GET(priv->x, q_bits, &values[4]);
66   
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)
70     return 0;
71   GET(pub->g, p_max_bits, &values[2]);
72   GET(pub->y, p_max_bits, &values[3]);
73   
74   return 1;
75 }
76
77 int
78 dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub,
79                            struct dsa_private_key *priv,
80                            unsigned p_max_bits, 
81                            unsigned length, const uint8_t *expr)
82 {
83   struct sexp_iterator i;
84
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);
89 }
90
91 int
92 dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub,
93                              struct dsa_private_key *priv,
94                              unsigned p_max_bits, 
95                              unsigned length, const uint8_t *expr)
96 {
97   struct sexp_iterator i;
98
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);
103 }
104
105 int
106 dsa_signature_from_sexp(struct dsa_signature *rs,
107                         struct sexp_iterator *i,
108                         unsigned q_bits)
109 {
110   static const uint8_t * const names[2] = { "r", "s" };
111   struct sexp_iterator values[2];
112
113   if (!sexp_iterator_assoc(i, 2, names, values))
114     return 0;
115
116   GET(rs->r, q_bits, &values[0]);
117   GET(rs->s, q_bits, &values[1]);
118
119   return 1;
120 }