1726fad2Dina K Nimeh/*
2726fad2Dina K Nimeh * CDDL HEADER START
3726fad2Dina K Nimeh *
4726fad2Dina K Nimeh * The contents of this file are subject to the terms of the
5726fad2Dina K Nimeh * Common Development and Distribution License (the "License").
6726fad2Dina K Nimeh * You may not use this file except in compliance with the License.
7726fad2Dina K Nimeh *
8726fad2Dina K Nimeh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9726fad2Dina K Nimeh * or http://www.opensolaris.org/os/licensing.
10726fad2Dina K Nimeh * See the License for the specific language governing permissions
11726fad2Dina K Nimeh * and limitations under the License.
12726fad2Dina K Nimeh *
13726fad2Dina K Nimeh * When distributing Covered Code, include this CDDL HEADER in each
14726fad2Dina K Nimeh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15726fad2Dina K Nimeh * If applicable, add the following below this CDDL HEADER, with the
16726fad2Dina K Nimeh * fields enclosed by brackets "[]" replaced with your own identifying
17726fad2Dina K Nimeh * information: Portions Copyright [yyyy] [name of copyright owner]
18726fad2Dina K Nimeh *
19726fad2Dina K Nimeh * CDDL HEADER END
20726fad2Dina K Nimeh */
21726fad2Dina K Nimeh
22726fad2Dina K Nimeh/*
23726fad2Dina K Nimeh * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24726fad2Dina K Nimeh */
25726fad2Dina K Nimeh
26726fad2Dina K Nimeh#ifndef _DSA_IMPL_H
27726fad2Dina K Nimeh#define	_DSA_IMPL_H
28726fad2Dina K Nimeh
29726fad2Dina K Nimeh#ifdef __cplusplus
30726fad2Dina K Nimehextern "C" {
31726fad2Dina K Nimeh#endif
32726fad2Dina K Nimeh
33726fad2Dina K Nimeh#include <sys/types.h>
34726fad2Dina K Nimeh#include <bignum.h>
35726fad2Dina K Nimeh
36726fad2Dina K Nimeh/* DSA Signature is always 40 bytes */
37726fad2Dina K Nimeh#define	DSA_SIGNATURE_LENGTH	40
38726fad2Dina K Nimeh#define	MIN_DSA_KEY_LEN		(512 >> 3)
39726fad2Dina K Nimeh#define	MAX_DSA_KEY_LEN		(1024 >> 3)
40726fad2Dina K Nimeh
41726fad2Dina K Nimeh#define	DSA_SUBPRIME_BITS	160
42726fad2Dina K Nimeh#define	DSA_SUBPRIME_BYTES	(DSA_SUBPRIME_BITS >> 3)
43726fad2Dina K Nimeh
44726fad2Dina K Nimeh#ifdef _KERNEL
45726fad2Dina K Nimeh
46726fad2Dina K Nimeh#include <sys/sunddi.h>
47726fad2Dina K Nimeh#include <sys/crypto/common.h>
48726fad2Dina K Nimeh
49726fad2Dina K Nimeh#define	CK_RV			int
50726fad2Dina K Nimeh
51726fad2Dina K Nimeh#define	CKR_OK			CRYPTO_SUCCESS
52726fad2Dina K Nimeh#define	CKR_ARGUMENTS_BAD	CRYPTO_ARGUMENTS_BAD
53726fad2Dina K Nimeh#define	CKR_ATTRIBUTE_VALUE_INVALID	CRYPTO_ATTRIBUTE_VALUE_INVALID
54726fad2Dina K Nimeh#define	CKR_DEVICE_ERROR	CRYPTO_DEVICE_ERROR
55726fad2Dina K Nimeh#define	CKR_GENERAL_ERROR	CRYPTO_GENERAL_ERROR
56726fad2Dina K Nimeh#define	CKR_HOST_MEMORY		CRYPTO_HOST_MEMORY
57726fad2Dina K Nimeh#define	CKR_KEY_SIZE_RANGE	CRYPTO_KEY_SIZE_RANGE
58726fad2Dina K Nimeh#define	CKR_SIGNATURE_INVALID	CRYPTO_SIGNATURE_INVALID
59726fad2Dina K Nimeh
60726fad2Dina K Nimehint random_get_bytes(uint8_t *ran_out, size_t ran_len);
61726fad2Dina K Nimehint random_get_pseudo_bytes(uint8_t *ran_out, size_t ran_len);
62726fad2Dina K Nimeh
63726fad2Dina K Nimeh#else
64726fad2Dina K Nimeh
65726fad2Dina K Nimeh#include <security/cryptoki.h>
66726fad2Dina K Nimeh#include <security/pkcs11t.h>
67726fad2Dina K Nimeh
68726fad2Dina K Nimeh#endif	/* _KERNEL */
69726fad2Dina K Nimeh
70726fad2Dina K Nimeh
71726fad2Dina K Nimeh/* DSA key using BIGNUM representations */
72726fad2Dina K Nimehtypedef struct {
73726fad2Dina K Nimeh	int 	size;		/* key size in bits */
74726fad2Dina K Nimeh	BIGNUM	p;		/* p (<size-bit> prime) */
75726fad2Dina K Nimeh	BIGNUM	q;		/* q (160-bit prime) */
76726fad2Dina K Nimeh	BIGNUM	g;		/* g (the base) */
77726fad2Dina K Nimeh	BIGNUM	x;		/* private key (< q) */
78726fad2Dina K Nimeh	BIGNUM	y;		/* = g^x mod p */
79726fad2Dina K Nimeh	BIGNUM	k;		/* k (random number < q) */
80726fad2Dina K Nimeh	BIGNUM	r;		/* r (signature 1st part) */
81726fad2Dina K Nimeh	BIGNUM	s;		/* s (signature 2st part) */
82726fad2Dina K Nimeh	BIGNUM	v;		/* v (verification value - should be = r) */
83726fad2Dina K Nimeh	BIGNUM	p_rr;		/* 2^(2*(32*p->len)) mod p */
84726fad2Dina K Nimeh	BIGNUM	q_rr;		/* 2^(2*(32*q->len)) mod q */
85726fad2Dina K Nimeh} DSAkey;
86726fad2Dina K Nimeh
87726fad2Dina K Nimeh/* DSA key using byte string representations, useful for parameter lists */
88726fad2Dina K Nimehtypedef struct {
89726fad2Dina K Nimeh	uint32_t prime_bits;	/* size */
90726fad2Dina K Nimeh	uchar_t	*prime;		/* p */
91726fad2Dina K Nimeh	uint32_t subprime_bits;	/* = 160 */
92726fad2Dina K Nimeh	uchar_t	*subprime;	/* q */
93726fad2Dina K Nimeh	uint32_t base_bytes;
94726fad2Dina K Nimeh	uchar_t	*base;		/* g */
95726fad2Dina K Nimeh	uchar_t	*private_x;	/* x */
96726fad2Dina K Nimeh	uint32_t private_x_bits;
97726fad2Dina K Nimeh	uchar_t	*public_y;	/* y */
98726fad2Dina K Nimeh	uint32_t public_y_bits;
99726fad2Dina K Nimeh	uchar_t	*signature;	/* concat(r, s) */
100726fad2Dina K Nimeh	int	(*rfunc)(void *, size_t);	/* random function */
101726fad2Dina K Nimeh} DSAbytekey;
102726fad2Dina K Nimeh
103726fad2Dina K Nimeh
104726fad2Dina K NimehCK_RV dsa_genkey_pair(DSAbytekey *bkey);
105726fad2Dina K Nimeh
106726fad2Dina K NimehCK_RV dsa_sign(DSAbytekey *bkey, uchar_t *msg, uint32_t msglen, uchar_t *sig);
107726fad2Dina K Nimeh
108726fad2Dina K NimehCK_RV dsa_verify(DSAbytekey *bkey, uchar_t *msg, uchar_t *sig);
109726fad2Dina K Nimeh
110726fad2Dina K Nimeh#ifdef	__cplusplus
111726fad2Dina K Nimeh}
112726fad2Dina K Nimeh#endif
113726fad2Dina K Nimeh
114726fad2Dina K Nimeh#endif /* _DSA_IMPL_H */
115