always call cipher-init so that we can reset IV when caller wants too
[heimdal.git] / lib / hcrypto / evp-cc.c
blob1bf8ca8af98ace3a368b197074d93040c98087a1
1 /*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 /* CommonCrypto provider */
36 #ifdef __APPLE__
38 #include "config.h"
40 #include <sys/types.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
46 #include <CommonCrypto/CommonDigest.h>
47 #include <CommonCrypto/CommonCryptor.h>
49 #include <evp.h>
50 #include <evp-cc.h>
56 struct cc_key {
57 CCCryptorRef href;
60 static int
61 cc_do_cipher(EVP_CIPHER_CTX *ctx,
62 unsigned char *out,
63 const unsigned char *in,
64 unsigned int size)
66 struct cc_key *cc = ctx->cipher_data;
67 CCCryptorStatus ret;
68 size_t moved;
70 memcpy(out, in, size);
72 ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
73 if (ret)
74 return 0;
76 if (moved != size)
77 return 0;
79 return 1;
82 static int
83 cc_cleanup(EVP_CIPHER_CTX *ctx)
85 struct cc_key *cc = ctx->cipher_data;
86 if (cc->href)
87 CCCryptorRelease(cc->href);
88 return 1;
91 static int
92 init_cc_key(int encp, CCAlgorithm alg, const void *key,
93 size_t keylen, const void *iv, CCCryptorRef *ref)
95 CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
96 CCCryptorStatus ret;
98 if (*ref) {
99 if (key == NULL && iv) {
100 CCCryptorReset(*ref, iv);
101 return 1;
103 CCCryptorRelease(*ref);
106 ret = CCCryptorCreate(op, alg, 0, key, keylen, iv, ref);
107 if (ret)
108 return 0;
109 return 1;
112 static int
113 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
114 const unsigned char * key,
115 const unsigned char * iv,
116 int encp)
118 struct cc_key *cc = ctx->cipher_data;
119 return init_cc_key(encp, kCCAlgorithm3DES, key, kCCKeySize3DES, iv, &cc->href);
123 * The tripple DES cipher type (Apple CommonCrypto provider)
125 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
127 * @ingroup hcrypto_evp
130 const EVP_CIPHER *
131 EVP_cc_des_ede3_cbc(void)
133 static const EVP_CIPHER des_ede3_cbc = {
138 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
139 cc_des_ede3_cbc_init,
140 cc_do_cipher,
141 cc_cleanup,
142 sizeof(struct cc_key),
143 NULL,
144 NULL,
145 NULL,
146 NULL
148 return &des_ede3_cbc;
155 static int
156 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
157 const unsigned char * key,
158 const unsigned char * iv,
159 int encp)
161 struct cc_key *cc = ctx->cipher_data;
162 return init_cc_key(encp, kCCAlgorithmDES, key, kCCBlockSizeDES, iv, &cc->href);
166 * The DES cipher type (Apple CommonCrypto provider)
168 * @return the DES-CBC EVP_CIPHER pointer.
170 * @ingroup hcrypto_evp
173 const EVP_CIPHER *
174 EVP_cc_des_cbc(void)
176 static const EVP_CIPHER des_ede3_cbc = {
178 kCCBlockSizeDES,
179 kCCBlockSizeDES,
180 kCCBlockSizeDES,
181 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
182 cc_des_cbc_init,
183 cc_do_cipher,
184 cc_cleanup,
185 sizeof(struct cc_key),
186 NULL,
187 NULL,
188 NULL,
189 NULL
191 return &des_ede3_cbc;
198 static int
199 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
200 const unsigned char * key,
201 const unsigned char * iv,
202 int encp)
204 struct cc_key *cc = ctx->cipher_data;
205 return init_cc_key(encp, kCCAlgorithmAES128, key, ctx->cipher->key_len, iv, &cc->href);
209 * The AES-128 cipher type (Apple CommonCrypto provider)
211 * @return the AES-128-CBC EVP_CIPHER pointer.
213 * @ingroup hcrypto_evp
216 const EVP_CIPHER *
217 EVP_cc_aes_128_cbc(void)
219 static const EVP_CIPHER c = {
221 kCCBlockSizeAES128,
222 kCCKeySizeAES128,
223 kCCBlockSizeAES128,
224 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
225 cc_aes_cbc_init,
226 cc_do_cipher,
227 cc_cleanup,
228 sizeof(struct cc_key),
229 NULL,
230 NULL,
231 NULL,
232 NULL
234 return &c;
238 * The AES-192 cipher type (Apple CommonCrypto provider)
240 * @return the AES-192-CBC EVP_CIPHER pointer.
242 * @ingroup hcrypto_evp
245 const EVP_CIPHER *
246 EVP_cc_aes_192_cbc(void)
248 static const EVP_CIPHER c = {
250 kCCBlockSizeAES128,
251 kCCKeySizeAES192,
252 kCCBlockSizeAES128,
253 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
254 cc_aes_cbc_init,
255 cc_do_cipher,
256 cc_cleanup,
257 sizeof(struct cc_key),
258 NULL,
259 NULL,
260 NULL,
261 NULL
263 return &c;
267 * The AES-256 cipher type (Apple CommonCrypto provider)
269 * @return the AES-256-CBC EVP_CIPHER pointer.
271 * @ingroup hcrypto_evp
274 const EVP_CIPHER *
275 EVP_cc_aes_256_cbc(void)
277 static const EVP_CIPHER c = {
279 kCCBlockSizeAES128,
280 kCCKeySizeAES256,
281 kCCBlockSizeAES128,
282 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
283 cc_aes_cbc_init,
284 cc_do_cipher,
285 cc_cleanup,
286 sizeof(struct cc_key),
287 NULL,
288 NULL,
289 NULL,
290 NULL
292 return &c;
299 static int
300 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
301 const unsigned char * key,
302 const unsigned char * iv,
303 int encp)
305 struct cc_key *cc = ctx->cipher_data;
306 return init_cc_key(encp, kCCAlgorithmRC2, key, ctx->cipher->key_len, iv, &cc->href);
310 * The RC2 cipher type - common crypto
312 * @return the RC2 EVP_CIPHER pointer.
314 * @ingroup hcrypto_evp
318 const EVP_CIPHER *
319 EVP_cc_rc2_cbc(void)
321 static const EVP_CIPHER rc2_cbc = {
323 kCCBlockSizeRC2,
325 kCCBlockSizeRC2,
326 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
327 cc_rc2_cbc_init,
328 cc_do_cipher,
329 cc_cleanup,
330 sizeof(struct cc_key),
331 NULL,
332 NULL,
333 NULL,
334 NULL
336 return &rc2_cbc;
340 * The RC2-40 cipher type - common crypto
342 * @return the RC2-40 EVP_CIPHER pointer.
344 * @ingroup hcrypto_evp
348 const EVP_CIPHER *
349 EVP_cc_rc2_40_cbc(void)
351 static const EVP_CIPHER rc2_40_cbc = {
353 kCCBlockSizeRC2,
355 kCCBlockSizeRC2,
356 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
357 cc_rc2_cbc_init,
358 cc_do_cipher,
359 cc_cleanup,
360 sizeof(struct cc_key),
361 NULL,
362 NULL,
363 NULL,
364 NULL
366 return &rc2_40_cbc;
371 * The RC2-64 cipher type - common crypto
373 * @return the RC2-64 EVP_CIPHER pointer.
375 * @ingroup hcrypto_evp
379 const EVP_CIPHER *
380 EVP_cc_rc2_64_cbc(void)
382 static const EVP_CIPHER rc2_64_cbc = {
384 kCCBlockSizeRC2,
386 kCCBlockSizeRC2,
387 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
388 cc_rc2_cbc_init,
389 cc_do_cipher,
390 cc_cleanup,
391 sizeof(struct cc_key),
392 NULL,
393 NULL,
394 NULL,
395 NULL
397 return &rc2_64_cbc;
401 * The CommonCrypto md2 provider
403 * @ingroup hcrypto_evp
406 const EVP_MD *
407 EVP_cc_md2(void)
409 static const struct hc_evp_md md2 = {
410 CC_MD2_DIGEST_LENGTH,
411 CC_MD2_BLOCK_BYTES,
412 sizeof(CC_MD2_CTX),
413 (hc_evp_md_init)CC_MD2_Init,
414 (hc_evp_md_update)CC_MD2_Update,
415 (hc_evp_md_final)CC_MD2_Final,
416 (hc_evp_md_cleanup)NULL
418 return &md2;
422 * The CommonCrypto md4 provider
424 * @ingroup hcrypto_evp
427 const EVP_MD *
428 EVP_cc_md4(void)
430 static const struct hc_evp_md md4 = {
431 CC_MD4_DIGEST_LENGTH,
432 CC_MD4_BLOCK_BYTES,
433 sizeof(CC_MD4_CTX),
434 (hc_evp_md_init)CC_MD4_Init,
435 (hc_evp_md_update)CC_MD4_Update,
436 (hc_evp_md_final)CC_MD4_Final,
437 (hc_evp_md_cleanup)NULL
439 return &md4;
443 * The CommonCrypto md5 provider
445 * @ingroup hcrypto_evp
448 const EVP_MD *
449 EVP_cc_md5(void)
451 static const struct hc_evp_md md5 = {
452 CC_MD5_DIGEST_LENGTH,
453 CC_MD5_BLOCK_BYTES,
454 sizeof(CC_MD5_CTX),
455 (hc_evp_md_init)CC_MD5_Init,
456 (hc_evp_md_update)CC_MD5_Update,
457 (hc_evp_md_final)CC_MD5_Final,
458 (hc_evp_md_cleanup)NULL
460 return &md5;
464 * The CommonCrypto sha1 provider
466 * @ingroup hcrypto_evp
469 const EVP_MD *
470 EVP_cc_sha1(void)
472 static const struct hc_evp_md sha1 = {
473 CC_SHA1_DIGEST_LENGTH,
474 CC_SHA1_BLOCK_BYTES,
475 sizeof(CC_SHA1_CTX),
476 (hc_evp_md_init)CC_SHA1_Init,
477 (hc_evp_md_update)CC_SHA1_Update,
478 (hc_evp_md_final)CC_SHA1_Final,
479 (hc_evp_md_cleanup)NULL
481 return &sha1;
485 * The CommonCrypto sha256 provider
487 * @ingroup hcrypto_evp
490 const EVP_MD *
491 EVP_cc_sha256(void)
493 static const struct hc_evp_md sha256 = {
494 CC_SHA256_DIGEST_LENGTH,
495 CC_SHA256_BLOCK_BYTES,
496 sizeof(CC_SHA256_CTX),
497 (hc_evp_md_init)CC_SHA256_Init,
498 (hc_evp_md_update)CC_SHA256_Update,
499 (hc_evp_md_final)CC_SHA256_Final,
500 (hc_evp_md_cleanup)NULL
502 return &sha256;
506 * The Camellia-128 cipher type - CommonCrypto
508 * @return the Camellia-128 EVP_CIPHER pointer.
510 * @ingroup hcrypto_evp
513 const EVP_CIPHER *
514 EVP_cc_camellia_128_cbc(void)
516 return NULL;
520 * The Camellia-198 cipher type - CommonCrypto
522 * @return the Camellia-198 EVP_CIPHER pointer.
524 * @ingroup hcrypto_evp
527 const EVP_CIPHER *
528 EVP_cc_camellia_192_cbc(void)
530 return NULL;
534 * The Camellia-256 cipher type - CommonCrypto
536 * @return the Camellia-256 EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
541 const EVP_CIPHER *
542 EVP_cc_camellia_256_cbc(void)
544 return NULL;
551 static int
552 cc_rc4_init(EVP_CIPHER_CTX *ctx,
553 const unsigned char * key,
554 const unsigned char * iv,
555 int encp)
557 struct cc_key *cc = ctx->cipher_data;
558 return init_cc_key(encp, kCCAlgorithmRC4, key, ctx->key_len, iv, &cc->href);
562 * The RC4 cipher type (Apple CommonCrypto provider)
564 * @return the RC4 EVP_CIPHER pointer.
566 * @ingroup hcrypto_evp
569 const EVP_CIPHER *
570 EVP_cc_rc4(void)
572 static const EVP_CIPHER rc4 = {
577 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
578 cc_rc4_init,
579 cc_do_cipher,
580 cc_cleanup,
581 sizeof(struct cc_key),
582 NULL,
583 NULL,
584 NULL,
585 NULL
587 return &rc4;
592 * The RC4-40 cipher type (Apple CommonCrypto provider)
594 * @return the RC4 EVP_CIPHER pointer.
596 * @ingroup hcrypto_evp
599 const EVP_CIPHER *
600 EVP_cc_rc4_40(void)
602 static const EVP_CIPHER rc4_40 = {
607 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
608 cc_rc4_init,
609 cc_do_cipher,
610 cc_cleanup,
611 sizeof(struct cc_key),
612 NULL,
613 NULL,
614 NULL,
615 NULL
617 return &rc4_40;
620 #endif /* __APPLE__ */