src/abi.c

filter: Statementstmt 100% 78 / 78 Branchbran 100% 80 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 158 / 158 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Crypt/Symmetric/Primitives/BLAKE2b.pm ↑ Summary src/symprim.c »
1 #include "symprim/abi.h"
2  
3 #include "symprim/symprim.h"
4 #include "symprim/argon2id.h"
5 #include "symprim/blake2b.h"
6 #include "symprim/random.h"
7 #include "symprim/wipe.h"
8 #include "symprim/xchacha20poly1305.h"
9  
10 /* symprim_abi.h spells out numbers that are the engine's, and a consumer was
11 * compiled with them as they were on the day it was built. Where this does
12 * not compile, a number of the engine's has moved: move it back, since no
13 * consumer can be told. The line that counts them holds the header to every
14 * answer the engine has: a new one is added to both. */
15 typedef char symprim_abi_numbers_are_the_engines[(
16 SYMPRIM_ABI_OK == SYMPRIM_OK
17 && SYMPRIM_ABI_E_ARGUMENT == SYMPRIM_E_ARGUMENT
18 && SYMPRIM_ABI_E_MEMORY == SYMPRIM_E_MEMORY
19 && SYMPRIM_ABI_E_RANDOM == SYMPRIM_E_RANDOM
20 && SYMPRIM_ABI_E_NOLOCK == SYMPRIM_E_NOLOCK
21 && SYMPRIM_ABI_E_AUTH == SYMPRIM_E_AUTH
22 && SYMPRIM_ABI_E_AUTH + 1 == SYMPRIM_E_COUNT
23 && SYMPRIM_ABI_BLAKE2B_MAX == SYMPRIM_BLAKE2B_MAX
24 && SYMPRIM_ABI_BLAKE2B_MAX == SYMPRIM_BLAKE2B_KEY_MAX
25 && SYMPRIM_ABI_BLAKE2B_SALT_BYTES == SYMPRIM_BLAKE2B_SALT
26 && SYMPRIM_ABI_BLAKE2B_PERSONAL_BYTES == SYMPRIM_BLAKE2B_PERSONAL
27 && SYMPRIM_ABI_KEY_BYTES == SYMPRIM_AEAD_KEY
28 && SYMPRIM_ABI_NONCE_BYTES == SYMPRIM_AEAD_NONCE
29 && SYMPRIM_ABI_TAG_BYTES == SYMPRIM_AEAD_TAG) ? 1 : -1];
30  
31 /* A caller holds the state of a hash as the 28 words symprim_abi.h spells,
32 * and the engine uses that room as its own state. Where this does not
33 * compile, the engine's state has outgrown the room or asks to be laid more
34 * strictly than the room is: make it fit again, since the room is what every
35 * consumer was compiled with. How strictly each is laid is measured here and
36 * not taken for 8, since there are machines that lay a 64 bit word on a 4
37 * byte boundary. */
38 struct symprim_abi_engine_state_laid { char before; symprim_blake2b_state state; };
39 struct symprim_abi_caller_state_laid { char before; symprim_abi_blake2b state; };
40 typedef char symprim_abi_state_holds_the_engines[(
41 sizeof(symprim_abi_blake2b) == 28 * sizeof(uint64_t)
42 && sizeof(symprim_blake2b_state) <= sizeof(symprim_abi_blake2b)
43 && offsetof(struct symprim_abi_engine_state_laid, state)
44 <= offsetof(struct symprim_abi_caller_state_laid, state)) ? 1 : -1];
45  
46 /* Where the seal points when there are no bytes. A caller with nothing to
47 * give may give NULL and a length of 0, and no function of the C library is
48 * to be handed that. */
49 static const uint8_t no_bytes[1] = { 0 };
50  
51 /* ---- a hash --------------------------------------------------------------- */
52  
53 563 static int abi_blake2b(void *out, size_t outlen, const void *in, size_t len,
54 const void *key, size_t keylen)
55 {
56 563 ▶ if (!out || (!in && len))
Branch: 3 branches
BranchTrueFalseTotal
if (!out || (!in && len)) 562 1 563
if (!out || (!in && len)) 2 560 562
if (!out || (!in && len)) 1 1 2
57 2 return SYMPRIM_E_ARGUMENT;
58 561 return symprim_blake2b(out, outlen, in, len, key, keylen);
59 }
60  
61 /* The engine's state, in the room a caller holds for it. */
62 19283 static symprim_blake2b_state *state_in(symprim_abi_blake2b *held)
63 {
64 19283 return (symprim_blake2b_state *)(void *)held;
65 }
66  
67 /* Whether a state is one blake2b_init made, as far as what it holds can
68 * tell. One that was never made holds whatever was in its memory, and one
69 * that was wiped holds zeros: each says a length that no state has. The
70 * engine would follow such a length out of the state, so it is not given
71 * one. A state that was never made and happens to hold two lengths that
72 * could be is not told from one that was. */
73 18058 static int state_is_made(const symprim_blake2b_state *s)
74 {
75 18058 return s->buflen <= SYMPRIM_BLAKE2B_BLOCK
76 18058 ▶ && s->outlen >= 1 && s->outlen <= SYMPRIM_BLAKE2B_MAX;
Branch: 3 branches
BranchTrueFalseTotal
&& s->outlen >= 1 && s->outlen <= SYMPRIM_BLAKE2B_MAX; 18056 2 18058
&& s->outlen >= 1 && s->outlen <= SYMPRIM_BLAKE2B_MAX; 18054 2 18056
&& s->outlen >= 1 && s->outlen <= SYMPRIM_BLAKE2B_MAX; 18052 2 18054
77 }
78  
79 1219 static int abi_blake2b_init(symprim_abi_blake2b *state, size_t outlen,
80 const void *key, size_t keylen,
81 const uint8_t *salt, const uint8_t *personal)
82 {
83 1219 return symprim_blake2b_init(state_in(state), outlen, key, keylen, salt, personal);
84 }
85  
86 16847 static void abi_blake2b_update(symprim_abi_blake2b *state, const void *in, size_t len)
87 {
88 16847 symprim_blake2b_state *s = state_in(state);
89  
90 16847 ▶ if (!s || !in || !len || !state_is_made(s))
Branch: 4 branches
BranchTrueFalseTotal
if (!s || !in || !len || !state_is_made(s)) 16846 1 16847
if (!s || !in || !len || !state_is_made(s)) 16844 2 16846
if (!s || !in || !len || !state_is_made(s)) 16843 1 16844
if (!s || !in || !len || !state_is_made(s)) 3 16840 16843
91 7 return;
92 16840 symprim_blake2b_update(s, in, len);
93 }
94  
95 1217 static void abi_blake2b_final(symprim_abi_blake2b *state, void *out)
96 {
97 1217 symprim_blake2b_state *s = state_in(state);
98  
99 1217 ▶ if (!s)
Branch: 1 branch
BranchTrueFalseTotal
if (!s) 1 1216 1217
100 1 return;
101 1216 ▶ if (out && state_is_made(s))
Branch: 2 branches
BranchTrueFalseTotal
if (out && state_is_made(s)) 1215 1 1216
if (out && state_is_made(s)) 1212 3 1215
102 1212 symprim_blake2b_final(s, out);
103 /* All of the room, which the engine's own wipe may fall short of where
104 * its state is the smaller of the two. */
105 1216 symprim_wipe(state, sizeof *state);
106 }
107  
108 /* ---- a password hash ------------------------------------------------------ */
109  
110 53 static int abi_argon2id(const symprim_abi_argon2 *in, uint8_t *tag, size_t tag_len)
111 {
112 symprim_argon2_in at;
113  
114 53 ▶ if (!in)
Branch: 1 branch
BranchTrueFalseTotal
if (!in) 1 52 53
115 1 return SYMPRIM_E_ARGUMENT;
116 52 at.memory_kib = in->memory_kib;
117 52 at.passes = in->passes;
118 52 at.lanes = in->lanes;
119 52 at.password = in->password;
120 52 at.password_len = in->password_len;
121 52 at.salt = in->salt;
122 52 at.salt_len = in->salt_len;
123 52 at.secret = in->secret;
124 52 at.secret_len = in->secret_len;
125 52 at.data = in->data;
126 52 at.data_len = in->data_len;
127 52 return symprim_argon2id(&at, tag, tag_len);
128 }
129  
130 /* ---- a seal --------------------------------------------------------------- */
131  
132 /* What the two halves of the seal ask of their pointers. */
133 43 static int sealing_fits(const uint8_t *out, const uint8_t *in, size_t len, const uint8_t *tag,
134 const uint8_t *ad, size_t ad_len,
135 const uint8_t *nonce, const uint8_t *key)
136 {
137 43 ▶ return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad);
Branch: 8 branches
BranchTrueFalseTotal
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 41 2 43
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 39 2 41
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 37 2 39
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 28 9 37
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 26 2 28
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 24 2 26
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 16 17 33
return tag && nonce && key && (!len || (out && in)) && (!ad_len || ad); 14 2 16
138 }
139  
140 17 static int abi_aead_seal(uint8_t *out, uint8_t *tag,
141 const uint8_t *in, size_t len,
142 const uint8_t *ad, size_t ad_len,
143 const uint8_t *nonce, const uint8_t *key)
144 {
145 uint8_t nowhere[1];
146  
147 17 ▶ if (!sealing_fits(out, in, len, tag, ad, ad_len, nonce, key))
Branch: 1 branch
BranchTrueFalseTotal
if (!sealing_fits(out, in, len, tag, ad, ad_len, nonce, key)) 6 11 17
148 6 return SYMPRIM_E_ARGUMENT;
149 11 ▶ return symprim_aead_seal(out ? out : nowhere, tag, in ? in : no_bytes, len,
Branch: 3 branches
BranchTrueFalseTotal
return symprim_aead_seal(out ? out : nowhere, tag, in ? in : no_bytes, len, 4 7 11
return symprim_aead_seal(out ? out : nowhere, tag, in ? in : no_bytes, len, 10 1 11
return symprim_aead_seal(out ? out : nowhere, tag, in ? in : no_bytes, len, 1 10 11
150 ad ? ad : no_bytes, ad_len, nonce, key);
151 }
152  
153 26 static int abi_aead_open(uint8_t *out,
154 const uint8_t *in, size_t len, const uint8_t *tag,
155 const uint8_t *ad, size_t ad_len,
156 const uint8_t *nonce, const uint8_t *key)
157 {
158 uint8_t nowhere[1];
159  
160 26 ▶ if (!sealing_fits(out, in, len, tag, ad, ad_len, nonce, key))
Branch: 1 branch
BranchTrueFalseTotal
if (!sealing_fits(out, in, len, tag, ad, ad_len, nonce, key)) 6 20 26
161 6 return SYMPRIM_E_ARGUMENT;
162 20 ▶ return symprim_aead_open(out ? out : nowhere, in ? in : no_bytes, len, tag,
Branch: 3 branches
BranchTrueFalseTotal
return symprim_aead_open(out ? out : nowhere, in ? in : no_bytes, len, tag, 10 10 20
return symprim_aead_open(out ? out : nowhere, in ? in : no_bytes, len, tag, 18 2 20
return symprim_aead_open(out ? out : nowhere, in ? in : no_bytes, len, tag, 2 18 20
163 ad ? ad : no_bytes, ad_len, nonce, key);
164 }
165  
166 /* ---- what everything rests on --------------------------------------------- */
167  
168 7 static int abi_random_bytes(void *buf, size_t len)
169 {
170 7 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 2 5 7
171 2 return SYMPRIM_OK;
172 5 ▶ if (!buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) 1 4 5
173 1 return SYMPRIM_E_ARGUMENT;
174 4 return symprim_random(buf, len);
175 }
176  
177 4 static void abi_wipe(void *buf, size_t len)
178 {
179 4 ▶ if (buf)
Branch: 1 branch
BranchTrueFalseTotal
if (buf) 3 1 4
180 3 symprim_wipe(buf, len);
181 4 }
182  
183 7 static int abi_equal(const void *a, const void *b, size_t len)
184 {
185 7 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 2 5 7
186 2 return 1;
187 5 ▶ if (!a || !b)
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) 4 1 5
if (!a || !b) 1 3 4
188 2 return 0;
189 3 return symprim_equal(a, b, len);
190 }
191  
192 8 static int abi_lock_memory(void *buf, size_t len)
193 {
194 8 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 4 4 8
195 4 return SYMPRIM_OK;
196 4 ▶ if (!buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) 1 3 4
197 1 return SYMPRIM_E_ARGUMENT;
198 3 return symprim_lock_memory(buf, len);
199 }
200  
201 8 static int abi_unlock_memory(void *buf, size_t len)
202 {
203 8 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 4 4 8
204 4 return SYMPRIM_OK;
205 4 ▶ if (!buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) 1 3 4
206 1 return SYMPRIM_E_ARGUMENT;
207 3 return symprim_unlock_memory(buf, len);
208 }
209  
210 /* ---- the table ------------------------------------------------------------ */
211  
212 /* In the order of the structure, which is the order they were added in and
213 * never changes. A new member goes at the end of both. */
214 static const symprim_abi table = {
215 SYMPRIM_ABI_VERSION,
216 (uint32_t)sizeof(symprim_abi),
217 abi_blake2b,
218 abi_blake2b_init,
219 abi_blake2b_update,
220 abi_blake2b_final,
221 abi_argon2id,
222 abi_aead_seal,
223 abi_aead_open,
224 abi_random_bytes,
225 abi_wipe,
226 abi_equal,
227 abi_lock_memory,
228 abi_unlock_memory,
229 symprim_error_message
230 };
231  
232 24 const symprim_abi *symprim_abi_table(void)
233 {
234 24 return &table;
235 }
« blib/lib/Crypt/Symmetric/Primitives/BLAKE2b.pm ↑ Summary src/symprim.c »