~jonathank89/burg/burg-percise

« back to all changes in this revision

Viewing changes to include/grub/crypto.h

merge mainline into mips

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
 
4
 *                2007, 2008, 2009  Free Software Foundation, Inc.
 
5
 *
 
6
 *  GRUB is free software: you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation, either version 3 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  GRUB is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
/* Contains elements based on gcrypt-module.h and gcrypt.h.in.
 
21
   If it's changed please update this file.  */
 
22
 
 
23
#ifndef GRUB_CRYPTO_HEADER
 
24
#define GRUB_CRYPTO_HEADER 1
 
25
 
 
26
#include <grub/symbol.h>
 
27
#include <grub/types.h>
 
28
#include <grub/err.h>
 
29
 
 
30
typedef enum 
 
31
  {
 
32
    GPG_ERR_NO_ERROR,
 
33
    GPG_ERR_BAD_MPI,
 
34
    GPG_ERR_BAD_SECKEY,
 
35
    GPG_ERR_BAD_SIGNATURE,
 
36
    GPG_ERR_CIPHER_ALGO,
 
37
    GPG_ERR_CONFLICT,
 
38
    GPG_ERR_DECRYPT_FAILED,
 
39
    GPG_ERR_DIGEST_ALGO,
 
40
    GPG_ERR_GENERAL,
 
41
    GPG_ERR_INTERNAL,
 
42
    GPG_ERR_INV_ARG,
 
43
    GPG_ERR_INV_CIPHER_MODE,
 
44
    GPG_ERR_INV_FLAG,
 
45
    GPG_ERR_INV_KEYLEN,
 
46
    GPG_ERR_INV_OBJ,
 
47
    GPG_ERR_INV_OP,
 
48
    GPG_ERR_INV_SEXP,
 
49
    GPG_ERR_INV_VALUE,
 
50
    GPG_ERR_MISSING_VALUE,
 
51
    GPG_ERR_NO_ENCRYPTION_SCHEME,
 
52
    GPG_ERR_NO_OBJ,
 
53
    GPG_ERR_NO_PRIME,
 
54
    GPG_ERR_NO_SIGNATURE_SCHEME,
 
55
    GPG_ERR_NOT_FOUND,
 
56
    GPG_ERR_NOT_IMPLEMENTED,
 
57
    GPG_ERR_NOT_SUPPORTED,
 
58
    GPG_ERROR_CFLAGS,
 
59
    GPG_ERR_PUBKEY_ALGO,
 
60
    GPG_ERR_SELFTEST_FAILED,
 
61
    GPG_ERR_TOO_SHORT,
 
62
    GPG_ERR_UNSUPPORTED,
 
63
    GPG_ERR_WEAK_KEY,
 
64
    GPG_ERR_WRONG_KEY_USAGE,
 
65
    GPG_ERR_WRONG_PUBKEY_ALGO,
 
66
    GPG_ERR_OUT_OF_MEMORY
 
67
  } gcry_err_code_t;
 
68
#define gpg_err_code_t gcry_err_code_t
 
69
#define gpg_error_t gcry_err_code_t
 
70
 
 
71
enum gcry_cipher_modes 
 
72
  {
 
73
    GCRY_CIPHER_MODE_NONE   = 0,  /* Not yet specified. */
 
74
    GCRY_CIPHER_MODE_ECB    = 1,  /* Electronic codebook. */
 
75
    GCRY_CIPHER_MODE_CFB    = 2,  /* Cipher feedback. */
 
76
    GCRY_CIPHER_MODE_CBC    = 3,  /* Cipher block chaining. */
 
77
    GCRY_CIPHER_MODE_STREAM = 4,  /* Used with stream ciphers. */
 
78
    GCRY_CIPHER_MODE_OFB    = 5,  /* Outer feedback. */
 
79
    GCRY_CIPHER_MODE_CTR    = 6   /* Counter. */
 
80
  };
 
81
 
 
82
/* Type for the cipher_setkey function.  */
 
83
typedef gcry_err_code_t (*gcry_cipher_setkey_t) (void *c,
 
84
                                                 const unsigned char *key,
 
85
                                                 unsigned keylen);
 
86
 
 
87
/* Type for the cipher_encrypt function.  */
 
88
typedef void (*gcry_cipher_encrypt_t) (void *c,
 
89
                                       unsigned char *outbuf,
 
90
                                       const unsigned char *inbuf);
 
91
 
 
92
/* Type for the cipher_decrypt function.  */
 
93
typedef void (*gcry_cipher_decrypt_t) (void *c,
 
94
                                       unsigned char *outbuf,
 
95
                                       const unsigned char *inbuf);
 
96
 
 
97
/* Type for the cipher_stencrypt function.  */
 
98
typedef void (*gcry_cipher_stencrypt_t) (void *c,
 
99
                                         unsigned char *outbuf,
 
100
                                         const unsigned char *inbuf,
 
101
                                         unsigned int n);
 
102
 
 
103
/* Type for the cipher_stdecrypt function.  */
 
104
typedef void (*gcry_cipher_stdecrypt_t) (void *c,
 
105
                                         unsigned char *outbuf,
 
106
                                         const unsigned char *inbuf,
 
107
                                         unsigned int n);
 
108
 
 
109
typedef struct gcry_cipher_oid_spec
 
110
{
 
111
  const char *oid;
 
112
  int mode;
 
113
} gcry_cipher_oid_spec_t;
 
114
 
 
115
/* Module specification structure for ciphers.  */
 
116
typedef struct gcry_cipher_spec
 
117
{
 
118
  const char *name;
 
119
  const char **aliases;
 
120
  gcry_cipher_oid_spec_t *oids;
 
121
  grub_size_t blocksize;
 
122
  grub_size_t keylen;
 
123
  grub_size_t contextsize;
 
124
  gcry_cipher_setkey_t setkey;
 
125
  gcry_cipher_encrypt_t encrypt;
 
126
  gcry_cipher_decrypt_t decrypt;
 
127
  gcry_cipher_stencrypt_t stencrypt;
 
128
  gcry_cipher_stdecrypt_t stdecrypt;
 
129
  struct gcry_cipher_spec *next;
 
130
} gcry_cipher_spec_t;
 
131
 
 
132
/* Type for the md_init function.  */
 
133
typedef void (*gcry_md_init_t) (void *c);
 
134
 
 
135
/* Type for the md_write function.  */
 
136
typedef void (*gcry_md_write_t) (void *c, const void *buf, grub_size_t nbytes);
 
137
 
 
138
/* Type for the md_final function.  */
 
139
typedef void (*gcry_md_final_t) (void *c);
 
140
 
 
141
/* Type for the md_read function.  */
 
142
typedef unsigned char *(*gcry_md_read_t) (void *c);
 
143
 
 
144
typedef struct gcry_md_oid_spec
 
145
{
 
146
  const char *oidstring;
 
147
} gcry_md_oid_spec_t;
 
148
 
 
149
/* Module specification structure for message digests.  */
 
150
typedef struct gcry_md_spec
 
151
{
 
152
  const char *name;
 
153
  unsigned char *asnoid;
 
154
  int asnlen;
 
155
  gcry_md_oid_spec_t *oids;
 
156
  grub_size_t mdlen;
 
157
  gcry_md_init_t init;
 
158
  gcry_md_write_t write;
 
159
  gcry_md_final_t final;
 
160
  gcry_md_read_t read;
 
161
  grub_size_t contextsize; /* allocate this amount of context */
 
162
  /* Block size, needed for HMAC.  */
 
163
  grub_size_t blocksize;
 
164
  struct gcry_md_spec *next;
 
165
} gcry_md_spec_t;
 
166
 
 
167
struct grub_crypto_cipher_handle
 
168
{
 
169
  const struct gcry_cipher_spec *cipher;
 
170
  char ctx[0];
 
171
};
 
172
 
 
173
typedef struct grub_crypto_cipher_handle *grub_crypto_cipher_handle_t;
 
174
 
 
175
struct grub_crypto_hmac_handle;
 
176
 
 
177
const gcry_cipher_spec_t *
 
178
grub_crypto_lookup_cipher_by_name (const char *name);
 
179
 
 
180
grub_crypto_cipher_handle_t
 
181
grub_crypto_cipher_open (const struct gcry_cipher_spec *cipher);
 
182
 
 
183
gcry_err_code_t
 
184
grub_crypto_cipher_set_key (grub_crypto_cipher_handle_t cipher,
 
185
                            const unsigned char *key,
 
186
                            unsigned keylen);
 
187
 
 
188
void
 
189
grub_crypto_cipher_close (grub_crypto_cipher_handle_t cipher);
 
190
 
 
191
void
 
192
grub_crypto_xor (void *out, const void *in1, const void *in2, grub_size_t size);
 
193
 
 
194
gcry_err_code_t
 
195
grub_crypto_ecb_decrypt (grub_crypto_cipher_handle_t cipher,
 
196
                         void *out, void *in, grub_size_t size);
 
197
 
 
198
gcry_err_code_t
 
199
grub_crypto_ecb_encrypt (grub_crypto_cipher_handle_t cipher,
 
200
                         void *out, void *in, grub_size_t size);
 
201
gcry_err_code_t
 
202
grub_crypto_cbc_encrypt (grub_crypto_cipher_handle_t cipher,
 
203
                         void *out, void *in, grub_size_t size,
 
204
                         void *iv_in);
 
205
gcry_err_code_t
 
206
grub_crypto_cbc_decrypt (grub_crypto_cipher_handle_t cipher,
 
207
                         void *out, void *in, grub_size_t size,
 
208
                         void *iv);
 
209
void 
 
210
grub_cipher_register (gcry_cipher_spec_t *cipher);
 
211
void
 
212
grub_cipher_unregister (gcry_cipher_spec_t *cipher);
 
213
void 
 
214
grub_md_register (gcry_md_spec_t *digest);
 
215
void 
 
216
grub_md_unregister (gcry_md_spec_t *cipher);
 
217
void
 
218
grub_crypto_hash (const gcry_md_spec_t *hash, void *out, const void *in,
 
219
                  grub_size_t inlen);
 
220
const gcry_md_spec_t *
 
221
grub_crypto_lookup_md_by_name (const char *name);
 
222
 
 
223
grub_err_t
 
224
grub_crypto_gcry_error (gcry_err_code_t in);
 
225
 
 
226
void grub_burn_stack (grub_size_t size);
 
227
 
 
228
struct grub_crypto_hmac_handle *
 
229
grub_crypto_hmac_init (const struct gcry_md_spec *md,
 
230
                       const void *key, grub_size_t keylen);
 
231
void
 
232
grub_crypto_hmac_write (struct grub_crypto_hmac_handle *hnd, void *data,
 
233
                        grub_size_t datalen);
 
234
gcry_err_code_t
 
235
grub_crypto_hmac_fini (struct grub_crypto_hmac_handle *hnd, void *out);
 
236
 
 
237
gcry_err_code_t
 
238
grub_crypto_hmac_buffer (const struct gcry_md_spec *md,
 
239
                         const void *key, grub_size_t keylen,
 
240
                         void *data, grub_size_t datalen, void *out);
 
241
 
 
242
extern gcry_md_spec_t _gcry_digest_spec_md5;
 
243
extern gcry_md_spec_t _gcry_digest_spec_sha1;
 
244
extern gcry_md_spec_t _gcry_digest_spec_sha256;
 
245
extern gcry_md_spec_t _gcry_digest_spec_sha512;
 
246
#define GRUB_MD_MD5 ((const gcry_md_spec_t *) &_gcry_digest_spec_md5)
 
247
#define GRUB_MD_SHA1 ((const gcry_md_spec_t *) &_gcry_digest_spec_sha1)
 
248
#define GRUB_MD_SHA256 ((const gcry_md_spec_t *) &_gcry_digest_spec_sha256)
 
249
#define GRUB_MD_SHA512 ((const gcry_md_spec_t *) &_gcry_digest_spec_sha512)
 
250
 
 
251
/* Implement PKCS#5 PBKDF2 as per RFC 2898.  The PRF to use is HMAC variant
 
252
   of digest supplied by MD.  Inputs are the password P of length PLEN,
 
253
   the salt S of length SLEN, the iteration counter C (> 0), and the
 
254
   desired derived output length DKLEN.  Output buffer is DK which
 
255
   must have room for at least DKLEN octets.  The output buffer will
 
256
   be filled with the derived data.  */
 
257
gcry_err_code_t
 
258
grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
 
259
                    const grub_uint8_t *P, grub_size_t Plen,
 
260
                    const grub_uint8_t *S, grub_size_t Slen,
 
261
                    unsigned int c,
 
262
                    grub_uint8_t *DK, grub_size_t dkLen);
 
263
 
 
264
int
 
265
grub_crypto_memcmp (const void *a, const void *b, grub_size_t n);
 
266
 
 
267
int
 
268
grub_password_get (char buf[], unsigned buf_size);
 
269
 
 
270
/* For indistinguishibility.  */
 
271
#define GRUB_ACCESS_DENIED grub_error (GRUB_ERR_ACCESS_DENIED, "Access denied.")
 
272
 
 
273
extern void (*grub_crypto_autoload_hook) (const char *name);
 
274
 
 
275
#endif