xref: /titanic_44/usr/src/common/crypto/dh/dh_impl.h (revision 53a3dbbbad58b810bf04c5f94c0ac119a1d8d348)
1726fad2aSDina K Nimeh /*
2726fad2aSDina K Nimeh  * CDDL HEADER START
3726fad2aSDina K Nimeh  *
4726fad2aSDina K Nimeh  * The contents of this file are subject to the terms of the
5726fad2aSDina K Nimeh  * Common Development and Distribution License (the "License").
6726fad2aSDina K Nimeh  * You may not use this file except in compliance with the License.
7726fad2aSDina K Nimeh  *
8726fad2aSDina K Nimeh  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9726fad2aSDina K Nimeh  * or http://www.opensolaris.org/os/licensing.
10726fad2aSDina K Nimeh  * See the License for the specific language governing permissions
11726fad2aSDina K Nimeh  * and limitations under the License.
12726fad2aSDina K Nimeh  *
13726fad2aSDina K Nimeh  * When distributing Covered Code, include this CDDL HEADER in each
14726fad2aSDina K Nimeh  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15726fad2aSDina K Nimeh  * If applicable, add the following below this CDDL HEADER, with the
16726fad2aSDina K Nimeh  * fields enclosed by brackets "[]" replaced with your own identifying
17726fad2aSDina K Nimeh  * information: Portions Copyright [yyyy] [name of copyright owner]
18726fad2aSDina K Nimeh  *
19726fad2aSDina K Nimeh  * CDDL HEADER END
20726fad2aSDina K Nimeh  */
21726fad2aSDina K Nimeh 
22726fad2aSDina K Nimeh /*
23726fad2aSDina K Nimeh  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24726fad2aSDina K Nimeh  */
25726fad2aSDina K Nimeh 
26726fad2aSDina K Nimeh #ifndef _DH_IMPL_H
27726fad2aSDina K Nimeh #define	_DH_IMPL_H
28726fad2aSDina K Nimeh 
29726fad2aSDina K Nimeh #ifdef __cplusplus
30726fad2aSDina K Nimeh extern "C" {
31726fad2aSDina K Nimeh #endif
32726fad2aSDina K Nimeh 
33726fad2aSDina K Nimeh #include <sys/types.h>
34726fad2aSDina K Nimeh #include <bignum.h>
35726fad2aSDina K Nimeh 
36726fad2aSDina K Nimeh #define	MIN_DH_KEYLENGTH_IN_BYTES	8
37726fad2aSDina K Nimeh #define	MAX_DH_KEYLENGTH_IN_BYTES	512
38726fad2aSDina K Nimeh #define	DH_MIN_KEY_LEN		64
39726fad2aSDina K Nimeh #define	DH_MAX_KEY_LEN		4096
40726fad2aSDina K Nimeh 
41726fad2aSDina K Nimeh #ifdef _KERNEL
42726fad2aSDina K Nimeh 
43726fad2aSDina K Nimeh #include <sys/sunddi.h>
44726fad2aSDina K Nimeh #include <sys/crypto/common.h>
45726fad2aSDina K Nimeh 
46726fad2aSDina K Nimeh #define	CK_RV			ulong_t
47726fad2aSDina K Nimeh 
48726fad2aSDina K Nimeh #define	CKR_OK			CRYPTO_SUCCESS
49726fad2aSDina K Nimeh #define	CKR_ARGUMENTS_BAD	CRYPTO_ARGUMENTS_BAD
50726fad2aSDina K Nimeh #define	CKR_ATTRIBUTE_TYPE_INVALID	CRYPTO_ATTRIBUTE_TYPE_INVALID
51726fad2aSDina K Nimeh #define	CKR_ATTRIBUTE_VALUE_INVALID	CRYPTO_ATTRIBUTE_VALUE_INVALID
52726fad2aSDina K Nimeh #define	CKR_DEVICE_ERROR	CRYPTO_DEVICE_ERROR
53726fad2aSDina K Nimeh #define	CKR_GENERAL_ERROR	CRYPTO_GENERAL_ERROR
54726fad2aSDina K Nimeh #define	CKR_HOST_MEMORY		CRYPTO_HOST_MEMORY
55726fad2aSDina K Nimeh #define	CKR_KEY_SIZE_RANGE	CRYPTO_KEY_SIZE_RANGE
56726fad2aSDina K Nimeh 
57726fad2aSDina K Nimeh int random_get_bytes(uint8_t *ran_out, size_t ran_len);
58726fad2aSDina K Nimeh int random_get_pseudo_bytes(uint8_t *ran_out, size_t ran_len);
59726fad2aSDina K Nimeh 
60726fad2aSDina K Nimeh #else
61726fad2aSDina K Nimeh 
62726fad2aSDina K Nimeh #include <security/cryptoki.h>
63726fad2aSDina K Nimeh #include <security/pkcs11t.h>
64726fad2aSDina K Nimeh 
65726fad2aSDina K Nimeh #endif	/* _KERNEL */
66726fad2aSDina K Nimeh 
67726fad2aSDina K Nimeh 
68726fad2aSDina K Nimeh /* DH key using BIGNUM representations */
69726fad2aSDina K Nimeh typedef struct {
70726fad2aSDina K Nimeh 	int 	size;		/* key size in bits */
71726fad2aSDina K Nimeh 	BIGNUM	p;		/* p (prime) */
72726fad2aSDina K Nimeh 	BIGNUM	g;		/* g (base) */
73726fad2aSDina K Nimeh 	BIGNUM	x;		/* private value (random) */
74726fad2aSDina K Nimeh 	BIGNUM	y;		/* public value (= g^x mod p) */
75726fad2aSDina K Nimeh } DHkey;
76726fad2aSDina K Nimeh 
77726fad2aSDina K Nimeh /* DH key using byte string representations, useful for parameter lists */
78726fad2aSDina K Nimeh typedef struct {
79726fad2aSDina K Nimeh 	uint32_t prime_bits;	/* size */
80726fad2aSDina K Nimeh 	uchar_t	*prime;		/* p */
81726fad2aSDina K Nimeh 	uint32_t base_bytes;
82726fad2aSDina K Nimeh 	uchar_t *base;		/* g */
83726fad2aSDina K Nimeh 	uint32_t value_bits;	/* for both x and y */
84726fad2aSDina K Nimeh 	uchar_t	*private_x;	/* x */
85726fad2aSDina K Nimeh 	uchar_t *public_y;	/* y */
86726fad2aSDina K Nimeh 	int	(*rfunc)(void *, size_t);	/* random function */
87726fad2aSDina K Nimeh } DHbytekey;
88726fad2aSDina K Nimeh 
89726fad2aSDina K Nimeh 
90726fad2aSDina K Nimeh CK_RV dh_genkey_pair(DHbytekey *bkey);
91726fad2aSDina K Nimeh 
92726fad2aSDina K Nimeh CK_RV dh_key_derive(DHbytekey *bkey, uint32_t key_type,
93*53a3dbbbSJason King     uchar_t *secretkey, uint32_t *secretkey_len, int flag);
94726fad2aSDina K Nimeh 
95726fad2aSDina K Nimeh #ifdef	__cplusplus
96726fad2aSDina K Nimeh }
97726fad2aSDina K Nimeh #endif
98726fad2aSDina K Nimeh 
99726fad2aSDina K Nimeh #endif /* _DH_IMPL_H */
100