master
  1/*
  2 * Copyright (c) 2004 Apple Computer, 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 * CommonDigest.h - common digest routines: MD2, MD4, MD5, SHA1.
 26 */
 27
 28#ifndef _CC_COMMON_DIGEST_H_
 29#define _CC_COMMON_DIGEST_H_
 30
 31#include <stdint.h>
 32
 33#if defined(_MSC_VER)
 34#include <availability.h>
 35#else
 36#include <os/availability.h>
 37#endif
 38
 39
 40#ifdef __cplusplus
 41extern "C" {
 42#endif
 43
 44#define CC_DIGEST_DEPRECATION_WARNING                                                                                          \
 45    "This function is cryptographically broken and should not be used in security contexts. Clients should migrate to SHA256 (or stronger)."
 46
 47/*
 48 * For compatibility with legacy implementations, the *Init(), *Update(),
 49 * and *Final() functions declared here *always* return a value of 1 (one).
 50 * This corresponds to "success" in the similar openssl implementations.
 51 * There are no errors of any kind which can be, or are, reported here,
 52 * so you can safely ignore the return values of all of these functions
 53 * if you are implementing new code.
 54 *
 55 * The one-shot functions (CC_MD2(), CC_SHA1(), etc.) perform digest
 56 * calculation and place the result in the caller-supplied buffer
 57 * indicated by the md parameter. They return the md parameter.
 58 * Unlike the opensssl counterparts, these one-shot functions require
 59 * a non-NULL md pointer. Passing in NULL for the md parameter
 60 * results in a NULL return and no digest calculation.
 61 */
 62
 63typedef uint32_t CC_LONG;       /* 32 bit unsigned integer */
 64typedef uint64_t CC_LONG64;     /* 64 bit unsigned integer */
 65
 66/*** MD2 ***/
 67
 68#define CC_MD2_DIGEST_LENGTH    16          /* digest length in bytes */
 69#define CC_MD2_BLOCK_BYTES      64          /* block size in bytes */
 70#define CC_MD2_BLOCK_LONG       (CC_MD2_BLOCK_BYTES / sizeof(CC_LONG))
 71
 72typedef struct CC_MD2state_st
 73{
 74    int num;
 75    unsigned char data[CC_MD2_DIGEST_LENGTH];
 76    CC_LONG cksm[CC_MD2_BLOCK_LONG];
 77    CC_LONG state[CC_MD2_BLOCK_LONG];
 78} CC_MD2_CTX;
 79
 80extern int CC_MD2_Init(CC_MD2_CTX *c)
 81API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
 82
 83extern int CC_MD2_Update(CC_MD2_CTX *c, const void *data, CC_LONG len)
 84API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
 85
 86extern int CC_MD2_Final(unsigned char *md, CC_MD2_CTX *c)
 87API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
 88
 89extern unsigned char *CC_MD2(const void *data, CC_LONG len, unsigned char *md)
 90API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
 91
 92/*** MD4 ***/
 93
 94#define CC_MD4_DIGEST_LENGTH    16          /* digest length in bytes */
 95#define CC_MD4_BLOCK_BYTES      64          /* block size in bytes */
 96#define CC_MD4_BLOCK_LONG       (CC_MD4_BLOCK_BYTES / sizeof(CC_LONG))
 97
 98typedef struct CC_MD4state_st
 99{
100    CC_LONG A,B,C,D;
101    CC_LONG Nl,Nh;
102    CC_LONG data[CC_MD4_BLOCK_LONG];
103    uint32_t num;
104} CC_MD4_CTX;
105
106extern int CC_MD4_Init(CC_MD4_CTX *c)
107API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
108
109extern int CC_MD4_Update(CC_MD4_CTX *c, const void *data, CC_LONG len)
110API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
111
112extern int CC_MD4_Final(unsigned char *md, CC_MD4_CTX *c)
113API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
114
115extern unsigned char *CC_MD4(const void *data, CC_LONG len, unsigned char *md)
116API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
117
118
119/*** MD5 ***/
120
121#define CC_MD5_DIGEST_LENGTH    16          /* digest length in bytes */
122#define CC_MD5_BLOCK_BYTES      64          /* block size in bytes */
123#define CC_MD5_BLOCK_LONG       (CC_MD5_BLOCK_BYTES / sizeof(CC_LONG))
124
125typedef struct CC_MD5state_st
126{
127    CC_LONG A,B,C,D;
128    CC_LONG Nl,Nh;
129    CC_LONG data[CC_MD5_BLOCK_LONG];
130    int num;
131} CC_MD5_CTX;
132
133extern int CC_MD5_Init(CC_MD5_CTX *c)
134API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
135    
136extern int CC_MD5_Update(CC_MD5_CTX *c, const void *data, CC_LONG len)
137API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
138    
139extern int CC_MD5_Final(unsigned char *md, CC_MD5_CTX *c)
140API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
141
142extern unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md)
143API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
144
145/*** SHA1 ***/
146
147#define CC_SHA1_DIGEST_LENGTH   20          /* digest length in bytes */
148#define CC_SHA1_BLOCK_BYTES     64          /* block size in bytes */
149#define CC_SHA1_BLOCK_LONG      (CC_SHA1_BLOCK_BYTES / sizeof(CC_LONG))
150
151typedef struct CC_SHA1state_st
152{
153    CC_LONG h0,h1,h2,h3,h4;
154    CC_LONG Nl,Nh;
155    CC_LONG data[CC_SHA1_BLOCK_LONG];
156    int num;
157} CC_SHA1_CTX;
158
159extern int CC_SHA1_Init(CC_SHA1_CTX *c);
160
161extern int CC_SHA1_Update(CC_SHA1_CTX *c, const void *data, CC_LONG len);
162
163extern int CC_SHA1_Final(unsigned char *md, CC_SHA1_CTX *c);
164
165extern unsigned char *CC_SHA1(const void *data, CC_LONG len, unsigned char *md);
166
167/*** SHA224 ***/
168#define CC_SHA224_DIGEST_LENGTH     28          /* digest length in bytes */
169#define CC_SHA224_BLOCK_BYTES       64          /* block size in bytes */
170
171/* same context struct is used for SHA224 and SHA256 */
172typedef struct CC_SHA256state_st
173{   CC_LONG count[2];
174    CC_LONG hash[8];
175    CC_LONG wbuf[16];
176} CC_SHA256_CTX;
177
178extern int CC_SHA224_Init(CC_SHA256_CTX *c)
179API_AVAILABLE(macos(10.4), ios(2.0));
180
181extern int CC_SHA224_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)
182API_AVAILABLE(macos(10.4), ios(2.0));
183
184extern int CC_SHA224_Final(unsigned char *md, CC_SHA256_CTX *c)
185API_AVAILABLE(macos(10.4), ios(2.0));
186
187extern unsigned char *CC_SHA224(const void *data, CC_LONG len, unsigned char *md)
188API_AVAILABLE(macos(10.4), ios(2.0));
189
190
191/*** SHA256 ***/
192
193#define CC_SHA256_DIGEST_LENGTH     32          /* digest length in bytes */
194#define CC_SHA256_BLOCK_BYTES       64          /* block size in bytes */
195
196extern int CC_SHA256_Init(CC_SHA256_CTX *c)
197API_AVAILABLE(macos(10.4), ios(2.0));
198
199extern int CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)
200API_AVAILABLE(macos(10.4), ios(2.0));
201
202extern int CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c)
203API_AVAILABLE(macos(10.4), ios(2.0));
204
205extern unsigned char *CC_SHA256(const void *data, CC_LONG len, unsigned char *md)
206API_AVAILABLE(macos(10.4), ios(2.0));
207
208
209/*** SHA384 ***/
210
211#define CC_SHA384_DIGEST_LENGTH     48          /* digest length in bytes */
212#define CC_SHA384_BLOCK_BYTES      128          /* block size in bytes */
213
214/* same context struct is used for SHA384 and SHA512 */
215typedef struct CC_SHA512state_st
216{   CC_LONG64 count[2];
217    CC_LONG64 hash[8];
218    CC_LONG64 wbuf[16];
219} CC_SHA512_CTX;
220
221extern int CC_SHA384_Init(CC_SHA512_CTX *c)
222API_AVAILABLE(macos(10.4), ios(2.0));
223
224extern int CC_SHA384_Update(CC_SHA512_CTX *c, const void *data, CC_LONG len)
225API_AVAILABLE(macos(10.4), ios(2.0));
226
227extern int CC_SHA384_Final(unsigned char *md, CC_SHA512_CTX *c)
228API_AVAILABLE(macos(10.4), ios(2.0));
229
230extern unsigned char *CC_SHA384(const void *data, CC_LONG len, unsigned char *md)
231API_AVAILABLE(macos(10.4), ios(2.0));
232
233
234/*** SHA512 ***/
235
236#define CC_SHA512_DIGEST_LENGTH     64          /* digest length in bytes */
237#define CC_SHA512_BLOCK_BYTES      128          /* block size in bytes */
238
239extern int CC_SHA512_Init(CC_SHA512_CTX *c)
240API_AVAILABLE(macos(10.4), ios(2.0));
241
242extern int CC_SHA512_Update(CC_SHA512_CTX *c, const void *data, CC_LONG len)
243API_AVAILABLE(macos(10.4), ios(2.0));
244
245extern int CC_SHA512_Final(unsigned char *md, CC_SHA512_CTX *c)
246API_AVAILABLE(macos(10.4), ios(2.0));
247
248extern unsigned char *CC_SHA512(const void *data, CC_LONG len, unsigned char *md)
249API_AVAILABLE(macos(10.4), ios(2.0));
250
251/*
252 * To use the above digest functions with existing code which uses
253 * the corresponding openssl functions, #define the symbol
254 * COMMON_DIGEST_FOR_OPENSSL in your client code (BEFORE including
255 * this file), and simply link against libSystem (or System.framework)
256 * instead of libcrypto.
257 *
258 * You can *NOT* mix and match functions operating on a given data
259 * type from the two implementations; i.e., if you do a CC_MD5_Init()
260 * on a CC_MD5_CTX object, do not assume that you can do an openssl-style
261 * MD5_Update() on that same context.
262 */
263
264#ifdef  COMMON_DIGEST_FOR_OPENSSL
265
266#define MD2_DIGEST_LENGTH           CC_MD2_DIGEST_LENGTH
267#define MD2_CTX                     CC_MD2_CTX
268#define MD2_Init                    CC_MD2_Init
269#define MD2_Update                  CC_MD2_Update
270#define MD2_Final                   CC_MD2_Final
271
272#define MD4_DIGEST_LENGTH           CC_MD4_DIGEST_LENGTH
273#define MD4_CTX                     CC_MD4_CTX
274#define MD4_Init                    CC_MD4_Init
275#define MD4_Update                  CC_MD4_Update
276#define MD4_Final                   CC_MD4_Final
277
278#define MD5_DIGEST_LENGTH           CC_MD5_DIGEST_LENGTH
279#define MD5_CTX                     CC_MD5_CTX
280#define MD5_Init                    CC_MD5_Init
281#define MD5_Update                  CC_MD5_Update
282#define MD5_Final                   CC_MD5_Final
283
284#define SHA_DIGEST_LENGTH           CC_SHA1_DIGEST_LENGTH
285#define SHA_CTX                     CC_SHA1_CTX
286#define SHA1_Init                   CC_SHA1_Init
287#define SHA1_Update                 CC_SHA1_Update
288#define SHA1_Final                  CC_SHA1_Final
289
290#define SHA224_DIGEST_LENGTH        CC_SHA224_DIGEST_LENGTH
291#define SHA256_CTX                  CC_SHA256_CTX
292#define SHA224_Init                 CC_SHA224_Init
293#define SHA224_Update               CC_SHA224_Update
294#define SHA224_Final                CC_SHA224_Final
295
296#define SHA256_DIGEST_LENGTH        CC_SHA256_DIGEST_LENGTH
297#define SHA256_Init                 CC_SHA256_Init
298#define SHA256_Update               CC_SHA256_Update
299#define SHA256_Final                CC_SHA256_Final
300
301#define SHA384_DIGEST_LENGTH        CC_SHA384_DIGEST_LENGTH
302#define SHA512_CTX                  CC_SHA512_CTX
303#define SHA384_Init                 CC_SHA384_Init
304#define SHA384_Update               CC_SHA384_Update
305#define SHA384_Final                CC_SHA384_Final
306
307#define SHA512_DIGEST_LENGTH        CC_SHA512_DIGEST_LENGTH
308#define SHA512_Init                 CC_SHA512_Init
309#define SHA512_Update               CC_SHA512_Update
310#define SHA512_Final                CC_SHA512_Final
311
312
313#endif  /* COMMON_DIGEST_FOR_OPENSSL */
314
315/*
316 * In a manner similar to that described above for openssl
317 * compatibility, these macros can be used to provide compatiblity
318 * with legacy implementations of MD5 using the interface defined
319 * in RFC 1321.
320 */
321
322#ifdef  COMMON_DIGEST_FOR_RFC_1321
323
324#define MD5_CTX                     CC_MD5_CTX
325#define MD5Init                     CC_MD5_Init
326#define MD5Update                   CC_MD5_Update
327
328void MD5Final (unsigned char [16], MD5_CTX *)
329API_DEPRECATED(CC_DIGEST_DEPRECATION_WARNING, macos(10.4, 10.15), ios(2.0, 13.0));
330
331#endif  /* COMMON_DIGEST_FOR_RFC_1321 */
332
333#ifdef __cplusplus
334}
335#endif
336
337#endif  /* _CC_COMMON_DIGEST_H_ */