summaryrefslogtreecommitdiff
path: root/toxcore/crypto_core.api.h
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-11-06 15:05:52 +0000
committeriphydf <iphydf@users.noreply.github.com>2016-11-12 11:55:40 +0000
commit8bbde23f48eca2f51d56437fdce29c9443e21cb1 (patch)
treefaf5f63010742c65f27f759ec4d801f54154fdd4 /toxcore/crypto_core.api.h
parenta205b788ad118e91e013bac87c1eca1b8eadcbbe (diff)
Use apidsl for the crypto_core API.
This allows us to use apidsl features like namespaces to enforce a naming standard.
Diffstat (limited to 'toxcore/crypto_core.api.h')
-rw-r--r--toxcore/crypto_core.api.h150
1 files changed, 150 insertions, 0 deletions
diff --git a/toxcore/crypto_core.api.h b/toxcore/crypto_core.api.h
new file mode 100644
index 00000000..0c72471c
--- /dev/null
+++ b/toxcore/crypto_core.api.h
@@ -0,0 +1,150 @@
1%{
2/* crypto_core.h
3 *
4 * Functions for the core crypto.
5 *
6 * Copyright (C) 2013 Tox project All Rights Reserved.
7 *
8 * This file is part of Tox.
9 *
10 * Tox is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24#ifndef CORE_CRYPTO_H
25#define CORE_CRYPTO_H
26
27#include "network.h"
28
29#ifndef VANILLA_NACL
30/* We use libsodium by default. */
31#include <sodium.h>
32#else
33#include <crypto_box.h>
34#include <crypto_hash_sha256.h>
35#include <crypto_hash_sha512.h>
36#include <crypto_scalarmult_curve25519.h>
37#include <crypto_verify_16.h>
38#include <crypto_verify_32.h>
39#include <randombytes.h>
40#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
41/* I know */
42#define sodium_memcmp(a, b, c) memcmp(a, b, c)
43#define sodium_memzero(a, c) memset(a, 0, c)
44#endif
45
46#define crypto_box_KEYBYTES (crypto_box_BEFORENMBYTES)
47%}
48
49/**
50 * compare 2 public keys of length crypto_box_PUBLICKEYBYTES, not vulnerable to timing attacks.
51 * returns 0 if both mem locations of length are equal,
52 * return -1 if they are not.
53 */
54static int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2);
55
56/**
57 * Return a random 32 bit integer.
58 */
59static uint32_t random_int();
60
61/**
62 * Return a random 64 bit integer.
63 */
64static uint64_t random_64b();
65
66/**
67 * Check if a Tox public key crypto_box_PUBLICKEYBYTES is valid or not.
68 * This should only be used for input validation.
69 *
70 * return 0 if it isn't.
71 * return 1 if it is.
72 */
73static int32_t public_key_valid(const uint8_t *public_key);
74
75/**
76 * Encrypts plain of length length to encrypted of length + 16 using the
77 * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce.
78 *
79 * return -1 if there was a problem.
80 * return length of encrypted data if everything was fine.
81 */
82static int32_t encrypt_data(
83 const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
84 const uint8_t *plain, uint32_t length, uint8_t *encrypted);
85
86
87/**
88 * Decrypts encrypted of length length to plain of length length - 16 using the
89 * public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce.
90 *
91 * return -1 if there was a problem (decryption failed).
92 * return length of plain data if everything was fine.
93 */
94static int32_t decrypt_data(
95 const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
96 const uint8_t *encrypted, uint32_t length, uint8_t *plain);
97
98/**
99 * Fast encrypt/decrypt operations. Use if this is not a one-time communication.
100 * encrypt_precompute does the shared-key generation once so it does not have
101 * to be preformed on every encrypt/decrypt.
102 */
103static int32_t encrypt_precompute(
104 const uint8_t *public_key, const uint8_t *secret_key, uint8_t *enc_key);
105
106/**
107 * Encrypts plain of length length to encrypted of length + 16 using a
108 * secret key crypto_box_KEYBYTES big and a 24 byte nonce.
109 *
110 * return -1 if there was a problem.
111 * return length of encrypted data if everything was fine.
112 */
113static int32_t encrypt_data_symmetric(
114 const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *plain,
115 uint32_t length, uint8_t *encrypted);
116
117/**
118 * Decrypts encrypted of length length to plain of length length - 16 using a
119 * secret key crypto_box_KEYBYTES big and a 24 byte nonce.
120 *
121 * return -1 if there was a problem (decryption failed).
122 * return length of plain data if everything was fine.
123 */
124static int32_t decrypt_data_symmetric(
125 const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *encrypted,
126 uint32_t length, uint8_t *plain);
127
128/**
129 * Increment the given nonce by 1.
130 */
131static void increment_nonce(uint8_t *nonce);
132
133/**
134 * Increment the given nonce by num.
135 */
136static void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num);
137
138/**
139 * Fill the given nonce with random bytes.
140 */
141static void random_nonce(uint8_t *nonce);
142
143/**
144 * Fill a key crypto_box_KEYBYTES big with random bytes.
145 */
146static void new_symmetric_key(uint8_t *key);
147
148%{
149#endif
150%}