static void do_des_decrypt(struct crypto_instance *s, UINT32 length, const UINT8 *src, UINT8 *dst) { CAST(des_instance, self, s); des_decrypt(&self->ctx, length, dst, src); }
int unpkg_auth(const uint8_t body[], int len, login_session_t* sess) { char outbuf[32]; if ( len != 16 ) return -1; //session: ip + time + userid + time des_decrypt(LOGIN_DES_KEY, (char*)(body), outbuf); des_decrypt(LOGIN_DES_KEY, (char*)(body + 8), outbuf + 8); sess->ip = *(uint32_t *)outbuf; sess->uid = *(uint32_t *)(outbuf + 8); sess->tm1 = *(uint32_t*)(outbuf + 4); sess->tm2 = *(uint32_t*)(outbuf + 12); return 0; }
void decryptLog(char *fn, int entryNo, FILE *fd) { int logType; char *data; char *prevHashChain; char *msgAuth; char *authKey = A0; prevHashChain = (char *)malloc(20+1); // the initial hash chain memset(prevHashChain, 'a', 20+1); int i = 0; int fileOpen = 1; int logValid = 1; int normalClose = 0; while(fileOpen) { if(entryNo != -1 && i-ENTRYNO_OFFSET > entryNo){ break; } struct ALogEntry *entry = readAEntry(fn, i); if(entry == NULL) { break; } if(entry->logType == NORMAL_MSG || entry->logType == LOG_INIT || entry->logType == NORMAL_CLOSE) { if(entry->logType == LOG_INIT) { fprintf(fd, "%s", "LOG_INIT:"); } else if(entry->logType == NORMAL_CLOSE) { fprintf(fd, "%s", "NORMAL_CLOSE:"); } //char *decKey = _sessionKey; char *decKey = createKey(entry->logType, authKey); char *text = des_decrypt(decKey, entry->data, strlen(entry->data)); if(text != NULL) { if(entryNo == -1) { fprintf(fd, "%s\n", text); } else if(i-ENTRYNO_OFFSET == entryNo) { fprintf(fd, "%s\n", text); } } } if(entry->logType == NORMAL_CLOSE) { break; } logType = entry->logType; data = entry->data; prevHashChain = entry->hashChain; msgAuth = entry->msgAuth; //next authKey authKey = hash(authKey); i++; } }
void testDES() { char t[100]="11111111"; int i; set_key(t); scanf("%s",t); des_encrypt(t,t); printf("密文长度为%d %s\n",strlen(t),t); des_decrypt(t,t); printf("明文长度为%d %s\n",strlen(t),t); }
static int dencrypt_des(unsigned char* key,long keylen,unsigned char *data,short datalen,unsigned char *Des_result) { des_context ctx; if(keylen != 8) return INVALID_KEY_LEN; if(datalen != 8) return INVALID_DATA_LEN; memset(&ctx,0,sizeof ctx); des_set_key(&ctx,key); des_decrypt(&ctx,data,Des_result); return 0; }
static void test_des(const struct tstring *key, int expected_parity, const struct tstring *cleartext, const struct tstring *ciphertext) { struct des_ctx ctx; uint8_t *data; unsigned length; ASSERT (cleartext->length == ciphertext->length); length = cleartext->length; ASSERT (key->length == DES_KEY_SIZE); data = xalloc(length); ASSERT (des_check_parity(8, key->data) == expected_parity); ASSERT (des_set_key(&ctx, key->data)); des_encrypt(&ctx, length, data, cleartext->data); if (!MEMEQ(length, data, ciphertext->data)) { fprintf(stderr, "Encrypt failed:\nInput:"); tstring_print_hex(cleartext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); tstring_print_hex(ciphertext); fprintf(stderr, "\n"); FAIL(); } des_decrypt(&ctx, length, data, data); if (!MEMEQ(length, data, cleartext->data)) { fprintf(stderr, "Decrypt failed:\nInput:"); tstring_print_hex(ciphertext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); tstring_print_hex(cleartext); fprintf(stderr, "\n"); FAIL(); } free(data); }
char* getX(struct Msg *msg, char *privKeyFile, char *pubKeyFile) { //get key char *encKey = msg->pke; char *key = rsa_decrypt(encKey, privKeyFile); //decrypt the message char *enc = msg->enc; char *text = des_decrypt( key, enc, msg->encLen); //divide message into x and signiture int xLen = msg->xLen; char *x = (char *)malloc(xLen +1); memcpy( x, text, xLen ); x[xLen] = '\0'; return x; }
static void test_des(const uint8_t *key, int expected_parity, unsigned length, const uint8_t *cleartext, const uint8_t *ciphertext) { struct des_ctx ctx; uint8_t *data = xalloc(length); if (des_check_parity(8, key) != expected_parity) FAIL(); if (!des_set_key(&ctx, key)) FAIL(); des_encrypt(&ctx, length, data, cleartext); if (!MEMEQ(length, data, ciphertext)) { fprintf(stderr, "Encrypt failed:\nInput:"); print_hex(length, cleartext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); print_hex(length, ciphertext); fprintf(stderr, "\n"); FAIL(); } des_decrypt(&ctx, length, data, data); if (!MEMEQ(length, data, cleartext)) { fprintf(stderr, "Decrypt failed:\nInput:"); print_hex(length, ciphertext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); print_hex(length, cleartext); fprintf(stderr, "\n"); FAIL(); } free(data); }
int secure_listener_callback(uint8_t *msg, size_t msg_len, jnx_socket *s) { if(last_session) { if(last_session->current_state == SESSION_CONNECTED) { JNX_LOG(DEFAULT_CONTEXT,"Attempting to resolve message...\n"); JNX_LOG(DEFAULT_CONTEXT,"Decrypting with %s\n",last_session->shared_secret); char *decrypted_message = des_decrypt(last_session->shared_secret, msg,msg_len); JNX_LOG(DEFAULT_CONTEXT,"decrypted message: %s\n",decrypted_message); jnx_term_printf_in_color(JNX_COL_MAGENTA,">%s\n",decrypted_message); free(decrypted_message); } } free(msg); ///need to free ip? return 0; }
int verifyMsg(struct Msg *msg, char *privKeyFile, char *pubKeyFile) { //get key char *encKey = msg->pke; char *key = rsa_decrypt(encKey, privKeyFile); //decrypt the message char *enc = msg->enc; char *text = des_decrypt( key, enc, msg->encLen); //divide message into x and signiture int xLen = msg->xLen; int sigLen = msg->sigLen; char x[xLen+1]; char sig[sigLen+1]; memcpy( x, text, xLen ); x[xLen] = '\0'; text = text+xLen; memcpy( sig, text, sigLen ); sig[sigLen] ='\0'; //verify signiture int result = rsa_verify(x, sig, pubKeyFile, sigLen); return result; }
int parse_pkcs8_private_key( rsa_key *privkey, const unsigned char *buffer, int buffer_length, const unsigned char *passphrase ) { struct asn1struct pkcs8_key; struct asn1struct private_key; struct asn1struct *encryptionId; struct asn1struct *salt; struct asn1struct *iteration_count; struct asn1struct *encrypted_key; struct asn1struct *key_type_oid; struct asn1struct *priv_key_data; digest_ctx initial_hash; int counter; unsigned char passphrase_hash_in[ MD5_RESULT_SIZE * sizeof( int ) ]; unsigned char passphrase_hash_out[ MD5_RESULT_SIZE * sizeof( int ) ]; unsigned char *decrypted_key; asn1parse( buffer, buffer_length, &pkcs8_key ); encryptionId = pkcs8_key.children->children; if ( memcmp( OID_pbeWithMD5andDES_CBC, encryptionId->data, encryptionId->length ) ) { fprintf( stderr, "Unsupported key encryption algorithm\n" ); asn1free( &pkcs8_key ); return 1; } // TODO support more algorithms salt = encryptionId->next->children; iteration_count = salt->next; encrypted_key = pkcs8_key.children->next; // ugly typecasting counter = ntohs( *iteration_count->data ); new_md5_digest( &initial_hash ); update_digest( &initial_hash, passphrase, strlen( passphrase ) ); update_digest( &initial_hash, salt->data, salt->length ); finalize_digest( &initial_hash ); memcpy( passphrase_hash_out, initial_hash.hash, initial_hash.hash_len * sizeof( int ) ); while ( --counter ) { memcpy( passphrase_hash_in, passphrase_hash_out, sizeof( int ) * MD5_RESULT_SIZE ); md5_hash( passphrase_hash_in, sizeof( int ) * MD5_RESULT_SIZE, ( unsigned int * ) passphrase_hash_out ); } decrypted_key = ( unsigned char * ) malloc( encrypted_key->length ); des_decrypt( encrypted_key->data, encrypted_key->length, decrypted_key, ( unsigned char * ) passphrase_hash_out + DES_KEY_SIZE, ( unsigned char * ) passphrase_hash_out ); // sanity check if ( decrypted_key[ encrypted_key->length - 1 ] > 8 ) { fprintf( stderr, "Decryption error, bad padding\n"); asn1free( &pkcs8_key ); free( decrypted_key ); return 1; } asn1parse( decrypted_key, encrypted_key->length - decrypted_key[ encrypted_key->length - 1 ], &private_key ); free( decrypted_key ); key_type_oid = private_key.children->next->children; if ( memcmp( OID_RSAPrivateKey, key_type_oid->data, key_type_oid->length ) ) { fprintf( stderr, "Unsupported private key type" ); asn1free( &pkcs8_key ); asn1free( &private_key ); } priv_key_data = private_key.children->next->next; parse_pkcs8_private_key( privkey, priv_key_data->data, priv_key_data->length, "password" ); asn1free( &pkcs8_key ); asn1free( &private_key ); return 0; }
//! \brief Main example doing DES encryption/decryption. int main( void ) { uint8_t i; board_init(); sleepmgr_init(); bool success = true; /* Example of how to use Single DES encryption and decryption functions. */ des_encrypt(data, single_ans, keys); des_decrypt(single_ans, single_ans, keys); /* Check if decrypted answer is equal to plaintext. */ for (i = 0; i < DES_BLOCK_LENGTH ; i++ ){ if (data[i] != single_ans[i]){ success = false; break; } } if (success){ /* Example of how to use 3DES encryption and decryption functions. */ des_3des_encrypt(data, single_ans, keys); des_3des_decrypt(single_ans, single_ans, keys); /* Check if decrypted answer is equal to plaintext. */ for (i = 0; i < DES_BLOCK_LENGTH ; i++ ){ if (data[i] != single_ans[i]){ success = false; break; } } } if (success){ /* Example of how to use DES Cipher Block Chaining encryption and * decryption functions. */ des_cbc_encrypt(data_block, cipher_block_ans, keys, init, true, DES_BLOCK_COUNT); des_cbc_decrypt(cipher_block_ans, block_ans, keys, init, true, DES_BLOCK_COUNT); /* Check if decrypted answer is equal to plaintext. */ for (i = 1; i < (DES_BLOCK_LENGTH * DES_BLOCK_COUNT); i++ ){ if (data_block[i] != block_ans[i]){ success = false; break; } } } /* Indicate final result by lighting LED. */ if (success) { /* If the example ends up here every thing is ok. */ ioport_set_pin_low(LED0_GPIO); } else { /* If the example ends up here something is wrong. */ ioport_set_pin_low(LED1_GPIO); } while (true) { /* Go to sleep. */ sleepmgr_enter_sleep(); } }
static void des_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length, uint8_t *dst, const uint8_t *src) { des_decrypt(ctx, length, dst, src); }