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