1/*	$NetBSD: crypto.h,v 1.1.1.1 2011/04/13 18:15:33 elric Exp $	*/
2
3/*
4 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#ifndef HEIMDAL_SMALLER
37#define DES3_OLD_ENCTYPE 1
38#endif
39
40struct _krb5_key_data {
41    krb5_keyblock *key;
42    krb5_data *schedule;
43};
44
45struct _krb5_key_usage;
46
47struct krb5_crypto_data {
48    struct _krb5_encryption_type *et;
49    struct _krb5_key_data key;
50    int num_key_usage;
51    struct _krb5_key_usage *key_usage;
52};
53
54#define CRYPTO_ETYPE(C) ((C)->et->type)
55
56/* bits for `flags' below */
57#define F_KEYED		 1	/* checksum is keyed */
58#define F_CPROOF	 2	/* checksum is collision proof */
59#define F_DERIVED	 4	/* uses derived keys */
60#define F_VARIANT	 8	/* uses `variant' keys (6.4.3) */
61#define F_PSEUDO	16	/* not a real protocol type */
62#define F_SPECIAL	32	/* backwards */
63#define F_DISABLED	64	/* enctype/checksum disabled */
64#define F_WEAK	       128	/* enctype is considered weak */
65
66struct salt_type {
67    krb5_salttype type;
68    const char *name;
69    krb5_error_code (*string_to_key)(krb5_context, krb5_enctype, krb5_data,
70				     krb5_salt, krb5_data, krb5_keyblock*);
71};
72
73struct _krb5_key_type {
74    krb5_keytype type; /* XXX */
75    const char *name;
76    size_t bits;
77    size_t size;
78    size_t schedule_size;
79    void (*random_key)(krb5_context, krb5_keyblock*);
80    void (*schedule)(krb5_context, struct _krb5_key_type *, struct _krb5_key_data *);
81    struct salt_type *string_to_key;
82    void (*random_to_key)(krb5_context, krb5_keyblock*, const void*, size_t);
83    void (*cleanup)(krb5_context, struct _krb5_key_data *);
84    const EVP_CIPHER *(*evp)(void);
85};
86
87struct _krb5_checksum_type {
88    krb5_cksumtype type;
89    const char *name;
90    size_t blocksize;
91    size_t checksumsize;
92    unsigned flags;
93    krb5_error_code (*checksum)(krb5_context context,
94				struct _krb5_key_data *key,
95				const void *buf, size_t len,
96				unsigned usage,
97				Checksum *csum);
98    krb5_error_code (*verify)(krb5_context context,
99			      struct _krb5_key_data *key,
100			      const void *buf, size_t len,
101			      unsigned usage,
102			      Checksum *csum);
103};
104
105struct _krb5_encryption_type {
106    krb5_enctype type;
107    const char *name;
108    size_t blocksize;
109    size_t padsize;
110    size_t confoundersize;
111    struct _krb5_key_type *keytype;
112    struct _krb5_checksum_type *checksum;
113    struct _krb5_checksum_type *keyed_checksum;
114    unsigned flags;
115    krb5_error_code (*encrypt)(krb5_context context,
116			       struct _krb5_key_data *key,
117			       void *data, size_t len,
118			       krb5_boolean encryptp,
119			       int usage,
120			       void *ivec);
121    size_t prf_length;
122    krb5_error_code (*prf)(krb5_context,
123			   krb5_crypto, const krb5_data *, krb5_data *);
124};
125
126#define ENCRYPTION_USAGE(U) (((U) << 8) | 0xAA)
127#define INTEGRITY_USAGE(U) (((U) << 8) | 0x55)
128#define CHECKSUM_USAGE(U) (((U) << 8) | 0x99)
129
130/* Checksums */
131
132extern struct _krb5_checksum_type _krb5_checksum_none;
133extern struct _krb5_checksum_type _krb5_checksum_crc32;
134extern struct _krb5_checksum_type _krb5_checksum_rsa_md4;
135extern struct _krb5_checksum_type _krb5_checksum_rsa_md4_des;
136extern struct _krb5_checksum_type _krb5_checksum_rsa_md5_des;
137extern struct _krb5_checksum_type _krb5_checksum_rsa_md5_des3;
138extern struct _krb5_checksum_type _krb5_checksum_rsa_md5;
139extern struct _krb5_checksum_type _krb5_checksum_hmac_sha1_des3;
140extern struct _krb5_checksum_type _krb5_checksum_hmac_sha1_aes128;
141extern struct _krb5_checksum_type _krb5_checksum_hmac_sha1_aes256;
142extern struct _krb5_checksum_type _krb5_checksum_hmac_md5;
143extern struct _krb5_checksum_type _krb5_checksum_sha1;
144
145extern struct _krb5_checksum_type *_krb5_checksum_types[];
146extern int _krb5_num_checksums;
147
148/* Salts */
149
150extern struct salt_type _krb5_AES_salt[];
151extern struct salt_type _krb5_arcfour_salt[];
152extern struct salt_type _krb5_des_salt[];
153extern struct salt_type _krb5_des3_salt[];
154extern struct salt_type _krb5_des3_salt_derived[];
155
156/* Encryption types */
157
158extern struct _krb5_encryption_type _krb5_enctype_aes256_cts_hmac_sha1;
159extern struct _krb5_encryption_type _krb5_enctype_aes128_cts_hmac_sha1;
160extern struct _krb5_encryption_type _krb5_enctype_des3_cbc_sha1;
161extern struct _krb5_encryption_type _krb5_enctype_des3_cbc_md5;
162extern struct _krb5_encryption_type _krb5_enctype_des3_cbc_none;
163extern struct _krb5_encryption_type _krb5_enctype_arcfour_hmac_md5;
164extern struct _krb5_encryption_type _krb5_enctype_des_cbc_md5;
165extern struct _krb5_encryption_type _krb5_enctype_old_des3_cbc_sha1;
166extern struct _krb5_encryption_type _krb5_enctype_des_cbc_crc;
167extern struct _krb5_encryption_type _krb5_enctype_des_cbc_md4;
168extern struct _krb5_encryption_type _krb5_enctype_des_cbc_md5;
169extern struct _krb5_encryption_type _krb5_enctype_des_cbc_none;
170extern struct _krb5_encryption_type _krb5_enctype_des_cfb64_none;
171extern struct _krb5_encryption_type _krb5_enctype_des_pcbc_none;
172extern struct _krb5_encryption_type _krb5_enctype_null;
173
174extern struct _krb5_encryption_type *_krb5_etypes[];
175extern int _krb5_num_etypes;
176
177/* Interface to the EVP crypto layer provided by hcrypto */
178struct _krb5_evp_schedule {
179    EVP_CIPHER_CTX ectx;
180    EVP_CIPHER_CTX dctx;
181};
182