5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1999 - 2000 Pekka Riikonen
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.
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.
23 * Revision 1.1.1.1 2000/06/27 11:36:54 priikone
24 * Importet from internal CVS/Added Log headers.
32 #include "cast_internal.h"
35 * SILC Crypto API for Cast
38 /* Sets the key for the cipher. */
40 inline int silc_cast_init(void *context,
41 const unsigned char *key,
44 cast_set_key((CastContext *)context, (unsigned int *)key, keylen);
48 /* Sets the string as a new key for the cipher. The string is first
49 hashed and then used as a new key. */
51 inline int silc_cast_set_string_as_key(void *context,
52 const unsigned char *string,
53 unsigned int stringlen)
56 unsigned char key[16];
58 silc_hash_alloc("md5", &hash);
59 hash->make_hash(hash, string, stringlen, key);
61 cast_set_key((CastContext *)context, (const u4byte *)key, sizeof(key));
64 memset(&key, 'F', sizeof(key));
69 /* Returns the size of the cipher context. */
71 inline unsigned int silc_cast_context_len()
73 return sizeof(CastContext);
76 /* Encrypts with the cipher in CBC mode. */
78 inline int silc_cast_encrypt_cbc(void *context,
79 const unsigned char *src,
84 unsigned int *in, *out, *tiv;
88 in = (unsigned int *)src;
89 out = (unsigned int *)dst;
90 tiv = (unsigned int *)iv;
92 tmp[0] = in[0] ^ tiv[0];
93 tmp[1] = in[1] ^ tiv[1];
94 tmp[2] = in[2] ^ tiv[2];
95 tmp[3] = in[3] ^ tiv[3];
96 cast_encrypt((CastContext *)context, tmp, out);
100 for (i = 16; i < len; i += 16) {
101 tmp[0] = in[0] ^ out[0 - 4];
102 tmp[1] = in[1] ^ out[1 - 4];
103 tmp[2] = in[2] ^ out[2 - 4];
104 tmp[3] = in[3] ^ out[3 - 4];
105 cast_encrypt((CastContext *)context, tmp, out);
113 /* Decrypts with the cipher in CBC mode. */
115 inline int silc_cast_decrypt_cbc(void *context,
116 const unsigned char *src,
121 unsigned int *in, *out, *tiv;
124 in = (unsigned int *)src;
125 out = (unsigned int *)dst;
126 tiv = (unsigned int *)iv;
128 cast_decrypt((CastContext *)context, in, out);
136 for (i = 16; i < len; i += 16) {
137 cast_decrypt((CastContext *)context, in, out);