summaryrefslogtreecommitdiff
path: root/sys/miscfs/tcfs/tcfs_cipher_TDES.c
blob: f90d697336d4de0f7ea1d1573e81ef7cd94e624f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*	$OpenBSD: tcfs_cipher_TDES.c,v 1.3 2000/06/17 20:25:54 provos Exp $	*/
/*
 * Copyright 2000 The TCFS Project at http://tcfs.dia.unisa.it/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the authors may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>

#include <miscfs/tcfs/tcfs_cipher.h>

#include <crypto/des_locl.h>
#include <crypto/des.h>

void *
TDES_init_key (char *key)
{
        des_key_schedule *ks;

        ks = (des_key_schedule *)malloc (2 * sizeof (des_key_schedule),
					 M_FREE, M_NOWAIT);
        if (!ks) 
                return NULL;

        des_set_key ((des_cblock *)key, ks[0]);
        des_set_key ((des_cblock *)(key + 8), ks[1]);

        return (void *)ks;
}

void
TDES_cleanup_key(void *k)
{
/*	tcfs_keytab_dispnode does it
	free((des_key_schedule*)k,M_FREE);
*/
}

void
TDES_encrypt(char *block, int nb, void *key)
{
        u_int32_t * xi;
        int i;
        char *tmp;
        des_key_schedule *ks = (des_key_schedule *)key;

        xi = (u_int32_t *)block;
        tmp = block;
        des_ecb3_encrypt((des_cblock *)tmp, (des_cblock *)tmp,
			 ks[0],ks[1],ks[0],DES_ENCRYPT);
        tmp += 8;
        for (i = 1;i < nb/8;i++) {
                *(xi+2) ^= *xi;
                *(xi+3) ^= *(xi + 1);
                des_ecb3_encrypt((des_cblock *)tmp, (des_cblock *)tmp,
				 ks[0], ks[1], ks[0], DES_ENCRYPT);
                tmp += 8;
                xi += 2;
        }
}

void
TDES_decrypt(char *block, int nb, void *key)
{
        u_int32_t * xi, xo[2], xa[2];
        int i;
        char *tmp;
        des_key_schedule *ks = (des_key_schedule *)key;

        xi = (u_int32_t *)block;
        tmp = block;
        xo[0] = *xi; xo[1] = *(xi+1);
        des_ecb3_encrypt((des_cblock *)tmp, (des_cblock *)tmp,
			 ks[0], ks[1], ks[0], DES_DECRYPT);
        tmp += 8;
        xi = (u_int32_t *)tmp;
        for (i = 1;i < nb/8; i++) {
                xa[0] = *xi; xa[1] = *(xi+1);
                des_ecb3_encrypt((des_cblock *)tmp, (des_cblock *)tmp,
				 ks[0], ks[1], ks[0], DES_DECRYPT);
                *(xi) ^= xo[0];
                *(xi+1)^= xo[1];
                xo[0] = xa[0];  
                xo[1] = xa[1];
                tmp += 8;
                xi += 2;
        }
}