Ejemplo n.º 1
0
int AsymmCipher::decrypt(const byte* cipher, int cipherlen, byte* out, int numbytes)
{
    Integer m;

    if (!decodeintarray(&m, 1, cipher, cipherlen))
    {
        return 0;
    }

    rsadecrypt(key, &m);

    unsigned l = key[AsymmCipher::PRIV_P].ByteCount() + key[AsymmCipher::PRIV_Q].ByteCount() - 2;

    if (m.ByteCount() > l)
    {
        l = m.ByteCount();
    }

    l -= numbytes;

    while (numbytes--)
    {
        out[numbytes] = m.GetByte(l++);
    }

    return 1;
}
Ejemplo n.º 2
0
int AsymmCipher::decrypt(const byte* c, int cl, byte* out, int numbytes)
{
    Integer m;

    if (!decodeintarray(&m,1,c,cl)) return 0;

    rsadecrypt(key,&m);

    unsigned l = key[AsymmCipher::PRIV_D].ByteCount()-2;

    if (m.ByteCount() > l) l = m.ByteCount();

    l -= numbytes;

    while (numbytes--) out[numbytes] = m.GetByte(l++);

    return 1;
}
extern "C" int rsa_pss_sign(const char *key_file, const unsigned char *msg,
			int len, unsigned char *sig_buf, unsigned char *modulus_buf)
{
	try {
		AutoSeededRandomPool rng;
		FileSource file(key_file, true);
		RSA::PrivateKey key;
		ByteQueue bq;

		// Load the key
		file.TransferTo(bq);
		bq.MessageEnd();
		key.BERDecodePrivateKey(bq, false, bq.MaxRetrievable());

		// Write the modulus
		Integer mod = key.GetModulus();
		// error check
		if (mod.ByteCount() != RCM_RSA_MODULUS_SIZE)
			throw std::length_error("incorrect rsa key modulus length");
		for (int i = 0; i < mod.ByteCount(); i++)
			modulus_buf[i] = mod.GetByte(i);

		// Sign the message
		RSASS<PSS, SHA256>::Signer signer(key);
		size_t length = signer.MaxSignatureLength();
		SecByteBlock signature(length);

		length = signer.SignMessage(rng, msg, len, signature);

		// Copy in reverse order
		for (int i = 0; i < length; i++)
			sig_buf[length - i - 1] = signature[i];
	}
	catch(const CryptoPP::Exception& e) {
		cerr << e.what() << endl;
		return 1;
	}
	catch(std::length_error& le) {
		cerr << "Error: " << le.what() << endl;
		return 1;
	}

	return 0;
}