Example #1
0
PyMODINIT_FUNC
init_pycryptopp(void) {
    PyObject *module;

    module = Py_InitModule3("_pycryptopp", _pycryptopp_functions, _pycryptopp__doc__);
    if (!module)
      return;

    PyObject* version;

    /* a tuple of (Crypto++ version, extra-version) */
    #ifndef DISABLE_EMBEDDED_CRYPTOPP
    /* In the version of Crypto++ which is included in pycryptopp, there is a
       symbol named `cryptopp_extra_version' which is declared (external
       variable) in config.h and defined in cryptlib.cpp. Of course it is
       possible that the header file we've #include'd is from the
       embedded-in-pycryptopp version of Crypto++ but the dynamically linked
       library that we load is from an older version which doesn't have this
       symbol. In that case, the load will fail before we get this far. */
    version = Py_BuildValue("is", CRYPTOPP_VERSION, cryptopp_extra_version);
    #else
    version = Py_BuildValue("iO", CRYPTOPP_VERSION, Py_None);
    #endif

    int succ = PyModule_AddObject(module, "cryptopp_version", version);
    if (succ != 0)
        return;


    init_ecdsa(module);
    init_rsa(module);
    init_sha256(module);
    init_aes(module);
    init_xsalsa20(module);
}
Example #2
0
struct rsa_key new_rsa_key(int bits) {
	init_rsa();

	mpz_t* e = (mpz_t*)calloc(1, sizeof(mpz_t));
	mpz_t* d = (mpz_t*)calloc(1, sizeof(mpz_t));
	mpz_t* n = (mpz_t*)calloc(1, sizeof(mpz_t));
	mpz_t p, q, et;
	int r = 0;

	mpz_init(*e); mpz_init(*d); mpz_init(*n);
	mpz_init(p); mpz_init(q); mpz_init(et);

	while (r == 0) {
		mpz_urandomb(p, rsa_rand_state, bits/2);
		mpz_nextprime(p, p);
		mpz_urandomb(q, rsa_rand_state, bits/2);
		mpz_nextprime(q, q);

		mpz_mul(*n, p, q);
		mpz_sub_ui(p, p, 1);
		mpz_sub_ui(q, q, 1);
		mpz_mul(et, p, q);
		mpz_set_ui(*e, 3);
		r = mpz_invert(*d, *e, et);
	}

	return (rsa_key) {.e = e, .d = d, .n = n};
}