void HandshakeResponder::responderRespondHello(){ //R:1 forge QByteArray clearText; clearText.append(SUPPORTED_PROTOCOL_VERSION); QByteArray firstHalfSymKey = generateRandomBlock(32); m_gcmKey.append(firstHalfSymKey.left(16)); //first half key m_gcmBaseIV.append(firstHalfSymKey.right(16));//first half IV clearText.append(firstHalfSymKey); updateIntegrityHash(&m_responderIntegrityHash, clearText); disconnect(m_socket, &AbstractLink::readyRead, this, 0); connect(m_socket, &AbstractLink::readyRead, this, &HandshakeResponder::responderParseHalfKeyAndResponderIntegrity); m_socketStream << rsaEncrypt(clearText); m_timeout.start(); }
void initFlashData(int *success){ int aesErr = 0; //error handling int rsaErr = 0; // hello(REP_TRUSTED_NAME); printf("In trusted mode initFlashData\n"); // TRUSTED_DATA static char data[50000] = "Messenger: Choose your next words carefully, Leonidas. They may be your last as king.\nKing Leonidas: [to himself: thinking] \"Earth and water\"?\n[Leonidas unsheathes and points his sword at the Messenger's throat]\nMessenger: Madman! You're a madman!\nKing Leonidas: Earth and water? You'll find plenty of both down there.\nMessenger: No man, Persian or Greek, no man threatens a messenger!\nKing Leonidas: You bring the crowns and heads of conquered kings to my city steps. You insult my queen. You threaten my people with slavery and death! Oh, I've chosen my words carefully, Persian. Perhaps you should have done the same!\nMessenger: This is blasphemy! This is madness!\nKing Leonidas: Madness...?\n[shouting]\nKing Leonidas: THIS IS SPARTA!\n[Kicks the messenger down the well]\n"; // 32 bytes HEX key digits used as key to AES-128 // TRUSTED_DATA static char sessionKey[33] = "0123456789abcdeffedcba9876543210"; AES key char sessionKey[32]; generateAESKey(sessionKey); //generates a random 16 byte AES key (AES-128) nbytes = strlen(data); //setting the global variable, used in aesDecrypt printf("\nEncrypting message with random generated AES-128 one time session key:\n%s",sessionKey); aesErr = aesEncrypt(sessionKey,data,encrypted); memcpy(contract,encrypted,nbytes); #if 1 if(aesErr != 0){ printf("AES encryption failed.\n"); // *success = 0; } else{ printf("\nTry to print encrypted data! \n %s \n\n", encrypted); printf("\n Encrypting AES session key with RSA\n"); rsaErr = rsaEncrypt(sessionKey,g_e,g_modulus,encryptedAESKey); } if(rsaErr != 0){ printf("RSA encryption failed.\n"); //*success = 0; } else if(aesErr == 0 && rsaErr == 0){ // *success = 1; } #endif }
int main(int argc, char* argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; TPMI_DH_OBJECT keyHandle; TPM2B_PUBLIC_KEY_RSA message; char outFilePath[PATH_MAX] = {0}; char *contextKeyFile = NULL; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvk:I:o:p:d:c:"; static struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"keyHandle",1,NULL,'k'}, {"inFile",1,NULL,'I'}, {"outFile",1,NULL,'o'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {"keyContext",1,NULL,'c'}, {0,0,0,0} }; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, k_flag = 0, I_flag = 0, c_flag = 0, o_flag = 0; if(argc == 1) { showHelp(argv[0]); return 0; } while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'k': if(getSizeUint32Hex(optarg,&keyHandle) != 0) { returnVal = -1; break; } k_flag = 1; break; case 'I': message.t.size = sizeof(message) - 2; if(loadDataFromFile(optarg, message.t.buffer, &message.t.size) != 0) { returnVal = -2; break; } I_flag = 1; break; case 'o': safeStrNCpy(outFilePath, optarg, sizeof(outFilePath)); if(checkOutFile(outFilePath) != 0) { returnVal = -3; break; } o_flag = 1; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -4; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -5; } break; case 'c': contextKeyFile = optarg; if(contextKeyFile == NULL || contextKeyFile[0] == '\0') { returnVal = -6; break; } printf("contextKeyFile = %s\n", contextKeyFile); c_flag = 1; break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -7; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -8; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) return returnVal; flagCnt = h_flag + v_flag + k_flag + I_flag + o_flag + c_flag; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); return -9; } } else if((flagCnt == 3) && (k_flag == 1 || c_flag == 1) && (I_flag == 1) && (o_flag == 1)) { prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &keyHandle, contextKeyFile); if (returnVal == 0) returnVal = rsaEncrypt(keyHandle, &message, outFilePath); finishTest(); if(returnVal) return -10; } else { showArgMismatch(argv[0]); return -11; } return 0; }