1/*
2 * Copyright (c) 2010 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#ifndef	_CC_DigestSPI_H_
25#define _CC_DigestSPI_H_
26
27#include <stdint.h>
28#include <sys/types.h>
29
30#include <Availability.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36
37/*!
38    @enum       CCDigestAlgorithm
39    @abstract   Algorithms implemented in this module.
40
41    @constant 	kCCDigestNone		Digest Selector for "no digest"
42    @constant 	kCCDigestMD2		MD2 digest
43    @constant 	kCCDigestMD4		MD4 digest
44    @constant 	kCCDigestMD5		MD5 digest
45    @constant 	kCCDigestRMD128		RMD 128 bit digest
46    @constant 	kCCDigestRMD160		RMD 160 bit digest
47    @constant 	kCCDigestRMD256		RMD 256 bit digest
48    @constant 	kCCDigestRMD320		RMD 320 bit digest
49    @constant 	kCCDigestSHA1		SHA-1 digest
50    @constant 	kCCDigestSHA224		SHA-2 224 bit digest
51    @constant 	kCCDigestSHA256		SHA-2 256 bit digest
52    @constant 	kCCDigestSHA384		SHA-2 384 bit digest
53    @constant 	kCCDigestSHA512		SHA-2 512 bit digest
54    @constant 	kCCDigestSkein128	Skein 128 bit digest
55    @constant 	kCCDigestSkein160	Skein 160 bit digest
56    @constant 	kCCDigestSkein224	Skein 224 bit digest
57    @constant 	kCCDigestSkein256	Skein 256 bit digest
58    @constant 	kCCDigestSkein384	Skein 384 bit digest
59    @constant 	kCCDigestSkein512	Skein 512 bit digest
60 */
61
62enum {
63    kCCDigestNone               = 0,
64	kCCDigestMD2				= 1,
65	kCCDigestMD4				= 2,
66	kCCDigestMD5				= 3,
67	kCCDigestRMD128				= 4,
68	kCCDigestRMD160				= 5,
69	kCCDigestRMD256				= 6,
70	kCCDigestRMD320				= 7,
71	kCCDigestSHA1				= 8,
72	kCCDigestSHA224				= 9,
73	kCCDigestSHA256				= 10,
74	kCCDigestSHA384				= 11,
75	kCCDigestSHA512				= 12,
76	kCCDigestSkein128			= 13, // Deprecated in iPhoneOS 6.0 and MacOSX10.9
77	kCCDigestSkein160			= 14, // Deprecated in iPhoneOS 6.0 and MacOSX10.9
78	kCCDigestSkein224			= 16, // Deprecated in iPhoneOS 6.0 and MacOSX10.9
79	kCCDigestSkein256			= 17, // Deprecated in iPhoneOS 6.0 and MacOSX10.9
80	kCCDigestSkein384			= 18, // Deprecated in iPhoneOS 6.0 and MacOSX10.9
81	kCCDigestSkein512			= 19, // Deprecated in iPhoneOS 6.0 and MacOSX10.9
82};
83typedef uint32_t CCDigestAlgorithm;
84
85// Hold this until Heimdal has changed.
86
87#define CCDigestAlg CCDigestAlgorithm
88
89/*!
90    @typedef    CCDigestCtx
91    @abstract   Digest context.
92 */
93
94#define CC_DIGEST_SIZE 1032
95typedef struct CCDigestCtx_t {
96    uint8_t context[CC_DIGEST_SIZE];
97} CCDigestCtx, *CCDigestRef;
98
99#define CC_RMD128_DIGEST_LENGTH   16          /* digest length in bytes */
100#define CC_RMD128_BLOCK_BYTES     64          /* block size in bytes */
101#define CC_RMD128_BLOCK_LONG      (CC_RMD128_BLOCK_BYTES / sizeof(CC_LONG))
102
103#define CC_RMD160_DIGEST_LENGTH   20          /* digest length in bytes */
104#define CC_RMD160_BLOCK_BYTES     64          /* block size in bytes */
105#define CC_RMD160_BLOCK_LONG      (CC_RMD160_BLOCK_BYTES / sizeof(CC_LONG))
106
107#define CC_RMD256_DIGEST_LENGTH   32          /* digest length in bytes */
108#define CC_RMD256_BLOCK_BYTES     64          /* block size in bytes */
109#define CC_RMD256_BLOCK_LONG      (CC_RMD256_BLOCK_BYTES / sizeof(CC_LONG))
110
111#define CC_RMD320_DIGEST_LENGTH   40          /* digest length in bytes */
112#define CC_RMD320_BLOCK_BYTES     64          /* block size in bytes */
113#define CC_RMD320_BLOCK_LONG      (CC_RMD320_BLOCK_BYTES / sizeof(CC_LONG))
114
115/**************************************************************************/
116/* SPI Only                                                               */
117/**************************************************************************/
118
119/*
120 * This information will remain SPI - internal functions available
121 * to callers not needing a stable ABI that have a need to provide
122 * their own memory for use as contexts and return digest values.
123 */
124
125
126/*!
127    @function   CCDigestInit
128    @abstract   Initialize a CCDigestCtx for a digest.
129
130    @param      algorithm   Digest algorithm to perform.
131    @param      ctx         A digest context.
132
133    returns 0 on success.
134 */
135
136int
137CCDigestInit(CCDigestAlgorithm algorithm, CCDigestRef ctx)
138__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
139
140
141
142/**************************************************************************/
143/* Future API                                                             */
144/**************************************************************************/
145
146/*
147 * These functions will be put out for API review after this release.  For
148 * right now we're "road testing" them internally.
149 */
150
151/*!
152    @function   CCDigest
153    @abstract   Stateless, one-shot Digest function.
154
155    @param      algorithm   Digest algorithm to perform.
156    @param      data        The data to digest.
157    @param      length      The length of the data to digest.
158    @param      output      The digest bytes (space provided by the caller).
159
160    Output is written to caller-supplied buffer, as in CCDigestFinal().
161 */
162
163int
164CCDigest(CCDigestAlgorithm algorithm,
165         const uint8_t *data, size_t length, uint8_t *output)
166__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
167
168/*!
169    @function   CCDigestCreate
170    @abstract   Allocate and initialize a CCDigestCtx for a digest.
171
172    @param      algorithm   Digest algorithm to setup.
173
174    returns a pointer to a digestRef on success.
175 */
176
177CCDigestRef
178CCDigestCreate(CCDigestAlgorithm alg)
179__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
180
181/*!
182    @function   CCDigestUpdate
183    @abstract   Continue to digest data.
184
185    @param      ctx         A digest context.
186    @param      data        The data to digest.
187    @param      length      The length of the data to digest.
188
189    returns 0 on success.
190 */
191
192int
193CCDigestUpdate(CCDigestRef ctx, const void *data, size_t length)
194__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
195
196/*!
197    @function   CCDigestFinal
198    @abstract   Conclude digest operations and produce the digest output.
199
200    @param      ctx         A digest context.
201    @param      output      The digest bytes (space provided by the caller).
202
203    returns 0 on success.
204 */
205
206int
207CCDigestFinal(CCDigestRef ctx, uint8_t *output)
208__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
209/*!
210    @function   CCDigestDestroy
211    @abstract   Clear and free a CCDigestCtx
212
213    @param      ctx         A digest context.
214 */
215
216
217void
218CCDigestDestroy(CCDigestRef ctx)
219__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
220
221/*!
222    @function   CCDigestReset
223    @abstract   Clear and re-initialize a CCDigestCtx for the same algorithm.
224
225    @param      ctx         A digest context.
226 */
227
228void
229CCDigestReset(CCDigestRef ctx)
230__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
231
232/*!
233    @function   CCDigestRefGetDigest
234    @abstract   Produce the digest output result for the bytes currently
235                processed.
236
237    @param      ctx         A digest context.
238    @param      output      The digest bytes (space provided by the caller).
239
240    returns 0 on success.
241 */
242
243int
244CCDigestGetDigest(CCDigestRef ctx, uint8_t *output)
245__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
246
247/*!
248 @function   CCDigestGetBlockSize
249 @abstract   Provides the block size of the digest algorithm
250
251 @param      algorithm         A digest algorithm selector.
252
253 returns 0 on failure or the block size on success.
254 */
255
256size_t
257CCDigestGetBlockSize(CCDigestAlgorithm algorithm)
258__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
259
260
261
262/*!
263 @function   CCDigestGetOutputSize
264 @abstract   Provides the digest output size of the digest algorithm
265
266 @param      algorithm         A digest algorithm selector.
267
268 returns 0 on failure or the digest output size on success.
269 */
270
271size_t
272CCDigestGetOutputSize(CCDigestAlgorithm algorithm)
273__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
274
275/*!
276 @function   CCDigestGetBlockSizeFromRef
277 @abstract   Provides the block size of the digest algorithm
278
279 @param      ctx         A digest context.
280
281 returns 0 on failure or the block size on success.
282 */
283
284size_t
285CCDigestGetBlockSizeFromRef(CCDigestRef ctx)
286__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
287
288// Until Heimdal Changes
289// #define CCDigestBlockSize CCDigestGetBlockSizeFromRef
290size_t
291CCDigestBlockSize(CCDigestRef ctx)
292__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);
293
294/*!
295 @function   CCDigestGetOutputSizeFromRef
296 @abstract   Provides the digest output size of the digest algorithm
297
298 @param      ctx         A digest context.
299
300 returns 0 on failure or the digest output size on success.
301 */
302
303size_t
304CCDigestGetOutputSizeFromRef(CCDigestRef ctx)
305__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
306
307// Until Heimdal Changes
308// #define CCDigestOutputSize CCDigestGetOutputSizeFromRef
309size_t
310CCDigestOutputSize(CCDigestRef ctx)
311__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
312
313
314
315uint8_t *
316CCDigestOID(CCDigestRef ctx)
317__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
318
319size_t
320CCDigestOIDLen(CCDigestRef ctx)
321__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
322
323CCDigestRef
324CCDigestCreateByOID(uint8_t *OID, size_t OIDlen)
325__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
326
327
328
329#ifdef __cplusplus
330}
331#endif
332
333#endif /* _CC_DigestSPI_H_ */
334