SCM yacl_scm_hkdf_sha256 (SCM ikm, SCM salt, SCM info) { int rc; uint8_t * ikm_ptr, *salt_ptr, *info_ptr; size_t ikm_len, salt_len, info_len; if (!scm_is_bytevector (ikm)) scm_throw (scm_from_locale_symbol ("BADIKM"), SCM_BOOL_T); ikm_ptr = SCM_BYTEVECTOR_CONTENTS (ikm); ikm_len = SCM_BYTEVECTOR_LENGTH (ikm); if (SCM_UNBNDP (salt)) { salt_ptr = NULL; salt_len = 0; } else if (!scm_is_bytevector (salt)) scm_throw (scm_from_locale_symbol ("BADSALT"), SCM_BOOL_T); else { salt_ptr = SCM_BYTEVECTOR_CONTENTS(salt); salt_len = SCM_BYTEVECTOR_LENGTH (salt); } if (SCM_UNBNDP (info)) { info_ptr = NULL; info_len = 0; } else if (!scm_is_bytevector (info)) scm_throw (scm_from_locale_symbol ("BADINFO"), SCM_BOOL_T); else { info_ptr = SCM_BYTEVECTOR_CONTENTS(info); info_len = SCM_BYTEVECTOR_LENGTH (info); } SCM out = scm_c_make_bytevector (YACL_SHA256_LEN); rc = yacl_hkdf_256(salt_ptr, salt_len, ikm_ptr, ikm_len, info_ptr, info_len, SCM_BYTEVECTOR_CONTENTS (out), YACL_SHA256_LEN); if (rc) scm_throw (scm_from_locale_symbol ("BADHKDF"), SCM_BOOL_T); return out; }
/* Change the size of a port's bytevector to NEW_SIZE. This doesn't change `read_buf_size'. */ static void st_resize_port (scm_t_port *pt, scm_t_off new_size) { SCM old_stream = SCM_PACK (pt->stream); const signed char *src = SCM_BYTEVECTOR_CONTENTS (old_stream); SCM new_stream = scm_c_make_bytevector (new_size); signed char *dst = SCM_BYTEVECTOR_CONTENTS (new_stream); unsigned long int old_size = SCM_BYTEVECTOR_LENGTH (old_stream); unsigned long int min_size = min (old_size, new_size); scm_t_off offset = pt->write_pos - pt->write_buf; pt->write_buf_size = new_size; memcpy (dst, src, min_size); scm_remember_upto_here_1 (old_stream); /* reset buffer. */ { pt->stream = SCM_UNPACK (new_stream); pt->read_buf = pt->write_buf = (unsigned char *)dst; pt->read_pos = pt->write_pos = pt->write_buf + offset; pt->write_end = pt->write_buf + pt->write_buf_size; pt->read_end = pt->read_buf + pt->read_buf_size; } }
SCM yacl_scm_p256_sign(SCM data, SCM d) { int rc; uint8_t out[YACL_SHA256_LEN*2] = {}; unsigned char* data_ptr = SCM_BYTEVECTOR_CONTENTS (data); size_t data_len = SCM_BYTEVECTOR_LENGTH (data); unsigned char* d_ptr = SCM_BYTEVECTOR_CONTENTS (d); size_t d_len = SCM_BYTEVECTOR_LENGTH (d); rc = yacl_hash_ecdsa_sign(data_ptr, data_len, d_ptr, out); SCM sig = scm_c_make_bytevector (YACL_SHA256_LEN*2); memcpy (SCM_BYTEVECTOR_CONTENTS (sig), &out, YACL_SHA256_LEN*2); return sig; }
SCM yacl_scm_hexdump (SCM bv) { int rc; unsigned char* data_ptr = SCM_BYTEVECTOR_CONTENTS (bv); size_t data_len = SCM_BYTEVECTOR_LENGTH (bv); yacl_hexdump (data_ptr, data_len); return SCM_BOOL_T; }
SCM yacl_scm_p256_verify(SCM data, SCM q, SCM sig) { int rc; unsigned char* data_ptr = SCM_BYTEVECTOR_CONTENTS (data); size_t data_len = SCM_BYTEVECTOR_LENGTH (data); unsigned char* q_ptr = SCM_BYTEVECTOR_CONTENTS (q); size_t q_len = SCM_BYTEVECTOR_LENGTH (q); unsigned char* sig_ptr = SCM_BYTEVECTOR_CONTENTS (sig); size_t sig_len = SCM_BYTEVECTOR_LENGTH (sig); rc = yacl_hash_verify(data_ptr, data_len, q_ptr, sig_ptr); if (0 == rc) return SCM_BOOL_T; else return SCM_BOOL_F; }
static void copy_to_bytevector (const uint8_t *src, unsigned int len, SCM bv) { unsigned int x = 0; assert (SCM_BYTEVECTOR_LENGTH (bv) == len); for (x = 0; x < len; x++) { scm_c_bytevector_set_x (bv, x, src[x]); } }
SCM yacl_scm_sha256 (SCM bv) { int rc; uint8_t out[YACL_SHA256_LEN] = {}; signed char* p = SCM_BYTEVECTOR_CONTENTS (bv); size_t len = SCM_BYTEVECTOR_LENGTH (bv); rc = yacl_sha256 (p, len, out); SCM digest = scm_c_make_bytevector (YACL_SHA256_LEN); memcpy (SCM_BYTEVECTOR_CONTENTS (digest), &out, YACL_SHA256_LEN); return digest; }
SCM yacl_scm_b64url_encode (SCM bv) { if (!scm_is_bytevector (bv)) scm_throw (scm_from_locale_symbol ("BADBV"), SCM_BOOL_T); uint8_t *bv_ptr; size_t bv_len; bv_ptr = SCM_BYTEVECTOR_CONTENTS (bv); bv_len = SCM_BYTEVECTOR_LENGTH (bv); char *b64url = yacl_b64url_encode (bv_ptr, bv_len); if (NULL == b64url) scm_throw (scm_from_locale_symbol ("BADENCODE"), SCM_BOOL_T); SCM out = scm_from_utf8_string (b64url); free (b64url); return out; }