int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) { printf ("decryption: len %d, input %p, output %p\n",ciphertext_len,ciphertext, plaintext); EVP_CIPHER_CTX *ctx; int len = 0; int offset = 0; if(!(ctx = EVP_CIPHER_CTX_new())) error_handler(); if(EVP_DecryptInit_ex(ctx, EVP_aes_256_xts(), NULL, key, iv) != 1) error_handler(); while (offset < ciphertext_len) { len = _decrypt(ctx, ciphertext + offset, 512, plaintext + offset ); printf ("decryption: offset %d, input %p, output %p, len %d\n",offset, ciphertext + offset, plaintext + offset, len); offset += len; } EVP_CIPHER_CTX_free(ctx); return offset; }
virtual shared_ptr< vector<char> > GetContents() { shared_ptr<vector<char>> compressData = m_packFile->Read(m_offset, m_compress_size); _decrypt(&(*compressData->begin()), compressData->size(), m_seed); shared_ptr<vector<char>> contents(new vector<char>(m_decompress_size)); unsigned long destLen = contents->size(); uncompress((byte *)&(*contents->begin()), &destLen, (byte *)&(*compressData->begin()), compressData->size()); return contents; };
static void _proc_msg(int new_fd, char *msg, slurm_addr_t cli_addr) { /* Locks: Read job and node data */ slurmctld_lock_t job_read_lock = { NO_LOCK, READ_LOCK, READ_LOCK, NO_LOCK, NO_LOCK }; /* Locks: Write job */ slurmctld_lock_t job_write_lock = { NO_LOCK, WRITE_LOCK, NO_LOCK, NO_LOCK, NO_LOCK }; /* Locks: Write job, write node, read partition */ slurmctld_lock_t job_write_lock2 = { NO_LOCK, WRITE_LOCK, WRITE_LOCK, READ_LOCK, READ_LOCK }; /* Locks: Write node data */ slurmctld_lock_t node_write_lock = { NO_LOCK, NO_LOCK, WRITE_LOCK, NO_LOCK, READ_LOCK }; char *cmd_ptr, *resp = NULL, *msg_decrypted = NULL; uid_t cmd_uid; uint32_t protocol_version = 0; if (!msg) { info("slurmctld/nonstop: NULL message received"); resp = xstrdup("Error:\"NULL message received\""); goto send_resp; } msg_decrypted = _decrypt(msg, &cmd_uid); if (!msg_decrypted) { info("slurmctld/nonstop: Message decrypt failure"); resp = xstrdup("Error:\"Message decrypt failure\""); goto send_resp; } if (nonstop_debug > 0) info("slurmctld/nonstop: msg decrypted:%s", msg_decrypted); cmd_ptr = msg_decrypted; /* 123456789012345678901234567890 */ if (xstrncmp(cmd_ptr, version_string, 13) == 0) { cmd_ptr = strchr(cmd_ptr + 13, ':'); if (cmd_ptr) { cmd_ptr++; protocol_version = SLURM_PROTOCOL_VERSION; } } if (protocol_version == 0) { info("slurmctld/nonstop: Message version invalid"); resp = xstrdup("Error:\"Message version invalid\""); goto send_resp; } if (xstrncmp(cmd_ptr, "CALLBACK:JOBID:", 15) == 0) { resp = register_callback(cmd_ptr, cmd_uid, cli_addr, protocol_version); } else if (xstrncmp(cmd_ptr, "DRAIN:NODES:", 12) == 0) { lock_slurmctld(node_write_lock); resp = drain_nodes_user(cmd_ptr, cmd_uid, protocol_version); unlock_slurmctld(node_write_lock); } else if (xstrncmp(cmd_ptr, "DROP_NODE:JOBID:", 15) == 0) { lock_slurmctld(job_write_lock2); resp = drop_node(cmd_ptr, cmd_uid, protocol_version); unlock_slurmctld(job_write_lock2); } else if (xstrncmp(cmd_ptr, "GET_FAIL_NODES:JOBID:", 21) == 0) { lock_slurmctld(job_read_lock); resp = fail_nodes(cmd_ptr, cmd_uid, protocol_version); unlock_slurmctld(job_read_lock); } else if (xstrncmp(cmd_ptr, "REPLACE_NODE:JOBID:", 19) == 0) { lock_slurmctld(job_write_lock2); resp = replace_node(cmd_ptr, cmd_uid, protocol_version); unlock_slurmctld(job_write_lock2); } else if (xstrncmp(cmd_ptr, "SHOW_CONFIG", 11) == 0) { resp = show_config(cmd_ptr, cmd_uid, protocol_version); } else if (xstrncmp(cmd_ptr, "SHOW_JOB:JOBID:", 15) == 0) { resp = show_job(cmd_ptr, cmd_uid, protocol_version); } else if (xstrncmp(cmd_ptr, "TIME_INCR:JOBID:", 16) == 0) { lock_slurmctld(job_write_lock); resp = time_incr(cmd_ptr, cmd_uid, protocol_version); unlock_slurmctld(job_write_lock); } else { info("slurmctld/nonstop: Invalid command: %s", cmd_ptr); xstrfmtcat(resp, "%s ECMD", SLURM_VERSION_STRING); } send_resp: if (nonstop_debug > 0) info("slurmctld/nonstop: msg send:%s", resp); _send_reply(new_fd, resp); xfree(resp); if (msg_decrypted) free(msg_decrypted); return; }
int decrypt(char* clave, char* palabra, char* out){ bool is_present = 0;//falso int pos = 0; // matrix 3x3 int adj[9]; int inv[9] = {0}; int det, det_1, det_2, inv_det; int result2[3]; int word_code[6]; int i; int matrix[9] = {0}; for (i = 0; i < 9; i++) { is_present = 0; pos = 0; while(!is_present && pos < 41){ if(alfabeto[pos] == clave[i]){ matrix[i] = pos; is_present = 1; } pos += 1; } } // adj(matrix) adj[0] = (matrix[4]*matrix[8] - matrix[5]*matrix[7]) % 41; // 0 adj[1] = (matrix[1]*matrix[8] - matrix[2]*matrix[7]) % 41; // 3 adj[2] = (matrix[1]*matrix[5] - matrix[2]*matrix[4]) % 41; // 6 adj[3] = (matrix[3]*matrix[8] - matrix[5]*matrix[6]) % 41; // 1 adj[4] = (matrix[0]*matrix[8] - matrix[2]*matrix[6]) % 41; // 4 adj[5] = (matrix[0]*matrix[5] - matrix[2]*matrix[3]) % 41; // 7 adj[6] = (matrix[3]*matrix[7] - matrix[4]*matrix[6]) % 41; // 2 adj[7] = (matrix[0]*matrix[7] - matrix[1]*matrix[6]) % 41; // 5 adj[8] = (matrix[0]*matrix[4] - matrix[1]*matrix[3]) % 41; // 8 // calculamos el determinante det_1 = matrix[0]*matrix[4]*matrix[8] + matrix[1]*matrix[5]*matrix[6] + matrix[2]*matrix[3]*matrix[7]; det_2 = matrix[2]*matrix[4]*matrix[6] + matrix[5]*matrix[7]*matrix[0] + matrix[8]*matrix[1]*matrix[3]; det = det_1 - det_2; if(det < 0) det = (det % 41)+41; else det = det % 41; inv_det = euclides(0, 1, 0, 41, 0, 1, det); if(inv_det == 0){ return -1; } for (i = 0; i < 9; i++) { if(i%2 == 0) inv[i] = ( adj[i] * inv_det ) % 41; else{ inv[i] = ( (-1) * adj[i] * inv_det ) % 41; } } int size = strlen(palabra); out[size] = 0; while(size > 2){ for (i = 0; i < 3; i++) { is_present = 0; pos = 0; while(!is_present && pos < 41){ if(alfabeto[pos] == palabra[i]){ word_code[i] = pos; is_present = 1; } pos += 1; } } _decrypt(inv, word_code, result2); for (i = 0; i < 3; i++) *(out++) = alfabeto[result2[i]]; palabra += 3; size -= 3; } if(size){ word_code[0] = 0; word_code[1] = 0; word_code[2] = 0; for (i = 0; i < size; i++) { is_present = 0; pos = 0; while(!is_present && pos < 41){ if(alfabeto[pos] == palabra[i]){ word_code[i] = pos; is_present = 1; } pos += 1; } } if(size == 2){ result2[1] = (word_code[1] * matrix[0] - matrix[3] * word_code[0]) / (matrix[0] * matrix[4] - matrix[1] * matrix[3]); result2[0] = (word_code[0] - matrix[1] * result2[1]) / matrix[0]; }else{ result2[0] = word_code[0] / matrix[0]; } //_decrypt(inv, word_code, result2); for (i = 0; i < size; i++) *(out++) = alfabeto[result2[i]]; } return 0; }