Initial revision
[silc.git] / lib / silccrypt / crypton.h
1 /*
2
3   crypton.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.1  2000/06/27 11:36:54  priikone
24  * Initial revision
25  *
26  *
27  */
28
29 #ifndef CRYPTON_H
30 #define CRYPTON_H
31
32 #include "crypton_internal.h"
33
34 /* 
35  * SILC Crypto API for Crypton
36  */
37
38 /* Sets the key for the cipher. */
39
40 SILC_CIPHER_API_SET_KEY(crypton)
41 {
42   crypton_set_key((CryptonContext *)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(crypton)
50 {
51   SilcHash hash;
52   unsigned char key[16];
53
54   silc_hash_alloc("md5", &hash);
55   hash->make_hash(hash, string, stringlen, key);
56
57   crypton_set_key((CryptonContext *)context, key, sizeof(key));
58
59   silc_hash_free(hash);
60   memset(&key, 'F', sizeof(key));
61
62   return TRUE;
63 }
64
65 /* Returns the size of the cipher context. */
66
67 SILC_CIPHER_API_CONTEXT_LEN(crypton)
68 {
69   return sizeof(CryptonContext);
70 }
71
72 /* Encrypts with the cipher in CBC mode. */
73
74 SILC_CIPHER_API_ENCRYPT_CBC(crypton)
75 {
76   unsigned int *in, *out, *tiv;
77   unsigned int tmp[4];
78   int i;
79
80   in = (unsigned int *)src;
81   out = (unsigned int *)dst;
82   tiv = (unsigned int *)iv;
83
84   tmp[0] = in[0] ^ tiv[0];
85   tmp[1] = in[1] ^ tiv[1];
86   tmp[2] = in[2] ^ tiv[2];
87   tmp[3] = in[3] ^ tiv[3];
88   crypton_encrypt((CryptonContext *)context, tmp, out);
89   in += 4;
90   out += 4;
91
92   for (i = 16; i < len; i += 16) {
93     tmp[0] = in[0] ^ out[0 - 4];
94     tmp[1] = in[1] ^ out[1 - 4];
95     tmp[2] = in[2] ^ out[2 - 4];
96     tmp[3] = in[3] ^ out[3 - 4];
97     crypton_encrypt((CryptonContext *)context, tmp, out);
98     in += 4;
99     out += 4;
100   }
101
102   return TRUE;
103 }
104
105 /* Decrypts with the cipher in CBC mode. */
106
107 SILC_CIPHER_API_DECRYPT_CBC(crypton)
108 {
109   unsigned int *in, *out, *tiv;
110   int i;
111
112   in = (unsigned int *)src;
113   out = (unsigned int *)dst;
114   tiv = (unsigned int *)iv;
115
116   crypton_decrypt((CryptonContext *)context, in, out);
117   out[0] ^= tiv[0];
118   out[1] ^= tiv[1];
119   out[2] ^= tiv[2];
120   out[3] ^= tiv[3];
121   in += 4;
122   out += 4;
123
124   for (i = 16; i < len; i += 16) {
125     crypton_decrypt((CryptonContext *)context, in, out);
126     out[0] ^= in[0 - 4];
127     out[1] ^= in[1 - 4];
128     out[2] ^= in[2 - 4];
129     out[3] ^= in[3 - 4];
130     in += 4;
131     out += 4;
132   }
133
134   return TRUE;
135 }
136
137 #endif