crypto.c 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
 *                2007, 2008, 2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/crypto.h>
#include <grub/misc.h>
#include <grub/mm.h>
23
#include <grub/term.h>
24
#include <grub/dl.h>
25
#include <grub/i18n.h>
26
#include <grub/env.h>
27 28

GRUB_MOD_LICENSE ("GPLv3+");
29

30 31 32 33 34 35 36
struct grub_crypto_hmac_handle
{
  const struct gcry_md_spec *md;
  void *ctx;
  void *opad;
};

37 38 39
static gcry_cipher_spec_t *grub_ciphers = NULL;
static gcry_md_spec_t *grub_digests = NULL;

40 41
void (*grub_crypto_autoload_hook) (const char *name) = NULL;

42 43 44 45 46 47 48 49 50 51 52
/* Based on libgcrypt-1.4.4/src/misc.c.  */
void
grub_burn_stack (grub_size_t size)
{
  char buf[64];

  grub_memset (buf, 0, sizeof (buf));
  if (size > sizeof (buf))
    grub_burn_stack (size - sizeof (buf));
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
void
_gcry_burn_stack (int size)
{
  grub_burn_stack (size);
}

void __attribute__ ((noreturn))
_gcry_assert_failed (const char *expr, const char *file, int line,
		     const char *func)
  
{
  grub_fatal ("assertion %s at %s:%d (%s) failed\n", expr, file, line, func);
}


void _gcry_log_error (const char *fmt, ...)
{
  va_list args;
  const char *debug = grub_env_get ("debug");

  if (! debug)
    return;

  if (grub_strword (debug, "all") || grub_strword (debug, "gcrypt"))
    {
      grub_printf ("gcrypt error: ");
      va_start (args, fmt);
      grub_vprintf (fmt, args);
      va_end (args);
      grub_refresh ();
    }
}
85 86 87 88 89 90 91 92 93 94 95 96 97 98

void 
grub_cipher_register (gcry_cipher_spec_t *cipher)
{
  cipher->next = grub_ciphers;
  grub_ciphers = cipher;
}

void
grub_cipher_unregister (gcry_cipher_spec_t *cipher)
{
  gcry_cipher_spec_t **ciph;
  for (ciph = &grub_ciphers; *ciph; ciph = &((*ciph)->next))
    if (*ciph == cipher)
99 100 101 102
      {
	*ciph = (*ciph)->next;
	break;
      }
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

void 
grub_md_register (gcry_md_spec_t *digest)
{
  digest->next = grub_digests;
  grub_digests = digest;
}

void 
grub_md_unregister (gcry_md_spec_t *cipher)
{
  gcry_md_spec_t **ciph;
  for (ciph = &grub_digests; *ciph; ciph = &((*ciph)->next))
    if (*ciph == cipher)
118 119 120 121
      {
	*ciph = (*ciph)->next;
	break;
      }
122 123 124
}

void
125
grub_crypto_hash (const gcry_md_spec_t *hash, void *out, const void *in,
126 127
		  grub_size_t inlen)
{
128 129 130 131
  GRUB_PROPERLY_ALIGNED_ARRAY (ctx, GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);

  if (hash->contextsize > sizeof (ctx))
    grub_fatal ("Too large md context");
132 133 134 135 136 137 138 139 140 141
  hash->init (&ctx);
  hash->write (&ctx, in, inlen);
  hash->final (&ctx);
  grub_memcpy (out, hash->read (&ctx), hash->mdlen);
}

const gcry_md_spec_t *
grub_crypto_lookup_md_by_name (const char *name)
{
  const gcry_md_spec_t *md;
142 143 144 145 146 147 148 149 150 151 152 153
  int first = 1;
  while (1)
    {
      for (md = grub_digests; md; md = md->next)
	if (grub_strcasecmp (name, md->name) == 0)
	  return md;
      if (grub_crypto_autoload_hook && first)
	grub_crypto_autoload_hook (name);
      else
	return NULL;
      first = 0;
    }
154 155 156 157 158 159
}

const gcry_cipher_spec_t *
grub_crypto_lookup_cipher_by_name (const char *name)
{
  const gcry_cipher_spec_t *ciph;
160 161
  int first = 1;
  while (1)
162
    {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      for (ciph = grub_ciphers; ciph; ciph = ciph->next)
	{
	  const char **alias;
	  if (grub_strcasecmp (name, ciph->name) == 0)
	    return ciph;
	  if (!ciph->aliases)
	    continue;
	  for (alias = ciph->aliases; *alias; alias++)
	    if (grub_strcasecmp (name, *alias) == 0)
	      return ciph;
	}
      if (grub_crypto_autoload_hook && first)
	grub_crypto_autoload_hook (name);
      else
	return NULL;
      first = 0;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    }
}


grub_crypto_cipher_handle_t
grub_crypto_cipher_open (const struct gcry_cipher_spec *cipher)
{
  grub_crypto_cipher_handle_t ret;
  ret = grub_malloc (sizeof (*ret) + cipher->contextsize);
  if (!ret)
    return NULL;
  ret->cipher = cipher;
  return ret;
}

gcry_err_code_t
grub_crypto_cipher_set_key (grub_crypto_cipher_handle_t cipher,
			    const unsigned char *key,
			    unsigned keylen)
{
  return cipher->cipher->setkey (cipher->ctx, key, keylen);
}

202
gcry_err_code_t
203
grub_crypto_ecb_decrypt (grub_crypto_cipher_handle_t cipher,
204
			 void *out, const void *in, grub_size_t size)
205
{
206 207
  const grub_uint8_t *inptr, *end;
  grub_uint8_t *outptr;
208
  grub_size_t blocksize;
209 210
  if (!cipher->cipher->decrypt)
    return GPG_ERR_NOT_SUPPORTED;
211 212 213
  blocksize = cipher->cipher->blocksize;
  if (blocksize == 0 || (((blocksize - 1) & blocksize) != 0)
      || ((size & (blocksize - 1)) != 0))
214
    return GPG_ERR_INV_ARG;
215
  end = (const grub_uint8_t *) in + size;
216
  for (inptr = in, outptr = out; inptr < end;
217
       inptr += blocksize, outptr += blocksize)
218
    cipher->cipher->decrypt (cipher->ctx, outptr, inptr);
219
  return GPG_ERR_NO_ERROR;
220 221
}

222
gcry_err_code_t
223
grub_crypto_ecb_encrypt (grub_crypto_cipher_handle_t cipher,
224
			 void *out, const void *in, grub_size_t size)
225
{
226 227
  const grub_uint8_t *inptr, *end;
  grub_uint8_t *outptr;
228
  grub_size_t blocksize;
229 230
  if (!cipher->cipher->encrypt)
    return GPG_ERR_NOT_SUPPORTED;
231 232 233
  blocksize = cipher->cipher->blocksize;
  if (blocksize == 0 || (((blocksize - 1) & blocksize) != 0)
      || ((size & (blocksize - 1)) != 0))
234
    return GPG_ERR_INV_ARG;
235
  end = (const grub_uint8_t *) in + size;
236
  for (inptr = in, outptr = out; inptr < end;
237
       inptr += blocksize, outptr += blocksize)
238
    cipher->cipher->encrypt (cipher->ctx, outptr, inptr);
239
  return GPG_ERR_NO_ERROR;
240 241
}

242
gcry_err_code_t
243
grub_crypto_cbc_encrypt (grub_crypto_cipher_handle_t cipher,
244
			 void *out, const void *in, grub_size_t size,
245 246
			 void *iv_in)
{
247 248
  grub_uint8_t *outptr;
  const grub_uint8_t *inptr, *end;
249
  void *iv;
250 251
  grub_size_t blocksize;
  if (!cipher->cipher->encrypt)
252
    return GPG_ERR_NOT_SUPPORTED;
253 254 255
  blocksize = cipher->cipher->blocksize;
  if (blocksize == 0 || (((blocksize - 1) & blocksize) != 0)
      || ((size & (blocksize - 1)) != 0))
256
    return GPG_ERR_INV_ARG;
257
  end = (const grub_uint8_t *) in + size;
258 259
  iv = iv_in;
  for (inptr = in, outptr = out; inptr < end;
260
       inptr += blocksize, outptr += blocksize)
261
    {
262
      grub_crypto_xor (outptr, inptr, iv, blocksize);
263 264 265
      cipher->cipher->encrypt (cipher->ctx, outptr, outptr);
      iv = outptr;
    }
266
  grub_memcpy (iv_in, iv, blocksize);
267
  return GPG_ERR_NO_ERROR;
268 269
}

270
gcry_err_code_t
271
grub_crypto_cbc_decrypt (grub_crypto_cipher_handle_t cipher,
272
			 void *out, const void *in, grub_size_t size,
273 274
			 void *iv)
{
275 276
  const grub_uint8_t *inptr, *end;
  grub_uint8_t *outptr;
277
  grub_uint8_t ivt[GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE];
278
  grub_size_t blocksize;
279 280
  if (!cipher->cipher->decrypt)
    return GPG_ERR_NOT_SUPPORTED;
281 282 283
  blocksize = cipher->cipher->blocksize;
  if (blocksize == 0 || (((blocksize - 1) & blocksize) != 0)
      || ((size & (blocksize - 1)) != 0))
284
    return GPG_ERR_INV_ARG;
285
  if (blocksize > GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE)
286
    return GPG_ERR_INV_ARG;
287
  end = (const grub_uint8_t *) in + size;
288
  for (inptr = in, outptr = out; inptr < end;
289
       inptr += blocksize, outptr += blocksize)
290
    {
291
      grub_memcpy (ivt, inptr, blocksize);
292
      cipher->cipher->decrypt (cipher->ctx, outptr, inptr);
293 294
      grub_crypto_xor (outptr, outptr, iv, blocksize);
      grub_memcpy (iv, ivt, blocksize);
295
    }
296 297 298
  return GPG_ERR_NO_ERROR;
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/* Based on gcry/cipher/md.c.  */
struct grub_crypto_hmac_handle *
grub_crypto_hmac_init (const struct gcry_md_spec *md,
		       const void *key, grub_size_t keylen)
{
  grub_uint8_t *helpkey = NULL;
  grub_uint8_t *ipad = NULL, *opad = NULL;
  void *ctx = NULL;
  struct grub_crypto_hmac_handle *ret = NULL;
  unsigned i;

  if (md->mdlen > md->blocksize)
    return NULL;

  ctx = grub_malloc (md->contextsize);
  if (!ctx)
    goto err;

  if ( keylen > md->blocksize ) 
    {
      helpkey = grub_malloc (md->mdlen);
      if (!helpkey)
	goto err;
      grub_crypto_hash (md, helpkey, key, keylen);

      key = helpkey;
      keylen = md->mdlen;
    }

  ipad = grub_zalloc (md->blocksize);
  if (!ipad)
    goto err;

  opad = grub_zalloc (md->blocksize);
  if (!opad)
    goto err;

  grub_memcpy ( ipad, key, keylen );
  grub_memcpy ( opad, key, keylen );
  for (i=0; i < md->blocksize; i++ ) 
    {
      ipad[i] ^= 0x36;
      opad[i] ^= 0x5c;
    }
  grub_free (helpkey);
  helpkey = NULL;

  md->init (ctx);

  md->write (ctx, ipad, md->blocksize); /* inner pad */
  grub_memset (ipad, 0, md->blocksize);
  grub_free (ipad);
  ipad = NULL;

  ret = grub_malloc (sizeof (*ret));
  if (!ret)
    goto err;

  ret->md = md;
  ret->ctx = ctx;
  ret->opad = opad;

  return ret;

 err:
  grub_free (helpkey);
  grub_free (ctx);
  grub_free (ipad);
  grub_free (opad);
  return NULL;
}

void
372 373
grub_crypto_hmac_write (struct grub_crypto_hmac_handle *hnd,
			const void *data,
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
			grub_size_t datalen)
{
  hnd->md->write (hnd->ctx, data, datalen);
}

gcry_err_code_t
grub_crypto_hmac_fini (struct grub_crypto_hmac_handle *hnd, void *out)
{
  grub_uint8_t *p;
  grub_uint8_t *ctx2;

  ctx2 = grub_malloc (hnd->md->contextsize);
  if (!ctx2)
    return GPG_ERR_OUT_OF_MEMORY;

  hnd->md->final (hnd->ctx);
  hnd->md->read (hnd->ctx);
  p = hnd->md->read (hnd->ctx);

  hnd->md->init (ctx2);
  hnd->md->write (ctx2, hnd->opad, hnd->md->blocksize);
  hnd->md->write (ctx2, p, hnd->md->mdlen);
  hnd->md->final (ctx2);
  grub_memset (hnd->opad, 0, hnd->md->blocksize);
  grub_free (hnd->opad);
  grub_memset (hnd->ctx, 0, hnd->md->contextsize);
  grub_free (hnd->ctx);

  grub_memcpy (out, hnd->md->read (ctx2), hnd->md->mdlen);
  grub_memset (ctx2, 0, hnd->md->contextsize);
  grub_free (ctx2);

  grub_memset (hnd, 0, sizeof (*hnd));
  grub_free (hnd);

  return GPG_ERR_NO_ERROR;
}

gcry_err_code_t
grub_crypto_hmac_buffer (const struct gcry_md_spec *md,
			 const void *key, grub_size_t keylen,
415
			 const void *data, grub_size_t datalen, void *out)
416 417 418 419 420 421 422 423 424 425 426 427
{
  struct grub_crypto_hmac_handle *hnd;

  hnd = grub_crypto_hmac_init (md, key, keylen);
  if (!hnd)
    return GPG_ERR_OUT_OF_MEMORY;

  grub_crypto_hmac_write (hnd, data, datalen);
  return grub_crypto_hmac_fini (hnd, out);
}


428 429 430 431 432 433
grub_err_t
grub_crypto_gcry_error (gcry_err_code_t in)
{
  if (in == GPG_ERR_NO_ERROR)
    return GRUB_ERR_NONE;
  return GRUB_ACCESS_DENIED;
434
}
435 436

int
437
grub_crypto_memcmp (const void *a, const void *b, grub_size_t n)
438 439
{
  register grub_size_t counter = 0;
440
  const grub_uint8_t *pa, *pb;
441 442 443 444 445 446 447 448 449

  for (pa = a, pb = b; n; pa++, pb++, n--)
    {
      if (*pa != *pb)
	counter++;
    }

  return !!counter;
}
450

451 452
#ifndef GRUB_UTIL

453 454 455 456 457 458 459 460
int
grub_password_get (char buf[], unsigned buf_size)
{
  unsigned cur_len = 0;
  int key;

  while (1)
    {
461
      key = grub_getkey (); 
462 463 464 465 466 467 468 469 470 471 472
      if (key == '\n' || key == '\r')
	break;

      if (key == '\e')
	{
	  cur_len = 0;
	  break;
	}

      if (key == '\b')
	{
473 474
	  if (cur_len)
	    cur_len--;
475 476 477 478 479 480 481 482 483 484 485 486
	  continue;
	}

      if (!grub_isprint (key))
	continue;

      if (cur_len + 2 < buf_size)
	buf[cur_len++] = key;
    }

  grub_memset (buf + cur_len, 0, buf_size - cur_len);

487
  grub_xputs ("\n");
488 489 490
  grub_refresh ();

  return (key != '\e');
491
}
492
#endif
493