void blake2b_too(void *pout, const void *in) { uint8_t *out = (uint8_t *)pout; uint8_t out_buffer[64]; uint8_t in_buffer[64]; blake2b_state blake_state; blake2b_init(&blake_state, 64); blake_state.buflen = blake_state.buf[1] = 4; my_blake2b_update(&blake_state, in, 72); blake2b_final(&blake_state, out_buffer, 64); memcpy(out, out_buffer, 32); out += 32; register uint8_t i = 29; while (i--) { memcpy(in_buffer, out_buffer, 64); blake2b(out_buffer, in_buffer, NULL, 0); memcpy(out, out_buffer, 32); out += 32; } memcpy(in_buffer, out_buffer, 64); blake2b(out_buffer, in_buffer, NULL, 0); memcpy(out, out_buffer, 64); burn(&blake_state, sizeof(blake_state)); }
int blake2b_selftest(void) { const uint8_t d0[32] = { 0xc2,0x3a,0x78,0x00,0xd9,0x81,0x23,0xbd, 0x10,0xf5,0x06,0xc6,0x1e,0x29,0xda,0x56, 0x03,0xd7,0x63,0xb8,0xbb,0xad,0x2e,0x73, 0x7f,0x5e,0x76,0x5a,0x7b,0xcc,0xd4,0x75, }; const unsigned dlen[4] = { 20, 32, 48, 64 }; const unsigned mlen[6] = { 0, 3, 128, 129, 255, 1024 }; uint8_t m[1024], d[64], k[64]; struct blake2b ctx; unsigned di, mi, i; blake2b_init(&ctx, 32, NULL, 0); for (di = 0; di < 4; di++) { for (mi = 0; mi < 6; mi++) { blake2_selftest_prng(m, mlen[mi], mlen[mi]); blake2b(d, dlen[di], NULL, 0, m, mlen[mi]); blake2b_update(&ctx, d, dlen[di]); blake2_selftest_prng(k, dlen[di], dlen[di]); blake2b(d, dlen[di], k, dlen[di], m, mlen[mi]); blake2b_update(&ctx, d, dlen[di]); } } blake2b_final(&ctx, d); for (i = 0; i < 32; i++) { if (d[i] != d0[i]) return -1; } return 0; }
/* MMXVI Team - Begin Code */ int blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) { uint8_t *out = (uint8_t *)pout; blake2b_state blake_state; uint8_t outlen_bytes[sizeof(uint32_t)] = {0}; int ret = -1; if (outlen > UINT32_MAX) { goto fail; } /* Ensure little-endian byte order! */ store32(outlen_bytes, (uint32_t)outlen); #define TRY(statement) \ do { \ ret = statement; \ if (ret < 0) { \ goto fail; \ } \ } while ((void)0, 0) if (outlen <= BLAKE2B_OUTBYTES) { TRY(blake2b_init(&blake_state, outlen)); TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes))); TRY(blake2b_update(&blake_state, in, inlen)); TRY(blake2b_final(&blake_state, out, outlen)); } else { uint32_t toproduce; uint8_t out_buffer[BLAKE2B_OUTBYTES]; uint8_t in_buffer[BLAKE2B_OUTBYTES]; TRY(blake2b_init(&blake_state, BLAKE2B_OUTBYTES)); TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes))); TRY(blake2b_update(&blake_state, in, inlen)); TRY(blake2b_final(&blake_state, out_buffer, BLAKE2B_OUTBYTES)); memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2); out += BLAKE2B_OUTBYTES / 2; toproduce = (uint32_t)outlen - BLAKE2B_OUTBYTES / 2; while (toproduce > BLAKE2B_OUTBYTES) { memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES); TRY(blake2b(out_buffer, BLAKE2B_OUTBYTES, in_buffer, BLAKE2B_OUTBYTES, NULL, 0)); memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2); out += BLAKE2B_OUTBYTES / 2; toproduce -= BLAKE2B_OUTBYTES / 2; } memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES); TRY(blake2b(out_buffer, toproduce, in_buffer, BLAKE2B_OUTBYTES, NULL, 0)); memcpy(out, out_buffer, toproduce); } fail: burn(&blake_state, sizeof(blake_state)); return ret; #undef TRY }
CALL_CONVENTION int run_solver(SolverCtx* ctx, char* header, int header_length, u32 nonce, u32 range, SolverSolutions *solutions, SolverStats *stats ) { u64 time0, time1; u32 timems; u32 sumnsols = 0; for (u32 r = 0; r < range; r++) { time0 = timestamp(); ctx->setheadernonce(header, header_length, nonce + r); print_log("nonce %d k0 k1 k2 k3 %llx %llx %llx %llx\n", nonce+r, ctx->trimmer.sip_keys.k0, ctx->trimmer.sip_keys.k1, ctx->trimmer.sip_keys.k2, ctx->trimmer.sip_keys.k3); u32 nsols = ctx->solve(); time1 = timestamp(); timems = (time1 - time0) / 1000000; print_log("Time: %d ms\n", timems); for (unsigned s = 0; s < nsols; s++) { print_log("Solution"); word_t *prf = &ctx->sols[s * PROOFSIZE]; for (u32 i = 0; i < PROOFSIZE; i++) print_log(" %jx", (uintmax_t)prf[i]); print_log("\n"); if (solutions != NULL){ solutions->edge_bits = EDGEBITS; solutions->num_sols++; solutions->sols[sumnsols+s].nonce = nonce + r; for (u32 i = 0; i < PROOFSIZE; i++) solutions->sols[sumnsols+s].proof[i] = (u64) prf[i]; } int pow_rc = verify(prf, &ctx->trimmer.sip_keys); if (pow_rc == POW_OK) { print_log("Verified with cyclehash "); unsigned char cyclehash[32]; blake2b((void *)cyclehash, sizeof(cyclehash), (const void *)prf, sizeof(proof), 0, 0); for (int i=0; i<32; i++) print_log("%02x", cyclehash[i]); print_log("\n"); } else { print_log("FAILED due to %s\n", errstr[pow_rc]); } } sumnsols += nsols; if (stats != NULL) { stats->device_id = 0; stats->edge_bits = EDGEBITS; strncpy(stats->device_name, "CPU\0", MAX_NAME_LEN); stats->last_start_time = time0; stats->last_end_time = time1; stats->last_solution_time = time1 - time0; } } print_log("%d total solutions\n", sumnsols); return sumnsols > 0; }
int main( int argc, char **argv ) { uint8_t key[BLAKE2B_KEYBYTES]; uint8_t buf[KAT_LENGTH]; for( size_t i = 0; i < BLAKE2B_KEYBYTES; ++i ) key[i] = ( uint8_t )i; for( size_t i = 0; i < KAT_LENGTH; ++i ) buf[i] = ( uint8_t )i; for( size_t i = 0; i < KAT_LENGTH; ++i ) { uint8_t hash[BLAKE2B_OUTBYTES]; blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ); if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) ) { puts( "error" ); return -1; } } puts( "ok" ); return 0; }
int main( int argc, char **argv ) { byte key[BLAKE2B_KEYBYTES]; byte buf[KAT_LENGTH]; for( word32 i = 0; i < BLAKE2B_KEYBYTES; ++i ) key[i] = ( byte )i; for( word32 i = 0; i < KAT_LENGTH; ++i ) buf[i] = ( byte )i; for( word32 i = 0; i < KAT_LENGTH; ++i ) { byte hash[BLAKE2B_OUTBYTES]; if ( blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 ) { puts( "error" ); return -1; } if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) ) { puts( "error" ); return -1; } } puts( "ok" ); return 0; }
bool KeyPair::Decrypt(const Ciphertext& in, Digest key) { unsigned char temp[33]; temp[0] = DAT_BYTE; memcpy(&temp[1], in, 32); if (!ecdh_shared_secret(temp, privateKey, key.Byte)) return false; blake2b(key, sizeof(key), 0, 0, key, sizeof(key)); return true; }
bool KeyPair::Encrypt(Ciphertext& out, Digest key) { KeyPair temp; temp.Keygen(); memcpy(out, &temp.publicKey[1], sizeof(out)); if (!ecdh_shared_secret(publicKey, temp.privateKey, key.Byte)) return false; blake2b(key, sizeof(key), 0, 0, key, sizeof(key)); return true; }
void BlockDecipher(void* data_, u64 length, const void* key, u64 keylength) { u8* data = (u8*)data_; u64 rounds = length*4 + 1; for(u64 i=rounds;i--;) { int index = (int)(i % length); u8 tmp = data[index]; data[index] = i; blake2b(&data[index], 1, key, keylength, data, length); data[index] ^= tmp; } }
void BlockCipher(void* data_, u64 length, const void* key, u64 keylength) { u8* data = (u8*)data_; u64 rounds = length*4 + 1; for(u64 i=0;i<rounds;i++) { u64 index = i % length; u8 tmp = data[index]; data[index] = i; blake2b(&data[index], 1, key, keylength, data, length); data[index] ^= tmp; } }
int main (int argc, char * argv[]) { int rc; char in_str[16], hash_str[16]; printf("Enter in a string to be hashed: \n"); scanf("%s", in_str); rc = blake2b ( hash_str, 16, in_str, strlen(in_str), NULL, 0); fprintf(stdout, "Hashed String: %s\n", hash_str); return 0; }
void StreamCipher(void* data, u64 length, const void* key, u64 keylength) { unsigned i=0; u64 pad[8]; u64* buf = (u64*)data; for(u64 ctr=0;!ctr || length>=8;ctr++) { blake2b(pad, 64, &ctr, 8, key, keylength); u64 j = length / 8; if (j > 8) j = 8; for(i=0;i<j;i++) *buf++ ^= pad[i]; length -= 8 * j; } u8* buf8 = (u8*)buf; u8* pad8 = (u8*)&pad[i]; for(i=0;i<length;i++) *buf8++ ^= *pad8++; }
int crypto_generichash_blake2b(unsigned char *out, size_t outlen, const unsigned char *in, unsigned long long inlen, const unsigned char *key, size_t keylen) { if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES || keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) { return -1; } assert(outlen <= UINT8_MAX); assert(keylen <= UINT8_MAX); return blake2b((uint8_t *) out, in, key, (uint8_t) outlen, (uint64_t) inlen, (uint8_t) keylen); }
ERL_NIF_TERM blake2_hash(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { int bits = 0; enif_get_int(env, argv[0], &bits); ErlNifBinary bin, out; enif_inspect_binary(env, argv[1], &bin); enif_alloc_binary_compat(env, (size_t)(bits/8), &out); int r = blake2b((uint8_t *)out.data, (const void *)bin.data, NULL, (bits / 8), bin.size, 0); if (r == 0) { return enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_binary(env, &out)); } else { return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_atom(env, "hash_failure")); } }
int main(int argc, char **argv) { const char *header = ""; int nonce = 0; int c; while ((c = getopt (argc, argv, "h:n:")) != -1) { switch (c) { case 'h': header = optarg; break; case 'n': nonce = atoi(optarg); break; } } char headernonce[HEADERLEN]; u32 hdrlen = strlen(header); memcpy(headernonce, header, hdrlen); memset(headernonce+hdrlen, 0, sizeof(headernonce)-hdrlen); ((u32 *)headernonce)[HEADERLEN/sizeof(u32)-1] = htole32(nonce); siphash_keys keys; setheader(headernonce, sizeof(headernonce), &keys); printf("nonce %d k0 k1 k2 k3 %llx %llx %llx %llx\n", nonce, keys.k0, keys.k1, keys.k2, keys.k3); printf("Verifying size %d proof for cuckaroo%d(\"%s\",%d)\n", PROOFSIZE, EDGEBITS, header, nonce); for (int nsols=0; scanf(" Solution") == 0; nsols++) { word_t nonces[PROOFSIZE]; for (int n = 0; n < PROOFSIZE; n++) { uint64_t nonce; int nscan = scanf(" %" SCNx64, &nonce); assert(nscan == 1); nonces[n] = nonce; } int pow_rc = verify(nonces, &keys); if (pow_rc == POW_OK) { printf("Verified with cyclehash "); unsigned char cyclehash[32]; blake2b((void *)cyclehash, sizeof(cyclehash), (const void *)nonces, sizeof(nonces), 0, 0); for (int i=0; i<32; i++) printf("%02x", cyclehash[i]); printf("\n"); } else { printf("FAILED due to %s\n", errstr[pow_rc]); } } return 0; }
static PyObject * blake2b_func(PyObject *self, PyObject *args, PyObject *keywds) { char *data; int data_length; int key_length ; long hashSize = BLAKE2B_OUTBYTES; char *key = ""; int rawOutput = 1 ; static char *kwlist[] = {"data","hashSize", "key", "rawOutput", NULL}; if (!PyArg_ParseTupleAndKeywords(args, keywds, "z#|is#h", kwlist, &data, &data_length, &hashSize, &key, &key_length, &rawOutput)){ Py_INCREF(Py_None); return Py_None; } if( NULL == data){ Py_INCREF(Py_None); return Py_None; } char outputHash[ hashSize ]; int result ; if ( ! (( hashSize > 0 ) && ( hashSize <= 64 ))){ PyErr_SetString(Blake2HashSizeError, "hashSize error"); Py_INCREF(Py_None); return Py_None; } result = blake2b( outputHash, data, key, hashSize, data_length, key_length ); if ( rawOutput == 1 ){ char HexOutputHash[ hashSize * 2 + 1 ]; for(int i=0; i<hashSize;i++){ int a = ( outputHash[i] >> 4 ) &0x0F; int b = outputHash[i] &0x0F; HexOutputHash[ i * 2 ] = table[a] ; HexOutputHash[ i * 2 + 1 ] = table[b] ; } return Py_BuildValue("s#", HexOutputHash , hashSize * 2 ); }
// TODO: Make group signature indiependent of package load order. const char* package_group_signature(package_group_t* group) { if(group->signature == NULL) { pony_ctx_t ctx; memset(&ctx, 0, sizeof(pony_ctx_t)); ponyint_array_t array; memset(&array, 0, sizeof(ponyint_array_t)); char* buf = (char*)ponyint_pool_alloc_size(SIGNATURE_LENGTH); pony_serialise(&ctx, group, package_group_signature_pony_type(), &array, s_alloc_fn, s_throw_fn); int status = blake2b(buf, SIGNATURE_LENGTH, array.ptr, array.size, NULL, 0); (void)status; pony_assert(status == 0); group->signature = buf; ponyint_pool_free_size(array.size, array.ptr); } return group->signature; }
static int scrypt(void) { int in_fd_flags; unsigned char * const plaintext = secure_bytes, * const ciphertext = secure_bytes + SECURE_SPACE_BLOCK_BYTE_COUNT, * const key = secure_bytes + (2 * SECURE_SPACE_BLOCK_BYTE_COUNT), * const iv = secure_bytes + (2 * SECURE_SPACE_BLOCK_BYTE_COUNT) + (PASSWORD_LENGTH_MAX / 2); if (fcntl(STDIN_FILENO, F_GETFL, &in_fd_flags) == -1) err_exit(msg_in_unreadable); in_fd_flags &= O_NONBLOCK; if (fcntl(STDIN_FILENO, F_SETFL, &in_fd_flags) == -1) err_exit(msg_in_unreadable); blake2b(key, key + PASSWORD_LENGTH_MAX, key + PASSWORD_LENGTH_MAX, PASSWORD_LENGTH_MAX, strlen((const char *)(key + PASSWORD_LENGTH_MAX)), strlen((const char *)(key + PASSWORD_LENGTH_MAX))); memset(key + PASSWORD_LENGTH_MAX, 0, PASSWORD_LENGTH_MAX); memset(secure_bytes, 0, 2 * SECURE_SPACE_BLOCK_BYTE_COUNT); int bytes_read; for (;;) { if (must_terminate()) err_exit(msg_sig); bytes_read = read(STDIN_FILENO, plaintext, SECURE_SPACE_BLOCK_BYTE_COUNT); if (must_terminate()) err_exit(msg_sig); if (bytes_read == -1) err_exit(msg_in_unreadable); if (bytes_read == 0) break; ECRYPT_keysetup(&ecrypt_struct, key, (PASSWORD_LENGTH_MAX / 2) * 8, IV_LENGTH_MAX * 8); ECRYPT_ivsetup(&ecrypt_struct, iv); (operation == ENCRYPT ? ECRYPT_encrypt_bytes : ECRYPT_decrypt_bytes)(&ecrypt_struct, plaintext, ciphertext, bytes_read); memset(plaintext, 0, SECURE_SPACE_BLOCK_BYTE_COUNT); if (write(STDOUT_FILENO, ciphertext, bytes_read) != bytes_read) err_exit(msg_out_unwritable); memset(ciphertext, 0, SECURE_SPACE_BLOCK_BYTE_COUNT); if (must_terminate()) err_exit(msg_sig); if (bytes_read < SECURE_SPACE_BLOCK_BYTE_COUNT) break; } return EXIT_SUCCESS; }
int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen ) { return blake2b( out, in, NULL, BLAKE2B_OUTBYTES, inlen, 0 ); }
gboolean rspamd_map_add (struct rspamd_config *cfg, const gchar *map_line, const gchar *description, map_cb_t read_callback, map_fin_cb_t fin_callback, void **user_data) { struct rspamd_map *new_map; enum fetch_proto proto; const gchar *def, *p, *hostend; struct file_map_data *fdata; struct http_map_data *hdata; gchar portbuf[6], *cksum_encoded, cksum[BLAKE2B_OUTBYTES]; gint i, s, r; struct addrinfo hints, *res; rspamd_mempool_t *pool; /* First of all detect protocol line */ if (!rspamd_map_check_proto (map_line, (int *)&proto, &def)) { return FALSE; } /* Constant pool */ if (cfg->map_pool == NULL) { cfg->map_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "map"); memcpy (cfg->map_pool->tag.uid, cfg->cfg_pool->tag.uid, sizeof (cfg->map_pool->tag.uid)); } new_map = rspamd_mempool_alloc0 (cfg->map_pool, sizeof (struct rspamd_map)); new_map->read_callback = read_callback; new_map->fin_callback = fin_callback; new_map->user_data = user_data; new_map->protocol = proto; new_map->cfg = cfg; new_map->id = g_random_int (); new_map->locked = rspamd_mempool_alloc0_shared (cfg->cfg_pool, sizeof (gint)); if (proto == MAP_PROTO_FILE) { new_map->uri = rspamd_mempool_strdup (cfg->cfg_pool, def); def = new_map->uri; } else { new_map->uri = rspamd_mempool_strdup (cfg->cfg_pool, map_line); } if (description != NULL) { new_map->description = rspamd_mempool_strdup (cfg->cfg_pool, description); } /* Now check for each proto separately */ if (proto == MAP_PROTO_FILE) { fdata = rspamd_mempool_alloc0 (cfg->map_pool, sizeof (struct file_map_data)); if (access (def, R_OK) == -1) { if (errno != ENOENT) { msg_err_config ("cannot open file '%s': %s", def, strerror (errno)); return FALSE; } msg_info_config ( "map '%s' is not found, but it can be loaded automatically later", def); /* We still can add this file */ fdata->st.st_mtime = -1; } else { stat (def, &fdata->st); } fdata->filename = rspamd_mempool_strdup (cfg->map_pool, def); new_map->map_data = fdata; } else if (proto == MAP_PROTO_HTTP) { hdata = rspamd_mempool_alloc0 (cfg->map_pool, sizeof (struct http_map_data)); /* Try to search port */ if ((p = strchr (def, ':')) != NULL) { hostend = p; i = 0; p++; while (g_ascii_isdigit (*p) && i < (gint)sizeof (portbuf) - 1) { portbuf[i++] = *p++; } if (*p != '/') { msg_info_config ("bad http map definition: %s", def); return FALSE; } portbuf[i] = '\0'; hdata->port = atoi (portbuf); } else { /* Default http port */ rspamd_snprintf (portbuf, sizeof (portbuf), "80"); hdata->port = 80; /* Now separate host from path */ if ((p = strchr (def, '/')) == NULL) { msg_info_config ("bad http map definition: %s", def); return FALSE; } hostend = p; } hdata->host = rspamd_mempool_alloc (cfg->map_pool, hostend - def + 1); rspamd_strlcpy (hdata->host, def, hostend - def + 1); hdata->path = rspamd_mempool_strdup (cfg->map_pool, p); /* Now try to resolve */ memset (&hints, 0, sizeof (hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_STREAM; /* Stream socket */ hints.ai_flags = 0; hints.ai_protocol = 0; /* Any protocol */ hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; if ((r = getaddrinfo (hdata->host, portbuf, &hints, &res)) == 0) { hdata->addr = res; rspamd_mempool_add_destructor (cfg->cfg_pool, (rspamd_mempool_destruct_t)freeaddrinfo, hdata->addr); } else { msg_err_config ("address resolution for %s failed: %s", hdata->host, gai_strerror (r)); return FALSE; } /* Now try to connect */ if ((s = rspamd_socket_tcp (hdata->addr, FALSE, FALSE)) == -1) { msg_info_config ("cannot connect to http server %s: %d, %s", hdata->host, errno, strerror (errno)); return FALSE; } close (s); hdata->conn = rspamd_http_connection_new (http_map_read, http_map_error, http_map_finish, RSPAMD_HTTP_BODY_PARTIAL | RSPAMD_HTTP_CLIENT_SIMPLE, RSPAMD_HTTP_CLIENT, NULL); new_map->map_data = hdata; } /* Temp pool */ blake2b (cksum, new_map->uri, NULL, sizeof (cksum), strlen (new_map->uri), 0); cksum_encoded = rspamd_encode_base32 (cksum, sizeof (cksum)); new_map->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "map"); memcpy (new_map->pool->tag.uid, cksum_encoded, sizeof (new_map->pool->tag.uid)); g_free (cksum_encoded); pool = new_map->pool; msg_info_pool ("added map %s", new_map->uri); cfg->maps = g_list_prepend (cfg->maps, new_map); return TRUE; }
int main( void ) { uint8_t key[BLAKE2B_KEYBYTES]; uint8_t buf[BLAKE2_KAT_LENGTH]; size_t i, step; for( i = 0; i < BLAKE2B_KEYBYTES; ++i ) key[i] = ( uint8_t )i; for( i = 0; i < BLAKE2_KAT_LENGTH; ++i ) buf[i] = ( uint8_t )i; /* Test simple API */ for( i = 0; i < BLAKE2_KAT_LENGTH; ++i ) { uint8_t hash[BLAKE2B_OUTBYTES]; blake2b( hash, BLAKE2B_OUTBYTES, buf, i, key, BLAKE2B_KEYBYTES ); if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) ) { goto fail; } } /* Test streaming API */ for(step = 1; step < BLAKE2B_BLOCKBYTES; ++step) { for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) { uint8_t hash[BLAKE2B_OUTBYTES]; blake2b_state S; uint8_t * p = buf; size_t mlen = i; int err = 0; if( (err = blake2b_init_key(&S, BLAKE2B_OUTBYTES, key, BLAKE2B_KEYBYTES)) < 0 ) { goto fail; } while (mlen >= step) { if ( (err = blake2b_update(&S, p, step)) < 0 ) { goto fail; } mlen -= step; p += step; } if ( (err = blake2b_update(&S, p, mlen)) < 0) { goto fail; } if ( (err = blake2b_final(&S, hash, BLAKE2B_OUTBYTES)) < 0) { goto fail; } if (0 != memcmp(hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES)) { goto fail; } } } puts( "ok" ); return 0; fail: puts("error"); return -1; }
int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) { return blake2b(out, outlen, in, inlen, key, keylen); }
bool KeyPair::SignData(Signature& out, const void* data, size_t len) { Digest hash; blake2b(hash, sizeof(hash), 0, 0, data, len); return Sign(out, hash); }
bool KeyPair::VerifyData(const Signature& in, const void* data, size_t len) { Digest hash; blake2b(hash, sizeof(hash), 0, 0, data, len); return Verify(in, hash); }
int Blake512(unsigned char *in, uint64_t inlen, unsigned char *out) { return blake2b(out, in, NULL, BLAKE2B_OUTBYTES, inlen, 0); }
int main(int argc, char *argv[]) { int hashsize = 512; const char *keyfile = NULL; const char *datafile = NULL; if (argc < 3) { printUsage(argv[0]); return -1; } if (argc > 3) { int size = atoi(argv[3]); if (!(size % 8) && size > 0 && size <= 512) { hashsize = size; } } fprintf(stderr, "Using Hash Size %d\n", hashsize); hashsize /= 8; keyfile = argv[1]; datafile = argv[2]; FILE *keyhandle = fopen(keyfile, "rb"); if (keyhandle == NULL) { perror("Couldn't open key file"); return -1; } fseek(keyhandle, 0, SEEK_END); off_t keylen = ftell(keyhandle); FILE *datahandle = fopen(datafile, "rb"); if (datahandle == NULL) { perror("Couldn't open data file"); fclose(keyhandle); return -1; } fseek(datahandle, 0, SEEK_END); off_t datalen = ftell(datahandle); rewind(keyhandle); uint8_t *key = (uint8_t *)malloc(keylen); int res = fread(key, keylen, 1, keyhandle); if (res < 1) { perror("Couldn't read file"); fclose(keyhandle); fclose(datahandle); free(key); return -1; } rewind(datahandle); uint8_t *data = (uint8_t *)malloc(datalen); res = fread(data, datalen, 1, datahandle); if (res < 1) { perror("Couldn't read file"); fclose(keyhandle); fclose(datahandle); free(key); free(data); return -1; } fclose(keyhandle); fclose(datahandle); uint8_t *hash = (uint8_t *)malloc(hashsize); fprintf(stderr, "Key size: %ld, Data size: %ld\n", keylen, datalen); int result = blake2b(hash, data, key, hashsize, datalen, keylen); free(data); free(key); fwrite(hash, hashsize, 1, stdout); free(hash); return 0; }