Changeset 14360


Ignore:
Timestamp:
Dec 4, 2014, 8:45:18 PM (8 years ago)
Author:
mikedld
Message:

#4400, #5462: Move SSHA1 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 SSHA1 password generation and checking to crypto-utils.{c,h}.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/crypto-utils.c

    r14359 r14360  
    1111#include <stdarg.h>
    1212#include <stdlib.h> /* abs (), srand (), rand () */
    13 #include <string.h> /* memmove (), memset (), strlen () */
     13#include <string.h> /* memcpy (), memmove (), memset (), strcmp (), strlen () */
    1414
    1515#include "transmission.h"
     
    121121***/
    122122
     123char *
     124tr_ssha1 (const char * plain_text)
     125{
     126  enum { saltval_len = 8,
     127         salter_len  = 64 };
     128  static const char * salter = "0123456789"
     129                               "abcdefghijklmnopqrstuvwxyz"
     130                               "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     131                               "./";
     132
     133  size_t i;
     134  unsigned char salt[saltval_len];
     135  uint8_t sha[SHA_DIGEST_LENGTH];
     136  char buf[2 * SHA_DIGEST_LENGTH + saltval_len + 2];
     137
     138  tr_rand_buffer (salt, saltval_len);
     139  for (i = 0; i < saltval_len; ++i)
     140    salt[i] = salter[salt[i] % salter_len];
     141
     142  tr_sha1 (sha, plain_text, strlen (plain_text), salt, (size_t) saltval_len, NULL);
     143  tr_sha1_to_hex (&buf[1], sha);
     144  memcpy (&buf[1 + 2 * SHA_DIGEST_LENGTH], &salt, saltval_len);
     145  buf[1 + 2 * SHA_DIGEST_LENGTH + saltval_len] = '\0';
     146  buf[0] = '{'; /* signal that this is a hash. this makes saving/restoring easier */
     147
     148  return tr_strdup (buf);
     149}
     150
     151bool
     152tr_ssha1_matches (const char * ssha1,
     153                  const char * plain_text)
     154{
     155  char * salt;
     156  size_t saltlen;
     157  char * my_ssha1;
     158  uint8_t buf[SHA_DIGEST_LENGTH];
     159  bool result;
     160  const size_t sourcelen = strlen (ssha1);
     161
     162  /* extract the salt */
     163  if (sourcelen < 2 * SHA_DIGEST_LENGTH - 1)
     164    return false;
     165  saltlen = sourcelen - 2 * SHA_DIGEST_LENGTH - 1;
     166  salt = tr_malloc (saltlen);
     167  memcpy (salt, ssha1 + 2 * SHA_DIGEST_LENGTH + 1, saltlen);
     168
     169  /* hash pass + salt */
     170  my_ssha1 = tr_malloc (2 * SHA_DIGEST_LENGTH + saltlen + 2);
     171  tr_sha1 (buf, plain_text, strlen (plain_text), salt, saltlen, NULL);
     172  tr_sha1_to_hex (&my_ssha1[1], buf);
     173  memcpy (my_ssha1 + 1 + 2 * SHA_DIGEST_LENGTH, salt, saltlen);
     174  my_ssha1[1 + 2 * SHA_DIGEST_LENGTH + saltlen] = '\0';
     175  my_ssha1[0] = '{';
     176
     177  result = strcmp (ssha1, my_ssha1) == 0;
     178
     179  tr_free (my_ssha1);
     180  tr_free (salt);
     181
     182  return result;
     183}
     184
     185/***
     186****
     187***/
     188
    123189void *
    124190tr_base64_encode (const void * input,
  • trunk/libtransmission/crypto-utils.h

    r14359 r14360  
    156156
    157157/**
     158 * @brief Generate a SSHA password from its plaintext source.
     159 */
     160char           * tr_ssha1              (const char     * plain_text) TR_GNUC_MALLOC;
     161
     162/**
     163 * @brief Validate a test password against the a ssha1 password.
     164 */
     165bool             tr_ssha1_matches      (const char     * ssha1,
     166                                        const char     * plain_text);
     167
     168/**
    158169 * @brief Translate a block of bytes into base64.
    159170 * @return a newly-allocated null-terminated string that can be freed with tr_free ()
  • trunk/libtransmission/crypto.c

    r14358 r14360  
    99
    1010#include <assert.h>
    11 #include <string.h> /* memcpy (), memmove (), memset (), strcmp () */
     11#include <string.h> /* memcpy (), memmove (), memset () */
    1212
    1313#include "transmission.h"
     
    220220  return crypto->torrentHashIsSet;
    221221}
    222 
    223 /***
    224 ****
    225 ***/
    226 
    227 char*
    228 tr_ssha1 (const void * plaintext)
    229 {
    230   enum { saltval_len = 8,
    231          salter_len  = 64 };
    232   static const char * salter = "0123456789"
    233                                "abcdefghijklmnopqrstuvwxyz"
    234                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    235                                "./";
    236 
    237   size_t i;
    238   unsigned char salt[saltval_len];
    239   uint8_t sha[SHA_DIGEST_LENGTH];
    240   char buf[2*SHA_DIGEST_LENGTH + saltval_len + 2];
    241 
    242   tr_rand_buffer (salt, saltval_len);
    243   for (i=0; i<saltval_len; ++i)
    244     salt[i] = salter[ salt[i] % salter_len ];
    245 
    246   tr_sha1 (sha, plaintext, strlen (plaintext), salt, saltval_len, NULL);
    247   tr_sha1_to_hex (&buf[1], sha);
    248   memcpy (&buf[1+2*SHA_DIGEST_LENGTH], &salt, saltval_len);
    249   buf[1+2*SHA_DIGEST_LENGTH + saltval_len] = '\0';
    250   buf[0] = '{'; /* signal that this is a hash. this makes saving/restoring easier */
    251 
    252   return tr_strdup (&buf);
    253 }
    254 
    255 bool
    256 tr_ssha1_matches (const char * source, const char * pass)
    257 {
    258   char * salt;
    259   size_t saltlen;
    260   char * hashed;
    261   uint8_t buf[SHA_DIGEST_LENGTH];
    262   bool result;
    263   const size_t sourcelen = strlen (source);
    264 
    265   /* extract the salt */
    266   if (sourcelen < 2*SHA_DIGEST_LENGTH-1)
    267     return false;
    268   saltlen = sourcelen - 2*SHA_DIGEST_LENGTH-1;
    269   salt = tr_malloc (saltlen);
    270   memcpy (salt, source + 2*SHA_DIGEST_LENGTH+1, saltlen);
    271 
    272   /* hash pass + salt */
    273   hashed = tr_malloc (2*SHA_DIGEST_LENGTH + saltlen + 2);
    274   tr_sha1 (buf, pass, strlen (pass), salt, saltlen, NULL);
    275   tr_sha1_to_hex (&hashed[1], buf);
    276   memcpy (hashed + 1+2*SHA_DIGEST_LENGTH, salt, saltlen);
    277   hashed[1+2*SHA_DIGEST_LENGTH + saltlen] = '\0';
    278   hashed[0] = '{';
    279 
    280   result = strcmp (source, hashed) == 0 ? true : false;
    281 
    282   tr_free (hashed);
    283   tr_free (salt);
    284 
    285   return result;
    286 }
  • trunk/libtransmission/crypto.h

    r14358 r14360  
    8686/* @} */
    8787
    88 /**
    89 *** @addtogroup utils Utilities
    90 *** @{
    91 **/
    92 
    93 /** @brief generate a SSHA password from its plaintext source */
    94 char*  tr_ssha1 (const void * plaintext);
    95 
    96 /** @brief Validate a test password against the a ssha1 password */
    97 bool tr_ssha1_matches (const char * ssha1, const char * pass);
    98 
    99 /* @} */
    100 
    10188#endif
Note: See TracChangeset for help on using the changeset viewer.