2013-09-29 00:21:18 +04:00
|
|
|
/* j/5/shax.c
|
|
|
|
**
|
|
|
|
** This file is in the public domain.
|
|
|
|
*/
|
|
|
|
#include "all.h"
|
2014-09-04 07:10:43 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-05 03:29:08 +03:00
|
|
|
#if defined(U3_OS_osx)
|
2014-04-21 01:51:35 +04:00
|
|
|
#include <CommonCrypto/CommonDigest.h>
|
|
|
|
#else
|
2013-09-29 00:21:18 +04:00
|
|
|
#include <openssl/sha.h>
|
2014-04-21 01:51:35 +04:00
|
|
|
#endif
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
/* functions
|
|
|
|
*/
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cqe_shax(
|
|
|
|
u3_atom a)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-11-06 03:20:01 +03:00
|
|
|
c3_w met_w = u3r_met(3, a);
|
2014-04-02 04:47:01 +04:00
|
|
|
c3_y* fat_y = c3_malloc(met_w + 1);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
u3r_bytes(0, met_w, fat_y, a);
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
c3_y dig_y[32];
|
2014-11-05 03:29:08 +03:00
|
|
|
#if defined(U3_OS_osx)
|
2014-04-21 01:51:35 +04:00
|
|
|
CC_SHA256_CTX ctx_h;
|
|
|
|
|
|
|
|
CC_SHA256_Init(&ctx_h);
|
|
|
|
CC_SHA256_Update(&ctx_h, fat_y, met_w);
|
|
|
|
CC_SHA256_Final(dig_y, &ctx_h);
|
|
|
|
#else
|
2013-09-29 00:21:18 +04:00
|
|
|
SHA256_CTX ctx_h;
|
|
|
|
|
|
|
|
SHA256_Init(&ctx_h);
|
|
|
|
SHA256_Update(&ctx_h, fat_y, met_w);
|
|
|
|
SHA256_Final(dig_y, &ctx_h);
|
2014-04-21 01:51:35 +04:00
|
|
|
#endif
|
2013-12-17 04:01:56 +04:00
|
|
|
free(fat_y);
|
2014-11-06 03:20:01 +03:00
|
|
|
return u3i_bytes(32, dig_y);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cqe_shal(u3_atom a,
|
|
|
|
u3_atom b)
|
2014-01-21 06:10:40 +04:00
|
|
|
{
|
2014-11-06 03:20:01 +03:00
|
|
|
c3_assert(_(u3a_is_cat(a)));
|
2014-04-02 04:47:01 +04:00
|
|
|
c3_y* fat_y = c3_malloc(a + 1);
|
2014-01-21 06:10:40 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
u3r_bytes(0, a, fat_y, b);
|
2014-01-21 06:10:40 +04:00
|
|
|
{
|
|
|
|
c3_y dig_y[64];
|
2014-11-05 03:29:08 +03:00
|
|
|
#if defined(U3_OS_osx)
|
2014-04-21 01:51:35 +04:00
|
|
|
CC_SHA512_CTX ctx_h;
|
|
|
|
|
|
|
|
CC_SHA512_Init(&ctx_h);
|
|
|
|
CC_SHA512_Update(&ctx_h, fat_y, a);
|
|
|
|
CC_SHA512_Final(dig_y, &ctx_h);
|
|
|
|
#else
|
2014-01-21 06:10:40 +04:00
|
|
|
SHA512_CTX ctx_h;
|
|
|
|
|
|
|
|
SHA512_Init(&ctx_h);
|
2014-01-22 03:01:14 +04:00
|
|
|
SHA512_Update(&ctx_h, fat_y, a);
|
2014-01-21 06:10:40 +04:00
|
|
|
SHA512_Final(dig_y, &ctx_h);
|
2014-04-21 01:51:35 +04:00
|
|
|
#endif
|
2014-01-21 06:10:40 +04:00
|
|
|
free(fat_y);
|
2014-11-06 03:20:01 +03:00
|
|
|
return u3i_bytes(64, dig_y);
|
2014-01-21 06:10:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cqe_shas(
|
|
|
|
u3_atom sal,
|
|
|
|
u3_atom ruz)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun one = u3_cqe_shax(ruz);
|
|
|
|
u3_noun two = u3_cqc_mix(sal, one);
|
|
|
|
u3_noun tri = u3_cqe_shax(two);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(one); u3z(two); return tri;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cwe_shax(
|
|
|
|
u3_noun cor)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun a;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
if ( (u3_none == (a = u3r_at(u3v_sam, cor))) ||
|
2014-11-05 04:18:47 +03:00
|
|
|
(c3n == u3ud(a)) )
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-11-06 03:20:01 +03:00
|
|
|
return u3m_bail(c3__exit);
|
2013-09-29 00:21:18 +04:00
|
|
|
} else {
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_cqe_shax(a);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cwe_shal(u3_noun cor)
|
2014-01-21 06:10:40 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun a, b;
|
2014-01-21 06:10:40 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
if ( (u3_none == (a = u3r_at(u3v_sam_2, cor))) ||
|
|
|
|
(u3_none == (b = u3r_at(u3v_sam_3, cor))) ||
|
2014-11-05 04:18:47 +03:00
|
|
|
(c3n == u3ud(a)) ||
|
2014-11-06 03:20:01 +03:00
|
|
|
(c3n == u3a_is_cat(a)) ||
|
2014-11-05 04:18:47 +03:00
|
|
|
(c3n == u3ud(b)) )
|
2014-01-21 06:10:40 +04:00
|
|
|
{
|
2014-11-06 03:20:01 +03:00
|
|
|
return u3m_bail(c3__exit);
|
2014-01-21 06:10:40 +04:00
|
|
|
} else {
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_cqe_shal(a, b);
|
2014-01-21 06:10:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cwe_shas(u3_noun cor)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun sal, ruz;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
if ( (u3_none == (sal = u3r_at(u3v_sam_2, cor))) ||
|
|
|
|
(u3_none == (ruz = u3r_at(u3v_sam_3, cor))) ||
|
2014-11-05 04:18:47 +03:00
|
|
|
(c3n == u3ud(sal)) ||
|
|
|
|
(c3n == u3ud(ruz)) )
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-11-06 03:20:01 +03:00
|
|
|
return u3m_bail(c3__exit);
|
2013-09-29 00:21:18 +04:00
|
|
|
} else {
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_cqe_shas(sal, ruz);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
static u3_noun
|
2014-08-20 10:22:41 +04:00
|
|
|
_og_list(
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun a,
|
|
|
|
u3_noun b,
|
|
|
|
u3_noun c)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun l = u3_nul;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
if ( !_(u3a_is_cat(b)) ) {
|
|
|
|
return u3m_bail(c3__fail);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
while ( 0 != b ) {
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun x = u3_cqc_mix(a, c);
|
|
|
|
u3_noun y = u3_cqc_mix(b, x);
|
|
|
|
u3_noun d = u3_cqe_shas(c3_s4('o','g','-','b'), y);
|
|
|
|
u3_noun m;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(x); u3z(y);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
if ( b < 256 ) {
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun e = u3_cqc_end(0, b, d);
|
2014-01-21 06:10:40 +04:00
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(d);
|
|
|
|
m = u3nc(b, e);
|
2013-09-29 00:21:18 +04:00
|
|
|
b = 0;
|
|
|
|
} else {
|
2014-09-06 00:13:24 +04:00
|
|
|
m = u3nc(256, d);
|
2013-09-29 00:21:18 +04:00
|
|
|
c = d;
|
|
|
|
|
|
|
|
b -= 256;
|
|
|
|
}
|
2014-09-06 00:13:24 +04:00
|
|
|
l = u3nc(m, l);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_ckb_flop(l);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2014-01-21 06:10:40 +04:00
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cqeo_raw(u3_atom a, u3_atom b)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun x = u3_cqc_mix(b, a);
|
|
|
|
u3_noun c = u3_cqe_shas(c3_s4('o','g','-','a'), x);
|
|
|
|
u3_noun l = _og_list(a, b, c);
|
|
|
|
u3_noun r = u3_cqc_can(0, l);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(l);
|
|
|
|
u3z(c);
|
|
|
|
u3z(x);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cweo_raw(u3_noun cor)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun a, b;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
if ( c3n == u3r_mean(cor, u3v_sam, &b, u3v_con_sam, &a, 0) ) {
|
|
|
|
return u3m_bail(c3__exit);
|
2013-09-29 00:21:18 +04:00
|
|
|
} else {
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_cqeo_raw(a, b);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|