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 assert(lasym_key != NULL);
83 ret = EVP_PKEY_size(lasym_key->evp);
85 ret = YACA_ERROR_INTERNAL;
92 ret = yaca_zalloc(max_len, (void**)&loutput);
93 if (ret != YACA_ERROR_NONE)
97 (const unsigned char*)input,
98 (unsigned char*)loutput,
99 EVP_PKEY_get0_RSA(lasym_key->evp),
103 ret = ERROR_HANDLE();
115 ret = YACA_ERROR_NONE;
123 API int yaca_rsa_public_encrypt(yaca_padding_e padding,
124 const yaca_key_h pub_key,
125 const char *plaintext,
126 size_t plaintext_len,
128 size_t *ciphertext_len)
130 if (pub_key == YACA_KEY_NULL || pub_key->type != YACA_KEY_TYPE_RSA_PUB)
131 return YACA_ERROR_INVALID_PARAMETER;
134 case YACA_PADDING_NONE:
135 case YACA_PADDING_PKCS1:
136 case YACA_PADDING_PKCS1_OAEP:
137 case YACA_PADDING_PKCS1_SSLV23:
140 return YACA_ERROR_INVALID_PARAMETER;
143 return encrypt_decrypt(padding,
152 API int yaca_rsa_private_decrypt(yaca_padding_e padding,
153 const yaca_key_h prv_key,
154 const char *ciphertext,
155 size_t ciphertext_len,
157 size_t *plaintext_len)
159 if (prv_key == YACA_KEY_NULL || prv_key->type != YACA_KEY_TYPE_RSA_PRIV)
160 return YACA_ERROR_INVALID_PARAMETER;
163 case YACA_PADDING_NONE:
164 case YACA_PADDING_PKCS1:
165 case YACA_PADDING_PKCS1_OAEP:
166 case YACA_PADDING_PKCS1_SSLV23:
169 return YACA_ERROR_INVALID_PARAMETER;
172 return encrypt_decrypt(padding,
178 RSA_private_decrypt);
181 API int yaca_rsa_private_encrypt(yaca_padding_e padding,
182 const yaca_key_h prv_key,
183 const char *plaintext,
184 size_t plaintext_len,
186 size_t *ciphertext_len)
188 if (prv_key == YACA_KEY_NULL || prv_key->type != YACA_KEY_TYPE_RSA_PRIV)
189 return YACA_ERROR_INVALID_PARAMETER;
192 case YACA_PADDING_NONE:
193 case YACA_PADDING_PKCS1:
196 return YACA_ERROR_INVALID_PARAMETER;
199 return encrypt_decrypt(padding,
205 RSA_private_encrypt);
208 API int yaca_rsa_public_decrypt(yaca_padding_e padding,
209 const yaca_key_h pub_key,
210 const char *ciphertext,
211 size_t ciphertext_len,
213 size_t *plaintext_len)
215 if (pub_key == YACA_KEY_NULL || pub_key->type != YACA_KEY_TYPE_RSA_PUB)
216 return YACA_ERROR_INVALID_PARAMETER;
219 case YACA_PADDING_NONE:
220 case YACA_PADDING_PKCS1:
223 return YACA_ERROR_INVALID_PARAMETER;
226 return encrypt_decrypt(padding,