Beispiel #1
0
	string DataFileDB::FileName(const string& entry) const
	{
		if(entry=="")
			return dir+"/data/[empty_string]";
		
		return dir+"/data/"+HexEncode(entry);
	}
Beispiel #2
0
void BM_EncodePi(benchmark::State& state) {  // NOLINT(runtime/references)
  bool correct = true;
  state.PauseTiming();
  std::vector<uint8_t> const input_bytes = Bytes();
  std::vector<uint8_t> const expected_digits = Digits();
  state.ResumeTiming();
  while (state.KeepRunning()) {
    HexEncode(&state, &correct, input_bytes, expected_digits);
  }
  std::stringstream ss;
  ss << correct;
  state.SetLabel(ss.str());
}
Beispiel #3
0
        virtual void DoWrite(const void* data, size_t len) {
            const char* b = (const char*)data;

            while (len) {
                const unsigned char c = *b;
                char buf[12];
                char* tmp = buf;

                if (mC % COLS == 0) {
                    *tmp++ = ' ';
                    *tmp++ = ' ';
                    *tmp++ = ' ';
                    *tmp++ = ' ';
                }

                if (mC && mC % COLS != 0) {
                    *tmp++ = ',';
                    *tmp++ = ' ';
                }

                *tmp++ = '0';
                *tmp++ = 'x';
                tmp = HexEncode(&c, 1, tmp);

                if ((mC % COLS) == (COLS - 1)) {
                    *tmp++ = ',';
                    *tmp++ = ' ';
                    *tmp++ = '\n';
                }

                mSlave->Write(buf, tmp - buf);

                --len;
                ++b;
                ++mC;
            }
        }
Beispiel #4
0
void CzString::HexEncode()
{
	HexEncode(Data, Length);
}
void
CHTTPXcertServer::SendCertificateRequest(
    PGPKeyRef		inCAKey,
    PGPKeyRef		inRequestKey,
    const void *	inBuffer,
    PGPSize			inBufferSize,
    void **			outBuffer,
    PGPSize *		outBufferSize)
{
    try {
        PGPError			pgpErr;
        StPGPDataRef		result;
        StPGPDataRef		digest;
        PGPSize				digestSize;
        StPGPDataRef		cnk;
        PGPSize				cnkSize;
        StPGPDataRef		encodedCNK;
        PGPSize				bufSize;
        StPGPDataRef		post;
        PGPSize				postSize;
        StPGPDataRef		encodedBuffer;

        InitOperation();
        if (! PGPKeyRefIsValid(inCAKey)) {
            ThrowPGPError_(kPGPError_OptionNotFound);
        }
        if (! PGPKeyRefIsValid(inRequestKey)) {
            ThrowPGPError_(kPGPError_OptionNotFound);
        }

        digest = static_cast<PGPByte *>(PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                        GetMaxDigestedKeyBufferSize(),
                                        kPGPMemoryMgrFlags_None));
        if (digest == 0) {
            ThrowPGPError_(kPGPError_OutOfMemory);
        };
        digestSize = DigestKey(inCAKey, digest);

        encodedBuffer = static_cast<PGPByte *>(
                            PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                        GetMaxBase64EncodedBufferSize(inBufferSize),
                                        kPGPMemoryMgrFlags_None));
        if (encodedBuffer == 0) {
            ThrowPGPError_(kPGPError_OutOfMemory);
        };
        bufSize = Base64Encode(	static_cast<const PGPByte *>(inBuffer),
                                inBufferSize,
                                encodedBuffer);
        pgpErr = PGPGetKeyPropertyBuffer(inRequestKey, kPGPKeyPropX509MD5Hash, 0, 0, &cnkSize);
        ThrowIfPGPError_(pgpErr);
        cnk = static_cast<PGPByte *>(PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                     cnkSize,
                                     kPGPMemoryMgrFlags_None));
        if (cnk == 0) {
            ThrowPGPError_(kPGPError_OutOfMemory);
        }
        pgpErr = PGPGetKeyPropertyBuffer(	inRequestKey,
                                            kPGPKeyPropX509MD5Hash,
                                            cnkSize,
                                            static_cast<PGPByte *>(cnk),
                                            &cnkSize);
        ThrowIfPGPError_(pgpErr);
        encodedCNK = static_cast<PGPByte *>(PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                            GetMaxHexEncodedBufferSize(cnkSize),
                                            kPGPMemoryMgrFlags_None));
        if (encodedCNK == 0) {
            ThrowPGPError_(kPGPError_OutOfMemory);
        }
        cnkSize = HexEncode(cnk, cnkSize, encodedCNK);
        cnk.Free();
        post = static_cast<PGPByte *>(
                   PGPNewData(	PGPGetContextMemoryMgr(mContext),
                               strlen(kCA) + digestSize + strlen(kCNK) + cnkSize
                               + strlen(kPKCS10_input) + GetMaxURLEncodedBufferSize(bufSize),
                               kPGPMemoryMgrFlags_None));
        if (post == 0) {
            ThrowPGPError_(kPGPError_OutOfMemory);
        };
        postSize = sprintf(post, "%s%s%s%s%s", kCA, static_cast<char *>(digest), kCNK,
                           static_cast<char *>(encodedCNK), kPKCS10_input);
        digest.Free();
        encodedCNK.Free();
        postSize += URLEncode(encodedBuffer, bufSize, static_cast<char *>(post) + postSize);
        encodedBuffer.Free();
        GetPost(	kPGPKeyServerState_Uploading,
                    (mPath == 0) ? kDefaultRequestPath : mPath,
                    &result,
                    kContentType,
                    postSize,
                    post);
        post.Free();
        ProcessSendCertResult(result, outBuffer, outBufferSize);
    }

    catch (...) {
        if (mCanceled) {
            ThrowPGPError_(kPGPError_UserAbort);
        } else {
            throw;
        }
    }
}
PGPUInt32
CHTTPXcertServer::DigestKey(
    PGPKeyRef	inKey,
    char *		inOutputBuffer)
{
    StPGPKeySetRef		singleKeySet;
    StPGPHashContextRef	hashContext;
    StPGPDataRef		buffer;
    PGPSize				bufSize;
    StPGPDataRef		encodedBuffer;
    PGPError			pgpErr;

    pgpErr = PGPNewSingletonKeySet(inKey, &singleKeySet);
    ThrowIfPGPError_(pgpErr);

    pgpErr = PGPExportKeySet(	singleKeySet,
                                PGPOExportFormat(mContext, kPGPExportFormat_X509Cert),
                                PGPOAllocatedOutputBuffer(	mContext,
                                        (void **) &buffer,
                                        MAX_PGPSize,
                                        &bufSize),
                                PGPOLastOption(mContext));
    ThrowIfPGPError_(pgpErr);
    singleKeySet.Free();
    encodedBuffer = static_cast<PGPByte *>(
                        PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                    GetMaxBase64EncodedBufferSize(bufSize),
                                    kPGPMemoryMgrFlags_None));
    if (encodedBuffer == 0) {
        ThrowPGPError_(kPGPError_OutOfMemory);
    };
    bufSize = Base64Encode(	static_cast<const PGPByte *>(buffer),
                            bufSize,
                            encodedBuffer);
    buffer.Free();
    pgpErr = PGPNewHashContext(	PGPGetContextMemoryMgr(mContext),
                                kPGPHashAlgorithm_MD5,
                                &hashContext);
    ThrowIfPGPError_(pgpErr);

    // We have to skip the CRs in order to match XCert's hash
    char *	cur = encodedBuffer;
    char *	next;

    while ((next = strchr(cur, '\r')) != 0) {
        pgpErr = PGPContinueHash(hashContext, cur, next - cur);
        ThrowIfPGPError_(pgpErr);
        cur = next + 1;
    }
    pgpErr = PGPContinueHash(hashContext, cur, strlen(cur));
    ThrowIfPGPError_(pgpErr);
    encodedBuffer.Free();
    pgpErr = PGPGetHashSize(hashContext, &bufSize);
    buffer = static_cast<PGPByte *>(PGPNewData(	PGPGetContextMemoryMgr(mContext),
                                    bufSize,
                                    kPGPMemoryMgrFlags_None));
    if (buffer == 0) {
        ThrowPGPError_(kPGPError_OutOfMemory);
    };
    pgpErr = PGPFinalizeHash(hashContext, static_cast<char *>(buffer));
    ThrowIfPGPError_(pgpErr);
    return HexEncode(buffer, bufSize, inOutputBuffer);
}
Beispiel #7
0
int main(int argc, char *argv[])
#endif
{
#ifdef _CRTDBG_LEAK_CHECK_DF
	// Turn on leak-checking
	int tempflag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
	tempflag |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag( tempflag );
#endif

#if defined(__MWERKS__) && defined(macintosh)
	argc = ccommand(&argv);
#endif

	try
	{
		std::string command, executableName, edcFilename;

		if (argc < 2)
			command = 'h';
		else
			command = argv[1];

		if (FIPS_140_2_ComplianceEnabled())
		{
			edcFilename = "edc.dat";

#ifdef CRYPTOPP_WIN32_AVAILABLE
			TCHAR filename[MAX_PATH];
			GetModuleFileName(GetModuleHandle(NULL), filename, sizeof(filename));
			executableName = filename;
			std::string::size_type pos = executableName.rfind('\\');
			if (pos != std::string::npos)
				edcFilename = executableName.substr(0, pos+1) + edcFilename;
#else
			executableName = argv[0];
#endif

			if (command.substr(0, 4) != "fips")
			{
				byte expectedModuleDigest[SHA1::DIGESTSIZE];
				FileSource(edcFilename.c_str(), true, new HexDecoder(new ArraySink(expectedModuleDigest, sizeof(expectedModuleDigest))));

				DoPowerUpSelfTest(executableName.c_str(), expectedModuleDigest);
			}
		}

		switch (command[0])
		{
		case 'g':
		  {
			char seed[1024], privFilename[128], pubFilename[128];
			unsigned int keyLength;

			cout << "Key length in bits: ";
			cin >> keyLength;

			cout << "\nSave private key to file: ";
			cin >> privFilename;

			cout << "\nSave public key to file: ";
			cin >> pubFilename;

			cout << "\nRandom Seed: ";
			ws(cin);
			cin.getline(seed, 1024);

			GenerateRSAKey(keyLength, privFilename, pubFilename, seed);
			return 0;
		  }
		case 'r':
		  {
			switch (argv[1][1])
			{
			case 's':
				RSASignFile(argv[2], argv[3], argv[4]);
				return 0;
			case 'v':
			  {
				bool verified = RSAVerifyFile(argv[2], argv[3], argv[4]);
				cout << (verified ? "valid signature" : "invalid signature") << endl;
				return 0;
			  }
			default:
			  {
				char privFilename[128], pubFilename[128];
				char seed[1024], message[1024];

				cout << "Private key file: ";
				cin >> privFilename;

				cout << "\nPublic key file: ";
				cin >> pubFilename;

				cout << "\nRandom Seed: ";
				ws(cin);
				cin.getline(seed, 1024);

				cout << "\nMessage: ";
				cin.getline(message, 1024);

				string ciphertext = RSAEncryptString(pubFilename, seed, message);
				cout << "\nCiphertext: " << ciphertext << endl;

				string decrypted = RSADecryptString(privFilename, ciphertext.c_str());
				cout << "\nDecrypted: " << decrypted << endl;

				return 0;
			  }
			}
		  }
		case 'm':
			DigestFile(argv[2]);
			return 0;
		case 't':
		  {
			if (command == "tv")
			{
				return !RunTestDataFile(argv[2]);
			}
			// VC60 workaround: use char array instead of std::string to workaround MSVC's getline bug
			char passPhrase[MAX_PHRASE_LENGTH], plaintext[1024];

			cout << "Passphrase: ";
			cin.getline(passPhrase, MAX_PHRASE_LENGTH);

			cout << "\nPlaintext: ";
			cin.getline(plaintext, 1024);

			string ciphertext = EncryptString(plaintext, passPhrase);
			cout << "\nCiphertext: " << ciphertext << endl;

			string decrypted = DecryptString(ciphertext.c_str(), passPhrase);
			cout << "\nDecrypted: " << decrypted << endl;

			return 0;
		  }
		case 'e':
		case 'd':
			if (command == "e64")
				Base64Encode(argv[2], argv[3]);
			else if (command == "d64")
				Base64Decode(argv[2], argv[3]);
			else if (command == "e16")
				HexEncode(argv[2], argv[3]);
			else if (command == "d16")
				HexDecode(argv[2], argv[3]);
			else
			{
				char passPhrase[MAX_PHRASE_LENGTH];
				cout << "Passphrase: ";
				cin.getline(passPhrase, MAX_PHRASE_LENGTH);
				if (command == "e")
					EncryptFile(argv[2], argv[3], passPhrase);
				else
					DecryptFile(argv[2], argv[3], passPhrase);
			}
			return 0;
		case 's':
			if (argv[1][1] == 's')
			{
				char seed[1024];
				cout << "\nRandom Seed: ";
				ws(cin);
				cin.getline(seed, 1024);
				SecretShareFile(atoi(argv[2]), atoi(argv[3]), argv[4], seed);
			}
			else
				SecretRecoverFile(argc-3, argv[2], argv+3);
			return 0;
		case 'i':
			if (argv[1][1] == 'd')
				InformationDisperseFile(atoi(argv[2]), atoi(argv[3]), argv[4]);
			else
				InformationRecoverFile(argc-3, argv[2], argv+3);
			return 0;
		case 'v':
			return !Validate(argc>2 ? atoi(argv[2]) : 0, argv[1][1] == 'v', argc>3 ? argv[3] : NULL);
		case 'b':
			if (argc<3)
				BenchMarkAll();
			else
				BenchMarkAll((float)atof(argv[2]));
			return 0;
		case 'z':
			GzipFile(argv[3], argv[4], argv[2][0]-'0');
			return 0;
		case 'u':
			GunzipFile(argv[2], argv[3]);
			return 0;
		case 'f':
			if (command == "fips")
				FIPS140_SampleApplication(executableName.c_str(), edcFilename.c_str());
			else if (command == "fips-rand")
				FIPS140_GenerateRandomFiles();
			else if (command == "ft")
				ForwardTcpPort(argv[2], argv[3], argv[4]);
			return 0;
		case 'a':
			if (AdhocTest)
				return (*AdhocTest)(argc, argv);
			else
				return 0;
		default:
			FileSource usage("usage.dat", true, new FileSink(cout));
			return 1;
		}
	}
	catch(CryptoPP::Exception &e)
	{
		cout << "\nCryptoPP::Exception caught: " << e.what() << endl;
		return -1;
	}
	catch(std::exception &e)
	{
		cout << "\nstd::exception caught: " << e.what() << endl;
		return -2;
	}
}