1 /*	$NetBSD: evp-crypt.c,v 1.1.1.1 2011/04/13 18:14:49 elric Exp $	*/
2 
3 /*
4  * Copyright (c) 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 /* Windows crypto provider plugin, sample */
37 
38 #include <config.h>
39 
40 #define HC_DEPRECATED
41 
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <assert.h>
47 
48 #include <evp.h>
49 
50 #include <crypt.h>
51 
52 
53 static HCRYPTPROV hCryptProv = NULL;
54 
55 /*
56  *
57  */
58 
59 struct generic_key {
60     HCRYPTKEY *hKey;
61 };
62 
63 static int
64 generic_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
65 		       unsigned char *out,
66 		       const unsigned char *in,
67 		       unsigned int size)
68 {
69     struct generic_key *gk = ctx->cipher_data;
70     BOOL bResult;
71     DWORD length = size;
72 
73     bResult = CryptSetKeyParam(gk->hKey, KP_IV, ctx->iv, 0);
74     _ASSERT(bResult);
75 
76     memcpy(out, in, size);
77 
78     if (ctx->encrypt)
79 	bResult = CryptEncrypt(gk->hKey, 0, TRUE, 0, out, &length, size);
80     else
81 	bResult = CryptDecrypt(gk->hKey, 0, TRUE, 0, out, &length);
82     _ASSERT(bResult);
83 
84     return 1;
85 }
86 
87 static int
88 generic_cleanup(EVP_CIPHER_CTX *ctx)
89 {
90     struct generic_key *gk = ctx->cipher_data;
91     CryptDestroyKey(gk->hKey);
92     gk->hKey = NULL;
93     return 1;
94 }
95 
96 static HCRYPTKEY
97 import_key(int alg, const unsigned char *key, size_t keylen)
98 {
99     struct {
100 	BLOBHEADER hdr;
101 	DWORD len;
102 	BYTE key[1];
103     } *key_blob;
104     size_t bloblen = sizeof(*key_blob) - 1 + keylen;
105 
106     key_blob = malloc(bloblen);
107 
108     key_blob->hdr.bType = PLAINTEXTKEYBLOB;
109     key_blob->hdr.bVersion = CUR_BLOB_VERSION;
110     key_blob->hdr.reserved = 0;
111     key_blob->hdr.aiKeyAlg = alg;
112     key_blob->len = 24;
113     memcpy(key_blob->key, key, keylen);
114 
115     bResult = CryptImportKey(hCryptProv,
116 			     (void *)key_blob, bloblen, 0, 0,
117 			     &gk->hKey);
118     free(key_blob);
119     _ASSERT(bResult);
120 
121     return hKey;
122 }
123 
124 static int
125 crypto_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
126 			 const unsigned char * key,
127 			 const unsigned char * iv,
128 			 int encp)
129 {
130     struct generic_key *gk = ctx->cipher_data;
131     DWORD paramData;
132 
133     gk->hKey = import_key(CALG_3DES,
134 			  key->key->keyvalue.data,
135 			  key->key->keyvalue.len);
136 
137     return 1;
138 }
139 
140 /**
141  * The tripple DES cipher type (Micrsoft crypt provider)
142  *
143  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
144  *
145  * @ingroup hcrypto_evp
146  */
147 
148 const EVP_CIPHER *
149 EVP_wincrypt_des_ede3_cbc(void)
150 {
151     static const EVP_CIPHER des_ede3_cbc = {
152 	0,
153 	8,
154 	24,
155 	8,
156 	EVP_CIPH_CBC_MODE,
157 	crypto_des_ede3_cbc_init,
158 	generic_cbc_do_cipher,
159 	generic_cleanup,
160 	sizeof(struct generic_key),
161 	NULL,
162 	NULL,
163 	NULL,
164 	NULL
165     };
166     return &des_ede3_cbc;
167 }
168 
169 /*
170  *
171  */
172 
173 struct generic_hash {
174     HCRYPTHASH hHash;
175 };
176 
177 static void
178 crypto_md5_init(struct generic_hash *m);
179 {
180     BOOL bResult;
181     bResult = CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &m->hHash);
182     _ASSERT(bResult);
183 }
184 
185 static void
186 generic_hash_update (struct generic_hash *m, const void *p, size_t len)
187 {
188     BOOL bResult;
189     bResult = CryptHashData(m->hHash, data, ( DWORD )len, 0 );
190     _ASSERT(bResult);
191 }
192 
193 static void
194 generic_hash_final (void *res, struct generic_hash *m);
195 {
196     DWORD length;
197     BOOL bResult;
198     bResult = CryptGetHashParam(m->hHash, HP_HASHVAL, res, &length, 0)
199     _ASSERT(bResult);
200 }
201 
202 static void
203 generic_hash_cleanup(struct generic_hash *m)
204 {
205     CryptDestroyHash(m->hHash);
206     m->hHash = NULL;
207 }
208 
209 const EVP_MD *
210 EVP_wincrypt_md5(void)
211 {
212     static const struct hc_evp_md md5 = {
213 	16,
214 	64,
215 	sizeof(struct generic_hash),
216 	(hc_evp_md_init)crypto_md5_init,
217 	(hc_evp_md_update)generic_hash_update,
218 	(hc_evp_md_final)generic_hash_final,
219 	(hc_evp_md_cleanup)generic_hash_cleanup
220     };
221     return &md5;
222 }
223