| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_DES_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_DES_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
#include <openssl/des.h> |
| 9 |
|
| 10 |
#include "../internal.h" |
| 11 |
|
| 12 |
#if defined(__cplusplus) |
| 13 |
extern "C" { |
| 14 |
#endif |
| 15 |
|
| 16 |
|
| 17 |
// TODO(davidben): Ideally these macros would be replaced with |
| 18 |
// |CRYPTO_load_u32_le| and |CRYPTO_store_u32_le|. |
| 19 |
|
| 20 |
#define c2l(c, l) \ |
| 21 |
do { \ |
| 22 |
(l) = ((uint32_t)(*((c)++))); \ |
| 23 |
(l) |= ((uint32_t)(*((c)++))) << 8L; \ |
| 24 |
(l) |= ((uint32_t)(*((c)++))) << 16L; \ |
| 25 |
(l) |= ((uint32_t)(*((c)++))) << 24L; \ |
| 26 |
} while (0) |
| 27 |
|
| 28 |
#define l2c(l, c) \ |
| 29 |
do { \ |
| 30 |
*((c)++) = (unsigned char)(((l)) & 0xff); \ |
| 31 |
*((c)++) = (unsigned char)(((l) >> 8L) & 0xff); \ |
| 32 |
*((c)++) = (unsigned char)(((l) >> 16L) & 0xff); \ |
| 33 |
*((c)++) = (unsigned char)(((l) >> 24L) & 0xff); \ |
| 34 |
} while (0) |
| 35 |
|
| 36 |
// NOTE - c is not incremented as per c2l |
| 37 |
#define c2ln(c, l1, l2, n) \ |
| 38 |
do { \ |
| 39 |
(c) += (n); \ |
| 40 |
(l1) = (l2) = 0; \ |
| 41 |
switch (n) { \ |
| 42 |
case 8: \ |
| 43 |
(l2) = ((uint32_t)(*(--(c)))) << 24L; \ |
| 44 |
OPENSSL_FALLTHROUGH; \ |
| 45 |
case 7: \ |
| 46 |
(l2) |= ((uint32_t)(*(--(c)))) << 16L; \ |
| 47 |
OPENSSL_FALLTHROUGH; \ |
| 48 |
case 6: \ |
| 49 |
(l2) |= ((uint32_t)(*(--(c)))) << 8L; \ |
| 50 |
OPENSSL_FALLTHROUGH; \ |
| 51 |
case 5: \ |
| 52 |
(l2) |= ((uint32_t)(*(--(c)))); \ |
| 53 |
OPENSSL_FALLTHROUGH; \ |
| 54 |
case 4: \ |
| 55 |
(l1) = ((uint32_t)(*(--(c)))) << 24L; \ |
| 56 |
OPENSSL_FALLTHROUGH; \ |
| 57 |
case 3: \ |
| 58 |
(l1) |= ((uint32_t)(*(--(c)))) << 16L; \ |
| 59 |
OPENSSL_FALLTHROUGH; \ |
| 60 |
case 2: \ |
| 61 |
(l1) |= ((uint32_t)(*(--(c)))) << 8L; \ |
| 62 |
OPENSSL_FALLTHROUGH; \ |
| 63 |
case 1: \ |
| 64 |
(l1) |= ((uint32_t)(*(--(c)))); \ |
| 65 |
} \ |
| 66 |
} while (0) |
| 67 |
|
| 68 |
// NOTE - c is not incremented as per l2c |
| 69 |
#define l2cn(l1, l2, c, n) \ |
| 70 |
do { \ |
| 71 |
(c) += (n); \ |
| 72 |
switch (n) { \ |
| 73 |
case 8: \ |
| 74 |
*(--(c)) = (unsigned char)(((l2) >> 24L) & 0xff); \ |
| 75 |
OPENSSL_FALLTHROUGH; \ |
| 76 |
case 7: \ |
| 77 |
*(--(c)) = (unsigned char)(((l2) >> 16L) & 0xff); \ |
| 78 |
OPENSSL_FALLTHROUGH; \ |
| 79 |
case 6: \ |
| 80 |
*(--(c)) = (unsigned char)(((l2) >> 8L) & 0xff); \ |
| 81 |
OPENSSL_FALLTHROUGH; \ |
| 82 |
case 5: \ |
| 83 |
*(--(c)) = (unsigned char)(((l2)) & 0xff); \ |
| 84 |
OPENSSL_FALLTHROUGH; \ |
| 85 |
case 4: \ |
| 86 |
*(--(c)) = (unsigned char)(((l1) >> 24L) & 0xff); \ |
| 87 |
OPENSSL_FALLTHROUGH; \ |
| 88 |
case 3: \ |
| 89 |
*(--(c)) = (unsigned char)(((l1) >> 16L) & 0xff); \ |
| 90 |
OPENSSL_FALLTHROUGH; \ |
| 91 |
case 2: \ |
| 92 |
*(--(c)) = (unsigned char)(((l1) >> 8L) & 0xff); \ |
| 93 |
OPENSSL_FALLTHROUGH; \ |
| 94 |
case 1: \ |
| 95 |
*(--(c)) = (unsigned char)(((l1)) & 0xff); \ |
| 96 |
} \ |
| 97 |
} while (0) |
| 98 |
|
| 99 |
|
| 100 |
// Correctly-typed versions of DES functions. |
| 101 |
// |
| 102 |
// See https://crbug.com/boringssl/683. |
| 103 |
|
| 104 |
void DES_set_key_ex(const uint8_t key[8], DES_key_schedule *schedule); |
| 105 |
void DES_ecb_encrypt_ex(const uint8_t in[8], uint8_t out[8], |
| 106 |
const DES_key_schedule *schedule, int is_encrypt); |
| 107 |
void DES_ncbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len, |
| 108 |
const DES_key_schedule *schedule, uint8_t ivec[8], |
| 109 |
int enc); |
| 110 |
void DES_ecb3_encrypt_ex(const uint8_t input[8], uint8_t output[8], |
| 111 |
const DES_key_schedule *ks1, |
| 112 |
const DES_key_schedule *ks2, |
| 113 |
const DES_key_schedule *ks3, int enc); |
| 114 |
void DES_ede3_cbc_encrypt_ex(const uint8_t *in, uint8_t *out, size_t len, |
| 115 |
const DES_key_schedule *ks1, |
| 116 |
const DES_key_schedule *ks2, |
| 117 |
const DES_key_schedule *ks3, uint8_t ivec[8], |
| 118 |
int enc); |
| 119 |
|
| 120 |
|
| 121 |
// Private functions. |
| 122 |
// |
| 123 |
// These functions are only exported for use in |decrepit|. |
| 124 |
|
| 125 |
OPENSSL_EXPORT void DES_decrypt3(uint32_t data[2], const DES_key_schedule *ks1, |
| 126 |
const DES_key_schedule *ks2, |
| 127 |
const DES_key_schedule *ks3); |
| 128 |
|
| 129 |
OPENSSL_EXPORT void DES_encrypt3(uint32_t data[2], const DES_key_schedule *ks1, |
| 130 |
const DES_key_schedule *ks2, |
| 131 |
const DES_key_schedule *ks3); |
| 132 |
|
| 133 |
|
| 134 |
#if defined(__cplusplus) |
| 135 |
} // extern C |
| 136 |
#endif |
| 137 |
|
| 138 |
#endif // OPENSSL_HEADER_DES_INTERNAL_H |