summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/des/enc_writ.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/des/enc_writ.c')
-rw-r--r--src/lib/libcrypto/des/enc_writ.c62
1 files changed, 35 insertions, 27 deletions
diff --git a/src/lib/libcrypto/des/enc_writ.c b/src/lib/libcrypto/des/enc_writ.c
index 29a7330fb0..ba3f0822ef 100644
--- a/src/lib/libcrypto/des/enc_writ.c
+++ b/src/lib/libcrypto/des/enc_writ.c
@@ -58,32 +58,44 @@
58 58
59#include <errno.h> 59#include <errno.h>
60#include <time.h> 60#include <time.h>
61#include <stdio.h>
62#include "cryptlib.h"
61#include "des_locl.h" 63#include "des_locl.h"
64#include <openssl/rand.h>
62 65
63int des_enc_write(fd, buf, len, sched, iv) 66/*
64int fd; 67 * WARNINGS:
65char *buf; 68 *
66int len; 69 * - The data format used by des_enc_write() and des_enc_read()
67des_key_schedule sched; 70 * has a cryptographic weakness: When asked to write more
68des_cblock (*iv); 71 * than MAXWRITE bytes, des_enc_write will split the data
72 * into several chunks that are all encrypted
73 * using the same IV. So don't use these functions unless you
74 * are sure you know what you do (in which case you might
75 * not want to use them anyway).
76 *
77 * - This code cannot handle non-blocking sockets.
78 */
79
80int des_enc_write(int fd, const void *_buf, int len,
81 des_key_schedule sched, des_cblock *iv)
69 { 82 {
70#ifdef _LIBC 83#ifdef _LIBC
71 extern int srandom();
72 extern unsigned long time(); 84 extern unsigned long time();
73 extern int random();
74 extern int write(); 85 extern int write();
75#endif 86#endif
76 87 const unsigned char *buf=_buf;
77 long rnum; 88 long rnum;
78 int i,j,k,outnum; 89 int i,j,k,outnum;
79 static char *outbuf=NULL; 90 static unsigned char *outbuf=NULL;
80 char shortbuf[8]; 91 unsigned char shortbuf[8];
81 char *p; 92 unsigned char *p;
93 const unsigned char *cp;
82 static int start=1; 94 static int start=1;
83 95
84 if (outbuf == NULL) 96 if (outbuf == NULL)
85 { 97 {
86 outbuf=(char *)malloc(BSIZE+HDRSIZE); 98 outbuf=Malloc(BSIZE+HDRSIZE);
87 if (outbuf == NULL) return(-1); 99 if (outbuf == NULL) return(-1);
88 } 100 }
89 /* If we are sending less than 8 bytes, the same char will look 101 /* If we are sending less than 8 bytes, the same char will look
@@ -91,7 +103,6 @@ des_cblock (*iv);
91 if (start) 103 if (start)
92 { 104 {
93 start=0; 105 start=0;
94 srandom((unsigned int)time(NULL));
95 } 106 }
96 107
97 /* lets recurse if we want to send the data in small chunks */ 108 /* lets recurse if we want to send the data in small chunks */
@@ -117,35 +128,32 @@ des_cblock (*iv);
117 /* pad short strings */ 128 /* pad short strings */
118 if (len < 8) 129 if (len < 8)
119 { 130 {
120 p=shortbuf; 131 cp=shortbuf;
121 memcpy(shortbuf,buf,(unsigned int)len); 132 memcpy(shortbuf,buf,len);
122 for (i=len; i<8; i++) 133 RAND_bytes(shortbuf+len, 8-len);
123 shortbuf[i]=random();
124 rnum=8; 134 rnum=8;
125 } 135 }
126 else 136 else
127 { 137 {
128 p=buf; 138 cp=(unsigned char*)buf;
129 rnum=((len+7)/8*8); /* round up to nearest eight */ 139 rnum=((len+7)/8*8); /* round up to nearest eight */
130 } 140 }
131 141
132 if (des_rw_mode & DES_PCBC_MODE) 142 if (des_rw_mode & DES_PCBC_MODE)
133 des_pcbc_encrypt((des_cblock *)p, 143 des_pcbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv,
134 (des_cblock *)&(outbuf[HDRSIZE]), 144 DES_ENCRYPT);
135 (long)((len<8)?8:len),sched,iv,DES_ENCRYPT);
136 else 145 else
137 des_cbc_encrypt((des_cblock *)p, 146 des_cbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv,
138 (des_cblock *)&(outbuf[HDRSIZE]), 147 DES_ENCRYPT);
139 (long)((len<8)?8:len),sched,iv,DES_ENCRYPT);
140 148
141 /* output */ 149 /* output */
142 outnum=(int)rnum+HDRSIZE; 150 outnum=rnum+HDRSIZE;
143 151
144 for (j=0; j<outnum; j+=i) 152 for (j=0; j<outnum; j+=i)
145 { 153 {
146 /* eay 26/08/92 I was not doing writing from where we 154 /* eay 26/08/92 I was not doing writing from where we
147 * got upto. */ 155 * got upto. */
148 i=write(fd,&(outbuf[j]),(unsigned int)(outnum-j)); 156 i=write(fd,&(outbuf[j]),outnum-j);
149 if (i == -1) 157 if (i == -1)
150 { 158 {
151 if (errno == EINTR) 159 if (errno == EINTR)