diff options
Diffstat (limited to 'src/lib/libcrypto/des/enc_read.c')
-rw-r--r-- | src/lib/libcrypto/des/enc_read.c | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/src/lib/libcrypto/des/enc_read.c b/src/lib/libcrypto/des/enc_read.c index e08a904d75..694970ccd2 100644 --- a/src/lib/libcrypto/des/enc_read.c +++ b/src/lib/libcrypto/des/enc_read.c | |||
@@ -58,18 +58,34 @@ | |||
58 | 58 | ||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include <errno.h> | 60 | #include <errno.h> |
61 | #include "cryptlib.h" | ||
61 | #include "des_locl.h" | 62 | #include "des_locl.h" |
62 | 63 | ||
63 | /* This has some uglies in it but it works - even over sockets. */ | 64 | /* This has some uglies in it but it works - even over sockets. */ |
64 | /*extern int errno;*/ | 65 | /*extern int errno;*/ |
65 | int des_rw_mode=DES_PCBC_MODE; | 66 | OPENSSL_GLOBAL int des_rw_mode=DES_PCBC_MODE; |
66 | 67 | ||
67 | int des_enc_read(fd, buf, len, sched, iv) | 68 | |
68 | int fd; | 69 | /* |
69 | char *buf; | 70 | * WARNINGS: |
70 | int len; | 71 | * |
71 | des_key_schedule sched; | 72 | * - The data format used by des_enc_write() and des_enc_read() |
72 | des_cblock (*iv); | 73 | * has a cryptographic weakness: When asked to write more |
74 | * than MAXWRITE bytes, des_enc_write will split the data | ||
75 | * into several chunks that are all encrypted | ||
76 | * using the same IV. So don't use these functions unless you | ||
77 | * are sure you know what you do (in which case you might | ||
78 | * not want to use them anyway). | ||
79 | * | ||
80 | * - This code cannot handle non-blocking sockets. | ||
81 | * | ||
82 | * - This function uses an internal state and thus cannot be | ||
83 | * used on multiple files. | ||
84 | */ | ||
85 | |||
86 | |||
87 | int des_enc_read(int fd, void *buf, int len, des_key_schedule sched, | ||
88 | des_cblock *iv) | ||
73 | { | 89 | { |
74 | /* data to be unencrypted */ | 90 | /* data to be unencrypted */ |
75 | int net_num=0; | 91 | int net_num=0; |
@@ -77,27 +93,27 @@ des_cblock (*iv); | |||
77 | /* extra unencrypted data | 93 | /* extra unencrypted data |
78 | * for when a block of 100 comes in but is des_read one byte at | 94 | * for when a block of 100 comes in but is des_read one byte at |
79 | * a time. */ | 95 | * a time. */ |
80 | static char *unnet=NULL; | 96 | static unsigned char *unnet=NULL; |
81 | static int unnet_start=0; | 97 | static int unnet_start=0; |
82 | static int unnet_left=0; | 98 | static int unnet_left=0; |
83 | static char *tmpbuf=NULL; | 99 | static unsigned char *tmpbuf=NULL; |
84 | int i; | 100 | int i; |
85 | long num=0,rnum; | 101 | long num=0,rnum; |
86 | unsigned char *p; | 102 | unsigned char *p; |
87 | 103 | ||
88 | if (tmpbuf == NULL) | 104 | if (tmpbuf == NULL) |
89 | { | 105 | { |
90 | tmpbuf=(char *)malloc(BSIZE); | 106 | tmpbuf=Malloc(BSIZE); |
91 | if (tmpbuf == NULL) return(-1); | 107 | if (tmpbuf == NULL) return(-1); |
92 | } | 108 | } |
93 | if (net == NULL) | 109 | if (net == NULL) |
94 | { | 110 | { |
95 | net=(unsigned char *)malloc(BSIZE); | 111 | net=Malloc(BSIZE); |
96 | if (net == NULL) return(-1); | 112 | if (net == NULL) return(-1); |
97 | } | 113 | } |
98 | if (unnet == NULL) | 114 | if (unnet == NULL) |
99 | { | 115 | { |
100 | unnet=(char *)malloc(BSIZE); | 116 | unnet=Malloc(BSIZE); |
101 | if (unnet == NULL) return(-1); | 117 | if (unnet == NULL) return(-1); |
102 | } | 118 | } |
103 | /* left over data from last decrypt */ | 119 | /* left over data from last decrypt */ |
@@ -109,7 +125,7 @@ des_cblock (*iv); | |||
109 | * with the number of bytes we have - should always | 125 | * with the number of bytes we have - should always |
110 | * check the return value */ | 126 | * check the return value */ |
111 | memcpy(buf,&(unnet[unnet_start]), | 127 | memcpy(buf,&(unnet[unnet_start]), |
112 | (unsigned int)unnet_left); | 128 | unnet_left); |
113 | /* eay 26/08/92 I had the next 2 lines | 129 | /* eay 26/08/92 I had the next 2 lines |
114 | * reversed :-( */ | 130 | * reversed :-( */ |
115 | i=unnet_left; | 131 | i=unnet_left; |
@@ -117,7 +133,7 @@ des_cblock (*iv); | |||
117 | } | 133 | } |
118 | else | 134 | else |
119 | { | 135 | { |
120 | memcpy(buf,&(unnet[unnet_start]),(unsigned int)len); | 136 | memcpy(buf,&(unnet[unnet_start]),len); |
121 | unnet_start+=len; | 137 | unnet_start+=len; |
122 | unnet_left-=len; | 138 | unnet_left-=len; |
123 | i=len; | 139 | i=len; |
@@ -131,7 +147,7 @@ des_cblock (*iv); | |||
131 | /* first - get the length */ | 147 | /* first - get the length */ |
132 | while (net_num < HDRSIZE) | 148 | while (net_num < HDRSIZE) |
133 | { | 149 | { |
134 | i=read(fd,&(net[net_num]),(unsigned int)HDRSIZE-net_num); | 150 | i=read(fd,&(net[net_num]),HDRSIZE-net_num); |
135 | #ifdef EINTR | 151 | #ifdef EINTR |
136 | if ((i == -1) && (errno == EINTR)) continue; | 152 | if ((i == -1) && (errno == EINTR)) continue; |
137 | #endif | 153 | #endif |
@@ -153,7 +169,7 @@ des_cblock (*iv); | |||
153 | net_num=0; | 169 | net_num=0; |
154 | while (net_num < rnum) | 170 | while (net_num < rnum) |
155 | { | 171 | { |
156 | i=read(fd,&(net[net_num]),(unsigned int)rnum-net_num); | 172 | i=read(fd,&(net[net_num]),rnum-net_num); |
157 | #ifdef EINTR | 173 | #ifdef EINTR |
158 | if ((i == -1) && (errno == EINTR)) continue; | 174 | if ((i == -1) && (errno == EINTR)) continue; |
159 | #endif | 175 | #endif |
@@ -165,14 +181,12 @@ des_cblock (*iv); | |||
165 | if (len < num) | 181 | if (len < num) |
166 | { | 182 | { |
167 | if (des_rw_mode & DES_PCBC_MODE) | 183 | if (des_rw_mode & DES_PCBC_MODE) |
168 | des_pcbc_encrypt((des_cblock *)net,(des_cblock *)unnet, | 184 | des_pcbc_encrypt(net,unnet,num,sched,iv,DES_DECRYPT); |
169 | num,sched,iv,DES_DECRYPT); | ||
170 | else | 185 | else |
171 | des_cbc_encrypt((des_cblock *)net,(des_cblock *)unnet, | 186 | des_cbc_encrypt(net,unnet,num,sched,iv,DES_DECRYPT); |
172 | num,sched,iv,DES_DECRYPT); | 187 | memcpy(buf,unnet,len); |
173 | memcpy(buf,unnet,(unsigned int)len); | ||
174 | unnet_start=len; | 188 | unnet_start=len; |
175 | unnet_left=(int)num-len; | 189 | unnet_left=num-len; |
176 | 190 | ||
177 | /* The following line is done because we return num | 191 | /* The following line is done because we return num |
178 | * as the number of bytes read. */ | 192 | * as the number of bytes read. */ |
@@ -189,30 +203,26 @@ des_cblock (*iv); | |||
189 | { | 203 | { |
190 | 204 | ||
191 | if (des_rw_mode & DES_PCBC_MODE) | 205 | if (des_rw_mode & DES_PCBC_MODE) |
192 | des_pcbc_encrypt((des_cblock *)net, | 206 | des_pcbc_encrypt(net,tmpbuf,num,sched,iv, |
193 | (des_cblock *)tmpbuf, | 207 | DES_DECRYPT); |
194 | num,sched,iv,DES_DECRYPT); | ||
195 | else | 208 | else |
196 | des_cbc_encrypt((des_cblock *)net, | 209 | des_cbc_encrypt(net,tmpbuf,num,sched,iv, |
197 | (des_cblock *)tmpbuf, | 210 | DES_DECRYPT); |
198 | num,sched,iv,DES_DECRYPT); | ||
199 | 211 | ||
200 | /* eay 26/08/92 fix a bug that returned more | 212 | /* eay 26/08/92 fix a bug that returned more |
201 | * bytes than you asked for (returned len bytes :-( */ | 213 | * bytes than you asked for (returned len bytes :-( */ |
202 | memcpy(buf,tmpbuf,(unsigned int)num); | 214 | memcpy(buf,tmpbuf,num); |
203 | } | 215 | } |
204 | else | 216 | else |
205 | { | 217 | { |
206 | if (des_rw_mode & DES_PCBC_MODE) | 218 | if (des_rw_mode & DES_PCBC_MODE) |
207 | des_pcbc_encrypt((des_cblock *)net, | 219 | des_pcbc_encrypt(net,buf,num,sched,iv, |
208 | (des_cblock *)buf,num,sched,iv, | 220 | DES_DECRYPT); |
209 | DES_DECRYPT); | ||
210 | else | 221 | else |
211 | des_cbc_encrypt((des_cblock *)net, | 222 | des_cbc_encrypt(net,buf,num,sched,iv, |
212 | (des_cblock *)buf,num,sched,iv, | 223 | DES_DECRYPT); |
213 | DES_DECRYPT); | ||
214 | } | 224 | } |
215 | } | 225 | } |
216 | return((int)num); | 226 | return num; |
217 | } | 227 | } |
218 | 228 | ||