Loading changes/dropped_openssl_vers 0 → 100644 +3 −0 Original line number Diff line number Diff line o Required libraries: - Tor now requires OpenSSL 0.9.8 or later; OpenSSL 1.0.0 or later is strongly recommended. src/common/crypto.c +11 −89 Original line number Diff line number Diff line Loading @@ -57,8 +57,8 @@ #include "container.h" #include "compat.h" #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,7) #error "We require OpenSSL >= 0.9.7" #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) #error "We require OpenSSL >= 0.9.8" #endif #ifdef ANDROID Loading @@ -69,31 +69,6 @@ /** Longest recognized */ #define MAX_DNS_LABEL_SIZE 63 #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) && \ !defined(RUNNING_DOXYGEN) /** @{ */ /** On OpenSSL versions before 0.9.8, there is no working SHA256 * implementation, so we use Tom St Denis's nice speedy one, slightly adapted * to our needs. These macros make it usable by us. */ #define SHA256_CTX sha256_state #define SHA256_Init sha256_init #define SHA256_Update sha256_process #define LTC_ARGCHK(x) tor_assert(x) /** @} */ #include "sha256.c" #define SHA256_Final(a,b) sha256_done(b,a) static unsigned char * SHA256(const unsigned char *m, size_t len, unsigned char *d) { SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, m, len); SHA256_Final(d, &ctx); return d; } #endif /** Macro: is k a valid RSA public or private key? */ #define PUBLIC_KEY_OK(k) ((k) && (k)->key && (k)->key->n) /** Macro: is k a valid RSA private key? */ Loading Loading @@ -268,6 +243,11 @@ crypto_global_init(int useAccel, const char *accelName, const char *accelDir) SSLeay(), SSLeay_version(SSLEAY_VERSION)); } if (SSLeay() < OPENSSL_V_SERIES(1,0,0)) { log_notice(LD_CRYPTO, "Your OpenSSL version seems to be %s. We " "recommend 1.0.0 or later.", crypto_openssl_get_version_str()); } if (useAccel > 0) { #ifdef DISABLE_ENGINES (void)accelName; Loading Loading @@ -478,11 +458,7 @@ crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits) if (env->key) RSA_free(env->key); #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) /* In OpenSSL 0.9.7, RSA_generate_key is all we have. */ env->key = RSA_generate_key(bits, 65537, NULL, NULL); #else /* In OpenSSL 0.9.8, RSA_generate_key is deprecated. */ { BIGNUM *e = BN_new(); RSA *r = NULL; Loading @@ -504,7 +480,7 @@ crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits) if (r) RSA_free(r); } #endif if (!env->key) { crypto_log_errors(LOG_WARN, "generating RSA key"); return -1; Loading Loading @@ -1676,63 +1652,11 @@ crypto_hmac_sha256(char *hmac_out, const char *key, size_t key_len, const char *msg, size_t msg_len) { #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(0,9,8) /* If we've got OpenSSL >=0.9.8 we can use its hmac implementation. */ tor_assert(key_len < INT_MAX); tor_assert(msg_len < INT_MAX); HMAC(EVP_sha256(), key, (int)key_len, (unsigned char*)msg, (int)msg_len, (unsigned char*)hmac_out, NULL); #else /* OpenSSL doesn't have an EVP implementation for SHA256. We'll need to do HMAC on our own. HMAC isn't so hard: To compute HMAC(key, msg): 1. If len(key) > blocksize, key = H(key). 2. If len(key) < blocksize, right-pad key up to blocksize with 0 bytes. 3. let ipad = key xor 0x363636363636....36 let opad = key xor 0x5c5c5c5c5c5c....5c The result is H(opad | H( ipad | msg ) ) */ #define BLOCKSIZE 64 #define DIGESTSIZE 32 uint8_t k[BLOCKSIZE]; uint8_t pad[BLOCKSIZE]; uint8_t d[DIGESTSIZE]; int i; SHA256_CTX st; tor_assert(key_len < INT_MAX); tor_assert(msg_len < INT_MAX); if (key_len <= BLOCKSIZE) { memset(k, 0, sizeof(k)); memcpy(k, key, key_len); /* not time invariant in key_len */ } else { SHA256((const uint8_t *)key, key_len, k); memset(k+DIGESTSIZE, 0, sizeof(k)-DIGESTSIZE); } for (i = 0; i < BLOCKSIZE; ++i) pad[i] = k[i] ^ 0x36; SHA256_Init(&st); SHA256_Update(&st, pad, BLOCKSIZE); SHA256_Update(&st, (uint8_t*)msg, msg_len); SHA256_Final(d, &st); for (i = 0; i < BLOCKSIZE; ++i) pad[i] = k[i] ^ 0x5c; SHA256_Init(&st); SHA256_Update(&st, pad, BLOCKSIZE); SHA256_Update(&st, d, DIGESTSIZE); SHA256_Final((uint8_t*)hmac_out, &st); /* Now clear everything. */ memset(k, 0, sizeof(k)); memset(pad, 0, sizeof(pad)); memset(d, 0, sizeof(d)); memset(&st, 0, sizeof(st)); #undef BLOCKSIZE #undef DIGESTSIZE #endif } /* DH */ Loading Loading @@ -2335,9 +2259,7 @@ crypto_dh_free(crypto_dh_t *dh) * that fd without checking whether it fit in the fd_set. Thus, if the * system has not just been started up, it is unsafe to call */ #define RAND_POLL_IS_SAFE \ ((OPENSSL_VERSION_NUMBER >= OPENSSL_V(0,9,7,'j') && \ OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8)) || \ OPENSSL_VERSION_NUMBER >= OPENSSL_V(0,9,8,'c')) (OPENSSL_VERSION_NUMBER >= OPENSSL_V(0,9,8,'c')) /** Set the seed of the weak RNG to a random value. */ static void Loading src/common/include.am +1 −2 Original line number Diff line number Diff line Loading @@ -3,7 +3,6 @@ noinst_LIBRARIES+= src/common/libor.a src/common/libor-crypto.a src/common/libor EXTRA_DIST+= \ src/common/common_sha1.i \ src/common/sha256.c \ src/common/Makefile.nmake #CFLAGS = -Wall -Wpointer-arith -O2 Loading Loading @@ -72,4 +71,4 @@ src/common/common_sha1.i: $(libor_SOURCES) $(libor_crypto_a_SOURCES) $(COMMONHEA fi src/common/util_codedigest.o: src/common/common_sha1.i src/common/crypto.o: src/common/sha256.c src/common/sha256.cdeleted 100644 → 0 +0 −331 Original line number Diff line number Diff line /* Copyright (c) 2009-2012, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /* This SHA256 implementation is adapted from the public domain one in LibTomCrypt, version 1.6. Tor uses it on platforms where OpenSSL doesn't have a SHA256. */ typedef struct sha256_state { uint64_t length; uint32_t state[8], curlen; unsigned char buf[64]; } sha256_state; #define CRYPT_OK 0 #define CRYPT_NOP -1 #define CRYPT_INVALID_ARG -2 #define LOAD32H(x,y) STMT_BEGIN x = ntohl(get_uint32((const char*)y)); STMT_END #define STORE32H(x,y) STMT_BEGIN set_uint32((char*)y, htonl(x)); STMT_END #define STORE64H(x,y) STMT_BEGIN \ set_uint32((char*)y, htonl((uint32_t)((x)>>32))); \ set_uint32(((char*)y)+4, htonl((uint32_t)((x)&0xffffffff))); \ STMT_END #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) #ifndef MIN #define MIN(x, y) ( ((x)<(y))?(x):(y) ) #endif /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com */ /** @file sha256.c SHA256 by Tom St Denis */ #ifdef LTC_SMALL_CODE /* the K array */ static const uint32_t K[64] = { 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL }; #endif /* Various logical functions */ #define Ch(x,y,z) (z ^ (x & (y ^ z))) #define Maj(x,y,z) (((x | y) & z) | (x & y)) #define S(x, n) RORc((x),(n)) #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) /* compress 512-bits */ #ifdef LTC_CLEAN_STACK static int _sha256_compress(sha256_state * md, unsigned char *buf) #else static int sha256_compress(sha256_state * md, unsigned char *buf) #endif { uint32_t S[8], W[64], t0, t1; #ifdef LTC_SMALL_CODE uint32_t t; #endif int i; /* copy state into S */ for (i = 0; i < 8; i++) { S[i] = md->state[i]; } /* copy the state into 512-bits into W[0..15] */ for (i = 0; i < 16; i++) { LOAD32H(W[i], buf + (4*i)); } /* fill W[16..63] */ for (i = 16; i < 64; i++) { W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; } /* Compress */ #ifdef LTC_SMALL_CODE #define RND(a,b,c,d,e,f,g,h,i) \ t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ t1 = Sigma0(a) + Maj(a, b, c); \ d += t0; \ h = t0 + t1; for (i = 0; i < 64; ++i) { RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i); t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4]; S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t; } #else #define RND(a,b,c,d,e,f,g,h,i,ki) \ t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ t1 = Sigma0(a) + Maj(a, b, c); \ d += t0; \ h = t0 + t1; RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); #undef RND #endif /* feedback */ for (i = 0; i < 8; i++) { md->state[i] = md->state[i] + S[i]; } return CRYPT_OK; } #ifdef LTC_CLEAN_STACK static int sha256_compress(sha256_state * md, unsigned char *buf) { int err; err = _sha256_compress(md, buf); burn_stack(sizeof(uint32_t) * 74); return err; } #endif /** Initialize the hash state @param md The hash state you wish to initialize @return CRYPT_OK if successful */ static int sha256_init(sha256_state * md) { LTC_ARGCHK(md != NULL); md->curlen = 0; md->length = 0; md->state[0] = 0x6A09E667UL; md->state[1] = 0xBB67AE85UL; md->state[2] = 0x3C6EF372UL; md->state[3] = 0xA54FF53AUL; md->state[4] = 0x510E527FUL; md->state[5] = 0x9B05688CUL; md->state[6] = 0x1F83D9ABUL; md->state[7] = 0x5BE0CD19UL; return CRYPT_OK; } /** Process a block of memory though the hash @param md The hash state @param in The data to hash @param inlen The length of the data (octets) @return CRYPT_OK if successful */ static int sha256_process (sha256_state * md, const unsigned char *in, unsigned long inlen) { unsigned long n; int err; LTC_ARGCHK(md != NULL); LTC_ARGCHK(in != NULL); if (md->curlen > sizeof(md->buf)) { return CRYPT_INVALID_ARG; } while (inlen > 0) { if (md->curlen == 0 && inlen >= 64) { if ((err = sha256_compress (md, (unsigned char *)in)) != CRYPT_OK) { return err; } md->length += 64 * 8; in += 64; inlen -= 64; } else { n = MIN(inlen, (64 - md->curlen)); memcpy(md->buf + md->curlen, in, (size_t)n); md->curlen += n; in += n; inlen -= n; if (md->curlen == 64) { if ((err = sha256_compress (md, md->buf)) != CRYPT_OK) { return err; } md->length += 8*64; md->curlen = 0; } } } return CRYPT_OK; } /** Terminate the hash to get the digest @param md The hash state @param out [out] The destination of the hash (32 bytes) @return CRYPT_OK if successful */ static int sha256_done(sha256_state * md, unsigned char *out) { int i; LTC_ARGCHK(md != NULL); LTC_ARGCHK(out != NULL); if (md->curlen >= sizeof(md->buf)) { return CRYPT_INVALID_ARG; } /* increase the length of the message */ md->length += md->curlen * 8; /* append the '1' bit */ md->buf[md->curlen++] = (unsigned char)0x80; /* if the length is currently above 56 bytes we append zeros * then compress. Then we can fall back to padding zeros and length * encoding like normal. */ if (md->curlen > 56) { while (md->curlen < 64) { md->buf[md->curlen++] = (unsigned char)0; } sha256_compress(md, md->buf); md->curlen = 0; } /* pad upto 56 bytes of zeroes */ while (md->curlen < 56) { md->buf[md->curlen++] = (unsigned char)0; } /* store length */ STORE64H(md->length, md->buf+56); sha256_compress(md, md->buf); /* copy output */ for (i = 0; i < 8; i++) { STORE32H(md->state[i], out+(4*i)); } #ifdef LTC_CLEAN_STACK zeromem(md, sizeof(sha256_state)); #endif return CRYPT_OK; } /* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha256.c,v $ */ /* $Revision: 1.9 $ */ /* $Date: 2006/11/01 09:28:17 $ */ src/common/tortls.c +3 −8 Original line number Diff line number Diff line Loading @@ -58,8 +58,8 @@ #include "container.h" #include <string.h> #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,7) #error "We require OpenSSL >= 0.9.7" #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) #error "We require OpenSSL >= 0.9.8" #endif /* Enable the "v2" TLS handshake. Loading Loading @@ -778,13 +778,8 @@ tor_cert_decode(const uint8_t *certificate, size_t certificate_len) if (certificate_len > INT_MAX) return NULL; #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) /* This ifdef suppresses a type warning. Take out this case once everybody * is using OpenSSL 0.9.8 or later. */ x509 = d2i_X509(NULL, (unsigned char**)&cp, (int)certificate_len); #else x509 = d2i_X509(NULL, &cp, (int)certificate_len); #endif if (!x509) return NULL; /* Couldn't decode */ if (cp - certificate != (int)certificate_len) { Loading Loading
changes/dropped_openssl_vers 0 → 100644 +3 −0 Original line number Diff line number Diff line o Required libraries: - Tor now requires OpenSSL 0.9.8 or later; OpenSSL 1.0.0 or later is strongly recommended.
src/common/crypto.c +11 −89 Original line number Diff line number Diff line Loading @@ -57,8 +57,8 @@ #include "container.h" #include "compat.h" #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,7) #error "We require OpenSSL >= 0.9.7" #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) #error "We require OpenSSL >= 0.9.8" #endif #ifdef ANDROID Loading @@ -69,31 +69,6 @@ /** Longest recognized */ #define MAX_DNS_LABEL_SIZE 63 #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) && \ !defined(RUNNING_DOXYGEN) /** @{ */ /** On OpenSSL versions before 0.9.8, there is no working SHA256 * implementation, so we use Tom St Denis's nice speedy one, slightly adapted * to our needs. These macros make it usable by us. */ #define SHA256_CTX sha256_state #define SHA256_Init sha256_init #define SHA256_Update sha256_process #define LTC_ARGCHK(x) tor_assert(x) /** @} */ #include "sha256.c" #define SHA256_Final(a,b) sha256_done(b,a) static unsigned char * SHA256(const unsigned char *m, size_t len, unsigned char *d) { SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, m, len); SHA256_Final(d, &ctx); return d; } #endif /** Macro: is k a valid RSA public or private key? */ #define PUBLIC_KEY_OK(k) ((k) && (k)->key && (k)->key->n) /** Macro: is k a valid RSA private key? */ Loading Loading @@ -268,6 +243,11 @@ crypto_global_init(int useAccel, const char *accelName, const char *accelDir) SSLeay(), SSLeay_version(SSLEAY_VERSION)); } if (SSLeay() < OPENSSL_V_SERIES(1,0,0)) { log_notice(LD_CRYPTO, "Your OpenSSL version seems to be %s. We " "recommend 1.0.0 or later.", crypto_openssl_get_version_str()); } if (useAccel > 0) { #ifdef DISABLE_ENGINES (void)accelName; Loading Loading @@ -478,11 +458,7 @@ crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits) if (env->key) RSA_free(env->key); #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) /* In OpenSSL 0.9.7, RSA_generate_key is all we have. */ env->key = RSA_generate_key(bits, 65537, NULL, NULL); #else /* In OpenSSL 0.9.8, RSA_generate_key is deprecated. */ { BIGNUM *e = BN_new(); RSA *r = NULL; Loading @@ -504,7 +480,7 @@ crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits) if (r) RSA_free(r); } #endif if (!env->key) { crypto_log_errors(LOG_WARN, "generating RSA key"); return -1; Loading Loading @@ -1676,63 +1652,11 @@ crypto_hmac_sha256(char *hmac_out, const char *key, size_t key_len, const char *msg, size_t msg_len) { #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(0,9,8) /* If we've got OpenSSL >=0.9.8 we can use its hmac implementation. */ tor_assert(key_len < INT_MAX); tor_assert(msg_len < INT_MAX); HMAC(EVP_sha256(), key, (int)key_len, (unsigned char*)msg, (int)msg_len, (unsigned char*)hmac_out, NULL); #else /* OpenSSL doesn't have an EVP implementation for SHA256. We'll need to do HMAC on our own. HMAC isn't so hard: To compute HMAC(key, msg): 1. If len(key) > blocksize, key = H(key). 2. If len(key) < blocksize, right-pad key up to blocksize with 0 bytes. 3. let ipad = key xor 0x363636363636....36 let opad = key xor 0x5c5c5c5c5c5c....5c The result is H(opad | H( ipad | msg ) ) */ #define BLOCKSIZE 64 #define DIGESTSIZE 32 uint8_t k[BLOCKSIZE]; uint8_t pad[BLOCKSIZE]; uint8_t d[DIGESTSIZE]; int i; SHA256_CTX st; tor_assert(key_len < INT_MAX); tor_assert(msg_len < INT_MAX); if (key_len <= BLOCKSIZE) { memset(k, 0, sizeof(k)); memcpy(k, key, key_len); /* not time invariant in key_len */ } else { SHA256((const uint8_t *)key, key_len, k); memset(k+DIGESTSIZE, 0, sizeof(k)-DIGESTSIZE); } for (i = 0; i < BLOCKSIZE; ++i) pad[i] = k[i] ^ 0x36; SHA256_Init(&st); SHA256_Update(&st, pad, BLOCKSIZE); SHA256_Update(&st, (uint8_t*)msg, msg_len); SHA256_Final(d, &st); for (i = 0; i < BLOCKSIZE; ++i) pad[i] = k[i] ^ 0x5c; SHA256_Init(&st); SHA256_Update(&st, pad, BLOCKSIZE); SHA256_Update(&st, d, DIGESTSIZE); SHA256_Final((uint8_t*)hmac_out, &st); /* Now clear everything. */ memset(k, 0, sizeof(k)); memset(pad, 0, sizeof(pad)); memset(d, 0, sizeof(d)); memset(&st, 0, sizeof(st)); #undef BLOCKSIZE #undef DIGESTSIZE #endif } /* DH */ Loading Loading @@ -2335,9 +2259,7 @@ crypto_dh_free(crypto_dh_t *dh) * that fd without checking whether it fit in the fd_set. Thus, if the * system has not just been started up, it is unsafe to call */ #define RAND_POLL_IS_SAFE \ ((OPENSSL_VERSION_NUMBER >= OPENSSL_V(0,9,7,'j') && \ OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8)) || \ OPENSSL_VERSION_NUMBER >= OPENSSL_V(0,9,8,'c')) (OPENSSL_VERSION_NUMBER >= OPENSSL_V(0,9,8,'c')) /** Set the seed of the weak RNG to a random value. */ static void Loading
src/common/include.am +1 −2 Original line number Diff line number Diff line Loading @@ -3,7 +3,6 @@ noinst_LIBRARIES+= src/common/libor.a src/common/libor-crypto.a src/common/libor EXTRA_DIST+= \ src/common/common_sha1.i \ src/common/sha256.c \ src/common/Makefile.nmake #CFLAGS = -Wall -Wpointer-arith -O2 Loading Loading @@ -72,4 +71,4 @@ src/common/common_sha1.i: $(libor_SOURCES) $(libor_crypto_a_SOURCES) $(COMMONHEA fi src/common/util_codedigest.o: src/common/common_sha1.i src/common/crypto.o: src/common/sha256.c
src/common/sha256.cdeleted 100644 → 0 +0 −331 Original line number Diff line number Diff line /* Copyright (c) 2009-2012, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /* This SHA256 implementation is adapted from the public domain one in LibTomCrypt, version 1.6. Tor uses it on platforms where OpenSSL doesn't have a SHA256. */ typedef struct sha256_state { uint64_t length; uint32_t state[8], curlen; unsigned char buf[64]; } sha256_state; #define CRYPT_OK 0 #define CRYPT_NOP -1 #define CRYPT_INVALID_ARG -2 #define LOAD32H(x,y) STMT_BEGIN x = ntohl(get_uint32((const char*)y)); STMT_END #define STORE32H(x,y) STMT_BEGIN set_uint32((char*)y, htonl(x)); STMT_END #define STORE64H(x,y) STMT_BEGIN \ set_uint32((char*)y, htonl((uint32_t)((x)>>32))); \ set_uint32(((char*)y)+4, htonl((uint32_t)((x)&0xffffffff))); \ STMT_END #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) #ifndef MIN #define MIN(x, y) ( ((x)<(y))?(x):(y) ) #endif /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com */ /** @file sha256.c SHA256 by Tom St Denis */ #ifdef LTC_SMALL_CODE /* the K array */ static const uint32_t K[64] = { 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL }; #endif /* Various logical functions */ #define Ch(x,y,z) (z ^ (x & (y ^ z))) #define Maj(x,y,z) (((x | y) & z) | (x & y)) #define S(x, n) RORc((x),(n)) #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) /* compress 512-bits */ #ifdef LTC_CLEAN_STACK static int _sha256_compress(sha256_state * md, unsigned char *buf) #else static int sha256_compress(sha256_state * md, unsigned char *buf) #endif { uint32_t S[8], W[64], t0, t1; #ifdef LTC_SMALL_CODE uint32_t t; #endif int i; /* copy state into S */ for (i = 0; i < 8; i++) { S[i] = md->state[i]; } /* copy the state into 512-bits into W[0..15] */ for (i = 0; i < 16; i++) { LOAD32H(W[i], buf + (4*i)); } /* fill W[16..63] */ for (i = 16; i < 64; i++) { W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; } /* Compress */ #ifdef LTC_SMALL_CODE #define RND(a,b,c,d,e,f,g,h,i) \ t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ t1 = Sigma0(a) + Maj(a, b, c); \ d += t0; \ h = t0 + t1; for (i = 0; i < 64; ++i) { RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i); t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4]; S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t; } #else #define RND(a,b,c,d,e,f,g,h,i,ki) \ t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ t1 = Sigma0(a) + Maj(a, b, c); \ d += t0; \ h = t0 + t1; RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); #undef RND #endif /* feedback */ for (i = 0; i < 8; i++) { md->state[i] = md->state[i] + S[i]; } return CRYPT_OK; } #ifdef LTC_CLEAN_STACK static int sha256_compress(sha256_state * md, unsigned char *buf) { int err; err = _sha256_compress(md, buf); burn_stack(sizeof(uint32_t) * 74); return err; } #endif /** Initialize the hash state @param md The hash state you wish to initialize @return CRYPT_OK if successful */ static int sha256_init(sha256_state * md) { LTC_ARGCHK(md != NULL); md->curlen = 0; md->length = 0; md->state[0] = 0x6A09E667UL; md->state[1] = 0xBB67AE85UL; md->state[2] = 0x3C6EF372UL; md->state[3] = 0xA54FF53AUL; md->state[4] = 0x510E527FUL; md->state[5] = 0x9B05688CUL; md->state[6] = 0x1F83D9ABUL; md->state[7] = 0x5BE0CD19UL; return CRYPT_OK; } /** Process a block of memory though the hash @param md The hash state @param in The data to hash @param inlen The length of the data (octets) @return CRYPT_OK if successful */ static int sha256_process (sha256_state * md, const unsigned char *in, unsigned long inlen) { unsigned long n; int err; LTC_ARGCHK(md != NULL); LTC_ARGCHK(in != NULL); if (md->curlen > sizeof(md->buf)) { return CRYPT_INVALID_ARG; } while (inlen > 0) { if (md->curlen == 0 && inlen >= 64) { if ((err = sha256_compress (md, (unsigned char *)in)) != CRYPT_OK) { return err; } md->length += 64 * 8; in += 64; inlen -= 64; } else { n = MIN(inlen, (64 - md->curlen)); memcpy(md->buf + md->curlen, in, (size_t)n); md->curlen += n; in += n; inlen -= n; if (md->curlen == 64) { if ((err = sha256_compress (md, md->buf)) != CRYPT_OK) { return err; } md->length += 8*64; md->curlen = 0; } } } return CRYPT_OK; } /** Terminate the hash to get the digest @param md The hash state @param out [out] The destination of the hash (32 bytes) @return CRYPT_OK if successful */ static int sha256_done(sha256_state * md, unsigned char *out) { int i; LTC_ARGCHK(md != NULL); LTC_ARGCHK(out != NULL); if (md->curlen >= sizeof(md->buf)) { return CRYPT_INVALID_ARG; } /* increase the length of the message */ md->length += md->curlen * 8; /* append the '1' bit */ md->buf[md->curlen++] = (unsigned char)0x80; /* if the length is currently above 56 bytes we append zeros * then compress. Then we can fall back to padding zeros and length * encoding like normal. */ if (md->curlen > 56) { while (md->curlen < 64) { md->buf[md->curlen++] = (unsigned char)0; } sha256_compress(md, md->buf); md->curlen = 0; } /* pad upto 56 bytes of zeroes */ while (md->curlen < 56) { md->buf[md->curlen++] = (unsigned char)0; } /* store length */ STORE64H(md->length, md->buf+56); sha256_compress(md, md->buf); /* copy output */ for (i = 0; i < 8; i++) { STORE32H(md->state[i], out+(4*i)); } #ifdef LTC_CLEAN_STACK zeromem(md, sizeof(sha256_state)); #endif return CRYPT_OK; } /* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha256.c,v $ */ /* $Revision: 1.9 $ */ /* $Date: 2006/11/01 09:28:17 $ */
src/common/tortls.c +3 −8 Original line number Diff line number Diff line Loading @@ -58,8 +58,8 @@ #include "container.h" #include <string.h> #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,7) #error "We require OpenSSL >= 0.9.7" #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) #error "We require OpenSSL >= 0.9.8" #endif /* Enable the "v2" TLS handshake. Loading Loading @@ -778,13 +778,8 @@ tor_cert_decode(const uint8_t *certificate, size_t certificate_len) if (certificate_len > INT_MAX) return NULL; #if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8) /* This ifdef suppresses a type warning. Take out this case once everybody * is using OpenSSL 0.9.8 or later. */ x509 = d2i_X509(NULL, (unsigned char**)&cp, (int)certificate_len); #else x509 = d2i_X509(NULL, &cp, (int)certificate_len); #endif if (!x509) return NULL; /* Couldn't decode */ if (cp - certificate != (int)certificate_len) { Loading