From aea45ee03797b5a6514263d52f1f067d37f6e495 Mon Sep 17 00:00:00 2001 From: Paul Driver Date: Thu, 20 Aug 2020 14:43:22 -0700 Subject: [PATCH] ripemd-160->urcrypt --- pkg/urbit/include/jets/q.h | 10 ------ pkg/urbit/jets/e/argon2.c | 13 ++++--- pkg/urbit/jets/e/ripe.c | 69 +++++++++++--------------------------- pkg/urbit/noun/manage.c | 1 + pkg/urcrypt/urcrypt.c | 17 ++++++++++ pkg/urcrypt/urcrypt.h | 2 ++ 6 files changed, 48 insertions(+), 64 deletions(-) diff --git a/pkg/urbit/include/jets/q.h b/pkg/urbit/include/jets/q.h index 68f946603..de2f95257 100644 --- a/pkg/urbit/include/jets/q.h +++ b/pkg/urbit/include/jets/q.h @@ -138,16 +138,6 @@ u3_atom, u3_atom, u3_atom, u3_atom); - u3_noun u3qe_argon2(u3_atom, u3_atom, u3_atom, - u3_atom, u3_atom, u3_atom, - u3_atom, u3_atom, u3_atom, u3_atom, - u3_atom, u3_atom, u3_atom, u3_atom); - - u3_noun u3qe_blake(u3_atom wid, u3_atom dat, - u3_atom wik, u3_atom dak, u3_atom out); - - u3_noun u3qe_ripe(u3_atom wid, u3_atom dat); - u3_noun u3qe_make(u3_atom has, u3_atom prv); u3_noun u3qe_reco(u3_atom has, u3_atom sig_v, u3_atom sig_r, u3_atom sig_s); u3_noun u3qe_sign(u3_atom has, u3_atom prv); diff --git a/pkg/urbit/jets/e/argon2.c b/pkg/urbit/jets/e/argon2.c index 7b47628d0..b9c9415ac 100644 --- a/pkg/urbit/jets/e/argon2.c +++ b/pkg/urbit/jets/e/argon2.c @@ -43,15 +43,18 @@ c3_w out_w, wik_w, wix_w, wid_w, wis_w, ver_w, ted_w, mem_w, tim_w; if ( !(u3r_word_fit(&out_w, out) && - _cqear_unpack_type(&typ_u, type) && - u3r_word_fit(&ver_w, version) && - u3r_word_fit(&ted_w, threads) && - u3r_word_fit(&mem_w, mem_cost) && - u3r_word_fit(&tim_w, time_cost) && u3r_word_fit(&wik_w, wik) && u3r_word_fit(&wix_w, wix) && u3r_word_fit(&wid_w, wid) && u3r_word_fit(&wis_w, wis)) ) { + // too big to allocate + return u3m_bail(c3__fail); + } + else if ( !(_cqear_unpack_type(&typ_u, type) && + u3r_word_fit(&ver_w, version) && + u3r_word_fit(&ted_w, threads) && + u3r_word_fit(&mem_w, mem_cost) && + u3r_word_fit(&tim_w, time_cost)) ) { u3l_log("%s\r\n", "argon2-punt"); return u3_none; } diff --git a/pkg/urbit/jets/e/ripe.c b/pkg/urbit/jets/e/ripe.c index 908e6aa1c..971a04b2c 100644 --- a/pkg/urbit/jets/e/ripe.c +++ b/pkg/urbit/jets/e/ripe.c @@ -2,62 +2,34 @@ ** */ #include "all.h" -#include +#include /* functions */ - - u3_noun - u3qe_ripe(u3_atom wid, u3_atom dat) + static u3_atom + _cqe_ripe(u3_atom wid, u3_atom dat) { - c3_assert(_(u3a_is_cat(wid))); - dat = u3qc_rev(3, wid, dat); + c3_w len_w; + if ( !u3r_word_fit(&len_w, wid) ) { + return u3m_bail(c3__fail); + } + else { + u3_atom ret; + c3_y out_y[20]; + c3_y *dat_y = u3r_bytes_alloc(0, len_w, dat); - c3_y* dat_y = (c3_y*)u3a_malloc(wid); // msg body - u3r_bytes(0, wid, (void*)dat_y, dat); - - const EVP_MD* rip_u = EVP_ripemd160(); // ripem algorithm - EVP_MD_CTX* con_u = EVP_MD_CTX_create(); - - /* perform signature - */ - - c3_y sib_y[20]; // signature body - c3_w sil_w; // signature length - c3_w ret_w; // return code - - ret_w = EVP_DigestInit_ex(con_u, rip_u, NULL); - if ( 1 != ret_w ) { + if ( 0 == urcrypt_ripemd160(dat_y, len_w, out_y) ) { + ret = u3i_bytes(20, out_y); + } + else { + u3l_log("%s\r\n", "ripemd160-punt"); + ret = u3_none; + } u3a_free(dat_y); - EVP_MD_CTX_destroy(con_u); - u3l_log("\rripe jet: crypto library fail 1\n"); - return u3m_bail(c3__fail); + return ret; } - - ret_w = EVP_DigestUpdate(con_u, (void*)dat_y, wid); - u3a_free(dat_y); - if (1 != ret_w) { - EVP_MD_CTX_destroy(con_u); - u3l_log("\rripe jet: crypto library fail 2\n"); - return u3m_bail(c3__fail); - } - - ret_w = EVP_DigestFinal_ex(con_u, sib_y, &sil_w); - if ( 1 != ret_w ) { - EVP_MD_CTX_destroy(con_u); - u3l_log("\rripe jet: crypto library fail 3\n"); - return u3m_bail(c3__fail); - } - - EVP_MD_CTX_destroy(con_u); - - /* endian conversion; - turn into noun for return - */ - return u3kc_rev(3, sil_w, u3i_bytes(sil_w, sib_y)); } - u3_noun u3we_ripe(u3_noun cor) { @@ -68,10 +40,9 @@ u3ud(wid) || u3ud(dat)) ) { - u3l_log("\rripe jet: argument error\n"); return u3m_bail(c3__exit); } else { - return u3qe_ripe(wid, dat); + return _cqe_ripe(wid, dat); } } diff --git a/pkg/urbit/noun/manage.c b/pkg/urbit/noun/manage.c index 852c2d777..873cc3fee 100644 --- a/pkg/urbit/noun/manage.c +++ b/pkg/urbit/noun/manage.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "all.h" diff --git a/pkg/urcrypt/urcrypt.c b/pkg/urcrypt/urcrypt.c index 4566ff693..efdd4ad55 100644 --- a/pkg/urcrypt/urcrypt.c +++ b/pkg/urcrypt/urcrypt.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -525,6 +526,22 @@ urcrypt_aes_cbcc_de(uint8_t **message_ptr, } } +int +urcrypt_ripemd160(uint8_t *message, size_t length, uint8_t out[20]) +{ + unsigned long n = length; + + if ( length != n ) { + return -1; + } + else { + _urcrypt_reverse(length, message); + RIPEMD160(message, n, out); + _urcrypt_reverse(20, out); + return 0; + } +} + /* argon2 does memory allocation, but takes function pointers in the context. * the signatures don't match, so we need these wrappers. */ diff --git a/pkg/urcrypt/urcrypt.h b/pkg/urcrypt/urcrypt.h index 4d326d4a1..397fde87d 100644 --- a/pkg/urcrypt/urcrypt.h +++ b/pkg/urcrypt/urcrypt.h @@ -100,6 +100,8 @@ int urcrypt_aes_cbcc_de(uint8_t **message_ptr, uint8_t ivec[16], urcrypt_realloc_t realloc_ptr); +int urcrypt_ripemd160(uint8_t *message, size_t length, uint8_t out[20]); + typedef enum urcrypt_argon2_type { urcrypt_argon2_d = 0, urcrypt_argon2_i = 1,