p_enc.c revision bdfb8ad83da0647e9b9a32792598e8ce7ba3ef4d
105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* crypto/evp/p_enc.c */
205436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang * All rights reserved.
405436638acc7c010349a69c3395f1a57c642dc62Ying Wang *
505436638acc7c010349a69c3395f1a57c642dc62Ying Wang * This package is an SSL implementation written
605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * by Eric Young (eay@cryptsoft.com).
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * The implementation was written so as to conform with Netscapes SSL.
805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *
905436638acc7c010349a69c3395f1a57c642dc62Ying Wang * This library is free for commercial and non-commercial use as long as
1005436638acc7c010349a69c3395f1a57c642dc62Ying Wang * the following conditions are aheared to.  The following conditions
1105436638acc7c010349a69c3395f1a57c642dc62Ying Wang * apply to all code found in this distribution, be it the RC4, RSA,
1205436638acc7c010349a69c3395f1a57c642dc62Ying Wang * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1305436638acc7c010349a69c3395f1a57c642dc62Ying Wang * included with this distribution is covered by the same copyright terms
1405436638acc7c010349a69c3395f1a57c642dc62Ying Wang * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1505436638acc7c010349a69c3395f1a57c642dc62Ying Wang *
1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * Copyright remains Eric Young's, and as such any Copyright notices in
1705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * the code are not to be removed.
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang * If this package is used in a product, Eric Young should be given attribution
1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang * as the author of the parts of the library used.
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang * This can be in the form of a textual message at program startup or
2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang * in documentation (online or textual) provided with the package.
2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang *
2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang * Redistribution and use in source and binary forms, with or without
2405436638acc7c010349a69c3395f1a57c642dc62Ying Wang * modification, are permitted provided that the following conditions
2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang * are met:
2605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * 1. Redistributions of source code must retain the copyright
2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    notice, this list of conditions and the following disclaimer.
2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang * 2. Redistributions in binary form must reproduce the above copyright
2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    notice, this list of conditions and the following disclaimer in the
3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    documentation and/or other materials provided with the distribution.
3105436638acc7c010349a69c3395f1a57c642dc62Ying Wang * 3. All advertising materials mentioning features or use of this software
3205436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    must display the following acknowledgement:
3305436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    "This product includes cryptographic software written by
3405436638acc7c010349a69c3395f1a57c642dc62Ying Wang *     Eric Young (eay@cryptsoft.com)"
3505436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    The word 'cryptographic' can be left out if the rouines from the library
3605436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    being used are not cryptographic related :-).
3705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * 4. If you include any Windows specific code (or a derivative thereof) from
3805436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    the apps directory (application code) you must include an acknowledgement:
3905436638acc7c010349a69c3395f1a57c642dc62Ying Wang *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4005436638acc7c010349a69c3395f1a57c642dc62Ying Wang *
4105436638acc7c010349a69c3395f1a57c642dc62Ying Wang * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4205436638acc7c010349a69c3395f1a57c642dc62Ying Wang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4305436638acc7c010349a69c3395f1a57c642dc62Ying Wang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4405436638acc7c010349a69c3395f1a57c642dc62Ying Wang * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4505436638acc7c010349a69c3395f1a57c642dc62Ying Wang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4705436638acc7c010349a69c3395f1a57c642dc62Ying Wang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4805436638acc7c010349a69c3395f1a57c642dc62Ying Wang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4905436638acc7c010349a69c3395f1a57c642dc62Ying Wang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5005436638acc7c010349a69c3395f1a57c642dc62Ying Wang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5105436638acc7c010349a69c3395f1a57c642dc62Ying Wang * SUCH DAMAGE.
5205436638acc7c010349a69c3395f1a57c642dc62Ying Wang *
5305436638acc7c010349a69c3395f1a57c642dc62Ying Wang * The licence and distribution terms for any publically available version or
5405436638acc7c010349a69c3395f1a57c642dc62Ying Wang * derivative of this code cannot be changed.  i.e. this code cannot simply be
5505436638acc7c010349a69c3395f1a57c642dc62Ying Wang * copied and put under another distribution licence
5605436638acc7c010349a69c3395f1a57c642dc62Ying Wang * [including the GNU Public Licence.]
5705436638acc7c010349a69c3395f1a57c642dc62Ying Wang */
5805436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5905436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <stdio.h>
6005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include "cryptlib.h"
6105436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <openssl/rand.h>
6205436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifndef OPENSSL_NO_RSA
6305436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <openssl/rsa.h>
6405436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif
6505436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <openssl/evp.h>
6605436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <openssl/objects.h>
6705436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <openssl/x509.h>
6805436638acc7c010349a69c3395f1a57c642dc62Ying Wang
6905436638acc7c010349a69c3395f1a57c642dc62Ying Wangint EVP_PKEY_encrypt(unsigned char *ek, const unsigned char *key, int key_len,
7005436638acc7c010349a69c3395f1a57c642dc62Ying Wang	     EVP_PKEY *pubk)
7105436638acc7c010349a69c3395f1a57c642dc62Ying Wang	{
7205436638acc7c010349a69c3395f1a57c642dc62Ying Wang	int ret=0;
7305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
7405436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifndef OPENSSL_NO_RSA
7505436638acc7c010349a69c3395f1a57c642dc62Ying Wang	if (pubk->type != EVP_PKEY_RSA)
7605436638acc7c010349a69c3395f1a57c642dc62Ying Wang		{
7705436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif
7805436638acc7c010349a69c3395f1a57c642dc62Ying Wang		EVPerr(EVP_F_EVP_PKEY_ENCRYPT,EVP_R_PUBLIC_KEY_NOT_RSA);
7905436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifndef OPENSSL_NO_RSA
8005436638acc7c010349a69c3395f1a57c642dc62Ying Wang		goto err;
8105436638acc7c010349a69c3395f1a57c642dc62Ying Wang		}
8205436638acc7c010349a69c3395f1a57c642dc62Ying Wang	ret=RSA_public_encrypt(key_len,key,ek,pubk->pkey.rsa,RSA_PKCS1_PADDING);
8305436638acc7c010349a69c3395f1a57c642dc62Ying Wangerr:
8405436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif
8505436638acc7c010349a69c3395f1a57c642dc62Ying Wang	return(ret);
8605436638acc7c010349a69c3395f1a57c642dc62Ying Wang	}
8705436638acc7c010349a69c3395f1a57c642dc62Ying Wang