int DoDataEncrypt(HWND hwnd, LPTSTR key, UINT numberOfRounds, bool isForwards){ //fully functional as of Dec 06, 2013; 10:56am #pragma region DECLARATIONS ///#pragma region n is a command to my editor for outlining purposes, does not affect the code at all LPTSTR buffer = new TCHAR[CRYPTO_BUFFER_SIZE]; //our string of input data LPTSTR bufferOut;// = new TCHAR[CRYPTO_BUFFER_SIZE]; //our string of output data LPTSTR bufferKey;// = new TCHAR[CRYPTO_KEY_SIZE]; LPTSTR roundKey = new TCHAR[CRYPTO_KEY_SIZE]; _tcscpy(roundKey, key); int inLenght; // simple check for possible buffer overflow //todo : add a zeroing function to these, set them all with a known slack value HWND hEdit; int debugReturn; GLint infoLogLength = 0; //used for errorchecking GLenum errString = glGetError(); #pragma endregion #pragma region PREPARE_SHADER #if SHADER_MODE glewExperimental = GL_TRUE; //due to compute shaders GLenum err = glewInit(); if (GLEW_OK != err) { // Problem: glewInit failed, something is seriously wrong. fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); const GLenum errString = glGetError(); MessageBox(hwnd, _T(" glewInit() returned ")+(int)(GLint)err, //let's hope this doublecast from GLint to std int works _T("glewInit() failed, something is seriously wrong."), MB_OK | MB_ICONEXCLAMATION); }//end if //fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); //this section is a mix of book and Landon code. //const GLubyte* gluErrorString(GLenum errorCode); // BUG NOTE (Dec 12 2013, 10:52) : gglewInit was returning 1, but adding in the creator code for an openGL context in WinMain() appears to have resolved the issue // Create a shader, attach source, and compile. compute_shader = glCreateShader(GL_COMPUTE_SHADER); LPTSTR shaderName = _T("XOR.compute"); // GLchar * fSource = readShaderSourceT(shaderName); //(Dec 12 2013, 10:54)fScource returned null //(12:16 target file needs to be inside lower level file, probably "include" because of linking. Unsure due to multiple copies)//(12:20 not /include. not /Lib. It's for some reason the folder ABOVE the solution file)//(12:15 it has to be at the same level that the plaintext was DoFileLoad()ed from. That'll be annoying to fix, but it can wait.) if( fSource == NULL){ const GLenum errString = glGetError(); return SHADER_NOT_FOUND; } const GLchar ** compute_source = (const GLchar **)&fSource; glShaderSource(compute_shader, 1, compute_source, NULL); glCompileShader(compute_shader); //create a program, attach shader, link compute_program = glCreateProgram(); glAttachShader(compute_program, compute_shader); glLinkProgram(compute_program); //TODO : set up the needed SSBO for the strings (after data import) #endif #pragma endregion #pragma region DATA_IMPORTATION //hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); /// dec 2, 4:27pm; not getting valid id, despite code being a copy from File Load Save hEdit = hEditGlobal; inLenght = GetWindowTextLength(hEdit); // simple check for possible buffer overflow #pragma region SANITY_CHECKS if ( hEdit == 0 ){ //needs to check before DATA_TOO_BIG return PLAINTEXT_NOT_FOUND; }//end debugger if if ( _tcslen(key) > inLenght) return DATA_LESSTHAN_KEY; // also the result of a failure to aquire plaintext data if ( inLenght > CRYPTO_BUFFER_SIZE ) return DATA_TOO_BIG; //int debugReturn; debugReturn = GetWindowText(hEdit, buffer, CRYPTO_BUFFER_SIZE); //dumps contents of 'hEdit' to 'buffer'. #if DEBUG_OFF MessageBox(hwnd, key, "Notice", MB_OK | MB_ICONINFORMATION); #endif #pragma endregion #pragma region SHADER_STORAGE_BUFFER_OBJECTS #if EXPERIMENT_ON #pragma region KEY glGenBuffers(1, &keySSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, keySSBO); glBufferData(GL_SHADER_STORAGE_BUFFER, CRYPTO_KEY_SIZE*sizeof(TCHAR), NULL, //might not want to keep that as null, and replace with contents of LPTSTR key GL_STATIC_DRAW ); //might want GL_DYNAMIC_READ instead? GLint bufMask = GL_MAP_WRITE_BIT | GL_MAP_READ_BIT ; // the invalidate makes a big difference when re-writing // (Dec 12, 2:07 pm) invalidate bit removed, we're doing a full overwrite anyway bufferKey = (LPTSTR) glMapBufferRange( GL_SHADER_STORAGE_BUFFER, 0, CRYPTO_KEY_SIZE*sizeof(TCHAR), bufMask); // I'd rather use as simple of a string as I can afford //that appears to be bufferKey for now, will later use RoundKey if(bufferKey == NULL){ errString = glGetError(); return BUFFER_NOT_FOUND; } for (int i = 0; i < CRYPTO_KEY_SIZE; i++) { bufferKey[i] = roundKey[i]; // should move this section into the round loop } int chk; chk = glUnmapBuffer( GL_SHADER_STORAGE_BUFFER ); errString = glGetError(); #if DEBUG_ON //should really be doing this before I unmap for (int i = 0; i < CRYPTO_KEY_SIZE; i++) { roundKey[i] = _T('0'); roundKey[i] = bufferKey[i]; // should move this section into the round loop } MessageBox(hwnd, roundKey, //shaderName _T("Roundkey"), MB_OK | MB_ICONEXCLAMATION); #endif #pragma endregion #pragma region TEXT if( bufferKey == NULL){ errString = glGetError(); return BUFFER_NOT_FOUND; } glGenBuffers(1, &txtSSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, txtSSBO); glBufferData(GL_SHADER_STORAGE_BUFFER, CRYPTO_BUFFER_SIZE*sizeof(TCHAR), NULL, //might not want to keep that as null, and replace with contents of LPTSTR key GL_STATIC_DRAW ); //might want GL_DYNAMIC_READ instead? bufferOut = (LPTSTR) glMapBufferRange( GL_SHADER_STORAGE_BUFFER, 0, CRYPTO_BUFFER_SIZE*sizeof(TCHAR), bufMask); for (int i = 0; i < CRYPTO_BUFFER_SIZE; i++) { bufferOut[i] = buffer[i]; } #if DEBUG_ON { LPTSTR checkout = new TCHAR[CRYPTO_BUFFER_SIZE]; for (int i = 0; i < CRYPTO_BUFFER_SIZE; i++) { checkout[i] = bufferOut[i]; // should move this section into the round loop } MessageBox(hwnd, checkout, //contents of bufferOut _T("text buffer"), MB_OK | MB_ICONEXCLAMATION); } #endif // glUnmapBuffer( GL_SHADER_STORAGE_BUFFER ); //holding onto this one until after the compute has run if( bufferKey == NULL){ const GLenum errString = glGetError(); return BUFFER_NOT_FOUND; } #pragma endregion glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 4, keySSBO ); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 5, txtSSBO ); errString = glGetError(); #if EXPERIMENT_OFF keyLengthLocation = glGetUniformLocation(compute_program, "keyByteLength"); errString = glGetError(); #endif #pragma endregion #endif #pragma endregion glUseProgram(compute_program); errString = glGetError(); keyLengthLocation = 0; glUniform1ui(keyLengthLocation, (int)CRYPTO_KEY_SIZE*sizeof(TCHAR)); // error 1282 errString = glGetError(); #pragma region ACTUAL_ENCRYPTION if (isForwards == CRYPTO_FORWARDS) { #if !SHADER_MODE for (int i = 0; i < numberOfRounds; i++) { //cpu functionality int chk = GetRoundKey(key, roundKey, i); CryptoCollisionCheck(hwnd, roundKey); encryptXORstep(hwnd, buffer, roundKey, bufferOut); _tcscpy(buffer,bufferOut); //so that the next loop will use the results of this loop }//end for number of rounds #endif #if SHADER_MODE infoLogLength = 0; glGetProgramiv(compute_program, GL_INFO_LOG_LENGTH, &infoLogLength) ; GLchar * debugLog = new GLchar[infoLogLength]; glGetProgramInfoLog(compute_program, infoLogLength, &infoLogLength, debugLog); if( infoLogLength > 0 ) MessageBoxA(hwnd, (LPSTR)debugLog, ("ProgramInfoLog"), MB_OK | MB_ICONEXCLAMATION); if ( CompileSuccessful(compute_shader) == FALSE) return SHADER_COMPILE_FAILED; if( LinkSuccessful(compute_program) == FALSE) return SHADER_LINK_FAILED; errString = glGetError(); //glGetProgramResourceIndex(compute_program, GL_SHADER_STORAGE_BLOCK); { keyLengthLocation; } //for debug reference only for (int i = 0; i < numberOfRounds; i++) { int chk = GetRoundKey(key, roundKey, i); //getRoundKey glDispatchCompute(CRYPTO_DISPATCH_VEC3); errString = glGetError(); glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT ); // makes all the workgroups wait for each other to finish errString = glGetError(); }//end for number of rounds { keyLengthLocation; } //for debug reference only }//end if #endif else { // CRYPTO BACKWARDS //still in CPU space until GPU is confirmed functional for (int i = numberOfRounds-1; i >= 0; i--) { int chk = GetRoundKey(key, roundKey, i); CryptoCollisionCheck(hwnd, roundKey); //doesn't modify anything, just dialogs an output of "at-risk" characters encryptXORstep(hwnd, buffer, roundKey, bufferOut); _tcscpy(buffer,bufferOut); //so that the next loop will use the results of this loop }//end for number of rounds }//end else #if DEBUG_ON { LPTSTR checkout = new TCHAR[CRYPTO_BUFFER_SIZE]; for (int i = 0; i < CRYPTO_BUFFER_SIZE; i++) { checkout[i] = bufferOut[i]; // should move this section into the round loop } MessageBox(hwnd, checkout, //shaderName _T("ciphertext buffer"), MB_OK | MB_ICONEXCLAMATION); } #endif #pragma endregion #pragma region DATA_EXPORTATION SetWindowText(hEdit, bufferOut); //sends the encrypted results to the edit window if (debugReturn > 0) //debugReturn is the number of characters GetWindowText spat out, or 0 return NO_ERRORS_DETECTED; else return debugReturn; // from API : " If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, call GetLastError." #pragma endregion #if SHADER_MODE //delete shader once done with it //int chk; chk = glUnmapBuffer( GL_SHADER_STORAGE_BUFFER ); glDeleteShader(compute_shader); #endif }//end DDE()