int main(int argc, char *argv[]) { uint32_t n = 1; uint32_t bn; char *char_list = "12345"; long long int nn = 1; printf("ascii\n"); printf("'1' : 0x%x\n", '1'); printf("'2' : 0x%x\n", '2'); printf("'a' : 0x%x\n", 'a'); printf("'b' : 0x%x\n", 'b'); printf("'A' : 0x%x\n", 'A'); printf("'B' : 0x%x\n", 'B'); printf("\nuint32_t 1\n"); print_bytes(&n, sizeof(n)); bn = htonl(n); printf("\nBigEndian uint32_t 1\n"); print_bytes(&bn, sizeof(bn)); printf("\n\"12345\"\n"); print_bytes(char_list, sizeof(char_list)); nn = 1; nn = nn << 32; nn |= 2; /* nn == 00 00 00 01 00 00 00 02 */ printf("\n\"long long int 1\"\n"); print_bytes(&nn, sizeof(nn)); return 0; }
control_ret_t control_query_version(control_version_t *version) { unsigned b[XSCOPE_UPLOAD_MAX_WORDS]; size_t len = control_xscope_create_upload_buffer(b, CONTROL_GET_VERSION, CONTROL_SPECIAL_RESID, NULL, sizeof(control_version_t)); DBG(printf("%d: send version command: ", num_commands)); DBG(print_bytes((unsigned char*)b, len)); record_count = 0; if (xscope_ep_request_upload(len, (unsigned char*)b) != XSCOPE_EP_SUCCESS) { printf("xscope_ep_request_upload failed\n"); return CONTROL_ERROR; } // wait for response on xSCOPE probe while (record_count == 0) { pause_short(); } DBG(printf("response: ")); DBG(print_bytes(last_response, last_response_length)); *version = *(control_version_t*)(last_response + sizeof(struct control_xscope_response)); num_commands++; return CONTROL_SUCCESS + last_response_struct->ret; }
control_ret_t control_write_command(control_resid_t resid, control_cmd_t cmd, const uint8_t payload[], size_t payload_len) { unsigned b[XSCOPE_UPLOAD_MAX_WORDS]; size_t len = control_xscope_create_upload_buffer(b, CONTROL_CMD_SET_WRITE(cmd), resid, payload, payload_len); if (upload_len_exceeds_xscope_limit(len)) return CONTROL_DATA_LENGTH_ERROR; DBG(printf("%u: send write command: ", num_commands)); DBG(print_bytes((unsigned char*)b, len)); record_count = 0; if (xscope_ep_request_upload(len, (unsigned char*)b) != XSCOPE_EP_SUCCESS) { printf("xscope_ep_request_upload failed\n"); return CONTROL_ERROR; } // wait for response on xSCOPE probe while (record_count == 0) { pause_short(); } DBG(printf("response: ")); DBG(print_bytes(last_response, XSCOPE_HEADER_BYTES)); num_commands++; return CONTROL_SUCCESS + last_response_struct->ret; }
int main(void) { byte key1[16], key2[16]; bbkem_ciphertext ct; bbkem_public params; bbkem_msk msk; bbkem_pk upk; const char *id = "Bob"; cprng_get_bytes(key1, 16); generate_params(&msk, ¶ms); print("Generating parameters...\n"); derive_private_key(&upk, &msk, ¶ms, id); print("Group Private Key: \n"); PRINT_G2("d0: ", upk.d0); PRINT_G2("d1: ", upk.d1); encapsulate_key(key1, &ct, ¶ms, id); print("Generated symmetric key: \n"); print("k: "); print_bytes(key1, 16); print("\n"); print("Ciphertext: \n"); PRINT_G1("c0:", ct.c0); PRINT_G1("c1:", ct.c1); decapsulate_key(key2, &ct, ¶ms, &upk); print("Restored symmetric key: "); print_bytes(key2, 16); print("\n"); }
control_ret_t control_read_command(control_resid_t resid, control_cmd_t cmd, uint8_t payload[], size_t payload_len) { unsigned b[XSCOPE_UPLOAD_MAX_WORDS]; size_t len = control_xscope_create_upload_buffer(b, CONTROL_CMD_SET_READ(cmd), resid, NULL, payload_len); DBG(printf("%d: send read command: ", num_commands)); DBG(print_bytes((unsigned char*)b, len)); record_count = 0; if (xscope_ep_request_upload(len, (unsigned char*)b) != XSCOPE_EP_SUCCESS) { printf("xscope_ep_request_upload failed\n"); return CONTROL_ERROR; } // wait for response on xSCOPE probe while (record_count == 0) { pause_short(); } DBG(printf("response: ")); DBG(print_bytes(last_response, last_response_length)); // ignore returned payload length, use one supplied in request memcpy(payload, last_response + sizeof(struct control_xscope_response), payload_len); num_commands++; return CONTROL_SUCCESS + last_response_struct->ret; }
void decode() { char buffer[1024]; char input_block[44]; // space for terminating null size_t block_index = 0; input_block[43] = '\0'; // make sure the input_block is null-terminated mpz_t bignum; mpz_init(bignum); while (true) { size_t buffer_count = fread(buffer, 1, 1024, stdin); if (buffer_count == 0) { break; } for (int i=0; i<buffer_count; i++) { char c = buffer[i]; if (is_base62_char(c)) { input_block[block_index] = c; block_index = (block_index + 1) % 43; // If we just filled the block, serialize it. if (block_index == 0) { print_bytes(&bignum, input_block); } } } } // Decode any remaining characters. if (block_index > 0) { input_block[block_index] = '\0'; // make sure it's null-terminated again print_bytes(&bignum, input_block); } }
void cmd_transfer(int argc, char **argv) { int res; char *hello = "Hello"; if (spi_master != 1) { puts("error: node is not initialized as master, please do so first"); return; } if (argc < 2) { puts("No data to transfer given, will transfer 'Hello' to device"); } else { hello = argv[1]; } /* do the actual data transfer */ gpio_clear(spi_cs); res = spi_transfer_bytes(spi_dev, hello, buffer, strlen(hello)); gpio_set(spi_cs); /* look at the results */ if (res < 0) { printf("error: unable to transfer data to slave (code: %i)\n", res); } else { printf("Transfered %i bytes:\n", res); print_bytes("MOSI", hello, res); print_bytes("MISO", buffer, res); } }
int generate_key(char *secret_key_name, char *public_key_name) { int error = 0; int written; unsigned char public_key[crypto_sign_ed25519_PUBLICKEYBYTES]; unsigned char secret_key[crypto_sign_ed25519_SECRETKEYBYTES]; FILE *public_key_file = NULL; FILE *secret_key_file = NULL; error = crypto_sign_ed25519_keypair(public_key, secret_key); if (error != 0) { fprintf(stderr, "Could not generate key pair\n"); error = -1; goto out; } public_key_file = fopen(public_key_name, "w"); if (public_key_file == NULL) { perror("public key file"); error = -1; goto out; } secret_key_file = fopen(secret_key_name, "w"); if (public_key_file == NULL) { perror("secret key file"); error = -1; goto out; } written = fwrite(public_key, sizeof(public_key), 1, public_key_file); if (written != 1 || ferror(public_key_file) != 0) { perror("writing to public key file"); error = -1; goto out; } written = fwrite(secret_key, sizeof(secret_key), 1, secret_key_file); if (written != 1 || ferror(secret_key_file) != 0) { perror("writing to secret key file"); error = -1; goto out; } #if DEBUG printf("Public key:\n"); print_bytes(public_key, sizeof(public_key)); printf("Secret key:\n"); print_bytes(secret_key, sizeof(secret_key)); #endif out: fclose(public_key_file); fclose(secret_key_file); return error; }
int main(void) { int i, len, n; const int num_tests = 500000; printf("Encode test #1:\n"); len = sizeof(enc_test_1); n = encode_4b6b(enc_test_1, result, len); print_bytes(result, n); printf("Encode test #2:\n"); len = sizeof(enc_test_2); n = encode_4b6b(enc_test_2, result, len); print_bytes(result, n); printf("Decode test #1:\n"); len = sizeof(dec_test_1); n = decode_4b6b(dec_test_1, result, len); if (n >= 0) { print_bytes(result, n); } else { printf("Decoding error!\n"); return 1; } printf("Decode test #2:\n"); len = sizeof(dec_test_2); n = decode_4b6b(dec_test_2, result, len); if (n >= 0) { print_bytes(result, n); } else { printf("Decoding error!\n"); return 1; } printf("Inverse test:\n"); for (i = 0; i < num_tests; ++i) { randomly_fill(test, 128); n = encode_4b6b(test, tmp, 128); len = decode_4b6b(tmp, result, n); if (len != 128) { printf("Decoding error!\n"); return 1; } if (memcmp(result, test, 128) != 0) { printf("Decoded to wrong value!\n"); return 1; } } return 0; }
static void norx_debug(norx_state_t state, const uint8_t *in, size_t inlen, const uint8_t *out, size_t outlen) { if (in != NULL && inlen > 0) { printf("In:\n"); print_bytes(in, inlen); } if (out != NULL && outlen > 0) { printf("Out:\n"); print_bytes(out, outlen); } printf("State:\n"); norx_print_state(state); }
static MRS_INLINE void mrs_decrypt_lastblock(mrs_state_t state, unsigned char * out, const unsigned char * in, size_t inlen) { size_t i; mrs_word_t * S = state->S; uint8_t lastblock[BYTES(MRS_R)]; mrs_permute(state); for(i = 0; i < WORDS(MRS_R); ++i) { STORE(lastblock + i * BYTES(MRS_W), S[i]); } /* undo padding */ memcpy(lastblock, in, inlen); /*lastblock[inlen] ^= 0x01; lastblock[BYTES(MRS_R) - 1] ^= 0x80;*/ for (i = 0; i < WORDS(MRS_R); ++i) { const mrs_word_t c = LOAD(lastblock + i * BYTES(MRS_W)); STORE(lastblock + i * BYTES(MRS_W), S[i] ^ c); S[i] = c; } memcpy(out, lastblock, inlen); #if defined(MRS_DEBUG) printf("DECRYPTING LASTBLOCK:\n"); print_bytes(lastblock, BYTES(MRS_R)); printf("STATE:\n"); print_state(state->S); #endif burn(lastblock, 0, sizeof lastblock); }
void print_array(FILE * o, char * ptr, char * name, int l) { fprintf(o, "typedef char %s_t[%d];\n", name, l); fprintf(o, "static char %s[] = ", name); print_bytes(o, ptr, l, l + (rand() & 0xf)); fprintf(o, ";\n"); }
static int print_tpm_pcrs(TSS_HCONTEXT hContext, TSS_HOBJECT hTPM) { UINT32 num_pcrs; int i; if (get_tpm_capability(hContext, hTPM, TSS_TPMCAP_PROPERTY, TSS_TPMCAP_PROP_PCR, &num_pcrs, sizeof (num_pcrs))) return (ERR_FAIL); (void) printf(gettext("Platform Configuration Registers (%u)\n"), num_pcrs); /* Print each PCR */ for (i = 0; i < num_pcrs; i++) { TSS_RESULT ret; UINT32 datalen; BYTE *data; ret = Tspi_TPM_PcrRead(hTPM, i, &datalen, &data); if (ret) { print_error(ret, gettext("Read PCR")); return (ret); } (void) printf("\tPCR %u:\t", i); print_bytes(data, datalen, FALSE); ret = Tspi_Context_FreeMemory(hContext, data); if (ret) { print_error(ret, gettext("Free PCR memory")); return (ret); } } return (0); }
int main() { char input[2048], c; unsigned char *bytes, key, *k; int i; i = 0; while ((c = getchar()) != EOF) { input[i++] = c; } input[i--] = 0; i = (i + 1) / 2; bytes = malloc(sizeof(unsigned char) * i); str_to_bytes(input, bytes, i); key = break_singlechar_xor(bytes, i); k = malloc(sizeof(char)+1); k[0] = key; k[1] = 0; decrypt_repeat_xor(bytes, k, i); printf("\nPrinting bytes:\n"); print_bytes(bytes); printf("\nPrinting clear text\n"); printf("%s\n", bytes); return 0; }
int main(int argc, char **argv) { int n, len; if (argc <= 1 || argc >= BUF_SIZE) { fprintf(stderr, "Usage: %s bytes in hex ...\n", argv[0]); return 1; } --argc; ++argv; for (n = 0; n < argc; ++n) { int v; char *endp; v = strtol(*argv, &endp, 16); if (*endp != '\0' || v < 0 || v > 0xFF) { fprintf(stderr, "Argument '%s' is not a hex byte\n", *argv); return 1; } input[n] = v; ++argv; } len = decode_4b6b_length(n); memset(output, 0, len); if (decode_4b6b(input, output, n)) printf("Decoding FAILED\n"); print_bytes(output, len); return 0; }
void print_token(html5token token) { switch (token.type) { case START_TAG: if (first) { first = false; } else { putc('\n', stdout); } printf("%s<", indent); print_bytes(token.start, token.end); putc('>', stdout); strcat(indent, " "); break; case END_TAG: if (first) { first = false; } else { putc('\n', stdout); } printf("%s</", indent); print_bytes(token.start, token.end); putc('>', stdout); strcat(indent, " "); break; case ATTRIBUTE: printf("\n%s", indent); print_bytes(token.start, token.end); break; case VALUE: putc('=', stdout); print_bytes(token.start, token.end); break; case COMMENT: printf("\n%s<!-- ", indent); print_bytes(token.start, token.end); printf(" -->"); break; default: printf("\nhere %d, %d\n", token.type, token.end - token.start); break; } }
void print_hex(void *data, uint32_t length) { if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0) print_dwords(data, length); else print_bytes(data, length); }
void print_huffman_codes(huffman_code huff_code[], size_t size) { size_t i; for (i = 0; i < size; i++) { printf("%d: code = ", i); print_bytes(huff_code[i].code, sizeof(size_t)); printf(", code_len = %d\n", huff_code[i].code_len); } }
void send_packet(uint8_t *buf, size_t len) { int n; buf[len - 1] = crc8(buf, len - 1); print_time(); #if VERBOSE printf("Transmitting packet:\n"); #else printf("> "); #endif print_bytes(buf, len); n = encode_4b6b(buf, packet, len); #if VERBOSE printf("4b/6b encoding:\n"); print_bytes(packet, n); #endif radio_transmit(packet, n); }
void print_bytes(void *value, size_t n) { if(n == 1) { printf(" %02X", (*(char*)value) & 0x000000FF); } else if(n == 2) { printf(" %02X", (*(char*)value) & 0x000000FF); printf(" %02X", (*(char*)(value + 1)) & 0x000000FF); } else if(n == 3) { printf(" %02X", (*(char*)value) & 0x000000FF); printf(" %02X", (*(char*)(value + 1)) & 0x000000FF); printf(" %02X", (*(char*)(value + 2)) & 0x000000FF); } else if(n == 4) { printf(" %02X", (*(char*)value) & 0x000000FF); printf(" %02X", (*(char*)(value + 1)) & 0x000000FF); printf(" %02X", (*(char*)(value + 2)) & 0x000000FF); printf(" %02X", (*(char*)(value + 3)) & 0x000000FF); } else if(n > 4) { print_bytes(value, 4); print_bytes(value + 4, n - 4); } }
int put_data(int broadcast, int type) { int len = offsetof(struct arp_pkt, send_mac); memcpy(arp_info.src_mac, host_mac, sizeof(arp_info.src_mac)); if(type) memset(arp_info.dst_mac, 0xff, sizeof(arp_info.dst_mac)); else { if(broadcast) memset(arp_info.dst_mac, 0xff, sizeof(arp_info.dst_mac)); else memcpy(arp_info.dst_mac, gw_mac, sizeof(arp_info.dst_mac)); } arp_info.ptype = htons(0x0806); arp_info.hd_type = htons(0x0001); arp_info.prot_type = htons(0x0800); arp_info.hd_size = 6; arp_info.prot_size = 4; arp_info.opcode = htons(0x0002); memcpy(arp_info.send_mac, host_mac, sizeof(arp_info.send_mac)); arp_info.send_ip = cheat_ip; if(type) { memset(arp_info.tag_mac, 0xff, sizeof(arp_info.tag_mac)); arp_info.tag_ip = gw_ip; } else { if(broadcast) { memset(arp_info.tag_mac, 0xff, sizeof(arp_info.tag_mac)); arp_info.tag_ip = broad_ip; } else { memcpy(arp_info.tag_mac, gw_mac, sizeof(arp_info.tag_mac)); arp_info.tag_ip = gw_ip; } } memcpy(arp_data, &arp_info, len); memcpy(&arp_data[len], arp_info.send_mac, sizeof(arp_info.send_mac)); len += sizeof(arp_info.send_mac); memcpy(&arp_data[len], &arp_info.send_ip, sizeof(arp_info.send_ip)); len += sizeof(arp_info.send_ip); memcpy(&arp_data[len], arp_info.tag_mac, sizeof(arp_info.tag_mac)); len += sizeof(arp_info.tag_mac); memcpy(&arp_data[len], &arp_info.tag_ip, sizeof(arp_info.tag_ip)); len += sizeof(arp_info.tag_ip); //print_bytes((unsigned char *)&arp_info, sizeof(arp_info)); print_bytes(arp_data, len); return len; }
uint8_t *recv_packet(int timeout) { __xdata static uint8_t bytes[256]; int length, n; uint8_t crc; length = radio_receive(packet, sizeof(packet), timeout); if (length == 0) { #if VERBOSE print_time(); printf("Receive timeout\n"); #endif return 0; } #if VERBOSE print_time(); printf("Received %d-byte packet:\n", length); print_bytes(packet, length); #endif memset(bytes, 0, sizeof(bytes)); n = decode_4b6b(packet, bytes, length); #if VERBOSE printf("4b/6b decoding%s:\n", n == -1 ? " FAILED" : ""); #else print_time(); if (n == -1) { printf("4b/6b decoding failed\n"); return 0; } printf("< "); #endif print_bytes(bytes, n); if (n < 2) return 0; crc = crc8(bytes, n - 1); if (bytes[n - 1] != crc) printf("CRC should be %02X\n", crc); return bytes; }
int main() { srand(9); for (size_t i = 0; i < sizeof(key); ++i) { key[i] = randn(256) - 128; } printf("Original key:\n"); print_bytes(key, 16); size_t n; char *e = encode_user_data("", &n); error err; memset(e + 16, '\0', 16); memcpy(e + 32, e, 16); verify(e, n, &err); char *p = err.msg + strlen("Invalid message: "); xor(p, p + 32, 16, p); printf("Reversed key:\n"); print_bytes(p, 16); free(e); }
// // Sends a USB message to the device from a given buffer. // int send_usb_msgbuf(struct usb_dev_handle *h, char *msg, int msgsize) { int bytes_written = 0; debug_printf(2, "--> "); print_bytes(2, msg, msgsize); bytes_written = usb_control_msg(h, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x200, 0, msg, msgsize, USB_TIMEOUT); //assert(bytes_written == msgsize); return bytes_written; }
int cmd_print(int argc, char **argv) { if (spi_master != 0) { puts("error: node is not initialized as slave"); return 1; } else { printf("Received %i bytes:\n", rx_counter); print_bytes("MOSI", rx_buffer, rx_counter); } rx_counter = 0; memset(&rx_buffer, 0, 256); return 0; }
static void print_boot(smbios_hdl_t *shp, FILE *fp) { smbios_boot_t b; (void) smbios_info_boot(shp, &b); desc_printf(smbios_boot_desc(b.smbt_status), fp, " Boot Status Code: 0x%x", b.smbt_status); if (b.smbt_size != 0) { oprintf(fp, " Boot Data (%lu bytes):\n", (ulong_t)b.smbt_size); print_bytes(b.smbt_data, b.smbt_size, fp); } }
void print_mbuf(const struct os_mbuf *om) { int colon; colon = 0; while (om != NULL) { if (colon) { console_printf(":"); } else { colon = 1; } print_bytes(om->om_data, om->om_len); om = SLIST_NEXT(om, om_next); } }
int main(void) { uint8_t pt1[16]; int equ; memcpy(pt1, pt, 16); Noekeon(key, pt1); equ = memcmp (pt1, ct, 16)==0; printf ("\nEncryption : %s : ", equ ? "OK" : "FAILED"); print_bytes("CT", pt1, 16); return 0; }
static MRS_INLINE void mrs_absorb_block(mrs_state_t state, const unsigned char * in) { size_t i; mrs_word_t * S = state->S; mrs_permute(state); for (i = 0; i < WORDS(MRS_B); ++i) { S[i] ^= LOAD(in + i * BYTES(MRS_W)); } #if defined(MRS_DEBUG) printf("ABSORBING BLOCK:\n"); print_bytes(in, BYTES(MRS_B)); printf("STATE:\n"); print_state(state->S); #endif }
static MRS_INLINE void mrs_encrypt_block(mrs_state_t state, unsigned char * out, const unsigned char * in) { size_t i; mrs_word_t * S = state->S; mrs_permute(state); for (i = 0; i < WORDS(MRS_R); ++i) { S[i] ^= LOAD(in + i * BYTES(MRS_W)); STORE(out + i * BYTES(MRS_W), S[i]); } #if defined(MRS_DEBUG) printf("ENCRYPTING BLOCK:\n"); print_bytes(in, BYTES(MRS_R)); printf("STATE:\n"); print_state(state->S); #endif }