[or-cvs] Add convenience functions to wrap create and init for symme...

Nick Mathewson nickm at seul.org
Wed Oct 2 20:39:53 UTC 2002


Update of /home/or/cvsroot/src/common
In directory moria.seul.org:/tmp/cvs-serv15578/src/common

Modified Files:
	crypto.c crypto.h 
Log Message:
Add convenience functions to wrap create and init for symmetric ciphers; clean up error handling in onion.c

Index: crypto.c
===================================================================
RCS file: /home/or/cvsroot/src/common/crypto.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- crypto.c	27 Sep 2002 22:13:22 -0000	1.8
+++ crypto.c	2 Oct 2002 20:39:51 -0000	1.9
@@ -98,6 +98,49 @@
   return;
 }
 
+
+/* Create a new crypto_cipher_env_t for a given onion cipher type, key,
+ * iv, and encryption flag (1=encrypt, 0=decrypt).  Return the crypto object
+ * on success; NULL on failure.
+ */
+crypto_cipher_env_t *
+crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode) 
+{
+  int r;
+  crypto_cipher_env_t *crypto = NULL;
+
+  if (! (crypto = crypto_new_cipher_env(cipher_type))) {
+    log(LOG_ERR, "Unable to allocate crypto object");
+    return NULL;
+  }
+
+  if (crypto_cipher_set_key(crypto, key)) {
+    log(LOG_ERR, "Unable to set key: %s", crypto_perror());
+    goto error;
+  }
+
+  if (crypto_cipher_set_iv(crypto, iv)) {
+    log(LOG_ERR, "Unable to set iv: %s", crypto_perror());
+    goto error;
+  }
+  
+  if (encrypt_mode)
+    r = crypto_cipher_encrypt_init_cipher(crypto);
+  else
+    r = crypto_cipher_decrypt_init_cipher(crypto);
+
+  if (r) {
+    log(LOG_ERR, "Unabble to initialize cipher: %s", crypto_perror());
+    goto error;
+  }
+  return crypto;
+
+ error:
+  if (crypto)
+    crypto_free_cipher_env(crypto);
+  return NULL;
+}
+
 crypto_cipher_env_t *crypto_new_cipher_env(int type)
 {
   crypto_cipher_env_t *env;
@@ -650,3 +693,4 @@
 {
   return (char *)ERR_reason_error_string(ERR_get_error());
 }
+

Index: crypto.h
===================================================================
RCS file: /home/or/cvsroot/src/common/crypto.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- crypto.h	24 Sep 2002 10:43:54 -0000	1.4
+++ crypto.h	2 Oct 2002 20:39:51 -0000	1.5
@@ -74,6 +74,9 @@
 int crypto_cipher_encrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to);
 int crypto_cipher_decrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to);
 
+/* convenience function: wraps crypto_create_crypto_env, set_key, set_iv, and init. */
+crypto_cipher_env_t *crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode);
+
 /* SHA-1 */
 int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest);
 



More information about the tor-commits mailing list