S2OPC OPCUA Toolkit
Loading...
Searching...
No Matches
mbedtls_common.h
Go to the documentation of this file.
1/*
2 * Licensed to Systerel under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work
4 * for additional information regarding copyright ownership.
5 * Systerel licenses this file to you under the Apache
6 * License, Version 2.0 (the "License"); you may not use this
7 * file except in compliance with the License. You may obtain
8 * a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
25#ifndef SOPC_CRYPTO_MBEDTLS_COMMON_H_
26#define SOPC_CRYPTO_MBEDTLS_COMMON_H_
27
28#include "mbedtls/version.h"
29
30#if MBEDTLS_VERSION_MAJOR == 2
31#if MBEDTLS_VERSION_MINOR >= 28
32#define MBEDTLS_CAN_RESOLVE_HOSTNAME 1
33#else
34#define MBEDTLS_CAN_RESOLVE_HOSTNAME 0
35/* X509 SAN are not implemented before 2.28 but these values are only used to write generic ASN.1 buffer */
36#define MBEDTLS_X509_SAN_DNS_NAME 2
37#define MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER 6
38#endif // MBEDTLS_VERSION_MINOR >= 28
39
40/* MBEDTLS V2 */
41#define MBEDTLS_RSA_RSAES_OAEP_ENCRYPT(ctx, f_rng, p_rng, label, label_len, ilen, input, output) \
42 mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, MBEDTLS_RSA_PUBLIC, label, label_len, ilen, input, output)
43#define MBEDTLS_RSA_RSAES_OAEP_DECRYPT(ctx, f_rng, p_rng, label, label_len, olen, input, output, output_max_len) \
44 mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, label, label_len, olen, input, output, \
45 output_max_len)
46#define MBEDTLS_RSA_RSASSA_PKCS1_V15_SIGN(ctx, f_rng, p_rng, md_alg, hashlen, hash, sig) \
47 mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg, hashlen, hash, sig)
48#define MBEDTLS_RSA_RSASSA_PKCS1_V15_VERIFY(ctx, md_alg, hashlen, hash, sig) \
49 mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, md_alg, hashlen, hash, sig)
50#define MBEDTLS_RSA_RSASSA_PSS_SIGN(ctx, f_rng, p_rng, md_alg, hashlen, hash, sig) \
51 mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg, hashlen, hash, sig)
52#define MBEDTLS_RSA_RSASSA_PSS_VERIFY(ctx, md_alg, hashlen, hash, sig) \
53 mbedtls_rsa_rsassa_pss_verify(ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, md_alg, hashlen, hash, sig)
54#define MBEDTLS_PK_PARSE_KEY(ctx, key, keylen, pwd, pwdlen, f_rng, p_rng) \
55 mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen)
56#define MBEDTLS_PK_PARSE_KEY_FILE(ctx, path, password, f_rng, p_rng) mbedtls_pk_parse_keyfile(ctx, path, password)
57#define MBEDTLS_RSA_SET_PADDING(prsa, padding, hash_id) mbedtls_rsa_set_padding(prsa, padding, (int) hash_id)
58#define MBEDTLS_X509WRITE_CSR_SET_EXTENSION(ctx, oid, oid_len, val, val_len) \
59 mbedtls_x509write_csr_set_extension(ctx, oid, oid_len, val, val_len)
60#define MBEDTLS_MD5_UPDATE(ctx, pwd, pwdLen) mbedtls_md5_update_ret(ctx, pwd, pwdLen)
61#define MBEDTLS_MD5_FINISH(ctx, pSum) mbedtls_md5_finish_ret(ctx, pSum)
62#define MBEDTLS_MD5_STARTS(ctx) mbedtls_md5_starts_ret(ctx)
63
64#elif MBEDTLS_VERSION_MAJOR == 3
65/* MBEDTLS V3 */
66#define MBEDTLS_CAN_RESOLVE_HOSTNAME 1
67#define MBEDTLS_RSA_RSAES_OAEP_ENCRYPT mbedtls_rsa_rsaes_oaep_encrypt
68#define MBEDTLS_RSA_RSAES_OAEP_DECRYPT mbedtls_rsa_rsaes_oaep_decrypt
69#define MBEDTLS_RSA_RSASSA_PKCS1_V15_SIGN mbedtls_rsa_rsassa_pkcs1_v15_sign
70#define MBEDTLS_RSA_RSASSA_PKCS1_V15_VERIFY mbedtls_rsa_rsassa_pkcs1_v15_verify
71#define MBEDTLS_RSA_RSASSA_PSS_SIGN mbedtls_rsa_rsassa_pss_sign
72#define MBEDTLS_RSA_RSASSA_PSS_VERIFY mbedtls_rsa_rsassa_pss_verify
73#define MBEDTLS_RSA_SET_PADDING(prsa, padding, hash_id) mbedtls_rsa_set_padding(prsa, padding, hash_id)
74#define MBEDTLS_X509WRITE_CSR_SET_EXTENSION(ctx, oid, oid_len, val, val_len) \
75 mbedtls_x509write_csr_set_extension(ctx, oid, oid_len, 0, val, val_len)
76
77#define MBEDTLS_MD5_UPDATE(ctx, pwd, pwdLen) mbedtls_md5_update(ctx, pwd, pwdLen)
78#define MBEDTLS_MD5_FINISH(ctx, pSum) mbedtls_md5_finish(ctx, pSum)
79#define MBEDTLS_MD5_STARTS(ctx) mbedtls_md5_starts(ctx)
80
81#define MBEDTLS_PK_PARSE_KEY(ctx, key, keylen, pwd, pwdlen, f_rng, p_rng) \
82 mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen, f_rng, p_rng)
83#define MBEDTLS_PK_PARSE_KEY_FILE(ctx, path, password, f_rng, p_rng) \
84 mbedtls_pk_parse_keyfile(ctx, path, password, f_rng, p_rng)
85
86// These defines shall be set before including any other MBEDTLS headers
87#ifndef MBEDTLS_ALLOW_PRIVATE_ACCESS
88#define MBEDTLS_ALLOW_PRIVATE_ACCESS
89#endif
90
91#else /* MBEDTLS_VERSION_MAJOR neither 2 nor 3 */
92#error "Unsupported MBEDTLS VERSION (see MBEDTLS_VERSION_MAJOR)"
93#endif
94
95#endif /* SOPC_CRYPTO_MBEDTLS_COMMON_H_ */