2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
2N/A * 2007, 2008, 2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A/* Contains elements based on gcrypt-module.h and gcrypt.h.in.
2N/A If it's changed please update this file. */
2N/A
2N/A#ifndef GRUB_CRYPTO_HEADER
2N/A#define GRUB_CRYPTO_HEADER 1
2N/A
2N/A#include <grub/symbol.h>
2N/A#include <grub/types.h>
2N/A#include <grub/err.h>
2N/A#include <grub/mm.h>
2N/A
2N/Atypedef enum
2N/A {
2N/A GPG_ERR_NO_ERROR,
2N/A GPG_ERR_BAD_MPI,
2N/A GPG_ERR_BAD_SECKEY,
2N/A GPG_ERR_BAD_SIGNATURE,
2N/A GPG_ERR_CIPHER_ALGO,
2N/A GPG_ERR_CONFLICT,
2N/A GPG_ERR_DECRYPT_FAILED,
2N/A GPG_ERR_DIGEST_ALGO,
2N/A GPG_ERR_GENERAL,
2N/A GPG_ERR_INTERNAL,
2N/A GPG_ERR_INV_ARG,
2N/A GPG_ERR_INV_CIPHER_MODE,
2N/A GPG_ERR_INV_FLAG,
2N/A GPG_ERR_INV_KEYLEN,
2N/A GPG_ERR_INV_OBJ,
2N/A GPG_ERR_INV_OP,
2N/A GPG_ERR_INV_SEXP,
2N/A GPG_ERR_INV_VALUE,
2N/A GPG_ERR_MISSING_VALUE,
2N/A GPG_ERR_NO_ENCRYPTION_SCHEME,
2N/A GPG_ERR_NO_OBJ,
2N/A GPG_ERR_NO_PRIME,
2N/A GPG_ERR_NO_SIGNATURE_SCHEME,
2N/A GPG_ERR_NOT_FOUND,
2N/A GPG_ERR_NOT_IMPLEMENTED,
2N/A GPG_ERR_NOT_SUPPORTED,
2N/A GPG_ERROR_CFLAGS,
2N/A GPG_ERR_PUBKEY_ALGO,
2N/A GPG_ERR_SELFTEST_FAILED,
2N/A GPG_ERR_TOO_SHORT,
2N/A GPG_ERR_UNSUPPORTED,
2N/A GPG_ERR_WEAK_KEY,
2N/A GPG_ERR_WRONG_KEY_USAGE,
2N/A GPG_ERR_WRONG_PUBKEY_ALGO,
2N/A GPG_ERR_OUT_OF_MEMORY
2N/A } gcry_err_code_t;
2N/A#define gpg_err_code_t gcry_err_code_t
2N/A#define gpg_error_t gcry_err_code_t
2N/A
2N/Aenum gcry_cipher_modes
2N/A {
2N/A GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */
2N/A GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */
2N/A GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */
2N/A GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */
2N/A GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */
2N/A GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */
2N/A GCRY_CIPHER_MODE_CTR = 6 /* Counter. */
2N/A };
2N/A
2N/A/* Type for the cipher_setkey function. */
2N/Atypedef gcry_err_code_t (*gcry_cipher_setkey_t) (void *c,
2N/A const unsigned char *key,
2N/A unsigned keylen);
2N/A
2N/A/* Type for the cipher_encrypt function. */
2N/Atypedef void (*gcry_cipher_encrypt_t) (void *c,
2N/A unsigned char *outbuf,
2N/A const unsigned char *inbuf);
2N/A
2N/A/* Type for the cipher_decrypt function. */
2N/Atypedef void (*gcry_cipher_decrypt_t) (void *c,
2N/A unsigned char *outbuf,
2N/A const unsigned char *inbuf);
2N/A
2N/A/* Type for the cipher_stencrypt function. */
2N/Atypedef void (*gcry_cipher_stencrypt_t) (void *c,
2N/A unsigned char *outbuf,
2N/A const unsigned char *inbuf,
2N/A unsigned int n);
2N/A
2N/A/* Type for the cipher_stdecrypt function. */
2N/Atypedef void (*gcry_cipher_stdecrypt_t) (void *c,
2N/A unsigned char *outbuf,
2N/A const unsigned char *inbuf,
2N/A unsigned int n);
2N/A
2N/Atypedef struct gcry_cipher_oid_spec
2N/A{
2N/A const char *oid;
2N/A int mode;
2N/A} gcry_cipher_oid_spec_t;
2N/A
2N/A/* Module specification structure for ciphers. */
2N/Atypedef struct gcry_cipher_spec
2N/A{
2N/A const char *name;
2N/A const char **aliases;
2N/A gcry_cipher_oid_spec_t *oids;
2N/A grub_size_t blocksize;
2N/A grub_size_t keylen;
2N/A grub_size_t contextsize;
2N/A gcry_cipher_setkey_t setkey;
2N/A gcry_cipher_encrypt_t encrypt;
2N/A gcry_cipher_decrypt_t decrypt;
2N/A gcry_cipher_stencrypt_t stencrypt;
2N/A gcry_cipher_stdecrypt_t stdecrypt;
2N/A#ifdef GRUB_UTIL
2N/A const char *modname;
2N/A#endif
2N/A struct gcry_cipher_spec *next;
2N/A} gcry_cipher_spec_t;
2N/A
2N/A/* Type for the md_init function. */
2N/Atypedef void (*gcry_md_init_t) (void *c);
2N/A
2N/A/* Type for the md_write function. */
2N/Atypedef void (*gcry_md_write_t) (void *c, const void *buf, grub_size_t nbytes);
2N/A
2N/A/* Type for the md_final function. */
2N/Atypedef void (*gcry_md_final_t) (void *c);
2N/A
2N/A/* Type for the md_read function. */
2N/Atypedef unsigned char *(*gcry_md_read_t) (void *c);
2N/A
2N/Atypedef struct gcry_md_oid_spec
2N/A{
2N/A const char *oidstring;
2N/A} gcry_md_oid_spec_t;
2N/A
2N/A/* Module specification structure for message digests. */
2N/Atypedef struct gcry_md_spec
2N/A{
2N/A const char *name;
2N/A unsigned char *asnoid;
2N/A int asnlen;
2N/A gcry_md_oid_spec_t *oids;
2N/A grub_size_t mdlen;
2N/A gcry_md_init_t init;
2N/A gcry_md_write_t write;
2N/A gcry_md_final_t final;
2N/A gcry_md_read_t read;
2N/A grub_size_t contextsize; /* allocate this amount of context */
2N/A /* Block size, needed for HMAC. */
2N/A grub_size_t blocksize;
2N/A#ifdef GRUB_UTIL
2N/A const char *modname;
2N/A#endif
2N/A struct gcry_md_spec *next;
2N/A} gcry_md_spec_t;
2N/A
2N/Astruct grub_crypto_cipher_handle
2N/A{
2N/A const struct gcry_cipher_spec *cipher;
2N/A char ctx[0];
2N/A};
2N/A
2N/Atypedef struct grub_crypto_cipher_handle *grub_crypto_cipher_handle_t;
2N/A
2N/Astruct grub_crypto_hmac_handle;
2N/A
2N/Aconst gcry_cipher_spec_t *
2N/Agrub_crypto_lookup_cipher_by_name (const char *name);
2N/A
2N/Agrub_crypto_cipher_handle_t
2N/Agrub_crypto_cipher_open (const struct gcry_cipher_spec *cipher);
2N/A
2N/Agcry_err_code_t
2N/Agrub_crypto_cipher_set_key (grub_crypto_cipher_handle_t cipher,
2N/A const unsigned char *key,
2N/A unsigned keylen);
2N/A
2N/Astatic inline void
2N/Agrub_crypto_cipher_close (grub_crypto_cipher_handle_t cipher)
2N/A{
2N/A grub_free (cipher);
2N/A}
2N/A
2N/Astatic inline void
2N/Agrub_crypto_xor (void *out, const void *in1, const void *in2, grub_size_t size)
2N/A{
2N/A const grub_uint8_t *in1ptr = in1, *in2ptr = in2;
2N/A grub_uint8_t *outptr = out;
2N/A while (size && (((grub_addr_t) in1ptr & (sizeof (grub_uint64_t) - 1))
2N/A || ((grub_addr_t) in2ptr & (sizeof (grub_uint64_t) - 1))
2N/A || ((grub_addr_t) outptr & (sizeof (grub_uint64_t) - 1))))
2N/A {
2N/A *outptr = *in1ptr ^ *in2ptr;
2N/A in1ptr++;
2N/A in2ptr++;
2N/A outptr++;
2N/A size--;
2N/A }
2N/A while (size >= sizeof (grub_uint64_t))
2N/A {
2N/A *(grub_uint64_t *) (void *) outptr
2N/A = (*(grub_uint64_t *) (void *) in1ptr
2N/A ^ *(grub_uint64_t *) (void *) in2ptr);
2N/A in1ptr += sizeof (grub_uint64_t);
2N/A in2ptr += sizeof (grub_uint64_t);
2N/A outptr += sizeof (grub_uint64_t);
2N/A size -= sizeof (grub_uint64_t);
2N/A }
2N/A while (size)
2N/A {
2N/A *outptr = *in1ptr ^ *in2ptr;
2N/A in1ptr++;
2N/A in2ptr++;
2N/A outptr++;
2N/A size--;
2N/A }
2N/A}
2N/A
2N/Agcry_err_code_t
2N/Agrub_crypto_ecb_decrypt (grub_crypto_cipher_handle_t cipher,
2N/A void *out, const void *in, grub_size_t size);
2N/A
2N/Agcry_err_code_t
2N/Agrub_crypto_ecb_encrypt (grub_crypto_cipher_handle_t cipher,
2N/A void *out, const void *in, grub_size_t size);
2N/Agcry_err_code_t
2N/Agrub_crypto_cbc_encrypt (grub_crypto_cipher_handle_t cipher,
2N/A void *out, void *in, grub_size_t size,
2N/A void *iv_in);
2N/Agcry_err_code_t
2N/Agrub_crypto_cbc_decrypt (grub_crypto_cipher_handle_t cipher,
2N/A void *out, const void *in, grub_size_t size,
2N/A void *iv);
2N/Avoid
2N/Agrub_cipher_register (gcry_cipher_spec_t *cipher);
2N/Avoid
2N/Agrub_cipher_unregister (gcry_cipher_spec_t *cipher);
2N/Avoid
2N/Agrub_md_register (gcry_md_spec_t *digest);
2N/Avoid
2N/Agrub_md_unregister (gcry_md_spec_t *cipher);
2N/Avoid
2N/Agrub_crypto_hash (const gcry_md_spec_t *hash, void *out, const void *in,
2N/A grub_size_t inlen);
2N/Aconst gcry_md_spec_t *
2N/Agrub_crypto_lookup_md_by_name (const char *name);
2N/A
2N/Agrub_err_t
2N/Agrub_crypto_gcry_error (gcry_err_code_t in);
2N/A
2N/Avoid grub_burn_stack (grub_size_t size);
2N/A
2N/Astruct grub_crypto_hmac_handle *
2N/Agrub_crypto_hmac_init (const struct gcry_md_spec *md,
2N/A const void *key, grub_size_t keylen);
2N/Avoid
2N/Agrub_crypto_hmac_write (struct grub_crypto_hmac_handle *hnd,
2N/A const void *data,
2N/A grub_size_t datalen);
2N/Agcry_err_code_t
2N/Agrub_crypto_hmac_fini (struct grub_crypto_hmac_handle *hnd, void *out);
2N/A
2N/Agcry_err_code_t
2N/Agrub_crypto_hmac_buffer (const struct gcry_md_spec *md,
2N/A const void *key, grub_size_t keylen,
2N/A const void *data, grub_size_t datalen, void *out);
2N/A
2N/Aextern gcry_md_spec_t _gcry_digest_spec_md5;
2N/Aextern gcry_md_spec_t _gcry_digest_spec_sha1;
2N/Aextern gcry_md_spec_t _gcry_digest_spec_sha256;
2N/Aextern gcry_md_spec_t _gcry_digest_spec_sha512;
2N/Aextern gcry_md_spec_t _gcry_digest_spec_crc32;
2N/Aextern gcry_cipher_spec_t _gcry_cipher_spec_aes;
2N/A#define GRUB_MD_MD5 ((const gcry_md_spec_t *) &_gcry_digest_spec_md5)
2N/A#define GRUB_MD_SHA1 ((const gcry_md_spec_t *) &_gcry_digest_spec_sha1)
2N/A#define GRUB_MD_SHA256 ((const gcry_md_spec_t *) &_gcry_digest_spec_sha256)
2N/A#define GRUB_MD_SHA512 ((const gcry_md_spec_t *) &_gcry_digest_spec_sha512)
2N/A#define GRUB_MD_CRC32 ((const gcry_md_spec_t *) &_gcry_digest_spec_crc32)
2N/A#define GRUB_CIPHER_AES ((const gcry_cipher_spec_t *) &_gcry_cipher_spec_aes)
2N/A
2N/A/* Implement PKCS#5 PBKDF2 as per RFC 2898. The PRF to use is HMAC variant
2N/A of digest supplied by MD. Inputs are the password P of length PLEN,
2N/A the salt S of length SLEN, the iteration counter C (> 0), and the
2N/A desired derived output length DKLEN. Output buffer is DK which
2N/A must have room for at least DKLEN octets. The output buffer will
2N/A be filled with the derived data. */
2N/Agcry_err_code_t
2N/Agrub_crypto_pbkdf2 (const struct gcry_md_spec *md,
2N/A const grub_uint8_t *P, grub_size_t Plen,
2N/A const grub_uint8_t *S, grub_size_t Slen,
2N/A unsigned int c,
2N/A grub_uint8_t *DK, grub_size_t dkLen);
2N/A
2N/Aint
2N/Agrub_crypto_memcmp (const void *a, const void *b, grub_size_t n);
2N/A
2N/Aint
2N/Agrub_password_get (char buf[], unsigned buf_size);
2N/A
2N/A/* For indistinguishibility. */
2N/A#define GRUB_ACCESS_DENIED grub_error (GRUB_ERR_ACCESS_DENIED, "Access denied.")
2N/A
2N/Aextern void (*grub_crypto_autoload_hook) (const char *name);
2N/A
2N/A#ifdef GRUB_UTIL
2N/Avoid grub_gcry_init_all (void);
2N/Avoid grub_gcry_fini_all (void);
2N/A#endif
2N/A
2N/A
2N/A#endif