2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @brief Advanced API for low-level RSA operations
26 #include <openssl/evp.h>
27 #include <openssl/rsa.h>
30 #include <yaca_error.h>
31 #include <yaca_types.h>
32 #include <yaca_crypto.h>
37 int rsa_padding2openssl(yaca_padding_e padding)
40 case YACA_PADDING_NONE:
41 return RSA_NO_PADDING;
42 case YACA_PADDING_X931:
43 return RSA_X931_PADDING;
44 case YACA_PADDING_PKCS1:
45 return RSA_PKCS1_PADDING;
46 case YACA_PADDING_PKCS1_PSS:
47 return RSA_PKCS1_PSS_PADDING;
48 case YACA_PADDING_PKCS1_OAEP:
49 return RSA_PKCS1_OAEP_PADDING;
50 case YACA_PADDING_PKCS1_SSLV23:
51 return RSA_SSLV23_PADDING;
57 typedef int (*encrypt_decrypt_fn)(int, const unsigned char*, unsigned char*, RSA*, int);
59 static int encrypt_decrypt(yaca_padding_e padding,
65 encrypt_decrypt_fn fn)
70 struct yaca_key_evp_s *lasym_key;
73 if ((input == NULL && input_len > 0) || (input != NULL && input_len == 0) ||
74 output == NULL || output_len == NULL)
75 return YACA_ERROR_INVALID_PARAMETER;
77 lpadding = rsa_padding2openssl(padding);
78 assert(lpadding != -1);
80 lasym_key = key_get_evp(key);
81 if (lasym_key == NULL)
82 return YACA_ERROR_INVALID_PARAMETER;
84 ret = EVP_PKEY_size(lasym_key->evp);
86 ret = YACA_ERROR_INTERNAL;
93 ret = yaca_zalloc(max_len, (void**)&loutput);
94 if (ret != YACA_ERROR_NONE)
98 (const unsigned char*)input,
99 (unsigned char*)loutput,
100 lasym_key->evp->pkey.rsa,
104 ret = ERROR_HANDLE();
116 ret = YACA_ERROR_NONE;
124 API int yaca_rsa_public_encrypt(yaca_padding_e padding,
125 const yaca_key_h pub_key,
126 const char *plaintext,
127 size_t plaintext_len,
129 size_t *ciphertext_len)
131 if (pub_key == YACA_KEY_NULL || pub_key->type != YACA_KEY_TYPE_RSA_PUB)
132 return YACA_ERROR_INVALID_PARAMETER;
135 case YACA_PADDING_NONE:
136 case YACA_PADDING_PKCS1:
137 case YACA_PADDING_PKCS1_OAEP:
138 case YACA_PADDING_PKCS1_SSLV23:
141 return YACA_ERROR_INVALID_PARAMETER;
144 return encrypt_decrypt(padding,
153 API int yaca_rsa_private_decrypt(yaca_padding_e padding,
154 const yaca_key_h prv_key,
155 const char *ciphertext,
156 size_t ciphertext_len,
158 size_t *plaintext_len)
160 if (prv_key == YACA_KEY_NULL || prv_key->type != YACA_KEY_TYPE_RSA_PRIV)
161 return YACA_ERROR_INVALID_PARAMETER;
164 case YACA_PADDING_NONE:
165 case YACA_PADDING_PKCS1:
166 case YACA_PADDING_PKCS1_OAEP:
167 case YACA_PADDING_PKCS1_SSLV23:
170 return YACA_ERROR_INVALID_PARAMETER;
173 return encrypt_decrypt(padding,
179 RSA_private_decrypt);
182 API int yaca_rsa_private_encrypt(yaca_padding_e padding,
183 const yaca_key_h prv_key,
184 const char *plaintext,
185 size_t plaintext_len,
187 size_t *ciphertext_len)
189 if (prv_key == YACA_KEY_NULL || prv_key->type != YACA_KEY_TYPE_RSA_PRIV)
190 return YACA_ERROR_INVALID_PARAMETER;
193 case YACA_PADDING_NONE:
194 case YACA_PADDING_PKCS1:
197 return YACA_ERROR_INVALID_PARAMETER;
200 return encrypt_decrypt(padding,
206 RSA_private_encrypt);
209 API int yaca_rsa_public_decrypt(yaca_padding_e padding,
210 const yaca_key_h pub_key,
211 const char *ciphertext,
212 size_t ciphertext_len,
214 size_t *plaintext_len)
216 if (pub_key == YACA_KEY_NULL || pub_key->type != YACA_KEY_TYPE_RSA_PUB)
217 return YACA_ERROR_INVALID_PARAMETER;
220 case YACA_PADDING_NONE:
221 case YACA_PADDING_PKCS1:
224 return YACA_ERROR_INVALID_PARAMETER;
227 return encrypt_decrypt(padding,