Initial revision
[silc.git] / lib / silccrypt / loki.h
1 /*
2
3   loki.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 LOKI_H
30 #define LOKI_H
31
32 #include "loki_internal.h"
33
34 /* 
35  * SILC Crypto API for Loki
36  */
37
38 /* Sets the key for the cipher. */
39
40 inline int silc_loki_init(void *context, 
41                           const unsigned char *key, 
42                           size_t keylen)
43 {
44   loki_set_key((LokiContext *)context, (unsigned int *)key, keylen);
45   return 1;
46 }
47
48 /* Sets the string as a new key for the cipher. The string
49    is first hashed and then used as a new key. */
50
51 inline int silc_loki_set_string_as_key(void *context, 
52                                        const unsigned char *string,
53                                        size_t keylen)
54 {
55   /*  unsigned char key[md5_hash_len];
56   SilcMarsContext *ctx = (SilcMarsContext *)context;
57
58   make_md5_hash(string, &key);
59   memcpy(&ctx->key, mars_set_key(&key, keylen), keylen);
60   memset(&key, 'F', sizeoof(key));
61   */
62
63   return 1;
64 }
65
66 /* Returns the size of the cipher context. */
67
68 inline size_t silc_loki_context_len()
69 {
70   return sizeof(LokiContext);
71 }
72
73 /* Encrypts with the cipher in CBC mode. */
74
75 inline int silc_loki_encrypt_cbc(void *context,
76                                  const unsigned char *src,
77                                  unsigned char *dst,
78                                  size_t len,
79                                  unsigned char *iv)
80 {
81   unsigned int *in, *out, *tiv;
82   unsigned int tmp[4];
83   int i;
84
85   in = (unsigned int *)src;
86   out = (unsigned int *)dst;
87   tiv = (unsigned int *)iv;
88
89   tmp[0] = in[0] ^ tiv[0];
90   tmp[1] = in[1] ^ tiv[1];
91   tmp[2] = in[2] ^ tiv[2];
92   tmp[3] = in[3] ^ tiv[3];
93   loki_encrypt((LokiContext *)context, tmp, out);
94   in += 4;
95   out += 4;
96
97   for (i = 16; i < len; i += 16) {
98     tmp[0] = in[0] ^ out[0 - 4];
99     tmp[1] = in[1] ^ out[1 - 4];
100     tmp[2] = in[2] ^ out[2 - 4];
101     tmp[3] = in[3] ^ out[3 - 4];
102     loki_encrypt((LokiContext *)context, tmp, out);
103     in += 4;
104     out += 4;
105   }
106
107   return 1;
108 }
109
110 /* Decrypts with the cipher in CBC mode. */
111
112 inline int silc_loki_decrypt_cbc(void *context,
113                                  const unsigned char *src,
114                                  unsigned char *dst,
115                                  size_t len,
116                                  unsigned char *iv)
117 {
118   unsigned int *in, *out, *tiv;
119   int i;
120
121   in = (unsigned int *)src;
122   out = (unsigned int *)dst;
123   tiv = (unsigned int *)iv;
124
125   loki_decrypt((LokiContext *)context, in, out);
126   out[0] ^= tiv[0];
127   out[1] ^= tiv[1];
128   out[2] ^= tiv[2];
129   out[3] ^= tiv[3];
130   in += 4;
131   out += 4;
132
133   for (i = 16; i < len; i += 16) {
134     loki_decrypt((LokiContext *)context, in, out);
135     out[0] ^= in[0 - 4];
136     out[1] ^= in[1 - 4];
137     out[2] ^= in[2 - 4];
138     out[3] ^= in[3 - 4];
139     in += 4;
140     out += 4;
141   }
142
143   return 1;
144 }
145
146 #endif