lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (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 | #include "des_local.h" |
| 11 | |
| 12 | /* |
| 13 | * The input and output encrypted as though 64bit cfb mode is being used. |
| 14 | * The extra state information to record how much of the 64bit block we have |
| 15 | * used is contained in *num; |
| 16 | */ |
| 17 | |
| 18 | void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, |
| 19 | long length, DES_key_schedule *ks1, |
| 20 | DES_key_schedule *ks2, DES_key_schedule *ks3, |
| 21 | DES_cblock *ivec, int *num, int enc) |
| 22 | { |
| 23 | register DES_LONG v0, v1; |
| 24 | register long l = length; |
| 25 | register int n = *num; |
| 26 | DES_LONG ti[2]; |
| 27 | unsigned char *iv, c, cc; |
| 28 | |
| 29 | iv = &(*ivec)[0]; |
| 30 | if (enc) { |
| 31 | while (l--) { |
| 32 | if (n == 0) { |
| 33 | c2l(iv, v0); |
| 34 | c2l(iv, v1); |
| 35 | |
| 36 | ti[0] = v0; |
| 37 | ti[1] = v1; |
| 38 | DES_encrypt3(ti, ks1, ks2, ks3); |
| 39 | v0 = ti[0]; |
| 40 | v1 = ti[1]; |
| 41 | |
| 42 | iv = &(*ivec)[0]; |
| 43 | l2c(v0, iv); |
| 44 | l2c(v1, iv); |
| 45 | iv = &(*ivec)[0]; |
| 46 | } |
| 47 | c = *(in++) ^ iv[n]; |
| 48 | *(out++) = c; |
| 49 | iv[n] = c; |
| 50 | n = (n + 1) & 0x07; |
| 51 | } |
| 52 | } else { |
| 53 | while (l--) { |
| 54 | if (n == 0) { |
| 55 | c2l(iv, v0); |
| 56 | c2l(iv, v1); |
| 57 | |
| 58 | ti[0] = v0; |
| 59 | ti[1] = v1; |
| 60 | DES_encrypt3(ti, ks1, ks2, ks3); |
| 61 | v0 = ti[0]; |
| 62 | v1 = ti[1]; |
| 63 | |
| 64 | iv = &(*ivec)[0]; |
| 65 | l2c(v0, iv); |
| 66 | l2c(v1, iv); |
| 67 | iv = &(*ivec)[0]; |
| 68 | } |
| 69 | cc = *(in++); |
| 70 | c = iv[n]; |
| 71 | iv[n] = cc; |
| 72 | *(out++) = c ^ cc; |
| 73 | n = (n + 1) & 0x07; |
| 74 | } |
| 75 | } |
| 76 | v0 = v1 = ti[0] = ti[1] = c = cc = 0; |
| 77 | *num = n; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * This is compatible with the single key CFB-r for DES, even thought that's |
| 82 | * not what EVP needs. |
| 83 | */ |
| 84 | |
| 85 | void DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out, |
| 86 | int numbits, long length, DES_key_schedule *ks1, |
| 87 | DES_key_schedule *ks2, DES_key_schedule *ks3, |
| 88 | DES_cblock *ivec, int enc) |
| 89 | { |
| 90 | register DES_LONG d0, d1, v0, v1; |
| 91 | register unsigned long l = length, n = ((unsigned int)numbits + 7) / 8; |
| 92 | register int num = numbits, i; |
| 93 | DES_LONG ti[2]; |
| 94 | unsigned char *iv; |
| 95 | unsigned char ovec[16]; |
| 96 | |
| 97 | if (num > 64) |
| 98 | return; |
| 99 | iv = &(*ivec)[0]; |
| 100 | c2l(iv, v0); |
| 101 | c2l(iv, v1); |
| 102 | if (enc) { |
| 103 | while (l >= n) { |
| 104 | l -= n; |
| 105 | ti[0] = v0; |
| 106 | ti[1] = v1; |
| 107 | DES_encrypt3(ti, ks1, ks2, ks3); |
| 108 | c2ln(in, d0, d1, n); |
| 109 | in += n; |
| 110 | d0 ^= ti[0]; |
| 111 | d1 ^= ti[1]; |
| 112 | l2cn(d0, d1, out, n); |
| 113 | out += n; |
| 114 | /* |
| 115 | * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under |
| 116 | * gcc :-( |
| 117 | */ |
| 118 | if (num == 32) { |
| 119 | v0 = v1; |
| 120 | v1 = d0; |
| 121 | } else if (num == 64) { |
| 122 | v0 = d0; |
| 123 | v1 = d1; |
| 124 | } else { |
| 125 | iv = &ovec[0]; |
| 126 | l2c(v0, iv); |
| 127 | l2c(v1, iv); |
| 128 | l2c(d0, iv); |
| 129 | l2c(d1, iv); |
| 130 | /* shift ovec left most of the bits... */ |
| 131 | memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0)); |
| 132 | /* now the remaining bits */ |
| 133 | if (num % 8 != 0) |
| 134 | for (i = 0; i < 8; ++i) { |
| 135 | ovec[i] <<= num % 8; |
| 136 | ovec[i] |= ovec[i + 1] >> (8 - num % 8); |
| 137 | } |
| 138 | iv = &ovec[0]; |
| 139 | c2l(iv, v0); |
| 140 | c2l(iv, v1); |
| 141 | } |
| 142 | } |
| 143 | } else { |
| 144 | while (l >= n) { |
| 145 | l -= n; |
| 146 | ti[0] = v0; |
| 147 | ti[1] = v1; |
| 148 | DES_encrypt3(ti, ks1, ks2, ks3); |
| 149 | c2ln(in, d0, d1, n); |
| 150 | in += n; |
| 151 | /* |
| 152 | * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under |
| 153 | * gcc :-( |
| 154 | */ |
| 155 | if (num == 32) { |
| 156 | v0 = v1; |
| 157 | v1 = d0; |
| 158 | } else if (num == 64) { |
| 159 | v0 = d0; |
| 160 | v1 = d1; |
| 161 | } else { |
| 162 | iv = &ovec[0]; |
| 163 | l2c(v0, iv); |
| 164 | l2c(v1, iv); |
| 165 | l2c(d0, iv); |
| 166 | l2c(d1, iv); |
| 167 | /* shift ovec left most of the bits... */ |
| 168 | memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0)); |
| 169 | /* now the remaining bits */ |
| 170 | if (num % 8 != 0) |
| 171 | for (i = 0; i < 8; ++i) { |
| 172 | ovec[i] <<= num % 8; |
| 173 | ovec[i] |= ovec[i + 1] >> (8 - num % 8); |
| 174 | } |
| 175 | iv = &ovec[0]; |
| 176 | c2l(iv, v0); |
| 177 | c2l(iv, v1); |
| 178 | } |
| 179 | d0 ^= ti[0]; |
| 180 | d1 ^= ti[1]; |
| 181 | l2cn(d0, d1, out, n); |
| 182 | out += n; |
| 183 | } |
| 184 | } |
| 185 | iv = &(*ivec)[0]; |
| 186 | l2c(v0, iv); |
| 187 | l2c(v1, iv); |
| 188 | v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0; |
| 189 | } |