1*1c9681d1Schristos /* $NetBSD: dsa.c,v 1.2 2017/01/28 21:31:47 christos Exp $ */
2f59d82ffSelric
3f59d82ffSelric /*
4f59d82ffSelric * Copyright (c) 2006 Kungliga Tekniska Högskolan
5f59d82ffSelric * (Royal Institute of Technology, Stockholm, Sweden).
6f59d82ffSelric * All rights reserved.
7f59d82ffSelric *
8f59d82ffSelric * Redistribution and use in source and binary forms, with or without
9f59d82ffSelric * modification, are permitted provided that the following conditions
10f59d82ffSelric * are met:
11f59d82ffSelric *
12f59d82ffSelric * 1. Redistributions of source code must retain the above copyright
13f59d82ffSelric * notice, this list of conditions and the following disclaimer.
14f59d82ffSelric *
15f59d82ffSelric * 2. Redistributions in binary form must reproduce the above copyright
16f59d82ffSelric * notice, this list of conditions and the following disclaimer in the
17f59d82ffSelric * documentation and/or other materials provided with the distribution.
18f59d82ffSelric *
19f59d82ffSelric * 3. Neither the name of the Institute nor the names of its contributors
20f59d82ffSelric * may be used to endorse or promote products derived from this software
21f59d82ffSelric * without specific prior written permission.
22f59d82ffSelric *
23f59d82ffSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24f59d82ffSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f59d82ffSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f59d82ffSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27f59d82ffSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f59d82ffSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f59d82ffSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f59d82ffSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f59d82ffSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f59d82ffSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f59d82ffSelric * SUCH DAMAGE.
34f59d82ffSelric */
35f59d82ffSelric
36f59d82ffSelric #include <config.h>
37f59d82ffSelric #include <krb5/roken.h>
38f59d82ffSelric
39e0895134Schristos #include <dsa.h>
40e0895134Schristos
41f59d82ffSelric /*
42f59d82ffSelric *
43f59d82ffSelric */
44f59d82ffSelric
45f59d82ffSelric DSA *
DSA_new(void)46f59d82ffSelric DSA_new(void)
47f59d82ffSelric {
48f59d82ffSelric DSA *dsa = calloc(1, sizeof(*dsa));
49f59d82ffSelric dsa->meth = rk_UNCONST(DSA_get_default_method());
50f59d82ffSelric dsa->references = 1;
51f59d82ffSelric return dsa;
52f59d82ffSelric }
53f59d82ffSelric
54f59d82ffSelric void
DSA_free(DSA * dsa)55f59d82ffSelric DSA_free(DSA *dsa)
56f59d82ffSelric {
57f59d82ffSelric if (dsa->references <= 0)
58f59d82ffSelric abort();
59f59d82ffSelric
60f59d82ffSelric if (--dsa->references > 0)
61f59d82ffSelric return;
62f59d82ffSelric
63f59d82ffSelric (*dsa->meth->finish)(dsa);
64f59d82ffSelric
65f59d82ffSelric #define free_if(f) if (f) { BN_free(f); }
66f59d82ffSelric free_if(dsa->p);
67f59d82ffSelric free_if(dsa->q);
68f59d82ffSelric free_if(dsa->g);
69f59d82ffSelric free_if(dsa->pub_key);
70f59d82ffSelric free_if(dsa->priv_key);
71f59d82ffSelric free_if(dsa->kinv);
72f59d82ffSelric free_if(dsa->r);
73f59d82ffSelric #undef free_if
74f59d82ffSelric
75f59d82ffSelric memset(dsa, 0, sizeof(*dsa));
76f59d82ffSelric free(dsa);
77f59d82ffSelric
78f59d82ffSelric }
79f59d82ffSelric
80f59d82ffSelric int
DSA_up_ref(DSA * dsa)81f59d82ffSelric DSA_up_ref(DSA *dsa)
82f59d82ffSelric {
83f59d82ffSelric return ++dsa->references;
84f59d82ffSelric }
85f59d82ffSelric
86f59d82ffSelric /*
87f59d82ffSelric *
88f59d82ffSelric */
89f59d82ffSelric
90f59d82ffSelric static const DSA_METHOD dsa_null_method = {
91e0895134Schristos "hcrypto null DSA",
92e0895134Schristos NULL,
93e0895134Schristos NULL,
94e0895134Schristos NULL,
95e0895134Schristos NULL,
96e0895134Schristos NULL,
97e0895134Schristos NULL,
98e0895134Schristos NULL,
99e0895134Schristos 0,
100e0895134Schristos NULL
101f59d82ffSelric };
102f59d82ffSelric
103f59d82ffSelric const DSA_METHOD *
DSA_null_method(void)104f59d82ffSelric DSA_null_method(void)
105f59d82ffSelric {
106f59d82ffSelric return &dsa_null_method;
107f59d82ffSelric }
108f59d82ffSelric
109f59d82ffSelric
110f59d82ffSelric const DSA_METHOD *dsa_default_mech = &dsa_null_method;
111f59d82ffSelric
112f59d82ffSelric void
DSA_set_default_method(const DSA_METHOD * mech)113f59d82ffSelric DSA_set_default_method(const DSA_METHOD *mech)
114f59d82ffSelric {
115f59d82ffSelric dsa_default_mech = mech;
116f59d82ffSelric }
117f59d82ffSelric
118f59d82ffSelric const DSA_METHOD *
DSA_get_default_method(void)119f59d82ffSelric DSA_get_default_method(void)
120f59d82ffSelric {
121f59d82ffSelric return dsa_default_mech;
122f59d82ffSelric }
123f59d82ffSelric
124f59d82ffSelric int
DSA_verify(int type,const unsigned char * digest,int digest_len,const unsigned char * sig,int sig_len,DSA * dsa)125f59d82ffSelric DSA_verify(int type, const unsigned char * digest, int digest_len,
126f59d82ffSelric const unsigned char *sig, int sig_len, DSA *dsa)
127f59d82ffSelric {
128f59d82ffSelric return -1;
129f59d82ffSelric }
130