rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | /* Copyright (C) 1996 Eric Young (eay@mincom.oz.au) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@mincom.oz.au). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@mincom.oz.au). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@mincom.oz.au)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@mincom.oz.au)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | |
| 58 | /***************************************************************************** |
| 59 | * |
| 60 | * Filename: |
| 61 | * --------- |
| 62 | * des.h |
| 63 | * |
| 64 | * Project: |
| 65 | * -------- |
| 66 | * Maui_Software |
| 67 | * |
| 68 | * Description: |
| 69 | * ------------ |
| 70 | * des software function. |
| 71 | * |
| 72 | * Author: |
| 73 | * ------- |
| 74 | * ------- |
| 75 | * |
| 76 | *============================================================================ |
| 77 | * HISTORY |
| 78 | * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 79 | *------------------------------------------------------------------------------ |
| 80 | * removed! |
| 81 | * |
| 82 | * removed! |
| 83 | * removed! |
| 84 | * removed! |
| 85 | * |
| 86 | * removed! |
| 87 | * removed! |
| 88 | * removed! |
| 89 | * |
| 90 | * removed! |
| 91 | * removed! |
| 92 | * removed! |
| 93 | * |
| 94 | * removed! |
| 95 | * removed! |
| 96 | * removed! |
| 97 | * |
| 98 | * removed! |
| 99 | * removed! |
| 100 | * removed! |
| 101 | * |
| 102 | * removed! |
| 103 | * removed! |
| 104 | * removed! |
| 105 | * |
| 106 | * removed! |
| 107 | * removed! |
| 108 | * removed! |
| 109 | * |
| 110 | * removed! |
| 111 | * removed! |
| 112 | * removed! |
| 113 | * |
| 114 | * removed! |
| 115 | * removed! |
| 116 | * removed! |
| 117 | * |
| 118 | *------------------------------------------------------------------------------ |
| 119 | * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 120 | *============================================================================ |
| 121 | ****************************************************************************/ |
| 122 | |
| 123 | #ifndef __DES_H__ |
| 124 | #define __DES_H__ |
| 125 | |
| 126 | #include "kal_general_types.h" |
| 127 | |
| 128 | typedef kal_uint8 des_cblock[8]; |
| 129 | typedef struct des_ks_struct{ |
| 130 | union{ |
| 131 | des_cblock _; |
| 132 | kal_uint32 pad[2]; |
| 133 | } ks; |
| 134 | #undef _ |
| 135 | #define _ ks._ |
| 136 | } des_key_schedule[16]; |
| 137 | |
| 138 | |
| 139 | #define DES_KEY_SZ (sizeof(des_cblock)) |
| 140 | #define DES_SCHEDULE_SZ (sizeof(des_key_schedule)) |
| 141 | |
| 142 | #define DES_ENCRYPT 1 |
| 143 | #define DES_DECRYPT 0 |
| 144 | |
| 145 | #define DES_CBC_MODE 0 |
| 146 | #define DES_PCBC_MODE 1 |
| 147 | |
| 148 | #define C_Block des_cblock |
| 149 | #define Key_schedule des_key_schedule |
| 150 | |
| 151 | |
| 152 | #define ITERATIONS 16 |
| 153 | #define HALF_ITERATIONS 8 |
| 154 | |
| 155 | #define c2l(c,l)(l =((kal_uint32)(*((c)++))) , \ |
| 156 | l|=((kal_uint32)(*((c)++)))<< 8L, \ |
| 157 | l|=((kal_uint32)(*((c)++)))<<16L, \ |
| 158 | l|=((kal_uint32)(*((c)++)))<<24L) |
| 159 | |
| 160 | |
| 161 | #define l2c(l,c)(*((c)++)=(kal_uint8)(((l) )&0xff), \ |
| 162 | *((c)++)=(kal_uint8)(((l)>> 8L)&0xff), \ |
| 163 | *((c)++)=(kal_uint8)(((l)>>16L)&0xff), \ |
| 164 | *((c)++)=(kal_uint8)(((l)>>24L)&0xff)) |
| 165 | |
| 166 | |
| 167 | |
| 168 | #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n)))) |
| 169 | |
| 170 | #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g) |
| 171 | #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \ |
| 172 | u=R^s[S ]; \ |
| 173 | t=R^s[S+1] |
| 174 | |
| 175 | #define D_ENCRYPT(LL,R,S) {\ |
| 176 | LOAD_DATA_tmp(R,S,u,t,E0,E1); \ |
| 177 | t=ROTATE(t,4); \ |
| 178 | LL^=\ |
| 179 | des_SPtrans[0][(u>> 2L)&0x3f]^ \ |
| 180 | des_SPtrans[2][(u>>10L)&0x3f]^ \ |
| 181 | des_SPtrans[4][(u>>18L)&0x3f]^ \ |
| 182 | des_SPtrans[6][(u>>26L)&0x3f]^ \ |
| 183 | des_SPtrans[1][(t>> 2L)&0x3f]^ \ |
| 184 | des_SPtrans[3][(t>>10L)&0x3f]^ \ |
| 185 | des_SPtrans[5][(t>>18L)&0x3f]^ \ |
| 186 | des_SPtrans[7][(t>>26L)&0x3f]; } |
| 187 | |
| 188 | #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\ |
| 189 | (b)^=(t),\ |
| 190 | (a)^=((t)<<(n))) |
| 191 | |
| 192 | #define IP(l,r) \ |
| 193 | { \ |
| 194 | register kal_uint32 tt; \ |
| 195 | PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \ |
| 196 | PERM_OP(l,r,tt,16,0x0000ffffL); \ |
| 197 | PERM_OP(r,l,tt, 2,0x33333333L); \ |
| 198 | PERM_OP(l,r,tt, 8,0x00ff00ffL); \ |
| 199 | PERM_OP(r,l,tt, 1,0x55555555L); \ |
| 200 | } |
| 201 | |
| 202 | #define FP(l,r) \ |
| 203 | { \ |
| 204 | register kal_uint32 tt; \ |
| 205 | PERM_OP(l,r,tt, 1,0x55555555L); \ |
| 206 | PERM_OP(r,l,tt, 8,0x00ff00ffL); \ |
| 207 | PERM_OP(l,r,tt, 2,0x33333333L); \ |
| 208 | PERM_OP(r,l,tt,16,0x0000ffffL); \ |
| 209 | PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \ |
| 210 | } |
| 211 | |
| 212 | |
| 213 | #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ |
| 214 | (a)=(a)^(t)^(t>>(16-(n)))) |
| 215 | |
| 216 | |
| 217 | |
| 218 | kal_int32 CHE_des_key_sched(des_cblock *key, des_key_schedule *schedule); |
| 219 | void CHE_des_ecb_encrypt(des_cblock *input, des_cblock *output, des_key_schedule *ks, kal_int32 encrypt); |
| 220 | |
| 221 | #endif //__DES_H__ |