/* ============================== Process Associated Data =================================*/ static void process_AD( block W, block Delta_1, const block npub, block param, const u8 *ad, u64 adlen) { block Delta_2, blk, result; u8 Is_complete = 1, ozs[16]; int i; for(i=1; i<16; i++){ozs[i]=0x00;} ozs[0] = 0x80; /* ===== make the first block blk based on npub and param ===== */ load_block(blk, npub, param, 8, 8); while(1){ /* ============= Process the current Block ==================== */ process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, 0, AD); /* === Compute the next Block and updating the pointers and counters ===*/ if(adlen==0) break; else if(adlen <= 16) { load_block(blk, ad, ozs, adlen, 16-adlen); if(adlen != 16) Is_complete = 0; adlen = 0; } else {load_block(blk, ad, ozs, 16, 0); ad +=16; adlen -=16;} } }
/** * Try to load a savefile */ static bool try_load(ang_file *f, const struct blockinfo *loaders) { struct blockheader b; errr err; if (!check_header(f)) { note("Savefile is corrupted -- incorrect file header."); return FALSE; } /* Get the next block header */ while ((err = next_blockheader(f, &b)) == 0) { loader_t loader = find_loader(&b, loaders); if (!loader) { note("Savefile block can't be read."); note("Maybe try and load the savefile in an earlier version of Angband."); return FALSE; } if (!load_block(f, &b, loader)) { note(format("Savefile corrupted - Couldn't load block %s", b.name)); return FALSE; } } if (err == -1) { note("Savefile is corrupted -- block header mangled."); return FALSE; } return TRUE; }
/* Try to load a savefile */ static bool try_load(ang_file *f, const struct blockinfo *loaders) { struct blockheader b; errr err; if (!check_header(f)) { note("Savefile is corrupted -- incorrect file header."); return FALSE; } /* Get the next block header */ while ((err = next_blockheader(f, &b)) == 0) { loader_t loader = find_loader(&b, loaders); if (!loader) { note("Savefile block can't be read."); note("Maybe try and load the savefile in an earlier version of Angband."); return FALSE; } if (!load_block(f, &b, loader)) { note(format("Savefile corrupted - Couldn't load block %s", b.name)); return FALSE; } } if (err == -1) { note("Savefile is corrupted -- block header mangled."); return FALSE; } /* XXX Reset cause of death */ if (p_ptr->chp >= 0) my_strcpy(p_ptr->died_from, "(alive and well)", sizeof(p_ptr->died_from)); return TRUE; }
//helper function void SpinBlock::Load (std::ifstream & ifs) { dmrginp.diskio->start(); boost::archive::binary_iarchive load_block(ifs); load_block >> *this; dmrginp.diskio->stop(); }
/** * Try to get the 'description' block from a savefile. Fail gracefully. */ const char *savefile_get_description(const char *path) { struct blockheader b; ang_file *f = file_open(path, MODE_READ, FTYPE_TEXT); if (!f) return NULL; /* Blank the description */ savefile_desc[0] = 0; if (!check_header(f)) { my_strcpy(savefile_desc, "Invalid savefile", sizeof savefile_desc); } else { while (!next_blockheader(f, &b)) { if (!streq(b.name, "description")) { skip_block(f, &b); continue; } load_block(f, &b, get_desc); break; } } file_close(f); return savefile_desc; }
/** * Retreives the value of the given element index. * For read only. */ const T read(size_t i) { const size_t b_index = i / BLOCK_SIZE; i %= BLOCK_SIZE; // Increment block priority if it's already loaded, // load it if not. if (data_table[b_index]) data_table[b_index]->priority = priot(); else load_block(b_index); return cache[data_table[b_index]->c_index+i]; }
/** * Loads a chunk of vertex values * @param vertex_st first vertex id * @param vertex_en last vertex id, inclusive */ virtual void load(vid_t _vertex_st, vid_t _vertex_en) { assert(_vertex_en >= _vertex_st); vertex_st = _vertex_st; vertex_en = _vertex_en; releaseblocks(); int min_blockid = vertex_st / verticesperblock; int max_blockid = vertex_en / verticesperblock; for(int i=min_blockid; i <= max_blockid; i++) { loadedblocks.push_back(load_block(i)); } }
int dynamic_load(dfile *df,int key,char *data) { int block,length,l; /* printf("Dynamic load key=%d\n",key); */ if (!convert_key(df,key,&block,&length)) return -1; l=length; while(l>0) { /* printf("DL block %d, Length = %d\n",block,l); */ if (!load_block(df,&block,data)) handle_error("Failed to load block of data"); l-=(df->granularity-4); data+=(df->granularity-4); } return length; }
static void drop_cb(struct game_state *gs) { if (collision(gs, 0, 1)) { int old = value(gs); put_shape(gs, 1); fix_grid(gs); fprintf(gs->log, "%d %d\n", old, gs->lines); printf("%d\n", value(gs) - old); load_block(gs); } else { put_shape(gs, 0); gs->y += 1; put_shape(gs, 4); } }
void init_game_state(struct game_state *gs) { gs->x = G_WIDTH / 2; gs->lines = 0; gs->rot = 0; int i,j; for (i = 0; i < G_WIDTH; i++) { for (j = 0; j < G_HEIGHT; j++) { gs->grid[i][j] = 0; } } load_block(gs); gs->lastdrop = clock(); }
void reload_config (void) { log_message ("reload_config: reloading configuration files"); config (0); /* since the motd is stored in memory, reread it */ motd_close (); motd_init (); #ifndef ROUTING_ONLY /* reread filter file */ load_filter (); load_block (); #endif load_server_auth (); /* since the servers file may have changed, ensure that there is * no nickname that matches an alias for a server. */ list_foreach (Server_Auth, (list_callback_t) nick_check, 0); }
void AES_ige_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, const int enc) { size_t n; size_t len = length; OPENSSL_assert(in && out && key && ivec); OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc)); OPENSSL_assert((length % AES_BLOCK_SIZE) == 0); len = length / AES_BLOCK_SIZE; if (AES_ENCRYPT == enc) { if (in != out && (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) == 0)) { aes_block_t *ivp = (aes_block_t *) ivec; aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE); while (len) { aes_block_t *inp = (aes_block_t *) in; aes_block_t *outp = (aes_block_t *) out; for (n = 0; n < N_WORDS; ++n) outp->data[n] = inp->data[n] ^ ivp->data[n]; AES_encrypt((unsigned char *)outp->data, (unsigned char *)outp->data, key); for (n = 0; n < N_WORDS; ++n) outp->data[n] ^= iv2p->data[n]; ivp = outp; iv2p = inp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } sgx_memcpy(ivec, ivp->data, AES_BLOCK_SIZE); sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE); } else { aes_block_t tmp, tmp2; aes_block_t iv; aes_block_t iv2; load_block(iv, ivec); load_block(iv2, ivec + AES_BLOCK_SIZE); while (len) { load_block(tmp, in); for (n = 0; n < N_WORDS; ++n) tmp2.data[n] = tmp.data[n] ^ iv.data[n]; AES_encrypt((unsigned char *)tmp2.data, (unsigned char *)tmp2.data, key); for (n = 0; n < N_WORDS; ++n) tmp2.data[n] ^= iv2.data[n]; store_block(out, tmp2); iv = tmp2; iv2 = tmp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } sgx_memcpy(ivec, iv.data, AES_BLOCK_SIZE); sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE); } } else { if (in != out && (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) == 0)) { aes_block_t *ivp = (aes_block_t *) ivec; aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE); while (len) { aes_block_t tmp; aes_block_t *inp = (aes_block_t *) in; aes_block_t *outp = (aes_block_t *) out; for (n = 0; n < N_WORDS; ++n) tmp.data[n] = inp->data[n] ^ iv2p->data[n]; AES_decrypt((unsigned char *)tmp.data, (unsigned char *)outp->data, key); for (n = 0; n < N_WORDS; ++n) outp->data[n] ^= ivp->data[n]; ivp = inp; iv2p = outp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } sgx_memcpy(ivec, ivp->data, AES_BLOCK_SIZE); sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE); } else { aes_block_t tmp, tmp2; aes_block_t iv; aes_block_t iv2; load_block(iv, ivec); load_block(iv2, ivec + AES_BLOCK_SIZE); while (len) { load_block(tmp, in); tmp2 = tmp; for (n = 0; n < N_WORDS; ++n) tmp.data[n] ^= iv2.data[n]; AES_decrypt((unsigned char *)tmp.data, (unsigned char *)tmp.data, key); for (n = 0; n < N_WORDS; ++n) tmp.data[n] ^= iv.data[n]; store_block(out, tmp); iv = tmp2; iv2 = tmp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } sgx_memcpy(ivec, iv.data, AES_BLOCK_SIZE); sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE); } } }
static void TftpSend(void) { uchar *pkt; uchar *xp; int len = 0; ushort *s; #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP.. non-MasterClients do not ACK data. */ if (Multicast && (TftpState == STATE_DATA) && (MasterClient == 0)) return; #endif /* * We will always be sending some sort of packet, so * cobble together the packet headers now. */ pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; switch (TftpState) { case STATE_SEND_RRQ: case STATE_SEND_WRQ: xp = pkt; s = (ushort *)pkt; #ifdef CONFIG_CMD_TFTPPUT *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ : TFTP_WRQ); #else *s++ = htons(TFTP_RRQ); #endif pkt = (uchar *)s; strcpy((char *)pkt, tftp_filename); pkt += strlen(tftp_filename) + 1; strcpy((char *)pkt, "octet"); pkt += 5 /*strlen("octet")*/ + 1; strcpy((char *)pkt, "timeout"); pkt += 7 /*strlen("timeout")*/ + 1; sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000); debug("send option \"timeout %s\"\n", (char *)pkt); pkt += strlen((char *)pkt) + 1; #ifdef CONFIG_TFTP_TSIZE pkt += sprintf((char *)pkt, "tsize%c%lu%c", 0, NetBootFileXferSize, 0); #endif /* try for more effic. blk size */ pkt += sprintf((char *)pkt, "blksize%c%d%c", 0, TftpBlkSizeOption, 0); #ifdef CONFIG_MCAST_TFTP /* Check all preconditions before even trying the option */ if (!ProhibitMcast) { Bitmap = malloc(Mapsize); if (Bitmap && eth_get_dev()->mcast) { free(Bitmap); Bitmap = NULL; pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0); } } #endif /* CONFIG_MCAST_TFTP */ len = pkt - xp; break; case STATE_OACK: #ifdef CONFIG_MCAST_TFTP /* My turn! Start at where I need blocks I missed.*/ if (Multicast) TftpBlock = ext2_find_next_zero_bit(Bitmap, (Mapsize*8), 0); /*..falling..*/ #endif case STATE_RECV_WRQ: case STATE_DATA: xp = pkt; s = (ushort *)pkt; s[0] = htons(TFTP_ACK); s[1] = htons(TftpBlock); pkt = (uchar *)(s + 2); #ifdef CONFIG_CMD_TFTPPUT if (TftpWriting) { int toload = TftpBlkSize; int loaded = load_block(TftpBlock, pkt, toload); s[0] = htons(TFTP_DATA); pkt += loaded; TftpFinalBlock = (loaded < toload); } #endif len = pkt - xp; break; case STATE_TOO_LARGE: xp = pkt; s = (ushort *)pkt; *s++ = htons(TFTP_ERROR); *s++ = htons(3); pkt = (uchar *)s; strcpy((char *)pkt, "File too large"); pkt += 14 /*strlen("File too large")*/ + 1; len = pkt - xp; break; case STATE_BAD_MAGIC: xp = pkt; s = (ushort *)pkt; *s++ = htons(TFTP_ERROR); *s++ = htons(2); pkt = (uchar *)s; strcpy((char *)pkt, "File has bad magic"); pkt += 18 /*strlen("File has bad magic")*/ + 1; len = pkt - xp; break; } NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort, TftpOurPort, len); }
// // Run the in memory binary instruction stream on the machine // in a real time loop. // // Tracks underrun conditions. // // Stops on reported errors. // unsigned long stream_instructions( void* registers, PBLOCK_ARRAY binary ) { int ret; unsigned long status; void *block; unsigned long instruction_block_count; unsigned long underrun_errors; // Reset the timing engine // TODO: Have option to leave its state intact by default. // -reset or -resetfirst // printf("resetting timing engine fabric...\n"); status = menlo_cnc_reset_timing_engine(registers); printf("reset timing engine done\n"); printf("status after reset 0x%lx\n", status); status = menlo_cnc_read_status(registers); printf("status on enttry 0x%lx\n", status); // // context->compiled_binary is a pointer to the block array // that contains the compiled assembly instructions as machine // code that can be saved, fed to the registers, placed into memory // for DMA, etc. // // // Read instructions from block array. // // Consider batches. // // Validate that compiled instruction block machine // parameters and versions match the machine/version // we are talking to. Fail if no match. // // Have command line parameters, config file which specifies // which machine we are controlling so that wrong assembler // files are rejected. // // Support a binary file mode as well in which its read into // the block array without the source assembly path. This // can allow streaming from a large file on flash SD and minimizes // processing time. // // Implement instruction block packet/record boundary model // that can transport over UDP, TCP, etc. to allow streaming // to machines from a streaming file server. // ret = block_array_seek_entry(binary, 0); if (ret != 0) { printf("error rewinding block array %d\n", ret); return ret; } // // Note: First entry must be header // block = block_array_get_next_entry(binary); if (block == NULL) { printf("Empty assembly streadm\n"); // status is set from last load, or initial value if array is empty. return EBADF; } ret = validate_header_block(registers, block); if (ret != 0) { printf("No HEADER block at start of instruction stream\n"); return ret; } // // Load commands from block array until end or error. // // Note: This is a real-time, interrupt free CPU spinloop. // // If this is a real time thread this keeps the axis fed in real time. // // If not a real time thread, its good for jogging and general // positioning, but should be a dedicated real time thread for // timing critical machining to ensure the FIFO never runs empty // when there are valid instruction blocks left in the in memory stream. // // You can use Linux real time facilities to lock this program // into memory and dedicate it to a single core at high thread/process // priority. // // In addition if you use Linux real time user mode exteions to move // system services such as driver and timer interrupts, kernel // callouts (DPC's), event timers, and kernel threads off of the core you // will have a dedicated "hard" processing loop even if its a Linux user mode // process. // // With these real time user mode extensions this thread will not be // pre-empted if the above items are done and this process does not make // any system calls, which it does not in the inner loop until done, // or an error occurs (the printf, etc.). // // This model eliminates the need to create a special // "real time kernel" version of the inner loop staying with the ease of // debug, management, upgrade, etc. of a standard Linux user mode binary, // even if it has real time operating modes. // // The only limit to this model is the uncontrolability of the ARM // SoC core itself, which not even Linux can control at its lowest // levels such as TrustZone, co-processor emulation, thermal and power // management, etc. // // At the very real time level, the FPGA takes care of the timing accuracies // measured in nanoseconds. (20 ns for a very modest 50Mhz FPGA clock). // // If this loop can't handle a very high feed rate machine, a DMA model // or loading the 64MB FPGA dedicated SDRAM is preferred over the // easier to program SoC register interface model. // // // A run consists of non-realtime setup, real time command // stream, then non-real time cleanup. // // // Real time loop is defined as streaming commands from memory // until there are no more, without an underrun condition occuring. // // // Begin Run // status = 0; instruction_block_count = 0; underrun_errors = 0; // // Clear Sticky FIFO Underrun before entring the real time loop. // // This gets set if the FIFO's go empty once the first instruction // has been loaded. // menlo_cnc_registers_reset_sfe(registers); // // Begin real time Loop // while (1) { // // Note: instruction counts can be reduced by inlining, macro's, // etc. for status test, bumping through the sequential array, etc. // // These are considerations if this inner loop moves to a weaker embedded // soft processor such as a Nios II. // // The ARM SoC's have plently of instruction execution rate, its // just that their environment is more prone to interruptions // and pauses by both system and internal chip processes. // block = block_array_get_next_entry(binary); if (block == NULL) { printf("No more instruction entries in block array, loaded %ld blocks\n", instruction_block_count); // status is set from last load, or initial value if array is empty. goto Done; } instruction_block_count++; status = load_block(registers, block); if (menlo_cnc_registers_is_error(status)) { printf("error %ld loading block 0x%lx\n", status, instruction_block_count); goto Done; } if (menlo_cnc_registers_is_underrun(status)) { // Just report it, don't abort printf("underrun occurred status 0x%lx at instruction block %ld\n", status, instruction_block_count); underrun_errors++; // Rearm it menlo_cnc_registers_reset_sfe(registers); } } // // End real time Loop // // // End Run // Done: printf("instruction block count %ld, underrun errors %ld, status 0x%ld\n", instruction_block_count, underrun_errors, status); return status; }
int crypto_aead_encrypt( unsigned char *c,unsigned long long *clen, const unsigned char *m,unsigned long long mlen, const unsigned char *ad,unsigned long long adlen, const unsigned char *nsec, const unsigned char *npub, const unsigned char *k ) { u8 param[]={0x06,0,0,0,0x80,0,0,0}; block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS; int i; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1; unsigned long long cnt; for(i=0; i<16; i++) {zeroes[i]=0x00;} for(i=1; i<16; i++) {ozs[i]=0x00;} ozs[0] = 0x80; cnt = (8+mlen-1)/16 + 1; *clen = (cnt +1)* 16 + 1; if((mlen+8)%16 == 0) c[*clen-1] = 0x00; else c[*clen-1] = 0x01; key_schedule(k); /* ========== Generate the Masks =========== */ AES(6, ENCRYPT, blk, zeroes, &aes_key1); AES(6, ENCRYPT, L, blk, &aes_key1); mult_3(Delta_0, L); mult_inv2(Delta_0, Delta_0); mult_inv2(Delta_1, L); mult_3(Delta_2, L); mult_3(Delta_2, Delta_2); mult_inv2(Delta_2, Delta_2); /* ====== Process Associated Data ======== */ for(i=0; i<16; i++) W[i]=0x00; process_AD(W, Delta_0, npub, param, ad, adlen); /* ================ Process Successive Message Blocks ==================== */ /* ====== Process the first Message block, whose first 8 byte is the secret message number ===== */ if(mlen < 8){ Is_complete = 0; } if(mlen <= 8) { blen = 8 + mlen; } load_block(blk, nsec, m, 8, blen-8); copy_block(CS, blk); process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); store_bytes(c, result, 0, 15); c +=16; if(mlen >= 8) {mlen -= 8; m +=8;} else mlen = 0; /* ============= Process Message blocks ================== */ while(mlen > 0){ if(mlen >= 16){ load_block(blk, m, ozs, 16, 0); if(mlen == 16) {xor_block(blk, blk, CS); } else xor_block(CS, CS, blk); blen = 16; mlen -= 16; m+=16; } else {Is_complete = 0; blen = mlen; mlen = 0; load_block(blk, m, ozs, blen, 0); xor_block(blk, CS, blk); } process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); store_bytes(c, result, 0, 15); c +=16; } /* ================ Process checksum block ====================== */ process_block(Delta_1, Delta_2, result, blk, W, 1, ENCRYPT, 1, MESSAGE); store_bytes(c, result, 0, 15); return 0; }
IO_stat MCParagraph::load(IO_handle stream, const char *version) { IO_stat stat; stat = IO_NORMAL; if (stat == IO_NORMAL) stat = IO_read_uint2(&m_text_size, stream); if (m_text_size == 1) { if (stat == IO_NORMAL) { char t_temp; uint4 t_temp_count; t_temp_count = 1; stat = IO_read(&t_temp, sizeof(uint1), t_temp_count, stream); } if (stat == IO_NORMAL) m_text_size = 0; } else if (m_text_size > 1) { if (stat == IO_NORMAL) { m_text = malloc(m_text_size); if (m_text == NULL) stat = IO_ERROR; } if (stat == IO_NORMAL) { uint4 t_temp_text_size; t_temp_text_size = m_text_size; stat = IO_read(m_text, sizeof(uint1), t_temp_text_size, stream); } if (stat == IO_NORMAL) { if (MCencryptstring != NULL) MCX_passde((char *)m_text, MCencryptstring, m_text_size); m_text_size -= 1; } } while(stat == IO_NORMAL) { uint1 t_type; stat = IO_read_uint1(&t_type, stream); if (stat != IO_NORMAL) break; uint4 t_block_offset, t_block_length, t_block_style; stat = load_block(stream, version, t_block_offset, t_block_length, t_block_style); if (stat != IO_NORMAL) break; // Make sure the block doesn't overrun the text buffer (shouldn't ever be // necessary...) t_block_length = MCU_min(m_text_size - t_block_offset, t_block_length); // Byte-swap the text to native byte-order if its unicode, otherwise translate // Mac<->ISO if necessary. const MCParagraphCharStyle *t_style_info; t_style_info = FetchCharStyle(t_block_style); if (t_style_info -> is_unicode) { uint4 t_length; t_length = t_block_length / 2; uint2 *t_ptr; t_ptr = (uint2 *)((uint1 *)m_text + t_block_offset); for(; t_length > 0; t_length -= 1) swap_uint2(t_ptr++); } else if (MCtranslatechars) { #ifdef _MACOSX IO_iso_to_mac((char *)m_text + t_block_offset, t_block_length); #else IO_mac_to_iso((char *)m_text + t_block_offset, t_block_length); #endif } // Replace the style in the given range with the style ReplaceStyles(t_block_offset, t_block_length, t_block_style); } if (stat == IO_NORMAL) { // If no styles were applied, we need to translate the text if (m_styles_size == 0 && MCtranslatechars && m_text_size > 0) #ifdef _MACOSX IO_iso_to_mac((char *)m_text, m_text_size); #else IO_mac_to_iso((char *)m_text, m_text_size); #endif Compact(); MCS_seek_cur(stream, -1); } return stat; }
/* =================== ELmD(6,0,0) Verified Decryption Function =================== */ int crypto_aead_decrypt( unsigned char *m,unsigned long long *mlen, unsigned char *nsec, const unsigned char *c, unsigned long long clen, const unsigned char *ad, unsigned long long adlen, const unsigned char *npub, const unsigned char *k ) { u8 param[]={0x06,0,0,0,0x80,0,0,0}; block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS; int i, j, flag=0; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1; unsigned long long outputmlen; for(i=0; i<16; i++) {zeroes[i]=0x00;} // all zero. for(i=1; i<16; i++) {ozs[i]=0x00;} ozs[0] = 0x80; //useful for padding. if(((clen % 16) != 1)||(clen < 33)) return -1; key_schedule(k); /* =========== Generate the Masks ========== */ AES(6, ENCRYPT, blk, zeroes, &aes_key1); AES(6, ENCRYPT, L, blk, &aes_key1); mult_3(Delta_0, L); mult_inv2(Delta_0, Delta_0); mult_inv2(Delta_1, L); mult_3(Delta_2, L); mult_3(Delta_2, Delta_2); mult_inv2(Delta_2, Delta_2); /* ============= Process Associated Data ================ */ for(i=0; i<16; i++) W[i]=0x00; process_AD(W, Delta_0, npub, param, ad, adlen); load_block(CS, zeroes, zeroes, 16, 0); /* ================ Process Ciphertext Blocks ============ */ load_block(blk, c, zeroes, 16, 0); /* =================== Process 1st Block =================== */ if( (clen==33) && (c[clen-1]==0x01) ) { process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); for(i=15; i>7; i--) { if(result[i]==0x80) { flag=1; break; } } if(flag == 0) {return -1; } for(j=i+1; j<16; j++) { if(result[j]!=0) {return -1; }} store_bytes(m, result, 8, i-1); outputmlen = i-8; } else { process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); store_bytes(m, result, 8, 15); m +=8; outputmlen = 8; } xor_block(CS, CS, result); store_bytes(nsec, result, 0, 7); clen -= 16; c+=16; /* ============= Process Successive Ciphertext Blocks ============== */ while(clen > 17){ load_block(blk, c, zeroes, 16, 0); if((clen == 33) && (c[clen-1]==0x01)) { process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); xor_block(result, result, CS); for(i=15; i>0; i--) { if(result[i]==0x80) { flag=1; break; } } if(flag == 0) {return -1;} for(j=i+1; j<16; j++) { if(result[j]!=0) {return -1; }} store_bytes(m, result, 0, i-1); outputmlen += i; } else{ process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); if(clen == 33) { xor_block(result, result, CS); } store_bytes(m, result, 0, 15); outputmlen += 16; } xor_block(CS, CS, result); clen -= 16; c+=16; if(clen != 17) m +=16; } /* ========== Process checksum block ============= */ Is_final = 1; process_block(Delta_2, Delta_1, result, c, W, 1, DECRYPT, Is_final, CIPHERTEXT); for(i=0; i<16; i++) {if(CS[i]!=result[i]) return -1; } *mlen = outputmlen; return 0; }
int crypto_aead_encrypt( unsigned char *c,unsigned long long *clen, const unsigned char *m,unsigned long long mlen, const unsigned char *ad,unsigned long long adlen, const unsigned char *nsec, const unsigned char *npub, const unsigned char *k ) { u8 param[]={0x06,0,0x7f,0x81,0x80,0,0,0}; block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS; int i; unsigned long long h, blk_ctr=0, blk_ctr1=0 ; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1; for(i=0; i<16; i++) {zeroes[i]=0x00;} for(i=1; i<16; i++) {ozs[i]=0x00;} ozs[0] = 0x80; h = (mlen+8-1) / (IT_GAP * 16); if(h > IT_MAX) h = IT_MAX; *clen = mlen + 24 + h*16 ; key_schedule(k); /* ========== Generate the Masks =========== */ AES(6, ENCRYPT, blk, zeroes, &aes_key1); AES(6, ENCRYPT, L, blk, &aes_key1); mult_3(Delta_0, L); mult_inv2(Delta_0, Delta_0); mult_inv2(Delta_1, L); mult_3(Delta_2, L); mult_3(Delta_2, Delta_2); mult_inv2(Delta_2, Delta_2); /* ====== Process Associated Data ======== */ for(i=0; i<16; i++) W[i]=0x00; process_AD(W, Delta_0, npub, param, ad, adlen); /* ================ Process Message Blocks ==================== */ /* ====== Process the first Message block, whose first 8 byte is the secret message number ===== */ if(mlen < 8){ Is_complete = 0; } if(mlen <= 8) { blen = 8 + mlen; } load_block(blk, nsec, m, 8, blen-8); copy_block(CS, blk); process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); store_bytes(c, result, 0, 15); c +=16; blk_ctr++; if(mlen >= 8) {mlen -= 8; m +=8;} else mlen = 0; /* ============= Process Message blocks ================== */ while(mlen > 0){ if(mlen >= 16){ load_block(blk, m, ozs, 16, 0); if(mlen == 16){xor_block(blk, CS, blk); } else xor_block(CS, CS, blk); //xor_block(CS, CS, blk); blen = 16; mlen -= 16; m+=16; } else {Is_complete = 0; blen = mlen; mlen = 0; load_block(blk, m, ozs, blen, 0); xor_block(blk, CS, blk); } process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); store_bytes(c, result, 0, 15); c +=16; blk_ctr++; if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && mlen>0) { AES(6, DECRYPT, result, W, &aes_key2); mask(Delta_2, result, result, 1); store_bytes(c, result, 0, 15); c +=16; blk_ctr =0; blk_ctr1++; } } /* ================ Process checksum block ====================== */ process_block(Delta_1, Delta_2, result, blk, W, 1, ENCRYPT, 1, MESSAGE); store_bytes(c, result, 0, blen-1); return 0; }
/* =================== ELmD(6,127,1) Verified Decryption Function =================== */ int crypto_aead_decrypt( unsigned char *m,unsigned long long *mlen, unsigned char *nsec, const unsigned char *c, unsigned long long clen, const unsigned char *ad, unsigned long long adlen, const unsigned char *npub, const unsigned char *k ) { u8 param[]={0x06,0,0x7f,0x81,0x80,0,0,0}; block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS; int i; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1; unsigned long long outputmlen, blk_ctr=0, blk_ctr1=0; for(i=0; i<16; i++) {zeroes[i]=0x00;} // all zero. for(i=1; i<16; i++) {ozs[i]=0x00;} ozs[0] = 0x80; //useful for padding. if(clen < 24) return -1; key_schedule(k); /* =========== Generate the Masks ========== */ AES(6, ENCRYPT, blk, zeroes, &aes_key1); AES(6, ENCRYPT, L, blk, &aes_key1); mult_3(Delta_0, L); mult_inv2(Delta_0, Delta_0); mult_inv2(Delta_1, L); mult_3(Delta_2, L); mult_3(Delta_2, Delta_2); mult_inv2(Delta_2, Delta_2); /* ============= Process Associated Data ================ */ for(i=0; i<16; i++) W[i]=0x00; process_AD(W, Delta_0, npub, param, ad, adlen); load_block(CS, zeroes, zeroes, 16, 0); /* ================ Process Ciphertext Blocks ============ */ load_block(blk, c, zeroes, 16, 0); /* =================== Process 1st Block =================== */ if(clen<32){ process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); store_bytes(m, result, 8, 8+(clen-25)); outputmlen = clen - 24; } else { process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); store_bytes(m, result, 8, 15); m +=8; outputmlen = 8; } xor_block(CS, CS, result); store_bytes(nsec, result, 0, 7); clen -= 16; c+=16; blk_ctr ++; if(clen < 16){ if(result[clen] != 0x80) return -1; for(i=clen+1; i<16; i++) {if(result[i]!=0) return -1;} } /* ============= Process Successive Ciphertext Blocks ============== */ while(clen > 16){ load_block(blk, c, zeroes, 16, 0); if(clen < 32){ process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); xor_block(result, result, CS); store_bytes(m, result, 0, clen - 17); outputmlen += clen-16 ; } else{ process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); if(clen == 32) {xor_block(result, result, CS);} store_bytes(m, result, 0, 15); outputmlen += 16; } xor_block(CS, CS, result); clen -= 16; c+=16; blk_ctr ++; if(clen < 16){ if(result[clen] != 0x80) return -1; for(i=clen+1; i<16; i++) {if(result[i]!=0) return -1;} } else m +=16; if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && clen > 32){ AES(6, DECRYPT, result, W, &aes_key2); mask(Delta_2, result, result, 1); for(i=0; i<16; i++) { if(c[i] != result[i]) {return -1; } } c +=16; clen -= 16; blk_ctr =0; blk_ctr1++; } } /* ========== Process checksum block ============= */ Is_final = 1; process_block(Delta_1, Delta_2, result, CS, W, 1, ENCRYPT, Is_final, MESSAGE); for(i=0; i<clen; i++) {if(result[i]!=c[i]) return -1;} *mlen = outputmlen; return 0; }
int main(int argc, char **argv) { int fd; if (argc <= 1) { printf("Usage: load_file image\n"); return 1; } fd = open(argv[1], O_RDONLY); lseek(fd, BOOT_BLOCK_SIZE, SEEK_SET); struct superblock *superblock = (struct superblock *) malloc(SUPERBLOCK_SIZE); read(fd, superblock, SUPERBLOCK_SIZE); uint32_t block_size = 1024 << superblock->s_log_block_size; void *block = malloc(block_size); cached_blocks = malloc(sizeof(void *) * (DEPTHS - 1)); for (int i = 0; i < DEPTHS - 1; i++) { cached_blocks[i] = malloc(block_size); } struct group_descriptor *group_descriptor = block; load_block(group_descriptor, fd, 1 + superblock->s_first_data_block, block_size, 0); printf("block size: %d\n", block_size); printf("inode table: %d\n", group_descriptor->bg_inode_table); printf("block bitmap: %d\n", group_descriptor->bg_block_bitmap); printf("inodes per group: %d\n", superblock->s_inodes_per_group); printf("inode size: %d\n", superblock->s_inode_size); uint32_t inode_table_start = group_descriptor->bg_inode_table; #if 0 uint32_t inodes_per_block = 8 << superblock->s_log_block_size; printf("inodes per block: %d\n", inodes_per_block); for (int i = 0; i < superblock->s_inodes_per_group; i += inodes_per_block) { load_block(block, fd, inode_table_start + i / inodes_per_block, block_size, 0); struct inode_table *inode_table = block; for (int j = 0; j < inodes_per_block; j++) { if (inode_table[j].i_mode) { printf("i_mode[%d]: %.8x; %.8lx\n", i + j, inode_table[j].i_mode, j * sizeof(struct inode_table) + block_size * (inode_table_start + i / inodes_per_block)); printf("\ti_size[%d]: %.8x\n", i+j, inode_table[j].i_size); #if 1 if((inode_table[j].i_mode >>12) == 0x08) { uint8_t * buf = load_inode(fd, &inode_table[j], block_size); printf("inode size: %d\n", inode_table[j].i_size); for(int i=0; i<inode_table[j].i_size; i++) { putchar(buf[i]); } free(buf); } #endif } } }
//helper function void SpinBlock::Load (std::ifstream & ifs) { boost::archive::binary_iarchive load_block(ifs); load_block >> *this; }
void load_elf_file(void *file, unsigned long long virt) { struct elfheader eh; struct sheader *sh; long i; int addr_displayed = 0; D(kprintf("[ELF Loader] Loading ELF module from address %p\n", (unsigned int)file)); /* Check the header of ELF file */ if ( !read_block(file, 0, &eh, sizeof(eh)) || !check_header(&eh) || !(sh = load_block(file, eh.shoff, eh.shnum * eh.shentsize)) ) { kprintf("[ELF Loader] Wrong module header, aborting.\n"); return; } /* Iterate over the section header in order to prepare memory and eventually load some hunks */ for (i=0; i < eh.shnum; i++) { /* Load the symbol and string tables */ if (sh[i].type == SHT_SYMTAB || sh[i].type == SHT_STRTAB) { sh[i].addr = load_block(file, sh[i].offset, sh[i].size); } /* Does the section require memoy allcation? */ else if (sh[i].flags & SHF_ALLOC) { /* Yup, it does. Load the hunk */ if (!load_hunk(file, &sh[i])) { kprintf("[ELF Loader] Error at loading of the hunk!\n"); } else if (!addr_displayed) { kprintf("[ELF Loader] shared mem@0x%x\n", sh[i].addr); addr_displayed = 1; } } } /* For every loaded section perform the relocations */ for (i=0; i < eh.shnum; i++) { if ((sh[i].type == SHT_RELA || sh[i].type == SHT_REL) && sh[sh[i].info].addr) { sh[i].addr = load_block(file, sh[i].offset, sh[i].size); if (!sh[i].addr || !relocate(&eh, sh, i, virt)) { kprintf("[ELF Loader] Relocation error!\n"); } free_block(sh[i].addr); } else if (sh[i].type == SHT_SYMTAB || sh[i].type == SHT_STRTAB) free_block(sh[i].addr); } free_block(sh); }