[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / hcrypto / evp-cc.c
blob15b3479f8ed7fc81a48d8d3726030c079fe347f9
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 #ifdef COMMONCRYPTO_SUPPORTS_RC2
300 static int
301 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
302 const unsigned char * key,
303 const unsigned char * iv,
304 int encp)
306 struct cc_key *cc = ctx->cipher_data;
307 return init_cc_key(encp, kCCAlgorithmRC2, key, ctx->cipher->key_len, iv, &cc->href);
309 #endif
312 * The RC2 cipher type - common crypto
314 * @return the RC2 EVP_CIPHER pointer.
316 * @ingroup hcrypto_evp
320 const EVP_CIPHER *
321 EVP_cc_rc2_cbc(void)
323 #ifdef COMMONCRYPTO_SUPPORTS_RC2
324 static const EVP_CIPHER rc2_cbc = {
326 kCCBlockSizeRC2,
328 kCCBlockSizeRC2,
329 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
330 cc_rc2_cbc_init,
331 cc_do_cipher,
332 cc_cleanup,
333 sizeof(struct cc_key),
334 NULL,
335 NULL,
336 NULL,
337 NULL
339 return &rc2_cbc;
340 #else
341 return NULL;
342 #endif
346 * The RC2-40 cipher type - common crypto
348 * @return the RC2-40 EVP_CIPHER pointer.
350 * @ingroup hcrypto_evp
354 const EVP_CIPHER *
355 EVP_cc_rc2_40_cbc(void)
357 #ifdef COMMONCRYPTO_SUPPORTS_RC2
358 static const EVP_CIPHER rc2_40_cbc = {
360 kCCBlockSizeRC2,
362 kCCBlockSizeRC2,
363 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
364 cc_rc2_cbc_init,
365 cc_do_cipher,
366 cc_cleanup,
367 sizeof(struct cc_key),
368 NULL,
369 NULL,
370 NULL,
371 NULL
373 return &rc2_40_cbc;
374 #else
375 return NULL;
376 #endif
381 * The RC2-64 cipher type - common crypto
383 * @return the RC2-64 EVP_CIPHER pointer.
385 * @ingroup hcrypto_evp
389 const EVP_CIPHER *
390 EVP_cc_rc2_64_cbc(void)
392 #ifdef COMMONCRYPTO_SUPPORTS_RC2
393 static const EVP_CIPHER rc2_64_cbc = {
395 kCCBlockSizeRC2,
397 kCCBlockSizeRC2,
398 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
399 cc_rc2_cbc_init,
400 cc_do_cipher,
401 cc_cleanup,
402 sizeof(struct cc_key),
403 NULL,
404 NULL,
405 NULL,
406 NULL
408 return &rc2_64_cbc;
409 #else
410 return NULL;
411 #endif
415 * The CommonCrypto md2 provider
417 * @ingroup hcrypto_evp
420 const EVP_MD *
421 EVP_cc_md2(void)
423 static const struct hc_evp_md md2 = {
424 CC_MD2_DIGEST_LENGTH,
425 CC_MD2_BLOCK_BYTES,
426 sizeof(CC_MD2_CTX),
427 (hc_evp_md_init)CC_MD2_Init,
428 (hc_evp_md_update)CC_MD2_Update,
429 (hc_evp_md_final)CC_MD2_Final,
430 (hc_evp_md_cleanup)NULL
432 return &md2;
436 * The CommonCrypto md4 provider
438 * @ingroup hcrypto_evp
441 const EVP_MD *
442 EVP_cc_md4(void)
444 static const struct hc_evp_md md4 = {
445 CC_MD4_DIGEST_LENGTH,
446 CC_MD4_BLOCK_BYTES,
447 sizeof(CC_MD4_CTX),
448 (hc_evp_md_init)CC_MD4_Init,
449 (hc_evp_md_update)CC_MD4_Update,
450 (hc_evp_md_final)CC_MD4_Final,
451 (hc_evp_md_cleanup)NULL
453 return &md4;
457 * The CommonCrypto md5 provider
459 * @ingroup hcrypto_evp
462 const EVP_MD *
463 EVP_cc_md5(void)
465 static const struct hc_evp_md md5 = {
466 CC_MD5_DIGEST_LENGTH,
467 CC_MD5_BLOCK_BYTES,
468 sizeof(CC_MD5_CTX),
469 (hc_evp_md_init)CC_MD5_Init,
470 (hc_evp_md_update)CC_MD5_Update,
471 (hc_evp_md_final)CC_MD5_Final,
472 (hc_evp_md_cleanup)NULL
474 return &md5;
478 * The CommonCrypto sha1 provider
480 * @ingroup hcrypto_evp
483 const EVP_MD *
484 EVP_cc_sha1(void)
486 static const struct hc_evp_md sha1 = {
487 CC_SHA1_DIGEST_LENGTH,
488 CC_SHA1_BLOCK_BYTES,
489 sizeof(CC_SHA1_CTX),
490 (hc_evp_md_init)CC_SHA1_Init,
491 (hc_evp_md_update)CC_SHA1_Update,
492 (hc_evp_md_final)CC_SHA1_Final,
493 (hc_evp_md_cleanup)NULL
495 return &sha1;
499 * The CommonCrypto sha256 provider
501 * @ingroup hcrypto_evp
504 const EVP_MD *
505 EVP_cc_sha256(void)
507 static const struct hc_evp_md sha256 = {
508 CC_SHA256_DIGEST_LENGTH,
509 CC_SHA256_BLOCK_BYTES,
510 sizeof(CC_SHA256_CTX),
511 (hc_evp_md_init)CC_SHA256_Init,
512 (hc_evp_md_update)CC_SHA256_Update,
513 (hc_evp_md_final)CC_SHA256_Final,
514 (hc_evp_md_cleanup)NULL
516 return &sha256;
520 * The Camellia-128 cipher type - CommonCrypto
522 * @return the Camellia-128 EVP_CIPHER pointer.
524 * @ingroup hcrypto_evp
527 const EVP_CIPHER *
528 EVP_cc_camellia_128_cbc(void)
530 return NULL;
534 * The Camellia-198 cipher type - CommonCrypto
536 * @return the Camellia-198 EVP_CIPHER pointer.
538 * @ingroup hcrypto_evp
541 const EVP_CIPHER *
542 EVP_cc_camellia_192_cbc(void)
544 return NULL;
548 * The Camellia-256 cipher type - CommonCrypto
550 * @return the Camellia-256 EVP_CIPHER pointer.
552 * @ingroup hcrypto_evp
555 const EVP_CIPHER *
556 EVP_cc_camellia_256_cbc(void)
558 return NULL;
565 static int
566 cc_rc4_init(EVP_CIPHER_CTX *ctx,
567 const unsigned char * key,
568 const unsigned char * iv,
569 int encp)
571 struct cc_key *cc = ctx->cipher_data;
572 return init_cc_key(encp, kCCAlgorithmRC4, key, ctx->key_len, iv, &cc->href);
576 * The RC4 cipher type (Apple CommonCrypto provider)
578 * @return the RC4 EVP_CIPHER pointer.
580 * @ingroup hcrypto_evp
583 const EVP_CIPHER *
584 EVP_cc_rc4(void)
586 static const EVP_CIPHER rc4 = {
591 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
592 cc_rc4_init,
593 cc_do_cipher,
594 cc_cleanup,
595 sizeof(struct cc_key),
596 NULL,
597 NULL,
598 NULL,
599 NULL
601 return &rc4;
606 * The RC4-40 cipher type (Apple CommonCrypto provider)
608 * @return the RC4 EVP_CIPHER pointer.
610 * @ingroup hcrypto_evp
613 const EVP_CIPHER *
614 EVP_cc_rc4_40(void)
616 static const EVP_CIPHER rc4_40 = {
621 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
622 cc_rc4_init,
623 cc_do_cipher,
624 cc_cleanup,
625 sizeof(struct cc_key),
626 NULL,
627 NULL,
628 NULL,
629 NULL
631 return &rc4_40;
634 #endif /* __APPLE__ */