Line data Source code
1 : /* seskey.c - make sesssion keys etc.
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 : * 2006, 2009, 2010 Free Software Foundation, Inc.
4 : *
5 : * This file is part of GnuPG.
6 : *
7 : * GnuPG is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * GnuPG is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <config.h>
22 : #include <stdio.h>
23 : #include <stdlib.h>
24 : #include <string.h>
25 :
26 : #include "gpg.h"
27 : #include "util.h"
28 : #include "options.h"
29 : #include "main.h"
30 : #include "i18n.h"
31 :
32 :
33 : /* Generate a new session key in *DEK that is appropriate for the
34 : * algorithm DEK->ALGO (i.e., ensure that the key is not weak).
35 : *
36 : * This function overwrites DEK->KEYLEN, DEK->KEY. The rest of the
37 : * fields are left as is. */
38 : void
39 247 : make_session_key( DEK *dek )
40 : {
41 : gcry_cipher_hd_t chd;
42 : int i, rc;
43 :
44 247 : dek->keylen = openpgp_cipher_get_algo_keylen (dek->algo);
45 :
46 247 : if (openpgp_cipher_open (&chd, dek->algo, GCRY_CIPHER_MODE_CFB,
47 : (GCRY_CIPHER_SECURE
48 : | (dek->algo >= 100 ?
49 : 0 : GCRY_CIPHER_ENABLE_SYNC))) )
50 0 : BUG();
51 247 : gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM );
52 247 : for (i=0; i < 16; i++ )
53 : {
54 247 : rc = gcry_cipher_setkey (chd, dek->key, dek->keylen);
55 247 : if (!rc)
56 : {
57 247 : gcry_cipher_close (chd);
58 247 : return;
59 : }
60 0 : if (gpg_err_code (rc) != GPG_ERR_WEAK_KEY)
61 0 : BUG();
62 0 : log_info(_("weak key created - retrying\n") );
63 : /* Renew the session key until we get a non-weak key. */
64 0 : gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM);
65 : }
66 0 : log_fatal (_("cannot avoid weak key for symmetric cipher; "
67 : "tried %d times!\n"), i);
68 : }
69 :
70 :
71 : /* Encode the session key stored in DEK as an MPI in preparation to
72 : * encrypt it with the public key algorithm OPENPGP_PK_ALGO with a key
73 : * whose length (the size of the public key) is NBITS.
74 : *
75 : * On success, returns an MPI, which the caller must free using
76 : * gcry_mpi_release(). */
77 : gcry_mpi_t
78 255 : encode_session_key (int openpgp_pk_algo, DEK *dek, unsigned int nbits)
79 : {
80 255 : size_t nframe = (nbits+7) / 8;
81 : byte *p;
82 : byte *frame;
83 : int i,n;
84 : u16 csum;
85 : gcry_mpi_t a;
86 :
87 255 : if (DBG_CRYPTO)
88 0 : log_debug ("encode_session_key: encoding %d byte DEK", dek->keylen);
89 :
90 255 : csum = 0;
91 6543 : for (p = dek->key, i=0; i < dek->keylen; i++)
92 6288 : csum += *p++;
93 :
94 : /* Shortcut for ECDH. It's padding is minimal to simply make the
95 : output be a multiple of 8 bytes. */
96 255 : if (openpgp_pk_algo == PUBKEY_ALGO_ECDH)
97 : {
98 : /* Pad to 8 byte granulatiry; the padding byte is the number of
99 : * padded bytes.
100 : *
101 : * A DEK(k bytes) CSUM(2 bytes) 0x 0x 0x 0x ... 0x
102 : * +---- x times ---+
103 : */
104 48 : nframe = (( 1 + dek->keylen + 2 /* The value so far is always odd. */
105 24 : + 7 ) & (~7));
106 :
107 : /* alg+key+csum fit and the size is congruent to 8. */
108 24 : log_assert (!(nframe%8) && nframe > 1 + dek->keylen + 2 );
109 :
110 24 : frame = xmalloc_secure (nframe);
111 24 : n = 0;
112 24 : frame[n++] = dek->algo;
113 24 : memcpy (frame+n, dek->key, dek->keylen);
114 24 : n += dek->keylen;
115 24 : frame[n++] = csum >> 8;
116 24 : frame[n++] = csum;
117 24 : i = nframe - n; /* Number of padded bytes. */
118 24 : memset (frame+n, i, i); /* Use it as the value of each padded byte. */
119 24 : log_assert (n+i == nframe);
120 :
121 24 : if (DBG_CRYPTO)
122 0 : log_debug ("encode_session_key: "
123 : "[%d] %02x %02x %02x ... %02x %02x %02x\n",
124 0 : (int) nframe, frame[0], frame[1], frame[2],
125 0 : frame[nframe-3], frame[nframe-2], frame[nframe-1]);
126 :
127 24 : if (gcry_mpi_scan (&a, GCRYMPI_FMT_USG, frame, nframe, &nframe))
128 0 : BUG();
129 24 : xfree(frame);
130 24 : return a;
131 : }
132 :
133 : /* The current limitation is that we can only use a session key
134 : * whose length is a multiple of BITS_PER_MPI_LIMB
135 : * I think we can live with that.
136 : */
137 231 : if (dek->keylen + 7 > nframe || !nframe)
138 0 : log_bug ("can't encode a %d bit key in a %d bits frame\n",
139 0 : dek->keylen*8, nbits );
140 :
141 : /* We encode the session key according to PKCS#1 v1.5 (see section
142 : * 13.1.1 of RFC 4880):
143 : *
144 : * 0 2 RND(i bytes) 0 A DEK(k bytes) CSUM(2 bytes)
145 : *
146 : * (But how can we store the leading 0 - the external representaion
147 : * of MPIs doesn't allow leading zeroes =:-)
148 : *
149 : * RND are (at least 1) non-zero random bytes.
150 : * A is the cipher algorithm
151 : * DEK is the encryption key (session key) length k depends on the
152 : * cipher algorithm (20 is used with blowfish160).
153 : * CSUM is the 16 bit checksum over the DEK
154 : */
155 :
156 231 : frame = xmalloc_secure( nframe );
157 231 : n = 0;
158 231 : frame[n++] = 0;
159 231 : frame[n++] = 2;
160 : /* The number of random bytes are the number of otherwise unused
161 : bytes. See diagram above. */
162 231 : i = nframe - 6 - dek->keylen;
163 231 : log_assert( i > 0 );
164 231 : p = gcry_random_bytes_secure (i, GCRY_STRONG_RANDOM);
165 : /* Replace zero bytes by new values. */
166 : for (;;)
167 : {
168 : int j, k;
169 : byte *pp;
170 :
171 : /* Count the zero bytes. */
172 26015 : for (j=k=0; j < i; j++ )
173 25722 : if (!p[j])
174 78 : k++;
175 293 : if (!k)
176 231 : break; /* Okay: no zero bytes. */
177 62 : k += k/128 + 3; /* Better get some more. */
178 62 : pp = gcry_random_bytes_secure (k, GCRY_STRONG_RANDOM);
179 5936 : for (j=0; j < i && k ;)
180 : {
181 5812 : if (!p[j])
182 78 : p[j] = pp[--k];
183 5812 : if (p[j])
184 5812 : j++;
185 : }
186 62 : xfree (pp);
187 62 : }
188 231 : memcpy (frame+n, p, i);
189 231 : xfree (p);
190 231 : n += i;
191 231 : frame[n++] = 0;
192 231 : frame[n++] = dek->algo;
193 231 : memcpy (frame+n, dek->key, dek->keylen );
194 231 : n += dek->keylen;
195 231 : frame[n++] = csum >>8;
196 231 : frame[n++] = csum;
197 231 : log_assert (n == nframe);
198 231 : if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, n, &nframe))
199 0 : BUG();
200 231 : xfree (frame);
201 231 : return a;
202 : }
203 :
204 :
205 : static gcry_mpi_t
206 195 : do_encode_md( gcry_md_hd_t md, int algo, size_t len, unsigned nbits,
207 : const byte *asn, size_t asnlen )
208 : {
209 195 : size_t nframe = (nbits+7) / 8;
210 : byte *frame;
211 : int i,n;
212 : gcry_mpi_t a;
213 :
214 195 : if (len + asnlen + 4 > nframe)
215 : {
216 0 : log_error ("can't encode a %d bit MD into a %d bits frame, algo=%d\n",
217 : (int)(len*8), (int)nbits, algo);
218 0 : return NULL;
219 : }
220 :
221 : /* We encode the MD in this way:
222 : *
223 : * 0 1 PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
224 : *
225 : * PAD consists of FF bytes.
226 : */
227 195 : frame = gcry_md_is_secure (md)? xmalloc_secure (nframe) : xmalloc (nframe);
228 195 : n = 0;
229 195 : frame[n++] = 0;
230 195 : frame[n++] = 1; /* block type */
231 195 : i = nframe - len - asnlen -3 ;
232 195 : log_assert( i > 1 );
233 195 : memset( frame+n, 0xff, i ); n += i;
234 195 : frame[n++] = 0;
235 195 : memcpy( frame+n, asn, asnlen ); n += asnlen;
236 195 : memcpy( frame+n, gcry_md_read (md, algo), len ); n += len;
237 195 : log_assert( n == nframe );
238 :
239 195 : if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, n, &nframe ))
240 0 : BUG();
241 195 : xfree(frame);
242 :
243 : /* Note that PGP before version 2.3 encoded the MD as:
244 : *
245 : * 0 1 MD(16 bytes) 0 PAD(n bytes) 1
246 : *
247 : * The MD is always 16 bytes here because it's always MD5. We do
248 : * not support pre-v2.3 signatures, but I'm including this comment
249 : * so the information is easily found in the future.
250 : */
251 :
252 195 : return a;
253 : }
254 :
255 :
256 : /****************
257 : * Encode a message digest into an MPI.
258 : * If it's for a DSA signature, make sure that the hash is large
259 : * enough to fill up q. If the hash is too big, take the leftmost
260 : * bits.
261 : */
262 : gcry_mpi_t
263 559 : encode_md_value (PKT_public_key *pk, gcry_md_hd_t md, int hash_algo)
264 : {
265 : gcry_mpi_t frame;
266 : size_t mdlen;
267 :
268 559 : log_assert (hash_algo);
269 559 : log_assert (pk);
270 :
271 559 : if (pk->pubkey_algo == PUBKEY_ALGO_EDDSA)
272 : {
273 : /* EdDSA signs data of arbitrary length. Thus no special
274 : treatment is required. */
275 10 : frame = gcry_mpi_set_opaque_copy (NULL, gcry_md_read (md, hash_algo),
276 10 : 8*gcry_md_get_algo_dlen (hash_algo));
277 : }
278 549 : else if (pk->pubkey_algo == PUBKEY_ALGO_DSA
279 232 : || pk->pubkey_algo == PUBKEY_ALGO_ECDSA)
280 354 : {
281 : /* It's a DSA signature, so find out the size of q. */
282 :
283 354 : size_t qbits = gcry_mpi_get_nbits (pk->pkey[1]);
284 :
285 : /* pkey[1] is Q for ECDSA, which is an uncompressed point,
286 : i.e. 04 <x> <y> */
287 354 : if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA)
288 37 : qbits = ecdsa_qbits_from_Q (qbits);
289 :
290 : /* Make sure it is a multiple of 8 bits. */
291 354 : if ((qbits%8))
292 : {
293 0 : log_error(_("DSA requires the hash length to be a"
294 : " multiple of 8 bits\n"));
295 0 : return NULL;
296 : }
297 :
298 : /* Don't allow any q smaller than 160 bits. This might need a
299 : revisit as the DSA2 design firms up, but for now, we don't
300 : want someone to issue signatures from a key with a 16-bit q
301 : or something like that, which would look correct but allow
302 : trivial forgeries. Yes, I know this rules out using MD5 with
303 : DSA. ;) */
304 354 : if (qbits < 160)
305 : {
306 0 : log_error (_("%s key %s uses an unsafe (%zu bit) hash\n"),
307 0 : openpgp_pk_algo_name (pk->pubkey_algo),
308 : keystr_from_pk (pk), qbits);
309 0 : return NULL;
310 : }
311 :
312 :
313 : /* ECDSA 521 is special has it is larger than the largest hash
314 : we have (SHA-512). Thus we chnage the size for further
315 : processing to 512. */
316 354 : if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA && qbits > 512)
317 11 : qbits = 512;
318 :
319 : /* Check if we're too short. Too long is safe as we'll
320 : automatically left-truncate. */
321 354 : mdlen = gcry_md_get_algo_dlen (hash_algo);
322 354 : if (mdlen < qbits/8)
323 : {
324 0 : log_error (_("%s key %s requires a %zu bit or larger hash "
325 : "(hash is %s)\n"),
326 0 : openpgp_pk_algo_name (pk->pubkey_algo),
327 : keystr_from_pk (pk), qbits,
328 : gcry_md_algo_name (hash_algo));
329 0 : return NULL;
330 : }
331 :
332 : /* Note that we do the truncation by passing QBITS/8 as length to
333 : mpi_scan. */
334 708 : if (gcry_mpi_scan (&frame, GCRYMPI_FMT_USG,
335 354 : gcry_md_read (md, hash_algo), qbits/8, NULL))
336 0 : BUG();
337 : }
338 : else
339 : {
340 : gpg_error_t rc;
341 : byte *asn;
342 : size_t asnlen;
343 :
344 195 : rc = gcry_md_algo_info (hash_algo, GCRYCTL_GET_ASNOID, NULL, &asnlen);
345 195 : if (rc)
346 0 : log_fatal ("can't get OID of digest algorithm %d: %s\n",
347 : hash_algo, gpg_strerror (rc));
348 195 : asn = xtrymalloc (asnlen);
349 195 : if (!asn)
350 0 : return NULL;
351 195 : if ( gcry_md_algo_info (hash_algo, GCRYCTL_GET_ASNOID, asn, &asnlen) )
352 0 : BUG();
353 195 : frame = do_encode_md (md, hash_algo, gcry_md_get_algo_dlen (hash_algo),
354 : gcry_mpi_get_nbits (pk->pkey[0]), asn, asnlen);
355 195 : xfree (asn);
356 : }
357 :
358 559 : return frame;
359 : }
|