// main entrypoint int main(int argc, char **argv) { int keylength; printf("Give a key length [only 128 or 192 or 256!]:\n"); scanf("%d", &keylength); /* generate a key with a given length */ unsigned char aes_key[keylength/8]; memset(aes_key, 0, keylength/8); if (!RAND_bytes(aes_key, keylength/8)) exit(-1); /* input struct creation */ size_t inputslength = sizeof(USR_TICKET); USR_TICKET ticket; ticket.ticketId = 1; time_t now = time(NULL); strftime(ticket.date, 20, "%Y-%m-%d", localtime(&now)); strcpy(ticket.username, "Hello AES"); printf("Username - %s\n", ticket.username); printf("Ticket Id - %d\n", ticket.ticketId); printf("Date - %s\n", ticket.date); /* init vector */ unsigned char iv_enc[AES_BLOCK_SIZE], iv_dec[AES_BLOCK_SIZE]; RAND_bytes(iv_enc, AES_BLOCK_SIZE); memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE); // buffers for encryption and decryption const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; unsigned char enc_out[encslength]; unsigned char dec_out[inputslength]; memset(enc_out, 0, sizeof(enc_out)); memset(dec_out, 0, sizeof(dec_out)); // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256 AES_KEY enc_key, dec_key; AES_set_encrypt_key(aes_key, keylength, &enc_key); AES_cbc_encrypt((unsigned char *)&ticket, enc_out, encslength, &enc_key, iv_enc, AES_ENCRYPT); AES_set_decrypt_key(aes_key, keylength, &dec_key); AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT); printf("original:\t"); hex_print((unsigned char *)&ticket, inputslength); printf("encrypt:\t"); hex_print(enc_out, sizeof(enc_out)); printf("decrypt:\t"); hex_print(dec_out, sizeof(dec_out)); USR_TICKET * dyc = (USR_TICKET *)dec_out; printf("Username - %s\n", dyc->username); printf("Ticket Id - %d\n", dyc->ticketId); printf("Date - %s\n", dyc->date); return 0; }
str new_str(const char* fmt, va_list args) { char* c; str s; reg x, xl, sl; reg i, ls = 0, la = 0; for (i = 0; fmt[i]; ++i) la += (fmt[i] == '%' ? 1 : 0); str retval = blank(0); ls = 0; for (i = 0; fmt[i]; ++i) { if (fmt[i] == '%') switch (fmt[++i]) { case 'c': c = va_arg(args,char*); sl = strlen(c); memcpy(&retval->data[ls],c,sl); ls += sl; break; case 's': s = va_arg(args,str); memcpy(&retval->data[ls],s->data,s->length); ls += s->length; break; case 'd': case 'i': x = va_arg(args,reg); xl = int_len(x); int_print(&retval->data[ls],x,xl); ls += xl; break; case 'p': case 'x': c = va_arg(args,char*); xl = hex_len((reg)c); hex_print(&retval->data[ls],(reg)c,xl); ls += xl; break; case 'h': x = va_arg(args,reg); xl = hex_len(x); hex_print(&retval->data[ls],x,xl); ls += xl; break; default: ++ls; } else retval->data[ls++] = fmt[i]; }
str hex_str(reg i) { reg l = hex_len(i); str retval = blank(l); hex_print(retval->data,i,l); return retval; }
TEST(SecurityAESEncryption, aes_controller_initial){ utils::uuid_generator uuid_gen; std::string uuid = uuid_gen.generate(); std::string ip_address = "127.0.0.1"; internet::security::aes_controller<message_scan::RequestScan> aes_conn; std::unique_ptr<internet::security::aes_cbc> aes = aes_conn.initial_key(ip_address, uuid); EXPECT_EQ("127.0.0.1", aes->ip); EXPECT_EQ(uuid , aes->uuid); //LOG(INFO)<<"AES : " << std::string((const char*)aes->key); //LOG(INFO)<<"IV : " << std::string((const char*)aes->iv); printf("AES key : \t"); hex_print((unsigned char*)&aes->key, aes->key_length); printf("IV key : \t"); hex_print((unsigned char*)&aes->iv, aes->key_length); }
int dbg_hexdump(DBG * d, const unsigned char *data, size_t len) { if (!d) return -1; if (!d->on) return 0; // dbg_printf_raw(d, "对长度为%08d字节的数据(地址0x%08x)进行十六进制转储:", len, data); hex_print(d, "", data, len); return 0; }
/* * Usage : pksender -p packet-type -s source-address -d destine-address * Desc : the format of packet payload is like that "send timestamp" * Output: the result of sending packet * */ i32_t main(i32_t argc, i8_p argv[]) { i32_t i; i8_t data[128]; for (i=0; i<128; i++) { data[i] = i; } hex_print(stdout, data, 125); return 0; }
void hexdump(const void *vp, unsigned int len) { const unsigned char *p = vp; unsigned int i, addr; unsigned int wordlen = sizeof(void*); unsigned char v, line[BYTES_PER_LINE * 5]; for (addr = 0; addr < len; addr += BYTES_PER_LINE) { /* clear line */ for (i = 0; i < sizeof(line); i++) { if (i == wordlen * 2 + 52 || i == wordlen * 2 + 69) { line[i] = '|'; continue; } if (i == wordlen * 2 + 70) { line[i] = '\0'; continue; } line[i] = ' '; } /* print address */ for (i = 0; i < wordlen * 2; i++) { v = addr >> ((wordlen * 2 - i - 1) * 4); line[i] = nibble[v & 0xf]; } /* dump content */ for (i = 0; i < BYTES_PER_LINE; i++) { int pos = (wordlen * 2) + 3 + (i / 8); if (addr + i >= len) break; v = p[addr + i]; line[pos + (i * 3) + 0] = nibble[v >> 4]; line[pos + (i * 3) + 1] = nibble[v & 0xf]; /* character printable? */ line[(wordlen * 2) + 53 + i] = (v >= ' ' && v <= '~') ? v : '.'; } hex_print(line); } }
int main(int argc, char *argv[]) { hex_print("\n\t", "Hello, World!\n", 14); printf("\n"); hex_and_ascii_print("\n\t", "Hello, World!\n", 14); printf("\n"); ascii_print("Hello, World!\n", 14); printf("\n"); #define TMSG "Now is the winter of our discontent...\n" hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100); printf("\n"); hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100); printf("\n"); exit(0); }
int print_unknown_data(netdissect_options *ndo, const u_char *cp,const char *ident,int len) { if (len < 0) { ND_PRINT((ndo,"%sDissector error: print_unknown_data called with negative length", ident)); return(0); } if (ndo->ndo_snapend - cp < len) len = ndo->ndo_snapend - cp; if (len < 0) { ND_PRINT((ndo,"%sDissector error: print_unknown_data called with pointer past end of packet", ident)); return(0); } hex_print(ndo, ident,cp,len); return(1); /* everything is ok */ }
int print_unknown_data(const u_char *cp,const char *ident,int len) { if (len < 0) { printf("%sDissector error: print_unknown_data called with negative length", ident); return(0); } if (snapend - cp < len) len = snapend - cp; if (len < 0) { printf("%sDissector error: print_unknown_data called with pointer past end of packet", ident); return(0); } hex_print(ident,cp,len); return(1); /* everything is ok */ }
int main(int argc, char *argv[]) { int s; struct iphdr *iph; struct icmphdr *icmph; ssize_t rn; /* receive number */ struct sockaddr_in saddr; socklen_t fromlen; char packet[4096]; char from[INET_ADDRSTRLEN]; if ((s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { fprintf(stderr, "create raw socket failed: %s\n", strerror(errno)); exit(1); } memset(packet, 0, sizeof(packet)); fromlen = sizeof(saddr); while (1) { if ((rn = recvfrom(s, (char *)&packet, sizeof(packet), 0, (struct sockaddr *)&saddr, &fromlen)) < 0) { fprintf(stderr, "recvfrom failed: %s\n", strerror(errno)); } if (rn == 0) { fprintf(stdout, "the peer has performed an orderly shutdown\n"); break; } fprintf(stdout, "=================================\n"); inet_ntop(AF_INET, &saddr.sin_addr, from, INET_ADDRSTRLEN); fprintf(stdout, "receive packet: %lu, from: %s\n", rn, from); hex_print(packet, rn); iph = (struct iphdr *)packet; if (iph->protocol == IPPROTO_ICMP) { fprintf(stdout, "recevie icmp\n"); } icmph = (struct icmphdr *)(packet + iph->ihl * 4); fprintf(stdout, "type %u, code %u\n", icmph->type, icmph->code); fprintf(stdout, "checksum 0x%04x, \n", icmph->checksum); fprintf(stdout, "id %u, sequence %u\n", ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence)); } return 0; }
//This function contains the AES Encyption and Decryption logic used void AESEncryptData(const char* filename,int choice) { int keylength=256; /* generate a key with a given length */ //Key used for Encryption and Decryption unsigned char aes_key[]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0x6E,0xF8,0x07,0x11,0x25,0x32,0x47,0x57,0x66,0x74,0x88,0x90,0x2A,0x3B,0xCE,0x4D,0xEE,0xFA}; /* unsigned char aes_key[keylength/8]; memset(aes_key, 0, keylength/8); if (!RAND_bytes(aes_key, keylength/8)) exit(-1);*/ size_t inputslength = sizeof(buf); /* generate input with a given length */ unsigned char aes_input[inputslength]; memcpy(aes_input, buf, inputslength); /* init vector */ unsigned char iv_enc[AES_BLOCK_SIZE], iv_dec[AES_BLOCK_SIZE]; memset(iv_enc,0x00,AES_BLOCK_SIZE); memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE); // buffers for encryption and decryption const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; unsigned char enc_out[encslength]; unsigned char dec_out[inputslength]; memset(enc_out, 0, sizeof(enc_out)); memset(dec_out, 0, sizeof(dec_out)); //Path where the encrypted file will be Stored char filepath[90]="/Users/prashanth/Desktop/try-1/ProejectTry-2/cloud/"; strcat(filepath, filename); AES_KEY enc_key, dec_key; //Upload Scenario if(choice ==1){ AES_set_encrypt_key(aes_key, keylength, &enc_key); AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT); printf("encrypted message of File: \n"); hex_print(enc_out, sizeof(enc_out)); printf("\n\nPath where file is stored at server :%s\n",filepath); FILE *restFp = fopen(filepath, "wb"); if(restFp == NULL) printf("Sorry, file cannot be created\n"); else{ fwrite(enc_out,sizeof(unsigned char), sizeof(enc_out),restFp); fclose(restFp); printf("\nFile created at server\n"); } /* AES_set_decrypt_key(aes_key, keylength, &dec_key); AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT); printf("decrypt:\t"); */ // hex_print(dec_out, sizeof(dec_out)); } //Download Scenario else{ FILE *restFp = fopen(filepath, "rb"); if(restFp == NULL) printf("Sorry, file cannot be found\n"); else{ //printf("%lu",sizeof(buf)); fread(buf,sizeof(unsigned char), sizeof(buf),restFp); inputslength = sizeof(buf); const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; memcpy(enc_out, buf,inputslength); printf("\n\n Decrypting File...\n"); hex_print(enc_out, sizeof(enc_out)); AES_set_decrypt_key(aes_key, keylength, &dec_key); AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT); //printf("decrypted Message:\t"); // hex_print(dec_out, sizeof(dec_out)); //buf =0; sprintf(buf,"%s",dec_out); //printf("After utting to bugf---%s",buf); fclose(restFp); } //printf("decrypt:\t"); //hex_print(dec_out, sizeof(dec_out)); } }
void send_packets(char *device, char *trace_file) { FILE *fp; /* file pointer to trace file */ struct pcap_file_header preamble; struct pcap_sf_pkthdr header; int pkt_len; /* packet length to send */ int ret; int i; int it; // LC: iterator struct pcap_timeval p_ts; struct timeval ts; struct timeval sleep = {0,0}; struct timeval cur_ts; struct timeval prev_ts = {0,0}; struct timespec nsleep; sigset_t block_sig; // LC: variables unsigned int ipsourceT; struct in_addr ipsource; u_char *pkt_ip; int pcount; // packet count pcount = 0; (void)sigemptyset(&block_sig); (void)sigaddset(&block_sig, SIGINT); notice("trace file: %s", trace_file); if ((fp = fopen(trace_file, "rb")) == NULL) error("fopen(): error reading %s", trace_file); /* preamble occupies the first 24 bytes of a trace file */ if (fread(&preamble, sizeof(preamble), 1, fp) == 0) error("fread(): error reading %s", trace_file); if (preamble.magic != PCAP_MAGIC) error("%s is not a valid pcap based trace file", trace_file); /* * loop through the remaining data by reading the packet header first. * packet header (16 bytes) = timestamp + length */ while ((ret = fread(&header, sizeof(header), 1, fp))) { if (ret == 0) error("fread(): error reading %s", trace_file); /* copy timestamp for current packet */ memcpy(&p_ts, &header.ts, sizeof(p_ts)); cur_ts.tv_sec = p_ts.tv_sec; cur_ts.tv_usec = p_ts.tv_usec; if (len < 0) /* captured length */ pkt_len = header.caplen; else if (len == 0) /* actual length */ pkt_len = header.len; else /* user specified length */ pkt_len = len; if (timerisset(&prev_ts)) { /* pass first packet */ if (speed != 0) { if (interval > 0) { /* user specified interval is in seconds only */ sleep.tv_sec = interval; if (speed != 1) timer_div(&sleep, speed); /* speed factor */ } else { /* grab captured interval */ timersub(&cur_ts, &prev_ts, &sleep); if (speed != 1) { if (sleep.tv_sec > SLEEP_MAX) /* to avoid integer overflow in timer_div() */ notice("ignoring speed due to large interval"); else timer_div(&sleep, speed); } } if (linerate > 0) { i = linerate_interval(pkt_len); /* check if we exceed line rate */ if ((sleep.tv_sec == 0) && (sleep.tv_usec < i)) sleep.tv_usec = i; /* exceeded -> adjust */ } } else { /* send immediately */ if (linerate > 0) sleep.tv_usec = linerate_interval(pkt_len); } if (timerisset(&sleep)) { /* notice("sleep %d seconds %d microseconds", sleep.tv_sec, sleep.tv_usec); */ TIMEVAL_TO_TIMESPEC(&sleep, &nsleep); if (nanosleep(&nsleep, NULL) == -1) /* create the artificial slack time */ notice("nanosleep(): %s", strerror(errno)); } } // LC: force to skip the packet if(pkt_len < ETHER_MAX_LEN){ for (i = 0; i < pkt_len; i++) { /* copy captured packet data starting from link-layer header */ if (i < header.caplen) { if ((ret = fgetc(fp)) == EOF) error("fgetc(): error reading %s", trace_file); pkt_data[i] = ret; } else /* pad trailing bytes with zeros */ pkt_data[i] = PKT_PAD; } (void)sigprocmask(SIG_BLOCK, &block_sig, NULL); /* hold SIGINT */ // LC: find source ip //fprintf(stdout, "%d Sending %d bytes ", pcount++, pkt_len); if(pkt_len > 33){ // at least could be ip if( ((struct ether_header *)pkt_data)->ether_type == 8){ pkt_ip = pkt_data+14; //fprintf(stdout, "Ether type = 8 "); //fprintf(stdout, "Ip v = %d \t", ((struct ip *)pkt_ip)->ip_v); ipsource = ((struct ip *)pkt_ip)->ip_src; ipsourceT = ntohl(ipsource.s_addr); //fprintf(stdout, "Source %u %s ", ipsourceT, inet_ntoa(ipsource)); // //fprintf(stdout, " %s ", inet_ntoa(ipsource)); for(it = 0; it < nentries; it++){ //fprintf(stdout, "(%u %u %u) \t", (unsigned int)ipTable[(it * 3) + 0], ipsourceT, (unsigned int)ipTable[(it * 3) + 1]); // if( ((unsigned int)ipTable[(it * 3) + 0] <= (unsigned int)ipsourceT ) && ((unsigned int)ipTable[(it * 3) + 1]) >= (unsigned int)ipsourceT ){ //fprintf(stdout, "In range at if %s \r\n", ipTable[(it * 3) + 2]); //pcap_t *pd = ipTable[(it * 3) + 2]; pd = pdl[it]; break; } } } } // LC: checks if(pkt_len < ETHER_MAX_LEN && it != nentries){ /* finish the injection and verbose output before we give way to SIGINT */ if (pcap_sendpacket(pd, pkt_data, pkt_len) == -1) { notice("%s", pcap_geterr(pd)); ++failed; } else { ++pkts_sent; bytes_sent += pkt_len; /* copy timestamp for previous packet sent */ memcpy(&prev_ts, &cur_ts, sizeof(struct timeval)); /* verbose output */ if (vflag) { if (gettimeofday(&ts, NULL) == -1) notice("gettimeofday(): %s", strerror(errno)); else ts_print(&ts); (void)printf("#%d (%d bytes)", pkts_sent, pkt_len); if (vflag > 1) hex_print(pkt_data, pkt_len); else putchar('\n'); fflush(stdout); } } (void)sigprocmask(SIG_UNBLOCK, &block_sig, NULL); /* release SIGINT */ if ((max_pkts > 0) && (pkts_sent >= max_pkts)) cleanup(0); } }else{ // LC: force skip // fprintf(stdout, "LC: Force Skip packet !\r\n"); for (i = 0; i < pkt_len; i++) { /* copy captured packet data starting from link-layer header */ if (i < header.caplen) { if ((ret = fgetc(fp)) == EOF) error("fgetc(): error reading %s", trace_file); } } } /* move file pointer to the end of this packet data */ if (i < header.caplen) { if (fseek(fp, header.caplen - pkt_len, SEEK_CUR) != 0) error("fseek(): error reading %s", trace_file); } } /* end while */ (void)fclose(fp); }
bool hex_print(String & string, const S8 & hex) { U32 tmp = hex; return hex_print(string,tmp); }
int main ( void ) { init_uart0(); /* For 2 MHz crystal use this hack */ //BAUD0L_REG = 12; trigger_setup(); //OSCCAL only needed for internal oscillator mode, doesn't hurt anyway #ifdef OSCCAL OSCCAL = OSCCAL_UARTGOOD; _delay_ms(500); #else #ifdef VARYING_CLOCK #error "VARYING_CLOCK requested but target does not have OSCCAL register" #endif #endif /* Uncomment this to get a HELLO message for debug */ /* output_ch_0('h'); output_ch_0('e'); output_ch_0('l'); output_ch_0('l'); output_ch_0('o'); output_ch_0('\n'); */ /* Super-Crappy Protocol works like this: Send kKEY Send pPLAINTEXT *** Encryption Occurs *** receive rRESPONSE e.g.: kE8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA\n p014BAF2278A69D331D5180103643E99A\n r6743C3D1519AB4F2CD9A78AB09A511BD\n */ char c; int ptr = 0; //Initial key aes_indep_init(); aes_indep_key(tmp); char state = 0; #ifdef VARYING_CLOCK uint8_t newosc; #endif while(1){ c = input_ch_0(); if (c == 'x') { ptr = 0; state = IDLE; continue; } if (c == 'k') { ptr = 0; state = KEY; continue; } else if (c == 'p') { ptr = 0; state = PLAIN; continue; } else if (state == KEY) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, tmp); aes_indep_key(tmp); state = IDLE; } else { asciibuf[ptr++] = c; } } else if (state == PLAIN) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, pt); /* Do Encryption */ //The following is used for varying the clock #ifdef VARYING_CLOCK _delay_ms(25); #ifdef OSCCAL_CENTER newosc = (rand() & OSCCAL_VARY); OSCCAL = OSCCAL_CENTER + (int8_t)((int8_t)(OSCCAL_VARY/2) - (int8_t)newosc); #else OSCCAL = rand() & OSCMAX; #endif _delay_ms(1); #endif trigger_high(); aes_indep_enc(pt); /* encrypting the data block */ trigger_low(); #ifdef VARYING_CLOCK OSCCAL = OSCCAL_UARTGOOD; _delay_ms(100); #endif /* Print Results */ hex_print(pt, 16, asciibuf); output_ch_0('r'); for(int i = 0; i < 32; i++){ output_ch_0(asciibuf[i]); } output_ch_0('\n'); state = IDLE; } else { if (ptr >= BUFLEN){ state = IDLE; } else { asciibuf[ptr++] = c; } } } } return 1; }
int main ( void ) { platform_init(); init_uart(); trigger_setup(); /* Uncomment this to get a HELLO message for debug */ /* putch('h'); putch('e'); putch('l'); putch('l'); putch('o'); putch('\n'); */ /* Super-Crappy Protocol works like this: Send kKEY Send pPLAINTEXT *** Encryption Occurs *** receive rRESPONSE e.g.: kE8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA\n p014BAF2278A69D331D5180103643E99A\n r6743C3D1519AB4F2CD9A78AB09A511BD\n */ char c; int ptr = 0; //Initial key aes_indep_init(); aes_indep_key(tmp); char state = 0; while(1){ c = getch(); if (c == 'x') { ptr = 0; state = IDLE; continue; } if (c == 'k') { ptr = 0; state = KEY; continue; } else if (c == 'p') { ptr = 0; state = PLAIN; continue; } else if (state == KEY) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, tmp); aes_indep_key(tmp); state = IDLE; } else { asciibuf[ptr++] = c; } } else if (state == PLAIN) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, pt); /* Do Encryption */ trigger_high(); aes_indep_enc(pt); /* encrypting the data block */ trigger_low(); /* Print Results */ hex_print(pt, 16, asciibuf); putch('r'); for(int i = 0; i < 32; i++){ putch(asciibuf[i]); } putch('\n'); state = IDLE; } else { if (ptr >= BUFLEN){ state = IDLE; } else { asciibuf[ptr++] = c; } } } } return 1; }
int main(int argc, char** argv) { unsigned int cmd = 0; unsigned int attr = ATTR_NONE; int ret = 0; int fd = 0; if (argc < 4) { fprintf(stderr,"usage: %s [get|set] filename [BASE_10_VALUE]\n",argv[0]); return -1; } fd = open(argv[2], 0); if (fd < 0) { fprintf(stderr,"can't open file: %s\n", argv[2]); return -1; } else { printf("opened file: %s\n", argv[2]); } if (strcasecmp(argv[1],"set") == 0) { attr = atoi(argv[3]); cmd = FAT_IOCTL_SET_ATTRIBUTES; } else if (strcasecmp(argv[1],"get") == 0) { cmd = FAT_IOCTL_GET_ATTRIBUTES; } else { fprintf(stderr,"unknown option %s\n",argv[1]); goto quit; } if ((ret = ioctl(fd,cmd,(unsigned long)&attr)) != 0) { fprintf(stderr,"ioctl: %s\n",strerror(errno)); } if (ret > -1) { if (argv[1][0] == 's') { printf("set file attributes to "); } else if (argv[1][0] == 'g') { printf("file attributes are "); } hex_print((char*)&attr,1); printf("\n"); } quit: close(fd); return ret; }
int main ( void ) { platform_init(); init_uart(); trigger_setup(); putch('P'); putch('a'); putch('s'); putch('s'); putch('w'); putch('o'); putch('r'); putch('d'); putch(':'); putch(' '); /* Require password to unlock simple serial protocol. */ trigger_high(); if (read_pass() || test_pass()) { _delay_ms(BAD_PASS_DELAY); trigger_low(); soft_reset(); } trigger_low(); putch('\n'); putch('W'); putch('e'); putch('l'); putch('c'); putch('o'); putch('m'); putch('e'); putch('!'); putch('\n'); /* Super-Crappy Protocol works like this: Send kKEY Send pPLAINTEXT *** Encryption Occurs *** receive rRESPONSE e.g.: kE8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA\n p014BAF2278A69D331D5180103643E99A\n r6743C3D1519AB4F2CD9A78AB09A511BD\n */ char c; int ptr = 0; //Initial key aes_indep_init(); aes_indep_key(tmp); char state = 0; while(1){ c = getch(); if (c == 'x') { ptr = 0; state = IDLE; continue; } if (c == 'k') { ptr = 0; state = KEY; continue; } else if (c == 'p') { ptr = 0; state = PLAIN; continue; } else if (state == KEY) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, tmp); aes_indep_key(tmp); state = IDLE; } else { asciibuf[ptr++] = c; } } else if (state == PLAIN) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, pt); /* Do Encryption */ trigger_high(); aes_indep_enc(pt); /* encrypting the data block */ trigger_low(); /* Print Results */ hex_print(pt, 16, asciibuf); putch('r'); for(int i = 0; i < 32; i++){ putch(asciibuf[i]); } putch('\n'); state = IDLE; } else { if (ptr >= BUFLEN){ state = IDLE; } else { asciibuf[ptr++] = c; } } } } return 1; }
void raw_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *sp, u_int hdrlen) { if (ndo->ndo_Xflag) { /* * Print the raw packet data in hex and ASCII. */ if (ndo->ndo_Xflag > 1) { /* * Include the link-layer header. */ hex_and_ascii_print(ndo, "\n\t", sp, h->caplen); } else { /* * Don't include the link-layer header - and if * we have nothing past the link-layer header, * print nothing. */ if (h->caplen > hdrlen) hex_and_ascii_print(ndo, "\n\t", sp + hdrlen, h->caplen - hdrlen); } } else if (ndo->ndo_xflag) { /* * Print the raw packet data in hex. */ if (ndo->ndo_xflag > 1) { /* * Include the link-layer header. */ hex_print(ndo, "\n\t", sp, h->caplen); } else { /* * Don't include the link-layer header - and if * we have nothing past the link-layer header, * print nothing. */ if (h->caplen > hdrlen) hex_print(ndo, "\n\t", sp + hdrlen, h->caplen - hdrlen); } } else if (ndo->ndo_Aflag) { /* * Print the raw packet data in ASCII. */ if (ndo->ndo_Aflag > 1) { /* * Include the link-layer header. */ ascii_print(ndo, sp, h->caplen); } else { /* * Don't include the link-layer header - and if * we have nothing past the link-layer header, * print nothing. */ if (h->caplen > hdrlen) ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen); } } }
void cfm_print(netdissect_options *ndo, const u_char *pptr, u_int length) { const struct cfm_common_header_t *cfm_common_header; uint8_t mdlevel_version, opcode, flags, first_tlv_offset; const struct cfm_tlv_header_t *cfm_tlv_header; const uint8_t *tptr, *tlv_ptr; const uint8_t *namesp; u_int names_data_remaining; uint8_t md_nameformat, md_namelength; const uint8_t *md_name; uint8_t ma_nameformat, ma_namelength; const uint8_t *ma_name; u_int hexdump, tlen, cfm_tlv_len, cfm_tlv_type, ccm_interval; union { const struct cfm_ccm_t *cfm_ccm; const struct cfm_lbm_t *cfm_lbm; const struct cfm_ltm_t *cfm_ltm; const struct cfm_ltr_t *cfm_ltr; } msg_ptr; tptr=pptr; cfm_common_header = (const struct cfm_common_header_t *)pptr; if (length < sizeof(*cfm_common_header)) goto tooshort; ND_TCHECK_SIZE(cfm_common_header); /* * Sanity checking of the header. */ mdlevel_version = EXTRACT_U_1(cfm_common_header->mdlevel_version); if (CFM_EXTRACT_VERSION(mdlevel_version) != CFM_VERSION) { ND_PRINT("CFMv%u not supported, length %u", CFM_EXTRACT_VERSION(mdlevel_version), length); return; } opcode = EXTRACT_U_1(cfm_common_header->opcode); ND_PRINT("CFMv%u %s, MD Level %u, length %u", CFM_EXTRACT_VERSION(mdlevel_version), tok2str(cfm_opcode_values, "unknown (%u)", opcode), CFM_EXTRACT_MD_LEVEL(mdlevel_version), length); /* * In non-verbose mode just print the opcode and md-level. */ if (ndo->ndo_vflag < 1) { return; } flags = EXTRACT_U_1(cfm_common_header->flags); first_tlv_offset = EXTRACT_U_1(cfm_common_header->first_tlv_offset); ND_PRINT("\n\tFirst TLV offset %u", first_tlv_offset); tptr += sizeof(struct cfm_common_header_t); tlen = length - sizeof(struct cfm_common_header_t); /* * Sanity check the first TLV offset. */ if (first_tlv_offset > tlen) { ND_PRINT(" (too large, must be <= %u)", tlen); return; } switch (opcode) { case CFM_OPCODE_CCM: msg_ptr.cfm_ccm = (const struct cfm_ccm_t *)tptr; if (first_tlv_offset < sizeof(*msg_ptr.cfm_ccm)) { ND_PRINT(" (too small 1, must be >= %lu)", (unsigned long) sizeof(*msg_ptr.cfm_ccm)); return; } if (tlen < sizeof(*msg_ptr.cfm_ccm)) goto tooshort; ND_TCHECK_SIZE(msg_ptr.cfm_ccm); ccm_interval = CFM_EXTRACT_CCM_INTERVAL(flags); ND_PRINT(", Flags [CCM Interval %u%s]", ccm_interval, flags & CFM_CCM_RDI_FLAG ? ", RDI" : ""); /* * Resolve the CCM interval field. */ if (ccm_interval) { ND_PRINT("\n\t CCM Interval %.3fs" ", min CCM Lifetime %.3fs, max CCM Lifetime %.3fs", ccm_interval_base[ccm_interval], ccm_interval_base[ccm_interval] * CCM_INTERVAL_MIN_MULTIPLIER, ccm_interval_base[ccm_interval] * CCM_INTERVAL_MAX_MULTIPLIER); } ND_PRINT("\n\t Sequence Number 0x%08x, MA-End-Point-ID 0x%04x", EXTRACT_BE_U_4(msg_ptr.cfm_ccm->sequence), EXTRACT_BE_U_2(msg_ptr.cfm_ccm->ma_epi)); namesp = msg_ptr.cfm_ccm->names; names_data_remaining = sizeof(msg_ptr.cfm_ccm->names); /* * Resolve the MD fields. */ md_nameformat = EXTRACT_U_1(namesp); namesp++; names_data_remaining--; /* We know this is != 0 */ if (md_nameformat != CFM_CCM_MD_FORMAT_NONE) { md_namelength = EXTRACT_U_1(namesp); namesp++; names_data_remaining--; /* We know this is !=0 */ ND_PRINT("\n\t MD Name Format %s (%u), MD Name length %u", tok2str(cfm_md_nameformat_values, "Unknown", md_nameformat), md_nameformat, md_namelength); /* * -3 for the MA short name format and length and one byte * of MA short name. */ if (md_namelength > names_data_remaining - 3) { ND_PRINT(" (too large, must be <= %u)", names_data_remaining - 2); return; } md_name = namesp; ND_PRINT("\n\t MD Name: "); switch (md_nameformat) { case CFM_CCM_MD_FORMAT_DNS: case CFM_CCM_MD_FORMAT_CHAR: safeputs(ndo, md_name, md_namelength); break; case CFM_CCM_MD_FORMAT_MAC: if (md_namelength == 6) { ND_PRINT("\n\t MAC %s", etheraddr_string(ndo, md_name)); } else { ND_PRINT("\n\t MAC (length invalid)"); } break; /* FIXME add printers for those MD formats - hexdump for now */ case CFM_CCM_MA_FORMAT_8021: default: print_unknown_data(ndo, md_name, "\n\t ", md_namelength); } namesp += md_namelength; names_data_remaining -= md_namelength; } else { ND_PRINT("\n\t MD Name Format %s (%u)", tok2str(cfm_md_nameformat_values, "Unknown", md_nameformat), md_nameformat); } /* * Resolve the MA fields. */ ma_nameformat = EXTRACT_U_1(namesp); namesp++; names_data_remaining--; /* We know this is != 0 */ ma_namelength = EXTRACT_U_1(namesp); namesp++; names_data_remaining--; /* We know this is != 0 */ ND_PRINT("\n\t MA Name-Format %s (%u), MA name length %u", tok2str(cfm_ma_nameformat_values, "Unknown", ma_nameformat), ma_nameformat, ma_namelength); if (ma_namelength > names_data_remaining) { ND_PRINT(" (too large, must be <= %u)", names_data_remaining); return; } ma_name = namesp; ND_PRINT("\n\t MA Name: "); switch (ma_nameformat) { case CFM_CCM_MA_FORMAT_CHAR: safeputs(ndo, ma_name, ma_namelength); break; /* FIXME add printers for those MA formats - hexdump for now */ case CFM_CCM_MA_FORMAT_8021: case CFM_CCM_MA_FORMAT_VID: case CFM_CCM_MA_FORMAT_INT: case CFM_CCM_MA_FORMAT_VPN: default: print_unknown_data(ndo, ma_name, "\n\t ", ma_namelength); } break; case CFM_OPCODE_LTM: msg_ptr.cfm_ltm = (const struct cfm_ltm_t *)tptr; if (first_tlv_offset < sizeof(*msg_ptr.cfm_ltm)) { ND_PRINT(" (too small 4, must be >= %lu)", (unsigned long) sizeof(*msg_ptr.cfm_ltm)); return; } if (tlen < sizeof(*msg_ptr.cfm_ltm)) goto tooshort; ND_TCHECK_SIZE(msg_ptr.cfm_ltm); ND_PRINT(", Flags [%s]", bittok2str(cfm_ltm_flag_values, "none", flags)); ND_PRINT("\n\t Transaction-ID 0x%08x, ttl %u", EXTRACT_BE_U_4(msg_ptr.cfm_ltm->transaction_id), EXTRACT_U_1(msg_ptr.cfm_ltm->ttl)); ND_PRINT("\n\t Original-MAC %s, Target-MAC %s", etheraddr_string(ndo, msg_ptr.cfm_ltm->original_mac), etheraddr_string(ndo, msg_ptr.cfm_ltm->target_mac)); break; case CFM_OPCODE_LTR: msg_ptr.cfm_ltr = (const struct cfm_ltr_t *)tptr; if (first_tlv_offset < sizeof(*msg_ptr.cfm_ltr)) { ND_PRINT(" (too small 5, must be >= %lu)", (unsigned long) sizeof(*msg_ptr.cfm_ltr)); return; } if (tlen < sizeof(*msg_ptr.cfm_ltr)) goto tooshort; ND_TCHECK_SIZE(msg_ptr.cfm_ltr); ND_PRINT(", Flags [%s]", bittok2str(cfm_ltr_flag_values, "none", flags)); ND_PRINT("\n\t Transaction-ID 0x%08x, ttl %u", EXTRACT_BE_U_4(msg_ptr.cfm_ltr->transaction_id), EXTRACT_U_1(msg_ptr.cfm_ltr->ttl)); ND_PRINT("\n\t Replay-Action %s (%u)", tok2str(cfm_ltr_replay_action_values, "Unknown", EXTRACT_U_1(msg_ptr.cfm_ltr->replay_action)), EXTRACT_U_1(msg_ptr.cfm_ltr->replay_action)); break; /* * No message decoder yet. * Hexdump everything up until the start of the TLVs */ case CFM_OPCODE_LBR: case CFM_OPCODE_LBM: default: print_unknown_data(ndo, tptr, "\n\t ", tlen - first_tlv_offset); break; } tptr += first_tlv_offset; tlen -= first_tlv_offset; while (tlen > 0) { cfm_tlv_header = (const struct cfm_tlv_header_t *)tptr; /* Enough to read the tlv type ? */ ND_TCHECK_1(cfm_tlv_header->type); cfm_tlv_type = EXTRACT_U_1(cfm_tlv_header->type); ND_PRINT("\n\t%s TLV (0x%02x)", tok2str(cfm_tlv_values, "Unknown", cfm_tlv_type), cfm_tlv_type); if (cfm_tlv_type == CFM_TLV_END) { /* Length is "Not present if the Type field is 0." */ return; } /* do we have the full tlv header ? */ if (tlen < sizeof(struct cfm_tlv_header_t)) goto tooshort; ND_TCHECK_LEN(tptr, sizeof(struct cfm_tlv_header_t)); cfm_tlv_len=EXTRACT_BE_U_2(cfm_tlv_header->length); ND_PRINT(", length %u", cfm_tlv_len); tptr += sizeof(struct cfm_tlv_header_t); tlen -= sizeof(struct cfm_tlv_header_t); tlv_ptr = tptr; /* do we have the full tlv ? */ if (tlen < cfm_tlv_len) goto tooshort; ND_TCHECK_LEN(tptr, cfm_tlv_len); hexdump = FALSE; switch(cfm_tlv_type) { case CFM_TLV_PORT_STATUS: if (cfm_tlv_len < 1) { ND_PRINT(" (too short, must be >= 1)"); return; } ND_PRINT(", Status: %s (%u)", tok2str(cfm_tlv_port_status_values, "Unknown", EXTRACT_U_1(tptr)), EXTRACT_U_1(tptr)); break; case CFM_TLV_INTERFACE_STATUS: if (cfm_tlv_len < 1) { ND_PRINT(" (too short, must be >= 1)"); return; } ND_PRINT(", Status: %s (%u)", tok2str(cfm_tlv_interface_status_values, "Unknown", EXTRACT_U_1(tptr)), EXTRACT_U_1(tptr)); break; case CFM_TLV_PRIVATE: if (cfm_tlv_len < 4) { ND_PRINT(" (too short, must be >= 4)"); return; } ND_PRINT(", Vendor: %s (%u), Sub-Type %u", tok2str(oui_values,"Unknown", EXTRACT_BE_U_3(tptr)), EXTRACT_BE_U_3(tptr), EXTRACT_U_1(tptr + 3)); hexdump = TRUE; break; case CFM_TLV_SENDER_ID: { u_int chassis_id_type, chassis_id_length; u_int mgmt_addr_length; if (cfm_tlv_len < 1) { ND_PRINT(" (too short, must be >= 1)"); goto next_tlv; } /* * Get the Chassis ID length and check it. * IEEE 802.1Q-2014 Section 21.5.3.1 */ chassis_id_length = EXTRACT_U_1(tptr); tptr++; tlen--; cfm_tlv_len--; if (chassis_id_length) { /* * IEEE 802.1Q-2014 Section 21.5.3.2: Chassis ID Subtype, references * IEEE 802.1AB-2005 Section 9.5.2.2, subsequently * IEEE 802.1AB-2016 Section 8.5.2.2: chassis ID subtype */ if (cfm_tlv_len < 1) { ND_PRINT("\n\t (TLV too short)"); goto next_tlv; } chassis_id_type = EXTRACT_U_1(tptr); cfm_tlv_len--; ND_PRINT("\n\t Chassis-ID Type %s (%u), Chassis-ID length %u", tok2str(cfm_tlv_senderid_chassisid_values, "Unknown", chassis_id_type), chassis_id_type, chassis_id_length); if (cfm_tlv_len < chassis_id_length) { ND_PRINT("\n\t (TLV too short)"); goto next_tlv; } /* IEEE 802.1Q-2014 Section 21.5.3.3: Chassis ID */ switch (chassis_id_type) { case CFM_CHASSIS_ID_MAC_ADDRESS: if (chassis_id_length != MAC_ADDR_LEN) { ND_PRINT(" (invalid MAC address length)"); hexdump = TRUE; break; } ND_PRINT("\n\t MAC %s", etheraddr_string(ndo, tptr + 1)); break; case CFM_CHASSIS_ID_NETWORK_ADDRESS: hexdump |= cfm_network_addr_print(ndo, tptr + 1, chassis_id_length); break; case CFM_CHASSIS_ID_INTERFACE_NAME: /* fall through */ case CFM_CHASSIS_ID_INTERFACE_ALIAS: case CFM_CHASSIS_ID_LOCAL: case CFM_CHASSIS_ID_CHASSIS_COMPONENT: case CFM_CHASSIS_ID_PORT_COMPONENT: safeputs(ndo, tptr + 1, chassis_id_length); break; default: hexdump = TRUE; break; } cfm_tlv_len -= chassis_id_length; tptr += 1 + chassis_id_length; tlen -= 1 + chassis_id_length; } /* * Check if there is a Management Address. * IEEE 802.1Q-2014 Section 21.5.3.4: Management Address Domain Length * This and all subsequent fields are not present if the TLV length * allows only the above fields. */ if (cfm_tlv_len == 0) { /* No, there isn't; we're done. */ break; } /* Here mgmt_addr_length stands for the management domain length. */ mgmt_addr_length = EXTRACT_U_1(tptr); tptr++; tlen--; cfm_tlv_len--; ND_PRINT("\n\t Management Address Domain Length %u", mgmt_addr_length); if (mgmt_addr_length) { /* IEEE 802.1Q-2014 Section 21.5.3.5: Management Address Domain */ if (cfm_tlv_len < mgmt_addr_length) { ND_PRINT("\n\t (TLV too short)"); goto next_tlv; } cfm_tlv_len -= mgmt_addr_length; /* * XXX - this is an OID; print it as such. */ hex_print(ndo, "\n\t Management Address Domain: ", tptr, mgmt_addr_length); tptr += mgmt_addr_length; tlen -= mgmt_addr_length; /* * IEEE 802.1Q-2014 Section 21.5.3.6: Management Address Length * This field is present if Management Address Domain Length is not 0. */ if (cfm_tlv_len < 1) { ND_PRINT(" (Management Address Length is missing)"); hexdump = TRUE; break; } /* Here mgmt_addr_length stands for the management address length. */ mgmt_addr_length = EXTRACT_U_1(tptr); tptr++; tlen--; cfm_tlv_len--; ND_PRINT("\n\t Management Address Length %u", mgmt_addr_length); if (mgmt_addr_length) { /* IEEE 802.1Q-2014 Section 21.5.3.7: Management Address */ if (cfm_tlv_len < mgmt_addr_length) { ND_PRINT("\n\t (TLV too short)"); return; } cfm_tlv_len -= mgmt_addr_length; /* * XXX - this is a TransportDomain; print it as such. */ hex_print(ndo, "\n\t Management Address: ", tptr, mgmt_addr_length); tptr += mgmt_addr_length; tlen -= mgmt_addr_length; } } break; } /* * FIXME those are the defined TLVs that lack a decoder * you are welcome to contribute code ;-) */ case CFM_TLV_DATA: case CFM_TLV_REPLY_INGRESS: case CFM_TLV_REPLY_EGRESS: default: hexdump = TRUE; break; } /* do we want to see an additional hexdump ? */ if (hexdump || ndo->ndo_vflag > 1) print_unknown_data(ndo, tlv_ptr, "\n\t ", cfm_tlv_len); next_tlv: tptr+=cfm_tlv_len; tlen-=cfm_tlv_len; } return; tooshort: ND_PRINT("\n\t\t packet is too short"); return; trunc: ND_PRINT("%s", tstr); }