- * SILC PKCS API for RSA
- */
-
-/* Generates RSA key pair. */
-
-SILC_PKCS_API_INIT(rsa)
-{
- uint32 prime_bits = keylen / 2;
- SilcMPInt p, q;
-
- printf("Generating RSA Public and Private keys, might take a while...\n");
-
- silc_mp_init(&p);
- silc_mp_init(&q);
-
- /* Find p and q */
- retry_primes:
- printf("Finding p: ");
- silc_math_gen_prime(&p, prime_bits, TRUE);
-
- printf("\nFinding q: ");
- silc_math_gen_prime(&q, prime_bits, TRUE);
-
- if ((silc_mp_cmp(&p, &q)) == 0) {
- printf("\nFound equal primes, not good, retrying...\n");
- goto retry_primes;
- }
-
- /* If p is smaller than q, switch them */
- if ((silc_mp_cmp(&p, &q)) > 0) {
- SilcMPInt hlp;
- silc_mp_init(&hlp);
-
- silc_mp_set(&hlp, &p);
- silc_mp_set(&p, &q);
- silc_mp_set(&q, &hlp);
-
- silc_mp_uninit(&hlp);
- }
-
- /* Generate the actual keys */
- rsa_generate_keys((RsaKey *)context, keylen, &p, &q);
-
- silc_mp_uninit(&p);
- silc_mp_uninit(&q);
-
- printf("\nKeys generated succesfully.\n");
-
- return TRUE;
-}
-
-SILC_PKCS_API_CLEAR_KEYS(rsa)
-{
- rsa_clear_keys((RsaKey *)context);
-}
-
-/* Returns SILC style encoded RSA public key. */
-
-SILC_PKCS_API_GET_PUBLIC_KEY(rsa)
-{
- RsaKey *key = (RsaKey *)context;
- unsigned char *e, *n, *ret;
- uint32 e_len, n_len;
- unsigned char tmp[4];
-
- e = silc_mp_mp2bin(&key->e, 0, &e_len);
- n = silc_mp_mp2bin(&key->n, key->bits / 8, &n_len);
-
- *ret_len = e_len + 4 + n_len + 4;
- ret = silc_calloc(*ret_len, sizeof(unsigned char));
-
- /* Put the length of the e. */
- SILC_PUT32_MSB(e_len, tmp);
- memcpy(ret, tmp, 4);
-
- /* Put the e. */
- memcpy(ret + 4, e, e_len);
-
- /* Put the length of the n. */
- SILC_PUT32_MSB(n_len, tmp);
- memcpy(ret + 4 + e_len, tmp, 4);
-
- /* Put the n. */
- memcpy(ret + 4 + e_len + 4, n, n_len);
-
- memset(e, 0, e_len);
- memset(n, 0, n_len);
- silc_free(e);
- silc_free(n);
-
- return ret;
-}
-
-/* Returns SILC style encoded RSA private key. Public key is always
- returned in private key as well. Public keys are often derived
- directly from private key. */
-
-SILC_PKCS_API_GET_PRIVATE_KEY(rsa)
-{
- RsaKey *key = (RsaKey *)context;
- unsigned char *e, *n, *d, *ret;
- uint32 e_len, n_len, d_len;
- unsigned char tmp[4];
-
- e = silc_mp_mp2bin(&key->e, 0, &e_len);
- n = silc_mp_mp2bin(&key->n, key->bits / 8, &n_len);
- d = silc_mp_mp2bin(&key->d, 0, &d_len);
-
- *ret_len = e_len + 4 + n_len + 4 + d_len + 4;
- ret = silc_calloc(*ret_len, sizeof(unsigned char));
-
- /* Put the length of the e. */
- SILC_PUT32_MSB(e_len, tmp);
- memcpy(ret, tmp, 4);
-
- /* Put the e. */
- memcpy(ret + 4, e, e_len);
-
- /* Put the length of the n. */
- SILC_PUT32_MSB(n_len, tmp);
- memcpy(ret + 4 + e_len, tmp, 4);
-
- /* Put the n. */
- memcpy(ret + 4 + e_len + 4, n, n_len);
-
- /* Put the length of the d. */
- SILC_PUT32_MSB(d_len, tmp);
- memcpy(ret + 4 + e_len + 4 + n_len, tmp, 4);
-
- /* Put the n. */
- memcpy(ret + 4 + e_len + 4 + n_len + 4, d, d_len);
-
- memset(e, 0, e_len);
- memset(n, 0, n_len);
- memset(d, 0, d_len);
- silc_free(e);
- silc_free(n);
- silc_free(d);
-
- return ret;
-}
-
-/* Set public key */
-
-SILC_PKCS_API_SET_PUBLIC_KEY(rsa)
-{
- RsaKey *key = (RsaKey *)context;
- unsigned char tmp[4];
- uint32 e_len, n_len;
-
- silc_mp_init(&key->e);
- silc_mp_init(&key->n);
-
- memcpy(tmp, key_data, 4);
- SILC_GET32_MSB(e_len, tmp);
- if (e_len > key_len) {
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->n);
- return 0;
- }
-
- silc_mp_bin2mp(key_data + 4, e_len, &key->e);
-
- memcpy(tmp, key_data + 4 + e_len, 4);
- SILC_GET32_MSB(n_len, tmp);
- if (e_len + n_len > key_len) {
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->n);
- return 0;
- }
-
- silc_mp_bin2mp(key_data + 4 + e_len + 4, n_len, &key->n);
-
- key->bits = n_len * 8;
-
- return key->bits;
-}
-
-/* Set private key. This derives the public key from the private
- key and sets the public key as well. Public key should not be set
- already and should not be set after setting private key. */
-
-SILC_PKCS_API_SET_PRIVATE_KEY(rsa)
-{
- RsaKey *key = (RsaKey *)context;
- unsigned char tmp[4];
- uint32 e_len, n_len, d_len;
-
- silc_mp_init(&key->e);
- silc_mp_init(&key->n);
- silc_mp_init(&key->d);
-
- memcpy(tmp, key_data, 4);
- SILC_GET32_MSB(e_len, tmp);
- if (e_len > key_len) {
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->n);
- return FALSE;
- }
-
- silc_mp_bin2mp(key_data + 4, e_len, &key->e);
-
- memcpy(tmp, key_data + 4 + e_len, 4);
- SILC_GET32_MSB(n_len, tmp);
- if (e_len + n_len > key_len) {
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->n);
- return FALSE;
- }
-
- silc_mp_bin2mp(key_data + 4 + e_len + 4, n_len, &key->n);
-
- memcpy(tmp, key_data + 4 + e_len + 4 + n_len, 4);
- SILC_GET32_MSB(d_len, tmp);
- if (e_len + n_len + d_len > key_len) {
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->n);
- return FALSE;
- }