| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #ifndef OPENSSL_HEADER_CRYPTO_DES_INTERNAL_H |
| 16 | #define OPENSSL_HEADER_CRYPTO_DES_INTERNAL_H |
| 17 |
| 18 | #include <openssl/base.h> |
| 19 | #include <openssl/des.h> |
| 20 |
| 21 | #include "../internal.h" |
| 22 |
| 23 | #if defined(__cplusplus) |
| 24 | extern "C" { |
| 25 | #endif |
| 26 |
| 27 |
| 28 | // TODO(davidben): Ideally these macros would be replaced with |
| 29 | // |CRYPTO_load_u32_le| and |CRYPTO_store_u32_le|. |
| 30 |
| 31 | #define c2l(c, l) \ |
| 32 | do { \ |
| 33 | (l) = ((uint32_t)(*((c)++))); \ |
| 34 | (l) |= ((uint32_t)(*((c)++))) << 8L; \ |
| 35 | (l) |= ((uint32_t)(*((c)++))) << 16L; \ |
| 36 | (l) |= ((uint32_t)(*((c)++))) << 24L; \ |
| 37 | } while (0) |
| 38 |
| 39 | #define l2c(l, c) \ |
| 40 | do { \ |
| 41 | *((c)++) = (unsigned char)(((l)) & 0xff); \ |
| 42 | *((c)++) = (unsigned char)(((l) >> 8L) & 0xff); \ |
| 43 | *((c)++) = (unsigned char)(((l) >> 16L) & 0xff); \ |
| 44 | *((c)++) = (unsigned char)(((l) >> 24L) & 0xff); \ |
| 45 | } while (0) |
| 46 |
| 47 | // NOTE - c is not incremented as per c2l |
| 48 | #define c2ln(c, l1, l2, n) \ |
| 49 | do { \ |
| 50 | (c) += (n); \ |
| 51 | (l1) = (l2) = 0; \ |
| 52 | switch (n) { \ |
| 53 | case 8: \ |
| 54 | (l2) = ((uint32_t)(*(--(c)))) << 24L; \ |
| 55 | [[fallthrough]]; \ |
| 56 | case 7: \ |
| 57 | (l2) |= ((uint32_t)(*(--(c)))) << 16L; \ |
| 58 | [[fallthrough]]; \ |
| 59 | case 6: \ |
| 60 | (l2) |= ((uint32_t)(*(--(c)))) << 8L; \ |
| 61 | [[fallthrough]]; \ |
| 62 | case 5: \ |
| 63 | (l2) |= ((uint32_t)(*(--(c)))); \ |
| 64 | [[fallthrough]]; \ |
| 65 | case 4: \ |
| 66 | (l1) = ((uint32_t)(*(--(c)))) << 24L; \ |
| 67 | [[fallthrough]]; \ |
| 68 | case 3: \ |
| 69 | (l1) |= ((uint32_t)(*(--(c)))) << 16L; \ |
| 70 | [[fallthrough]]; \ |
| 71 | case 2: \ |
| 72 | (l1) |= ((uint32_t)(*(--(c)))) << 8L; \ |
| 73 | [[fallthrough]]; \ |
| 74 | case 1: \ |
| 75 | (l1) |= ((uint32_t)(*(--(c)))); \ |
| 76 | } \ |
| 77 | } while (0) |
| 78 |
| 79 | // NOTE - c is not incremented as per l2c |
| 80 | #define l2cn(l1, l2, c, n) \ |
| 81 | do { \ |
| 82 | (c) += (n); \ |
| 83 | switch (n) { \ |
| 84 | case 8: \ |
| 85 | *(--(c)) = (unsigned char)(((l2) >> 24L) & 0xff); \ |
| 86 | [[fallthrough]]; \ |
| 87 | case 7: \ |
| 88 | *(--(c)) = (unsigned char)(((l2) >> 16L) & 0xff); \ |
| 89 | [[fallthrough]]; \ |
| 90 | case 6: \ |
| 91 | *(--(c)) = (unsigned char)(((l2) >> 8L) & 0xff); \ |
| 92 | [[fallthrough]]; \ |
| 93 | case 5: \ |
| 94 | *(--(c)) = (unsigned char)(((l2)) & 0xff); \ |
| 95 | [[fallthrough]]; \ |
| 96 | case 4: \ |
| 97 | *(--(c)) = (unsigned char)(((l1) >> 24L) & 0xff); \ |
| 98 | [[fallthrough]]; \ |
| 99 | case 3: \ |
| 100 | *(--(c)) = (unsigned char)(((l1) >> 16L) & 0xff); \ |
| 101 | [[fallthrough]]; \ |
| 102 | case 2: \ |
| 103 | *(--(c)) = (unsigned char)(((l1) >> 8L) & 0xff); \ |
| 104 | [[fallthrough]]; \ |
| 105 | case 1: \ |
| 106 | *(--(c)) = (unsigned char)(((l1)) & 0xff); \ |
| 107 | } \ |
| 108 | } while (0) |
| 109 |
| 110 |
| 111 | // Correctly-typed versions of DES functions. |
| 112 | // |
| 113 | // See https://crbug.com/boringssl/683. |
| 114 |
| 115 | void DES_set_key_ex(const uint8_t key[8], DES_key_schedule *schedule); |
| 116 | void DES_ecb_encrypt_ex(const uint8_t in[8], uint8_t out[8], |
| 117 | const DES_key_schedule *schedule, int is_encrypt); |
| 118 | void DES_ncbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len, |
| 119 | const DES_key_schedule *schedule, uint8_t ivec[8], |
| 120 | int enc); |
| 121 | void DES_ecb3_encrypt_ex(const uint8_t input[8], uint8_t output[8], |
| 122 | const DES_key_schedule *ks1, |
| 123 | const DES_key_schedule *ks2, |
| 124 | const DES_key_schedule *ks3, int enc); |
| 125 | void DES_ede3_cbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len, |
| 126 | const DES_key_schedule *ks1, |
| 127 | const DES_key_schedule *ks2, |
| 128 | const DES_key_schedule *ks3, uint8_t ivec[8], |
| 129 | int enc); |
| 130 |
| 131 |
| 132 | // Private functions. |
| 133 | // |
| 134 | // These functions are only exported for use in |decrepit|. |
| 135 |
| 136 | OPENSSL_EXPORT void DES_decrypt3(uint32_t data[2], const DES_key_schedule *ks1, |
| 137 | const DES_key_schedule *ks2, |
| 138 | const DES_key_schedule *ks3); |
| 139 |
| 140 | OPENSSL_EXPORT void DES_encrypt3(uint32_t data[2], const DES_key_schedule *ks1, |
| 141 | const DES_key_schedule *ks2, |
| 142 | const DES_key_schedule *ks3); |
| 143 |
| 144 |
| 145 | #if defined(__cplusplus) |
| 146 | } // extern C |
| 147 | #endif |
| 148 |
| 149 | #endif // OPENSSL_HEADER_CRYPTO_DES_INTERNAL_H |
| 150 |