2 * Copyright (c) 2019 Andreas Schneider <asn@samba.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef _GNUTLS_HELPERS_H
19 #define _GNUTLS_HELPERS_H
21 #include <gnutls/gnutls.h>
23 #include "libcli/util/ntstatus.h"
24 #include "libcli/util/werror.h"
26 /* Those macros are only available in GnuTLS >= 3.6.4 */
27 #ifndef GNUTLS_FIPS140_SET_LAX_MODE
28 #define GNUTLS_FIPS140_SET_LAX_MODE()
31 #ifndef GNUTLS_FIPS140_SET_STRICT_MODE
32 #define GNUTLS_FIPS140_SET_STRICT_MODE()
37 * @brief Convert a gnutls error code to a corresponding NTSTATUS.
39 * @param[in] gnutls_rc The GnuTLS return code.
41 * @param[in] blocked_status The NTSTATUS return code which should be returned
42 * in case the e.g. the cipher might be blocked due
45 * @return A corresponding NTSTATUS code.
47 NTSTATUS
gnutls_error_to_ntstatus(int gnutls_rc
, NTSTATUS blocked_status
);
49 NTSTATUS
_gnutls_error_to_ntstatus(int gnutls_rc
,
50 NTSTATUS blocked_status
,
52 const char *location
);
53 #define gnutls_error_to_ntstatus(gnutls_rc, blocked_status) \
54 _gnutls_error_to_ntstatus(gnutls_rc, \
62 * @brief Convert a gnutls error code to a corresponding WERROR.
64 * @param[in] gnutls_rc The GnuTLS return code.
66 * @param[in] blocked_werr The WERROR code which should be returned if e.g
67 * the cipher we want to used it not allowed to be
68 * used because of FIPS mode.
70 * @return A corresponding WERROR code.
72 WERROR
gnutls_error_to_werror(int gnutls_rc
, WERROR blocked_werr
);
74 WERROR
_gnutls_error_to_werror(int gnutls_rc
,
77 const char *location
);
78 #define gnutls_error_to_werror(gnutls_rc, blocked_werr) \
79 _gnutls_error_to_werror(gnutls_rc, \
85 enum samba_gnutls_direction
{ SAMBA_GNUTLS_ENCRYPT
, SAMBA_GNUTLS_DECRYPT
};
88 * @brief Encrypt or decrypt a data blob using RC4 with a key and salt.
90 * One of the key input should be a session key and the other a confounder
91 * (aka salt). Which one depends on the implementation details of the
94 * @param[in] key_input1 Either a session_key or a confounder.
96 * @param[in] key_input2 Either a session_key or a confounder.
98 * @param[in] data The data blob to either encrypt or decrypt. The data
99 * will be encrypted or decrypted in place.
101 * @param[in] encrypt The encryption direction.
103 * @return A gnutls error code.
105 int samba_gnutls_arcfour_confounded_md5(const DATA_BLOB
*key_input1
,
106 const DATA_BLOB
*key_input2
,
108 enum samba_gnutls_direction encrypt
);
111 * @brief Encrypted a secret plaintext using AEAD_AES_256_CBC_HMAC_SHA512 and
114 * This encrypts a secret plaintext using AEAD_AES_256_CBC_HMAC_SHA512 with a
115 * key (can be the session key or PBKDF2 password). This is used in SAMR and
118 * @param mem_ctx The memory context to allocate the cipher text pointer.
120 * @param plaintext The secret to encrypt
122 * @param cek The content encryption key to encrypt the secret.
124 * @param key_salt The salt used to calculate the encryption key.
126 * @param key_salt The salt used to calculate the mac key.
128 * @param iv The initialization vector used for the encryption.
130 * @param pciphertext A pointer to store the cipher text.
132 * @param pauth_tag[64] An array to store the auth tag.
134 * @return NT_STATUS_OK on success, an nt status error code otherwise.
137 samba_gnutls_aead_aes_256_cbc_hmac_sha512_encrypt(TALLOC_CTX
*mem_ctx
,
138 const DATA_BLOB
*plaintext
,
139 const DATA_BLOB
*cek
,
140 const DATA_BLOB
*key_salt
,
141 const DATA_BLOB
*mac_salt
,
143 DATA_BLOB
*pciphertext
,
144 uint8_t pauth_tag
[64]);
147 * @brief Decypt cipher text using AEAD_AES_256_CBC_HMAC_SHA512 and the session
150 * This decrypts the cipher text using AEAD_AES_256_CBC_HMAC_SHA512 with the
151 * given content decryption key key. The plaintext will be zeroed as soon as the
152 * data blob is freed.
154 * @param mem_ctx The memory context to allocate the plaintext on.
156 * @param ciphertext The cipher text to decrypt.
158 * @param cdk The content decryption key.
160 * @param key_salt The salt used to calculate the encryption key.
162 * @param key_salt The salt used to calculate the mac key.
164 * @param iv The initialization vector used for the encryption.
166 * @param auth_tag[64] The authentication blob to be verified.
168 * @param pplaintext A pointer to a DATA_BLOB to store the plaintext.
170 * @return NT_STATUS_OK on success, an nt status error code otherwise.
173 samba_gnutls_aead_aes_256_cbc_hmac_sha512_decrypt(TALLOC_CTX
*mem_ctx
,
174 const DATA_BLOB
*ciphertext
,
175 const DATA_BLOB
*cdk
,
176 const DATA_BLOB
*key_salt
,
177 const DATA_BLOB
*mac_salt
,
179 const uint8_t auth_tag
[64],
180 DATA_BLOB
*pplaintext
);
183 * @brief Check if weak crypto is allowed.
185 * @return true if weak crypo is allowed, false otherwise.
187 bool samba_gnutls_weak_crypto_allowed(void);
189 #endif /* _GNUTLS_HELPERS_H */