CompareResult CmpCacheFind(CmpCache_p *cache, Term_p t1, DerefType d1, Term_p t2, DerefType d2) { QuadKey key; QuadTree_p handle; CompareResult res = to_unknown; bool nat_order; if(TermIsVar(t1)||TermIsVar(t2)) { return to_unknown; } nat_order = prepare_key(t1,d1,t2,d2,&key); handle = QuadTreeFind(cache, &key); if(nat_order) { if(handle) { res = handle->val.i_val; } } else { if(handle) { res = POInverseRelation(handle->val.i_val); } } return res; }
void main(void){ char key[26]="ilovexingxing"; int rst=prepare_key(key); printf("rst=%i, key=%s\n",rst,key ); char message[]="i love xingxing"; encrypt(message,key); printf("encrypted message : %s\n",message ); decrypt(message,key); printf("decrypted message : %s\n",message ); }
void main(int argc, char* argv[]) { struct block key; FILE *fp1,*fp2; fp1=fopen(argv[1],"rb"); fp2=fopen(argv[3],"wb"); // Converting the key provided as string into key object. key=prepare_key(argv[2]); decrypt_file(fp1,key,fp2); fclose(fp1); fclose(fp2); }
int main(int argc, char *argv[]) { char keys[27] = "TRAILBLAZERS"; char data[] = "abcdefg"; prepare_key(keys); puts(keys); encrypt(data, keys); puts(data); decrypt(data, keys); puts(data); return 0; }
static void ffi_open_fn(VMState *state, CallInfo *info) { VM_ASSERT(info->args_len == 1, "wrong arity: expected 1, got %i", info->args_len); Object *root = state->root; Object *string_base = state->shared->vcache.string_base; Object *array_base = state->shared->vcache.array_base; Object *ffi = AS_OBJ(OBJECT_LOOKUP(root, ffi)); Object *handle_base = AS_OBJ(OBJECT_LOOKUP(ffi, handle)); StringObject *sarg = (StringObject*) obj_instance_of(OBJ_OR_NULL(load_arg(state->frame, INFO_ARGS_PTR(info)[0])), string_base); VM_ASSERT(sarg, "argument to ffi.open must be string!"); Object *libmap = AS_OBJ(OBJECT_LOOKUP(ffi, library_map)); char *file = sarg->value; bool file_found = false; FastKey file_key = prepare_key(file, strlen(file)); Value mapping = object_lookup_p(libmap, &file_key, &file_found); const char **file_list_ptr = NULL; int file_list_len = 0; if (file_found) { ArrayObject *aobj = (ArrayObject*) obj_instance_of(OBJ_OR_NULL(mapping), array_base); StringObject *sobj = (StringObject*) obj_instance_of(OBJ_OR_NULL(mapping), string_base); if (aobj) { file_list_len = aobj->length; file_list_ptr = malloc(sizeof(char*) * file_list_len); for (int i = 0; i < file_list_len; i++) { StringObject *file = (StringObject*) obj_instance_of(OBJ_OR_NULL(aobj->ptr[i]), string_base); VM_ASSERT(file, "library_map sub-entries must be string"); file_list_ptr[i] = my_asprintf("%s", file->value); // outside gc, make copy } } else if (sobj) { file_list_len = 1; file_list_ptr = malloc(sizeof(char*) * 1); file_list_ptr[0] = my_asprintf("%s", sobj->value); } else VM_ASSERT(false, "library_map entries must be string or array"); } else { file_list_len = 1; file_list_ptr = malloc(sizeof(char*) * 1); file_list_ptr[0] = file; } void *dlptr = my_dlopen(file_list_len, file_list_ptr); Object *handle_obj = AS_OBJ(make_object(state, handle_base, false)); handle_obj->flags |= OBJ_FROZEN; OBJECT_SET(state, handle_obj, pointer, make_ptr(state, dlptr)); vm_return(state, info, OBJ2VAL(handle_obj)); }
CString GetLink(int nIndex) { CString str = ""; rc4_key key; sPassword[16] = (unsigned char)(nIndex * 5); prepare_key(sPassword, 17, &key); char *sBuf = (char*)malloc(option.link_length); memcpy(sBuf, sLinksBuf + nIndex * option.link_length, option.link_length); rc4((unsigned char *)sBuf, option.link_length, &key); memset(&key, 0, sizeof(rc4_key )); str = sBuf; free(sBuf); return str; }
bool wpa_encr_key_data(eapol_wpa_key_header_t *body, uint16 key_info, uint8 *ekey, uint8 *gtk, uint8 *data, uint8 *encrkey, rc4_ks_t *rc4key) { uint16 len; switch (key_info & (WPA_KEY_DESC_V1 | WPA_KEY_DESC_V2)) { case WPA_KEY_DESC_V1: if (gtk) len = ntoh16_ua((uint8 *)&body->key_len); else len = ntoh16_ua((uint8 *)&body->data_len); /* create the iv/ptk key */ bcopy(body->iv, encrkey, 16); bcopy(ekey, &encrkey[16], 16); /* encrypt the key data */ prepare_key(encrkey, 32, rc4key); rc4(data, WPA_KEY_DATA_LEN_256, rc4key); /* dump 256 bytes */ rc4(body->data, len, rc4key); break; case WPA_KEY_DESC_V2: case WPA_KEY_DESC_V3: len = ntoh16_ua((uint8 *)&body->data_len); /* pad if needed - min. 16 bytes, 8 byte aligned */ /* padding is 0xdd followed by 0's */ if (len < 2*AKW_BLOCK_LEN) { body->data[len] = WPA2_KEY_DATA_PAD; bzero(&body->data[len+1], 2*AKW_BLOCK_LEN - (len+1)); len = 2*AKW_BLOCK_LEN; } else if (len % AKW_BLOCK_LEN) { body->data[len] = WPA2_KEY_DATA_PAD; bzero(&body->data[len+1], AKW_BLOCK_LEN - ((len+1) % AKW_BLOCK_LEN)); len += AKW_BLOCK_LEN - (len % AKW_BLOCK_LEN); } if (aes_wrap(WPA_MIC_KEY_LEN, ekey, len, body->data, body->data)) { return FALSE; } len += 8; hton16_ua_store(len, (uint8 *)&body->data_len); break; default: return FALSE; } return TRUE; }
/* Decrypt a key data from a WPA key message */ bool BCMROMFN(wpa_decr_key_data)(eapol_wpa_key_header_t *body, uint16 key_info, uint8 *ekey, uint8 *gtk, uint8 *data, uint8 *encrkey, rc4_ks_t *rc4key) { uint16 len; switch (key_info & (WPA_KEY_DESC_V1 | WPA_KEY_DESC_V2)) { case WPA_KEY_DESC_V1: bcopy(body->iv, encrkey, WPA_MIC_KEY_LEN); bcopy(ekey, &encrkey[WPA_MIC_KEY_LEN], WPA_MIC_KEY_LEN); /* decrypt the key data */ prepare_key(encrkey, WPA_MIC_KEY_LEN*2, rc4key); rc4(data, WPA_KEY_DATA_LEN_256, rc4key); /* dump 256 bytes */ if (gtk) len = ntoh16_ua((uint8 *)&body->key_len); else len = ntoh16_ua((uint8 *)&body->data_len); rc4(body->data, len, rc4key); if (gtk) bcopy(body->data, gtk, len); break; case WPA_KEY_DESC_V2: case WPA_KEY_DESC_V3: len = ntoh16_ua((uint8 *)&body->data_len); if (aes_unwrap(WPA_MIC_KEY_LEN, ekey, len, body->data, gtk ? gtk : body->data)) { return FALSE; } break; default: return FALSE; } return TRUE; }
int main (int argc, char **argv) { Elf32_Phdr * phdr; Elf32_Ehdr * ehdr; rc4_key key; int fd, i = 0, off; char * file = NULL, * passw = NULL, pass[256], * ptr, * buf; struct stat st; if (argc == 2) file = argv[1]; else usage(argv[0]); do { if (i) printf("Passphrases don't match.\n"); if ((passw = getpass("Passphrase: ")) == NULL) fatal("Bad pass"); memcpy(pass, passw, strlen(passw)); if ((passw = getpass("Confirm phrase: ")) == NULL) fatal("Bad pass"); i = 1; } while (STRCMP(passw, !=, pass)); memset(pass, 0x00, sizeof(pass)); if ((fd = open(file, O_RDWR, 0)) == -1) fatal("open host file"); if (fstat(fd, &st) < 0) fatal("stat host file"); if ((ptr = mmap(NULL, st.st_size, (PROT_READ|PROT_WRITE), MAP_SHARED, fd, 0)) == (void *)(-1)) fatal("mmap failed"); ehdr = (Elf32_Ehdr *)ptr; phdr = (Elf32_Phdr *)(ptr + ehdr->e_phoff); for (i = 0; i < ehdr->e_phnum; i++, phdr++) if (phdr->p_type == PT_LOAD) break; off = ehdr->e_entry - phdr->p_vaddr; buf = (char *)(ptr + off); prepare_key(passw, strlen(passw), &key); rc4(buf, phdr->p_filesz - off, &key); return 0; }
bool CmpCacheInsert(CmpCache_p *cache, Term_p t1, DerefType d1, Term_p t2, DerefType d2, CompareResult insert) { QuadKey key; IntOrP val; bool nat_order, res; QuadTree_p handle; assert(insert!=to_unknown); /* printf("%p:%d:%p:%d:%d\n", t1,d1,t2,d2,insert); */ if(TermIsVar(t1)||TermIsVar(t2)) { return false; } nat_order = prepare_key(t1,d1,t2,d2,&key); if(nat_order) { val.i_val = insert; } else { val.i_val = POInverseRelation(insert); } handle = QuadTreeFind(cache, &key); if(handle) { switch(handle->val.i_val) { case to_notgteq: /* printf("notgteq=%ld\n", val.i_val); */ if(val.i_val==to_notleeq) { handle->val.i_val = to_uncomparable; } else { assert((val.i_val==to_notgteq)|| (val.i_val==to_lesser)|| (val.i_val==to_uncomparable)); handle->val.i_val = val.i_val; } break; case to_notleeq: /* printf("notleeq=%ld\n", val.i_val); */ if(val.i_val==to_notgteq) { handle->val.i_val = to_uncomparable; } else { assert((val.i_val==to_notleeq)|| (val.i_val==to_greater)|| (val.i_val==to_uncomparable)); handle->val.i_val = val.i_val; } break; default: assert((handle->val.i_val==val.i_val) ||((val.i_val==to_notgteq) &&(handle->val.i_val==to_lesser)) ||((val.i_val==to_notleeq) &&(handle->val.i_val==to_greater))); break; } return false; } else { res = QuadTreeStore(cache, &key, val); return res; } }
int main(int argc, char* argv[]) { UCHAR seed[256]; UCHAR line[MAX_LINE+1]; UCHAR plain_text[MAX_TEXT+1]; UCHAR cipher_text[MAX_TEXT+1]; UCHAR comment[MAX_LINE+1]; UCHAR xored_text[MAX_TEXT+1]; int i, num; ULONG sweep, done = 0, iter; rc4_key key; FILE *fp; char** args = argv; int acount = argc; double time_taken; USHORT plain_text_len, cipher_text_len, text_len; USHORT cmd_line_project_id, file_check_sum = 0; USHORT completed_check_sum = 0; USHORT comment_len = 0; USHORT segments, start_segment; UCHAR* tag; UCHAR* value; int done_plain_text = 0; int done_cipher_text = 0; #if !defined(_MSDOS) && !defined(__OS2__) struct timeval tv_start,tv_end; #endif double keys_sec, run_time, start_time; int first = 1; #ifdef __OS2__ APIRET rc; #endif big_endian = ((char*)&test_endian)[0]; prog = argv[0]; #ifdef __OS2__ /* set priority class to IDLETIME */ rc = DosSetPriority(PRTYS_PROCESS, PRTYC_IDLETIME, 0, 0); if (rc != 0) { fprintf(stderr, "%s: unable to set priority, rc = %ld\n", prog, rc); exit(1); } #endif /* __OS2__ */ if (argc < 2) { usage(); exit(1); } if (args[1][0]=='-') { switch(args[1][1]) { case 'q': verbose = 0; break; case '\0': /* stdin = '-' */ acount++; args--; /* doesn't count as a flag */ break; default: fprintf(stderr,"invalid flag %s\n",args[1]); usage(); exit(1); } args++; acount--; } if (acount < 5) { usage(); exit(1); } if (!strcmp(args[1],"-")) { fp = stdin; } else { fp = fopen(args[1],"rb"); if (!fp) { fprintf(stderr,"error cannot open config file %s\n",args[1]); exit(2); } } num = sscanf(args[2],"%hx",&cmd_line_project_id); if (num < 1) { fprintf(stderr,"error: project-id should be a hexadecimal number\n\n"); usage(); exit(1); } if (strlen(args[3]) > 4) { fprintf(stderr,"error: start-key should be a 4 digit hexadecimal number\n\n"); usage(); exit(1); } i = byte_pack(seed, (UCHAR*) args[3], 2); if (i != 2) { fprintf(stderr,"error: start-key should be a 4 digit hexadecimal number\n\n"); usage(); exit(1); } seed[2] = seed[3] = seed[4] = 0; start_segment = (USHORT)seed[0]*256 + (USHORT)seed[1]; num = sscanf(args[4],"%hu",&segments); if (num < 1) { fprintf(stderr,"error: segments should be a decimal number\n\n"); usage(); exit(1); } sweep = (ULONG)segments << 8; while (!feof(fp)) { line[0] = '\0'; fgets(line, MAX_LINE, fp); strip_crlf(line); strip_comments(line); file_check_sum = checksum(file_check_sum, line); parse_line(line,&tag,&value); if (!tag || !*tag) { continue; } if (!strcasecmp(tag,"PLAIN-TEXT")) { if (done_plain_text) { fprintf(stderr, "config file error: should only have one PLAIN-TEXT field\n"); exit(2); } if (strlen(value) & 0x1 != 0) { fprintf(stderr, "config file error: PLAIN-TEXT field must be an even number of hex digits\n"); exit(2); } plain_text_len = byte_pack(plain_text, value, MAX_TEXT); if (plain_text_len == 0) { fprintf(stderr, "config file error: PLAIN-TEXT field must be hex digits\n"); exit(2); } done_plain_text = 1; } else if (!strcasecmp(tag,"CIPHER-TEXT")) { if (done_cipher_text) { fprintf(stderr, "config file error: should only have one CIPHER-TEXT field\n"); exit(2); } if (strlen(value) & 0x1 != 0) { fprintf(stderr, "config file error: CIPHER-TEXT field must be an even number of hex digits\n"); exit(2); } cipher_text_len = byte_pack(cipher_text, value, MAX_TEXT); if (cipher_text_len == 0) { fprintf(stderr, "config file error: CIPHER-TEXT field must be hex digits\n"); exit(2); } done_cipher_text = 1; } else if (!strcasecmp(tag,"COMMENT")) { char *rest = strtok(0, "\n"); /* ie to the end of string as there */ if (comment_len != 0) /* won't be a \n due to strip_crlf */ { fprintf(stderr, "config file error: should only have one COMMENT field\n"); exit(2); } strncpy(comment, value, MAX_LINE); if (rest) { comment_len = strlen(comment); strncat(comment," ", MAX_LINE - comment_len); comment_len++; strncat(comment, rest, MAX_LINE - comment_len); comment[MAX_LINE] = '\0'; } comment_len = strlen(comment); } else { fprintf(stderr,"config file error: unknown tag: %s\n",tag); exit(2); } } if (!done_plain_text) { fprintf(stderr,"config file error: no PLAIN-TEXT field\n"); exit(2); } if (!done_cipher_text) { fprintf(stderr,"config file error: no CIPHER-TEXT field\n"); exit(2); } if (plain_text_len != cipher_text_len) { fprintf(stderr,"config file warning: PLAIN-TEXT and CIPHER-TEXT are not the same length\n"); } text_len = plain_text_len < cipher_text_len ? plain_text_len : cipher_text_len; if (cmd_line_project_id != file_check_sum) { fprintf(stderr,"error: you have the wrong config file for project %04x (%04x)\n", cmd_line_project_id, file_check_sum); exit(1); } completed_check_sum = (( (ULONG)file_check_sum + (ULONG)start_segment + (ULONG)segments ) & 0xFFFFL); if (verbose) { fprintf(stderr,"PROJECT-ID\t%04x\n",file_check_sum); if (comment_len) fprintf(stderr,"COMMENT \t%s\n",comment); fprintf(stderr,"START-KEY\t%s\n",print_hex(seed,KEY_SIZE)); fprintf(stderr,"SEGMENTS\t%u (16M keys/segment)\n",segments); fprintf(stderr,"PLAIN-TEXT\t%s\n",print_hex(plain_text,text_len)); fprintf(stderr,"CIPHER-TEXT\t%s\n",print_hex(cipher_text,text_len)); fprintf(stderr,"TEXT-SIZE\t%d\n",text_len); fprintf(stderr,"256k keys per '.' printed, 1 segment per line, a segment = 16M keys\n"); fprintf(stderr,"LINES-TO-DO\t%d\n",segments); } /* pre-compute plain_text XOR cipher_text */ for (i=0; i<text_len; i++) { xored_text[i] = plain_text[i] ^ cipher_text[i]; } for (done=0; done<sweep; done++) { if (verbose) { if (first) { #if defined(_MSDOS) || defined(__OS2__) start_time = (double) clock()/CLOCKS_PER_SEC; #else gettimeofday(&tv_start,0); #endif } } for (iter=0; iter < 65536; iter++) { prepare_key(seed,KEY_SIZE,&key); if (rc4_eq(xored_text, text_len, &key)) { if (verbose) { fprintf(stderr,"\nFOUND-IT\t%s\n", print_hex(seed,KEY_SIZE)); instructions(); } printf("%04x %04x %04x %u %s\n",file_check_sum,completed_check_sum, start_segment,segments,print_hex(seed,KEY_SIZE)); exit(0); } inc_key(seed); } if (verbose) { if (first) { #if defined(_MSDOS) || defined(__OS2__) time_taken = ((double) clock()/CLOCKS_PER_SEC) - start_time; #else gettimeofday(&tv_end,0); time_taken = (double)(tv_end.tv_sec - tv_start.tv_sec); time_taken += ((double)(tv_end.tv_usec - tv_start.tv_usec)/1000000.0); #endif keys_sec = 65536.0 / time_taken; fprintf(stderr,"KEYS-PER-SEC\t%0.1lf keys/sec\n",keys_sec); fprintf(stderr,"EXPECTED-RUNNING-TIME\t"); run_time = sweep * time_taken; if (run_time < 60) { fprintf(stderr,"%0.1lf secs\n",run_time); } else if (run_time < 3600) { fprintf(stderr,"%0.1lf mins\n",run_time / 60); } else { fprintf(stderr,"%0.1lf hours\n",run_time / 3600); if (run_time > 180000) { fprintf(stderr,"in days: %0.1lf days\n",run_time / 86400); } } first = 0; } if ((done & 255L) == 0) { if (done > 0) fprintf(stderr,"]\n"); fprintf(stderr,"%sxxxxxx:[",print_hex(seed,2)); } if ((done & 3L) == 0) { fputc('.',stderr); } #ifdef __OS2__ fflush(stderr); /* needed to prevent buffering of stderr */ #endif } } if (verbose) { fprintf(stderr,"]\n"); instructions(); } printf("%04x %04x %04x %u no\n",file_check_sum,completed_check_sum, start_segment,segments); return 0; }
/* Send a EAPOL-Key packet */ void eapol_key(nas_t *nas, nas_sta_t *sta, unsigned char *send_key, int send_key_len, unsigned char *recv_key, int recv_key_len, unsigned char *key, int key_len, int index, int unicast) { struct iovec packet; eapol_header_t *eapol; eapol_key_header_t *body; struct timeval tv; struct timezone tz; unsigned short length; unsigned int replay[2]; unsigned char rc4_seed[48] = { 0 }; rc4_ks_t rc4_key; /* Allocate packet */ packet.iov_len = EAPOL_HEADER_LEN + EAPOL_KEY_HEADER_LEN; if (key) packet.iov_len += key_len; if (!(packet.iov_base = (caddr_t) malloc(packet.iov_len))) { perror("malloc"); return; } /* Fill EAPOL header */ eapol = (eapol_header_t *) packet.iov_base; memcpy(&eapol->eth.ether_dhost, &sta->ea, ETHER_ADDR_LEN); memcpy(&eapol->eth.ether_shost, &nas->ea, ETHER_ADDR_LEN); if (sta->flags & STA_FLAG_PRE_AUTH) eapol->eth.ether_type = htons(ETHER_TYPE_802_1X_PREAUTH); else eapol->eth.ether_type = htons(ETHER_TYPE_802_1X); eapol->version = sta->eapol_version; eapol->type = EAPOL_KEY; eapol->length = htons(packet.iov_len - EAPOL_HEADER_LEN); /* Fill EAPOL-Key header */ body = (eapol_key_header_t *) eapol->body; body->type = EAPOL_RC4_KEY; /* Length field is unaligned */ length = htons(key_len); memcpy(&body->length, &length, sizeof(body->length)); /* Replay Counter field is unaligned */ gettimeofday(&tv, &tz); /* * keep track timestamp locally in case gettimeofday() does not return * correct usec value, for example, on vx. It is ok to not adjust * tv.tv_sec since tv.tv_usec is not going to overflow anyway. */ if (tv.tv_usec == sta->rc4keyusec && tv.tv_sec == sta->rc4keysec) { tv.tv_usec += sta->rc4keycntr; sta->rc4keycntr ++; } else { sta->rc4keysec = tv.tv_sec; sta->rc4keyusec = tv.tv_usec; sta->rc4keycntr = 1; } replay[0] = htonl(tv.tv_sec + JAN_1970); replay[1] = htonl(NTPFRAC(tv.tv_usec)); memcpy(body->replay, replay, sizeof(body->replay)); /* Fill Key IV */ nas_rand128(body->iv); /* Fill Key Index */ body->index = index; if (unicast) body->index |= EAPOL_KEY_UNICAST; /* Encrypt Key */ if (key) { memcpy(rc4_seed, body->iv, 16); memcpy(&rc4_seed[16], recv_key, recv_key_len); prepare_key(rc4_seed, 16 + recv_key_len, &rc4_key); memcpy(body->key, key, key_len); rc4(body->key, key_len, &rc4_key); } /* Calculate HMAC-MD5 checksum with null signature */ if (send_key) { memset(body->signature, 0, 16); hmac_md5(&eapol->version, packet.iov_len - OFFSETOF(eapol_header_t, version), send_key, send_key_len, body->signature); } if (sta->flags & STA_FLAG_PRE_AUTH) nas_preauth_send_packet(nas, &packet, 1); else nas_eapol_send_packet(nas, &packet, 1); free(packet.iov_base); }