~ubuntu-branches/ubuntu/raring/gnupg2/raring-proposed

« back to all changes in this revision

Viewing changes to agent/protect.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2010-01-09 21:15:18 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100109211518-jy03kb8hv1a12gfz
Tags: 2.0.14-1
* New upstream release.
* debian/control: Build depend on libreadline-dev instead of
  libreadline5-dev, since libreadline6-dev is out. (Closes: #548922)
* debian/source/format, debian/source/options,
  debian/source/patch-header: Convert to v3 quilt format, with
  single-debian-patch.
* debian/control: Tighten dependency on gnupg-agent. (Closes: #551792)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <assert.h>
28
28
#include <unistd.h>
29
29
#include <sys/stat.h>
 
30
#ifdef HAVE_W32_SYSTEM
 
31
# include <windows.h>
 
32
#else
 
33
# include <sys/times.h>
 
34
#endif
30
35
 
31
36
#include "agent.h"
32
37
 
51
56
};
52
57
 
53
58
 
 
59
/* A helper object for time measurement.  */
 
60
struct calibrate_time_s
 
61
{
 
62
#ifdef HAVE_W32_SYSTEM
 
63
  FILETIME creation_time, exit_time, kernel_time, user_time;
 
64
#else
 
65
  clock_t ticks;
 
66
#endif
 
67
};
 
68
 
 
69
 
54
70
static int
55
71
hash_passphrase (const char *passphrase, int hashalgo,
56
72
                 int s2kmode,
57
73
                 const unsigned char *s2ksalt, unsigned long s2kcount,
58
74
                 unsigned char *key, size_t keylen);
59
75
 
 
76
/* Get the process time and store it in DATA.  */
 
77
static void
 
78
calibrate_get_time (struct calibrate_time_s *data)
 
79
{
 
80
#ifdef HAVE_W32_SYSTEM
 
81
  GetProcessTimes (GetCurrentProcess (),
 
82
                   &data->creation_time, &data->exit_time,
 
83
                   &data->kernel_time, &data->user_time);
 
84
#else
 
85
  struct tms tmp;
 
86
  
 
87
  times (&tmp);
 
88
  data->ticks = tmp.tms_utime;
 
89
#endif
 
90
}
 
91
 
 
92
 
 
93
static unsigned long
 
94
calibrate_elapsed_time (struct calibrate_time_s *starttime)
 
95
{
 
96
  struct calibrate_time_s stoptime;
 
97
  
 
98
  calibrate_get_time (&stoptime);
 
99
#ifdef HAVE_W32_SYSTEM
 
100
  {
 
101
    unsigned long long t1, t2;
 
102
    
 
103
    t1 = (((unsigned long long)starttime->kernel_time.dwHighDateTime << 32)
 
104
          + starttime->kernel_time.dwLowDateTime);
 
105
    t1 += (((unsigned long long)starttime->user_time.dwHighDateTime << 32)
 
106
           + starttime->user_time.dwLowDateTime);
 
107
    t2 = (((unsigned long long)stoptime.kernel_time.dwHighDateTime << 32)
 
108
          + stoptime.kernel_time.dwLowDateTime);
 
109
    t2 += (((unsigned long long)stoptime.user_time.dwHighDateTime << 32)
 
110
           + stoptime.user_time.dwLowDateTime);
 
111
    return (unsigned long)((t2 - t1)/10000);
 
112
  }
 
113
#else
 
114
  return (unsigned long)((((double) (stoptime.ticks - starttime->ticks))
 
115
                          /CLOCKS_PER_SEC)*10000000);
 
116
#endif
 
117
}
 
118
 
 
119
 
 
120
/* Run a test hashing for COUNT and return the time required in
 
121
   milliseconds.  */
 
122
static unsigned long
 
123
calibrate_s2k_count_one (unsigned long count)
 
124
{
 
125
  int rc;
 
126
  char keybuf[PROT_CIPHER_KEYLEN];
 
127
  struct calibrate_time_s starttime;
 
128
 
 
129
  calibrate_get_time (&starttime);
 
130
  rc = hash_passphrase ("123456789abcdef0", GCRY_MD_SHA1,
 
131
                        3, "saltsalt", count, keybuf, sizeof keybuf);
 
132
  if (rc)
 
133
    BUG ();
 
134
  return calibrate_elapsed_time (&starttime);
 
135
}
 
136
 
 
137
 
 
138
/* Measure the time we need to do the hash operations and deduce an
 
139
   S2K count which requires about 100ms of time.  */ 
 
140
static unsigned long
 
141
calibrate_s2k_count (void)
 
142
{
 
143
  unsigned long count;
 
144
  unsigned long ms;
 
145
 
 
146
  for (count = 65536; count; count *= 2)
 
147
    {
 
148
      ms = calibrate_s2k_count_one (count);
 
149
      if (opt.verbose > 1)
 
150
        log_info ("S2K calibration: %lu -> %lums\n", count, ms);
 
151
      if (ms > 100)
 
152
        break;
 
153
    }
 
154
 
 
155
  count = (unsigned long)(((double)count / ms) * 100);
 
156
  count /= 1024;
 
157
  count *= 1024;
 
158
  if (count < 65536)
 
159
    count = 65536;
 
160
 
 
161
  if (opt.verbose)
 
162
    {
 
163
      ms = calibrate_s2k_count_one (count);
 
164
      log_info ("S2K calibration: %lu iterations for %lums\n", count, ms);
 
165
    }
 
166
 
 
167
  return count;
 
168
}
 
169
 
 
170
 
 
171
 
 
172
/* Return the standard S2K count.  */
 
173
unsigned long
 
174
get_standard_s2k_count (void)
 
175
{
 
176
  static unsigned long count;
 
177
 
 
178
  if (!count)
 
179
    count = calibrate_s2k_count ();
 
180
 
 
181
  /* Enforce a lower limit.  */
 
182
  return count < 65536 ? 65536 : count;
 
183
}
 
184
 
 
185
 
60
186
 
61
187
 
62
188
/* Calculate the MIC for a private key S-Exp. SHA1HASH should point to
193
319
      else
194
320
        {
195
321
          rc = hash_passphrase (passphrase, GCRY_MD_SHA1,
196
 
                                3, iv+2*blklen, 96, key, keylen);
 
322
                                3, iv+2*blklen, 
 
323
                                get_standard_s2k_count (), key, keylen);
197
324
          if (!rc)
198
325
            rc = gcry_cipher_setkey (hd, key, keylen);
199
326
          xfree (key);
757
884
     is nothing we should worry about */
758
885
  if (s[n] != ')' )
759
886
    return gpg_error (GPG_ERR_INV_SEXP);
 
887
  
 
888
  /* Old versions of gpg-agent used the funny floating point number in
 
889
     a byte encoding as specified by OpenPGP.  However this is not
 
890
     needed and thus we now store it as a plain unsigned integer.  We
 
891
     can easily distinguish the old format by looking at its value:
 
892
     Less than 256 is an old-style encoded number; other values are
 
893
     plain integers.  In any case we check that they are at least
 
894
     65536 because we never used a lower value in the past and we
 
895
     should have a lower limit.  */
760
896
  s2kcount = strtoul ((const char*)s, NULL, 10);
761
897
  if (!s2kcount)
762
898
    return gpg_error (GPG_ERR_CORRUPTED_PROTECTION);
 
899
  if (s2kcount < 256)
 
900
    s2kcount = (16ul + (s2kcount & 15)) << ((s2kcount >> 4) + 6);
 
901
  if (s2kcount < 65536)
 
902
    return gpg_error (GPG_ERR_CORRUPTED_PROTECTION);
 
903
 
763
904
  s += n;
764
905
  s++; /* skip list end */
765
906
 
847
988
/* Transform a passphrase into a suitable key of length KEYLEN and
848
989
   store this key in the caller provided buffer KEY.  The caller must
849
990
   provide an HASHALGO, a valid S2KMODE (see rfc-2440) and depending on
850
 
   that mode an S2KSALT of 8 random bytes and an S2KCOUNT (a suitable
851
 
   value is 96).
 
991
   that mode an S2KSALT of 8 random bytes and an S2KCOUNT.
852
992
  
853
993
   Returns an error code on failure.  */
854
994
static int
890
1030
 
891
1031
          if (s2kmode == 3)
892
1032
            {
893
 
              count = (16ul + (s2kcount & 15)) << ((s2kcount >> 4) + 6);
 
1033
              count = s2kcount;
894
1034
              if (count < len2)
895
1035
                count = len2;
896
1036
            }