1 | /* -*- c -*- |
---|
2 | * |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * TRI's Simple Stream encryption system implementation |
---|
5 | * ---------------------------------------------------------------------- |
---|
6 | * Created : Fri Apr 14 14:20:00 1995 tri |
---|
7 | * Last modified: Wed Jul 12 21:58:55 1995 ylo |
---|
8 | * ---------------------------------------------------------------------- |
---|
9 | * Copyright (c) 1995 |
---|
10 | * Timo J. Rinne <tri@iki.fi> and Cirion oy. |
---|
11 | * |
---|
12 | * Address: Cirion oy, PO-BOX 250, 00121 HELSINKI, Finland |
---|
13 | * |
---|
14 | * Even though this code is copyrighted property of the author, it can |
---|
15 | * still be used for non-commercial purposes under following conditions: |
---|
16 | * |
---|
17 | * 1) This copyright notice is not removed. |
---|
18 | * 2) Source code follows any distribution of the software |
---|
19 | * if possible. |
---|
20 | * 3) Copyright notice above is found in the documentation |
---|
21 | * of the distributed software. |
---|
22 | * |
---|
23 | * For possibility to use this source code for commercial product, |
---|
24 | * please contact address above. |
---|
25 | * |
---|
26 | * Any express or implied warranties are disclaimed. In no event |
---|
27 | * shall the author be liable for any damages caused (directly or |
---|
28 | * otherwise) by the use of this software. |
---|
29 | * |
---|
30 | * Permission granted to Mr. Tatu Ylonen <ylo@cs.hut.fi> to include this |
---|
31 | * code into SSH (Secure Shell). Permission is granted to anyone to |
---|
32 | * use and distribute this code for any purpose as part of that product. |
---|
33 | * ---------------------------------------------------------------------- |
---|
34 | */ |
---|
35 | #define __TSS_C__ 1 |
---|
36 | |
---|
37 | #include "includes.h" |
---|
38 | #include "md5.h" |
---|
39 | #include "ssh.h" |
---|
40 | #include "tss.h" |
---|
41 | |
---|
42 | int TSS_Init(struct tss_context *context, |
---|
43 | const unsigned char *key, |
---|
44 | int keylen) |
---|
45 | { |
---|
46 | int i; |
---|
47 | struct MD5Context mdctx; |
---|
48 | |
---|
49 | if((!context) || (!key) || (keylen <= 0)) |
---|
50 | return 0; |
---|
51 | context->keyidx = 0; |
---|
52 | (context->key)[0] = (unsigned char)(keylen & 0xff); |
---|
53 | (context->key)[1] = (unsigned char)((keylen >> 8) & 0xff); |
---|
54 | for(i = 2; i < sizeof(context->key); i++) |
---|
55 | (context->key)[i] = key[i % keylen]; |
---|
56 | for(i = 0; i <= 16; i++) { |
---|
57 | MD5Init(&mdctx); |
---|
58 | MD5Update(&mdctx, context->key, (i + 1) * 16); |
---|
59 | MD5Final(&((context->key)[i * 16]), &mdctx); |
---|
60 | } |
---|
61 | for(i = 0; i < sizeof(context->key); i++) { |
---|
62 | (context->key)[(i + 1) & TSS_POOL_MASK] ^= (context->key)[i]; |
---|
63 | (context->key)[(i + 2 + ((context->key)[i])) & TSS_POOL_MASK] ^= |
---|
64 | ((context->key)[i] << 6) | |
---|
65 | ((context->key)[(i + 1) & TSS_POOL_MASK] >> 2); |
---|
66 | } |
---|
67 | (context->salt)[0] = (unsigned char)(keylen & 0xff); |
---|
68 | (context->salt)[1] = (unsigned char)((keylen >> 8) & 0xff); |
---|
69 | for(i = 2; i < sizeof(context->salt); i++) |
---|
70 | (context->salt)[i] = key[i % keylen]; |
---|
71 | return 1; |
---|
72 | } |
---|
73 | |
---|
74 | static void TSS_Resalt(struct tss_context *context) |
---|
75 | { |
---|
76 | int i; |
---|
77 | struct MD5Context mdctx; |
---|
78 | |
---|
79 | MD5Init(&mdctx); |
---|
80 | MD5Update(&mdctx, context->salt, sizeof(context->salt)); |
---|
81 | MD5Update(&mdctx, &((context->key)[sizeof(context->key) - 16]), 16); |
---|
82 | MD5Final(context->salt, &mdctx); |
---|
83 | for(i = 0; i < 16; i++) |
---|
84 | (context->key)[i] ^= (context->salt)[i]; |
---|
85 | return; |
---|
86 | } |
---|
87 | |
---|
88 | int TSS_Encrypt(struct tss_context *context, |
---|
89 | unsigned char *data, |
---|
90 | unsigned int len) |
---|
91 | { |
---|
92 | unsigned int i; |
---|
93 | |
---|
94 | for(i = 0; i < len; i++) { |
---|
95 | if(!(context->keyidx = ((context->keyidx + 1) & TSS_POOL_MASK))) |
---|
96 | TSS_Resalt(context); |
---|
97 | (context->key)[(context->keyidx + 1) & TSS_POOL_MASK] ^= data[i]; |
---|
98 | (context->key)[(context->keyidx + 3) & TSS_POOL_MASK] ^= |
---|
99 | (data[i] << 6) | (data[i] >> 2); |
---|
100 | data[i] ^= (context->key)[context->keyidx]; |
---|
101 | (context->key)[(context->keyidx + 2) & TSS_POOL_MASK] ^= data[i]; |
---|
102 | (context->key)[(context->keyidx + 4) & TSS_POOL_MASK] ^= |
---|
103 | (data[i] << 3) | (data[i] >> 5); |
---|
104 | } |
---|
105 | return 1; |
---|
106 | } |
---|
107 | |
---|
108 | int TSS_Decrypt(struct tss_context *context, |
---|
109 | unsigned char *data, |
---|
110 | unsigned int len) |
---|
111 | { |
---|
112 | unsigned int i; |
---|
113 | |
---|
114 | for(i = 0; i < len; i++) { |
---|
115 | if(!(context->keyidx = ((context->keyidx + 1) & TSS_POOL_MASK))) |
---|
116 | TSS_Resalt(context); |
---|
117 | (context->key)[(context->keyidx + 2) & TSS_POOL_MASK] ^= data[i]; |
---|
118 | (context->key)[(context->keyidx + 4) & TSS_POOL_MASK] ^= |
---|
119 | (data[i] << 3) | (data[i] >> 5); |
---|
120 | data[i] ^= (context->key)[context->keyidx]; |
---|
121 | (context->key)[(context->keyidx + 1) & TSS_POOL_MASK] ^= data[i]; |
---|
122 | (context->key)[(context->keyidx + 3) & TSS_POOL_MASK] ^= |
---|
123 | (data[i] << 6) | (data[i] >> 2); |
---|
124 | } |
---|
125 | return 1; |
---|
126 | } |
---|
127 | |
---|
128 | /* EOF (tss.c) */ |
---|