cecies
util.h
Go to the documentation of this file.
1/*
2 Copyright 2020 Raphael Beck
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
23#ifndef CECIES_UTIL_H
24#define CECIES_UTIL_H
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#include <time.h>
31#include <stdio.h>
32#include <stddef.h>
33#include <stdlib.h>
34#include <stdint.h>
35
36#include "types.h"
37#include "constants.h"
38
42#define CECIES_MIN(x, y) (((x) < (y)) ? (x) : (y))
43
47#define CECIES_MAX(x, y) (((x) > (y)) ? (x) : (y))
48
54static inline size_t cecies_calc_aes_cbc_ciphertext_length(const size_t plaintext_length)
55{
56 return plaintext_length + 16 - (plaintext_length % 16);
57}
58
64static inline size_t cecies_calc_compression_bound(const size_t data_length)
65{
66 return data_length + (data_length >> 12) + (data_length >> 14) + (data_length >> 25) + 13;
67}
68
75static inline size_t cecies_calc_output_buffer_needed_size(const size_t input_buffer_length, const size_t key_size)
76{
77 // 1 2 3 4 5
78 return 16 + 32 + key_size + 16 + input_buffer_length;
79
80 // 1: IV (AES initialization vector)
81 // 2: Salt (for HKDF)
82 // 3: R (ephemeral public key)
83 // 4: Tag (from AES-GCM)
84 // 5: Actual data array length
85}
86
92static inline size_t cecies_curve25519_calc_output_buffer_needed_size(const size_t input_buffer_length)
93{
95}
96
102static inline size_t cecies_curve448_calc_output_buffer_needed_size(const size_t input_buffer_length)
103{
105}
106
112static inline size_t cecies_calc_base64_length(const size_t data_length)
113{
114 return ((4 * data_length / 3 + 3) & ~(unsigned)3) + 1;
115}
116
127CECIES_API int cecies_hexstr2bin(const char* hexstr, size_t hexstr_length, uint8_t* output, size_t output_size, size_t* output_length);
128
140CECIES_API int cecies_bin2hexstr(const uint8_t* bin, size_t bin_length, char* output, size_t output_size, size_t* output_length, int uppercase);
141
146CECIES_API char* cecies_get_version_str();
147
152CECIES_API uint64_t cecies_get_version_nr();
153
159
167static inline int cecies_printvoid(FILE* stream, const char* format, ...)
168{
169 return 0;
170}
171
173CECIES_API extern int (*cecies_fprintf_fptr)(FILE* stream, const char* format, ...);
174
178CECIES_API void cecies_enable_fprintf();
179
183CECIES_API void cecies_disable_fprintf();
184
186#define cecies_fprintf cecies_fprintf_fptr
187
195static inline unsigned long long int cecies_get_random_big_integer()
196{
197 srand((unsigned int)time(NULL) * (unsigned int)time(NULL));
198 return rand() * rand() * rand() * rand();
199}
200
206CECIES_API void cecies_dev_urandom(uint8_t* output_buffer, size_t output_buffer_size);
207
213CECIES_API void cecies_free(void* mem);
214
215#ifdef __cplusplus
216} // extern "C"
217#endif
218
219#endif // CECIES_UTIL_H
CECIES constants.
#define CECIES_X448_KEY_SIZE
Definition: constants.h:63
#define CECIES_X25519_KEY_SIZE
Definition: constants.h:58
CECIES types.
CECIES_API int cecies_hexstr2bin(const char *hexstr, size_t hexstr_length, uint8_t *output, size_t output_size, size_t *output_length)
CECIES_API void cecies_enable_fprintf()
static int cecies_printvoid(FILE *stream, const char *format,...)
Definition: util.h:167
CECIES_API int cecies_bin2hexstr(const uint8_t *bin, size_t bin_length, char *output, size_t output_size, size_t *output_length, int uppercase)
static size_t cecies_calc_aes_cbc_ciphertext_length(const size_t plaintext_length)
Definition: util.h:54
CECIES_API void cecies_disable_fprintf()
CECIES_API void cecies_dev_urandom(uint8_t *output_buffer, size_t output_buffer_size)
static size_t cecies_curve448_calc_output_buffer_needed_size(const size_t input_buffer_length)
Definition: util.h:102
static size_t cecies_calc_output_buffer_needed_size(const size_t input_buffer_length, const size_t key_size)
Definition: util.h:75
CECIES_API char * cecies_get_version_str()
CECIES_API uint64_t cecies_get_version_nr()
static size_t cecies_calc_base64_length(const size_t data_length)
Definition: util.h:112
static size_t cecies_calc_compression_bound(const size_t data_length)
Definition: util.h:64
CECIES_API void cecies_free(void *mem)
static size_t cecies_curve25519_calc_output_buffer_needed_size(const size_t input_buffer_length)
Definition: util.h:92
CECIES_API int cecies_is_fprintf_enabled()
static unsigned long long int cecies_get_random_big_integer()
Definition: util.h:195