wickr-crypto-c

kdf.h
1 /*
2 * Copyright © 2012-2020 Wickr Inc. All rights reserved.
3 *
4 * This code is being released for EDUCATIONAL, ACADEMIC, AND CODE REVIEW PURPOSES
5 * ONLY. COMMERCIAL USE OF THE CODE IS EXPRESSLY PROHIBITED. For additional details,
6 * please see LICENSE
7 *
8 * THE CODE IS MADE AVAILABLE "AS-IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED GUARANTEES AS TO FITNESS, MERCHANTABILITY, NON-
10 * INFRINGEMENT OR OTHERWISE. IT IS NOT BEING PROVIDED IN TRADE BUT ON
11 * A VOLUNTARY BASIS ON BEHALF OF THE AUTHOR’S PART FOR THE BENEFIT
12 * OF THE LICENSEE AND IS NOT MADE AVAILABLE FOR CONSUMER USE OR ANY
13 * OTHER USE OUTSIDE THE TERMS OF THIS LICENSE. ANYONE ACCESSING THE
14 * CODE SHOULD HAVE THE REQUISITE EXPERTISE TO SECURE THEIR SYSTEM
15 * AND DEVICES AND TO ACCESS AND USE THE CODE FOR REVIEW PURPOSES
16 * ONLY. LICENSEE BEARS THE RISK OF ACCESSING AND USING THE CODE. IN
17 * PARTICULAR, AUTHOR BEARS NO LIABILITY FOR ANY INTERFERENCE WITH OR
18 * ADVERSE EFFECT THAT MAY OCCUR AS A RESULT OF THE LICENSEE
19 * ACCESSING AND/OR USING THE CODE ON LICENSEE’S SYSTEM.
20 */
21
22 #ifndef kdf_h
23 #define kdf_h
24
25 #include "buffer.h"
26 #include "digest.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32
47 typedef enum { KDF_BCRYPT, KDF_SCRYPT, KDF_HMAC_SHA2 } wickr_kdf_algo_id;
48
49 typedef enum { KDF_ID_SCRYPT_17 = 1, KDF_ID_SCRYPT_18, KDF_ID_SCRYPT_19, KDF_ID_SCRYPT_20, KDF_ID_BCRYPT_15, KDF_ID_HKDF_SHA256, KDF_ID_HKDF_SHA384, KDF_ID_HKDF_SHA512 } wickr_kdf_id;
50
71 struct wickr_kdf_algo {
72 wickr_kdf_algo_id algo_id;
73 wickr_kdf_id kdf_id;
74 uint8_t salt_size;
75 uint8_t output_size;
76 uint32_t cost;
77 };
78
79 typedef struct wickr_kdf_algo wickr_kdf_algo_t;
80
92 #define SCRYPT_2_17_COST 1116161
93 #define SCRYPT_2_18_COST 1181697
94 #define SCRYPT_2_19_COST 1247233
95 #define SCRYPT_2_20_COST 1312769
96
97 /* Truncate the output size of scrypt to give us 32byte values we can use as a cipher key */
98 #define SCRYPT_OUTPUT_SIZE 32
99
100 #define SCRYPT_SALT_SIZE 16
101 #define BCRYPT_15_COST 15
102 #define BCRYPT_HASH_SIZE 64
103
104 /* Passed without the $2y$15$. It will be injected internally */
105 #define BCRYPT_SALT_SIZE 22
106
107 /* SCRYPT Mode Definitions */
108 static const wickr_kdf_algo_t KDF_SCRYPT_2_17 = { KDF_SCRYPT, KDF_ID_SCRYPT_17, SCRYPT_SALT_SIZE, SCRYPT_OUTPUT_SIZE, SCRYPT_2_17_COST };
109 static const wickr_kdf_algo_t KDF_SCRYPT_2_18 = { KDF_SCRYPT, KDF_ID_SCRYPT_18, SCRYPT_SALT_SIZE, SCRYPT_OUTPUT_SIZE, SCRYPT_2_18_COST };
110 static const wickr_kdf_algo_t KDF_SCRYPT_2_19 = { KDF_SCRYPT, KDF_ID_SCRYPT_19, SCRYPT_SALT_SIZE, SCRYPT_OUTPUT_SIZE, SCRYPT_2_19_COST };
111 static const wickr_kdf_algo_t KDF_SCRYPT_2_20 = { KDF_SCRYPT, KDF_ID_SCRYPT_20, SCRYPT_SALT_SIZE, SCRYPT_OUTPUT_SIZE, SCRYPT_2_20_COST };
112
113 /* BCRYPT Mode Definitions */
114 static const wickr_kdf_algo_t KDF_BCRYPT_15 = { KDF_BCRYPT, KDF_ID_BCRYPT_15, BCRYPT_SALT_SIZE, BCRYPT_HASH_SIZE, BCRYPT_15_COST };
115
116 /* HKDF Mode Definitions */
117 static const wickr_kdf_algo_t KDF_HKDF_SHA256 = { KDF_HMAC_SHA2, KDF_ID_HKDF_SHA256, SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE, 0 };
118 static const wickr_kdf_algo_t KDF_HKDF_SHA384 = { KDF_HMAC_SHA2, KDF_ID_HKDF_SHA384, SHA384_DIGEST_SIZE, SHA384_DIGEST_SIZE, 0 };
119 static const wickr_kdf_algo_t KDF_HKDF_SHA512 = { KDF_HMAC_SHA2, KDF_ID_HKDF_SHA512, SHA512_DIGEST_SIZE, SHA512_DIGEST_SIZE, 0 };
120
135 struct wickr_kdf_meta {
136 wickr_kdf_algo_t algo;
137 wickr_buffer_t *salt;
138 wickr_buffer_t *info;
139 };
140
141 typedef struct wickr_kdf_meta wickr_kdf_meta_t;
142
155 struct wickr_kdf_result {
156 wickr_kdf_meta_t *meta;
157 wickr_buffer_t *hash;
158 };
159
160 typedef struct wickr_kdf_result wickr_kdf_result_t;
161
173 wickr_kdf_meta_t *wickr_kdf_meta_create(wickr_kdf_algo_t algo, wickr_buffer_t *salt, wickr_buffer_t *info);
174
186 uint8_t wickr_kdf_meta_size_with_buffer(const wickr_buffer_t *buffer);
187
198 wickr_buffer_t *wickr_kdf_meta_serialize(const wickr_kdf_meta_t *meta);
199
209 wickr_kdf_meta_t *wickr_kdf_meta_create_with_buffer(const wickr_buffer_t *buffer);
210
220 wickr_kdf_meta_t *wickr_kdf_meta_copy(const wickr_kdf_meta_t *source);
221
230 void wickr_kdf_meta_destroy(wickr_kdf_meta_t **meta);
231
242 wickr_kdf_result_t *wickr_kdf_result_create(wickr_kdf_meta_t *meta, wickr_buffer_t *hash);
243
253 wickr_kdf_result_t *wickr_kdf_result_copy(const wickr_kdf_result_t *source);
254
263 void wickr_kdf_result_destroy(wickr_kdf_result_t **result);
264
275 wickr_kdf_result_t *wickr_perform_kdf(wickr_kdf_algo_t algo, const wickr_buffer_t *passphrase);
276
287 wickr_kdf_result_t *wickr_perform_kdf_meta(const wickr_kdf_meta_t *existing_meta, const wickr_buffer_t *passphrase);
288
298 const wickr_kdf_algo_t *wickr_hkdf_algo_for_digest(wickr_digest_t digest);
299
300 #ifdef __cplusplus
301 }
302 #endif
303
304 #endif /* kdf_h */
wickr_kdf_algo_id
wickr_kdf_algo_id
Definition: kdf.h:47
wickr_kdf_meta_serialize
wickr_buffer_t * wickr_kdf_meta_serialize(const wickr_kdf_meta_t *meta)
wickr_kdf_meta_destroy
void wickr_kdf_meta_destroy(wickr_kdf_meta_t **meta)
wickr_kdf_result_create
wickr_kdf_result_t * wickr_kdf_result_create(wickr_kdf_meta_t *meta, wickr_buffer_t *hash)
SCRYPT_2_17_COST
#define SCRYPT_2_17_COST
Definition: kdf.h:92
wickr_kdf_result_destroy
void wickr_kdf_result_destroy(wickr_kdf_result_t **result)
wickr_perform_kdf_meta
wickr_kdf_result_t * wickr_perform_kdf_meta(const wickr_kdf_meta_t *existing_meta, const wickr_buffer_t *passphrase)
wickr_kdf_result_copy
wickr_kdf_result_t * wickr_kdf_result_copy(const wickr_kdf_result_t *source)
wickr_kdf_meta_copy
wickr_kdf_meta_t * wickr_kdf_meta_copy(const wickr_kdf_meta_t *source)
wickr_hkdf_algo_for_digest
const wickr_kdf_algo_t * wickr_hkdf_algo_for_digest(wickr_digest_t digest)
wickr_kdf_meta_size_with_buffer
uint8_t wickr_kdf_meta_size_with_buffer(const wickr_buffer_t *buffer)
wickr_perform_kdf
wickr_kdf_result_t * wickr_perform_kdf(wickr_kdf_algo_t algo, const wickr_buffer_t *passphrase)
wickr_kdf_meta_create_with_buffer
wickr_kdf_meta_t * wickr_kdf_meta_create_with_buffer(const wickr_buffer_t *buffer)
wickr_kdf_meta_create
wickr_kdf_meta_t * wickr_kdf_meta_create(wickr_kdf_algo_t algo, wickr_buffer_t *salt, wickr_buffer_t *info)
wickr_buffer
Represents an array of bytes and the length of the allocation associated with those bytes.
Definition: buffer.h:51
wickr_digest
Digest function parameters.
Definition: digest.h:54
wickr_kdf_algo
Metadata associated with a particular KDF function.
Definition: kdf.h:71
wickr_kdf_algo::kdf_id
wickr_kdf_id kdf_id
Definition: kdf.h:73
wickr_kdf_algo::algo_id
wickr_kdf_algo_id algo_id
Definition: kdf.h:72
wickr_kdf_algo::cost
uint32_t cost
Definition: kdf.h:76
wickr_kdf_algo::output_size
uint8_t output_size
Definition: kdf.h:75
wickr_kdf_algo::salt_size
uint8_t salt_size
Definition: kdf.h:74
wickr_kdf_meta
Represents the information the KDF function will need along with it's input to derive a particular ou...
Definition: kdf.h:135
wickr_kdf_meta::algo
wickr_kdf_algo_t algo
Definition: kdf.h:136
wickr_kdf_meta::salt
wickr_buffer_t * salt
Definition: kdf.h:137
wickr_kdf_meta::info
wickr_buffer_t * info
Definition: kdf.h:138
wickr_kdf_result
Represents the result of a KDF function execution.
Definition: kdf.h:155
wickr_kdf_result::meta
wickr_kdf_meta_t * meta
Definition: kdf.h:156
wickr_kdf_result::hash
wickr_buffer_t * hash
Definition: kdf.h:157