2007-12-12 21:09:01 +00:00
|
|
|
/* Copyright (c) 2001, Matej Pfajfar.
|
2006-02-09 05:46:49 +00:00
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 21:09:01 +00:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 16:13:17 -04:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2002-07-24 14:02:39 +00:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2004-05-10 03:53:24 +00:00
|
|
|
/**
|
2004-05-10 07:54:13 +00:00
|
|
|
* \file crypto.h
|
2004-05-10 03:53:24 +00:00
|
|
|
*
|
2005-06-11 05:31:17 +00:00
|
|
|
* \brief Headers for crypto.c
|
2004-05-10 03:53:24 +00:00
|
|
|
**/
|
|
|
|
|
2012-10-12 12:13:10 -04:00
|
|
|
#ifndef TOR_CRYPTO_H
|
|
|
|
#define TOR_CRYPTO_H
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2016-07-28 10:47:46 -04:00
|
|
|
#include "orconfig.h"
|
|
|
|
|
2002-08-22 07:30:03 +00:00
|
|
|
#include <stdio.h>
|
2006-10-01 21:59:05 +00:00
|
|
|
#include "torint.h"
|
2015-11-25 10:30:58 -05:00
|
|
|
#include "compat.h"
|
2017-11-17 11:55:52 -05:00
|
|
|
#include "util.h"
|
2018-01-25 20:16:50 +01:00
|
|
|
#include "crypto_rsa.h"
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2016-11-07 13:15:46 -05:00
|
|
|
/** Length of our symmetric cipher's keys of 128-bit. */
|
2004-04-03 02:40:30 +00:00
|
|
|
#define CIPHER_KEY_LEN 16
|
2016-11-07 13:15:46 -05:00
|
|
|
/** Length of our symmetric cipher's IV of 128-bit. */
|
2007-09-19 15:53:41 +00:00
|
|
|
#define CIPHER_IV_LEN 16
|
2016-11-07 13:15:46 -05:00
|
|
|
/** Length of our symmetric cipher's keys of 256-bit. */
|
|
|
|
#define CIPHER256_KEY_LEN 32
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Length of our DH keys. */
|
2004-05-01 20:46:28 +00:00
|
|
|
#define DH_BYTES (1024/8)
|
2003-12-16 05:29:04 +00:00
|
|
|
|
2004-05-10 03:53:24 +00:00
|
|
|
/** Length of encoded public key fingerprints, including space; but not
|
2004-05-01 21:41:23 +00:00
|
|
|
* including terminating NUL. */
|
2004-05-01 20:46:28 +00:00
|
|
|
#define FINGERPRINT_LEN 49
|
|
|
|
|
2016-09-16 10:18:02 -04:00
|
|
|
typedef struct aes_cnt_cipher crypto_cipher_t;
|
2012-01-18 15:53:30 -05:00
|
|
|
typedef struct crypto_dh_t crypto_dh_t;
|
2002-07-24 14:02:39 +00:00
|
|
|
|
|
|
|
/* global state */
|
2015-11-25 10:36:34 -05:00
|
|
|
int crypto_early_init(void) ATTR_WUR;
|
2009-05-23 16:42:44 -07:00
|
|
|
int crypto_global_init(int hardwareAccel,
|
|
|
|
const char *accelName,
|
2015-11-25 10:36:34 -05:00
|
|
|
const char *accelPath) ATTR_WUR;
|
2017-03-25 11:27:50 +01:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
int crypto_use_tor_alloc_functions(void);
|
|
|
|
#endif
|
|
|
|
|
2005-10-25 19:01:48 +00:00
|
|
|
void crypto_thread_cleanup(void);
|
2004-10-27 18:16:37 +00:00
|
|
|
int crypto_global_cleanup(void);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
|
|
|
/* environment setup */
|
2015-03-14 12:40:55 -04:00
|
|
|
void crypto_set_tls_dh_prime(void);
|
2012-03-20 15:35:43 -04:00
|
|
|
crypto_cipher_t *crypto_cipher_new(const char *key);
|
2016-09-16 11:21:33 -04:00
|
|
|
crypto_cipher_t *crypto_cipher_new_with_bits(const char *key, int bits);
|
2012-03-20 15:35:43 -04:00
|
|
|
crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
|
2016-09-16 11:21:33 -04:00
|
|
|
crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
|
|
|
|
const uint8_t *iv,
|
|
|
|
int bits);
|
2017-11-17 11:55:52 -05:00
|
|
|
void crypto_cipher_free_(crypto_cipher_t *env);
|
2017-12-07 10:52:55 -05:00
|
|
|
#define crypto_cipher_free(c) \
|
|
|
|
FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
|
2002-07-24 14:02:39 +00:00
|
|
|
|
|
|
|
/* symmetric crypto */
|
2012-01-18 15:53:30 -05:00
|
|
|
const char *crypto_cipher_get_key(crypto_cipher_t *env);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2012-01-18 15:53:30 -05:00
|
|
|
int crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen);
|
2012-01-18 15:53:30 -05:00
|
|
|
int crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
|
2005-05-07 05:55:06 +00:00
|
|
|
const char *from, size_t fromlen);
|
2016-02-06 12:14:39 -05:00
|
|
|
void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *d, size_t len);
|
2002-07-24 14:02:39 +00:00
|
|
|
|
2012-03-20 15:35:43 -04:00
|
|
|
int crypto_cipher_encrypt_with_iv(const char *key,
|
2007-09-19 15:53:41 +00:00
|
|
|
char *to, size_t tolen,
|
|
|
|
const char *from, size_t fromlen);
|
2012-03-20 15:35:43 -04:00
|
|
|
int crypto_cipher_decrypt_with_iv(const char *key,
|
2007-09-19 15:53:41 +00:00
|
|
|
char *to, size_t tolen,
|
|
|
|
const char *from, size_t fromlen);
|
|
|
|
|
2004-11-02 02:28:51 +00:00
|
|
|
/* Key negotiation */
|
2011-01-24 16:03:14 -05:00
|
|
|
#define DH_TYPE_CIRCUIT 1
|
|
|
|
#define DH_TYPE_REND 2
|
|
|
|
#define DH_TYPE_TLS 3
|
2012-01-18 15:53:30 -05:00
|
|
|
crypto_dh_t *crypto_dh_new(int dh_type);
|
2012-11-28 15:38:37 -05:00
|
|
|
crypto_dh_t *crypto_dh_dup(const crypto_dh_t *dh);
|
2012-01-18 15:53:30 -05:00
|
|
|
int crypto_dh_get_bytes(crypto_dh_t *dh);
|
|
|
|
int crypto_dh_generate_public(crypto_dh_t *dh);
|
|
|
|
int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
|
2004-11-02 02:28:51 +00:00
|
|
|
size_t pubkey_out_len);
|
2012-01-18 15:53:30 -05:00
|
|
|
ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
|
2004-11-02 02:28:51 +00:00
|
|
|
const char *pubkey, size_t pubkey_len,
|
|
|
|
char *secret_out, size_t secret_out_len);
|
2017-11-17 11:55:52 -05:00
|
|
|
void crypto_dh_free_(crypto_dh_t *dh);
|
2017-12-07 10:44:04 -05:00
|
|
|
#define crypto_dh_free(dh) FREE_AND_NULL(crypto_dh_t, crypto_dh_free_, (dh))
|
2012-12-03 12:20:05 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
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);
|
2004-11-02 02:28:51 +00:00
|
|
|
|
2010-04-09 18:45:08 -04:00
|
|
|
/* Prototypes for private functions only used by tortls.c, crypto.c, and the
|
|
|
|
* unit tests. */
|
2007-06-13 22:39:10 +00:00
|
|
|
struct dh_st;
|
2012-10-12 12:22:13 -04:00
|
|
|
struct dh_st *crypto_dh_get_dh_(crypto_dh_t *dh);
|
2013-06-06 17:58:28 -04:00
|
|
|
|
|
|
|
void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
|
2007-04-30 22:42:50 +00:00
|
|
|
|
2017-09-15 16:24:44 -04:00
|
|
|
#endif /* !defined(TOR_CRYPTO_H) */
|
2015-10-02 14:33:54 +02:00
|
|
|
|