1 /*
2 * Copyright (c)2013-2020 ZeroTier, Inc.
3 *
4 * Use of this software is governed by the Business Source License included
5 * in the LICENSE.TXT file in the project's root directory.
6 *
7 * Change Date: 2025-01-01
8 *
9 * On the date above, in accordance with the Business Source License, use
10 * of this software will be governed by version 2.0 of the Apache License.
11 */
12 /****/
13
14 #ifndef ZT_SHA512_HPP
15 #define ZT_SHA512_HPP
16
17 #include "Constants.hpp"
18
19 #ifdef __APPLE__
20 #include <CommonCrypto/CommonDigest.h>
21 #endif
22
23 #define ZT_SHA512_DIGEST_SIZE 64
24 #define ZT_SHA384_DIGEST_SIZE 48
25 #define ZT_SHA512_BLOCK_SIZE 128
26 #define ZT_SHA384_BLOCK_SIZE 128
27
28 #define ZT_HMACSHA384_LEN 48
29
30 namespace ZeroTier {
31
32 // SHA384 and SHA512 are actually in the standard libraries on MacOS and iOS
33 #ifdef __APPLE__
34 #define ZT_HAVE_NATIVE_SHA512 1
SHA512(void * digest,const void * data,unsigned int len)35 static ZT_INLINE void SHA512(void *digest,const void *data,unsigned int len)
36 {
37 CC_SHA512_CTX ctx;
38 CC_SHA512_Init(&ctx);
39 CC_SHA512_Update(&ctx,data,len);
40 CC_SHA512_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
41 }
SHA384(void * digest,const void * data,unsigned int len)42 static ZT_INLINE void SHA384(void *digest,const void *data,unsigned int len)
43 {
44 CC_SHA512_CTX ctx;
45 CC_SHA384_Init(&ctx);
46 CC_SHA384_Update(&ctx,data,len);
47 CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
48 }
SHA384(void * digest,const void * data0,unsigned int len0,const void * data1,unsigned int len1)49 static ZT_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1)
50 {
51 CC_SHA512_CTX ctx;
52 CC_SHA384_Init(&ctx);
53 CC_SHA384_Update(&ctx,data0,len0);
54 CC_SHA384_Update(&ctx,data1,len1);
55 CC_SHA384_Final(reinterpret_cast<unsigned char *>(digest),&ctx);
56 }
57 #endif
58
59 #ifndef ZT_HAVE_NATIVE_SHA512
60 void SHA512(void *digest,const void *data,unsigned int len);
61 void SHA384(void *digest,const void *data,unsigned int len);
62 void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1);
63 #endif
64
65 /**
66 * Compute HMAC SHA-384 using a 256-bit key
67 *
68 * @param key Secret key
69 * @param msg Message to HMAC
70 * @param msglen Length of message
71 * @param mac Buffer to fill with result
72 */
73 void HMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE],const void *msg,unsigned int msglen,uint8_t mac[48]);
74
75 /**
76 * Compute KBKDF (key-based key derivation function) using HMAC-SHA-384 as a PRF
77 *
78 * @param key Source master key
79 * @param label A label indicating the key's purpose in the ZeroTier system
80 * @param context An arbitrary "context" or zero if not applicable
81 * @param iter Key iteration for generation of multiple keys for the same label/context
82 * @param out Output to receive derived key
83 */
84 void KBKDFHMACSHA384(const uint8_t key[ZT_SYMMETRIC_KEY_SIZE],char label,char context,uint32_t iter,uint8_t out[ZT_SYMMETRIC_KEY_SIZE]);
85
86 } // namespace ZeroTier
87
88 #endif
89