Tor 0.4.9.0-alpha-dev
crypto_rsa.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-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file crypto_rsa.h
9 *
10 * \brief Headers for crypto_rsa.c
11 **/
12
13#ifndef TOR_CRYPTO_RSA_H
14#define TOR_CRYPTO_RSA_H
15
16#include "orconfig.h"
17
19#include "lib/cc/torint.h"
21#include "lib/log/log.h"
22
23/** Length of our public keys. */
24#define PK_BYTES (1024/8)
25
26/** Constant used to indicate OAEP padding for public-key encryption */
27#define PK_PKCS1_OAEP_PADDING 60002
28
29/** Number of bytes added for PKCS1-OAEP padding. */
30#define PKCS1_OAEP_PADDING_OVERHEAD 42
31
32/** Length of encoded public key fingerprints, including space; but not
33 * including terminating NUL. */
34#define FINGERPRINT_LEN 49
35
36/** Value of 'e' to use in our public keys */
37#define TOR_RSA_EXPONENT 65537
38
39/** A public key, or a public/private key-pair. */
40typedef struct crypto_pk_t crypto_pk_t;
41
42/* RSA environment setup */
45#define crypto_pk_free(pk) FREE_AND_NULL(crypto_pk_t, crypto_pk_free_, (pk))
46int crypto_get_rsa_padding_overhead(int padding);
47int crypto_get_rsa_padding(int padding);
48
49/* public key crypto */
51#define crypto_pk_generate_key(env) \
52 crypto_pk_generate_key_with_bits((env), (PK_BYTES*8))
53
55 const char *keyfile);
57 char **dest, size_t *len);
59 char **dest, size_t *len);
61 const char *src, size_t len);
63 const char *s, ssize_t len);
65 const char *src, ssize_t len);
67 const char *fname);
68
70int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b);
71int crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b);
72size_t crypto_pk_keysize(const crypto_pk_t *env);
79 size_t tolen,
80 const char *from, size_t fromlen,
81 int padding, int force);
83 size_t tolen,
84 const char *from, size_t fromlen,
85 int padding, int warnOnFailure);
86int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
87 const char *from, size_t fromlen, int padding);
88int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen,
89 const char *from, size_t fromlen,
90 int padding, int warnOnFailure);
92 char *to, size_t tolen,
93 const char *from, size_t fromlen));
94int crypto_pk_private_sign(const crypto_pk_t *env, char *to, size_t tolen,
95 const char *from, size_t fromlen);
96int crypto_pk_asn1_encode(const crypto_pk_t *pk, char *dest, size_t dest_len);
97crypto_pk_t *crypto_pk_asn1_decode(const char *str, size_t len);
99 char *dest, size_t dest_len);
100crypto_pk_t *crypto_pk_asn1_decode_private(const char *str, size_t len,
101 int max_bits);
102int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out,int add_space);
103int crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out);
104void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in);
105
107 const char *data, size_t datalen, const char *sig, size_t siglen));
108int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
109 const char *from, size_t fromlen);
110int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out);
112 common_digests_t *digests_out);
113int crypto_pk_base64_encode_private(const crypto_pk_t *pk, char **priv_out);
114crypto_pk_t *crypto_pk_base64_decode_private(const char *str, size_t len);
115
116#ifdef ENABLE_OPENSSL
117/* Prototypes for private functions only used by tortls.c, crypto.c, and the
118 * unit tests. */
119struct rsa_st;
120struct evp_pkey_st;
121struct rsa_st *crypto_pk_get_openssl_rsa_(crypto_pk_t *env);
122crypto_pk_t *crypto_new_pk_from_openssl_rsa_(struct rsa_st *rsa);
123MOCK_DECL(struct evp_pkey_st *, crypto_pk_get_openssl_evp_pkey_,(
124 crypto_pk_t *env,int private));
125#endif /* defined(ENABLE_OPENSSL) */
126
127#ifdef ENABLE_NSS
128struct SECKEYPublicKeyStr;
129struct SECKEYPrivateKeyStr;
130crypto_pk_t *crypto_pk_new_from_nss_pubkey(struct SECKEYPublicKeyStr *pub);
131const struct SECKEYPublicKeyStr *crypto_pk_get_nss_pubkey(
132 const crypto_pk_t *key);
133const struct SECKEYPrivateKeyStr *crypto_pk_get_nss_privkey(
134 const crypto_pk_t *key);
135#endif /* defined(ENABLE_NSS) */
136
137void crypto_pk_assign_public(crypto_pk_t *dest, const crypto_pk_t *src);
138void crypto_pk_assign_private(crypto_pk_t *dest, const crypto_pk_t *src);
139
140#ifdef TOR_UNIT_TESTS
141#ifdef ENABLE_NSS
142struct SECItemStr;
143STATIC int secitem_uint_cmp(const struct SECItemStr *a,
144 const struct SECItemStr *b);
145#endif
146#endif /* defined(TOR_UNIT_TESTS) */
147
148#endif /* !defined(TOR_CRYPTO_RSA_H) */
Headers for crypto_digest.c.
int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out, int add_space)
Definition: crypto_rsa.c:229
void crypto_pk_assign_private(crypto_pk_t *dest, const crypto_pk_t *src)
int crypto_pk_obsolete_public_hybrid_encrypt(crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen, int padding, int force)
Definition: crypto_rsa.c:94
crypto_pk_t * crypto_pk_new(void)
int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b)
int crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
size_t crypto_pk_keysize(const crypto_pk_t *env)
int crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b)
Definition: crypto_rsa.c:71
int crypto_pk_obsolete_private_hybrid_decrypt(crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen, int padding, int warnOnFailure)
Definition: crypto_rsa.c:163
int crypto_pk_write_public_key_to_string(crypto_pk_t *env, char **dest, size_t *len)
Definition: crypto_rsa.c:466
int crypto_pk_read_private_key_from_string(crypto_pk_t *env, const char *s, ssize_t len)
Definition: crypto_rsa.c:551
int crypto_pk_private_sign(const crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen)
int crypto_pk_write_private_key_to_filename(crypto_pk_t *env, const char *fname)
Definition: crypto_rsa.c:610
int crypto_pk_is_valid_private_key(const crypto_pk_t *env)
int crypto_pk_get_common_digests(crypto_pk_t *pk, common_digests_t *digests_out)
Definition: crypto_rsa.c:381
int crypto_pk_private_decrypt(crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen, int padding, int warnOnFailure)
int crypto_pk_read_private_key1024_from_string(crypto_pk_t *env, const char *src, ssize_t len)
Definition: crypto_rsa.c:564
int crypto_pk_base64_encode_private(const crypto_pk_t *pk, char **priv_out)
Definition: crypto_rsa.c:633
int crypto_pk_asn1_encode(const crypto_pk_t *pk, char *dest, size_t dest_len)
crypto_pk_t * crypto_pk_asn1_decode(const char *str, size_t len)
int crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out)
Definition: crypto_rsa.c:254
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
crypto_pk_t * crypto_pk_copy_full(crypto_pk_t *orig)
int crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen, int padding)
void crypto_pk_assign_public(crypto_pk_t *dest, const crypto_pk_t *src)
int crypto_get_rsa_padding_overhead(int padding)
Definition: crypto_rsa.c:41
crypto_pk_t * crypto_pk_asn1_decode_private(const char *str, size_t len, int max_bits)
int crypto_pk_num_bits(crypto_pk_t *env)
void crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in)
Definition: crypto_rsa.c:270
crypto_pk_t * crypto_pk_dup_key(crypto_pk_t *orig)
int crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen)
Definition: crypto_rsa.c:339
int crypto_pk_key_is_private(const crypto_pk_t *key)
int crypto_pk_asn1_encode_private(const crypto_pk_t *pk, char *dest, size_t dest_len)
int crypto_pk_public_checksig_digest(crypto_pk_t *env, const char *data, size_t datalen, const char *sig, size_t siglen)
Definition: crypto_rsa.c:295
int crypto_pk_read_private_key_from_filename(crypto_pk_t *env, const char *keyfile)
Definition: crypto_rsa.c:578
int crypto_pk_public_checksig(const crypto_pk_t *env, char *to, size_t tolen, const char *from, size_t fromlen)
int crypto_pk_public_exponent_ok(const crypto_pk_t *env)
int crypto_pk_read_public_key_from_string(crypto_pk_t *env, const char *src, size_t len)
Definition: crypto_rsa.c:539
int crypto_pk_write_private_key_to_string(crypto_pk_t *env, char **dest, size_t *len)
Definition: crypto_rsa.c:478
void crypto_pk_free_(crypto_pk_t *env)
crypto_pk_t * crypto_pk_base64_decode_private(const char *str, size_t len)
Definition: crypto_rsa.c:669
Headers for log.c.
Macros to implement mocking and selective exposure for the test code.
#define STATIC
Definition: testsupport.h:32
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
Integer definitions used throughout Tor.