Debian Codegraph @

src/crypto/psk.h

raw symbols SCIP
1 /* librist. Copyright © 2020 SipRadius LLC. All right reserved.
2 * Author: Gijs Peskens <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #ifndef RIST_CRYPTO_PSK_H
9 #define RIST_CRYPTO_PSK_H
10
11 #include "config.h"
12 #include "common/attributes.h"
13 #include "librist/librist_config.h"
14 #if HAVE_MBEDTLS
15 #include "mbedtls/aes.h"
16 #elif HAVE_NETTLE
17 #include <nettle/aes.h>
18 /* nettle's unified struct aes_ctx was deprecated for years and removed in
19 * 4.0. All we ever wanted from it was somewhere to hold one of the three
20 * per-size key schedules, so hold it ourselves. */
21 union rist_nettle_aes_ctx {
22 struct aes128_ctx ctx128;
23 struct aes192_ctx ctx192;
24 struct aes256_ctx ctx256;
25 };
26 #else
27 #ifdef LINUX_CRYPTO
28 #include "linux-crypto.h"
29 #endif
30 #include "contrib/aes.h"
31 #endif
32 #include <stdint.h>
33 #include <string.h>
34 #include <stdbool.h>
35
36 #ifndef AES_BLOCK_SIZE
37 #define AES_BLOCK_SIZE 16
38 #endif
39
40 struct rist_key {
41 uint32_t key_size;
42 uint8_t gre_nonce[4];
43 uint8_t iv[AES_BLOCK_SIZE];
44 #if HAVE_MBEDTLS
45 size_t aes_offset;
46 unsigned char strean_block[16];
47 mbedtls_aes_context mbedtls_aes_ctx;
48 #elif HAVE_NETTLE
49 union rist_nettle_aes_ctx nettle_ctx;
50 #elif defined(LINUX_CRYPTO)
51 struct linux_crypto *linux_crypto_ctx;
52 #endif
53 uint32_t aes_key_sched[60];//Do we still need this fallback?
54 uint32_t key_rotation;
55 uint64_t used_times;
56 uint8_t password[128];
57 size_t password_len;
58 bool bad_decryption;
59 int bad_count;
60 bool odd;
61 bool csprng_failed; /* set when nonce generation can't draw randomness; encrypt/decrypt short-circuit */
62 uint64_t last_rekey; /* timestampNTP_u64() of the last nonce-adoption rekey; rate-limits PBKDF2 */
63 };
64
65 RIST_PRIV int _librist_crypto_psk_rist_key_init(struct rist_key *key, uint32_t key_size, uint32_t rotation, const char *password, bool odd);
66 RIST_PRIV int _librist_crypto_psk_rist_key_destroy(struct rist_key *key);
67 RIST_PRIV int _librist_crypto_psk_rist_key_clone(struct rist_key *key_in, struct rist_key *key_out);
68 RIST_PRIV void _librist_crypto_psk_decrypt(struct rist_key *key, uint8_t nonce[4], uint32_t seq_nbe, uint8_t gre_version, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len);
69 RIST_PRIV void _librist_crypto_psk_encrypt(struct rist_key *key, uint32_t seq_nbe, uint8_t gre_version, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len);
70 RIST_PRIV void _librist_crypto_psk_encrypt_continue(struct rist_key *key, const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len);
71 RIST_PRIV int _librist_crypto_psk_set_passphrase(struct rist_key *key, const uint8_t *passsphrase, size_t passphrase_len);
72 RIST_PRIV void _librist_crypto_psk_get_passphrase(struct rist_key *key, const uint8_t **passphrase, size_t *passphrase_len);
73 RIST_PRIV void _librist_crypto_aes_ctr(const uint8_t key[], int key_size, uint8_t iv[], const uint8_t inbuf[], uint8_t outbuf[], size_t payload_len);
74 RIST_PRIV void _librist_crypto_psk_preannounce_nonce(struct rist_key *key, const uint8_t nonce[4], uint32_t key_size_bits);
75 #endif