OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [demos/] [vnc/] [libvncauth/] [vncauth.c] - Blame information for rev 365

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 *  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
3
 *
4
 *  This is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This software is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program; if not, write to the Free Software
16
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17
 *  USA.
18
 */
19
 
20
/*
21
 * vncauth.c - Functions for VNC password management and authentication.
22
 */
23
 
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <time.h>
28
#include <sys/types.h>
29
#include <sys/stat.h>
30
#include <vncauth.h>
31
#include <d3des.h>
32
 
33
 
34
/*
35
 * We use a fixed key to store passwords, since we assume that our local
36
 * file system is secure but nonetheless don't want to store passwords
37
 * as plaintext.
38
 */
39
 
40
unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};
41
 
42
 
43
/*
44
 * Encrypt a password and store it in a file.  Returns 0 if successful,
45
 * 1 if the file could not be written.
46
 */
47
 
48
int
49
vncEncryptAndStorePasswd(char *passwd, char *fname)
50
{
51
    FILE *fp;
52
    int i;
53
    unsigned char encryptedPasswd[8];
54
 
55
    if ((fp = fopen(fname,"w")) == NULL) return 1;
56
 
57
    chmod(fname, S_IRUSR|S_IWUSR);
58
 
59
    /* pad password with nulls */
60
 
61
    for (i = 0; i < 8; i++) {
62
        if (i < strlen(passwd)) {
63
            encryptedPasswd[i] = passwd[i];
64
        } else {
65
            encryptedPasswd[i] = 0;
66
        }
67
    }
68
 
69
    /* Do encryption in-place - this way we overwrite our copy of the plaintext
70
       password */
71
 
72
    deskey(fixedkey, EN0);
73
    des(encryptedPasswd, encryptedPasswd);
74
 
75
    for (i = 0; i < 8; i++) {
76
        putc(encryptedPasswd[i], fp);
77
    }
78
 
79
    fclose(fp);
80
    return 0;
81
}
82
 
83
 
84
/*
85
 * Decrypt a password from a file.  Returns a pointer to a newly allocated
86
 * string containing the password or a null pointer if the password could
87
 * not be retrieved for some reason.
88
 */
89
 
90
char *
91
vncDecryptPasswdFromFile(char *fname)
92
{
93
    FILE *fp;
94
    int i, ch;
95
    unsigned char *passwd = (unsigned char *)malloc(9);
96
 
97
    if ((fp = fopen(fname,"r")) == NULL) return NULL;
98
 
99
    for (i = 0; i < 8; i++) {
100
        ch = getc(fp);
101
        if (ch == EOF) {
102
            fclose(fp);
103
            return NULL;
104
        }
105
        passwd[i] = ch;
106
    }
107
 
108
    fclose(fp);
109
 
110
    deskey(fixedkey, DE1);
111
    des(passwd, passwd);
112
 
113
    passwd[8] = 0;
114
 
115
    return (char *)passwd;
116
}
117
 
118
 
119
/*
120
 * Generate CHALLENGESIZE random bytes for use in challenge-response
121
 * authentication.
122
 */
123
 
124
void
125
vncRandomBytes(unsigned char *bytes)
126
{
127
    int i;
128
    unsigned int seed = (unsigned int) time(0);
129
 
130
    srandom(seed);
131
    for (i = 0; i < CHALLENGESIZE; i++) {
132
        bytes[i] = (unsigned char)(random() & 255);
133
    }
134
}
135
 
136
 
137
/*
138
 * Encrypt CHALLENGESIZE bytes in memory using a password.
139
 */
140
 
141
void
142
vncEncryptBytes(unsigned char *bytes, char *passwd)
143
{
144
    unsigned char key[8];
145
    int i;
146
 
147
    /* key is simply password padded with nulls */
148
 
149
    for (i = 0; i < 8; i++) {
150
        if (i < strlen(passwd)) {
151
            key[i] = passwd[i];
152
        } else {
153
            key[i] = 0;
154
        }
155
    }
156
 
157
    deskey(key, EN0);
158
 
159
    for (i = 0; i < CHALLENGESIZE; i += 8) {
160
        des(bytes+i, bytes+i);
161
    }
162
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.