int main(void) { unsigned char in[BUFSIZE], encrypted[BUFSIZE], decrypted[BUFSIZE]; unsigned char *e = encrypted; int len; DES_cblock key1, key2, key3; DES_cblock seed = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}; DES_key_schedule ks1, ks2, ks3; DES_cblock ivsetup = {0xE1, 0xE2, 0xE3, 0xD4, 0xD5, 0xC6, 0xC7, 0xA8}; DES_cblock ivec; memset(in, 0, sizeof(in)); memset(encrypted, 0, sizeof(encrypted)); memset(decrypted, 0, sizeof(decrypted)); RAND_seed(seed, sizeof(DES_cblock)); DES_random_key(&key1); DES_random_key(&key2); DES_random_key(&key2); DES_set_key((DES_cblock *)key1, &ks1); DES_set_key((DES_cblock *)key2, &ks2); DES_set_key((DES_cblock *)key3, &ks3); /* plaintext */ strcpy(in, "this is a string encrypted with triple des. it is 64 bytes long."); printf("Plaintext: [%s]\n", in); len = strlen(in); memcpy(ivec, ivsetup, sizeof(ivsetup)); DES_ede3_cbc_encrypt(in, encrypted, len, &ks1, &ks2, &ks3, &ivec, DES_ENCRYPT); printf("Ciphertext:"); while (*e) printf(" [%02x]", *e++); printf("\n"); len = strlen(encrypted); memcpy(ivec, ivsetup, sizeof(ivsetup)); DES_ede3_cbc_encrypt(encrypted, decrypted, len, &ks1, &ks2, &ks3, &ivec, DES_DECRYPT); printf("Decrypted Text: [%s]\n", decrypted); exit(0); }
void openssl_des_crypt() { int size; DES_cblock key; DES_cblock outputs; const_DES_cblock inputs; DES_key_schedule schedule; unsigned char tmp[16] = "des crypt"; DES_random_key(&key); DES_string_to_key("beike2012", &key); DES_set_odd_parity(&key); des_check_key_parity(&key); DES_set_key_checked(&key, &schedule); DES_is_weak_key((const_DES_cblock *)tmp); DES_ecb_encrypt((const_DES_cblock *)tmp, &outputs, &schedule, DES_ENCRYPT); printf("\nDES_ecb_encrypt(%s) = ", tmp); for (size = 0; size < sizeof(outputs); size++) printf("%02x", outputs[size]); printf("\n"); DES_ecb_encrypt(&outputs, &inputs, &schedule, DES_DECRYPT); printf("DES_ecb_decrypt("); for (size = 0; size < sizeof(outputs); size++) printf("%02x", outputs[size]); printf(") = %s\n", inputs); }
int main(int argc, char* argv[]){ /* does not seem to be needed ot useful time_t curtime = time(NULL); unsigned int iseed = (unsigned int) curtime; srand(iseed); */ printf("size of random key is: %d\n", sizeof(DES_cblock)); printf("size of long is: %d\n", sizeof(long)); DES_cblock *key = (DES_cblock *) malloc(sizeof(DES_cblock)); assert(key != NULL); printf("key memory location is %lx\n", key); *((long*) key) = 0L; long before = *((long *) key); printf("key prior to initialization: %lx\n", before); DES_random_key(key); /* generating random key */ long after = *((long*) key); printf("key after initialization: %lx\n", after); free(key); return 0; }
int main(int argc, char** args) { if(argc!=5 || (strcmp(args[1], "cbc") && strcmp(args[1], "ecb")) || (strcmp(args[2], "encrypt") && strcmp(args[2], "decrypt"))) { printf("uzycie: des cbc/ecb encrypt/decrypt plik.in plik.key\ndane wyjsciowe przekierowywane sa do pliku plik.out\n"); return 1; } FILE* in = fopen(args[3], "r"); if(!in) { printf("blad przy otwieraniu pliku...\n"); return 1; } FILE* key; if(strcmp(args[2], "encrypt")) key = fopen(args[4], "r"); else key = fopen(args[4], "w"); DES_key_schedule klucz; DES_cblock sekwencja; DES_random_key(&sekwencja); if(DES_set_key_checked(&sekwencja, &klucz)<0) { printf("blad przy generacji klucza!\n"); return 1; } if(strcmp(args[1], "cbc")) { if(strcmp(args[2], "encrypt")) ecb(in, key, klucz, 0); else ecb(in, key, klucz, 1); } else { if(strcmp(args[2], "encrypt")) cbc(in, key, klucz, 0); else cbc(in, key, klucz, 1); } fclose(in); fclose(key); return 0; }
int CmdHF14AMfDESAuth(const char *Cmd){ uint8_t blockNo = 0; //keyNo=0; uint32_t cuid=0; uint8_t reply[16]; //DES_cblock r1_b1; uint8_t b1[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; uint8_t b2[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; DES_cblock nr, b0, r1, r0; uint8_t key[8]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //DES_cblock iv={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; DES_key_schedule ks1; DES_cblock key1; if (strlen(Cmd)<1) { PrintAndLog("Usage: hf desfire des-auth k <key number>"); PrintAndLog(" sample: hf desfire des-auth k 0"); return 0; } //Change key to user defined one memcpy(key1,key,8); //memcpy(key2,key+8,8); DES_set_key((DES_cblock *)key1,&ks1); //DES_set_key((DES_cblock *)key2,&ks2); //Auth1 UsbCommand c = {CMD_MIFARE_DES_AUTH1, {blockNo}}; SendCommand(&c); UsbCommand resp; if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { uint8_t isOK = resp.arg[0] & 0xff; cuid = resp.arg[1]; uint8_t * data= resp.d.asBytes; if (isOK){ PrintAndLog("enc(nc)/b0:%s", sprint_hex(data+2,8)); memcpy(b0,data+2,8); } } else { PrintAndLog("Command execute timeout"); } //Do crypto magic DES_random_key(&nr); //b1=dec(nr) //r0=dec(b0) DES_ecb_encrypt(&nr,&b1,&ks1,0); DES_ecb_encrypt(&b0,&r0,&ks1,0); //PrintAndLog("b1:%s",sprint_hex(b1, 8)); PrintAndLog("r0:%s",sprint_hex(r0, 8)); //r1=rol(r0) memcpy(r1,r0,8); rol(r1,8); PrintAndLog("r1:%s",sprint_hex(r1, 8)); for(int i=0;i<8;i++){ b2[i]=(r1[i] ^ b1[i]); } DES_ecb_encrypt(&b2,&b2,&ks1,0); //PrintAndLog("b1:%s",sprint_hex(b1, 8)); PrintAndLog("b2:%s",sprint_hex(b2, 8)); //Auth2 UsbCommand d = {CMD_MIFARE_DES_AUTH2, {cuid}}; memcpy(reply,b1,8); memcpy(reply+8,b2,8); memcpy(d.d.asBytes,reply, 16); SendCommand(&d); UsbCommand respb; if (WaitForResponseTimeout(CMD_ACK,&respb,1500)) { uint8_t isOK = respb.arg[0] & 0xff; uint8_t * data2= respb.d.asBytes; if (isOK){ PrintAndLog("b3:%s", sprint_hex(data2+2, 8)); } } else { PrintAndLog("Command execute timeout"); } return 1; }
/* * Authenticate to the provided MIFARE tag. */ int mifare_ultralightc_authenticate (MifareTag tag, const MifareDESFireKey key) { ASSERT_ACTIVE (tag); ASSERT_MIFARE_ULTRALIGHT_C (tag); BUFFER_INIT (cmd1, 2); BUFFER_INIT (res, 9); BUFFER_APPEND (cmd1, 0x1A); BUFFER_APPEND (cmd1, 0x00); ULTRALIGHT_TRANSCEIVE_RAW(tag, cmd1, res); uint8_t PICC_E_RndB[8]; memcpy (PICC_E_RndB, res+1, 8); uint8_t PICC_RndB[8]; memcpy (PICC_RndB, PICC_E_RndB, 8); uint8_t ivect[8]; memset (ivect, '\0', sizeof (ivect)); mifare_cypher_single_block (key, PICC_RndB, ivect, MCD_RECEIVE, MCO_DECYPHER, 8); uint8_t PCD_RndA[8]; DES_random_key ((DES_cblock*)&PCD_RndA); uint8_t PCD_r_RndB[8]; memcpy (PCD_r_RndB, PICC_RndB, 8); rol (PCD_r_RndB, 8); uint8_t token[16]; memcpy (token, PCD_RndA, 8); memcpy (token+8, PCD_r_RndB, 8); size_t offset = 0; while (offset < 16) { mifare_cypher_single_block (key, token + offset, ivect, MCD_SEND, MCO_ENCYPHER, 8); offset += 8; } BUFFER_INIT (cmd2, 17); BUFFER_APPEND (cmd2, 0xAF); BUFFER_APPEND_BYTES (cmd2, token, 16); ULTRALIGHT_TRANSCEIVE_RAW(tag, cmd2, res); uint8_t PICC_E_RndA_s[8]; memcpy (PICC_E_RndA_s, res+1, 8); uint8_t PICC_RndA_s[8]; memcpy (PICC_RndA_s, PICC_E_RndA_s, 8); mifare_cypher_single_block (key, PICC_RndA_s, ivect, MCD_RECEIVE, MCO_DECYPHER, 8); uint8_t PCD_RndA_s[8]; memcpy (PCD_RndA_s, PCD_RndA, 8); rol (PCD_RndA_s, 8); if (0 != memcmp (PCD_RndA_s, PICC_RndA_s, 8)) { return -1; } // XXX Should we store the state "authenticated" in the tag struct?? return 0; }
void _ossl_old_des_random_key(_ossl_old_des_cblock ret) { DES_random_key((DES_cblock *)ret); }
int main(void) { int i; BIO* bio_out; DES_cblock key; DES_key_schedule schedule; unsigned char const iv_data[DES_KEY_SZ] = { 0xcc, 0xfe, 0xcd, 0x3e, 0x21, 0xde, 0x1c, 0x31 }; unsigned char iv[DES_KEY_SZ]; char* data = "The worthwhile problems are the ones you can" "really solve or help solve, the ones you can" "really contribute something to. No " "problem is too small or too trivial if we " "can really do something about it." "- Richard Feynman"; int length = strlen(data); /* Intialise to '0' to indicate that '0' bytes of the IV has been used */ int num = 0; /* Allocate the memory for the resulting ciphertext and plaintext */ char* ciphertext = (char*) malloc(sizeof(char) * length); char* plaintext = (char*) malloc(sizeof(char) * length); /* Copy the IV data to the IV array. The IV array will be updated by the DES_ofb_encrypt call.*/ memcpy(iv, iv_data, DES_KEY_SZ); /* In this example, we shall be generating a random key. Before this can * happen, we must seed the PRNG. OpenSSL ensures that the PRNG is transparently * seeded on systems that provide the "/dev/urandom" file. */ /* Cater for seeding the PRNG in Windows */ #ifdef OPENSSL_SYS_WIN32 /* Add entropy */ #endif /* Generate the random key (as expected by DES) */ DES_random_key(&key); /* Check the odd parity of the key and its weakness. In doing so, * convert to the architecture dependent format. */ DES_set_key_checked(&key, &schedule); DES_ofb64_encrypt(data, ciphertext, length, &schedule, (DES_cblock*)iv, &num); bio_out = BIO_new_fp(stdout, BIO_NOCLOSE); BIO_printf(bio_out, "Original plaintext: %s\n\n", data); BIO_printf(bio_out, "Ciphertext: "); /* print out the ciphertext */ for (i = 0; i < length; i++) BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]); BIO_printf(bio_out, "\n\n"); /* Start the decryption process */ /* Re-intialise to '0' to indicate that '0' bytes of the IV has been used */ num = 0; /* First, copy the original IV data back to the IV array - as it was * overwritten during the encryption process */ memcpy(iv, iv_data, DES_KEY_SZ); DES_ofb64_encrypt(ciphertext, plaintext, length, &schedule, (DES_cblock*)iv, &num); BIO_printf(bio_out, "Recovered plaintext: "); /* print out the plaintext */ for (i = 0; i < length; i++) BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]); BIO_printf(bio_out, "\n"); BIO_free(bio_out); free(ciphertext); free(plaintext); return 0; }
int mifare_desfire_authenticate (MifareTag tag, uint8_t key_no, MifareDESFireKey key) { ASSERT_ACTIVE (tag); ASSERT_MIFARE_DESFIRE (tag); MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; MIFARE_DESFIRE (tag)->authenticated_key_no = NOT_YET_AUTHENTICATED; free (MIFARE_DESFIRE (tag)->session_key); MIFARE_DESFIRE (tag)->session_key = NULL; BUFFER_INIT (cmd1, 2); BUFFER_INIT (res, 9); BUFFER_APPEND (cmd1, 0x0A); BUFFER_APPEND (cmd1, key_no); DESFIRE_TRANSCEIVE (tag, cmd1, res); uint8_t PICC_E_RndB[8]; memcpy (PICC_E_RndB, res+1, 8); uint8_t PICC_RndB[8]; memcpy (PICC_RndB, PICC_E_RndB, 8); mifare_cbc_des (key, PICC_RndB, 8, MD_RECEIVE, 0); uint8_t PCD_RndA[8]; DES_random_key ((DES_cblock*)&PCD_RndA); uint8_t PCD_r_RndB[8]; memcpy (PCD_r_RndB, PICC_RndB, 8); rol8 (PCD_r_RndB); uint8_t token[16]; memcpy (token, PCD_RndA, 8); memcpy (token+8, PCD_r_RndB, 8); mifare_cbc_des (key, token, 16, MD_SEND, 0); BUFFER_INIT (cmd2, 17); BUFFER_APPEND (cmd2, 0xAF); BUFFER_APPEND_BYTES (cmd2, token, 16); DESFIRE_TRANSCEIVE (tag, cmd2, res); uint8_t PICC_E_RndA_s[8]; memcpy (PICC_E_RndA_s, res+1, 8); uint8_t PICC_RndA_s[8]; memcpy (PICC_RndA_s, PICC_E_RndA_s, 8); mifare_cbc_des (key, PICC_RndA_s, 8, MD_RECEIVE, 0); uint8_t PCD_RndA_s[8]; memcpy (PCD_RndA_s, PCD_RndA, 8); rol8 (PCD_RndA_s); if (0 != memcmp (PCD_RndA_s, PICC_RndA_s, 8)) { printf ("PCD_RndA_s != PICC_RndA_s"); return -1; } MIFARE_DESFIRE (tag)->authenticated_key_no = key_no; MIFARE_DESFIRE (tag)->session_key = mifare_desfire_session_key_new (PCD_RndA, PICC_RndB, key); return 0; }