9bc84f30e318a7a5cc475edceb96df484b47b0ad
[openafs.git] / src / external / heimdal / hcrypto / evp-cc.c
1 /*
2  * Copyright (c) 2008 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35
36 /* CommonCrypto provider */
37
38 #ifdef __APPLE__
39
40 #include "config.h"
41
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <assert.h>
47
48 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
49 #include <CommonCrypto/CommonDigest.h>
50 #endif
51 #include <CommonCrypto/CommonCryptor.h>
52
53 #include <evp.h>
54 #include <evp-cc.h>
55
56 /*
57  *
58  */
59
60 struct cc_key {
61     CCCryptorRef href;
62 };
63
64 static int
65 cc_do_cipher(EVP_CIPHER_CTX *ctx,
66              unsigned char *out,
67              const unsigned char *in,
68              unsigned int size)
69 {
70     struct cc_key *cc = ctx->cipher_data;
71     CCCryptorStatus ret;
72     size_t moved;
73
74     memcpy(out, in, size);
75
76     ret = CCCryptorUpdate(cc->href, in, size, out, size, &moved);
77     if (ret)
78         return 0;
79
80     if (moved != size)
81         return 0;
82
83     return 1;
84 }
85
86 static int
87 cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx,
88                   unsigned char *out,
89                   const unsigned char *in,
90                   unsigned int size)
91 {
92     struct cc_key *cc = ctx->cipher_data;
93     CCCryptorStatus ret;
94     size_t moved;
95     unsigned int i;
96
97     for (i = 0; i < size; i++) {
98         unsigned char oiv[EVP_MAX_IV_LENGTH + 1];
99
100         assert(ctx->cipher->iv_len + 1 <= sizeof(oiv));
101         memcpy(oiv, ctx->iv, ctx->cipher->iv_len);
102
103         ret = CCCryptorUpdate(cc->href, ctx->iv, ctx->cipher->iv_len,
104                               ctx->iv, ctx->cipher->iv_len, &moved);
105         if (ret)
106             return 0;
107
108         if (moved != ctx->cipher->iv_len)
109             return 0;
110
111         if (!ctx->encrypt)
112             oiv[ctx->cipher->iv_len] = in[i];
113         out[i] = in[i] ^ ctx->iv[0];
114         if (ctx->encrypt)
115             oiv[ctx->cipher->iv_len] = out[i];
116
117         memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len);
118     }
119
120      return 1;
121  }
122
123
124 static int
125 cc_cleanup(EVP_CIPHER_CTX *ctx)
126 {
127     struct cc_key *cc = ctx->cipher_data;
128     if (cc->href)
129         CCCryptorRelease(cc->href);
130     return 1;
131 }
132
133 static int
134 init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
135             size_t keylen, const void *iv, CCCryptorRef *ref)
136 {
137     CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
138     CCCryptorStatus ret;
139
140     if (*ref) {
141         if (key == NULL && iv) {
142             CCCryptorReset(*ref, iv);
143             return 1;
144         }
145         CCCryptorRelease(*ref);
146     }
147
148     ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
149     if (ret)
150         return 0;
151     return 1;
152 }
153
154 static int
155 cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
156                      const unsigned char * key,
157                      const unsigned char * iv,
158                      int encp)
159 {
160     struct cc_key *cc = ctx->cipher_data;
161     return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href);
162 }
163
164 /**
165  * The tripple DES cipher type (Apple CommonCrypto provider)
166  *
167  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
168  *
169  * @ingroup hcrypto_evp
170  */
171
172 const EVP_CIPHER *
173 EVP_cc_des_ede3_cbc(void)
174 {
175     static const EVP_CIPHER des_ede3_cbc = {
176         0,
177         8,
178         24,
179         8,
180         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
181         cc_des_ede3_cbc_init,
182         cc_do_cipher,
183         cc_cleanup,
184         sizeof(struct cc_key),
185         NULL,
186         NULL,
187         NULL,
188         NULL
189     };
190     return &des_ede3_cbc;
191 }
192
193 /*
194  *
195  */
196
197 static int
198 cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
199                 const unsigned char * key,
200                 const unsigned char * iv,
201                 int encp)
202 {
203     struct cc_key *cc = ctx->cipher_data;
204     return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href);
205 }
206
207 /**
208  * The DES cipher type (Apple CommonCrypto provider)
209  *
210  * @return the DES-CBC EVP_CIPHER pointer.
211  *
212  * @ingroup hcrypto_evp
213  */
214
215 const EVP_CIPHER *
216 EVP_cc_des_cbc(void)
217 {
218     static const EVP_CIPHER des_ede3_cbc = {
219         0,
220         kCCBlockSizeDES,
221         kCCBlockSizeDES,
222         kCCBlockSizeDES,
223         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
224         cc_des_cbc_init,
225         cc_do_cipher,
226         cc_cleanup,
227         sizeof(struct cc_key),
228         NULL,
229         NULL,
230         NULL,
231         NULL
232     };
233     return &des_ede3_cbc;
234 }
235
236 /*
237  *
238  */
239
240 static int
241 cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
242                 const unsigned char * key,
243                 const unsigned char * iv,
244                 int encp)
245 {
246     struct cc_key *cc = ctx->cipher_data;
247     return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href);
248 }
249
250 /**
251  * The AES-128 cipher type (Apple CommonCrypto provider)
252  *
253  * @return the AES-128-CBC EVP_CIPHER pointer.
254  *
255  * @ingroup hcrypto_evp
256  */
257
258 const EVP_CIPHER *
259 EVP_cc_aes_128_cbc(void)
260 {
261     static const EVP_CIPHER c = {
262         0,
263         kCCBlockSizeAES128,
264         kCCKeySizeAES128,
265         kCCBlockSizeAES128,
266         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
267         cc_aes_cbc_init,
268         cc_do_cipher,
269         cc_cleanup,
270         sizeof(struct cc_key),
271         NULL,
272         NULL,
273         NULL,
274         NULL
275     };
276     return &c;
277 }
278
279 /**
280  * The AES-192 cipher type (Apple CommonCrypto provider)
281  *
282  * @return the AES-192-CBC EVP_CIPHER pointer.
283  *
284  * @ingroup hcrypto_evp
285  */
286
287 const EVP_CIPHER *
288 EVP_cc_aes_192_cbc(void)
289 {
290     static const EVP_CIPHER c = {
291         0,
292         kCCBlockSizeAES128,
293         kCCKeySizeAES192,
294         kCCBlockSizeAES128,
295         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
296         cc_aes_cbc_init,
297         cc_do_cipher,
298         cc_cleanup,
299         sizeof(struct cc_key),
300         NULL,
301         NULL,
302         NULL,
303         NULL
304     };
305     return &c;
306 }
307
308 /**
309  * The AES-256 cipher type (Apple CommonCrypto provider)
310  *
311  * @return the AES-256-CBC EVP_CIPHER pointer.
312  *
313  * @ingroup hcrypto_evp
314  */
315
316 const EVP_CIPHER *
317 EVP_cc_aes_256_cbc(void)
318 {
319     static const EVP_CIPHER c = {
320         0,
321         kCCBlockSizeAES128,
322         kCCKeySizeAES256,
323         kCCBlockSizeAES128,
324         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
325         cc_aes_cbc_init,
326         cc_do_cipher,
327         cc_cleanup,
328         sizeof(struct cc_key),
329         NULL,
330         NULL,
331         NULL,
332         NULL
333     };
334     return &c;
335 }
336
337 static int
338 cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
339                 const unsigned char * key,
340                 const unsigned char * iv,
341                 int encp)
342 {
343     struct cc_key *cc = ctx->cipher_data;
344     return init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode,
345                        key, ctx->cipher->key_len, NULL, &cc->href);
346 }
347
348 /**
349  * The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
350  *
351  * @return the AES-128-CFB8 EVP_CIPHER pointer.
352  *
353  * @ingroup hcrypto_evp
354  */
355
356 const EVP_CIPHER *
357 EVP_cc_aes_128_cfb8(void)
358 {
359     static const EVP_CIPHER c = {
360         0,
361         1,
362         kCCKeySizeAES128,
363         kCCBlockSizeAES128,
364         EVP_CIPH_CFB8_MODE,
365         cc_aes_cfb8_init,
366         cc_do_cfb8_cipher,
367         cc_cleanup,
368         sizeof(struct cc_key),
369         NULL,
370         NULL,
371         NULL,
372         NULL
373     };
374     return &c;
375 }
376
377 /**
378  * The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
379  *
380  * @return the AES-192-CFB8 EVP_CIPHER pointer.
381  *
382  * @ingroup hcrypto_evp
383  */
384
385 const EVP_CIPHER *
386 EVP_cc_aes_192_cfb8(void)
387 {
388     static const EVP_CIPHER c = {
389         0,
390         1,
391         kCCKeySizeAES192,
392         kCCBlockSizeAES128,
393         EVP_CIPH_CFB8_MODE,
394         cc_aes_cfb8_init,
395         cc_do_cfb8_cipher,
396         cc_cleanup,
397         sizeof(struct cc_key),
398         NULL,
399         NULL,
400         NULL,
401         NULL
402     };
403     return &c;
404 }
405
406 /**
407  * The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
408  *
409  * @return the AES-256-CFB8 EVP_CIPHER pointer.
410  *
411  * @ingroup hcrypto_evp
412  */
413
414 const EVP_CIPHER *
415 EVP_cc_aes_256_cfb8(void)
416 {
417     static const EVP_CIPHER c = {
418         0,
419         1,
420         kCCKeySizeAES256,
421         kCCBlockSizeAES128,
422         EVP_CIPH_CFB8_MODE,
423         cc_aes_cfb8_init,
424         cc_do_cfb8_cipher,
425         cc_cleanup,
426         sizeof(struct cc_key),
427         NULL,
428         NULL,
429         NULL,
430         NULL
431     };
432     return &c;
433 }
434
435 /*
436  *
437  */
438
439 #ifdef COMMONCRYPTO_SUPPORTS_RC2
440 static int
441 cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
442                 const unsigned char * key,
443                 const unsigned char * iv,
444                 int encp)
445 {
446     struct cc_key *cc = ctx->cipher_data;
447     return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href);
448 }
449 #endif
450
451 /**
452  * The RC2 cipher type - common crypto
453  *
454  * @return the RC2 EVP_CIPHER pointer.
455  *
456  * @ingroup hcrypto_evp
457  */
458
459
460 const EVP_CIPHER *
461 EVP_cc_rc2_cbc(void)
462 {
463 #ifdef COMMONCRYPTO_SUPPORTS_RC2
464     static const EVP_CIPHER rc2_cbc = {
465         0,
466         kCCBlockSizeRC2,
467         16,
468         kCCBlockSizeRC2,
469         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
470         cc_rc2_cbc_init,
471         cc_do_cipher,
472         cc_cleanup,
473         sizeof(struct cc_key),
474         NULL,
475         NULL,
476         NULL,
477         NULL
478     };
479     return &rc2_cbc;
480 #else
481     return NULL;
482 #endif
483 }
484
485 /**
486  * The RC2-40 cipher type - common crypto
487  *
488  * @return the RC2-40 EVP_CIPHER pointer.
489  *
490  * @ingroup hcrypto_evp
491  */
492
493
494 const EVP_CIPHER *
495 EVP_cc_rc2_40_cbc(void)
496 {
497 #ifdef COMMONCRYPTO_SUPPORTS_RC2
498     static const EVP_CIPHER rc2_40_cbc = {
499         0,
500         kCCBlockSizeRC2,
501         5,
502         kCCBlockSizeRC2,
503         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
504         cc_rc2_cbc_init,
505         cc_do_cipher,
506         cc_cleanup,
507         sizeof(struct cc_key),
508         NULL,
509         NULL,
510         NULL,
511         NULL
512     };
513     return &rc2_40_cbc;
514 #else
515     return NULL;
516 #endif
517 }
518
519
520 /**
521  * The RC2-64 cipher type - common crypto
522  *
523  * @return the RC2-64 EVP_CIPHER pointer.
524  *
525  * @ingroup hcrypto_evp
526  */
527
528
529 const EVP_CIPHER *
530 EVP_cc_rc2_64_cbc(void)
531 {
532 #ifdef COMMONCRYPTO_SUPPORTS_RC2
533     static const EVP_CIPHER rc2_64_cbc = {
534         0,
535         kCCBlockSizeRC2,
536         8,
537         kCCBlockSizeRC2,
538         EVP_CIPH_CBC_MODE|EVP_CIPH_ALWAYS_CALL_INIT,
539         cc_rc2_cbc_init,
540         cc_do_cipher,
541         cc_cleanup,
542         sizeof(struct cc_key),
543         NULL,
544         NULL,
545         NULL,
546         NULL
547     };
548     return &rc2_64_cbc;
549 #else
550     return NULL;
551 #endif
552 }
553
554 /**
555  * The CommonCrypto md2 provider
556  *
557  * @ingroup hcrypto_evp
558  */
559
560 const EVP_MD *
561 EVP_cc_md2(void)
562 {
563 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
564     static const struct hc_evp_md md2 = {
565         CC_MD2_DIGEST_LENGTH,
566         CC_MD2_BLOCK_BYTES,
567         sizeof(CC_MD2_CTX),
568         (hc_evp_md_init)CC_MD2_Init,
569         (hc_evp_md_update)CC_MD2_Update,
570         (hc_evp_md_final)CC_MD2_Final,
571         (hc_evp_md_cleanup)NULL
572     };
573     return &md2;
574 #else
575     return NULL;
576 #endif
577 }
578
579 /**
580  * The CommonCrypto md4 provider
581  *
582  * @ingroup hcrypto_evp
583  */
584
585 const EVP_MD *
586 EVP_cc_md4(void)
587 {
588 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
589     static const struct hc_evp_md md4 = {
590         CC_MD4_DIGEST_LENGTH,
591         CC_MD4_BLOCK_BYTES,
592         sizeof(CC_MD4_CTX),
593         (hc_evp_md_init)CC_MD4_Init,
594         (hc_evp_md_update)CC_MD4_Update,
595         (hc_evp_md_final)CC_MD4_Final,
596         (hc_evp_md_cleanup)NULL
597     };
598     return &md4;
599 #else
600     return NULL;
601 #endif
602 }
603
604 /**
605  * The CommonCrypto md5 provider
606  *
607  * @ingroup hcrypto_evp
608  */
609
610 const EVP_MD *
611 EVP_cc_md5(void)
612 {
613 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
614     static const struct hc_evp_md md5 = {
615         CC_MD5_DIGEST_LENGTH,
616         CC_MD5_BLOCK_BYTES,
617         sizeof(CC_MD5_CTX),
618         (hc_evp_md_init)CC_MD5_Init,
619         (hc_evp_md_update)CC_MD5_Update,
620         (hc_evp_md_final)CC_MD5_Final,
621         (hc_evp_md_cleanup)NULL
622     };
623     return &md5;
624 #else
625     return NULL;
626 #endif
627 }
628
629 /**
630  * The CommonCrypto sha1 provider
631  *
632  * @ingroup hcrypto_evp
633  */
634
635 const EVP_MD *
636 EVP_cc_sha1(void)
637 {
638 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
639     static const struct hc_evp_md sha1 = {
640         CC_SHA1_DIGEST_LENGTH,
641         CC_SHA1_BLOCK_BYTES,
642         sizeof(CC_SHA1_CTX),
643         (hc_evp_md_init)CC_SHA1_Init,
644         (hc_evp_md_update)CC_SHA1_Update,
645         (hc_evp_md_final)CC_SHA1_Final,
646         (hc_evp_md_cleanup)NULL
647     };
648     return &sha1;
649 #else
650     return NULL;
651 #endif
652 }
653
654 /**
655  * The CommonCrypto sha256 provider
656  *
657  * @ingroup hcrypto_evp
658  */
659
660 const EVP_MD *
661 EVP_cc_sha256(void)
662 {
663 #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
664     static const struct hc_evp_md sha256 = {
665         CC_SHA256_DIGEST_LENGTH,
666         CC_SHA256_BLOCK_BYTES,
667         sizeof(CC_SHA256_CTX),
668         (hc_evp_md_init)CC_SHA256_Init,
669         (hc_evp_md_update)CC_SHA256_Update,
670         (hc_evp_md_final)CC_SHA256_Final,
671         (hc_evp_md_cleanup)NULL
672     };
673     return &sha256;
674 #else
675     return NULL;
676 #endif
677 }
678
679 /**
680  * The Camellia-128 cipher type - CommonCrypto
681  *
682  * @return the Camellia-128 EVP_CIPHER pointer.
683  *
684  * @ingroup hcrypto_evp
685  */
686
687 const EVP_CIPHER *
688 EVP_cc_camellia_128_cbc(void)
689 {
690     return NULL;
691 }
692
693 /**
694  * The Camellia-198 cipher type - CommonCrypto
695  *
696  * @return the Camellia-198 EVP_CIPHER pointer.
697  *
698  * @ingroup hcrypto_evp
699  */
700
701 const EVP_CIPHER *
702 EVP_cc_camellia_192_cbc(void)
703 {
704     return NULL;
705 }
706
707 /**
708  * The Camellia-256 cipher type - CommonCrypto
709  *
710  * @return the Camellia-256 EVP_CIPHER pointer.
711  *
712  * @ingroup hcrypto_evp
713  */
714
715 const EVP_CIPHER *
716 EVP_cc_camellia_256_cbc(void)
717 {
718     return NULL;
719 }
720
721 /*
722  *
723  */
724
725 static int
726 cc_rc4_init(EVP_CIPHER_CTX *ctx,
727             const unsigned char * key,
728             const unsigned char * iv,
729             int encp)
730 {
731     struct cc_key *cc = ctx->cipher_data;
732     return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href);
733 }
734
735 /**
736  * The RC4 cipher type (Apple CommonCrypto provider)
737  *
738  * @return the RC4 EVP_CIPHER pointer.
739  *
740  * @ingroup hcrypto_evp
741  */
742
743 const EVP_CIPHER *
744 EVP_cc_rc4(void)
745 {
746     static const EVP_CIPHER rc4 = {
747         0,
748         1,
749         16,
750         0,
751         EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
752         cc_rc4_init,
753         cc_do_cipher,
754         cc_cleanup,
755         sizeof(struct cc_key),
756         NULL,
757         NULL,
758         NULL,
759         NULL
760     };
761     return &rc4;
762 }
763
764
765 /**
766  * The RC4-40 cipher type (Apple CommonCrypto provider)
767  *
768  * @return the RC4 EVP_CIPHER pointer.
769  *
770  * @ingroup hcrypto_evp
771  */
772
773 const EVP_CIPHER *
774 EVP_cc_rc4_40(void)
775 {
776     static const EVP_CIPHER rc4_40 = {
777         0,
778         1,
779         5,
780         0,
781         EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH,
782         cc_rc4_init,
783         cc_do_cipher,
784         cc_cleanup,
785         sizeof(struct cc_key),
786         NULL,
787         NULL,
788         NULL,
789         NULL
790     };
791     return &rc4_40;
792 }
793
794 #endif /* __APPLE__ */