rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | /* crypto/md32_common.h */ |
| 2 | /* ==================================================================== |
| 3 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * 3. All advertising materials mentioning features or use of this |
| 18 | * software must display the following acknowledgment: |
| 19 | * "This product includes software developed by the OpenSSL Project |
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 21 | * |
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 23 | * endorse or promote products derived from this software without |
| 24 | * prior written permission. For written permission, please contact |
| 25 | * licensing@OpenSSL.org. |
| 26 | * |
| 27 | * 5. Products derived from this software may not be called "OpenSSL" |
| 28 | * nor may "OpenSSL" appear in their names without prior written |
| 29 | * permission of the OpenSSL Project. |
| 30 | * |
| 31 | * 6. Redistributions of any form whatsoever must retain the following |
| 32 | * acknowledgment: |
| 33 | * "This product includes software developed by the OpenSSL Project |
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | * ==================================================================== |
| 49 | * |
| 50 | */ |
| 51 | |
| 52 | /* |
| 53 | * This is a generic 32 bit "collector" for message digest algorithms. |
| 54 | * Whenever needed it collects input character stream into chunks of |
| 55 | * 32 bit values and invokes a block function that performs actual hash |
| 56 | * calculations. |
| 57 | * |
| 58 | * Porting guide. |
| 59 | * |
| 60 | * Obligatory macros: |
| 61 | * |
| 62 | * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN |
| 63 | * this macro defines byte order of input stream. |
| 64 | * HASH_CBLOCK |
| 65 | * size of a unit chunk HASH_BLOCK operates on. |
| 66 | * HASH_LONG |
| 67 | * has to be at lest 32 bit wide, if it's wider, then |
| 68 | * HASH_LONG_LOG2 *has to* be defined along |
| 69 | * HASH_CTX |
| 70 | * context structure that at least contains following |
| 71 | * members: |
| 72 | * typedef struct { |
| 73 | * ... |
| 74 | * HASH_LONG Nl,Nh; |
| 75 | * either { |
| 76 | * HASH_LONG data[HASH_LBLOCK]; |
| 77 | * unsigned char data[HASH_CBLOCK]; |
| 78 | * }; |
| 79 | * unsigned int num; |
| 80 | * ... |
| 81 | * } HASH_CTX; |
| 82 | * data[] vector is expected to be zeroed upon first call to |
| 83 | * HASH_UPDATE. |
| 84 | * HASH_UPDATE |
| 85 | * name of "Update" function, implemented here. |
| 86 | * HASH_TRANSFORM |
| 87 | * name of "Transform" function, implemented here. |
| 88 | * HASH_FINAL |
| 89 | * name of "Final" function, implemented here. |
| 90 | * HASH_BLOCK_DATA_ORDER |
| 91 | * name of "block" function capable of treating *unaligned* input |
| 92 | * message in original (data) byte order, implemented externally. |
| 93 | * HASH_MAKE_STRING |
| 94 | * macro convering context variables to an ASCII hash string. |
| 95 | * |
| 96 | * MD5 example: |
| 97 | * |
| 98 | * #define DATA_ORDER_IS_LITTLE_ENDIAN |
| 99 | * |
| 100 | * #define HASH_LONG MD5_LONG |
| 101 | * #define HASH_LONG_LOG2 MD5_LONG_LOG2 |
| 102 | * #define HASH_CTX MD5_CTX |
| 103 | * #define HASH_CBLOCK MD5_CBLOCK |
| 104 | * #define HASH_UPDATE MD5_Update |
| 105 | * #define HASH_TRANSFORM MD5_Transform |
| 106 | * #define HASH_FINAL MD5_Final |
| 107 | * #define HASH_BLOCK_DATA_ORDER md5_block_data_order |
| 108 | * |
| 109 | * <appro@fy.chalmers.se> |
| 110 | */ |
| 111 | /***************************************************************************** |
| 112 | * |
| 113 | * Filename: |
| 114 | * --------- |
| 115 | * md32_common.h |
| 116 | * |
| 117 | * Project: |
| 118 | * -------- |
| 119 | * Maui_Software |
| 120 | * |
| 121 | * Description: |
| 122 | * ------------ |
| 123 | * SHA1 Engine |
| 124 | * |
| 125 | * Author: |
| 126 | * ------- |
| 127 | * Leona Chou (mtk01890) |
| 128 | * |
| 129 | *============================================================================ |
| 130 | * HISTORY |
| 131 | * Below this line, this part is controlled by ClearCase. DO NOT MODIFY!! |
| 132 | *------------------------------------------------------------------------------ |
| 133 | * $Log$ |
| 134 | * |
| 135 | * 10 06 2015 raymondwt.chen |
| 136 | * [MOLY00144208] [Jade] Fix che/sys_svc_sec/sys_svc build warnings |
| 137 | * .Fix build warnings |
| 138 | * |
| 139 | * 03 16 2011 leona.chou |
| 140 | * removed! |
| 141 | * . |
| 142 | * |
| 143 | *------------------------------------------------------------------------------ |
| 144 | * Upper this line, this part is controlled by ClearCase. DO NOT MODIFY!! |
| 145 | *============================================================================ |
| 146 | ****************************************************************************/ |
| 147 | #ifndef MD32_COMMON_H |
| 148 | #define MD32_COMMON_H |
| 149 | |
| 150 | #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
| 151 | #error "DATA_ORDER must be defined!" |
| 152 | #endif |
| 153 | |
| 154 | #ifndef HASH_CBLOCK |
| 155 | #error "HASH_CBLOCK must be defined!" |
| 156 | #endif |
| 157 | #ifndef HASH_LONG |
| 158 | #error "HASH_LONG must be defined!" |
| 159 | #endif |
| 160 | #ifndef HASH_CTX |
| 161 | #error "HASH_CTX must be defined!" |
| 162 | #endif |
| 163 | |
| 164 | #ifndef HASH_UPDATE |
| 165 | #error "HASH_UPDATE must be defined!" |
| 166 | #endif |
| 167 | #ifndef HASH_TRANSFORM |
| 168 | #error "HASH_TRANSFORM must be defined!" |
| 169 | #endif |
| 170 | #ifndef HASH_FINAL |
| 171 | #error "HASH_FINAL must be defined!" |
| 172 | #endif |
| 173 | |
| 174 | #ifndef HASH_BLOCK_DATA_ORDER |
| 175 | #error "HASH_BLOCK_DATA_ORDER must be defined!" |
| 176 | #endif |
| 177 | |
| 178 | #undef ROTATE |
| 179 | #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) |
| 180 | |
| 181 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
| 182 | #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ |
| 183 | l|=(((unsigned long)(*((c)++)))<<16), \ |
| 184 | l|=(((unsigned long)(*((c)++)))<< 8), \ |
| 185 | l|=(((unsigned long)(*((c)++))) )) |
| 186 | |
| 187 | #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ |
| 188 | *((c)++)=(unsigned char)(((l)>>16)&0xff), \ |
| 189 | *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ |
| 190 | *((c)++)=(unsigned char)(((l) )&0xff)) |
| 191 | |
| 192 | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
| 193 | #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \ |
| 194 | l|=(((unsigned long)(*((c)++)))<< 8), \ |
| 195 | l|=(((unsigned long)(*((c)++)))<<16), \ |
| 196 | l|=(((unsigned long)(*((c)++)))<<24)) |
| 197 | |
| 198 | #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ |
| 199 | *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ |
| 200 | *((c)++)=(unsigned char)(((l)>>16)&0xff), \ |
| 201 | *((c)++)=(unsigned char)(((l)>>24)&0xff)) |
| 202 | |
| 203 | #endif |
| 204 | |
| 205 | /* |
| 206 | * Time for some action:-) |
| 207 | */ |
| 208 | |
| 209 | int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len) |
| 210 | { |
| 211 | const unsigned char *data=data_; |
| 212 | unsigned char *p; |
| 213 | HASH_LONG l; |
| 214 | size_t n; |
| 215 | |
| 216 | if (len==0) return 1; |
| 217 | |
| 218 | l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL; |
| 219 | /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to |
| 220 | * Wei Dai <weidai@eskimo.com> for pointing it out. */ |
| 221 | if (l < c->Nl) /* overflow */ |
| 222 | c->Nh++; |
| 223 | c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */ |
| 224 | c->Nl=l; |
| 225 | |
| 226 | n = c->num; |
| 227 | if (n != 0) |
| 228 | { |
| 229 | p=(unsigned char *)c->data; |
| 230 | |
| 231 | if (len >= HASH_CBLOCK || len+n >= HASH_CBLOCK) |
| 232 | { |
| 233 | memcpy (p+n,data,HASH_CBLOCK-n); |
| 234 | HASH_BLOCK_DATA_ORDER (c,p,1); |
| 235 | n = HASH_CBLOCK-n; |
| 236 | data += n; |
| 237 | len -= n; |
| 238 | c->num = 0; |
| 239 | memset (p,0,HASH_CBLOCK); /* keep it zeroed */ |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | memcpy (p+n,data,len); |
| 244 | c->num += (unsigned int)len; |
| 245 | return 1; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | n = len/HASH_CBLOCK; |
| 250 | if (n > 0) |
| 251 | { |
| 252 | HASH_BLOCK_DATA_ORDER (c,data,n); |
| 253 | n *= HASH_CBLOCK; |
| 254 | data += n; |
| 255 | len -= n; |
| 256 | } |
| 257 | |
| 258 | if (len != 0) |
| 259 | { |
| 260 | p = (unsigned char *)c->data; |
| 261 | c->num = (unsigned int)len; |
| 262 | memcpy (p,data,len); |
| 263 | } |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data) |
| 269 | { |
| 270 | HASH_BLOCK_DATA_ORDER (c,data,1); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | int HASH_FINAL (unsigned char *md, HASH_CTX *c) |
| 275 | { |
| 276 | unsigned char *p = (unsigned char *)c->data; |
| 277 | size_t n = c->num; |
| 278 | |
| 279 | p[n] = 0x80; /* there is always room for one */ |
| 280 | n++; |
| 281 | |
| 282 | if (n > (HASH_CBLOCK-8)) |
| 283 | { |
| 284 | memset (p+n,0,HASH_CBLOCK-n); |
| 285 | n=0; |
| 286 | HASH_BLOCK_DATA_ORDER (c,p,1); |
| 287 | } |
| 288 | memset (p+n,0,HASH_CBLOCK-8-n); |
| 289 | |
| 290 | p += HASH_CBLOCK-8; |
| 291 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
| 292 | (void)HOST_l2c(c->Nh,p); |
| 293 | (void)HOST_l2c(c->Nl,p); |
| 294 | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
| 295 | (void)HOST_l2c(c->Nl,p); |
| 296 | (void)HOST_l2c(c->Nh,p); |
| 297 | #endif |
| 298 | p -= HASH_CBLOCK; |
| 299 | HASH_BLOCK_DATA_ORDER (c,p,1); |
| 300 | c->num=0; |
| 301 | memset (p,0,HASH_CBLOCK); |
| 302 | |
| 303 | #ifndef HASH_MAKE_STRING |
| 304 | #error "HASH_MAKE_STRING must be defined!" |
| 305 | #else |
| 306 | HASH_MAKE_STRING(c,md); |
| 307 | #endif |
| 308 | |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | #ifndef MD32_REG_T |
| 313 | #define MD32_REG_T long |
| 314 | /* |
| 315 | * This comment was originaly written for MD5, which is why it |
| 316 | * discusses A-D. But it basically applies to all 32-bit digests, |
| 317 | * which is why it was moved to common header file. |
| 318 | * |
| 319 | * In case you wonder why A-D are declared as long and not |
| 320 | * as MD5_LONG. Doing so results in slight performance |
| 321 | * boost on LP64 architectures. The catch is we don't |
| 322 | * really care if 32 MSBs of a 64-bit register get polluted |
| 323 | * with eventual overflows as we *save* only 32 LSBs in |
| 324 | * *either* case. Now declaring 'em long excuses the compiler |
| 325 | * from keeping 32 MSBs zeroed resulting in 13% performance |
| 326 | * improvement under SPARC Solaris7/64 and 5% under AlphaLinux. |
| 327 | * Well, to be honest it should say that this *prevents* |
| 328 | * performance degradation. |
| 329 | * <appro@fy.chalmers.se> |
| 330 | * Apparently there're LP64 compilers that generate better |
| 331 | * code if A-D are declared int. Most notably GCC-x86_64 |
| 332 | * generates better code. |
| 333 | * <appro@fy.chalmers.se> |
| 334 | */ |
| 335 | #endif |
| 336 | |
| 337 | #endif /* MD32_COMMON_H */ |