Want more features on Pastebin? Sign Up, it's FREE!
Guest

dde()

By: a guest on Dec 13th, 2013  |  syntax: C++  |  size: 9.81 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. int DoDataEncrypt(HWND hwnd, LPTSTR key, UINT numberOfRounds, bool isForwards){
  3.         //fully functional as of Dec 06, 2013; 10:56am
  4. #pragma region DECLARATIONS
  5.  
  6.         ///#pragma region n is a command to my editor for outlining purposes, does not affect the code at all
  7.  
  8.         LPTSTR buffer    = new TCHAR[CRYPTO_BUFFER_SIZE]; //our string of input data
  9.         LPTSTR bufferOut;// = new TCHAR[CRYPTO_BUFFER_SIZE]; //our string of output data
  10.         LPTSTR bufferKey;// = new TCHAR[CRYPTO_KEY_SIZE];
  11.         LPTSTR roundKey  = new TCHAR[CRYPTO_KEY_SIZE];
  12.         _tcscpy(roundKey, key);
  13.         int inLenght; // simple check for possible buffer overflow
  14.        
  15.         //todo : add a zeroing function to these, set them all with a known slack value
  16.  
  17.         HWND hEdit;
  18.         int debugReturn;
  19.         GLint infoLogLength = 0; //used for errorchecking
  20.         GLenum errString = glGetError();
  21.  
  22. #pragma endregion
  23.  
  24. #pragma region PREPARE_SHADER
  25. #if SHADER_MODE
  26.         glewExperimental = GL_TRUE; //due to compute shaders
  27.         GLenum err = glewInit();
  28.         if (GLEW_OK != err)
  29.         {
  30.                 // Problem: glewInit failed, something is seriously wrong.
  31.                 fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  32.                 const GLenum errString = glGetError();
  33.                 MessageBox(hwnd,
  34.                         _T(" glewInit() returned ")+(int)(GLint)err, //let's hope this doublecast from GLint to std int works
  35.                         _T("glewInit() failed, something is seriously wrong."),
  36.                         MB_OK | MB_ICONEXCLAMATION);
  37.                
  38.         }//end if
  39.         //fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  40.         //this section is a mix of book and Landon code.
  41.         //const GLubyte* gluErrorString(GLenum errorCode);
  42.         // 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
  43.  
  44.  
  45.         // Create a shader, attach source, and compile.
  46.  
  47.         compute_shader = glCreateShader(GL_COMPUTE_SHADER);
  48.         LPTSTR shaderName = _T("XOR.compute"); //
  49.         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.)
  50.         if( fSource == NULL){
  51.                 const GLenum errString = glGetError();
  52.                
  53.                 return SHADER_NOT_FOUND;
  54.         }
  55.         const GLchar ** compute_source = (const GLchar **)&fSource;
  56.         glShaderSource(compute_shader, 1, compute_source, NULL);
  57.         glCompileShader(compute_shader);
  58.  
  59.         //create a program, attach shader, link
  60.         compute_program = glCreateProgram();
  61.         glAttachShader(compute_program, compute_shader);
  62.         glLinkProgram(compute_program);
  63.  
  64.         //TODO : set up the needed SSBO for the strings (after data import)
  65. #endif
  66. #pragma endregion
  67. #pragma region DATA_IMPORTATION
  68.         //hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); /// dec 2, 4:27pm; not getting valid id, despite code being a copy from File Load Save
  69.         hEdit = hEditGlobal;
  70.         inLenght = GetWindowTextLength(hEdit); // simple check for possible buffer overflow
  71.        
  72.  
  73. #pragma region SANITY_CHECKS
  74.         if ( hEdit == 0 ){ //needs to check before DATA_TOO_BIG
  75.                 return PLAINTEXT_NOT_FOUND;
  76.         }//end debugger if
  77.         if ( _tcslen(key) > inLenght)
  78.                 return DATA_LESSTHAN_KEY; // also the result of a failure to aquire plaintext data
  79.         if ( inLenght > CRYPTO_BUFFER_SIZE )
  80.                 return DATA_TOO_BIG;
  81.        
  82.  
  83.  
  84.         //int debugReturn;
  85.         debugReturn =
  86.                 GetWindowText(hEdit, buffer, CRYPTO_BUFFER_SIZE); //dumps contents of 'hEdit' to 'buffer'.
  87.  
  88. #if DEBUG_OFF
  89.         MessageBox(hwnd, key, "Notice",
  90.                                                         MB_OK | MB_ICONINFORMATION);
  91. #endif 
  92. #pragma endregion
  93.  
  94. #pragma region SHADER_STORAGE_BUFFER_OBJECTS
  95. #if EXPERIMENT_ON
  96. #pragma region KEY
  97.         glGenBuffers(1, &keySSBO);
  98.         glBindBuffer(GL_SHADER_STORAGE_BUFFER, keySSBO);
  99.         glBufferData(GL_SHADER_STORAGE_BUFFER,
  100.                 CRYPTO_KEY_SIZE*sizeof(TCHAR),
  101.                 NULL, //might not want to keep that as null, and replace with contents of LPTSTR key
  102.                 GL_STATIC_DRAW ); //might want GL_DYNAMIC_READ instead?
  103.  
  104.         GLint bufMask = GL_MAP_WRITE_BIT | GL_MAP_READ_BIT ; // the invalidate makes a big difference when re-writing
  105.         // (Dec 12, 2:07 pm) invalidate bit removed, we're doing a full overwrite anyway
  106.        
  107.  
  108.         bufferKey = (LPTSTR) glMapBufferRange( GL_SHADER_STORAGE_BUFFER, 0, CRYPTO_KEY_SIZE*sizeof(TCHAR), bufMask);
  109.         // I'd rather use as simple of a string as I can afford
  110.         //that appears to be bufferKey for now, will later use RoundKey
  111.         if(bufferKey == NULL){
  112.                 errString = glGetError();
  113.                 return BUFFER_NOT_FOUND;
  114.         }
  115.         for (int i = 0; i < CRYPTO_KEY_SIZE; i++)
  116.         {
  117.                 bufferKey[i] = roundKey[i]; // should move this section into the round loop
  118.         }
  119.  
  120.         int chk;
  121.         chk = glUnmapBuffer( GL_SHADER_STORAGE_BUFFER );
  122.         errString = glGetError();
  123.  
  124.  
  125. #if DEBUG_ON
  126.         //should really be doing this before I unmap
  127.         for (int i = 0; i < CRYPTO_KEY_SIZE; i++)
  128.         {
  129.                 roundKey[i] = _T('0');
  130.                 roundKey[i] = bufferKey[i]; // should move this section into the round loop
  131.         }
  132.         MessageBox(hwnd,
  133.                         roundKey, //shaderName
  134.                         _T("Roundkey"),
  135.                         MB_OK | MB_ICONEXCLAMATION);
  136. #endif
  137.  
  138. #pragma endregion
  139. #pragma region TEXT
  140.         if( bufferKey == NULL){
  141.                 errString = glGetError();
  142.                 return BUFFER_NOT_FOUND;
  143.         }
  144.  
  145.         glGenBuffers(1, &txtSSBO);
  146.         glBindBuffer(GL_SHADER_STORAGE_BUFFER, txtSSBO);
  147.         glBufferData(GL_SHADER_STORAGE_BUFFER,
  148.                 CRYPTO_BUFFER_SIZE*sizeof(TCHAR),
  149.                 NULL, //might not want to keep that as null, and replace with contents of LPTSTR key
  150.                 GL_STATIC_DRAW ); //might want GL_DYNAMIC_READ instead?
  151.  
  152.         bufferOut = (LPTSTR) glMapBufferRange( GL_SHADER_STORAGE_BUFFER, 0, CRYPTO_BUFFER_SIZE*sizeof(TCHAR), bufMask);
  153.         for (int i = 0; i < CRYPTO_BUFFER_SIZE; i++)
  154.         {
  155.                 bufferOut[i] = buffer[i];
  156.         }
  157. #if DEBUG_ON
  158.         {
  159.                 LPTSTR checkout = new TCHAR[CRYPTO_BUFFER_SIZE];
  160.                 for (int i = 0; i < CRYPTO_BUFFER_SIZE; i++)
  161.                 {
  162.                         checkout[i] = bufferOut[i]; // should move this section into the round loop
  163.                 }
  164.                 MessageBox(hwnd,
  165.                                 checkout, //contents of bufferOut
  166.                                 _T("text buffer"),
  167.                                 MB_OK | MB_ICONEXCLAMATION);
  168.         }
  169. #endif
  170. //      glUnmapBuffer( GL_SHADER_STORAGE_BUFFER ); //holding onto this one until after the compute has run
  171.         if( bufferKey == NULL){
  172.                 const GLenum errString = glGetError();
  173.                 return BUFFER_NOT_FOUND;
  174.         }
  175. #pragma endregion
  176.         glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 4, keySSBO );
  177.         glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 5, txtSSBO );
  178.  
  179.         errString = glGetError();
  180. #if EXPERIMENT_OFF
  181.         keyLengthLocation = glGetUniformLocation(compute_program, "keyByteLength");
  182.         errString = glGetError();
  183. #endif
  184.  
  185.        
  186. #pragma endregion
  187.  
  188.        
  189. #endif
  190.  
  191. #pragma endregion
  192.  
  193.         glUseProgram(compute_program);
  194.         errString = glGetError();
  195.         keyLengthLocation = 0;
  196.  
  197.         glUniform1ui(keyLengthLocation,  (int)CRYPTO_KEY_SIZE*sizeof(TCHAR)); // error 1282
  198.         errString = glGetError();
  199.        
  200. #pragma region ACTUAL_ENCRYPTION
  201.         if (isForwards == CRYPTO_FORWARDS)
  202.         {
  203. #if !SHADER_MODE
  204.                 for (int i = 0; i < numberOfRounds; i++)
  205.                 {
  206.                         //cpu functionality
  207.                         int chk = GetRoundKey(key, roundKey, i);
  208.        
  209.                         CryptoCollisionCheck(hwnd, roundKey);
  210.                         encryptXORstep(hwnd, buffer, roundKey, bufferOut);
  211.                         _tcscpy(buffer,bufferOut); //so that the next loop will use the results of this loop
  212.                 }//end for number of rounds
  213. #endif
  214. #if SHADER_MODE
  215.  
  216.                 infoLogLength = 0;
  217.                
  218.                 glGetProgramiv(compute_program, GL_INFO_LOG_LENGTH, &infoLogLength) ;
  219.                 GLchar * debugLog = new GLchar[infoLogLength];
  220.                 glGetProgramInfoLog(compute_program, infoLogLength, &infoLogLength, debugLog);
  221.                 if( infoLogLength > 0 )
  222.                         MessageBoxA(hwnd,
  223.                                 (LPSTR)debugLog,
  224.                                 ("ProgramInfoLog"),
  225.                                 MB_OK | MB_ICONEXCLAMATION);
  226.                
  227.                 if ( CompileSuccessful(compute_shader) == FALSE)
  228.                                 return SHADER_COMPILE_FAILED;
  229.                 if( LinkSuccessful(compute_program) == FALSE)
  230.                                 return SHADER_LINK_FAILED;
  231.                
  232.                 errString = glGetError();
  233.  
  234.                 //glGetProgramResourceIndex(compute_program, GL_SHADER_STORAGE_BLOCK);
  235.  
  236.                 { keyLengthLocation; } //for debug reference only
  237.  
  238.                 for (int i = 0; i < numberOfRounds; i++)
  239.                 {
  240.                         int chk = GetRoundKey(key, roundKey, i);
  241.                         //getRoundKey
  242.                         glDispatchCompute(CRYPTO_DISPATCH_VEC3);
  243.                         errString = glGetError();
  244.                         glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT ); // makes all the workgroups wait for each other to finish
  245.                         errString = glGetError();
  246.                 }//end for number of rounds
  247.                 { keyLengthLocation; } //for debug reference only
  248.         }//end if
  249. #endif
  250.         else { // CRYPTO BACKWARDS
  251.                 //still in CPU space until GPU is confirmed functional
  252.                 for (int i = numberOfRounds-1; i >= 0; i--)
  253.                 {
  254.                         int chk = GetRoundKey(key, roundKey, i);
  255.        
  256.                         CryptoCollisionCheck(hwnd, roundKey); //doesn't modify anything, just dialogs an output of "at-risk" characters
  257.                         encryptXORstep(hwnd, buffer, roundKey, bufferOut);
  258.                         _tcscpy(buffer,bufferOut);  //so that the next loop will use the results of this loop
  259.                 }//end for number of rounds
  260.         }//end else
  261.  
  262. #if DEBUG_ON
  263.         {
  264.                 LPTSTR checkout = new TCHAR[CRYPTO_BUFFER_SIZE];
  265.                 for (int i = 0; i < CRYPTO_BUFFER_SIZE; i++)
  266.                 {
  267.                         checkout[i] = bufferOut[i]; // should move this section into the round loop
  268.                 }
  269.                 MessageBox(hwnd,
  270.                                 checkout, //shaderName
  271.                                 _T("ciphertext buffer"),
  272.                                 MB_OK | MB_ICONEXCLAMATION);
  273.         }
  274. #endif
  275.  
  276. #pragma endregion
  277. #pragma region DATA_EXPORTATION
  278.         SetWindowText(hEdit, bufferOut); //sends the encrypted results to the edit window
  279.  
  280.         if (debugReturn > 0) //debugReturn is the number of characters GetWindowText spat out, or 0
  281.                 return NO_ERRORS_DETECTED;
  282.         else
  283.                 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."
  284. #pragma endregion
  285. #if SHADER_MODE
  286.         //delete shader once done with it
  287.         //int chk;
  288.         chk = glUnmapBuffer( GL_SHADER_STORAGE_BUFFER );
  289.         glDeleteShader(compute_shader);
  290. #endif
  291. }//end DDE()
clone this paste RAW Paste Data