Clarify documentation of password quality check modules
[heimdal.git] / lib / hcrypto / evp-cc.c
blob8b640516a25d341aa68487b4895223020881ccd4
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 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
47 #include <CommonCrypto/CommonDigest.h>
48 #endif
49 #include <CommonCrypto/CommonCryptor.h>
51 #include <evp.h>
52 #include <evp-cc.h>
58 struct cc_key {
59 CCCryptorRef href;
62 static int
63 cc_do_cipher(EVP_CIPHER_CTX *ctx,
64 unsigned char *out,
65 const unsigned char *in,
66 unsigned int size)
68 struct cc_key *cc = ctx->cipher_data;
69 CCCryptorStatus ret;
70 size_t moved;
72 memcpy(out, in, size);
74 ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
75 if (ret)
76 return 0;
78 if (moved != size)
79 return 0;
81 return 1;
84 static int
85 cc_cleanup(EVP_CIPHER_CTX *ctx)
87 struct cc_key *cc = ctx->cipher_data;
88 if (cc->href)
89 CCCryptorRelease(cc->href);
90 return 1;
93 static int
94 init_cc_key(int encp, CCAlgorithm alg, const void *key,
95 size_t keylen, const void *iv, CCCryptorRef *ref)
97 CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
98 CCCryptorStatus ret;
100 if (*ref) {
101 if (key == NULL && iv) {
102 CCCryptorReset(*ref, iv);
103 return 1;
105 CCCryptorRelease(*ref);
108 ret = CCCryptorCreate(op, alg, 0, key, keylen, iv, ref);
109 if (ret)
110 return 0;
111 return 1;
114 static int
115 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
116 const unsigned char * key,
117 const unsigned char * iv,
118 int encp)
120 struct cc_key *cc = ctx->cipher_data;
121 return init_cc_key(encp, kCCAlgorithm3DES, key, kCCKeySize3DES, iv, &cc->href);
125 * The tripple DES cipher type (Apple CommonCrypto provider)
127 * @return the DES-EDE3-CBC EVP_CIPHER pointer.
129 * @ingroup hcrypto_evp
132 const EVP_CIPHER *
133 EVP_cc_des_ede3_cbc(void)
135 static const EVP_CIPHER des_ede3_cbc = {
140 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
141 cc_des_ede3_cbc_init,
142 cc_do_cipher,
143 cc_cleanup,
144 sizeof(struct cc_key),
145 NULL,
146 NULL,
147 NULL,
148 NULL
150 return &des_ede3_cbc;
157 static int
158 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
159 const unsigned char * key,
160 const unsigned char * iv,
161 int encp)
163 struct cc_key *cc = ctx->cipher_data;
164 return init_cc_key(encp, kCCAlgorithmDES, key, kCCBlockSizeDES, iv, &cc->href);
168 * The DES cipher type (Apple CommonCrypto provider)
170 * @return the DES-CBC EVP_CIPHER pointer.
172 * @ingroup hcrypto_evp
175 const EVP_CIPHER *
176 EVP_cc_des_cbc(void)
178 static const EVP_CIPHER des_ede3_cbc = {
180 kCCBlockSizeDES,
181 kCCBlockSizeDES,
182 kCCBlockSizeDES,
183 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
184 cc_des_cbc_init,
185 cc_do_cipher,
186 cc_cleanup,
187 sizeof(struct cc_key),
188 NULL,
189 NULL,
190 NULL,
191 NULL
193 return &des_ede3_cbc;
200 static int
201 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
202 const unsigned char * key,
203 const unsigned char * iv,
204 int encp)
206 struct cc_key *cc = ctx->cipher_data;
207 return init_cc_key(encp, kCCAlgorithmAES128, key, ctx->cipher->key_len, iv, &cc->href);
211 * The AES-128 cipher type (Apple CommonCrypto provider)
213 * @return the AES-128-CBC EVP_CIPHER pointer.
215 * @ingroup hcrypto_evp
218 const EVP_CIPHER *
219 EVP_cc_aes_128_cbc(void)
221 static const EVP_CIPHER c = {
223 kCCBlockSizeAES128,
224 kCCKeySizeAES128,
225 kCCBlockSizeAES128,
226 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
227 cc_aes_cbc_init,
228 cc_do_cipher,
229 cc_cleanup,
230 sizeof(struct cc_key),
231 NULL,
232 NULL,
233 NULL,
234 NULL
236 return &c;
240 * The AES-192 cipher type (Apple CommonCrypto provider)
242 * @return the AES-192-CBC EVP_CIPHER pointer.
244 * @ingroup hcrypto_evp
247 const EVP_CIPHER *
248 EVP_cc_aes_192_cbc(void)
250 static const EVP_CIPHER c = {
252 kCCBlockSizeAES128,
253 kCCKeySizeAES192,
254 kCCBlockSizeAES128,
255 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
256 cc_aes_cbc_init,
257 cc_do_cipher,
258 cc_cleanup,
259 sizeof(struct cc_key),
260 NULL,
261 NULL,
262 NULL,
263 NULL
265 return &c;
269 * The AES-256 cipher type (Apple CommonCrypto provider)
271 * @return the AES-256-CBC EVP_CIPHER pointer.
273 * @ingroup hcrypto_evp
276 const EVP_CIPHER *
277 EVP_cc_aes_256_cbc(void)
279 static const EVP_CIPHER c = {
281 kCCBlockSizeAES128,
282 kCCKeySizeAES256,
283 kCCBlockSizeAES128,
284 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
285 cc_aes_cbc_init,
286 cc_do_cipher,
287 cc_cleanup,
288 sizeof(struct cc_key),
289 NULL,
290 NULL,
291 NULL,
292 NULL
294 return &c;
301 #ifdef COMMONCRYPTO_SUPPORTS_RC2
302 static int
303 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
304 const unsigned char * key,
305 const unsigned char * iv,
306 int encp)
308 struct cc_key *cc = ctx->cipher_data;
309 return init_cc_key(encp, kCCAlgorithmRC2, key, ctx->cipher->key_len, iv, &cc->href);
311 #endif
314 * The RC2 cipher type - common crypto
316 * @return the RC2 EVP_CIPHER pointer.
318 * @ingroup hcrypto_evp
322 const EVP_CIPHER *
323 EVP_cc_rc2_cbc(void)
325 #ifdef COMMONCRYPTO_SUPPORTS_RC2
326 static const EVP_CIPHER rc2_cbc = {
328 kCCBlockSizeRC2,
330 kCCBlockSizeRC2,
331 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
332 cc_rc2_cbc_init,
333 cc_do_cipher,
334 cc_cleanup,
335 sizeof(struct cc_key),
336 NULL,
337 NULL,
338 NULL,
339 NULL
341 return &rc2_cbc;
342 #else
343 return NULL;
344 #endif
348 * The RC2-40 cipher type - common crypto
350 * @return the RC2-40 EVP_CIPHER pointer.
352 * @ingroup hcrypto_evp
356 const EVP_CIPHER *
357 EVP_cc_rc2_40_cbc(void)
359 #ifdef COMMONCRYPTO_SUPPORTS_RC2
360 static const EVP_CIPHER rc2_40_cbc = {
362 kCCBlockSizeRC2,
364 kCCBlockSizeRC2,
365 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
366 cc_rc2_cbc_init,
367 cc_do_cipher,
368 cc_cleanup,
369 sizeof(struct cc_key),
370 NULL,
371 NULL,
372 NULL,
373 NULL
375 return &rc2_40_cbc;
376 #else
377 return NULL;
378 #endif
383 * The RC2-64 cipher type - common crypto
385 * @return the RC2-64 EVP_CIPHER pointer.
387 * @ingroup hcrypto_evp
391 const EVP_CIPHER *
392 EVP_cc_rc2_64_cbc(void)
394 #ifdef COMMONCRYPTO_SUPPORTS_RC2
395 static const EVP_CIPHER rc2_64_cbc = {
397 kCCBlockSizeRC2,
399 kCCBlockSizeRC2,
400 EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
401 cc_rc2_cbc_init,
402 cc_do_cipher,
403 cc_cleanup,
404 sizeof(struct cc_key),
405 NULL,
406 NULL,
407 NULL,
408 NULL
410 return &rc2_64_cbc;
411 #else
412 return NULL;
413 #endif
417 * The CommonCrypto md2 provider
419 * @ingroup hcrypto_evp
422 const EVP_MD *
423 EVP_cc_md2(void)
425 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
426 static const struct hc_evp_md md2 = {
427 CC_MD2_DIGEST_LENGTH,
428 CC_MD2_BLOCK_BYTES,
429 sizeof(CC_MD2_CTX),
430 (hc_evp_md_init)CC_MD2_Init,
431 (hc_evp_md_update)CC_MD2_Update,
432 (hc_evp_md_final)CC_MD2_Final,
433 (hc_evp_md_cleanup)NULL
435 return &md2;
436 #else
437 return NULL;
438 #endif
442 * The CommonCrypto md4 provider
444 * @ingroup hcrypto_evp
447 const EVP_MD *
448 EVP_cc_md4(void)
450 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
451 static const struct hc_evp_md md4 = {
452 CC_MD4_DIGEST_LENGTH,
453 CC_MD4_BLOCK_BYTES,
454 sizeof(CC_MD4_CTX),
455 (hc_evp_md_init)CC_MD4_Init,
456 (hc_evp_md_update)CC_MD4_Update,
457 (hc_evp_md_final)CC_MD4_Final,
458 (hc_evp_md_cleanup)NULL
460 return &md4;
461 #else
462 return NULL;
463 #endif
467 * The CommonCrypto md5 provider
469 * @ingroup hcrypto_evp
472 const EVP_MD *
473 EVP_cc_md5(void)
475 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
476 static const struct hc_evp_md md5 = {
477 CC_MD5_DIGEST_LENGTH,
478 CC_MD5_BLOCK_BYTES,
479 sizeof(CC_MD5_CTX),
480 (hc_evp_md_init)CC_MD5_Init,
481 (hc_evp_md_update)CC_MD5_Update,
482 (hc_evp_md_final)CC_MD5_Final,
483 (hc_evp_md_cleanup)NULL
485 return &md5;
486 #else
487 return NULL;
488 #endif
492 * The CommonCrypto sha1 provider
494 * @ingroup hcrypto_evp
497 const EVP_MD *
498 EVP_cc_sha1(void)
500 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
501 static const struct hc_evp_md sha1 = {
502 CC_SHA1_DIGEST_LENGTH,
503 CC_SHA1_BLOCK_BYTES,
504 sizeof(CC_SHA1_CTX),
505 (hc_evp_md_init)CC_SHA1_Init,
506 (hc_evp_md_update)CC_SHA1_Update,
507 (hc_evp_md_final)CC_SHA1_Final,
508 (hc_evp_md_cleanup)NULL
510 return &sha1;
511 #else
512 return NULL;
513 #endif
517 * The CommonCrypto sha256 provider
519 * @ingroup hcrypto_evp
522 const EVP_MD *
523 EVP_cc_sha256(void)
525 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
526 static const struct hc_evp_md sha256 = {
527 CC_SHA256_DIGEST_LENGTH,
528 CC_SHA256_BLOCK_BYTES,
529 sizeof(CC_SHA256_CTX),
530 (hc_evp_md_init)CC_SHA256_Init,
531 (hc_evp_md_update)CC_SHA256_Update,
532 (hc_evp_md_final)CC_SHA256_Final,
533 (hc_evp_md_cleanup)NULL
535 return &sha256;
536 #else
537 return NULL;
538 #endif
542 * The Camellia-128 cipher type - CommonCrypto
544 * @return the Camellia-128 EVP_CIPHER pointer.
546 * @ingroup hcrypto_evp
549 const EVP_CIPHER *
550 EVP_cc_camellia_128_cbc(void)
552 return NULL;
556 * The Camellia-198 cipher type - CommonCrypto
558 * @return the Camellia-198 EVP_CIPHER pointer.
560 * @ingroup hcrypto_evp
563 const EVP_CIPHER *
564 EVP_cc_camellia_192_cbc(void)
566 return NULL;
570 * The Camellia-256 cipher type - CommonCrypto
572 * @return the Camellia-256 EVP_CIPHER pointer.
574 * @ingroup hcrypto_evp
577 const EVP_CIPHER *
578 EVP_cc_camellia_256_cbc(void)
580 return NULL;
587 static int
588 cc_rc4_init(EVP_CIPHER_CTX *ctx,
589 const unsigned char * key,
590 const unsigned char * iv,
591 int encp)
593 struct cc_key *cc = ctx->cipher_data;
594 return init_cc_key(encp, kCCAlgorithmRC4, key, ctx->key_len, iv, &cc->href);
598 * The RC4 cipher type (Apple CommonCrypto provider)
600 * @return the RC4 EVP_CIPHER pointer.
602 * @ingroup hcrypto_evp
605 const EVP_CIPHER *
606 EVP_cc_rc4(void)
608 static const EVP_CIPHER rc4 = {
613 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
614 cc_rc4_init,
615 cc_do_cipher,
616 cc_cleanup,
617 sizeof(struct cc_key),
618 NULL,
619 NULL,
620 NULL,
621 NULL
623 return &rc4;
628 * The RC4-40 cipher type (Apple CommonCrypto provider)
630 * @return the RC4 EVP_CIPHER pointer.
632 * @ingroup hcrypto_evp
635 const EVP_CIPHER *
636 EVP_cc_rc4_40(void)
638 static const EVP_CIPHER rc4_40 = {
643 EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
644 cc_rc4_init,
645 cc_do_cipher,
646 cc_cleanup,
647 sizeof(struct cc_key),
648 NULL,
649 NULL,
650 NULL,
651 NULL
653 return &rc4_40;
656 #endif /* __APPLE__ */