int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key) { unsigned char buf2[5120]; unsigned long y, z; _ARGCHK(out != NULL); _ARGCHK(outlen != NULL); _ARGCHK(key != NULL); /* type valid? */ if (!(key->type == PK_PRIVATE || key->type == PK_PRIVATE_OPTIMIZED) && (type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED)) { return CRYPT_PK_INVALID_TYPE; } /* start at offset y=PACKET_SIZE */ y = PACKET_SIZE; /* output key type */ buf2[y++] = type; /* output modulus */ OUTPUT_BIGNUM(&key->N, buf2, y, z); /* output public key */ OUTPUT_BIGNUM(&key->e, buf2, y, z); if (type == PK_PRIVATE || type == PK_PRIVATE_OPTIMIZED) { OUTPUT_BIGNUM(&key->d, buf2, y, z); } if (type == PK_PRIVATE_OPTIMIZED) { OUTPUT_BIGNUM(&key->dQ, buf2, y, z); OUTPUT_BIGNUM(&key->dP, buf2, y, z); OUTPUT_BIGNUM(&key->pQ, buf2, y, z); OUTPUT_BIGNUM(&key->qP, buf2, y, z); OUTPUT_BIGNUM(&key->p, buf2, y, z); OUTPUT_BIGNUM(&key->q, buf2, y, z); } /* check size */ if (*outlen < y) { return CRYPT_BUFFER_OVERFLOW; } /* store packet header */ packet_store_header(buf2, PACKET_SECT_RSA, PACKET_SUB_KEY); /* copy to the user buffer */ memcpy(out, buf2, (size_t)y); *outlen = y; /* clear stack and return */ #ifdef CLEAN_STACK zeromem(buf2, sizeof(buf2)); #endif return CRYPT_OK; }
/** Sign a message digest using a DH private key @param in The data to sign @param inlen The length of the input (octets) @param out [out] The destination of the signature @param outlen [in/out] The max size and resulting size of the output @param prng An active PRNG state @param wprng The index of the PRNG desired @param key A private DH key @return CRYPT_OK if successful */ int dh_sign_hash(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, dh_key *key) { mp_int a, b, k, m, g, p, p1, tmp; unsigned char *buf; unsigned long x, y; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* check parameters */ if (key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } /* is the IDX valid ? */ if (is_valid_idx(key->idx) != 1) { return CRYPT_PK_INVALID_TYPE; } /* allocate ram for buf */ buf = XMALLOC(520); /* make up a random value k, * since the order of the group is prime * we need not check if gcd(k, r) is 1 */ if (prng_descriptor[wprng].read(buf, sets[key->idx].size, prng) != (unsigned long)(sets[key->idx].size)) { err = CRYPT_ERROR_READPRNG; goto LBL_ERR; } /* init bignums */ if ((err = mp_init_multi(&a, &b, &k, &m, &p, &g, &p1, &tmp, NULL)) != MP_OKAY) { err = mpi_to_ltc_error(err); goto LBL_ERR; } /* load k and m */ if ((err = mp_read_unsigned_bin(&m, (unsigned char *)in, inlen)) != MP_OKAY) { goto error; } if ((err = mp_read_unsigned_bin(&k, buf, sets[key->idx].size)) != MP_OKAY) { goto error; } /* load g, p and p1 */ if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error; } if ((err = mp_sub_d(&p, 1, &p1)) != MP_OKAY) { goto error; } if ((err = mp_div_2(&p1, &p1)) != MP_OKAY) { goto error; } /* p1 = (p-1)/2 */ /* now get a = g^k mod p */ if ((err = mp_exptmod(&g, &k, &p, &a)) != MP_OKAY) { goto error; } /* now find M = xa + kb mod p1 or just b = (M - xa)/k mod p1 */ if ((err = mp_invmod(&k, &p1, &k)) != MP_OKAY) { goto error; } /* k = 1/k mod p1 */ if ((err = mp_mulmod(&a, &key->x, &p1, &tmp)) != MP_OKAY) { goto error; } /* tmp = xa */ if ((err = mp_submod(&m, &tmp, &p1, &tmp)) != MP_OKAY) { goto error; } /* tmp = M - xa */ if ((err = mp_mulmod(&k, &tmp, &p1, &b)) != MP_OKAY) { goto error; } /* b = (M - xa)/k */ /* check for overflow */ if ((unsigned long)(PACKET_SIZE + 4 + 4 + mp_unsigned_bin_size(&a) + mp_unsigned_bin_size(&b)) > *outlen) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* store header */ y = PACKET_SIZE; /* now store them both (a,b) */ x = (unsigned long)mp_unsigned_bin_size(&a); STORE32L(x, out+y); y += 4; if ((err = mp_to_unsigned_bin(&a, out+y)) != MP_OKAY) { goto error; } y += x; x = (unsigned long)mp_unsigned_bin_size(&b); STORE32L(x, out+y); y += 4; if ((err = mp_to_unsigned_bin(&b, out+y)) != MP_OKAY) { goto error; } y += x; /* check if size too big */ if (*outlen < y) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* store header */ packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_SIGNED); *outlen = y; err = CRYPT_OK; goto LBL_ERR; error: err = mpi_to_ltc_error(err); LBL_ERR: mp_clear_multi(&tmp, &p1, &g, &p, &m, &k, &b, &a, NULL); XFREE(buf); return err; }
/** Encrypt a short symmetric key with a public DH key @param in The symmetric key to encrypt @param inlen The length of the key (octets) @param out [out] The ciphertext @param outlen [in/out] The max size and resulting size of the ciphertext @param prng An active PRNG state @param wprng The index of the PRNG desired @param hash The index of the hash desired (must produce a digest of size >= the size of the plaintext) @param key The public key you wish to encrypt with. @return CRYPT_OK if successful */ int dh_encrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, int hash, dh_key *key) { unsigned char *pub_expt, *dh_shared, *skey; dh_key pubkey; unsigned long x, y, z, hashsize, pubkeysize; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* check that wprng/hash are not invalid */ if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } if (inlen > hash_descriptor[hash].hashsize) { return CRYPT_INVALID_HASH; } /* allocate memory */ pub_expt = XMALLOC(DH_BUF_SIZE); dh_shared = XMALLOC(DH_BUF_SIZE); skey = XMALLOC(MAXBLOCKSIZE); if (pub_expt == NULL || dh_shared == NULL || skey == NULL) { if (pub_expt != NULL) { XFREE(pub_expt); } if (dh_shared != NULL) { XFREE(dh_shared); } if (skey != NULL) { XFREE(skey); } return CRYPT_MEM; } /* make a random key and export the public copy */ if ((err = dh_make_key(prng, wprng, dh_get_size(key), &pubkey)) != CRYPT_OK) { goto LBL_ERR; } pubkeysize = DH_BUF_SIZE; if ((err = dh_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } /* now check if the out buffer is big enough */ if (*outlen < (1 + 4 + 4 + PACKET_SIZE + pubkeysize + inlen)) { dh_free(&pubkey); err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* make random key */ hashsize = hash_descriptor[hash].hashsize; x = DH_BUF_SIZE; if ((err = dh_shared_secret(&pubkey, key, dh_shared, &x)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } dh_free(&pubkey); z = MAXBLOCKSIZE; if ((err = hash_memory(hash, dh_shared, x, skey, &z)) != CRYPT_OK) { goto LBL_ERR; } /* store header */ packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_ENC_KEY); /* output header */ y = PACKET_SIZE; /* size of hash name and the name itself */ out[y++] = hash_descriptor[hash].ID; /* length of DH pubkey and the key itself */ STORE32L(pubkeysize, out+y); y += 4; for (x = 0; x < pubkeysize; x++, y++) { out[y] = pub_expt[x]; } /* Store the encrypted key */ STORE32L(inlen, out+y); y += 4; for (x = 0; x < inlen; x++, y++) { out[y] = skey[x] ^ in[x]; } *outlen = y; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK /* clean up */ zeromem(pub_expt, DH_BUF_SIZE); zeromem(dh_shared, DH_BUF_SIZE); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(dh_shared); XFREE(pub_expt); return err; }