move argon unpacking helpers to u3r

This commit is contained in:
Paul Driver 2020-08-11 13:04:04 -07:00
parent 6deec62292
commit 3f917d7848
3 changed files with 110 additions and 45 deletions

View File

@ -348,16 +348,26 @@
/* u3r_bytes_fit(): /* u3r_bytes_fit():
** **
** Copy (len_w) bytes of (a) into (buf_y) if it fits, returning overage ** Copy (len_w) bytes of (a) into (buf_y) if it fits, returning overage.
*/ */
c3_w c3_w
u3r_bytes_fit(c3_w len_w, u3r_bytes_fit(c3_w len_w,
c3_y* buf_y, c3_y* buf_y,
u3_atom a); u3_atom a);
/* u3r_bytes_alloc():
**
** Copy (len_w) bytes starting at (a_w) from (b) into a fresh allocation.
*/
c3_y*
u3r_bytes_alloc(c3_w a_w,
c3_w len_w,
u3_atom b);
/* u3r_bytes_all(): /* u3r_bytes_all():
** **
** Allocate a new byte array with all the bytes of (a) ** Allocate and return a new byte array with all the bytes of (a),
** storing the length in (len_w).
*/ */
c3_y* c3_y*
u3r_bytes_all(c3_w* len_w, u3r_bytes_all(c3_w* len_w,
@ -393,6 +403,23 @@
u3r_word(c3_w a_w, u3r_word(c3_w a_w,
u3_atom b); u3_atom b);
/* u3r_word_fit():
**
** Fill (out_w) with (a) if it fits, returning success.
*/
c3_t
u3r_word_fit(c3_w* out_w,
u3_atom a);
/* u3r_size_fit():
**
** Fill (out) with (a) if it fits, returning success.
*/
c3_t
u3r_size_fit(size_t *out_p,
u3_atom a);
/* u3r_chub(): /* u3r_chub():
** **
** Return double-word (a_w) of (b). ** Return double-word (a_w) of (b).

View File

@ -7,31 +7,6 @@
/* helpers /* helpers
*/ */
static c3_t
_cqear_unpack_word(c3_w *out, u3_atom in)
{
if ( u3r_met(5, in) > 1 ) {
return 0;
}
else {
*out = u3r_word(0, in);
return 1;
}
}
static c3_t
_cqear_unpack_size(size_t *out, u3_atom in)
{
c3_w out_w;
if ( _cqear_unpack_word(&out_w, in) ) {
*out = out_w;
return 1;
}
else {
return 0;
}
}
static c3_t static c3_t
_cqear_unpack_type(urcrypt_argon2_type *out, u3_atom in) _cqear_unpack_type(urcrypt_argon2_type *out, u3_atom in)
{ {
@ -56,9 +31,9 @@
static c3_y* static c3_y*
_cqear_unpack_bytes(size_t size, u3_atom in) _cqear_unpack_bytes(size_t size, u3_atom in)
{ {
c3_y* out = u3a_malloc(size); c3_w len_w = size;
u3r_bytes(0, (c3_w) size, out, in); c3_assert(size == len_w);
return out; return u3r_bytes_alloc(0, len_w, in);
} }
/* functions /* functions
@ -76,16 +51,16 @@
c3_w ver_w, ted_w, mem_w, tim_w; c3_w ver_w, ted_w, mem_w, tim_w;
urcrypt_argon2_type typ_u; urcrypt_argon2_type typ_u;
if ( !(_cqear_unpack_size(&out_sz, out) && if ( !(u3r_size_fit(&out_sz, out) &&
_cqear_unpack_type(&typ_u, type) && _cqear_unpack_type(&typ_u, type) &&
_cqear_unpack_word(&ver_w, version) && u3r_word_fit(&ver_w, version) &&
_cqear_unpack_word(&ted_w, threads) && u3r_word_fit(&ted_w, threads) &&
_cqear_unpack_word(&mem_w, mem_cost) && u3r_word_fit(&mem_w, mem_cost) &&
_cqear_unpack_word(&tim_w, time_cost) && u3r_word_fit(&tim_w, time_cost) &&
_cqear_unpack_size(&key_sz, wik) && u3r_size_fit(&key_sz, wik) &&
_cqear_unpack_size(&ex_sz, wix) && u3r_size_fit(&ex_sz, wix) &&
_cqear_unpack_size(&dat_sz, wid) && u3r_size_fit(&dat_sz, wid) &&
_cqear_unpack_size(&sat_sz, wis)) ) { u3r_size_fit(&sat_sz, wis)) ) {
u3l_log("%s\r\n", "argon2-punt"); u3l_log("%s\r\n", "argon2-punt");
return u3_none; return u3_none;
} }
@ -96,6 +71,7 @@
*dat_y = _cqear_unpack_bytes(dat_sz, dat), *dat_y = _cqear_unpack_bytes(dat_sz, dat),
*sat_y = _cqear_unpack_bytes(sat_sz, sat), *sat_y = _cqear_unpack_bytes(sat_sz, sat),
*out_y = u3a_malloc(out_sz); *out_y = u3a_malloc(out_sz);
const c3_c* err_c = urcrypt_argon2( const c3_c* err_c = urcrypt_argon2(
typ_u, ver_w, ted_w, mem_w, tim_w, typ_u, ver_w, ted_w, mem_w, tim_w,
key_sz, key_y, key_sz, key_y,

View File

@ -1093,18 +1093,30 @@ u3r_bytes_fit(c3_w len_w, c3_y *buf_y, u3_atom a)
} }
} }
/* u3r_bytes_alloc():
**
** Copy (len_w) bytes starting at (a_w) from (b) into a fresh allocation.
*/
c3_y*
u3r_bytes_alloc(c3_w a_w,
c3_w len_w,
u3_atom b)
{
c3_y* b_y = u3a_malloc(len_w);
u3r_bytes(a_w, a_w + len_w, b_y, b);
return b_y;
}
/* u3r_bytes_all(): /* u3r_bytes_all():
** **
** Allocate a new byte array with all the bytes of (a) ** Allocate and return a new byte array with all the bytes of (a),
** storing the length in (len_w).
*/ */
c3_y* c3_y*
u3r_bytes_all(c3_w* len_w, u3_atom a) u3r_bytes_all(c3_w* len_w, u3_atom a)
{ {
c3_w met_w = u3r_met(3, a); c3_w met_w = *len_w = u3r_met(3, a);
c3_y* a_y = u3a_malloc(met_w); return u3r_bytes_alloc(0, met_w, a);
u3r_bytes(0, met_w, a_y, a);
*len_w = met_w;
return a_y;
} }
/* u3r_mp(): /* u3r_mp():
@ -1165,6 +1177,56 @@ u3r_word(c3_w a_w,
} }
} }
/* u3r_word_fit():
**
** Fill (out_w) with (a) if it fits, returning success.
*/
c3_t
u3r_word_fit(c3_w *out_w, u3_atom a)
{
if ( u3r_met(5, a) > 1 ) {
return 0;
}
else {
*out_w = u3r_word(0, a);
return 1;
}
}
/* u3r_size_fit():
**
** Fill (out) with (a) if it fits, returning success.
*/
c3_t
u3r_size_fit(size_t *out_p, u3_atom a)
{
if ( 0 == a ) {
*out_p = 0;
return 1;
}
else {
c3_w met_w = u3r_met(3, a);
if ( met_w > sizeof(size_t) ) {
return 0;
}
else {
u3r_bytes(0, sizeof(size_t), (c3_y*) out_p, a);
#if c3_endian == c3_endian_big
{ // reverse those bytes
c3_w i_w, j_w;
c3_y tmp, *s_y = (c3_y*) out_p;
for ( i_w = 0, j_w = sizeof(size_t) - 1; i_w < j_w; ++i_w, --j_w ) {
tmp = s_y[i_w];
s_y[i_w] = s_y[j_w];
s_y[j_w] = tmp;
}
}
#endif
return 1;
}
}
}
/* u3r_chub(): /* u3r_chub():
** **
** Return double-word (a_w) of (b). ** Return double-word (a_w) of (b).