| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/base.h> |
| 5 |
#include <openssl/bytestring.h> |
| 6 |
|
| 7 |
#if defined(__cplusplus) |
| 8 |
extern "C" { |
| 9 |
#endif |
| 10 |
|
| 11 |
#define PSS_DEFAULT_SALT_LEN 20 |
| 12 |
#define PSS_TRAILER_FIELD_VALUE 1 |
| 13 |
|
| 14 |
// Section 2.1. https://tools.ietf.org/html/rfc4055#section-2.1 |
| 15 |
typedef struct rsa_pss_supported_algor_st { |
| 16 |
int nid; |
| 17 |
uint8_t oid[9]; |
| 18 |
uint8_t oid_len; |
| 19 |
} RSA_PSS_SUPPORTED_ALGOR; |
| 20 |
|
| 21 |
typedef struct rsa_algor_identifier_st { |
| 22 |
int nid; |
| 23 |
} RSA_ALGOR_IDENTIFIER; |
| 24 |
|
| 25 |
typedef struct rsa_mga_identifier_st { |
| 26 |
// Mask generation algorithm identifier. |
| 27 |
RSA_ALGOR_IDENTIFIER *mask_gen; |
| 28 |
// Associated hash algorithm identifier. |
| 29 |
RSA_ALGOR_IDENTIFIER *one_way_hash; |
| 30 |
} RSA_MGA_IDENTIFIER; |
| 31 |
|
| 32 |
typedef struct rsa_integer_st { |
| 33 |
int64_t value; |
| 34 |
} RSA_INTEGER; |
| 35 |
|
| 36 |
// RSASSA-PSS-params is defined on rfc4055 section 3.1. |
| 37 |
// See https://tools.ietf.org/html/rfc4055#section-3.1 |
| 38 |
struct rsassa_pss_params_st { |
| 39 |
RSA_ALGOR_IDENTIFIER *hash_algor; |
| 40 |
RSA_MGA_IDENTIFIER *mask_gen_algor; |
| 41 |
RSA_INTEGER *salt_len; |
| 42 |
RSA_INTEGER *trailer_field; |
| 43 |
}; |
| 44 |
|
| 45 |
// RSASSA_PSS_PARAMS related malloc functions. |
| 46 |
RSA_INTEGER *RSA_INTEGER_new(void); |
| 47 |
RSA_ALGOR_IDENTIFIER *RSA_ALGOR_IDENTIFIER_new(void); |
| 48 |
RSA_MGA_IDENTIFIER *RSA_MGA_IDENTIFIER_new(void); |
| 49 |
OPENSSL_EXPORT RSASSA_PSS_PARAMS *RSASSA_PSS_PARAMS_new(void); |
| 50 |
|
| 51 |
// RSASSA_PSS_PARAMS related free functions. |
| 52 |
void RSA_INTEGER_free(RSA_INTEGER *ptr); |
| 53 |
void RSA_ALGOR_IDENTIFIER_free(RSA_ALGOR_IDENTIFIER *ptr); |
| 54 |
void RSA_MGA_IDENTIFIER_free(RSA_MGA_IDENTIFIER *identifier); |
| 55 |
OPENSSL_EXPORT void RSASSA_PSS_PARAMS_free(RSASSA_PSS_PARAMS *params); |
| 56 |
|
| 57 |
// RSASSA_PSS_parse_params return one on success and zero on failure. |
| 58 |
// If success and the |params| are not empty, |*pss| will be allocated |
| 59 |
// and have the bytes parsed. Otherwise, |*pss| will be NULL. |
| 60 |
OPENSSL_EXPORT int RSASSA_PSS_parse_params(CBS *params, |
| 61 |
RSASSA_PSS_PARAMS **pss); |
| 62 |
|
| 63 |
// RSASSA_PSS_PARAMS_create return one on success and zero on failure. |
| 64 |
// When success and the given algorithms are not default (sha1), |
| 65 |
// |*out| will hold the allocated |RSASSA_PSS_PARAMS|. |
| 66 |
OPENSSL_EXPORT int RSASSA_PSS_PARAMS_create(const EVP_MD *sigmd, |
| 67 |
const EVP_MD *mgf1md, int saltlen, |
| 68 |
RSASSA_PSS_PARAMS **out); |
| 69 |
|
| 70 |
// RSASSA_PSS_PARAMS_get return one on success and zero on failure. |
| 71 |
// When success, |*md|, |*mgf1md| and |saltlen| will get assigned. |
| 72 |
OPENSSL_EXPORT int RSASSA_PSS_PARAMS_get(const RSASSA_PSS_PARAMS *pss, |
| 73 |
const EVP_MD **md, |
| 74 |
const EVP_MD **mgf1md, int *saltlen); |
| 75 |
|
| 76 |
int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *out, size_t *out_len, |
| 77 |
size_t max_out, const uint8_t *from, |
| 78 |
size_t from_len, const uint8_t *param, |
| 79 |
size_t param_len, const EVP_MD *md, |
| 80 |
const EVP_MD *mgf1md); |
| 81 |
|
| 82 |
#if defined(__cplusplus) |
| 83 |
} // extern C |
| 84 |
|
| 85 |
extern "C++" { |
| 86 |
|
| 87 |
BSSL_NAMESPACE_BEGIN |
| 88 |
|
| 89 |
BORINGSSL_MAKE_DELETER(RSASSA_PSS_PARAMS, RSASSA_PSS_PARAMS_free) |
| 90 |
|
| 91 |
BSSL_NAMESPACE_END |
| 92 |
|
| 93 |
} // extern C++ |
| 94 |
|
| 95 |
#endif |
| 96 |
|