/src/openssl30/crypto/des/des_enc.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | /* | 
| 11 |  |  * DES low level APIs are deprecated for public use, but still ok for internal | 
| 12 |  |  * use. | 
| 13 |  |  */ | 
| 14 |  | #include "internal/deprecated.h" | 
| 15 |  |  | 
| 16 |  | #include <openssl/crypto.h> | 
| 17 |  | #include "des_local.h" | 
| 18 |  | #include "spr.h" | 
| 19 |  |  | 
| 20 |  | void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc) | 
| 21 | 0 | { | 
| 22 | 0 |     register DES_LONG l, r, t, u; | 
| 23 | 0 |     register DES_LONG *s; | 
| 24 |  | 
 | 
| 25 | 0 |     r = data[0]; | 
| 26 | 0 |     l = data[1]; | 
| 27 |  | 
 | 
| 28 | 0 |     IP(r, l); | 
| 29 |  |     /* | 
| 30 |  |      * Things have been modified so that the initial rotate is done outside | 
| 31 |  |      * the loop.  This required the DES_SPtrans values in sp.h to be rotated | 
| 32 |  |      * 1 bit to the right. One perl script later and things have a 5% speed | 
| 33 |  |      * up on a sparc2. Thanks to Richard Outerbridge for pointing this out. | 
| 34 |  |      */ | 
| 35 |  |     /* clear the top bits on machines with 8byte longs */ | 
| 36 |  |     /* shift left by 2 */ | 
| 37 | 0 |     r = ROTATE(r, 29) & 0xffffffffL; | 
| 38 | 0 |     l = ROTATE(l, 29) & 0xffffffffL; | 
| 39 |  | 
 | 
| 40 | 0 |     s = ks->ks->deslong; | 
| 41 |  |     /* | 
| 42 |  |      * I don't know if it is worth the effort of loop unrolling the inner | 
| 43 |  |      * loop | 
| 44 |  |      */ | 
| 45 | 0 |     if (enc) { | 
| 46 | 0 |         D_ENCRYPT(l, r, 0);     /* 1 */ | 
| 47 | 0 |         D_ENCRYPT(r, l, 2);     /* 2 */ | 
| 48 | 0 |         D_ENCRYPT(l, r, 4);     /* 3 */ | 
| 49 | 0 |         D_ENCRYPT(r, l, 6);     /* 4 */ | 
| 50 | 0 |         D_ENCRYPT(l, r, 8);     /* 5 */ | 
| 51 | 0 |         D_ENCRYPT(r, l, 10);    /* 6 */ | 
| 52 | 0 |         D_ENCRYPT(l, r, 12);    /* 7 */ | 
| 53 | 0 |         D_ENCRYPT(r, l, 14);    /* 8 */ | 
| 54 | 0 |         D_ENCRYPT(l, r, 16);    /* 9 */ | 
| 55 | 0 |         D_ENCRYPT(r, l, 18);    /* 10 */ | 
| 56 | 0 |         D_ENCRYPT(l, r, 20);    /* 11 */ | 
| 57 | 0 |         D_ENCRYPT(r, l, 22);    /* 12 */ | 
| 58 | 0 |         D_ENCRYPT(l, r, 24);    /* 13 */ | 
| 59 | 0 |         D_ENCRYPT(r, l, 26);    /* 14 */ | 
| 60 | 0 |         D_ENCRYPT(l, r, 28);    /* 15 */ | 
| 61 | 0 |         D_ENCRYPT(r, l, 30);    /* 16 */ | 
| 62 | 0 |     } else { | 
| 63 | 0 |         D_ENCRYPT(l, r, 30);    /* 16 */ | 
| 64 | 0 |         D_ENCRYPT(r, l, 28);    /* 15 */ | 
| 65 | 0 |         D_ENCRYPT(l, r, 26);    /* 14 */ | 
| 66 | 0 |         D_ENCRYPT(r, l, 24);    /* 13 */ | 
| 67 | 0 |         D_ENCRYPT(l, r, 22);    /* 12 */ | 
| 68 | 0 |         D_ENCRYPT(r, l, 20);    /* 11 */ | 
| 69 | 0 |         D_ENCRYPT(l, r, 18);    /* 10 */ | 
| 70 | 0 |         D_ENCRYPT(r, l, 16);    /* 9 */ | 
| 71 | 0 |         D_ENCRYPT(l, r, 14);    /* 8 */ | 
| 72 | 0 |         D_ENCRYPT(r, l, 12);    /* 7 */ | 
| 73 | 0 |         D_ENCRYPT(l, r, 10);    /* 6 */ | 
| 74 | 0 |         D_ENCRYPT(r, l, 8);     /* 5 */ | 
| 75 | 0 |         D_ENCRYPT(l, r, 6);     /* 4 */ | 
| 76 | 0 |         D_ENCRYPT(r, l, 4);     /* 3 */ | 
| 77 | 0 |         D_ENCRYPT(l, r, 2);     /* 2 */ | 
| 78 | 0 |         D_ENCRYPT(r, l, 0);     /* 1 */ | 
| 79 | 0 |     } | 
| 80 |  |  | 
| 81 |  |     /* rotate and clear the top bits on machines with 8byte longs */ | 
| 82 | 0 |     l = ROTATE(l, 3) & 0xffffffffL; | 
| 83 | 0 |     r = ROTATE(r, 3) & 0xffffffffL; | 
| 84 |  | 
 | 
| 85 | 0 |     FP(r, l); | 
| 86 | 0 |     data[0] = l; | 
| 87 | 0 |     data[1] = r; | 
| 88 | 0 |     l = r = t = u = 0; | 
| 89 | 0 | } | 
| 90 |  |  | 
| 91 |  | void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc) | 
| 92 | 182k | { | 
| 93 | 182k |     register DES_LONG l, r, t, u; | 
| 94 | 182k |     register DES_LONG *s; | 
| 95 |  |  | 
| 96 | 182k |     r = data[0]; | 
| 97 | 182k |     l = data[1]; | 
| 98 |  |  | 
| 99 |  |     /* | 
| 100 |  |      * Things have been modified so that the initial rotate is done outside | 
| 101 |  |      * the loop.  This required the DES_SPtrans values in sp.h to be rotated | 
| 102 |  |      * 1 bit to the right. One perl script later and things have a 5% speed | 
| 103 |  |      * up on a sparc2. Thanks to Richard Outerbridge for pointing this out. | 
| 104 |  |      */ | 
| 105 |  |     /* clear the top bits on machines with 8byte longs */ | 
| 106 | 182k |     r = ROTATE(r, 29) & 0xffffffffL; | 
| 107 | 182k |     l = ROTATE(l, 29) & 0xffffffffL; | 
| 108 |  |  | 
| 109 | 182k |     s = ks->ks->deslong; | 
| 110 |  |     /* | 
| 111 |  |      * I don't know if it is worth the effort of loop unrolling the inner | 
| 112 |  |      * loop | 
| 113 |  |      */ | 
| 114 | 182k |     if (enc) { | 
| 115 | 63.4k |         D_ENCRYPT(l, r, 0);     /* 1 */ | 
| 116 | 63.4k |         D_ENCRYPT(r, l, 2);     /* 2 */ | 
| 117 | 63.4k |         D_ENCRYPT(l, r, 4);     /* 3 */ | 
| 118 | 63.4k |         D_ENCRYPT(r, l, 6);     /* 4 */ | 
| 119 | 63.4k |         D_ENCRYPT(l, r, 8);     /* 5 */ | 
| 120 | 63.4k |         D_ENCRYPT(r, l, 10);    /* 6 */ | 
| 121 | 63.4k |         D_ENCRYPT(l, r, 12);    /* 7 */ | 
| 122 | 63.4k |         D_ENCRYPT(r, l, 14);    /* 8 */ | 
| 123 | 63.4k |         D_ENCRYPT(l, r, 16);    /* 9 */ | 
| 124 | 63.4k |         D_ENCRYPT(r, l, 18);    /* 10 */ | 
| 125 | 63.4k |         D_ENCRYPT(l, r, 20);    /* 11 */ | 
| 126 | 63.4k |         D_ENCRYPT(r, l, 22);    /* 12 */ | 
| 127 | 63.4k |         D_ENCRYPT(l, r, 24);    /* 13 */ | 
| 128 | 63.4k |         D_ENCRYPT(r, l, 26);    /* 14 */ | 
| 129 | 63.4k |         D_ENCRYPT(l, r, 28);    /* 15 */ | 
| 130 | 63.4k |         D_ENCRYPT(r, l, 30);    /* 16 */ | 
| 131 | 118k |     } else { | 
| 132 | 118k |         D_ENCRYPT(l, r, 30);    /* 16 */ | 
| 133 | 118k |         D_ENCRYPT(r, l, 28);    /* 15 */ | 
| 134 | 118k |         D_ENCRYPT(l, r, 26);    /* 14 */ | 
| 135 | 118k |         D_ENCRYPT(r, l, 24);    /* 13 */ | 
| 136 | 118k |         D_ENCRYPT(l, r, 22);    /* 12 */ | 
| 137 | 118k |         D_ENCRYPT(r, l, 20);    /* 11 */ | 
| 138 | 118k |         D_ENCRYPT(l, r, 18);    /* 10 */ | 
| 139 | 118k |         D_ENCRYPT(r, l, 16);    /* 9 */ | 
| 140 | 118k |         D_ENCRYPT(l, r, 14);    /* 8 */ | 
| 141 | 118k |         D_ENCRYPT(r, l, 12);    /* 7 */ | 
| 142 | 118k |         D_ENCRYPT(l, r, 10);    /* 6 */ | 
| 143 | 118k |         D_ENCRYPT(r, l, 8);     /* 5 */ | 
| 144 | 118k |         D_ENCRYPT(l, r, 6);     /* 4 */ | 
| 145 | 118k |         D_ENCRYPT(r, l, 4);     /* 3 */ | 
| 146 | 118k |         D_ENCRYPT(l, r, 2);     /* 2 */ | 
| 147 | 118k |         D_ENCRYPT(r, l, 0);     /* 1 */ | 
| 148 | 118k |     } | 
| 149 |  |     /* rotate and clear the top bits on machines with 8byte longs */ | 
| 150 | 182k |     data[0] = ROTATE(l, 3) & 0xffffffffL; | 
| 151 | 182k |     data[1] = ROTATE(r, 3) & 0xffffffffL; | 
| 152 | 182k |     l = r = t = u = 0; | 
| 153 | 182k | } | 
| 154 |  |  | 
| 155 |  | void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1, | 
| 156 |  |                   DES_key_schedule *ks2, DES_key_schedule *ks3) | 
| 157 | 2.69k | { | 
| 158 | 2.69k |     register DES_LONG l, r; | 
| 159 |  |  | 
| 160 | 2.69k |     l = data[0]; | 
| 161 | 2.69k |     r = data[1]; | 
| 162 | 2.69k |     IP(l, r); | 
| 163 | 2.69k |     data[0] = l; | 
| 164 | 2.69k |     data[1] = r; | 
| 165 | 2.69k |     DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT); | 
| 166 | 2.69k |     DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT); | 
| 167 | 2.69k |     DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT); | 
| 168 | 2.69k |     l = data[0]; | 
| 169 | 2.69k |     r = data[1]; | 
| 170 | 2.69k |     FP(r, l); | 
| 171 | 2.69k |     data[0] = l; | 
| 172 | 2.69k |     data[1] = r; | 
| 173 | 2.69k | } | 
| 174 |  |  | 
| 175 |  | void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1, | 
| 176 |  |                   DES_key_schedule *ks2, DES_key_schedule *ks3) | 
| 177 | 58.0k | { | 
| 178 | 58.0k |     register DES_LONG l, r; | 
| 179 |  |  | 
| 180 | 58.0k |     l = data[0]; | 
| 181 | 58.0k |     r = data[1]; | 
| 182 | 58.0k |     IP(l, r); | 
| 183 | 58.0k |     data[0] = l; | 
| 184 | 58.0k |     data[1] = r; | 
| 185 | 58.0k |     DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT); | 
| 186 | 58.0k |     DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT); | 
| 187 | 58.0k |     DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT); | 
| 188 | 58.0k |     l = data[0]; | 
| 189 | 58.0k |     r = data[1]; | 
| 190 | 58.0k |     FP(r, l); | 
| 191 | 58.0k |     data[0] = l; | 
| 192 | 58.0k |     data[1] = r; | 
| 193 | 58.0k | } | 
| 194 |  |  | 
| 195 |  | #ifndef DES_DEFAULT_OPTIONS | 
| 196 |  |  | 
| 197 |  | # undef CBC_ENC_C__DONT_UPDATE_IV | 
| 198 |  | # include "ncbc_enc.c"          /* DES_ncbc_encrypt */ | 
| 199 |  |  | 
| 200 |  | void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output, | 
| 201 |  |                           long length, DES_key_schedule *ks1, | 
| 202 |  |                           DES_key_schedule *ks2, DES_key_schedule *ks3, | 
| 203 |  |                           DES_cblock *ivec, int enc) | 
| 204 | 994 | { | 
| 205 | 994 |     register DES_LONG tin0, tin1; | 
| 206 | 994 |     register DES_LONG tout0, tout1, xor0, xor1; | 
| 207 | 994 |     register const unsigned char *in; | 
| 208 | 994 |     unsigned char *out; | 
| 209 | 994 |     register long l = length; | 
| 210 | 994 |     DES_LONG tin[2]; | 
| 211 | 994 |     unsigned char *iv; | 
| 212 |  |  | 
| 213 | 994 |     in = input; | 
| 214 | 994 |     out = output; | 
| 215 | 994 |     iv = &(*ivec)[0]; | 
| 216 |  |  | 
| 217 | 994 |     if (enc) { | 
| 218 | 589 |         c2l(iv, tout0); | 
| 219 | 589 |         c2l(iv, tout1); | 
| 220 | 3.28k |         for (l -= 8; l >= 0; l -= 8) { | 
| 221 | 2.69k |             c2l(in, tin0); | 
| 222 | 2.69k |             c2l(in, tin1); | 
| 223 | 2.69k |             tin0 ^= tout0; | 
| 224 | 2.69k |             tin1 ^= tout1; | 
| 225 |  |  | 
| 226 | 2.69k |             tin[0] = tin0; | 
| 227 | 2.69k |             tin[1] = tin1; | 
| 228 | 2.69k |             DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3); | 
| 229 | 2.69k |             tout0 = tin[0]; | 
| 230 | 2.69k |             tout1 = tin[1]; | 
| 231 |  |  | 
| 232 | 2.69k |             l2c(tout0, out); | 
| 233 | 2.69k |             l2c(tout1, out); | 
| 234 | 2.69k |         } | 
| 235 | 589 |         if (l != -8) { | 
| 236 | 0 |             c2ln(in, tin0, tin1, l + 8); | 
| 237 | 0 |             tin0 ^= tout0; | 
| 238 | 0 |             tin1 ^= tout1; | 
| 239 |  | 
 | 
| 240 | 0 |             tin[0] = tin0; | 
| 241 | 0 |             tin[1] = tin1; | 
| 242 | 0 |             DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3); | 
| 243 | 0 |             tout0 = tin[0]; | 
| 244 | 0 |             tout1 = tin[1]; | 
| 245 |  | 
 | 
| 246 | 0 |             l2c(tout0, out); | 
| 247 | 0 |             l2c(tout1, out); | 
| 248 | 0 |         } | 
| 249 | 589 |         iv = &(*ivec)[0]; | 
| 250 | 589 |         l2c(tout0, iv); | 
| 251 | 589 |         l2c(tout1, iv); | 
| 252 | 589 |     } else { | 
| 253 | 405 |         register DES_LONG t0, t1; | 
| 254 |  |  | 
| 255 | 405 |         c2l(iv, xor0); | 
| 256 | 405 |         c2l(iv, xor1); | 
| 257 | 58.4k |         for (l -= 8; l >= 0; l -= 8) { | 
| 258 | 58.0k |             c2l(in, tin0); | 
| 259 | 58.0k |             c2l(in, tin1); | 
| 260 |  |  | 
| 261 | 58.0k |             t0 = tin0; | 
| 262 | 58.0k |             t1 = tin1; | 
| 263 |  |  | 
| 264 | 58.0k |             tin[0] = tin0; | 
| 265 | 58.0k |             tin[1] = tin1; | 
| 266 | 58.0k |             DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3); | 
| 267 | 58.0k |             tout0 = tin[0]; | 
| 268 | 58.0k |             tout1 = tin[1]; | 
| 269 |  |  | 
| 270 | 58.0k |             tout0 ^= xor0; | 
| 271 | 58.0k |             tout1 ^= xor1; | 
| 272 | 58.0k |             l2c(tout0, out); | 
| 273 | 58.0k |             l2c(tout1, out); | 
| 274 | 58.0k |             xor0 = t0; | 
| 275 | 58.0k |             xor1 = t1; | 
| 276 | 58.0k |         } | 
| 277 | 405 |         if (l != -8) { | 
| 278 | 0 |             c2l(in, tin0); | 
| 279 | 0 |             c2l(in, tin1); | 
| 280 |  | 
 | 
| 281 | 0 |             t0 = tin0; | 
| 282 | 0 |             t1 = tin1; | 
| 283 |  | 
 | 
| 284 | 0 |             tin[0] = tin0; | 
| 285 | 0 |             tin[1] = tin1; | 
| 286 | 0 |             DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3); | 
| 287 | 0 |             tout0 = tin[0]; | 
| 288 | 0 |             tout1 = tin[1]; | 
| 289 |  | 
 | 
| 290 | 0 |             tout0 ^= xor0; | 
| 291 | 0 |             tout1 ^= xor1; | 
| 292 | 0 |             l2cn(tout0, tout1, out, l + 8); | 
| 293 | 0 |             xor0 = t0; | 
| 294 | 0 |             xor1 = t1; | 
| 295 | 0 |         } | 
| 296 |  |  | 
| 297 | 405 |         iv = &(*ivec)[0]; | 
| 298 | 405 |         l2c(xor0, iv); | 
| 299 | 405 |         l2c(xor1, iv); | 
| 300 | 405 |     } | 
| 301 | 994 |     tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; | 
| 302 | 994 |     tin[0] = tin[1] = 0; | 
| 303 | 994 | } | 
| 304 |  |  | 
| 305 |  | #endif                          /* DES_DEFAULT_OPTIONS */ |