~ubuntu-branches/ubuntu/maverick/pdns/maverick-updates

« back to all changes in this revision

Viewing changes to pdns/aes/aes.h

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Haas
  • Date: 2009-02-25 23:25:51 UTC
  • mfrom: (1.1.7 upstream) (12.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090225232551-ts3d9k9q0ti442i9
Tags: 2.9.22-1
New upstream version (closes: #513409).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 ---------------------------------------------------------------------------
 
3
 Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.
 
4
 
 
5
 LICENSE TERMS
 
6
 
 
7
 The free distribution and use of this software is allowed (with or without
 
8
 changes) provided that:
 
9
 
 
10
  1. source code distributions include the above copyright notice, this
 
11
     list of conditions and the following disclaimer;
 
12
 
 
13
  2. binary distributions include the above copyright notice, this list
 
14
     of conditions and the following disclaimer in their documentation;
 
15
 
 
16
  3. the name of the copyright holder is not used to endorse products
 
17
     built using this software without specific written permission.
 
18
 
 
19
 DISCLAIMER
 
20
 
 
21
 This software is provided 'as is' with no explicit or implied warranties
 
22
 in respect of its properties, including, but not limited to, correctness
 
23
 and/or fitness for purpose.
 
24
 ---------------------------------------------------------------------------
 
25
 Issue Date: 20/12/2007
 
26
 
 
27
 This file contains the definitions required to use AES in C. See aesopt.h
 
28
 for optimisation details.
 
29
*/
 
30
 
 
31
#ifndef _AES_H
 
32
#define _AES_H
 
33
 
 
34
#include <stdlib.h>
 
35
 
 
36
/*  This include is used to find 8 & 32 bit unsigned integer types  */
 
37
#include "brg_types.h"
 
38
 
 
39
#if defined(__cplusplus)
 
40
extern "C"
 
41
{
 
42
#endif
 
43
 
 
44
#define AES_128     /* if a fast 128 bit key scheduler is needed    */
 
45
#define AES_192     /* if a fast 192 bit key scheduler is needed    */
 
46
#define AES_256     /* if a fast 256 bit key scheduler is needed    */
 
47
#define AES_VAR     /* if variable key size scheduler is needed     */
 
48
#define AES_MODES   /* if support is needed for modes               */
 
49
 
 
50
/* The following must also be set in assembler files if being used  */
 
51
 
 
52
#define AES_ENCRYPT /* if support for encryption is needed          */
 
53
#define AES_DECRYPT /* if support for decryption is needed          */
 
54
#define AES_REV_DKS /* define to reverse decryption key schedule    */
 
55
 
 
56
#define AES_BLOCK_SIZE  16  /* the AES block size in bytes          */
 
57
#define N_COLS           4  /* the number of columns in the state   */
 
58
 
 
59
/* The key schedule length is 11, 13 or 15 16-byte blocks for 128,  */
 
60
/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes  */
 
61
/* or 44, 52 or 60 32-bit words.                                    */
 
62
 
 
63
#if defined( AES_VAR ) || defined( AES_256 )
 
64
#define KS_LENGTH       60
 
65
#elif defined( AES_192 )
 
66
#define KS_LENGTH       52
 
67
#else
 
68
#define KS_LENGTH       44
 
69
#endif
 
70
 
 
71
#define AES_RETURN INT_RETURN
 
72
 
 
73
/* the character array 'inf' in the following structures is used    */
 
74
/* to hold AES context information. This AES code uses cx->inf.b[0] */
 
75
/* to hold the number of rounds multiplied by 16. The other three   */
 
76
/* elements can be used by code that implements additional modes    */
 
77
 
 
78
typedef union
 
79
{   uint_32t l;
 
80
    uint_8t b[4];
 
81
} aes_inf;
 
82
 
 
83
typedef struct
 
84
{   uint_32t ks[KS_LENGTH];
 
85
    aes_inf inf;
 
86
} aes_encrypt_ctx;
 
87
 
 
88
typedef struct
 
89
{   uint_32t ks[KS_LENGTH];
 
90
    aes_inf inf;
 
91
} aes_decrypt_ctx;
 
92
 
 
93
/* This routine must be called before first use if non-static       */
 
94
/* tables are being used                                            */
 
95
 
 
96
AES_RETURN aes_init(void);
 
97
 
 
98
/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
 
99
/* those in the range 128 <= key_len <= 256 are given in bits       */
 
100
 
 
101
#if defined( AES_ENCRYPT )
 
102
 
 
103
#if defined( AES_128 ) || defined( AES_VAR)
 
104
AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
 
105
#endif
 
106
 
 
107
#if defined( AES_192 ) || defined( AES_VAR)
 
108
AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
 
109
#endif
 
110
 
 
111
#if defined( AES_256 ) || defined( AES_VAR)
 
112
AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
 
113
#endif
 
114
 
 
115
#if defined( AES_VAR )
 
116
AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
 
117
#endif
 
118
 
 
119
AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
 
120
 
 
121
#endif
 
122
 
 
123
#if defined( AES_DECRYPT )
 
124
 
 
125
#if defined( AES_128 ) || defined( AES_VAR)
 
126
AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
 
127
#endif
 
128
 
 
129
#if defined( AES_192 ) || defined( AES_VAR)
 
130
AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
 
131
#endif
 
132
 
 
133
#if defined( AES_256 ) || defined( AES_VAR)
 
134
AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
 
135
#endif
 
136
 
 
137
#if defined( AES_VAR )
 
138
AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
 
139
#endif
 
140
 
 
141
AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
 
142
 
 
143
#endif
 
144
 
 
145
#if defined( AES_MODES )
 
146
 
 
147
/* Multiple calls to the following subroutines for multiple block   */
 
148
/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
 
149
/* long messages incremantally provided that the context AND the iv */
 
150
/* are preserved between all such calls.  For the ECB and CBC modes */
 
151
/* each individual call within a series of incremental calls must   */
 
152
/* process only full blocks (i.e. len must be a multiple of 16) but */
 
153
/* the CFB, OFB and CTR mode calls can handle multiple incremental  */
 
154
/* calls of any length. Each mode is reset when a new AES key is    */
 
155
/* set but ECB and CBC operations can be reset without setting a    */
 
156
/* new key by setting a new IV value.  To reset CFB, OFB and CTR    */
 
157
/* without setting the key, aes_mode_reset() must be called and the */
 
158
/* IV must be set.  NOTE: All these calls update the IV on exit so  */
 
159
/* this has to be reset if a new operation with the same IV as the  */
 
160
/* previous one is required (or decryption follows encryption with  */
 
161
/* the same IV array).                                              */
 
162
 
 
163
AES_RETURN aes_test_alignment_detection(unsigned int n);
 
164
 
 
165
AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
 
166
                    int len, const aes_encrypt_ctx cx[1]);
 
167
 
 
168
AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
 
169
                    int len, const aes_decrypt_ctx cx[1]);
 
170
 
 
171
AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
 
172
                    int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
 
173
 
 
174
AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
 
175
                    int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
 
176
 
 
177
AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]);
 
178
 
 
179
AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
 
180
                    int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
 
181
 
 
182
AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
 
183
                    int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
 
184
 
 
185
#define aes_ofb_encrypt aes_ofb_crypt
 
186
#define aes_ofb_decrypt aes_ofb_crypt
 
187
 
 
188
AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
 
189
                    int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
 
190
 
 
191
typedef void cbuf_inc(unsigned char *cbuf);
 
192
 
 
193
#define aes_ctr_encrypt aes_ctr_crypt
 
194
#define aes_ctr_decrypt aes_ctr_crypt
 
195
 
 
196
AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
 
197
            int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
 
198
 
 
199
#endif
 
200
 
 
201
#if defined(__cplusplus)
 
202
}
 
203
#endif
 
204
 
 
205
#endif