]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/des/enc_writ.c
Remove --classic build entirely
[thirdparty/openssl.git] / crypto / des / enc_writ.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
d02b48c6
RE
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
d02b48c6
RE
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58#include <errno.h>
59#include <time.h>
199d59e5 60#include <stdio.h>
b39fc560 61#include "internal/cryptlib.h"
d02b48c6 62#include "des_locl.h"
cfa3747b 63#include <openssl/rand.h>
d02b48c6 64
1d97c843 65/*-
7c0f3d09
BM
66 * WARNINGS:
67 *
c2e4f17c 68 * - The data format used by DES_enc_write() and DES_enc_read()
7c0f3d09 69 * has a cryptographic weakness: When asked to write more
c2e4f17c 70 * than MAXWRITE bytes, DES_enc_write will split the data
7c0f3d09
BM
71 * into several chunks that are all encrypted
72 * using the same IV. So don't use these functions unless you
73 * are sure you know what you do (in which case you might
74 * not want to use them anyway).
75 *
76 * - This code cannot handle non-blocking sockets.
77 */
78
c2e4f17c 79int DES_enc_write(int fd, const void *_buf, int len,
0f113f3e
MC
80 DES_key_schedule *sched, DES_cblock *iv)
81{
1715e488 82#if defined(OPENSSL_NO_POSIX_IO)
0f113f3e 83 return (-1);
1715e488 84#else
0f113f3e
MC
85# ifdef _LIBC
86 extern unsigned long time();
87 extern int write();
88# endif
89 const unsigned char *buf = _buf;
90 long rnum;
91 int i, j, k, outnum;
92 static unsigned char *outbuf = NULL;
93 unsigned char shortbuf[8];
94 unsigned char *p;
95 const unsigned char *cp;
96 static int start = 1;
d02b48c6 97
873fb39f
MC
98 if (len < 0)
99 return -1;
100
0f113f3e
MC
101 if (outbuf == NULL) {
102 outbuf = OPENSSL_malloc(BSIZE + HDRSIZE);
103 if (outbuf == NULL)
104 return (-1);
105 }
106 /*
107 * If we are sending less than 8 bytes, the same char will look the same
108 * if we don't pad it out with random bytes
109 */
110 if (start) {
111 start = 0;
112 }
d02b48c6 113
0f113f3e
MC
114 /* lets recurse if we want to send the data in small chunks */
115 if (len > MAXWRITE) {
116 j = 0;
117 for (i = 0; i < len; i += k) {
118 k = DES_enc_write(fd, &(buf[i]),
119 ((len - i) > MAXWRITE) ? MAXWRITE : (len - i),
120 sched, iv);
121 if (k < 0)
122 return (k);
123 else
124 j += k;
125 }
126 return (j);
127 }
d02b48c6 128
0f113f3e
MC
129 /* write length first */
130 p = outbuf;
131 l2n(len, p);
d02b48c6 132
0f113f3e
MC
133 /* pad short strings */
134 if (len < 8) {
135 cp = shortbuf;
136 memcpy(shortbuf, buf, len);
266483d2
MC
137 if (RAND_bytes(shortbuf + len, 8 - len) <= 0)
138 return -1;
0f113f3e
MC
139 rnum = 8;
140 } else {
141 cp = buf;
142 rnum = ((len + 7) / 8 * 8); /* round up to nearest eight */
143 }
d02b48c6 144
0f113f3e
MC
145 if (DES_rw_mode & DES_PCBC_MODE)
146 DES_pcbc_encrypt(cp, &(outbuf[HDRSIZE]), (len < 8) ? 8 : len, sched,
147 iv, DES_ENCRYPT);
148 else
149 DES_cbc_encrypt(cp, &(outbuf[HDRSIZE]), (len < 8) ? 8 : len, sched,
150 iv, DES_ENCRYPT);
d02b48c6 151
0f113f3e
MC
152 /* output */
153 outnum = rnum + HDRSIZE;
d02b48c6 154
0f113f3e
MC
155 for (j = 0; j < outnum; j += i) {
156 /*
157 * eay 26/08/92 I was not doing writing from where we got up to.
158 */
159# ifndef _WIN32
160 i = write(fd, (void *)&(outbuf[j]), outnum - j);
161# else
162 i = _write(fd, (void *)&(outbuf[j]), outnum - j);
163# endif
164 if (i == -1) {
165# ifdef EINTR
166 if (errno == EINTR)
167 i = 0;
168 else
169# endif
170 /*
171 * This is really a bad error - very bad It will stuff-up
172 * both ends.
173 */
174 return (-1);
175 }
176 }
d02b48c6 177
0f113f3e
MC
178 return (len);
179#endif /* OPENSSL_NO_POSIX_IO */
180}