shrub/jets/e/blake.c

73 lines
1.8 KiB
C
Raw Normal View History

2018-08-10 21:04:03 +03:00
/* j/5/blake.c
**
*/
#include "all.h"
#include <argon2.h>
#include "../src/blake2/blake2.h"
/* functions
*/
2018-08-10 21:04:03 +03:00
u3_noun
u3qe_blake(u3_atom wid, u3_atom dat,
u3_atom wik, u3_atom dak,
u3_atom out)
{
c3_assert(_(u3a_is_cat(wid)) && _(u3a_is_cat(wik)) && _(u3a_is_cat(out)));
2018-08-10 21:04:03 +03:00
// flip endianness for the internal blake2b function
dat = u3qc_rev(3, wid, dat);
dak = u3qc_rev(3, wik, dak);
c3_y* dat_y = (c3_y*)u3a_malloc(wid);
u3r_bytes(0, wid, (void*)dat_y, dat);
2018-08-10 21:04:03 +03:00
c3_y* dak_y = (c3_y*)u3a_malloc(wik);
u3r_bytes(0, wik, (void*)dak_y, dak);
2018-08-10 21:04:03 +03:00
int ret;
c3_y out_y[64];
ret = blake2b(out_y, // OUT: output
out, // IN: max output size
dat_y, // IN: msg body
wid, // IN: msg len
dak_y, // IN: key body
wik); // IN: key len
2018-11-04 22:42:33 +03:00
/* free() BEFORE checking error code;
we don't want to leak memory if we return early
*/
u3a_free(dat_y);
u3a_free(dak_y);
2018-11-04 22:42:33 +03:00
if ( 0 != ret )
{
fprintf(stderr, "\rblake jet: cryto lib error\n");
return u3m_bail(c3__exit);
}
2018-11-04 22:42:33 +03:00
return u3kc_rev(3, out, u3i_bytes(out, out_y));
2018-08-10 21:04:03 +03:00
}
u3_noun
u3we_blake(u3_noun cor)
2018-11-04 22:42:33 +03:00
{
u3_noun msg, key, out, // arguments
wid, dat, // destructured msg
wik, dak; // destructured key
if ( c3n == u3r_mean(cor, u3x_sam_2, &msg,
u3x_sam_6, &key,
u3x_sam_7, &out, 0) ||
u3r_cell(msg, &wid, &dat) || u3ud(wid) || u3ud(dat) ||
u3r_cell(key, &wik, &dak) || u3ud(wik) || u3ud(dak) ||
u3ud(out) )
{
fprintf(stderr, "\rblake jet: arguments error\n");
return u3m_bail(c3__exit);
} else {
return u3qe_blake(wid, dat, wik, dak, out);
}
2018-08-10 21:04:03 +03:00
}