Initial revision
[silc.git] / lib / silccrypt / rijndael.h
1 /*
2
3   rijndael.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * $Id$
22  * $Log$
23  * Revision 1.1  2000/06/27 11:36:55  priikone
24  * Initial revision
25  *
26  *
27  */
28
29 #ifndef RIJNDAEL_H
30 #define RIJNDAEL_H
31
32 #include "rijndael_internal.h"
33
34 /* 
35  * SILC Crypto API for Rijndael
36  */
37
38 /* Sets the key for the cipher. */
39
40 SILC_CIPHER_API_SET_KEY(rijndael)
41 {
42   rijndael_set_key((RijndaelContext *)context, (unsigned int *)key, keylen);
43   return TRUE;
44 }
45
46 /* Sets the string as a new key for the cipher. The string is first
47    hashed and then used as a new key. */
48
49 SILC_CIPHER_API_SET_KEY_WITH_STRING(rijndael)
50 {
51   /*  unsigned char key[md5_hash_len];
52   SilcMarsContext *ctx = (SilcMarsContext *)context;
53
54   make_md5_hash(string, &key);
55   memcpy(&ctx->key, mars_set_key(&key, keylen), keylen);
56   memset(&key, 'F', sizeoof(key));
57   */
58
59   return TRUE;
60 }
61
62 /* Returns the size of the cipher context. */
63
64 SILC_CIPHER_API_CONTEXT_LEN(rijnadel)
65 {
66   return sizeof(RijndaelContext);
67 }
68
69 /* Encrypts with the cipher in CBC mode. Source and destination buffers
70    maybe one and same. */
71
72 SILC_CIPHER_API_ENCRYPT_CBC(rijndael)
73 {
74   unsigned int *in, *out, *tiv;
75   unsigned int tmp[4];
76   int i;
77
78   in = (unsigned int *)src;
79   out = (unsigned int *)dst;
80   tiv = (unsigned int *)iv;
81
82   tmp[0] = in[0] ^ tiv[0];
83   tmp[1] = in[1] ^ tiv[1];
84   tmp[2] = in[2] ^ tiv[2];
85   tmp[3] = in[3] ^ tiv[3];
86   rijndael_encrypt((RijndaelContext *)context, tmp, out);
87   in += 4;
88   out += 4;
89
90   for (i = 16; i < len; i += 16) {
91     tmp[0] = in[0] ^ out[0 - 4];
92     tmp[1] = in[1] ^ out[1 - 4];
93     tmp[2] = in[2] ^ out[2 - 4];
94     tmp[3] = in[3] ^ out[3 - 4];
95     rijndael_encrypt((RijndaelContext *)context, tmp, out);
96     in += 4;
97     out += 4;
98   }
99
100   return TRUE;
101 }
102
103 /* Decrypts with the cipher in CBC mode. Source and destination buffers
104    maybe one and same. */
105
106 SILC_CIPHER_API_DECRYPT_CBC(rijndael)
107 {
108   unsigned int *in, *out, *tiv;
109   unsigned int tmp[4], tmp2[4];
110   int i;
111
112   in = (unsigned int *)src;
113   out = (unsigned int *)dst;
114   tiv = (unsigned int *)iv;
115
116   tmp[0] = in[0];
117   tmp[1] = in[1];
118   tmp[2] = in[2];
119   tmp[3] = in[3];
120   rijndael_decrypt((RijndaelContext *)context, in, out);
121   out[0] ^= tiv[0];
122   out[1] ^= tiv[1];
123   out[2] ^= tiv[2];
124   out[3] ^= tiv[3];
125   in += 4;
126   out += 4;
127
128   for (i = 16; i < len; i += 16) {
129     tmp2[0] = tmp[0];
130     tmp2[1] = tmp[1];
131     tmp2[2] = tmp[2];
132     tmp2[3] = tmp[3];
133     tmp[0] = in[0];
134     tmp[1] = in[1];
135     tmp[2] = in[2];
136     tmp[3] = in[3];
137     rijndael_decrypt((RijndaelContext *)context, in, out);
138     out[0] ^= tmp2[0];
139     out[1] ^= tmp2[1];
140     out[2] ^= tmp2[2];
141     out[3] ^= tmp2[3];
142     in += 4;
143     out += 4;
144   }
145
146   return TRUE;
147 }
148
149 #endif