[tor-commits] [obfsproxy/master] Move obfs2_crypt.[ch] to top level and rename them crypt.[ch].

nickm at torproject.org nickm at torproject.org
Thu Jul 14 16:28:32 UTC 2011


commit 2032347fd043f41d1972bfeb0306b44a5122c148
Author: Zack Weinberg <zackw at panix.com>
Date:   Fri Jul 8 16:17:59 2011 -0700

    Move obfs2_crypt.[ch] to top level and rename them crypt.[ch].
---
 Makefile.am                 |   20 ++--
 src/crypt.c                 |  197 +++++++++++++++++++++++++++++++++++++++++
 src/crypt.h                 |   59 +++++++++++++
 src/protocols/obfs2.c       |    8 +-
 src/protocols/obfs2_crypt.c |  203 -------------------------------------------
 src/protocols/obfs2_crypt.h |   61 -------------
 src/test/unittest.c         |    2 +-
 src/test/unittest_crypt.c   |    2 +-
 src/test/unittest_obfs2.c   |    2 +-
 src/test/unittest_socks.c   |    2 +-
 10 files changed, 274 insertions(+), 282 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index eb32bdc..096c0fa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -8,13 +8,13 @@ noinst_LIBRARIES = libobfsproxy.a
 noinst_PROGRAMS = unittests
 
 libobfsproxy_a_SOURCES = \
+	src/crypt.c \
 	src/network.c \
 	src/protocol.c \
-	src/socks.c  \
-	src/util.c   \
-	src/protocols/obfs2.c \
-	src/protocols/obfs2_crypt.c \
-	src/protocols/dummy.c
+	src/socks.c \
+	src/util.c \
+	src/protocols/dummy.c \
+	src/protocols/obfs2.c
 
 obfsproxy_SOURCES = \
 	src/main.c
@@ -22,20 +22,20 @@ obfsproxy_SOURCES = \
 unittests_SOURCES = \
 	src/test/tinytest.c \
 	src/test/unittest.c \
-	src/test/unittest_obfs2.c \
 	src/test/unittest_crypt.c \
+	src/test/unittest_obfs2.c \
 	src/test/unittest_socks.c
 
 noinst_HEADERS = \
+	src/crypt.h \
 	src/network.h \
 	src/protocol.h \
 	src/socks.h \
 	src/util.h \
-	src/test/tinytest.h \
-	src/test/tinytest_macros.h \
+	src/protocols/dummy.h \
 	src/protocols/obfs2.h \
-	src/protocols/obfs2_crypt.h \
-	src/protocols/dummy.h
+	src/test/tinytest.h \
+	src/test/tinytest_macros.h
 
 EXTRA_DIST = doc/protocol-spec.txt src/sha256.c
 
diff --git a/src/crypt.c b/src/crypt.c
new file mode 100644
index 0000000..6c35f10
--- /dev/null
+++ b/src/crypt.c
@@ -0,0 +1,197 @@
+/* Copyright 2011 Nick Mathewson, George Kadianakis
+   See LICENSE for other credits and copying information
+*/
+
+#include "config.h"
+
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdint.h>
+
+#include <openssl/opensslv.h>
+#include <openssl/aes.h>
+#include <openssl/rand.h>
+#include <openssl/err.h>
+
+#define CRYPT_PRIVATE
+#include "crypt.h"
+
+#if OPENSSL_VERSION_NUMBER >= 0x0090800f
+#define USE_OPENSSL_RANDPOLL 1
+#define USE_OPENSSL_SHA256 1
+#include <openssl/sha.h>
+#else
+#define STMT_BEGIN do {
+#define STMT_END } while (0)
+static void
+set_uint32(void *ptr, uint32_t val)
+{
+  memcpy(ptr, &val, 4);
+}
+static uint32_t
+get_uint32(const void *ptr)
+{
+  uint32_t val;
+  memcpy(&val, ptr, 4);
+  return val;
+}
+#define LTC_ARGCHK(x) assert((x))
+#include "../sha256.c"
+#endif
+
+int
+initialize_crypto(void)
+{
+  ERR_load_crypto_strings();
+
+#ifdef USE_OPENSSL_RANDPOLL
+  return RAND_poll() == 1 ? 0 : -1;
+#else
+  /* XXX Or maybe fall back to the arc4random implementation in libevent2? */
+  {
+    char buf[32];
+    int fd, n;
+    fd = open("/dev/urandom", O_RDONLY);
+    if (fd == -1) {
+      perror("open");
+      return -1;
+    }
+    n = read(fd, buf, sizeof(buf));
+    if (n != sizeof(buf)) {
+      close(fd);
+      return -1;
+    }
+    RAND_seed(buf, sizeof(buf));
+    close(fd);
+    return 0;
+  }
+#endif
+}
+
+void
+cleanup_crypto(void)
+{
+  ERR_free_strings();
+}
+
+/* =====
+   Digests
+   ===== */
+
+#ifdef USE_OPENSSL_SHA256
+struct digest_t {
+  SHA256_CTX ctx;
+};
+digest_t *
+digest_new(void)
+{
+  digest_t *d = malloc(sizeof(digest_t));
+  SHA256_Init(&d->ctx);
+  return d;
+}
+void
+digest_update(digest_t *d, const uchar *buf, size_t len)
+{
+  SHA256_Update(&d->ctx, buf, len);
+}
+size_t
+digest_getdigest(digest_t *d, uchar *buf, size_t len)
+{
+  uchar tmp[SHA256_LENGTH];
+  int n = 32;
+  SHA256_Final(tmp, &d->ctx);
+  if (len < 32)
+    n = len;
+  memcpy(buf, tmp, n);
+  memset(tmp, 0, sizeof(tmp));
+  return n;
+}
+#else
+struct digest_t {
+  sha256_state ctx;
+};
+digest_t *
+digest_new(void)
+{
+  digest_t *d = malloc(sizeof(digest_t));
+  sha256_init(&d->ctx);
+  return d;
+}
+void
+digest_update(digest_t *d, const uchar *buf, size_t len)
+{
+  sha256_process(&d->ctx, buf, len);
+}
+size_t
+digest_getdigest(digest_t *d, uchar *buf, size_t len)
+{
+  uchar tmp[SHA256_LENGTH];
+  int n = 32;
+  sha256_done(&d->ctx, tmp);
+  if (len < 32)
+    n = len;
+  memcpy(buf, tmp, n);
+  memset(tmp, 0, sizeof(tmp));
+  return n;
+}
+#endif
+
+void
+digest_free(digest_t *d)
+{
+  memset(d, 0, sizeof(digest_t));
+  free(d);
+}
+
+/* =====
+   Stream crypto
+   ===== */
+
+crypt_t *
+crypt_new(const uchar *key, size_t keylen)
+{
+  crypt_t *k;
+  if (keylen < AES_BLOCK_SIZE)
+    return NULL;
+
+  k = calloc(1, sizeof(crypt_t));
+  if (k == NULL)
+    return NULL;
+
+  AES_set_encrypt_key(key, 128, &k->key);
+
+  return k;
+}
+void
+crypt_set_iv(crypt_t *key, const uchar *iv, size_t ivlen)
+{
+  assert(ivlen == sizeof(key->ivec));
+  memcpy(key->ivec, iv, ivlen);
+}
+void
+stream_crypt(crypt_t *key, uchar *buf, size_t len)
+{
+  AES_ctr128_encrypt(buf, buf, /* XXX make sure this is okay to do. */
+                     len,
+                     &key->key, key->ivec, key->ecount_buf,
+                     &key->pos);
+}
+void
+crypt_free(crypt_t *key)
+{
+  memset(key, 0, sizeof(key));
+  free(key);
+}
+
+/* =====
+   PRNG
+   ===== */
+
+int
+random_bytes(uchar *buf, size_t buflen)
+{
+  return RAND_bytes(buf, buflen) == 1 ? 0 : -1;
+}
diff --git a/src/crypt.h b/src/crypt.h
new file mode 100644
index 0000000..5681a4c
--- /dev/null
+++ b/src/crypt.h
@@ -0,0 +1,59 @@
+/* Copyright 2011 Nick Mathewson, George Kadianakis
+   See LICENSE for other credits and copying information
+*/
+
+#ifndef CRYPT_H
+#define CRYPT_H
+
+#define SHA256_LENGTH 32
+
+/* Stream cipher state */
+typedef struct crypt_t crypt_t;
+/* Digest state */
+typedef struct digest_t digest_t;
+
+typedef unsigned char uchar;
+
+/** Initialize global crypto state.  Returrn 0 on success, -1 on failure */
+int initialize_crypto(void);
+/** Clean up global crypto state */
+void cleanup_crypto(void);
+
+/** Return a newly allocated digest state, or NULL on failure. */
+digest_t *digest_new(void);
+/** Add n bytes from b to the digest state. */
+void digest_update(digest_t *, const uchar *b, size_t n);
+/** Get a digest from the digest state.  Put it in up the first n bytes of the
+buffer b.  Return the number of bytes actually written.*/
+size_t digest_getdigest(digest_t *, uchar *b, size_t n);
+/** Clear and free a digest state */
+void digest_free(digest_t *);
+
+/** Return a new stream cipher state taking key and IV from the data provided.
+ * The data length must be exactly 32 */
+crypt_t *crypt_new(const uchar *, size_t);
+void crypt_set_iv(crypt_t *key, const uchar *iv, size_t ivlen);
+
+/** Encrypt n bytes of data in the buffer b, in place. */
+void stream_crypt(crypt_t *, uchar *b, size_t n);
+/** Clear and free a stream cipher state. */
+void crypt_free(crypt_t *);
+
+/** Set b to contain n random bytes. */
+int random_bytes(uchar *b, size_t n);
+
+#ifdef CRYPT_PRIVATE
+/* ==========
+   These definitions are not part of the crypt interface.
+   They're exposed here so that the unit tests can use them.
+   ==========
+*/
+struct crypt_t {
+  AES_KEY key;
+  uchar ivec[AES_BLOCK_SIZE];
+  uchar ecount_buf[AES_BLOCK_SIZE];
+  unsigned int pos;
+};
+#endif
+
+#endif
diff --git a/src/protocols/obfs2.c b/src/protocols/obfs2.c
index 57a695f..d297c16 100644
--- a/src/protocols/obfs2.c
+++ b/src/protocols/obfs2.c
@@ -10,15 +10,15 @@
 #include <openssl/rand.h>
 #include <event2/buffer.h>
 
-#define CRYPT_PROTOCOL_PRIVATE
-
-#include "obfs2_crypt.h"
-#include "obfs2.h"
+#include "../crypt.h"
 #include "../network.h"
 #include "../util.h"
 #include "../protocol.h"
 #include "../network.h"
 
+#define CRYPT_PROTOCOL_PRIVATE
+#include "obfs2.h"
+
 static void obfs2_state_free(void *state);
 static int obfs2_send_initial_message(void *state, struct evbuffer *buf);
 static int obfs2_send(void *state,
diff --git a/src/protocols/obfs2_crypt.c b/src/protocols/obfs2_crypt.c
deleted file mode 100644
index 681fab6..0000000
--- a/src/protocols/obfs2_crypt.c
+++ /dev/null
@@ -1,203 +0,0 @@
-/* Copyright 2011 Nick Mathewson, George Kadianakis
-   See LICENSE for other credits and copying information
-*/
-
-#include "config.h"
-
-#include <assert.h>
-#include <string.h>
-#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-#ifdef HAVE_STDINT_H
-#include <stdint.h>
-#endif
-
-#include <openssl/opensslv.h>
-#include <openssl/aes.h>
-#include <openssl/rand.h>
-#include <openssl/err.h>
-
-#define CRYPT_PRIVATE
-#include "obfs2_crypt.h"
-
-#if OPENSSL_VERSION_NUMBER >= 0x0090800f
-#define USE_OPENSSL_RANDPOLL 1
-#define USE_OPENSSL_SHA256 1
-#include <openssl/sha.h>
-#else
-#define STMT_BEGIN do {
-#define STMT_END } while (0)
-static void
-set_uint32(void *ptr, uint32_t val)
-{
-  memcpy(ptr, &val, 4);
-}
-static uint32_t
-get_uint32(const void *ptr)
-{
-  uint32_t val;
-  memcpy(&val, ptr, 4);
-  return val;
-}
-#define LTC_ARGCHK(x) assert((x))
-#include "../sha256.c"
-#endif
-
-int
-initialize_crypto(void)
-{
-  ERR_load_crypto_strings();
-
-#ifdef USE_OPENSSL_RANDPOLL
-  return RAND_poll() == 1 ? 0 : -1;
-#else
-  /* XXX Or maybe fall back to the arc4random implementation in libevent2? */
-  {
-    char buf[32];
-    int fd, n;
-    fd = open("/dev/urandom", O_RDONLY);
-    if (fd == -1) {
-      perror("open");
-      return -1;
-    }
-    n = read(fd, buf, sizeof(buf));
-    if (n != sizeof(buf)) {
-      close(fd);
-      return -1;
-    }
-    RAND_seed(buf, sizeof(buf));
-    close(fd);
-    return 0;
-  }
-#endif
-}
-
-void
-cleanup_crypto(void)
-{
-  ERR_free_strings();
-}
-
-/* =====
-   Digests
-   ===== */
-
-#ifdef USE_OPENSSL_SHA256
-struct digest_t {
-  SHA256_CTX ctx;
-};
-digest_t *
-digest_new(void)
-{
-  digest_t *d = malloc(sizeof(digest_t));
-  SHA256_Init(&d->ctx);
-  return d;
-}
-void
-digest_update(digest_t *d, const uchar *buf, size_t len)
-{
-  SHA256_Update(&d->ctx, buf, len);
-}
-size_t
-digest_getdigest(digest_t *d, uchar *buf, size_t len)
-{
-  uchar tmp[SHA256_LENGTH];
-  int n = 32;
-  SHA256_Final(tmp, &d->ctx);
-  if (len < 32)
-    n = len;
-  memcpy(buf, tmp, n);
-  memset(tmp, 0, sizeof(tmp));
-  return n;
-}
-#else
-struct digest_t {
-  sha256_state ctx;
-};
-digest_t *
-digest_new(void)
-{
-  digest_t *d = malloc(sizeof(digest_t));
-  sha256_init(&d->ctx);
-  return d;
-}
-void
-digest_update(digest_t *d, const uchar *buf, size_t len)
-{
-  sha256_process(&d->ctx, buf, len);
-}
-size_t
-digest_getdigest(digest_t *d, uchar *buf, size_t len)
-{
-  uchar tmp[SHA256_LENGTH];
-  int n = 32;
-  sha256_done(&d->ctx, tmp);
-  if (len < 32)
-    n = len;
-  memcpy(buf, tmp, n);
-  memset(tmp, 0, sizeof(tmp));
-  return n;
-}
-#endif
-
-void
-digest_free(digest_t *d)
-{
-  memset(d, 0, sizeof(digest_t));
-  free(d);
-}
-
-/* =====
-   Stream crypto
-   ===== */
-
-crypt_t *
-crypt_new(const uchar *key, size_t keylen)
-{
-  crypt_t *k;
-  if (keylen < AES_BLOCK_SIZE)
-    return NULL;
-
-  k = calloc(1, sizeof(crypt_t));
-  if (k == NULL)
-    return NULL;
-
-  AES_set_encrypt_key(key, 128, &k->key);
-
-  return k;
-}
-void
-crypt_set_iv(crypt_t *key, const uchar *iv, size_t ivlen)
-{
-  assert(ivlen == sizeof(key->ivec));
-  memcpy(key->ivec, iv, ivlen);
-}
-void
-stream_crypt(crypt_t *key, uchar *buf, size_t len)
-{
-  AES_ctr128_encrypt(buf, buf, /* XXX make sure this is okay to do. */
-                     len,
-                     &key->key, key->ivec, key->ecount_buf,
-                     &key->pos);
-}
-void
-crypt_free(crypt_t *key)
-{
-  memset(key, 0, sizeof(key));
-  free(key);
-}
-
-/* =====
-   PRNG
-   ===== */
-
-int
-random_bytes(uchar *buf, size_t buflen)
-{
-  return RAND_bytes(buf, buflen) == 1 ? 0 : -1;
-}
diff --git a/src/protocols/obfs2_crypt.h b/src/protocols/obfs2_crypt.h
deleted file mode 100644
index b449047..0000000
--- a/src/protocols/obfs2_crypt.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/* Copyright 2011 Nick Mathewson, George Kadianakis
-   See LICENSE for other credits and copying information
-*/
-
-#ifndef OBFS2_CRYPT_H
-#define OBFS2_CRYPT_H
-
-#include <sys/types.h>
-
-#define SHA256_LENGTH 32
-
-/* Stream cipher state */
-typedef struct crypt_t crypt_t;
-/* Digest state */
-typedef struct digest_t digest_t;
-
-typedef unsigned char uchar;
-
-/** Initialize global crypto state.  Returrn 0 on success, -1 on failure */
-int initialize_crypto(void);
-/** Clean up global crypto state */
-void cleanup_crypto(void);
-
-/** Return a newly allocated digest state, or NULL on failure. */
-digest_t *digest_new(void);
-/** Add n bytes from b to the digest state. */
-void digest_update(digest_t *, const uchar *b, size_t n);
-/** Get a digest from the digest state.  Put it in up the first n bytes of the
-buffer b.  Return the number of bytes actually written.*/
-size_t digest_getdigest(digest_t *, uchar *b, size_t n);
-/** Clear and free a digest state */
-void digest_free(digest_t *);
-
-/** Return a new stream cipher state taking key and IV from the data provided.
- * The data length must be exactly 32 */
-crypt_t *crypt_new(const uchar *, size_t);
-void crypt_set_iv(crypt_t *key, const uchar *iv, size_t ivlen);
-
-/** Encrypt n bytes of data in the buffer b, in place. */
-void stream_crypt(crypt_t *, uchar *b, size_t n);
-/** Clear and free a stream cipher state. */
-void crypt_free(crypt_t *);
-
-/** Set b to contain n random bytes. */
-int random_bytes(uchar *b, size_t n);
-
-#ifdef CRYPT_PRIVATE
-/* ==========
-   These definitions are not part of the crypt interface.
-   They're exposed here so that the unit tests can use them.
-   ==========
-*/
-struct crypt_t {
-  AES_KEY key;
-  uchar ivec[AES_BLOCK_SIZE];
-  uchar ecount_buf[AES_BLOCK_SIZE];
-  unsigned int pos;
-};
-#endif
-
-#endif
diff --git a/src/test/unittest.c b/src/test/unittest.c
index 8ff3747..184a192 100644
--- a/src/test/unittest.c
+++ b/src/test/unittest.c
@@ -4,7 +4,7 @@
 #include <stdlib.h>
 
 #include "tinytest.h"
-#include "../protocols/obfs2_crypt.h"
+#include "../crypt.h"
 
 extern struct testcase_t crypt_tests[];
 extern struct testcase_t protocol_tests[];
diff --git a/src/test/unittest_crypt.c b/src/test/unittest_crypt.c
index b50d65d..a65f07b 100644
--- a/src/test/unittest_crypt.c
+++ b/src/test/unittest_crypt.c
@@ -9,7 +9,7 @@
 
 #include <openssl/aes.h>
 
-#include "../protocols/obfs2_crypt.h"
+#include "../crypt.h"
 
 struct crypt_t {
   AES_KEY key;
diff --git a/src/test/unittest_obfs2.c b/src/test/unittest_obfs2.c
index af31adc..ec5cc47 100644
--- a/src/test/unittest_obfs2.c
+++ b/src/test/unittest_obfs2.c
@@ -14,7 +14,7 @@
 
 #define CRYPT_PROTOCOL_PRIVATE
 #define CRYPT_PRIVATE
-#include "../protocols/obfs2_crypt.h"
+#include "../crypt.h"
 #include "../util.h"
 #include "../protocol.h"
 #include "../protocols/obfs2.h"
diff --git a/src/test/unittest_socks.c b/src/test/unittest_socks.c
index c77e3dc..1719f8e 100644
--- a/src/test/unittest_socks.c
+++ b/src/test/unittest_socks.c
@@ -12,7 +12,7 @@
 
 #define SOCKS_PRIVATE
 #include "../socks.h"
-#include "../protocols/obfs2_crypt.h"
+#include "../crypt.h"
 #include "../util.h"
 #include "../protocols/obfs2.h"
 





More information about the tor-commits mailing list