summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/des/enc_writ.c
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:50 +0000
committermarkus <>2002-09-05 12:51:50 +0000
commit15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch)
treebf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/des/enc_writ.c
parent027351f729b9e837200dae6e1520cda6577ab930 (diff)
downloadopenbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/des/enc_writ.c')
-rw-r--r--src/lib/libcrypto/des/enc_writ.c73
1 files changed, 42 insertions, 31 deletions
diff --git a/src/lib/libcrypto/des/enc_writ.c b/src/lib/libcrypto/des/enc_writ.c
index 29a7330fb0..af5b8c2349 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=OPENSSL_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 */
@@ -100,7 +111,7 @@ des_cblock (*iv);
100 j=0; 111 j=0;
101 for (i=0; i<len; i+=k) 112 for (i=0; i<len; i+=k)
102 { 113 {
103 k=des_enc_write(fd,&(buf[i]), 114 k=DES_enc_write(fd,&(buf[i]),
104 ((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv); 115 ((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv);
105 if (k < 0) 116 if (k < 0)
106 return(k); 117 return(k);
@@ -117,40 +128,40 @@ 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_pseudo_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=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 up to. */
148 i=write(fd,&(outbuf[j]),(unsigned int)(outnum-j)); 156 i=write(fd,(void *)&(outbuf[j]),outnum-j);
149 if (i == -1) 157 if (i == -1)
150 { 158 {
159#ifdef EINTR
151 if (errno == EINTR) 160 if (errno == EINTR)
152 i=0; 161 i=0;
153 else /* This is really a bad error - very bad 162 else
163#endif
164 /* This is really a bad error - very bad
154 * It will stuff-up both ends. */ 165 * It will stuff-up both ends. */
155 return(-1); 166 return(-1);
156 } 167 }