s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / third_party / heimdal / lib / hcrypto / evp-crypt.c
1 /*
2  * Copyright (c) 2008 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /* Windows crypto provider plugin, sample */
35
36 #include <config.h>
37 #include <roken.h>
38
39 #define HC_DEPRECATED
40
41 #include <assert.h>
42
43 #include <evp.h>
44
45 #include <crypt.h>
46
47
48 static HCRYPTPROV hCryptProv = NULL;
49
50 /*
51  *
52  */
53
54 struct generic_key {
55     HCRYPTKEY *hKey;
56 };
57
58 static int
59 generic_cbc_do_cipher(EVP_CIPHER_CTX *ctx,
60                        unsigned char *out,
61                        const unsigned char *in,
62                        unsigned int size)
63 {
64     struct generic_key *gk = ctx->cipher_data;
65     BOOL bResult;
66     DWORD length = size;
67
68     bResult = CryptSetKeyParam(gk->hKey, KP_IV, ctx->iv, 0);
69     _ASSERT(bResult);
70
71     memcpy(out, in, size);
72
73     if (ctx->encrypt)
74         bResult = CryptEncrypt(gk->hKey, 0, TRUE, 0, out, &length, size);
75     else
76         bResult = CryptDecrypt(gk->hKey, 0, TRUE, 0, out, &length);
77     _ASSERT(bResult);
78
79     return 1;
80 }
81
82 static int
83 generic_cleanup(EVP_CIPHER_CTX *ctx)
84 {
85     struct generic_key *gk = ctx->cipher_data;
86     CryptDestroyKey(gk->hKey);
87     gk->hKey = NULL;
88     return 1;
89 }
90
91 static HCRYPTKEY
92 import_key(int alg, const unsigned char *key, size_t keylen)
93 {
94     struct {
95         BLOBHEADER hdr;
96         DWORD len;
97         BYTE key[1];
98     } *key_blob;
99     size_t bloblen = sizeof(*key_blob) - 1 + keylen;
100
101     key_blob = malloc(bloblen);
102
103     key_blob->hdr.bType = PLAINTEXTKEYBLOB;
104     key_blob->hdr.bVersion = CUR_BLOB_VERSION;
105     key_blob->hdr.reserved = 0;
106     key_blob->hdr.aiKeyAlg = alg;
107     key_blob->len = 24;
108     memcpy(key_blob->key, key, keylen);
109
110     bResult = CryptImportKey(hCryptProv,
111                              (void *)key_blob, bloblen, 0, 0,
112                              &gk->hKey);
113     free(key_blob);
114     _ASSERT(bResult);
115
116     return hKey;
117 }
118
119 static int
120 crypto_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
121                          const unsigned char * key,
122                          const unsigned char * iv,
123                          int encp)
124 {
125     struct generic_key *gk = ctx->cipher_data;
126     DWORD paramData;
127
128     gk->hKey = import_key(CALG_3DES,
129                           key->key->keyvalue.data,
130                           key->key->keyvalue.len);
131
132     return 1;
133 }
134
135 /**
136  * The triple DES cipher type (Micrsoft crypt provider)
137  *
138  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
139  *
140  * @ingroup hcrypto_evp
141  */
142
143 const EVP_CIPHER *
144 EVP_wincrypt_des_ede3_cbc(void)
145 {
146     static const EVP_CIPHER des_ede3_cbc = {
147         0,
148         8,
149         24,
150         8,
151         EVP_CIPH_CBC_MODE,
152         crypto_des_ede3_cbc_init,
153         generic_cbc_do_cipher,
154         generic_cleanup,
155         sizeof(struct generic_key),
156         NULL,
157         NULL,
158         NULL,
159         NULL
160     };
161     return &des_ede3_cbc;
162 }
163
164 /*
165  *
166  */
167
168 struct generic_hash {
169     HCRYPTHASH hHash;
170 };
171
172 static void
173 crypto_md5_init(struct generic_hash *m);
174 {
175     BOOL bResult;
176     bResult = CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &m->hHash);
177     _ASSERT(bResult);
178 }
179
180 static void
181 generic_hash_update (struct generic_hash *m, const void *p, size_t len)
182 {
183     BOOL bResult;
184     bResult = CryptHashData(m->hHash, data, ( DWORD )len, 0 );
185     _ASSERT(bResult);
186 }
187
188 static void
189 generic_hash_final (void *res, struct generic_hash *m);
190 {
191     DWORD length;
192     BOOL bResult;
193     bResult = CryptGetHashParam(m->hHash, HP_HASHVAL, res, &length, 0)
194     _ASSERT(bResult);
195 }
196
197 static void
198 generic_hash_cleanup(struct generic_hash *m)
199 {
200     CryptDestroyHash(m->hHash);
201     m->hHash = NULL;
202 }
203
204 const EVP_MD *
205 EVP_wincrypt_md5(void)
206 {
207     static const struct hc_evp_md md5 = {
208         16,
209         64,
210         sizeof(struct generic_hash),
211         (hc_evp_md_init)crypto_md5_init,
212         (hc_evp_md_update)generic_hash_update,
213         (hc_evp_md_final)generic_hash_final,
214         (hc_evp_md_cleanup)generic_hash_cleanup
215     };
216     return &md5;
217 }