wickr-crypto-c

cipher.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 cipher_h
23 #define cipher_h
24
25 #include <stdlib.h>
26 #include "buffer.h"
27 #include "kdf.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
35 typedef enum { CIPHER_ID_AES256_GCM = 0, CIPHER_ID_AES256_CTR = 1 } wickr_cipher_id;
36
56 struct wickr_cipher {
57 wickr_cipher_id cipher_id;
58 uint8_t key_len;
59 uint8_t iv_len;
60 uint8_t auth_tag_len;
61 bool is_authenticated;
62 };
63
64 typedef struct wickr_cipher wickr_cipher_t;
65
66 static const wickr_cipher_t CIPHER_AES256_GCM = { CIPHER_ID_AES256_GCM, 32, 12, 16, true };
67 static const wickr_cipher_t CIPHER_AES256_CTR = { CIPHER_ID_AES256_CTR, 32, 16, 0, false };
68
78 const wickr_cipher_t *wickr_cipher_find(uint8_t cipher_id);
79
97 struct wickr_cipher_result {
98 wickr_cipher_t cipher;
99 wickr_buffer_t *iv;
100 wickr_buffer_t *cipher_text;
101 wickr_buffer_t *auth_tag;
102 };
103
104 typedef struct wickr_cipher_result wickr_cipher_result_t;
105
118 wickr_cipher_result_t *wickr_cipher_result_create(wickr_cipher_t cipher, wickr_buffer_t *iv, wickr_buffer_t *cipher_text, wickr_buffer_t *auth_tag);
119
129 wickr_cipher_result_t *wickr_cipher_result_copy(const wickr_cipher_result_t *result);
130
139 void wickr_cipher_result_destroy(wickr_cipher_result_t **result);
140
150 bool wickr_cipher_result_is_valid(const wickr_cipher_result_t *result);
151
162 wickr_buffer_t *wickr_cipher_result_serialize(const wickr_cipher_result_t *result);
163
174 wickr_cipher_result_t *wickr_cipher_result_from_buffer(const wickr_buffer_t *buffer);
175
189 struct wickr_cipher_key {
190 wickr_cipher_t cipher;
191 wickr_buffer_t *key_data;
192 };
193
194 typedef struct wickr_cipher_key wickr_cipher_key_t;
195
206 wickr_cipher_key_t *wickr_cipher_key_create(wickr_cipher_t cipher, wickr_buffer_t *key_data);
207
217 wickr_cipher_key_t *wickr_cipher_key_copy(const wickr_cipher_key_t *key);
218
227 void wickr_cipher_key_destroy(wickr_cipher_key_t **key);
228
239 wickr_buffer_t *wickr_cipher_key_serialize(const wickr_cipher_key_t *key);
240
251 wickr_cipher_key_t *wickr_cipher_key_from_buffer(const wickr_buffer_t *buffer);
252
253 #ifdef __cplusplus
254 }
255 #endif
256
257 #endif /* cipher_h */
wickr_cipher_key_from_buffer
wickr_cipher_key_t * wickr_cipher_key_from_buffer(const wickr_buffer_t *buffer)
wickr_cipher_result_create
wickr_cipher_result_t * wickr_cipher_result_create(wickr_cipher_t cipher, wickr_buffer_t *iv, wickr_buffer_t *cipher_text, wickr_buffer_t *auth_tag)
wickr_cipher_find
const wickr_cipher_t * wickr_cipher_find(uint8_t cipher_id)
wickr_cipher_result_is_valid
bool wickr_cipher_result_is_valid(const wickr_cipher_result_t *result)
wickr_cipher_key_destroy
void wickr_cipher_key_destroy(wickr_cipher_key_t **key)
wickr_cipher_key_create
wickr_cipher_key_t * wickr_cipher_key_create(wickr_cipher_t cipher, wickr_buffer_t *key_data)
wickr_cipher_key_serialize
wickr_buffer_t * wickr_cipher_key_serialize(const wickr_cipher_key_t *key)
wickr_cipher_key_copy
wickr_cipher_key_t * wickr_cipher_key_copy(const wickr_cipher_key_t *key)
wickr_cipher_result_serialize
wickr_buffer_t * wickr_cipher_result_serialize(const wickr_cipher_result_t *result)
wickr_cipher_result_copy
wickr_cipher_result_t * wickr_cipher_result_copy(const wickr_cipher_result_t *result)
wickr_cipher_result_from_buffer
wickr_cipher_result_t * wickr_cipher_result_from_buffer(const wickr_buffer_t *buffer)
wickr_cipher_result_destroy
void wickr_cipher_result_destroy(wickr_cipher_result_t **result)
wickr_buffer
Represents an array of bytes and the length of the allocation associated with those bytes.
Definition: buffer.h:51
wickr_cipher_key
Definition: cipher.h:189
wickr_cipher_key::key_data
wickr_buffer_t * key_data
Definition: cipher.h:191
wickr_cipher_key::cipher
wickr_cipher_t cipher
Definition: cipher.h:190
wickr_cipher_result
Definition: cipher.h:97
wickr_cipher_result::cipher_text
wickr_buffer_t * cipher_text
Definition: cipher.h:100
wickr_cipher_result::iv
wickr_buffer_t * iv
Definition: cipher.h:99
wickr_cipher_result::cipher
wickr_cipher_t cipher
Definition: cipher.h:98
wickr_cipher_result::auth_tag
wickr_buffer_t * auth_tag
Definition: cipher.h:101
wickr_cipher
Represents a cipher that can be used in the crypto_engine. This meta object holds parameters for the ...
Definition: cipher.h:56
wickr_cipher::iv_len
uint8_t iv_len
Definition: cipher.h:59
wickr_cipher::is_authenticated
bool is_authenticated
Definition: cipher.h:61
wickr_cipher::cipher_id
wickr_cipher_id cipher_id
Definition: cipher.h:57
wickr_cipher::auth_tag_len
uint8_t auth_tag_len
Definition: cipher.h:60
wickr_cipher::key_len
uint8_t key_len
Definition: cipher.h:58