- 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];
- SilcUInt32 e_len, n_len;
-
- if (key->pub_set) {
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->e);
- key->pub_set = FALSE;
- }
-
- 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;
- key->pub_set = TRUE;
-
- 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];
- SilcUInt32 e_len, n_len, d_len;
-
- if (key->prv_set) {
- silc_mp_uninit(&key->d);
- key->prv_set = FALSE;
- }
-
- if (key->pub_set) {
- silc_mp_uninit(&key->e);
- silc_mp_uninit(&key->n);
- key->pub_set = FALSE;
- }
-
- 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);
- silc_mp_uninit(&key->d);
- 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);
- silc_mp_uninit(&key->d);
- 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);
- silc_mp_uninit(&key->d);
- return FALSE;
- }
-
- silc_mp_bin2mp(key_data + 4 + e_len + 4 + n_len + 4, d_len, &key->d);
-
- key->bits = n_len * 8;
- key->prv_set = TRUE;
- key->pub_set = TRUE;
-
- return TRUE;
-}
-
-SILC_PKCS_API_CONTEXT_LEN(rsa)
-{
- return sizeof(RsaKey);
-}
-
-SILC_PKCS_API_ENCRYPT(rsa)
-{
- RsaKey *key = (RsaKey *)context;
- int i, tmplen;
- SilcMPInt mp_tmp;
- SilcMPInt mp_dst;
-
- silc_mp_init(&mp_tmp);
- silc_mp_init(&mp_dst);
- silc_mp_set_ui(&mp_tmp, 0);
- silc_mp_set_ui(&mp_dst, 0);
-
- /* Format the data into MP int */
- for (i = 0; i < src_len; i++) {
- silc_mp_mul_2exp(&mp_tmp, &mp_tmp, 8);
- silc_mp_add_ui(&mp_tmp, &mp_tmp, src[i]);
- }
-
- /* Encrypt */
- rsa_en_de_crypt(&mp_dst, &mp_tmp, &key->e, &key->n);
-
- tmplen = (key->bits + 7) / 8;
-
- /* Format the MP int back into data */
- for (i = tmplen; i > 0; i--) {
- dst[i - 1] = (unsigned char)(silc_mp_get_ui(&mp_dst) & 0xff);
- silc_mp_div_2exp(&mp_dst, &mp_dst, 8);
- }
- *dst_len = tmplen;
-
- silc_mp_uninit(&mp_tmp);
- silc_mp_uninit(&mp_dst);
-
- return TRUE;
-}
-
-SILC_PKCS_API_DECRYPT(rsa)
-{
- RsaKey *key = (RsaKey *)context;
- int i, tmplen;
- SilcMPInt mp_tmp;
- SilcMPInt mp_dst;
-
- silc_mp_init(&mp_tmp);
- silc_mp_init(&mp_dst);
- silc_mp_set_ui(&mp_tmp, 0);
- silc_mp_set_ui(&mp_dst, 0);
-
- /* Format the data into MP int */
- for (i = 0; i < src_len; i++) {
- silc_mp_mul_2exp(&mp_tmp, &mp_tmp, 8);
- silc_mp_add_ui(&mp_tmp, &mp_tmp, src[i]);
- }
-
- /* Decrypt */
- rsa_en_de_crypt(&mp_dst, &mp_tmp, &key->d, &key->n);
-
- tmplen = (key->bits + 7) / 8;
-
- /* Format the MP int back into data */
- for (i = tmplen; i > 0; i--) {
- dst[i - 1] = (unsigned char)(silc_mp_get_ui(&mp_dst) & 0xff);
- silc_mp_div_2exp(&mp_dst, &mp_dst, 8);
- }
- *dst_len = tmplen;
-
- silc_mp_uninit(&mp_tmp);
- silc_mp_uninit(&mp_dst);
-
- return TRUE;
-}
-
-SILC_PKCS_API_SIGN(rsa)
-{
- RsaKey *key = (RsaKey *)context;
- int i, tmplen;
- SilcMPInt mp_tmp;
- SilcMPInt mp_dst;
-
- silc_mp_init(&mp_tmp);
- silc_mp_init(&mp_dst);
- silc_mp_set_ui(&mp_tmp, 0);
- silc_mp_set_ui(&mp_dst, 0);
-
- /* Format the data into MP int */
- for (i = 0; i < src_len; i++) {
- silc_mp_mul_2exp(&mp_tmp, &mp_tmp, 8);
- silc_mp_add_ui(&mp_tmp, &mp_tmp, src[i]);
- }
-
- /* Sign */
- rsa_en_de_crypt(&mp_dst, &mp_tmp, &key->d, &key->n);
-
- tmplen = (key->bits + 7) / 8;