1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2020-2022 The Defold Foundation
// Copyright 2014-2020 King
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef DMSDK_CRYPT_H
#define DMSDK_CRYPT_H
#include <stdint.h>
/*# SDK Crypt API documentation
* [file:<dmsdk/dlib/crypt.h>]
*
* Various hash and encode/decode functions.
*
* @document
* @name Crypt
* @namespace dmCrypt
*/
namespace dmCrypt
{
/*# encryption algorithm enumeration
*
* @enum
* @name Algorithm
* @member dmCrypt::ALGORITHM_XTEA
*/
enum Algorithm
{
ALGORITHM_XTEA
};
/*# result enumeration
*
* @enum
* @name Result
* @member dmCrypt::RESULT_OK = 0
* @member dmCrypt::RESULT_ERROR = 1
*/
enum Result
{
RESULT_OK = 0,
RESULT_ERROR = 1,
};
/*# Encrypt data in place
* @param algo [type:dmCrypt::Algorithm] algorithm
* @param data [type:const uint8_t*] data
* @param datalen [type:uint32_t] data length in bytes
* @param key [type:const uint8_t*] key
* @param keylen [type:uint32_t] key length
* @return result [type:dmCrypt::Result] the encryption result
*/
Result Encrypt(Algorithm algo, uint8_t* data, uint32_t datalen, const uint8_t* key, uint32_t keylen);
/*# Decrypt data in place
* @param algo [type:dmCrypt::Algorithm] algorithm
* @param data [type:const uint8_t*] data
* @param datalen [type:uint32_t] data length in bytes
* @param key [type:const uint8_t*] key
* @param keylen [type:uint32_t] key length
* @return result [type:dmCrypt::Result] the decryption result
*/
Result Decrypt(Algorithm algo, uint8_t* data, uint32_t datalen, const uint8_t* key, uint32_t keylen);
/*# Hash buffer using SHA1
* @name dmCrypt::HashSha1
* @param buf [type:const uint8_t*] The source data to hash
* @param buflen [type:uint32_t] The length of source data in bytes
* @param digest [type:uint8_t*] The destination buffer (20 bytes)
*/
void HashSha1(const uint8_t* buf, uint32_t buflen, uint8_t* digest); // output is 20 bytes
/*# Hash buffer using SHA256
* @name dmCrypt::HashSha256
* @param buf [type:const uint8_t*] The source data to hash
* @param buflen [type:uint32_t] key The length of source data in bytes
* @param digest [type:uint8_t*] The destination buffer (32 bytes)
*/
void HashSha256(const uint8_t* buf, uint32_t buflen, uint8_t* digest); // output is 32 bytes
/*# Hash buffer using SHA512
* @name dmCrypt::HashSha512
* @param buf [type:const uint8_t*] The source data to hash
* @param buflen [type:uint32_t] key The length of source data in bytes
* @param digest [type:uint8_t*] The destination buffer (64 bytes)
*/
void HashSha512(const uint8_t* buf, uint32_t buflen, uint8_t* digest); // output is 64 bytes
/*# Hash buffer using MD5
* @name dmCrypt::HashMd5
* @param buf [type:const uint8_t*] The source data to hash
* @param buflen [type:uint32_t] key The length of source data in bytes
* @param digest [type:const uint8_t*] The destination buffer (16 bytes)
*/
void HashMd5(const uint8_t* buf, uint32_t buflen, uint8_t* digest); // output is 16 bytes
/*# Base64 encode a buffer
* @name dmCrypt::Base64Encode
* @param src [type:const uint8_t*] The source data to encode
* @param src_len [type:uint32_t] key The length of source data in bytes
* @param dst [type:uint8_t*] The destination buffer
* @param dst_len[in,out] [type:uint32_t*] In: The length of the destination in bytes. Out: The length of the encoded string.
* @return success [type:bool] true if the encoding went ok
* @note Call this function with *dst_len = 0 to obtain the required buffer size in *dst_len
*/
bool Base64Encode(const uint8_t* src, uint32_t src_len, uint8_t* dst, uint32_t* dst_len);
/*# Base64 decode a buffer
* @name dmCrypt::Base64Decode
* @param src [type:const uint8_t*] The source data to encode
* @param src_len [type:uint32_t] key The length of source data in bytes
* @param dst [type:uint8_t*] The destination buffer
* @param dst_len[in,out] [type:uint32_t*] In: The length of the destination in bytes. Out: The length of the decoded string.
* @return success [type:bool] true if the decoding went ok
* @note Call this function with *dst_len = 0 to obtain the required buffer size in *dst_len
*/
bool Base64Decode(const uint8_t* src, uint32_t src_len, uint8_t* dst, uint32_t* dst_len);
}
#endif /* DMSDK_CRYPT_H */