Changeset 14355


Ignore:
Timestamp:
Dec 4, 2014, 12:13:59 PM (7 years ago)
Author:
mikedld
Message:

#4400, #5462: Move SHA1 helpers to crypto-utils

On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing SHA1 calculation to crypto-utils.{c,h}. OpenSSL-related
functionality (SHA1 context management) is moved to crypto-utils-openssl.c.

Add new tr_sha1_ctx_t type and functions to be implemented by crypto
backends:

  • tr_sha1_init - allocate SHA1 context and and initialize it,
  • tr_sha1_update - hash some [more] data,
  • tr_sha1_final - finish hash calculation and free the context.

Add new files to CMakeLists.txt (leftover from previous commit) to fix
CMake-based configuration.

Location:
trunk/libtransmission
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/CMakeLists.txt

    r14350 r14355  
    1515    ConvertUTF.c
    1616    crypto.c
     17    crypto-utils.c
     18    crypto-utils-openssl.c
    1719    error.c
    1820    fdlimit.c
     
    9294    ConvertUTF.h
    9395    crypto.h
     96    crypto-utils.h
    9497    fdlimit.h
    9598    handshake.h
  • trunk/libtransmission/crypto-test.c

    r14354 r14355  
    100100  uint8_t hash[SHA_DIGEST_LENGTH];
    101101
    102   tr_sha1 (hash, "test", 4, NULL);
     102  check (tr_sha1 (hash, "test", 4, NULL));
    103103  check (memcmp (hash, "\xa9\x4a\x8f\xe5\xcc\xb1\x9b\xa6\x1c\x4c\x08\x73\xd3\x91\xe9\x87\x98\x2f\xbb\xd3", SHA_DIGEST_LENGTH) == 0);
    104104
    105   tr_sha1 (hash, "1", 1, "22", 2, "333", 3, NULL);
     105  check (tr_sha1 (hash, "1", 1, "22", 2, "333", 3, NULL));
    106106  check (memcmp (hash, "\x1f\x74\x64\x8e\x50\xa6\xa6\x70\x8e\xc5\x4a\xb3\x27\xa1\x63\xd5\x53\x6b\x7c\xed", SHA_DIGEST_LENGTH) == 0);
    107107
  • trunk/libtransmission/crypto-utils-openssl.c

    r14354 r14355  
    1111
    1212#include <openssl/err.h>
     13#include <openssl/evp.h>
    1314#include <openssl/rand.h>
    1415
     
    8384***/
    8485
     86tr_sha1_ctx_t
     87tr_sha1_init (void)
     88{
     89  EVP_MD_CTX * handle = EVP_MD_CTX_create ();
     90
     91  if (check_result (EVP_DigestInit_ex (handle, EVP_sha1 (), NULL)))
     92    return handle;
     93
     94  EVP_MD_CTX_destroy (handle);
     95  return NULL;
     96}
     97
     98bool
     99tr_sha1_update (tr_sha1_ctx_t   handle,
     100                const void    * data,
     101                size_t          data_length)
     102{
     103  assert (handle != NULL);
     104  assert (data != NULL);
     105
     106  return check_result (EVP_DigestUpdate (handle, data, data_length));
     107}
     108
     109bool
     110tr_sha1_final (tr_sha1_ctx_t   handle,
     111               uint8_t       * hash)
     112{
     113  bool ret = true;
     114
     115  if (hash != NULL)
     116    {
     117      unsigned int hash_length;
     118
     119      assert (handle != NULL);
     120
     121      ret = check_result (EVP_DigestFinal_ex (handle, hash, &hash_length));
     122
     123      assert (!ret || hash_length == SHA_DIGEST_LENGTH);
     124    }
     125
     126  EVP_MD_CTX_destroy (handle);
     127  return ret;
     128}
     129
     130/***
     131****
     132***/
     133
    85134bool
    86135tr_rand_buffer (void   * buffer,
  • trunk/libtransmission/crypto-utils.c

    r14354 r14355  
    99
    1010#include <assert.h>
     11#include <stdarg.h>
    1112#include <stdlib.h> /* abs (), srand (), rand () */
    1213
     
    1415#include "crypto-utils.h"
    1516#include "utils.h"
     17
     18/***
     19****
     20***/
     21
     22bool
     23tr_sha1 (uint8_t    * hash,
     24         const void * data1,
     25         int          data1_length,
     26                      ...)
     27{
     28  tr_sha1_ctx_t sha;
     29
     30  if ((sha = tr_sha1_init ()) == NULL)
     31    return false;
     32
     33  if (tr_sha1_update (sha, data1, data1_length))
     34    {
     35      va_list vl;
     36      const void * data;
     37
     38      va_start (vl, data1_length);
     39      while ((data = va_arg (vl, const void *)) != NULL)
     40        {
     41          const int data_length = va_arg (vl, int);
     42          assert (data_length >= 0);
     43          if (!tr_sha1_update (sha, data, data_length))
     44            break;
     45        }
     46      va_end (vl);
     47
     48      /* did we reach the end of argument list? */
     49      if (data == NULL)
     50        return tr_sha1_final (sha, hash);
     51    }
     52
     53  tr_sha1_final (sha, NULL);
     54  return false;
     55}
    1656
    1757/***
  • trunk/libtransmission/crypto-utils.h

    r14354 r14355  
    1111#define TR_CRYPTO_UTILS_H
    1212
     13#include <inttypes.h>
    1314#include <stddef.h>
     15
     16#include "utils.h" /* TR_GNUC_NULL_TERMINATED */
    1417
    1518/**
     
    1821**/
    1922
     23 /** @brief Opaque SHA1 context type. */
     24typedef void * tr_sha1_ctx_t;
     25
     26/**
     27 * @brief Generate a SHA1 hash from one or more chunks of memory.
     28 */
     29bool             tr_sha1               (uint8_t        * hash,
     30                                        const void     * data1,
     31                                        int              data1_length,
     32                                                         ...) TR_GNUC_NULL_TERMINATED;
     33
     34/**
     35 * @brief Allocate and initialize new SHA1 hasher context.
     36 */
     37tr_sha1_ctx_t    tr_sha1_init          (void);
     38
     39/**
     40 * @brief Update SHA1 hash.
     41 */
     42bool             tr_sha1_update        (tr_sha1_ctx_t    handle,
     43                                        const void     * data,
     44                                        size_t           data_length);
     45
     46/**
     47 * @brief Finalize and export SHA1 hash, free hasher context.
     48 */
     49bool             tr_sha1_final         (tr_sha1_ctx_t    handle,
     50                                        uint8_t        * hash);
    2051/**
    2152 * @brief Returns a random number in the range of [0...upper_bound).
  • trunk/libtransmission/crypto.c

    r14354 r14355  
    1616#include <openssl/err.h>
    1717#include <openssl/rc4.h>
    18 #include <openssl/sha.h>
    1918
    2019#include "transmission.h"
     
    2524
    2625#define MY_NAME "tr_crypto"
    27 
    28 /**
    29 ***
    30 **/
    31 
    32 void
    33 tr_sha1 (uint8_t * setme, const void * content1, int content1_len, ...)
    34 {
    35   va_list vl;
    36   SHA_CTX sha;
    37   const void * content;
    38 
    39   SHA1_Init (&sha);
    40   SHA1_Update (&sha, content1, content1_len);
    41 
    42   va_start (vl, content1_len);
    43   while ((content = va_arg (vl, const void*)))
    44     SHA1_Update (&sha, content, va_arg (vl, int));
    45   va_end (vl);
    46 
    47   SHA1_Final (setme, &sha);
    48 }
    4926
    5027/**
     
    199176         const char * key)
    200177{
    201   SHA_CTX sha;
    202178  uint8_t buf[SHA_DIGEST_LENGTH];
    203179
     
    205181  assert (crypto->mySecretIsSet);
    206182
    207   if (SHA1_Init (&sha)
    208       && SHA1_Update (&sha, key, 4)
    209       && SHA1_Update (&sha, crypto->mySecret, KEY_LEN)
    210       && SHA1_Update (&sha, crypto->torrentHash, SHA_DIGEST_LENGTH)
    211       && SHA1_Final (buf, &sha))
    212     {
    213       RC4_set_key (setme, SHA_DIGEST_LENGTH, buf);
    214     }
    215   else
    216     {
    217       logErrorFromSSL ();
    218     }
     183  if (tr_sha1 (buf,
     184               key, 4,
     185               crypto->mySecret, KEY_LEN,
     186               crypto->torrentHash, SHA_DIGEST_LENGTH,
     187               NULL))
     188    RC4_set_key (setme, SHA_DIGEST_LENGTH, buf);
    219189}
    220190
  • trunk/libtransmission/crypto.h

    r14354 r14355  
    8787**/
    8888
    89 
    90 /** @brief generate a SHA1 hash from one or more chunks of memory */
    91 void tr_sha1 (uint8_t    * setme,
    92               const void * content1,
    93               int          content1_len,
    94               ...) TR_GNUC_NULL_TERMINATED;
    95 
    96 
    9789/** @brief generate a SSHA password from its plaintext source */
    9890char*  tr_ssha1 (const void * plaintext);
  • trunk/libtransmission/inout.c

    r14327 r14355  
    1313#include <string.h> /* memcmp () */
    1414
    15 #include <openssl/sha.h>
    16 
    1715#include "transmission.h"
    1816#include "cache.h" /* tr_cacheReadBlock () */
     17#include "crypto-utils.h"
    1918#include "error.h"
    2019#include "fdlimit.h"
     
    279278  const size_t buflen = tor->blockSize;
    280279  void * buffer = tr_valloc (buflen);
    281   SHA_CTX sha;
     280  tr_sha1_ctx_t sha;
    282281
    283282  assert (tor != NULL);
     
    287286  assert (setme != NULL);
    288287
    289   SHA1_Init (&sha);
     288  sha = tr_sha1_init ();
    290289  bytesLeft = tr_torPieceCountBytes (tor, pieceIndex);
    291290
     
    298297      if (!success)
    299298        break;
    300       SHA1_Update (&sha, buffer, len);
     299      tr_sha1_update (sha, buffer, len);
    301300      offset += len;
    302301      bytesLeft -= len;
    303302    }
    304303
    305   if (success)
    306     SHA1_Final (setme, &sha);
     304  tr_sha1_final (sha, success ? setme : NULL);
    307305
    308306  tr_free (buffer);
  • trunk/libtransmission/makemeta.c

    r14331 r14355  
    1616
    1717#include "transmission.h"
    18 #include "crypto.h" /* tr_sha1 */
     18#include "crypto-utils.h" /* tr_sha1 */
    1919#include "error.h"
    2020#include "file.h"
  • trunk/libtransmission/metainfo.c

    r14320 r14355  
    1414
    1515#include "transmission.h"
    16 #include "crypto.h" /* tr_sha1 */
     16#include "crypto-utils.h" /* tr_sha1 */
    1717#include "file.h"
    1818#include "log.h"
  • trunk/libtransmission/torrent-magnet.c

    r14327 r14355  
    1414
    1515#include "transmission.h"
    16 #include "crypto.h" /* tr_sha1 () */
     16#include "crypto-utils.h" /* tr_sha1 () */
    1717#include "file.h"
    1818#include "log.h"
  • trunk/libtransmission/torrent.c

    r14354 r14355  
    2020#include <math.h>
    2121#include <stdarg.h>
     22#include <stdio.h> /* remove () */
    2223#include <string.h> /* memcmp */
    2324#include <stdlib.h> /* qsort */
     
    3132#include "cache.h"
    3233#include "completion.h"
    33 #include "crypto.h" /* for tr_sha1 */
    34 #include "crypto-utils.h"
     34#include "crypto-utils.h" /* for tr_sha1 */
    3535#include "error.h"
    3636#include "fdlimit.h" /* tr_fdTorrentClose */
  • trunk/libtransmission/tr-dht.c

    r14354 r14355  
    5050/* libT */
    5151#include "transmission.h"
    52 #include "crypto.h"
    5352#include "crypto-utils.h"
    5453#include "file.h"
  • trunk/libtransmission/verify.c

    r14347 r14355  
    1616#endif
    1717
    18 #include <openssl/sha.h>
    19 
    2018#include "transmission.h"
    2119#include "completion.h"
     20#include "crypto-utils.h"
    2221#include "file.h"
    2322#include "list.h"
     
    4140{
    4241  time_t end;
    43   SHA_CTX sha;
     42  tr_sha1_ctx_t sha;
    4443  tr_sys_file_t fd = TR_BAD_SYS_FILE;
    4544  uint64_t filePos = 0;
     
    5554  uint8_t * buffer = tr_valloc (buflen);
    5655
    57   SHA1_Init (&sha);
     56  sha = tr_sha1_init ();
    5857
    5958  tr_logAddTorDbg (tor, "%s", "verifying torrent...");
     
    9392            {
    9493              bytesThisPass = numRead;
    95               SHA1_Update (&sha, buffer, bytesThisPass);
     94              tr_sha1_update (sha, buffer, bytesThisPass);
    9695#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
    9796              posix_fadvise (fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED);
     
    113112          uint8_t hash[SHA_DIGEST_LENGTH];
    114113
    115           SHA1_Final (hash, &sha);
     114          tr_sha1_final (sha, hash);
    116115          hasPiece = !memcmp (hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH);
    117116
     
    134133            }
    135134
    136           SHA1_Init (&sha);
     135          sha = tr_sha1_init ();
    137136          pieceIndex++;
    138137          piecePos = 0;
     
    155154  if (fd != TR_BAD_SYS_FILE)
    156155    tr_sys_file_close (fd, NULL);
     156  tr_sha1_final (sha, NULL);
    157157  free (buffer);
    158158
Note: See TracChangeset for help on using the changeset viewer.