tor  master
crypto.h
Go to the documentation of this file.
1 /* Copyright (c) 2001, Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
13 #ifndef TOR_CRYPTO_H
14 #define TOR_CRYPTO_H
15 
16 #include "orconfig.h"
17 
18 #include <stdio.h>
19 #include "torint.h"
20 #include "compat.h"
21 #include "util.h"
22 #include "crypto_rsa.h"
23 
25 #define CIPHER_KEY_LEN 16
26 
27 #define CIPHER_IV_LEN 16
28 
29 #define CIPHER256_KEY_LEN 32
30 
31 #define DH_BYTES (1024/8)
32 
35 #define FINGERPRINT_LEN 49
36 
37 typedef struct aes_cnt_cipher crypto_cipher_t;
38 typedef struct crypto_dh_t crypto_dh_t;
39 
40 /* global state */
41 int crypto_init_siphash_key(void);
42 int crypto_early_init(void) ATTR_WUR;
43 int crypto_global_init(int hardwareAccel,
44  const char *accelName,
45  const char *accelPath) ATTR_WUR;
46 #ifdef USE_DMALLOC
47 int crypto_use_tor_alloc_functions(void);
48 #endif
49 
50 void crypto_thread_cleanup(void);
51 int crypto_global_cleanup(void);
52 
53 /* environment setup */
54 void crypto_set_tls_dh_prime(void);
55 crypto_cipher_t *crypto_cipher_new(const char *key);
56 crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
57 crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
59  const uint8_t *iv,
60  int bits);
62 #define crypto_cipher_free(c) \
63  FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
64 
65 /* symmetric crypto */
66 const char *crypto_cipher_get_key(crypto_cipher_t *env);
67 
68 int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
69  const char *from, size_t fromlen);
70 int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
71  const char *from, size_t fromlen);
72 void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
73 
74 int crypto_cipher_encrypt_with_iv(const char *key,
75  char *to, size_t tolen,
76  const char *from, size_t fromlen);
77 int crypto_cipher_decrypt_with_iv(const char *key,
78  char *to, size_t tolen,
79  const char *from, size_t fromlen);
80 
81 /* Key negotiation */
82 #define DH_TYPE_CIRCUIT 1
83 #define DH_TYPE_REND 2
84 #define DH_TYPE_TLS 3
85 crypto_dh_t *crypto_dh_new(int dh_type);
89 int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
90  size_t pubkey_out_len);
91 ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
92  const char *pubkey, size_t pubkey_len,
93  char *secret_out, size_t secret_out_len);
95 #define crypto_dh_free(dh) FREE_AND_NULL(crypto_dh_t, crypto_dh_free_, (dh))
96 
97 int crypto_expand_key_material_TAP(const uint8_t *key_in,
98  size_t key_in_len,
99  uint8_t *key_out, size_t key_out_len);
101  const uint8_t *key_in, size_t key_in_len,
102  const uint8_t *salt_in, size_t salt_in_len,
103  const uint8_t *info_in, size_t info_in_len,
104  uint8_t *key_out, size_t key_out_len);
105 
106 /* Prototypes for private functions only used by tortls.c, crypto.c, and the
107  * unit tests. */
108 struct dh_st;
109 struct dh_st *crypto_dh_get_dh_(crypto_dh_t *dh);
110 
111 void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
112 
113 #endif /* !defined(TOR_CRYPTO_H) */
114 
crypto_cipher_t * crypto_cipher_new_with_iv_and_bits(const uint8_t *key, const uint8_t *iv, int bits)
Definition: crypto.c:321
struct dh_st * crypto_dh_get_dh_(crypto_dh_t *dh)
Definition: crypto.c:312
int crypto_cipher_decrypt_with_iv(const char *key, char *to, size_t tolen, const char *from, size_t fromlen)
Definition: crypto.c:482
int crypto_expand_key_material_TAP(const uint8_t *key_in, size_t key_in_len, uint8_t *key_out, size_t key_out_len)
Definition: crypto.c:960
Headers for crypto_rsa.c.
Header file to define uint32_t and friends.
Definition: d.py:1
int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out, size_t pubkey_out_len)
Definition: crypto.c:811
int crypto_cipher_encrypt_with_iv(const char *key, char *to, size_t tolen, const char *from, size_t fromlen)
Definition: crypto.c:450
crypto_cipher_t * crypto_cipher_new_with_iv(const char *key, const char *iv)
Definition: crypto.c:336
void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in)
Definition: crypto.c:376
int crypto_init_siphash_key(void)
Definition: crypto.c:151
int crypto_global_init(int hardwareAccel, const char *accelName, const char *accelPath) ATTR_WUR
Definition: crypto.c:214
int crypto_expand_key_material_rfc5869_sha256(const uint8_t *key_in, size_t key_in_len, const uint8_t *salt_in, size_t salt_in_len, const uint8_t *info_in, size_t info_in_len, uint8_t *key_out, size_t key_out_len)
Definition: crypto.c:995
DH * dh
Definition: crypto.c:78
void crypto_cipher_free_(crypto_cipher_t *env)
Definition: crypto.c:365
void crypto_thread_cleanup(void)
Definition: crypto.c:302
Headers for util.c.
Definition: crypto.c:77
ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh, const char *pubkey, size_t pubkey_len, char *secret_out, size_t secret_out_len)
Definition: crypto.c:899
int crypto_dh_generate_public(crypto_dh_t *dh)
Definition: crypto.c:765
int crypto_global_cleanup(void)
Definition: crypto.c:1063
int crypto_dh_get_bytes(crypto_dh_t *dh)
Definition: crypto.c:755
void crypto_set_tls_dh_prime(void)
Definition: crypto.c:594
int crypto_cipher_encrypt(crypto_cipher_t *env, char *to, const char *from, size_t fromlen)
Definition: crypto.c:400
int crypto_cipher_decrypt(crypto_cipher_t *env, char *to, const char *from, size_t fromlen)
Definition: crypto.c:420
crypto_dh_t * crypto_dh_dup(const crypto_dh_t *dh)
Definition: crypto.c:742
int crypto_early_init(void) ATTR_WUR
Definition: crypto.c:166
crypto_cipher_t * crypto_cipher_new_with_bits(const char *key, int bits)
Definition: crypto.c:346
void crypto_dh_free_(crypto_dh_t *dh)
Definition: crypto.c:1049
Definition: aes.c:156
crypto_cipher_t * crypto_cipher_new(const char *key)
Definition: crypto.c:357
void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len)
Definition: crypto.c:437
crypto_dh_t * crypto_dh_new(int dh_type)
Definition: crypto.c:677