diff options
Diffstat (limited to 'src/lib/libcrypto/des/enc_writ.c')
| -rw-r--r-- | src/lib/libcrypto/des/enc_writ.c | 122 |
1 files changed, 59 insertions, 63 deletions
diff --git a/src/lib/libcrypto/des/enc_writ.c b/src/lib/libcrypto/des/enc_writ.c index 65fa720c1e..8a19fb84f9 100644 --- a/src/lib/libcrypto/des/enc_writ.c +++ b/src/lib/libcrypto/des/enc_writ.c | |||
| @@ -1,25 +1,25 @@ | |||
| 1 | /* $OpenBSD: enc_writ.c,v 1.15 2022/11/26 16:08:51 tb Exp $ */ | 1 | /* $OpenBSD: enc_writ.c,v 1.16 2023/07/08 07:11:07 beck Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| 5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * | 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * | 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
| 22 | * | 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: | 25 | * are met: |
| @@ -34,10 +34,10 @@ | |||
| 34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * | 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| @@ -49,7 +49,7 @@ | |||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
| 52 | * | 52 | * |
| 53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
| @@ -79,95 +79,91 @@ | |||
| 79 | * - This code cannot handle non-blocking sockets. | 79 | * - This code cannot handle non-blocking sockets. |
| 80 | */ | 80 | */ |
| 81 | 81 | ||
| 82 | int DES_enc_write(int fd, const void *_buf, int len, | 82 | int |
| 83 | DES_key_schedule *sched, DES_cblock *iv) | 83 | DES_enc_write(int fd, const void *_buf, int len, |
| 84 | { | 84 | DES_key_schedule *sched, DES_cblock *iv) |
| 85 | { | ||
| 85 | #ifdef _LIBC | 86 | #ifdef _LIBC |
| 86 | extern unsigned long time(); | 87 | extern unsigned long time(); |
| 87 | extern int write(); | 88 | extern int write(); |
| 88 | #endif | 89 | #endif |
| 89 | const unsigned char *buf=_buf; | 90 | const unsigned char *buf = _buf; |
| 90 | long rnum; | 91 | long rnum; |
| 91 | int i,j,k,outnum; | 92 | int i, j, k, outnum; |
| 92 | static unsigned char *outbuf=NULL; | 93 | static unsigned char *outbuf = NULL; |
| 93 | unsigned char shortbuf[8]; | 94 | unsigned char shortbuf[8]; |
| 94 | unsigned char *p; | 95 | unsigned char *p; |
| 95 | const unsigned char *cp; | 96 | const unsigned char *cp; |
| 96 | static int start=1; | 97 | static int start = 1; |
| 97 | 98 | ||
| 98 | if (outbuf == NULL) | 99 | if (outbuf == NULL) { |
| 99 | { | 100 | outbuf = malloc(BSIZE + HDRSIZE); |
| 100 | outbuf=malloc(BSIZE+HDRSIZE); | 101 | if (outbuf == NULL) |
| 101 | if (outbuf == NULL) return(-1); | 102 | return (-1); |
| 102 | } | 103 | } |
| 103 | /* If we are sending less than 8 bytes, the same char will look | 104 | /* If we are sending less than 8 bytes, the same char will look |
| 104 | * the same if we don't pad it out with random bytes */ | 105 | * the same if we don't pad it out with random bytes */ |
| 105 | if (start) | 106 | if (start) { |
| 106 | { | 107 | start = 0; |
| 107 | start=0; | 108 | } |
| 108 | } | ||
| 109 | 109 | ||
| 110 | /* lets recurse if we want to send the data in small chunks */ | 110 | /* lets recurse if we want to send the data in small chunks */ |
| 111 | if (len > MAXWRITE) | 111 | if (len > MAXWRITE) { |
| 112 | j = 0; | ||
| 113 | for (i = 0; i < len; i += k) | ||
| 112 | { | 114 | { |
| 113 | j=0; | 115 | k = DES_enc_write(fd, &(buf[i]), |
| 114 | for (i=0; i<len; i+=k) | 116 | ((len - i) > MAXWRITE) ? MAXWRITE : (len - i), |
| 115 | { | 117 | sched, iv); |
| 116 | k=DES_enc_write(fd,&(buf[i]), | ||
| 117 | ((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv); | ||
| 118 | if (k < 0) | 118 | if (k < 0) |
| 119 | return(k); | 119 | return (k); |
| 120 | else | 120 | else |
| 121 | j+=k; | 121 | j += k; |
| 122 | } | ||
| 123 | return(j); | ||
| 124 | } | 122 | } |
| 123 | return (j); | ||
| 124 | } | ||
| 125 | 125 | ||
| 126 | /* write length first */ | 126 | /* write length first */ |
| 127 | p=outbuf; | 127 | p = outbuf; |
| 128 | l2n(len,p); | 128 | l2n(len, p); |
| 129 | 129 | ||
| 130 | /* pad short strings */ | 130 | /* pad short strings */ |
| 131 | if (len < 8) | 131 | if (len < 8) { |
| 132 | { | 132 | cp = shortbuf; |
| 133 | cp=shortbuf; | 133 | memcpy(shortbuf, buf, len); |
| 134 | memcpy(shortbuf,buf,len); | 134 | arc4random_buf(shortbuf + len, 8 - len); |
| 135 | arc4random_buf(shortbuf+len, 8-len); | 135 | rnum = 8; |
| 136 | rnum=8; | 136 | } else { |
| 137 | } | 137 | cp = buf; |
| 138 | else | 138 | rnum = ((len + 7)/8*8); /* round up to nearest eight */ |
| 139 | { | 139 | } |
| 140 | cp=buf; | ||
| 141 | rnum=((len+7)/8*8); /* round up to nearest eight */ | ||
| 142 | } | ||
| 143 | 140 | ||
| 144 | if (DES_rw_mode & DES_PCBC_MODE) | 141 | if (DES_rw_mode & DES_PCBC_MODE) |
| 145 | DES_pcbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv, | 142 | DES_pcbc_encrypt(cp, &(outbuf[HDRSIZE]), (len < 8) ? 8 : len, |
| 146 | DES_ENCRYPT); | 143 | sched, iv, DES_ENCRYPT); |
| 147 | else | 144 | else |
| 148 | DES_cbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv, | 145 | DES_cbc_encrypt(cp, &(outbuf[HDRSIZE]), (len < 8) ? 8 : len, |
| 149 | DES_ENCRYPT); | 146 | sched, iv, DES_ENCRYPT); |
| 150 | 147 | ||
| 151 | /* output */ | 148 | /* output */ |
| 152 | outnum=rnum+HDRSIZE; | 149 | outnum = rnum + HDRSIZE; |
| 153 | 150 | ||
| 154 | for (j=0; j<outnum; j+=i) | 151 | for (j = 0; j < outnum; j += i) |
| 155 | { | 152 | { |
| 156 | /* eay 26/08/92 I was not doing writing from where we | 153 | /* eay 26/08/92 I was not doing writing from where we |
| 157 | * got up to. */ | 154 | * got up to. */ |
| 158 | i=write(fd,(void *)&(outbuf[j]),outnum-j); | 155 | i = write(fd, (void *)&(outbuf[j]), outnum - j); |
| 159 | if (i == -1) | 156 | if (i == -1) { |
| 160 | { | ||
| 161 | #ifdef EINTR | 157 | #ifdef EINTR |
| 162 | if (errno == EINTR) | 158 | if (errno == EINTR) |
| 163 | i=0; | 159 | i = 0; |
| 164 | else | 160 | else |
| 165 | #endif | 161 | #endif |
| 166 | /* This is really a bad error - very bad | 162 | /* This is really a bad error - very bad |
| 167 | * It will stuff-up both ends. */ | 163 | * It will stuff-up both ends. */ |
| 168 | return(-1); | 164 | return (-1); |
| 169 | } | ||
| 170 | } | 165 | } |
| 171 | |||
| 172 | return(len); | ||
| 173 | } | 166 | } |
| 167 | |||
| 168 | return (len); | ||
| 169 | } | ||
