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