summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/des/ncbc_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/des/ncbc_enc.c')
-rw-r--r--src/lib/libcrypto/des/ncbc_enc.c151
1 files changed, 80 insertions, 71 deletions
diff --git a/src/lib/libcrypto/des/ncbc_enc.c b/src/lib/libcrypto/des/ncbc_enc.c
index f2e1274b7e..d7e47813b3 100644
--- a/src/lib/libcrypto/des/ncbc_enc.c
+++ b/src/lib/libcrypto/des/ncbc_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ncbc_enc.c,v 1.8 2022/11/26 16:08:51 tb Exp $ */ 1/* $OpenBSD: ncbc_enc.c,v 1.9 2023/07/08 07:11:07 beck Exp $ */
2/* 2/*
3 * #included by: 3 * #included by:
4 * cbc_enc.c (DES_cbc_encrypt) 4 * cbc_enc.c (DES_cbc_encrypt)
@@ -10,21 +10,21 @@
10 * This package is an SSL implementation written 10 * This package is an SSL implementation written
11 * by Eric Young (eay@cryptsoft.com). 11 * by Eric Young (eay@cryptsoft.com).
12 * The implementation was written so as to conform with Netscapes SSL. 12 * The implementation was written so as to conform with Netscapes SSL.
13 * 13 *
14 * This library is free for commercial and non-commercial use as long as 14 * This library is free for commercial and non-commercial use as long as
15 * the following conditions are aheared to. The following conditions 15 * the following conditions are aheared to. The following conditions
16 * apply to all code found in this distribution, be it the RC4, RSA, 16 * apply to all code found in this distribution, be it the RC4, RSA,
17 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 17 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
18 * included with this distribution is covered by the same copyright terms 18 * included with this distribution is covered by the same copyright terms
19 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 19 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
20 * 20 *
21 * Copyright remains Eric Young's, and as such any Copyright notices in 21 * Copyright remains Eric Young's, and as such any Copyright notices in
22 * the code are not to be removed. 22 * the code are not to be removed.
23 * If this package is used in a product, Eric Young should be given attribution 23 * If this package is used in a product, Eric Young should be given attribution
24 * as the author of the parts of the library used. 24 * as the author of the parts of the library used.
25 * This can be in the form of a textual message at program startup or 25 * This can be in the form of a textual message at program startup or
26 * in documentation (online or textual) provided with the package. 26 * in documentation (online or textual) provided with the package.
27 * 27 *
28 * Redistribution and use in source and binary forms, with or without 28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions 29 * modification, are permitted provided that the following conditions
30 * are met: 30 * are met:
@@ -39,10 +39,10 @@
39 * Eric Young (eay@cryptsoft.com)" 39 * Eric Young (eay@cryptsoft.com)"
40 * The word 'cryptographic' can be left out if the rouines from the library 40 * The word 'cryptographic' can be left out if the rouines from the library
41 * being used are not cryptographic related :-). 41 * being used are not cryptographic related :-).
42 * 4. If you include any Windows specific code (or a derivative thereof) from 42 * 4. If you include any Windows specific code (or a derivative thereof) from
43 * the apps directory (application code) you must include an acknowledgement: 43 * the apps directory (application code) you must include an acknowledgement:
44 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 44 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
45 * 45 *
46 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 46 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -54,7 +54,7 @@
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE. 56 * SUCH DAMAGE.
57 * 57 *
58 * The licence and distribution terms for any publically available version or 58 * The licence and distribution terms for any publically available version or
59 * derivative of this code cannot be changed. i.e. this code cannot simply be 59 * derivative of this code cannot be changed. i.e. this code cannot simply be
60 * copied and put under another distribution licence 60 * copied and put under another distribution licence
@@ -64,85 +64,94 @@
64#include "des_local.h" 64#include "des_local.h"
65 65
66#ifdef CBC_ENC_C__DONT_UPDATE_IV 66#ifdef CBC_ENC_C__DONT_UPDATE_IV
67void DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 67void
68 DES_key_schedule *_schedule, DES_cblock *ivec, int enc) 68DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
69 DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
69#else 70#else
70void DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length, 71void
71 DES_key_schedule *_schedule, DES_cblock *ivec, int enc) 72DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length,
73 DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
72#endif 74#endif
73 { 75{
74 DES_LONG tin0,tin1; 76 DES_LONG tin0, tin1;
75 DES_LONG tout0,tout1,xor0,xor1; 77 DES_LONG tout0, tout1, xor0, xor1;
76 long l=length; 78 long l = length;
77 DES_LONG tin[2]; 79 DES_LONG tin[2];
78 unsigned char *iv; 80 unsigned char *iv;
79 81
80 iv = &(*ivec)[0]; 82 iv = &(*ivec)[0];
81 83
82 if (enc) 84 if (enc) {
85 c2l(iv, tout0);
86 c2l(iv, tout1);
87 for (l -= 8; l >= 0; l -= 8)
83 { 88 {
84 c2l(iv,tout0); 89 c2l(in, tin0);
85 c2l(iv,tout1); 90 c2l(in, tin1);
86 for (l-=8; l>=0; l-=8) 91 tin0 ^= tout0;
87 { 92 tin[0] = tin0;
88 c2l(in,tin0); 93 tin1 ^= tout1;
89 c2l(in,tin1); 94 tin[1] = tin1;
90 tin0^=tout0; tin[0]=tin0; 95 DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT);
91 tin1^=tout1; tin[1]=tin1; 96 tout0 = tin[0];
92 DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT); 97 l2c(tout0, out);
93 tout0=tin[0]; l2c(tout0,out); 98 tout1 = tin[1];
94 tout1=tin[1]; l2c(tout1,out); 99 l2c(tout1, out);
95 } 100 }
96 if (l != -8) 101 if (l != -8) {
97 { 102 c2ln(in, tin0, tin1, l + 8);
98 c2ln(in,tin0,tin1,l+8); 103 tin0 ^= tout0;
99 tin0^=tout0; tin[0]=tin0; 104 tin[0] = tin0;
100 tin1^=tout1; tin[1]=tin1; 105 tin1 ^= tout1;
101 DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT); 106 tin[1] = tin1;
102 tout0=tin[0]; l2c(tout0,out); 107 DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT);
103 tout1=tin[1]; l2c(tout1,out); 108 tout0 = tin[0];
104 } 109 l2c(tout0, out);
110 tout1 = tin[1];
111 l2c(tout1, out);
112 }
105#ifndef CBC_ENC_C__DONT_UPDATE_IV 113#ifndef CBC_ENC_C__DONT_UPDATE_IV
106 iv = &(*ivec)[0]; 114 iv = &(*ivec)[0];
107 l2c(tout0,iv); 115 l2c(tout0, iv);
108 l2c(tout1,iv); 116 l2c(tout1, iv);
109#endif 117#endif
110 } 118 } else {
111 else 119 c2l(iv, xor0);
120 c2l(iv, xor1);
121 for (l -= 8; l >= 0; l -= 8)
112 { 122 {
113 c2l(iv,xor0); 123 c2l(in, tin0);
114 c2l(iv,xor1); 124 tin[0] = tin0;
115 for (l-=8; l>=0; l-=8) 125 c2l(in, tin1);
116 { 126 tin[1] = tin1;
117 c2l(in,tin0); tin[0]=tin0; 127 DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT);
118 c2l(in,tin1); tin[1]=tin1; 128 tout0 = tin[0] ^ xor0;
119 DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT); 129 tout1 = tin[1] ^ xor1;
120 tout0=tin[0]^xor0; 130 l2c(tout0, out);
121 tout1=tin[1]^xor1; 131 l2c(tout1, out);
122 l2c(tout0,out); 132 xor0 = tin0;
123 l2c(tout1,out); 133 xor1 = tin1;
124 xor0=tin0; 134 }
125 xor1=tin1; 135 if (l != -8) {
126 } 136 c2l(in, tin0);
127 if (l != -8) 137 tin[0] = tin0;
128 { 138 c2l(in, tin1);
129 c2l(in,tin0); tin[0]=tin0; 139 tin[1] = tin1;
130 c2l(in,tin1); tin[1]=tin1; 140 DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT);
131 DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT); 141 tout0 = tin[0] ^ xor0;
132 tout0=tin[0]^xor0; 142 tout1 = tin[1] ^ xor1;
133 tout1=tin[1]^xor1; 143 l2cn(tout0, tout1, out, l + 8);
134 l2cn(tout0,tout1,out,l+8);
135#ifndef CBC_ENC_C__DONT_UPDATE_IV 144#ifndef CBC_ENC_C__DONT_UPDATE_IV
136 xor0=tin0; 145 xor0 = tin0;
137 xor1=tin1; 146 xor1 = tin1;
138#endif 147#endif
139 } 148 }
140#ifndef CBC_ENC_C__DONT_UPDATE_IV 149#ifndef CBC_ENC_C__DONT_UPDATE_IV
141 iv = &(*ivec)[0]; 150 iv = &(*ivec)[0];
142 l2c(xor0,iv); 151 l2c(xor0, iv);
143 l2c(xor1,iv); 152 l2c(xor1, iv);
144#endif 153#endif
145 }
146 tin0=tin1=tout0=tout1=xor0=xor1=0;
147 tin[0]=tin[1]=0;
148 } 154 }
155 tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
156 tin[0] = tin[1] = 0;
157}