[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r10633: refined documentation, checked spaces (tor/branches/114-dist-storage/src/common)



Author: kloesing
Date: 2007-06-17 08:40:29 -0400 (Sun, 17 Jun 2007)
New Revision: 10633

Modified:
   tor/branches/114-dist-storage/src/common/crypto.c
   tor/branches/114-dist-storage/src/common/crypto.h
Log:
refined documentation, checked spaces

Modified: tor/branches/114-dist-storage/src/common/crypto.c
===================================================================
--- tor/branches/114-dist-storage/src/common/crypto.c	2007-06-17 11:07:22 UTC (rev 10632)
+++ tor/branches/114-dist-storage/src/common/crypto.c	2007-06-17 12:40:29 UTC (rev 10633)
@@ -1148,46 +1148,44 @@
  * <b>key</b> of 16 bytes length to <b>to</b>. The length of <b>to</b> needs to
  * be the length of <b>from</b> plus 32 (up to 16 bytes for padding and exactly
  * 16 bytes for the initialization vector). On success, return the number of
- * bytes written, on failure, return -1  
+ * bytes written, on failure, return -1.
  */
 int
 crypto_cipher_encrypt_cbc(char *key, char *to, const char *from,
                           size_t fromlen)
 {
-  
-  int outlen, tmplen;
-  unsigned char iv[16];
-  EVP_CIPHER_CTX ctx;
 
+  EVP_CIPHER_CTX ctx; /* cipher context */
+  unsigned char iv[16]; /* initialization vector */
+  int outlen, tmplen; /* lengths of encrypted strings (w/ and wo/ final data) */
+
   tor_assert(key);
   tor_assert(to);
   tor_assert(from);
   tor_assert(fromlen);
 
+  /* initialize cipher contex */
+  EVP_CIPHER_CTX_init(&ctx);
+
   /* generate random initialization vector and write it to the first 16 bytes
    * of the result*/
   crypto_rand((char *)iv, 16);
-
-  /* copy initialization vector to result */
   memcpy((unsigned char *)to, iv, 16);
 
-  /* initialize cipher contex */
-  EVP_CIPHER_CTX_init(&ctx);
-
   /* set up cipher context for encryption with cipher type AES-128 in CBC mode,
    * default implementation, given key, and initialization vector */
   EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, (unsigned char *)key, iv);
 
   /* encrypt fromlen bytes from buffer from and write the encrypted version to
    * buffer to */
-  if(!EVP_EncryptUpdate(&ctx, ((unsigned char *)to) + 16, &outlen,
+  if (!EVP_EncryptUpdate(&ctx, ((unsigned char *)to) + 16, &outlen,
                         (const unsigned char *)from, (int)fromlen)) {
     crypto_log_errors(LOG_WARN, "encrypting");
     return -1;
   }
 
   /* encrypt the final data */
-  if(!EVP_EncryptFinal_ex(&ctx, ((unsigned char *)to) + 16 + outlen, &tmplen)) {
+  if (!EVP_EncryptFinal_ex(&ctx, ((unsigned char *)to)+16+outlen, &tmplen)) {
     crypto_log_errors(LOG_WARN, "encrypting the final data");
     return -1;
   }
@@ -1205,43 +1203,42 @@
  * <b>key</b> of 16 bytes length to <b>to</b>. The length of <b>to</b> may be
  * the length of <b>from</b> minus 16 (up to 16 bytes for padding and exactly
  * 16 bytes for the initialization vector). On success, return the number of
- * bytes written, on failure, return -1  
+ * bytes written, on failure, return -1.
  */
 int
 crypto_cipher_decrypt_cbc(char *key, char *to, const char *from,
                           size_t fromlen)
 {
-  
-  int outlen, tmplen;
-  unsigned char iv[16];
-  EVP_CIPHER_CTX ctx;
+  EVP_CIPHER_CTX ctx; /* cipher context */
+  unsigned char iv[16]; /* initialization vector */
+  int outlen, tmplen; /* lengths of decrypted strings (w/ and wo/ final data) */
 
   tor_assert(key);
   tor_assert(to);
   tor_assert(from);
   tor_assert(fromlen);
 
-  /* copy initialization vector from buffer */
-  memcpy(iv, (unsigned const char *)from, 16);
-
   /* initialize cipher contex */
   EVP_CIPHER_CTX_init(&ctx);
 
+  /* copy initialization vector from buffer */
+  memcpy(iv, (unsigned const char *)from, 16);
+
   /* set up cipher context for decryption with cipher type AES-128 in CBC mode,
    * default implementation, given key, and initialization vector */
   EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, (unsigned char *)key, iv);
 
   /* decrypt fromlen-16 bytes from buffer from and write the decrypted version
    * to buffer to */
-  if(!EVP_DecryptUpdate(&ctx, (unsigned char *)to, &outlen,
+  if (!EVP_DecryptUpdate(&ctx, (unsigned char *)to, &outlen,
                         ((const unsigned char *)from) + 16,
                         (int)fromlen - 16)) {
     crypto_log_errors(LOG_WARN, "decrypting");
     return -1;
   }
 
-  /* encrypt the final data */
-  if(!EVP_DecryptFinal_ex(&ctx, ((unsigned char *)to) + outlen, &tmplen)) {
+  /* decrypt the final data */
+  if (!EVP_DecryptFinal_ex(&ctx, ((unsigned char *)to) + outlen, &tmplen)) {
     crypto_log_errors(LOG_WARN, "decrypting the final data");
     return -1;
   }
@@ -1923,14 +1920,12 @@
   unsigned int nbits, i, j, bit;
   char *tmp;
   nbits = srclen * 5;
-  
-  //log_warn(LD_DIR, "srclen is %d, nbits is %d", srclen, nbits); 
-  
+
   tor_assert((nbits%8) == 0); /* We need an even multiple of 8 bits. */
   tor_assert((nbits/8) <= destlen); /* We need enough space. */
   tor_assert(destlen < SIZE_T_CEILING);
-  
-  /* convert base32 encoded chars by the 5-bit values that they represent */
+
+  /* convert base32 encoded chars to the 5-bit values that they represent */
   tmp = tor_malloc_zero(srclen);
   for (j = 0; j < srclen; ++j) {
     if (src[j] > 0x60 && src[j] < 0x7B) tmp[j] = src[j] - 0x61;
@@ -1940,12 +1935,12 @@
       return -1;
     }
   }
-  
+
   /* assemble result byte-wise by applying the five possible cases */
   for (i = 0, bit = 0; bit < nbits; ++i, bit += 8) {
     switch (bit % 40) {
     case 0:
-      dest[i] = (((uint8_t)tmp[(bit/5)]) << 3) + 
+      dest[i] = (((uint8_t)tmp[(bit/5)]) << 3) +
                 (((uint8_t)tmp[(bit/5)+1]) >> 2);
       break;
     case 8:
@@ -1968,7 +1963,7 @@
       break;
     }
   }
-  
+
   tor_free(tmp);
   tmp = NULL;
   return 0;

Modified: tor/branches/114-dist-storage/src/common/crypto.h
===================================================================
--- tor/branches/114-dist-storage/src/common/crypto.h	2007-06-17 11:07:22 UTC (rev 10632)
+++ tor/branches/114-dist-storage/src/common/crypto.h	2007-06-17 12:40:29 UTC (rev 10633)
@@ -123,7 +123,7 @@
                           const char *from, size_t fromlen);
 int crypto_cipher_decrypt(crypto_cipher_env_t *env, char *to,
                           const char *from, size_t fromlen);
-                          
+
 int crypto_cipher_encrypt_cbc(char *key, char *to, const char *from,
                               size_t fromlen);
 int crypto_cipher_decrypt_cbc(char *key, char *to, const char *from,