updates. New data types.
[crypto.git] / lib / silccrypt / cast.h
1 /*
2
3   cast.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1999 - 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.2  2001/04/03 19:54:10  priikone
24  *      updates. New data types.
25  *
26  * Revision 1.1.1.1  2000/06/27 11:36:54  priikone
27  *      Importet from internal CVS/Added Log headers.
28  *
29  *
30  */
31
32 #ifndef CAST_H
33 #define CAST_H
34
35 #include "cast_internal.h"
36
37 /* 
38  * SILC Crypto API for Cast
39  */
40
41 /* Sets the key for the cipher. */
42
43 inline int silc_cast_init(void *context, 
44                           const unsigned char *key, 
45                           uint32 keylen)
46 {
47   cast_set_key((CastContext *)context, (uint32 *)key, keylen);
48   return 1;
49 }
50
51 /* Sets the string as a new key for the cipher. The string is first
52    hashed and then used as a new key. */
53
54 inline int silc_cast_set_string_as_key(void *context, 
55                                        const unsigned char *string,
56                                        uint32 stringlen)
57 {
58   /*  SilcHash hash;
59   unsigned char key[16];
60
61   silc_hash_alloc("md5", &hash);
62   hash->make_hash(hash, string, stringlen, key);
63
64   cast_set_key((CastContext *)context, (const u4byte *)key, sizeof(key));
65
66   silc_hash_free(hash);
67   memset(&key, 'F', sizeof(key));
68   */
69   return TRUE;
70 }
71
72 /* Returns the size of the cipher context. */
73
74 inline uint32 silc_cast_context_len()
75 {
76   return sizeof(CastContext);
77 }
78
79 /* Encrypts with the cipher in CBC mode. */
80
81 inline int silc_cast_encrypt_cbc(void *context,
82                                  const unsigned char *src,
83                                  unsigned char *dst,
84                                  uint32 len,
85                                  unsigned char *iv)
86 {
87   uint32 *in, *out, *tiv;
88   uint32 tmp[4];
89   int i;
90
91   in = (uint32 *)src;
92   out = (uint32 *)dst;
93   tiv = (uint32 *)iv;
94
95   tmp[0] = in[0] ^ tiv[0];
96   tmp[1] = in[1] ^ tiv[1];
97   tmp[2] = in[2] ^ tiv[2];
98   tmp[3] = in[3] ^ tiv[3];
99   cast_encrypt((CastContext *)context, tmp, out);
100   in += 4;
101   out += 4;
102
103   for (i = 16; i < len; i += 16) {
104     tmp[0] = in[0] ^ out[0 - 4];
105     tmp[1] = in[1] ^ out[1 - 4];
106     tmp[2] = in[2] ^ out[2 - 4];
107     tmp[3] = in[3] ^ out[3 - 4];
108     cast_encrypt((CastContext *)context, tmp, out);
109     in += 4;
110     out += 4;
111   }
112
113   return 1;
114 }
115
116 /* Decrypts with the cipher in CBC mode. */
117
118 inline int silc_cast_decrypt_cbc(void *context,
119                                  const unsigned char *src,
120                                  unsigned char *dst,
121                                  uint32 len,
122                                  unsigned char *iv)
123 {
124   uint32 *in, *out, *tiv;
125   int i;
126
127   in = (uint32 *)src;
128   out = (uint32 *)dst;
129   tiv = (uint32 *)iv;
130
131   cast_decrypt((CastContext *)context, in, out);
132   out[0] ^= tiv[0];
133   out[1] ^= tiv[1];
134   out[2] ^= tiv[2];
135   out[3] ^= tiv[3];
136   in += 4;
137   out += 4;
138
139   for (i = 16; i < len; i += 16) {
140     cast_decrypt((CastContext *)context, in, out);
141     out[0] ^= in[0 - 4];
142     out[1] ^= in[1 - 4];
143     out[2] ^= in[2 - 4];
144     out[3] ^= in[3 - 4];
145     in += 4;
146     out += 4;
147   }
148
149   return 1;
150 }
151
152 #endif