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