1 /* $NetBSD: evp-crypt.c,v 1.1.1.1 2011/04/13 18:14:49 elric Exp $ */
4 * Copyright (c) 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
36 /* Windows crypto provider plugin, sample */
42 #include <sys/types.h>
53 static HCRYPTPROV hCryptProv
= NULL
;
64 generic_cbc_do_cipher(EVP_CIPHER_CTX
*ctx
,
66 const unsigned char *in
,
69 struct generic_key
*gk
= ctx
->cipher_data
;
73 bResult
= CryptSetKeyParam(gk
->hKey
, KP_IV
, ctx
->iv
, 0);
76 memcpy(out
, in
, size
);
79 bResult
= CryptEncrypt(gk
->hKey
, 0, TRUE
, 0, out
, &length
, size
);
81 bResult
= CryptDecrypt(gk
->hKey
, 0, TRUE
, 0, out
, &length
);
88 generic_cleanup(EVP_CIPHER_CTX
*ctx
)
90 struct generic_key
*gk
= ctx
->cipher_data
;
91 CryptDestroyKey(gk
->hKey
);
97 import_key(int alg
, const unsigned char *key
, size_t keylen
)
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
;
113 memcpy(key_blob
->key
, key
, keylen
);
115 bResult
= CryptImportKey(hCryptProv
,
116 (void *)key_blob
, bloblen
, 0, 0,
125 crypto_des_ede3_cbc_init(EVP_CIPHER_CTX
*ctx
,
126 const unsigned char * key
,
127 const unsigned char * iv
,
130 struct generic_key
*gk
= ctx
->cipher_data
;
133 gk
->hKey
= import_key(CALG_3DES
,
134 key
->key
->keyvalue
.data
,
135 key
->key
->keyvalue
.len
);
141 * The tripple DES cipher type (Micrsoft crypt provider)
143 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
145 * @ingroup hcrypto_evp
149 EVP_wincrypt_des_ede3_cbc(void)
151 static const EVP_CIPHER des_ede3_cbc
= {
157 crypto_des_ede3_cbc_init
,
158 generic_cbc_do_cipher
,
160 sizeof(struct generic_key
),
166 return &des_ede3_cbc
;
173 struct generic_hash
{
178 crypto_md5_init(struct generic_hash
*m
);
181 bResult
= CryptCreateHash(hCryptProv
, CALG_MD5
, 0, 0, &m
->hHash
);
186 generic_hash_update (struct generic_hash
*m
, const void *p
, size_t len
)
189 bResult
= CryptHashData(m
->hHash
, data
, ( DWORD
)len
, 0 );
194 generic_hash_final (void *res
, struct generic_hash
*m
);
198 bResult
= CryptGetHashParam(m
->hHash
, HP_HASHVAL
, res
, &length
, 0)
203 generic_hash_cleanup(struct generic_hash
*m
)
205 CryptDestroyHash(m
->hHash
);
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