]> git.saurik.com Git - apple/security.git/blob - libsecurity_ssl/lib/symCipher.c
Security-55179.1.tar.gz
[apple/security.git] / libsecurity_ssl / lib / symCipher.c
1 /*
2 * Copyright (c) 1999-2001,2005-2008,2010-2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * symCipher.c - CDSA-based symmetric cipher module
26 */
27
28 #include "sslContext.h"
29 #include "cryptType.h"
30 #include "sslDebug.h"
31 #include "sslMemory.h"
32 #include <CommonCrypto/CommonCryptor.h>
33 #include "symCipher.h"
34
35 /*
36 * CommonCrypto-based symmetric cipher callouts
37 */
38 OSStatus CCSymmInit(
39 uint8_t *key,
40 uint8_t* iv,
41 CipherContext *cipherCtx,
42 SSLContext *ctx)
43 {
44 /*
45 * Cook up a CCCryptorRef. Assumes:
46 * cipherCtx->symCipher.keyAlg
47 * cipherCtx->encrypting
48 * key (raw key bytes)
49 * iv (raw bytes)
50 * On successful exit:
51 * Resulting CCCryptorRef --> cipherCtx->cryptorRef
52 */
53 CCCryptorStatus ccrtn;
54 CCOperation op = cipherCtx->encrypting ? kCCEncrypt : kCCDecrypt;
55
56 if(cipherCtx->cryptorRef) {
57 CCCryptorRelease(cipherCtx->cryptorRef);
58 cipherCtx->cryptorRef = NULL;
59 }
60
61 ccrtn = CCCryptorCreate(op, cipherCtx->symCipher->keyAlg,
62 0, /* options - no padding, default CBC */
63 key, cipherCtx->symCipher->keySize,
64 iv,
65 &cipherCtx->cryptorRef);
66 if(ccrtn) {
67 sslErrorLog("CCCryptorCreate returned %d\n", (int)ccrtn);
68 return internalComponentErr;
69 }
70 return noErr;
71 }
72
73 /* same for en/decrypt */
74 OSStatus CCSymmEncryptDecrypt(
75 const uint8_t *src,
76 uint8_t *dest,
77 size_t len,
78 CipherContext *cipherCtx,
79 SSLContext *ctx)
80 {
81 CCCryptorStatus ccrtn;
82
83 ASSERT(cipherCtx != NULL);
84 ASSERT(cipherCtx->cryptorRef != NULL);
85 if(cipherCtx->cryptorRef == NULL) {
86 sslErrorLog("CCSymmEncryptDecrypt: NULL cryptorRef\n");
87 return internalComponentErr;
88 }
89 size_t data_moved;
90 ccrtn = CCCryptorUpdate(cipherCtx->cryptorRef, src, len,
91 dest, len, &data_moved);
92 assert(data_moved == len);
93 #if SSL_DEBUG
94 if(ccrtn) {
95 sslErrorLog("CCSymmEncryptDecrypt: returned %d\n", (int)ccrtn);
96 return internalComponentErr;
97 }
98 #endif
99 return noErr;
100 }
101
102 OSStatus CCSymmFinish(
103 CipherContext *cipherCtx,
104 SSLContext *ctx)
105 {
106 if(cipherCtx->cryptorRef) {
107 CCCryptorRelease(cipherCtx->cryptorRef);
108 cipherCtx->cryptorRef = NULL;
109 }
110 return noErr;
111 }
112