int pkcs1_v15_private_key_decrypt(struct crypto_rsa_key *key,
				  const u8 *in, size_t inlen,
				  u8 *out, size_t *outlen)
{
	int res;
	u8 *pos, *end;

	res = crypto_rsa_exptmod(in, inlen, out, outlen, key, 1);
	if (res)
		return res;

	if (*outlen < 2 || out[0] != 0 || out[1] != 2)
		return -1;

	/* Skip PS (pseudorandom non-zero octets) */
	pos = out + 2;
	end = out + *outlen;
	while (*pos && pos < end)
		pos++;
	if (pos == end)
		return -1;
	pos++;

	*outlen -= pos - out;

	/* Strip PKCS #1 header */
	os_memmove(out, pos, *outlen);

	return 0;
}
Beispiel #2
0
int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
                                    const u8 *crypt, size_t crypt_len,
                                    u8 *plain, size_t *plain_len)
{
    size_t len;
    u8 *pos;

    len = *plain_len;
    if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len,
                           (struct crypto_rsa_key *) key, 0) < 0)
        return -1;

    /*
     * PKCS #1 v1.5, 8.1:
     *
     * EB = 00 || BT || PS || 00 || D
     * BT = 01
     * PS = k-3-||D|| times FF
     * k = length of modulus in octets
     */

    if (len < 3 + 8 + 16 /* min hash len */ ||
            plain[0] != 0x00 || plain[1] != 0x01 || plain[2] != 0xff) {
        wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
                   "structure");
        return -1;
    }

    pos = plain + 3;
    while (pos < plain + len && *pos == 0xff)
        pos++;
    if (pos - plain - 2 < 8) {
        /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
        wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
                   "padding");
        return -1;
    }

    if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
        wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
                   "structure (2)");
        return -1;
    }
    pos++;
    len -= pos - plain;

    /* Strip PKCS #1 header */
    os_memmove(plain, pos, len);
    *plain_len = len;

    return 0;
}
int pkcs1_encrypt(int block_type, struct crypto_rsa_key *key,
		  int use_private, const u8 *in, size_t inlen,
		  u8 *out, size_t *outlen)
{
	size_t modlen;

	modlen = crypto_rsa_get_modulus_len(key);

	if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
					    out, outlen) < 0)
		return -1;

	return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
}