2 * Copyright (c) 1997,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _COMCRYPTION_H_
25 #define _COMCRYPTION_H_
35 CCR_SUCCESS
= 0, // normal result
36 CCR_OUTBUFFER_TOO_SMALL
, // caller needs to alloc more out buffer
37 CCR_MEMORY_ERROR
, // internal error
38 CCR_WRONG_VERSION
, // compatibility error
39 CCR_BAD_CIPHERTEXT
, // can't decrypt ciphertext stream
40 CCR_INTERNAL
// internal library error
44 * Used to specify optimization in ComcryptInit(). May be ignored in
45 * early implementation.
48 CCO_DEFAULT
, // let the low-level code decide
49 CCO_SIZE
, // optimize for max compression
50 CCO_SECURITY
, // optimize for max crypto security
51 CCO_TIME
, // optimize for minimum runtime; implies no
52 // second-level comcryption; security not
54 CCO_TIME_SIZE
, // minimum runtime with second-level
55 // comcryption enabled; implies loss of
57 CCO_ASCII
, // optimize for max compression for ASCII
63 * Used to specify operation type.
71 * Used to specify End of stream.
74 CCE_MORE_TO_COME
, // more ops to follow
75 CCE_END_OF_STREAM
// end of stream, close output strem
79 * Maximum key length in bytes.
81 #define COMCRYPT_MAX_KEYLENGTH 64
84 * Clients can *optionally* register external memory alloc/free functions here.
86 typedef void *(comMallocExternFcn
)(unsigned size
);
87 typedef void (comFreeExternFcn
)(void *data
);
88 void comMallocRegister(comMallocExternFcn
*mallocExtern
,
89 comFreeExternFcn
*freeExtern
);
92 * Opaque data type for ComCryptData() and DeComCryptData()
94 typedef void *comcryptObj
;
97 * Call once at startup. The resulting comcryptObj can be reused multiple
100 comcryptObj
comcryptAlloc(void);
103 * Use this before starting every stream process
105 comcryptReturn
comcryptInit(
107 const unsigned char *key
,
109 comcryptOptimize optimize
); // CCO_SIZE, etc.
112 * Free a comcryptObj object obtained via comcryptAlloc()
114 void comcryptObjFree(comcryptObj cobj
);
117 * Return the maximum input buffer size allowed for for specified
118 * output buffer size. Note that for both comcrypt and decomcrypt,
119 * to cover the worst case, the output buffer always has to be
120 * larger that the input buffer.
122 unsigned comcryptMaxInBufSize(comcryptObj cobj
,
124 comcryptOp op
); // CCOP_COMCRYPT, etc.
127 * Return the maximum output buffer size for specified input buffer size.
128 * Output buffer size will always be larger than input buffer size.
130 unsigned comcryptMaxOutBufSize(comcryptObj cobj
,
132 comcryptOp op
, // CCOP_COMCRYPT, etc.
133 char final
); // nonzero for last op
134 // only used for CCOP_DECOMCRYPT
137 * the one-function-fits-all comcrypt routine -
138 * call it multiple times for one ComcryptObj if
139 * you want, or just once to do a whole stream
142 * NOTE: in the current implementation, the endOfStream is not used;
143 * no "final" call is necessary on comcryption.
145 comcryptReturn
comcryptData(
147 unsigned char *plainText
,
148 unsigned plainTextLen
,
149 unsigned char *cipherText
, // malloc'd by caller
150 unsigned *cipherTextLen
, // IN/OUT
151 comcryptEos endOfStream
); // CCE_END_OF_STREAM, etc.
154 * decomcrypt routine - call it multiple times for
155 * one comcryptObj, or just once to do a whole stream
156 * in one shot. Boundaries of ciphertext segments -
157 * across calls to this function - are arbitrary.
159 * NOTE: in the current implementation, the final call to this (when
160 * endOfStrem == CCE_END_OF_STREAM) must contain a nonzero amount of
163 comcryptReturn
deComcryptData(
165 unsigned char *cipherText
,
166 unsigned cipherTextLen
,
167 unsigned char *plainText
,
168 unsigned *plainTextLen
, // IN/OUT
169 comcryptEos endOfStream
); // CCE_END_OF_STREAM, etc.
175 #endif /*_COMCRYPTION_H_*/