~ubuntu-branches/ubuntu/quantal/nettle/quantal

« back to all changes in this revision

Viewing changes to sexp2dsa.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#if WITH_PUBLIC_KEY
 
30
 
 
31
#include <string.h>
 
32
 
 
33
#include "dsa.h"
 
34
 
 
35
#include "bignum.h"
 
36
#include "sexp.h"
 
37
 
 
38
#define GET(x, l, v)                            \
 
39
do {                                            \
 
40
  if (!nettle_mpz_set_sexp((x), (l), (v))       \
 
41
      || mpz_sgn(x) <= 0)                       \
 
42
    return 0;                                   \
 
43
} while(0)
 
44
 
 
45
/* Iterator should point past the algorithm tag, e.g.
 
46
 *
 
47
 *   (public-key (dsa (p |xxxx|) ...)
 
48
 *                    ^ here
 
49
 */
 
50
 
 
51
int
 
52
dsa_keypair_from_sexp_alist(struct dsa_public_key *pub,
 
53
                            struct dsa_private_key *priv,
 
54
                            unsigned limit,
 
55
                            struct sexp_iterator *i)
 
56
{
 
57
  static const uint8_t *names[5]
 
58
    = { "p", "q", "g", "y", "x" };
 
59
  struct sexp_iterator values[5];
 
60
  unsigned nvalues = priv ? 5 : 4;
 
61
  
 
62
  if (!sexp_iterator_assoc(i, nvalues, names, values))
 
63
    return 0;
 
64
 
 
65
  if (priv)
 
66
    GET(priv->x, limit, &values[4]);
 
67
  
 
68
  GET(pub->p, limit, &values[0]);
 
69
  GET(pub->q, DSA_Q_BITS, &values[1]);
 
70
  GET(pub->g, limit, &values[2]);
 
71
  GET(pub->y, limit, &values[3]);
 
72
  
 
73
  return 1;
 
74
}
 
75
 
 
76
int
 
77
dsa_keypair_from_sexp(struct dsa_public_key *pub,
 
78
                      struct dsa_private_key *priv,
 
79
                      unsigned limit, 
 
80
                      unsigned length, const uint8_t *expr)
 
81
{
 
82
  struct sexp_iterator i;
 
83
 
 
84
  return sexp_iterator_first(&i, length, expr)
 
85
    && sexp_iterator_check_type(&i, priv ? "private-key" : "public-key")
 
86
    && sexp_iterator_check_type(&i, "dsa")
 
87
    && dsa_keypair_from_sexp_alist(pub, priv, limit, &i);
 
88
}
 
89
 
 
90
int
 
91
dsa_signature_from_sexp(struct dsa_signature *rs,
 
92
                        struct sexp_iterator *i)
 
93
{
 
94
  static const uint8_t *names[2] = { "r", "s" };
 
95
  struct sexp_iterator values[2];
 
96
 
 
97
  if (!sexp_iterator_assoc(i, 2, names, values))
 
98
    return 0;
 
99
 
 
100
  GET(rs->r, 160, &values[0]);
 
101
  GET(rs->s, 160, &values[1]);
 
102
 
 
103
  return 1;
 
104
}
 
105
 
 
106
#endif /* WITH_PUBLIC_KEY */