uint32_t BinarySerializerBase::ComputeChecksum() { if(m_bWithMetadata) return compute_checksum( m_data + ms_headerSize, m_size-ms_headerSize); else return compute_checksum( m_data, m_size ); }
static void checksum_test1(void) { RaucChecksum checksum = {}; GError *error = NULL; checksum.type = 0; checksum.digest = NULL; g_assert_false(verify_checksum(&checksum, "test/install-content/appfs.img", &error)); g_assert_error(error, R_CHECKSUM_ERROR, R_CHECKSUM_ERROR_FAILED); g_clear_error(&error); checksum.type = G_CHECKSUM_SHA256; checksum.digest = g_strdup(TEST_DIGEST_FAIL); g_assert_false(verify_checksum(&checksum, "test/install-content/appfs.img", &error)); g_assert_error(error, R_CHECKSUM_ERROR, R_CHECKSUM_ERROR_SIZE_MISMATCH); g_clear_error(&error); checksum.size = 32768; g_assert_false(verify_checksum(&checksum, "test/install-content/appfs.img", &error)); g_assert_error(error, R_CHECKSUM_ERROR, R_CHECKSUM_ERROR_DIGEST_MISMATCH); g_clear_error(&error); checksum.size = 0; checksum.digest = g_strdup(TEST_DIGEST_GOOD); g_assert_false(verify_checksum(&checksum, "test/install-content/appfs.img", &error)); g_assert_error(error, R_CHECKSUM_ERROR, R_CHECKSUM_ERROR_SIZE_MISMATCH); g_clear_error(&error); checksum.size = 32768; g_assert_true(verify_checksum(&checksum, "test/install-content/appfs.img", &error)); g_assert_no_error(error); g_assert_false(verify_checksum(&checksum, "tesinstall-content/rootfs.img", &error)); g_assert_error(error, G_FILE_ERROR, G_FILE_ERROR_NOENT); g_clear_error(&error); g_assert_false(verify_checksum(&checksum, "test/_MISSING_", &error)); g_assert_error(error, G_FILE_ERROR, G_FILE_ERROR_NOENT); g_clear_error(&error); g_clear_pointer(&checksum.digest, g_free); checksum.size = 0; g_assert_true(compute_checksum(&checksum, "test/install-content/appfs.img", &error)); g_assert_no_error(error); g_assert_cmpstr(checksum.digest, ==, TEST_DIGEST_GOOD); g_assert(checksum.size == 32768); g_clear_pointer(&checksum.digest, g_free); checksum.size = 0; g_assert_false(compute_checksum(&checksum, "tesinstall-content/rootfs.img", &error)); g_assert_error(error, G_FILE_ERROR, G_FILE_ERROR_NOENT); g_clear_error(&error); g_assert_null(checksum.digest); g_assert(checksum.size == 0); }
void WhitelistSerializeTypeface(const SkTypeface* tf, SkWStream* wstream) { if (!is_local(tf)) { serialize_name_only(tf, wstream); return; } int whitelistIndex = whitelist_name_index(tf); if (whitelistIndex < 0) { serialize_full(tf, wstream); return; } const char* fontName = whitelist[whitelistIndex].fFontName; if (!font_name_is_local(fontName, tf->style())) { #if WHITELIST_DEBUG SkDebugf("name not found locally \"%s\" style=%d\n", fontName, tf->style()); #endif serialize_full(tf, wstream); return; } uint32_t checksum = compute_checksum(tf); if (whitelist[whitelistIndex].fChecksum != checksum) { #if WHITELIST_DEBUG if (whitelist[whitelistIndex].fChecksum) { SkDebugf("!!! checksum changed !!!\n"); } SkDebugf("checksum updated\n"); SkDebugf(" { \"%s\", 0x%08x },\n", fontName, checksum); #endif whitelist[whitelistIndex].fChecksum = checksum; } serialize_sub(fontName, tf->style(), wstream); }
uint16_t TcpLayer::calculateChecksum(bool writeResultToPacket) { tcphdr* tcpHdr = getTcpHeader(); uint16_t checksumRes = 0; uint16_t currChecksumValue = tcpHdr->headerChecksum; if (m_PrevLayer != NULL) { tcpHdr->headerChecksum = 0; ScalarBuffer<uint16_t> vec[2]; LOG_DEBUG("data len = %d", (int)m_DataLen); vec[0].buffer = (uint16_t*)m_Data; vec[0].len = m_DataLen; if (m_PrevLayer->getProtocol() == IPv4) { uint32_t srcIP = ((IPv4Layer*)m_PrevLayer)->getSrcIpAddress().toInt(); uint32_t dstIP = ((IPv4Layer*)m_PrevLayer)->getDstIpAddress().toInt(); uint16_t pseudoHeader[6]; pseudoHeader[0] = srcIP >> 16; pseudoHeader[1] = srcIP & 0xFFFF; pseudoHeader[2] = dstIP >> 16; pseudoHeader[3] = dstIP & 0xFFFF; pseudoHeader[4] = 0xffff & htons(m_DataLen); pseudoHeader[5] = htons(0x00ff & PACKETPP_IPPROTO_TCP); vec[1].buffer = pseudoHeader; vec[1].len = 12; checksumRes = compute_checksum(vec, 2); LOG_DEBUG("calculated checksum = 0x%4X", checksumRes); }
void IPv4Layer::computeCalculateFields() { iphdr* ipHdr = getIPv4Header(); ipHdr->internetHeaderLength = (5 & 0xf); ipHdr->ipVersion = (4 & 0x0f); ipHdr->totalLength = htons(m_DataLen); ipHdr->headerChecksum = 0; if (m_NextLayer != NULL) { switch (m_NextLayer->getProtocol()) { case TCP: ipHdr->protocol = PACKETPP_IPPROTO_TCP; break; case UDP: ipHdr->protocol = PACKETPP_IPPROTO_UDP; break; case ICMP: ipHdr->protocol = PACKETPP_IPPROTO_ICMP; break; default: break; } } ScalarBuffer scalar = { (uint16_t*)ipHdr, ipHdr->internetHeaderLength*4 } ; ipHdr->headerChecksum = htons(compute_checksum(&scalar, 1)); }
int pack_data(int seq_num, int type, int flags, unsigned char *buf, int size) { uint16_t csum; if(!buf) { return 0; } buf[0] = (seq_num & 0xff000000)>>24; buf[1] = (seq_num & 0x00ff0000)>>16; buf[2] = (seq_num & 0x0000ff00)>>8; buf[3] = (seq_num & 0x000000ff); buf[4] = 0; // zero it for checksum computation buf[5] = 0; // zero it for checksum computation buf[6] = type; buf[7] = flags; compute_checksum(&csum, buf, size); //printf("computed : %x\n", csum); buf[4] = (csum & 0xff00)>>8; buf[5] = (csum & 0x00ff); //printf("header\n|%02x%02x %02x %02x|\n|%02x%02x|%02x|%02x|\n", // buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]); return 1; }
int unpack_data(int *seq_num, int *type, int *flags, unsigned char *buf, int size) { uint16_t msg_csum=0, comp_csum=0; if(!buf) { return 0; } *seq_num = 0; *seq_num |= buf[0]<<24; *seq_num |= buf[1]<<16; *seq_num |= buf[2]<<8; *seq_num |= buf[3]; *type = buf[6]; *flags = buf[7]; msg_csum |= buf[4]<<8; msg_csum |= buf[5]; compute_checksum(&comp_csum, buf, size); //printf("computed : %x, from_data : %x\n", comp_csum, msg_csum); //printf("header\n|%02x%02x %02x %02x|\n|%02x%02x|%02x|%02x|\n", // buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]); if(comp_csum) { // computed checksum should be 0. As we do another ~ in the compute_checksum function return -1; } return 1; }
void start_send(const int iPingTimeoutms) { std::string body("Ping from Domoticz."); // Create an ICMP header for an echo request. icmp_header echo_request; echo_request.type(icmp_header::echo_request); echo_request.code(0); echo_request.identifier(get_identifier()); echo_request.sequence_number(++sequence_number_); compute_checksum(echo_request, body.begin(), body.end()); // Encode the request packet. boost::asio::streambuf request_buffer; std::ostream os(&request_buffer); os << echo_request << body; // Send the request. time_sent_ = boost::posix_time::microsec_clock::universal_time(); socket_.send_to(request_buffer.data(), destination_); num_replies_ = 0; timer_.expires_at(time_sent_ + boost::posix_time::milliseconds(iPingTimeoutms)); timer_.async_wait(boost::bind(&pinger::handle_timeout, this, boost::asio::placeholders::error)); }
BOOLEANC dict_save_block( DICTIONARY *dict , char *toc_id , FILE *fo ) { DICT_TOC_ENTRY *dt_entry; int index, ret_code; char *block; index = dict_toc_index( dict , toc_id ); if ( index == -1 ) { signal_error( "dict_save_block: id not found " , toc_id , 1 ); return( FALSE ); } /* endif */ dt_entry = &(dict->toc[index]); block = (char*)(dt_entry->ptr); if ( block == NULL ) { signal_error( "dict_save_block: NULL block " , toc_id , 1 ); return( FALSE ); } /* endif */ /* dt_entry->offset = fseek( fo , 0 , SEEK_END ); */ dt_entry->checksum = compute_checksum( dt_entry->size , block ); ret_code = block_write( fo , dt_entry->ptr , dt_entry->size ); if ( ret_code == -1 ) { signal_error( "dict_save_block: block_write failed " , toc_id , 1 ); return( FALSE ); } /* endif */ if ( trace > 3 ) { fprintf( ft , "\nStored block\nTOC entry: id:%s offset:%lx size:%lx ptr:%p checksum:%lx type:%d\n" , dict->toc[index].id , dict->toc[index].offset , dict->toc[index].size , dict->toc[index].ptr , dict->toc[index].checksum , dict->toc[index].type ); } /* endif */ return( TRUE ); }
void BinaryDeserializer::Data(String const& str) { if(m_data != NULL) { delete[] m_data; } m_bIsValid = true; if(m_bWithMetadata) { m_data = new char[ms_headerSize]; m_pos = 0; memcpy(m_data, str.c_str(), ms_headerSize); serialize(m_size, sizeof(int32_t), "", false); serialize(m_version, sizeof(int32_t), "", false); serialize(m_checksum, sizeof(int32_t), "", false); delete[] m_data; m_pos = ms_headerSize; m_data = new char[m_size]; memcpy(m_data, str.c_str(), m_size); unsigned int computed_checksum = compute_checksum(m_data + ms_headerSize, m_size-ms_headerSize); if(computed_checksum != m_checksum) { m_bIsValid = false; } } else { m_size = (uint32_t)str.length(); m_pos = 0; m_data = new char[m_size]; memcpy(m_data, str.c_str(), m_size); } m_bInitialized = true; }
static void send_message(char *message, int len) { if (len == 0) { len = strlen(message); } unsigned char checksum = compute_checksum(message, len); gdb_printf(GDB_RESPONSE_START "$%s#%02X", message, checksum); gdb_printf(GDB_RESPONSE_END); }
String BinarySerializerBase::Str() { if(m_checksum_stale && m_bWithMetadata) { m_checksum = compute_checksum( m_data + ms_headerSize, m_size-ms_headerSize); memcpy( m_data + sizeof(int32_t)*2, &m_checksum, sizeof(int32_t) ); m_checksum_stale = false; } return sprawl::String(m_data, m_size); }
const char*BinarySerializerBase::Data() { if(m_checksum_stale && m_bWithMetadata) { m_checksum = compute_checksum( m_data + ms_headerSize, m_size-ms_headerSize); memcpy( m_data + sizeof(int32_t)*2, &m_checksum, sizeof(int32_t) ); m_checksum_stale = false; } return m_data; }
// Debugging for now, wouldnt work normally as text could be there for other reasons static void validate_is_slab(struct slab_header *slab) { if (strcmp(slab->signature, "MALLOC")) { koops("slab @ %p is not a slab!", slab); } if (compute_checksum(slab) != slab->checksum) { koops("slab @ %p has invalid checksum!", slab); } }
void *dict_load_block( DICTIONARY *dict , char *toc_id , FILE *fi , void *block ) { DICT_TOC_ENTRY *dt_entry; static void *ptr; int index, ret_code; index = dict_toc_index( dict , toc_id ); if ( index != -1 ) { /* Found the id */ dt_entry = &(dict->toc[index]); } else { signal_error( "dict_load_block: could not find TOC_id" , toc_id , 1 ); return( NULL ); } /* endif */ if ( block == NULL ) { ptr = malloc( dt_entry->size ); if ( trace > 3 ) { fprintf( ft , "\ndict_load_block allocates %lx bytes at location %p\n" , dt_entry->size , ptr ); } /* endif */ } else { ptr = block; if ( trace > 3 ) { fprintf( ft , "\ndict_load_block uses memory at location %p\n" , ptr ); } /* endif */ } /* endif */ if ( ptr == NULL ) { signal_error( "dict_load_block: alloc failed " , toc_id , 1 ); return( NULL ); } /* endif */ ret_code = block_read( fi , (char*)ptr , dt_entry->size , dt_entry->offset ); if ( ret_code == -1 ) return( NULL ); if ( dt_entry->checksum != compute_checksum( dt_entry->size , (char*)ptr ) ) { signal_error( "dict_load_block: invalid checksum ", toc_id, 1); return( NULL ); } /* endif */ dt_entry->ptr = ptr; if ( trace > 3 ) { fprintf( ft , "\nLoaded block\nTOC entry: id:%s offset:%lx size:%lx ptr:%p checksum:%lx type:%d\n" , dict->toc[index].id , dict->toc[index].offset , dict->toc[index].size , dict->toc[index].ptr , dict->toc[index].checksum , dict->toc[index].type ); } /* endif */ return( ptr ); }
bool CheckChecksums() { for (int i = 0; i < whitelistCount; ++i) { const char* fontName = whitelist[i].fFontName; sk_sp<SkTypeface> tf(SkTypeface::MakeFromName(fontName, SkTypeface::kNormal)); uint32_t checksum = compute_checksum(tf.get()); if (whitelist[i].fChecksum != checksum) { return false; } } return true; }
int main(int argc, char* argv[]) { int rv; if (argc == 1) { printf("Usage: %s <filename>\n", argv[0]); rv = 1; } else { rv = compute_checksum(argv[1]); } return 0; }
bool CheckChecksums() { for (int i = 0; i < whitelistCount; ++i) { const char* fontName = whitelist[i].fFontName; SkTypeface* tf = SkTypeface::CreateFromName(fontName, SkTypeface::kNormal); uint32_t checksum = compute_checksum(tf); if (whitelist[i].fChecksum != checksum) { return false; } } return true; }
static Code_t ZCheckAuthentication4(ZNotice_t *notice, struct sockaddr_in *from) { int result; char srcprincipal[ANAME_SZ+INST_SZ+REALM_SZ+4]; KTEXT_ST authent; AUTH_DAT dat; ZChecksum_t checksum; char instance[INST_SZ+1]; if (!notice->z_auth) return ZAUTH_NO; /* Check for bogus authentication data length. */ if (notice->z_authent_len <= 0) return ZAUTH_FAILED; /* Read in the authentication data. */ if (ZReadAscii(notice->z_ascii_authent, strlen(notice->z_ascii_authent)+1, (unsigned char *)authent.dat, notice->z_authent_len) == ZERR_BADFIELD) { return ZAUTH_FAILED; } authent.length = notice->z_authent_len; strcpy(instance, SERVER_INSTANCE); /* We don't have the session key cached; do it the long way. */ result = krb_rd_req(&authent, SERVER_SERVICE, instance, from->sin_addr.s_addr, &dat, srvtab_file); if (result == RD_AP_OK) { ZSetSessionDES(&dat.session); sprintf(srcprincipal, "%s%s%s@%s", dat.pname, dat.pinst[0] ? "." : "", dat.pinst, dat.prealm); if (strcmp(srcprincipal, notice->z_sender)) return ZAUTH_FAILED; } else { return ZAUTH_FAILED; /* didn't decode correctly */ } /* Check the cryptographic checksum. */ checksum = compute_checksum(notice, dat.session); if (checksum != notice->z_checksum) return ZAUTH_FAILED; return ZAUTH_YES; }
BOOLEANC dict_reset_toc_offsets( DICTIONARY *dict ) { int i; long offset; offset = sizeof(DICT_SIG) + dict->sig->toc_size * sizeof(DICT_TOC_ENTRY); for ( i = 0 ; i < dict->sig->toc_size ; i++ ) { dict->toc[i].offset = offset; offset += dict->toc[i].size; dict->toc[i].checksum = compute_checksum( dict->toc[i].size , dict->toc[i].ptr ); } /* endfor */ return( TRUE ); }
static void g_keyfile_settings_backend_keyfile_write (GKeyfileSettingsBackend *kfsb) { gchar *contents; gsize length; contents = g_key_file_to_data (kfsb->keyfile, &length, NULL); g_file_replace_contents (kfsb->file, contents, length, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION | G_FILE_CREATE_PRIVATE, NULL, NULL, NULL); compute_checksum (kfsb->digest, contents, length); g_free (contents); }
/* This called for TP and non-TP, but not for ZTP */ void jnl_write_logical(sgmnt_addrs *csa, jnl_format_buffer *jfb, uint4 com_csum, jnlpool_write_ctx_t *jplctx) { struct_jrec_upd *jrec; struct_jrec_null *jrec_null; struct_jrec_upd *jrec_alt; jnl_private_control *jpc; /* If REPL_WAS_ENABLED(csa) is TRUE, then we would not have gone through the code that initializes * jgbl.gbl_jrec_time or jpc->pini_addr. But in this case, we are not writing the journal record * to the journal buffer or journal file but write it only to the journal pool from where it gets * sent across to the update process that does not care about these fields so it is ok to leave them as is. */ jpc = csa->jnl; assert((0 != jpc->pini_addr) || REPL_WAS_ENABLED(csa)); assert(jgbl.gbl_jrec_time || REPL_WAS_ENABLED(csa)); assert(csa->now_crit); assert(IS_SET_KILL_ZKILL_ZTWORM_LGTRIG_ZTRIG(jfb->rectype) || (JRT_NULL == jfb->rectype)); assert(!IS_ZTP(jfb->rectype)); jrec = (struct_jrec_upd *)jfb->buff; assert(OFFSETOF(struct_jrec_null, prefix) == OFFSETOF(struct_jrec_upd, prefix)); assert(SIZEOF(jrec_null->prefix) == SIZEOF(jrec->prefix)); jrec->prefix.pini_addr = (0 == jpc->pini_addr) ? JNL_HDR_LEN : jpc->pini_addr; jrec->prefix.tn = csa->ti->curr_tn; jrec->prefix.time = jgbl.gbl_jrec_time; /* t_end/tp_tend/mur_output_record has already set token/jnl_seqno into jnl_fence_ctl.token */ assert((0 != jnl_fence_ctl.token) || (!dollar_tlevel && !jgbl.forw_phase_recovery && !REPL_ENABLED(csa)) || (!dollar_tlevel && jgbl.forw_phase_recovery && (repl_open != csa->hdr->intrpt_recov_repl_state))); assert(OFFSETOF(struct_jrec_null, jnl_seqno) == OFFSETOF(struct_jrec_upd, token_seq)); assert(SIZEOF(jrec_null->jnl_seqno) == SIZEOF(jrec->token_seq)); jrec->token_seq.token = jnl_fence_ctl.token; assert(OFFSETOF(struct_jrec_null, strm_seqno) == OFFSETOF(struct_jrec_upd, strm_seqno)); assert(SIZEOF(jrec_null->strm_seqno) == SIZEOF(jrec->strm_seqno)); jrec->strm_seqno = jnl_fence_ctl.strm_seqno; /* update checksum below */ if(JRT_NULL != jrec->prefix.jrec_type) { COMPUTE_LOGICAL_REC_CHECKSUM(jfb->checksum, jrec, com_csum, jrec->prefix.checksum); } else jrec->prefix.checksum = compute_checksum(INIT_CHECKSUM_SEED, (unsigned char *)jrec, SIZEOF(struct_jrec_null)); if (REPL_ALLOWED(csa) && USES_ANY_KEY(csa->hdr)) { jrec_alt = (struct_jrec_upd *)jfb->alt_buff; jrec_alt->prefix = jrec->prefix; jrec_alt->token_seq = jrec->token_seq; jrec_alt->strm_seqno = jrec->strm_seqno; jrec_alt->num_participants = jrec->num_participants; } JNL_WRITE_APPROPRIATE(csa, jpc, jfb->rectype, (jnl_record *)jrec, NULL, jfb, jplctx); }
update_checksum_unless_its_already_there(short int *waveform, header_t *header) { int field_len; int checksum; int wav_len; field_len = sizeof(int); if(sp_get_data(header,"sample_checksum",(char *)&checksum,&field_len) < 0) { field_len = sizeof(int); if(sp_get_data(header,"sample_count",(char *)&wav_len,&field_len) < 0) { fprintf(stderr,"HEY! couldn't read sample count from header!!\n"); longjmp(exitenv,-1); /* exit(-1); */ } checksum = compute_checksum(waveform, wav_len); sp_add_field(header,"sample_checksum",T_INTEGER,(void *)&checksum); } }
void GREv0Layer::computeCalculateFields() { computeCalculateFieldsInner(); if (getGreHeader()->checksumBit == 0) return; // calculate checksum setChecksum(0); ScalarBuffer<uint16_t> buffer; buffer.buffer = (uint16_t*)m_Data; buffer.len = m_DataLen; size_t checksum = compute_checksum(&buffer, 1); setChecksum(checksum); }
/** * Process result to extract the secret bytes * * @param send_buf Content buffer */ void process_result(char *send_buf) { #ifdef DEBUG fprintf(stderr, "PROCESS RESULT..."); #endif unsigned short *s = (unsigned short *)send_buf; char cs[4] = {0}; struct result r; // cgc_read result #ifdef DEBUG fprintf(stderr, "RESULTS..."); #endif if (sizeof(r) != recv_bytes(STDIN, (char *)&r, sizeof(r))) { _terminate(10); } #ifdef DEBUG fprintf(stderr, "recvd\n"); #endif // compute checksum on send_buf compute_checksum(send_buf, cs); // compute and save secret bytes secret[*s + 9] = r.hash0 ^ cs[0]; found[*s + 9] = 1; secret[*s + 7] = r.hash1 ^ cs[1]; found[*s + 7] = 1; secret[*s + 3] = r.hash2 ^ cs[2]; found[*s + 3] = 1; secret[*s + 0] = r.hash3 ^ cs[3]; found[*s + 0] = 1; #ifdef DEBUG char *s_ptr = secret; fprintf(stderr, "SECRET: "); for (unsigned int i = 0; i < 10; i++) { fprintf(stderr, "%02x", (unsigned char) *s_ptr++); } fprintf(stderr, "\n"); #endif }
static void g_keyfile_settings_backend_keyfile_reload (GKeyfileSettingsBackend *kfsb) { guint8 digest[32]; gchar *contents; gsize length; contents = NULL; length = 0; g_file_load_contents (kfsb->file, NULL, &contents, &length, NULL, NULL); compute_checksum (digest, contents, length); if (memcmp (kfsb->digest, digest, sizeof digest) != 0) { GKeyFile *keyfiles[2]; GTree *tree; tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL, g_free, g_free); keyfiles[0] = kfsb->keyfile; keyfiles[1] = g_key_file_new (); if (length > 0) g_key_file_load_from_data (keyfiles[1], contents, length, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL); keyfile_to_tree (kfsb, tree, keyfiles[0], FALSE); keyfile_to_tree (kfsb, tree, keyfiles[1], TRUE); g_key_file_free (keyfiles[0]); kfsb->keyfile = keyfiles[1]; if (g_tree_nnodes (tree) > 0) g_settings_backend_changed_tree (&kfsb->parent_instance, tree, NULL); g_tree_unref (tree); memcpy (kfsb->digest, digest, sizeof digest); } g_free (contents); }
ProtocolError DTLSMessageChannel::init( const uint8_t* core_private, size_t core_private_len, const uint8_t* core_public, size_t core_public_len, const uint8_t* server_public, size_t server_public_len, const uint8_t* device_id, Callbacks& callbacks, message_id_t* coap_state) { init(); this->coap_state = coap_state; int ret; this->callbacks = callbacks; this->device_id = device_id; keys_checksum = compute_checksum(callbacks.calculate_crc, server_public, server_public_len, core_private, core_private_len); ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_PRESET_DEFAULT); EXIT_ERROR(ret, "unable to configure defaults"); mbedtls_ssl_conf_handshake_timeout(&conf, 3000, 6000); mbedtls_ssl_conf_rng(&conf, dtls_rng, this); mbedtls_ssl_conf_dbg(&conf, my_debug, nullptr); mbedtls_ssl_conf_min_version(&conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); ret = mbedtls_pk_parse_public_key(&pkey, core_public, core_public_len); EXIT_ERROR(ret, "unable to parse device public key"); ret = mbedtls_pk_parse_key(&pkey, core_private, core_private_len, NULL, 0); EXIT_ERROR(ret, "unable to parse device private key"); ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey); EXIT_ERROR(ret, "unable to configure own certificate"); mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL); static int ssl_cert_types[] = { MBEDTLS_TLS_CERT_TYPE_RAW_PUBLIC_KEY, MBEDTLS_TLS_CERT_TYPE_NONE }; mbedtls_ssl_conf_client_certificate_types(&conf, ssl_cert_types); mbedtls_ssl_conf_server_certificate_types(&conf, ssl_cert_types); mbedtls_ssl_conf_certificate_receive(&conf, MBEDTLS_SSL_RECEIVE_CERTIFICATE_DISABLED); this->server_public = new uint8_t[server_public_len]; memcpy(this->server_public, server_public, server_public_len); this->server_public_len = server_public_len; return NO_ERROR; }
static int32 checksum_data(net_buffer *_buffer, uint32 offset, size_t size, bool finalize) { net_buffer_private *buffer = (net_buffer_private *)_buffer; if (offset + size > buffer->size || size == 0) return B_BAD_VALUE; uint16 sum = compute_checksum(buffer->data + offset, size); if ((offset & 1) != 0) { // if we're at an uneven offset, we have to swap the checksum sum = __swap_int16(sum); } if (!finalize) return (uint16)sum; return (uint16)~sum; }
int get_part(int client, char *text, int *index, int part_size) { int data_size = sizeof(struct message) - 1 + part_size; struct message *data = malloc(data_size); unsigned int local_checksum; ssize_t read_length = read(client, data, data_size); if (read_length < 0) read_length = -errno; // XXX LOG(DEBUG, "Read value: %i", read_length); *index = data->index; // TODO Check if number from range int size = data->size; char *text_part = data->text; convert(text_part); // Changes structure's field xP LOG(DEBUG, "Got part (%3u): '%s' (%u) #%u", index+1, text_part, size, data->checksum); // Make sure there is no garbage left //if (size < part_size) // text_part[size + 1] = 0; local_checksum = compute_checksum(text_part); if (data->checksum == local_checksum) { // Message delivered correctly; clear retransmit bit //retransmit[index >> 3] &= ~((index & 7) << 1); //++sent; strncpy(&text[*index * part_size], text_part, size); } else { LOG(ERROR, "Wrong part's checksum! Got %u, should be %u", data->checksum, local_checksum); } free(data); return read_length; }
void IcmpLayer::computeCalculateFields() { // calculate checksum getIcmpHeader()->checksum = 0; size_t icmpLen = 0; Layer* curLayer = this; while (curLayer != NULL) { icmpLen += curLayer->getHeaderLen(); curLayer = curLayer->getNextLayer(); } ScalarBuffer<uint16_t> buffer; buffer.buffer = (uint16_t*)getIcmpHeader(); buffer.len = icmpLen; size_t checksum = compute_checksum(&buffer, 1); getIcmpHeader()->checksum = htons(checksum); }