📄 crypto.c
字号:
outlen = crypto_pk_private_decrypt(env,buf,from,pkeylen,padding,
warnOnFailure);
if (outlen<0) {
log_fn(warnOnFailure?LOG_WARN:LOG_DEBUG, LD_CRYPTO,
"Error decrypting public-key data");
goto err;
}
if (outlen < CIPHER_KEY_LEN) {
log_fn(warnOnFailure?LOG_WARN:LOG_INFO, LD_CRYPTO,
"No room for a symmetric key");
goto err;
}
cipher = crypto_create_init_cipher(buf, 0);
if (!cipher) {
goto err;
}
memcpy(to,buf+CIPHER_KEY_LEN,outlen-CIPHER_KEY_LEN);
outlen -= CIPHER_KEY_LEN;
r = crypto_cipher_decrypt(cipher, to+outlen, from+pkeylen, fromlen-pkeylen);
if (r<0)
goto err;
memset(buf,0,pkeylen);
tor_free(buf);
crypto_free_cipher_env(cipher);
tor_assert(outlen + fromlen < INT_MAX);
return (int)(outlen + (fromlen-pkeylen));
err:
memset(buf,0,pkeylen);
tor_free(buf);
if (cipher) crypto_free_cipher_env(cipher);
return -1;
}
/** ASN.1-encode the public portion of <b>pk</b> into <b>dest</b>.
* Return -1 on error, or the number of characters used on success.
*/
int
crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, size_t dest_len)
{
int len;
unsigned char *buf, *cp;
len = i2d_RSAPublicKey(pk->key, NULL);
if (len < 0 || (size_t)len > dest_len)
return -1;
cp = buf = tor_malloc(len+1);
len = i2d_RSAPublicKey(pk->key, &cp);
if (len < 0) {
crypto_log_errors(LOG_WARN,"encoding public key");
tor_free(buf);
return -1;
}
/* We don't encode directly into 'dest', because that would be illegal
* type-punning. (C99 is smarter than me, C99 is smarter than me...)
*/
memcpy(dest,buf,len);
tor_free(buf);
return len;
}
/** Decode an ASN.1-encoded public key from <b>str</b>; return the result on
* success and NULL on failure.
*/
crypto_pk_env_t *
crypto_pk_asn1_decode(const char *str, size_t len)
{
RSA *rsa;
unsigned char *buf;
/* This ifdef suppresses a type warning. Take out the first case once
* everybody is using openssl 0.9.7 or later.
*/
const unsigned char *cp;
cp = buf = tor_malloc(len);
memcpy(buf,str,len);
rsa = d2i_RSAPublicKey(NULL, &cp, len);
tor_free(buf);
if (!rsa) {
crypto_log_errors(LOG_WARN,"decoding public key");
return NULL;
}
return _crypto_new_pk_env_rsa(rsa);
}
/** Given a private or public key <b>pk</b>, put a SHA1 hash of the
* public key into <b>digest_out</b> (must have DIGEST_LEN bytes of space).
* Return 0 on success, -1 on failure.
*/
int
crypto_pk_get_digest(crypto_pk_env_t *pk, char *digest_out)
{
unsigned char *buf, *bufp;
int len;
len = i2d_RSAPublicKey(pk->key, NULL);
if (len < 0)
return -1;
buf = bufp = tor_malloc(len+1);
len = i2d_RSAPublicKey(pk->key, &bufp);
if (len < 0) {
crypto_log_errors(LOG_WARN,"encoding public key");
tor_free(buf);
return -1;
}
if (crypto_digest(digest_out, (char*)buf, len) < 0) {
tor_free(buf);
return -1;
}
tor_free(buf);
return 0;
}
/** Copy <b>in</b> to the <b>outlen</b>-byte buffer <b>out</b>, adding spaces
* every four spaces. */
/* static */ void
add_spaces_to_fp(char *out, size_t outlen, const char *in)
{
int n = 0;
char *end = out+outlen;
while (*in && out<end) {
*out++ = *in++;
if (++n == 4 && *in && out<end) {
n = 0;
*out++ = ' ';
}
}
tor_assert(out<end);
*out = '\0';
}
/** Given a private or public key <b>pk</b>, put a fingerprint of the
* public key into <b>fp_out</b> (must have at least FINGERPRINT_LEN+1 bytes of
* space). Return 0 on success, -1 on failure.
*
* Fingerprints are computed as the SHA1 digest of the ASN.1 encoding
* of the public key, converted to hexadecimal, in upper case, with a
* space after every four digits.
*
* If <b>add_space</b> is false, omit the spaces.
*/
int
crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out, int add_space)
{
char digest[DIGEST_LEN];
char hexdigest[HEX_DIGEST_LEN+1];
if (crypto_pk_get_digest(pk, digest)) {
return -1;
}
base16_encode(hexdigest,sizeof(hexdigest),digest,DIGEST_LEN);
if (add_space) {
add_spaces_to_fp(fp_out, FINGERPRINT_LEN+1, hexdigest);
} else {
strncpy(fp_out, hexdigest, HEX_DIGEST_LEN+1);
}
return 0;
}
/** Return true iff <b>s</b> is in the correct format for a fingerprint.
*/
int
crypto_pk_check_fingerprint_syntax(const char *s)
{
int i;
for (i = 0; i < FINGERPRINT_LEN; ++i) {
if ((i%5) == 4) {
if (!TOR_ISSPACE(s[i])) return 0;
} else {
if (!TOR_ISXDIGIT(s[i])) return 0;
}
}
if (s[FINGERPRINT_LEN]) return 0;
return 1;
}
/* symmetric crypto */
/** Generate a new random key for the symmetric cipher in <b>env</b>.
* Return 0 on success, -1 on failure. Does not initialize the cipher.
*/
int
crypto_cipher_generate_key(crypto_cipher_env_t *env)
{
tor_assert(env);
return crypto_rand(env->key, CIPHER_KEY_LEN);
}
/** Set the symmetric key for the cipher in <b>env</b> to the first
* CIPHER_KEY_LEN bytes of <b>key</b>. Does not initialize the cipher.
* Return 0 on success, -1 on failure.
*/
int
crypto_cipher_set_key(crypto_cipher_env_t *env, const char *key)
{
tor_assert(env);
tor_assert(key);
if (!env->key)
return -1;
memcpy(env->key, key, CIPHER_KEY_LEN);
return 0;
}
/** Generate an initialization vector for our AES-CTR cipher; store it
* in the first CIPHER_IV_LEN bytes of <b>iv_out</b>. */
void
crypto_cipher_generate_iv(char *iv_out)
{
crypto_rand(iv_out, CIPHER_IV_LEN);
}
/** Adjust the counter of <b>env</b> to point to the first byte of the block
* corresponding to the encryption of the CIPHER_IV_LEN bytes at
* <b>iv</b>. */
int
crypto_cipher_set_iv(crypto_cipher_env_t *env, const char *iv)
{
tor_assert(env);
tor_assert(iv);
aes_set_iv(env->cipher, iv);
return 0;
}
/** Return a pointer to the key set for the cipher in <b>env</b>.
*/
const char *
crypto_cipher_get_key(crypto_cipher_env_t *env)
{
return env->key;
}
/** Initialize the cipher in <b>env</b> for encryption. Return 0 on
* success, -1 on failure.
*/
int
crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env)
{
tor_assert(env);
aes_set_key(env->cipher, env->key, CIPHER_KEY_LEN*8);
return 0;
}
/** Initialize the cipher in <b>env</b> for decryption. Return 0 on
* success, -1 on failure.
*/
int
crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env)
{
tor_assert(env);
aes_set_key(env->cipher, env->key, CIPHER_KEY_LEN*8);
return 0;
}
/** Encrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
* <b>env</b>; on success, store the result to <b>to</b> and return 0.
* On failure, return -1.
*/
int
crypto_cipher_encrypt(crypto_cipher_env_t *env, char *to,
const char *from, size_t fromlen)
{
tor_assert(env);
tor_assert(env->cipher);
tor_assert(from);
tor_assert(fromlen);
tor_assert(to);
aes_crypt(env->cipher, from, fromlen, to);
return 0;
}
/** Decrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
* <b>env</b>; on success, store the result to <b>to</b> and return 0.
* On failure, return -1.
*/
int
crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
const char *from, size_t fromlen)
{
tor_assert(env);
tor_assert(from);
tor_assert(to);
aes_crypt(env->cipher, from, fromlen, to);
return 0;
}
/** Encrypt <b>len</b> bytes on <b>from</b> using the cipher in <b>env</b>;
* on success, return 0. On failure, return -1.
*/
int
crypto_cipher_crypt_inplace(crypto_cipher_env_t *env, char *buf, size_t len)
{
aes_crypt_inplace(env->cipher, buf, len);
return 0;
}
/** Encrypt <b>fromlen</b> bytes (at least 1) from <b>from</b> with the key in
* <b>cipher</b> to the buffer in <b>to</b> of length
* <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> plus
* CIPHER_IV_LEN bytes for the initialization vector. On success, return the
* number of bytes written, on failure, return -1.
*
* This function adjusts the current position of the counter in <b>cipher</b>
* to immediately after the encrypted data.
*/
int
crypto_cipher_encrypt_with_iv(crypto_cipher_env_t *cipher,
char *to, size_t tolen,
const char *from, size_t fromlen)
{
tor_assert(cipher);
tor_assert(from);
tor_assert(to);
tor_assert(fromlen < INT_MAX);
if (fromlen < 1)
return -1;
if (tolen < fromlen + CIPHER_IV_LEN)
return -1;
crypto_cipher_generate_iv(to);
if (crypto_cipher_set_iv(cipher, to)<0)
return -1;
crypto_cipher_encrypt(cipher, to+CIPHER_IV_LEN, from, fromlen);
return (int)(fromlen + CIPHER_IV_LEN);
}
/** Decrypt <b>fromlen</b> bytes (at least 1+CIPHER_IV_LEN) from <b>from</b>
* with the key in <b>cipher</b> to the buffer in <b>to</b> of length
* <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> minus
* CIPHER_IV_LEN bytes for the initialization vector. On success, return the
* number of bytes written, on failure, return -1.
*
* This function adjusts the current position of the counter in <b>cipher</b>
* to immediately after the decrypted data.
*/
int
crypto_cipher_decrypt_with_iv(crypto_cipher_env_t *cipher,
char *to, size_t tolen,
const char *from, size_t fromlen)
{
tor_assert(cipher);
tor_assert(from);
tor_assert(to);
tor_assert(fromlen < INT_MAX);
if (fromlen <= CIPHER_IV_LEN)
return -1;
if (tolen < fromlen - CIPHER_IV_LEN)
return -1;
if (crypto_cipher_set_iv(cipher, from)<0)
return -1;
crypto_cipher_encrypt(cipher, to, from+CIPHER_IV_LEN, fromlen-CIPHER_IV_LEN);
return (int)(fromlen - CIPHER_IV_LEN);
}
/* SHA-1 */
/** Compute the SHA1 digest of <b>len</b> bytes in data stored in
* <b>m</b>. Write the DIGEST_LEN byte result into <b>digest</b>.
* Return 0 on success, -1 on failure.
*/
int
crypto_digest(char *digest, const char *m, size_t len)
{
tor_assert(m);
tor_assert(digest);
return (SHA1((const unsigned char*)m,len,(unsigned char*)digest) == NULL);
}
/** Intermediate information about the digest of a stream of data. */
struct crypto_digest_env_t {
SHA_CTX d;
};
/** Allocate and return a new digest object.
*/
crypto_digest_env_t *
crypto_new_digest_env(void)
{
crypto_digest_env_t *r;
r = tor_malloc(sizeof(crypto_digest_env_t));
SHA1_Init(&r->d);
return r;
}
/** Deallocate a digest object.
*/
void
crypto_free_digest_env(crypto_digest_env_t *digest)
{
memset(digest, 0, sizeof(crypto_digest_env_t));
tor_free(digest);
}
/** Add <b>len</b> bytes from <b>data</b> to the digest object.
*/
void
crypto_digest_add_bytes(crypto_digest_env_t *digest, const char *data,
size_t len)
{
tor_assert(digest);
tor_assert(data);
/* Using the SHA1_*() calls directly means we don't support doing
* sha1 in hardware. But so far the delay of getting the question
* to the hardware, and hearing the answer, is likely higher than
* just doing it ourselves. Hashes are fast.
*/
SHA1_Update(&digest->d, (void*)data, len);
}
/** Compute the hash of the data that has been passed to the digest
* object; write the first out_len bytes of the result to <b>out</b>.
* <b>out_len</b> must be \<= DIGEST_LEN.
*/
void
crypto_digest_get_digest(crypto_digest_env_t *digest,
char *out, size_t out_len)
{
unsigned char r[DIGEST_LEN];
SHA_CTX tmpctx;
tor_assert(digest);
tor_assert(out);
tor_assert(out_len <= DIGEST_LEN);
/* memcpy into a temporary ctx, since SHA1_Final clears the context */
memcpy(&tmpctx, &digest->d, sizeof(SHA_CTX));
SHA1_Final(r, &tmpctx);
memcpy(out, r, out_len);
memset(r, 0, sizeof(r));
}
/** Allocate and return a new digest object with the same state as
* <b>digest</b>
*/
crypto_digest_env_t *
crypto_digest_dup(const crypto_digest_env_t *digest)
{
crypto_digest_env_t *r;
tor_assert(digest);
r = tor_malloc(sizeof(crypto_digest_env_t));
memcpy(r,digest,sizeof(crypto_digest_env_t));
return r;
}
/** Replace the state of the digest object <b>into</b> with the state
* of the digest object <b>from</b>.
*/
void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -