Redundant now as it's moved to ./Makefile.org.
[openssl.git] / crypto / des / enc_writ.c
index 29a7330fb0f69a948a622c297e737d896ebf8cbd..af5b8c2349bba80fd02dba4d0314fb0e3d3819aa 100644 (file)
 
 #include <errno.h>
 #include <time.h>
 
 #include <errno.h>
 #include <time.h>
+#include <stdio.h>
+#include "cryptlib.h"
 #include "des_locl.h"
 #include "des_locl.h"
+#include <openssl/rand.h>
 
 
-int des_enc_write(fd, buf, len, sched, iv)
-int fd;
-char *buf;
-int len;
-des_key_schedule sched;
-des_cblock (*iv);
+/*
+ * WARNINGS:
+ *
+ *  -  The data format used by DES_enc_write() and DES_enc_read()
+ *     has a cryptographic weakness: When asked to write more
+ *     than MAXWRITE bytes, DES_enc_write will split the data
+ *     into several chunks that are all encrypted
+ *     using the same IV.  So don't use these functions unless you
+ *     are sure you know what you do (in which case you might
+ *     not want to use them anyway).
+ *
+ *  -  This code cannot handle non-blocking sockets.
+ */
+
+int DES_enc_write(int fd, const void *_buf, int len,
+                 DES_key_schedule *sched, DES_cblock *iv)
        {
 #ifdef _LIBC
        {
 #ifdef _LIBC
-       extern int srandom();
        extern unsigned long time();
        extern unsigned long time();
-       extern int random();
        extern int write();
 #endif
        extern int write();
 #endif
-
+       const unsigned char *buf=_buf;
        long rnum;
        int i,j,k,outnum;
        long rnum;
        int i,j,k,outnum;
-       static char *outbuf=NULL;
-       char shortbuf[8];
-       char *p;
+       static unsigned char *outbuf=NULL;
+       unsigned char shortbuf[8];
+       unsigned char *p;
+       const unsigned char *cp;
        static int start=1;
 
        if (outbuf == NULL)
                {
        static int start=1;
 
        if (outbuf == NULL)
                {
-               outbuf=(char *)malloc(BSIZE+HDRSIZE);
+               outbuf=OPENSSL_malloc(BSIZE+HDRSIZE);
                if (outbuf == NULL) return(-1);
                }
        /* If we are sending less than 8 bytes, the same char will look
                if (outbuf == NULL) return(-1);
                }
        /* If we are sending less than 8 bytes, the same char will look
@@ -91,7 +103,6 @@ des_cblock (*iv);
        if (start)
                {
                start=0;
        if (start)
                {
                start=0;
-               srandom((unsigned int)time(NULL));
                }
 
        /* lets recurse if we want to send the data in small chunks */
                }
 
        /* lets recurse if we want to send the data in small chunks */
@@ -100,7 +111,7 @@ des_cblock (*iv);
                j=0;
                for (i=0; i<len; i+=k)
                        {
                j=0;
                for (i=0; i<len; i+=k)
                        {
-                       k=des_enc_write(fd,&(buf[i]),
+                       k=DES_enc_write(fd,&(buf[i]),
                                ((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv);
                        if (k < 0)
                                return(k);
                                ((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv);
                        if (k < 0)
                                return(k);
@@ -117,40 +128,40 @@ des_cblock (*iv);
        /* pad short strings */
        if (len < 8)
                {
        /* pad short strings */
        if (len < 8)
                {
-               p=shortbuf;
-               memcpy(shortbuf,buf,(unsigned int)len);
-               for (i=len; i<8; i++)
-                       shortbuf[i]=random();
+               cp=shortbuf;
+               memcpy(shortbuf,buf,len);
+               RAND_pseudo_bytes(shortbuf+len, 8-len);
                rnum=8;
                }
        else
                {
                rnum=8;
                }
        else
                {
-               p=buf;
+               cp=buf;
                rnum=((len+7)/8*8); /* round up to nearest eight */
                }
 
                rnum=((len+7)/8*8); /* round up to nearest eight */
                }
 
-       if (des_rw_mode & DES_PCBC_MODE)
-               des_pcbc_encrypt((des_cblock *)p,
-                       (des_cblock *)&(outbuf[HDRSIZE]),
-                       (long)((len<8)?8:len),sched,iv,DES_ENCRYPT); 
+       if (DES_rw_mode & DES_PCBC_MODE)
+               DES_pcbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv,
+                                DES_ENCRYPT); 
        else
        else
-               des_cbc_encrypt((des_cblock *)p,
-                       (des_cblock *)&(outbuf[HDRSIZE]),
-                       (long)((len<8)?8:len),sched,iv,DES_ENCRYPT); 
+               DES_cbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv,
+                               DES_ENCRYPT); 
 
        /* output */
 
        /* output */
-       outnum=(int)rnum+HDRSIZE;
+       outnum=rnum+HDRSIZE;
 
        for (j=0; j<outnum; j+=i)
                {
                /* eay 26/08/92 I was not doing writing from where we
 
        for (j=0; j<outnum; j+=i)
                {
                /* eay 26/08/92 I was not doing writing from where we
-                * got upto. */
-               i=write(fd,&(outbuf[j]),(unsigned int)(outnum-j));
+                * got up to. */
+               i=write(fd,(void *)&(outbuf[j]),outnum-j);
                if (i == -1)
                        {
                if (i == -1)
                        {
+#ifdef EINTR
                        if (errno == EINTR)
                                i=0;
                        if (errno == EINTR)
                                i=0;
-                       else    /* This is really a bad error - very bad
+                       else
+#endif
+                               /* This is really a bad error - very bad
                                 * It will stuff-up both ends. */
                                return(-1);
                        }
                                 * It will stuff-up both ends. */
                                return(-1);
                        }