Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / hcrypto / evp-crypt.c
blob3d30c4a3c6fd7d5793081d793b112792237fa860
1 /* $NetBSD: evp-crypt.c,v 1.1.1.1 2011/04/13 18:14:49 elric Exp $ */
3 /*
4 * Copyright (c) 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 /* Windows crypto provider plugin, sample */
38 #include <config.h>
40 #define HC_DEPRECATED
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <assert.h>
48 #include <evp.h>
50 #include <crypt.h>
53 static HCRYPTPROV hCryptProv = NULL;
59 struct generic_key {
60 HCRYPTKEY *hKey;
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)
69 struct generic_key *gk = ctx->cipher_data;
70 BOOL bResult;
71 DWORD length = size;
73 bResult = CryptSetKeyParam(gk->hKey, KP_IV, ctx->iv, 0);
74 _ASSERT(bResult);
76 memcpy(out, in, size);
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);
84 return 1;
87 static int
88 generic_cleanup(EVP_CIPHER_CTX *ctx)
90 struct generic_key *gk = ctx->cipher_data;
91 CryptDestroyKey(gk->hKey);
92 gk->hKey = NULL;
93 return 1;
96 static HCRYPTKEY
97 import_key(int alg, const unsigned char *key, size_t keylen)
99 struct {
100 BLOBHEADER hdr;
101 DWORD len;
102 BYTE key[1];
103 } *key_blob;
104 size_t bloblen = sizeof(*key_blob) - 1 + keylen;
106 key_blob = malloc(bloblen);
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);
115 bResult = CryptImportKey(hCryptProv,
116 (void *)key_blob, bloblen, 0, 0,
117 &gk->hKey);
118 free(key_blob);
119 _ASSERT(bResult);
121 return hKey;
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)
130 struct generic_key *gk = ctx->cipher_data;
131 DWORD paramData;
133 gk->hKey = import_key(CALG_3DES,
134 key->key->keyvalue.data,
135 key->key->keyvalue.len);
137 return 1;
141 * The tripple DES cipher type (Micrsoft crypt provider)
143 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
145 * @ingroup hcrypto_evp
148 const EVP_CIPHER *
149 EVP_wincrypt_des_ede3_cbc(void)
151 static const EVP_CIPHER des_ede3_cbc = {
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
166 return &des_ede3_cbc;
173 struct generic_hash {
174 HCRYPTHASH hHash;
177 static void
178 crypto_md5_init(struct generic_hash *m);
180 BOOL bResult;
181 bResult = CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &m->hHash);
182 _ASSERT(bResult);
185 static void
186 generic_hash_update (struct generic_hash *m, const void *p, size_t len)
188 BOOL bResult;
189 bResult = CryptHashData(m->hHash, data, ( DWORD )len, 0 );
190 _ASSERT(bResult);
193 static void
194 generic_hash_final (void *res, struct generic_hash *m);
196 DWORD length;
197 BOOL bResult;
198 bResult = CryptGetHashParam(m->hHash, HP_HASHVAL, res, &length, 0)
199 _ASSERT(bResult);
202 static void
203 generic_hash_cleanup(struct generic_hash *m)
205 CryptDestroyHash(m->hHash);
206 m->hHash = NULL;
209 const EVP_MD *
210 EVP_wincrypt_md5(void)
212 static const struct hc_evp_md md5 = {
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
221 return &md5;