~ilya-yanok/ubuntu/precise/grub2/fix-for-948716

« back to all changes in this revision

Viewing changes to grub-core/lib/libgcrypt-grub/cipher/camellia-glue.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-01-17 13:43:06 UTC
  • mto: (17.6.26 experimental)
  • mto: This revision was merged to the branch mainline in revision 102.
  • Revision ID: james.westby@ubuntu.com-20110117134306-fy7qewn4s3qdx2pl
Tags: upstream-1.99~rc1
ImportĀ upstreamĀ versionĀ 1.99~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file was automatically imported with 
 
2
   import_gcry.py. Please don't modify it */
 
3
/* camellia-glue.c - Glue for the Camellia cipher
 
4
 * Copyright (C) 2007 Free Software Foundation, Inc.
 
5
 *
 
6
 * This file is part of Libgcrypt.
 
7
 *
 
8
 * Libgcrypt is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU Lesser General Public License as
 
10
 * published by the Free Software Foundation; either version 2.1 of
 
11
 * the License, or (at your option) any later version.
 
12
 *
 
13
 * Libgcrypt is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
21
 * 02110-1301, USA.
 
22
 */
 
23
 
 
24
/* I put all the libgcrypt-specific stuff in this file to keep the
 
25
   camellia.c/camellia.h files exactly as provided by NTT.  If they
 
26
   update their code, this should make it easier to bring the changes
 
27
   in. - dshaw
 
28
 
 
29
   There is one small change which needs to be done: Include the
 
30
   following code at the top of camellia.h: */
 
31
#if 0
 
32
 
 
33
/* To use Camellia with libraries it is often useful to keep the name
 
34
 * space of the library clean.  The following macro is thus useful:
 
35
 *
 
36
 *     #define CAMELLIA_EXT_SYM_PREFIX foo_
 
37
 *  
 
38
 * This prefixes all external symbols with "foo_".
 
39
 */
 
40
#ifdef HAVE_CONFIG_H
 
41
#endif
 
42
#ifdef CAMELLIA_EXT_SYM_PREFIX
 
43
#define CAMELLIA_PREFIX1(x,y) x ## y
 
44
#define CAMELLIA_PREFIX2(x,y) CAMELLIA_PREFIX1(x,y)
 
45
#define CAMELLIA_PREFIX(x)    CAMELLIA_PREFIX2(CAMELLIA_EXT_SYM_PREFIX,x)
 
46
#define Camellia_Ekeygen      CAMELLIA_PREFIX(Camellia_Ekeygen)
 
47
#define Camellia_EncryptBlock CAMELLIA_PREFIX(Camellia_EncryptBlock)
 
48
#define Camellia_DecryptBlock CAMELLIA_PREFIX(Camellia_DecryptBlock)
 
49
#define camellia_decrypt128   CAMELLIA_PREFIX(camellia_decrypt128)
 
50
#define camellia_decrypt256   CAMELLIA_PREFIX(camellia_decrypt256)
 
51
#define camellia_encrypt128   CAMELLIA_PREFIX(camellia_encrypt128)
 
52
#define camellia_encrypt256   CAMELLIA_PREFIX(camellia_encrypt256)
 
53
#define camellia_setup128     CAMELLIA_PREFIX(camellia_setup128)
 
54
#define camellia_setup192     CAMELLIA_PREFIX(camellia_setup192) 
 
55
#define camellia_setup256     CAMELLIA_PREFIX(camellia_setup256)
 
56
#endif /*CAMELLIA_EXT_SYM_PREFIX*/
 
57
 
 
58
#endif /* Code sample. */
 
59
 
 
60
 
 
61
#include "types.h"
 
62
#include "g10lib.h"
 
63
#include "cipher.h"
 
64
#include "camellia.h"
 
65
 
 
66
typedef struct
 
67
{
 
68
  int keybitlength;
 
69
  KEY_TABLE_TYPE keytable;
 
70
} CAMELLIA_context;
 
71
 
 
72
 
 
73
static gcry_err_code_t
 
74
camellia_setkey(void *c, const byte *key, unsigned keylen)
 
75
{
 
76
  CAMELLIA_context *ctx=c;
 
77
  static int initialized=0;
 
78
  static const char *selftest_failed=NULL;
 
79
 
 
80
  if(keylen!=16 && keylen!=24 && keylen!=32)
 
81
    return GPG_ERR_INV_KEYLEN;
 
82
 
 
83
  if(!initialized)
 
84
    {
 
85
      initialized=1;
 
86
      selftest_failed=selftest();
 
87
      if(selftest_failed)
 
88
        log_error("%s\n",selftest_failed);
 
89
    }
 
90
 
 
91
  if(selftest_failed)
 
92
    return GPG_ERR_SELFTEST_FAILED;
 
93
 
 
94
  ctx->keybitlength=keylen*8;
 
95
  Camellia_Ekeygen(ctx->keybitlength,key,ctx->keytable);
 
96
  _gcry_burn_stack
 
97
    ((19+34+34)*sizeof(u32)+2*sizeof(void*) /* camellia_setup256 */
 
98
     +(4+32)*sizeof(u32)+2*sizeof(void*)    /* camellia_setup192 */
 
99
     +0+sizeof(int)+2*sizeof(void*)         /* Camellia_Ekeygen */
 
100
     +3*2*sizeof(void*)                     /* Function calls.  */
 
101
     );  
 
102
 
 
103
  return 0;
 
104
}
 
105
 
 
106
static void
 
107
camellia_encrypt(void *c, byte *outbuf, const byte *inbuf)
 
108
{
 
109
  CAMELLIA_context *ctx=c;
 
110
 
 
111
  Camellia_EncryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
 
112
  _gcry_burn_stack
 
113
    (sizeof(int)+2*sizeof(unsigned char *)+sizeof(KEY_TABLE_TYPE)
 
114
     +4*sizeof(u32)
 
115
     +2*sizeof(u32*)+4*sizeof(u32)
 
116
     +2*2*sizeof(void*) /* Function calls.  */
 
117
    );
 
118
}
 
119
 
 
120
static void
 
121
camellia_decrypt(void *c, byte *outbuf, const byte *inbuf)
 
122
{
 
123
  CAMELLIA_context *ctx=c;
 
124
 
 
125
  Camellia_DecryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf);
 
126
  _gcry_burn_stack
 
127
    (sizeof(int)+2*sizeof(unsigned char *)+sizeof(KEY_TABLE_TYPE)
 
128
     +4*sizeof(u32)
 
129
     +2*sizeof(u32*)+4*sizeof(u32)
 
130
     +2*2*sizeof(void*) /* Function calls.  */
 
131
    );
 
132
}
 
133
 
 
134
 
 
135
/* These oids are from
 
136
   <http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications_oid.html>,
 
137
   retrieved May 1, 2007. */
 
138
 
 
139
static gcry_cipher_oid_spec_t camellia128_oids[] =
 
140
  {
 
141
    {"1.2.392.200011.61.1.1.1.2", GCRY_CIPHER_MODE_CBC},
 
142
    {"0.3.4401.5.3.1.9.1", GCRY_CIPHER_MODE_ECB},
 
143
    {"0.3.4401.5.3.1.9.3", GCRY_CIPHER_MODE_OFB},
 
144
    {"0.3.4401.5.3.1.9.4", GCRY_CIPHER_MODE_CFB},
 
145
    { NULL }
 
146
  };
 
147
 
 
148
static gcry_cipher_oid_spec_t camellia192_oids[] =
 
149
  {
 
150
    {"1.2.392.200011.61.1.1.1.3", GCRY_CIPHER_MODE_CBC},
 
151
    {"0.3.4401.5.3.1.9.21", GCRY_CIPHER_MODE_ECB},
 
152
    {"0.3.4401.5.3.1.9.23", GCRY_CIPHER_MODE_OFB},
 
153
    {"0.3.4401.5.3.1.9.24", GCRY_CIPHER_MODE_CFB},
 
154
    { NULL }
 
155
  };
 
156
 
 
157
static gcry_cipher_oid_spec_t camellia256_oids[] =
 
158
  {
 
159
    {"1.2.392.200011.61.1.1.1.4", GCRY_CIPHER_MODE_CBC},
 
160
    {"0.3.4401.5.3.1.9.41", GCRY_CIPHER_MODE_ECB},
 
161
    {"0.3.4401.5.3.1.9.43", GCRY_CIPHER_MODE_OFB},
 
162
    {"0.3.4401.5.3.1.9.44", GCRY_CIPHER_MODE_CFB},
 
163
    { NULL }
 
164
  };
 
165
 
 
166
gcry_cipher_spec_t _gcry_cipher_spec_camellia128 =
 
167
  {
 
168
    "CAMELLIA128",NULL,camellia128_oids,CAMELLIA_BLOCK_SIZE,128,
 
169
    sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
 
170
  };
 
171
 
 
172
gcry_cipher_spec_t _gcry_cipher_spec_camellia192 =
 
173
  {
 
174
    "CAMELLIA192",NULL,camellia192_oids,CAMELLIA_BLOCK_SIZE,192,
 
175
    sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
 
176
  };
 
177
 
 
178
gcry_cipher_spec_t _gcry_cipher_spec_camellia256 =
 
179
  {
 
180
    "CAMELLIA256",NULL,camellia256_oids,CAMELLIA_BLOCK_SIZE,256,
 
181
    sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt
 
182
  };
 
183
 
 
184
 
 
185
GRUB_MOD_INIT(gcry_camellia)
 
186
{
 
187
  grub_cipher_register (&_gcry_cipher_spec_camellia128);
 
188
  grub_cipher_register (&_gcry_cipher_spec_camellia192);
 
189
  grub_cipher_register (&_gcry_cipher_spec_camellia256);
 
190
}
 
191
 
 
192
GRUB_MOD_FINI(gcry_camellia)
 
193
{
 
194
  grub_cipher_unregister (&_gcry_cipher_spec_camellia128);
 
195
  grub_cipher_unregister (&_gcry_cipher_spec_camellia192);
 
196
  grub_cipher_unregister (&_gcry_cipher_spec_camellia256);
 
197
}