| 1 |
/* |
| 2 |
* Copyright 1995-2022 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 "internal/e_os.h" |
| 17 |
#include "des_local.h" |
| 18 |
#include <assert.h> |
| 19 |
|
| 20 |
/* |
| 21 |
* The input and output are loaded in multiples of 8 bits. What this means is |
| 22 |
* that if you hame numbits=12 and length=2 the first 12 bits will be |
| 23 |
* retrieved from the first byte and half the second. The second 12 bits |
| 24 |
* will come from the 3rd and half the 4th byte. |
| 25 |
*/ |
| 26 |
/* |
| 27 |
* Until Aug 1 2003 this function did not correctly implement CFB-r, so it |
| 28 |
* will not be compatible with any encryption prior to that date. Ben. |
| 29 |
*/ |
| 30 |
void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, |
| 31 |
long length, DES_key_schedule *schedule, |
| 32 |
DES_cblock *ivec, int enc) |
| 33 |
{ |
| 34 |
register DES_LONG d0, d1, v0, v1; |
| 35 |
register unsigned long l = length; |
| 36 |
register int num = numbits / 8, n = (numbits + 7) / 8, i, rem = numbits % 8; |
| 37 |
DES_LONG ti[2]; |
| 38 |
unsigned char *iv; |
| 39 |
#ifndef L_ENDIAN |
| 40 |
unsigned char ovec[16]; |
| 41 |
#else |
| 42 |
unsigned int sh[4]; |
| 43 |
unsigned char *ovec = (unsigned char *)sh; |
| 44 |
|
| 45 |
/* I kind of count that compiler optimizes away this assertion, */ |
| 46 |
assert(sizeof(sh[0]) == 4); /* as this holds true for all, */ |
| 47 |
/* but 16-bit platforms... */ |
| 48 |
|
| 49 |
#endif |
| 50 |
|
| 51 |
if (numbits <= 0 || numbits > 64) |
| 52 |
return; |
| 53 |
iv = &(*ivec)[0]; |
| 54 |
c2l(iv, v0); |
| 55 |
c2l(iv, v1); |
| 56 |
if (enc) { |
| 57 |
while (l >= (unsigned long)n) { |
| 58 |
l -= n; |
| 59 |
ti[0] = v0; |
| 60 |
ti[1] = v1; |
| 61 |
DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT); |
| 62 |
c2ln(in, d0, d1, n); |
| 63 |
in += n; |
| 64 |
d0 ^= ti[0]; |
| 65 |
d1 ^= ti[1]; |
| 66 |
l2cn(d0, d1, out, n); |
| 67 |
out += n; |
| 68 |
/* |
| 69 |
* 30-08-94 - eay - changed because l>>32 and l<<32 are bad under |
| 70 |
* gcc :-( |
| 71 |
*/ |
| 72 |
if (numbits == 32) { |
| 73 |
v0 = v1; |
| 74 |
v1 = d0; |
| 75 |
} else if (numbits == 64) { |
| 76 |
v0 = d0; |
| 77 |
v1 = d1; |
| 78 |
} else { |
| 79 |
#ifndef L_ENDIAN |
| 80 |
iv = &ovec[0]; |
| 81 |
l2c(v0, iv); |
| 82 |
l2c(v1, iv); |
| 83 |
l2c(d0, iv); |
| 84 |
l2c(d1, iv); |
| 85 |
#else |
| 86 |
sh[0] = v0, sh[1] = v1, sh[2] = d0, sh[3] = d1; |
| 87 |
#endif |
| 88 |
if (rem == 0) |
| 89 |
memmove(ovec, ovec + num, 8); |
| 90 |
else |
| 91 |
for (i = 0; i < 8; ++i) |
| 92 |
ovec[i] = ovec[i + num] << rem | ovec[i + num + 1] >> (8 - rem); |
| 93 |
#ifdef L_ENDIAN |
| 94 |
v0 = sh[0], v1 = sh[1]; |
| 95 |
#else |
| 96 |
iv = &ovec[0]; |
| 97 |
c2l(iv, v0); |
| 98 |
c2l(iv, v1); |
| 99 |
#endif |
| 100 |
} |
| 101 |
} |
| 102 |
} else { |
| 103 |
while (l >= (unsigned long)n) { |
| 104 |
l -= n; |
| 105 |
ti[0] = v0; |
| 106 |
ti[1] = v1; |
| 107 |
DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT); |
| 108 |
c2ln(in, d0, d1, n); |
| 109 |
in += n; |
| 110 |
/* |
| 111 |
* 30-08-94 - eay - changed because l>>32 and l<<32 are bad under |
| 112 |
* gcc :-( |
| 113 |
*/ |
| 114 |
if (numbits == 32) { |
| 115 |
v0 = v1; |
| 116 |
v1 = d0; |
| 117 |
} else if (numbits == 64) { |
| 118 |
v0 = d0; |
| 119 |
v1 = d1; |
| 120 |
} else { |
| 121 |
#ifndef L_ENDIAN |
| 122 |
iv = &ovec[0]; |
| 123 |
l2c(v0, iv); |
| 124 |
l2c(v1, iv); |
| 125 |
l2c(d0, iv); |
| 126 |
l2c(d1, iv); |
| 127 |
#else |
| 128 |
sh[0] = v0, sh[1] = v1, sh[2] = d0, sh[3] = d1; |
| 129 |
#endif |
| 130 |
if (rem == 0) |
| 131 |
memmove(ovec, ovec + num, 8); |
| 132 |
else |
| 133 |
for (i = 0; i < 8; ++i) |
| 134 |
ovec[i] = ovec[i + num] << rem | ovec[i + num + 1] >> (8 - rem); |
| 135 |
#ifdef L_ENDIAN |
| 136 |
v0 = sh[0], v1 = sh[1]; |
| 137 |
#else |
| 138 |
iv = &ovec[0]; |
| 139 |
c2l(iv, v0); |
| 140 |
c2l(iv, v1); |
| 141 |
#endif |
| 142 |
} |
| 143 |
d0 ^= ti[0]; |
| 144 |
d1 ^= ti[1]; |
| 145 |
l2cn(d0, d1, out, n); |
| 146 |
out += n; |
| 147 |
} |
| 148 |
} |
| 149 |
iv = &(*ivec)[0]; |
| 150 |
l2c(v0, iv); |
| 151 |
l2c(v1, iv); |
| 152 |
v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0; |
| 153 |
} |