Ruby 1.9.3p327(2012-11-10revision37606)
|
00001 /* 00002 * FILE: sha2.h 00003 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ 00004 * 00005 * Copyright (c) 2000-2001, Aaron D. Gifford 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 3. Neither the name of the copyright holder nor the names of contributors 00017 * may be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND 00021 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE 00024 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00025 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00026 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00027 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00028 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00029 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00030 * SUCH DAMAGE. 00031 * 00032 * $OrigId: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $ 00033 * $RoughId: sha2.h,v 1.3 2002/02/24 08:14:32 knu Exp $ 00034 * $Id: sha2.h 27437 2010-04-22 08:04:13Z nobu $ 00035 */ 00036 00037 #ifndef __SHA2_H__ 00038 #define __SHA2_H__ 00039 00040 #ifdef __cplusplus 00041 extern "C" { 00042 #endif 00043 00044 00045 /* 00046 * Import u_intXX_t size_t type definitions from system headers. You 00047 * may need to change this, or define these things yourself in this 00048 * file. 00049 */ 00050 #include <sys/types.h> 00051 00052 #ifdef RUBY 00053 # ifdef HAVE_PROTOTYPES 00054 # undef NOPROTO 00055 # else 00056 # define NOPROTO 00057 # endif /* HAVE_PROTOTYPES */ 00058 # ifndef BYTE_ORDER 00059 # define LITTLE_ENDIAN 1234 00060 # define BIG_ENDIAN 4321 00061 # ifdef WORDS_BIGENDIAN 00062 # define BYTE_ORDER BIG_ENDIAN 00063 # else 00064 # define BYTE_ORDER LITTLE_ENDIAN 00065 # endif 00066 # endif /* BYTE_ORDER */ 00067 # define SHA2_USE_INTTYPES_H 00068 #else /* RUBY */ 00069 #ifdef SHA2_USE_INTTYPES_H 00070 00071 #include <inttypes.h> 00072 00073 #endif /* SHA2_USE_INTTYPES_H */ 00074 #endif /* RUBY */ 00075 00076 00077 /*** SHA-256/384/512 Various Length Definitions ***********************/ 00078 #define SHA256_BLOCK_LENGTH 64 00079 #define SHA256_DIGEST_LENGTH 32 00080 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) 00081 #define SHA384_BLOCK_LENGTH 128 00082 #define SHA384_DIGEST_LENGTH 48 00083 #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1) 00084 #define SHA512_BLOCK_LENGTH 128 00085 #define SHA512_DIGEST_LENGTH 64 00086 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) 00087 00088 00089 /*** SHA-256/384/512 Context Structures *******************************/ 00090 /* NOTE: If your architecture does not define either u_intXX_t types or 00091 * uintXX_t (from inttypes.h), you may need to define things by hand 00092 * for your system: 00093 */ 00094 #ifndef SHA2_USE_INTTYPES_H 00095 # ifdef HAVE_U_INT8_T 00096 typedef u_int8_t uint8_t; /* 1-byte (8-bits) */ 00097 typedef u_int32_t uint32_t; /* 4-bytes (32-bits) */ 00098 typedef u_int64_t uint64_t; /* 8-bytes (64-bits) */ 00099 # else 00100 typedef unsigned char uint8_t; /* 1-byte (8-bits) */ 00101 typedef unsigned int uint32_t; /* 4-bytes (32-bits) */ 00102 typedef unsigned long long uint64_t; /* 8-bytes (64-bits) */ 00103 # endif 00104 #endif 00105 00106 /* 00107 * Most BSD systems already define u_intXX_t types, as does Linux. 00108 * Some systems, however, like Compaq's Tru64 Unix instead can use 00109 * uintXX_t types defined by very recent ANSI C standards and included 00110 * in the file: 00111 * 00112 * #include <inttypes.h> 00113 * 00114 * If you choose to use <inttypes.h> then please define: 00115 * 00116 * #define SHA2_USE_INTTYPES_H 00117 * 00118 * Or on the command line during compile: 00119 * 00120 * cc -DSHA2_USE_INTTYPES_H ... 00121 */ 00122 typedef struct _SHA256_CTX { 00123 uint32_t state[8]; 00124 uint64_t bitcount; 00125 uint8_t buffer[SHA256_BLOCK_LENGTH]; 00126 } SHA256_CTX; 00127 typedef struct _SHA512_CTX { 00128 uint64_t state[8]; 00129 uint64_t bitcount[2]; 00130 uint8_t buffer[SHA512_BLOCK_LENGTH]; 00131 } SHA512_CTX; 00132 00133 typedef SHA512_CTX SHA384_CTX; 00134 00135 00136 /*** SHA-256/384/512 Function Prototypes ******************************/ 00137 #ifdef RUBY 00138 #define SHA256_Init rb_Digest_SHA256_Init 00139 #define SHA256_Update rb_Digest_SHA256_Update 00140 #define SHA256_Finish rb_Digest_SHA256_Finish 00141 #define SHA256_Data rb_Digest_SHA256_Data 00142 #define SHA256_End rb_Digest_SHA256_End 00143 #define SHA256_Last rb_Digest_SHA256_Last 00144 #define SHA256_Transform rb_Digest_SHA256_Transform 00145 #define SHA256_Final(d, c) SHA256_Finish(c, d) 00146 00147 #define SHA384_Init rb_Digest_SHA384_Init 00148 #define SHA384_Update rb_Digest_SHA384_Update 00149 #define SHA384_Finish rb_Digest_SHA384_Finish 00150 #define SHA384_Data rb_Digest_SHA384_Data 00151 #define SHA384_End rb_Digest_SHA384_End 00152 #define SHA384_Last rb_Digest_SHA384_Last 00153 #define SHA384_Transform rb_Digest_SHA384_Transform 00154 #define SHA384_Final(d, c) SHA384_Finish(c, d) 00155 00156 #define SHA512_Init rb_Digest_SHA512_Init 00157 #define SHA512_Update rb_Digest_SHA512_Update 00158 #define SHA512_Finish rb_Digest_SHA512_Finish 00159 #define SHA512_Data rb_Digest_SHA512_Data 00160 #define SHA512_End rb_Digest_SHA512_End 00161 #define SHA512_Last rb_Digest_SHA512_Last 00162 #define SHA512_Transform rb_Digest_SHA512_Transform 00163 #define SHA512_Final(d, c) SHA512_Finish(c, d) 00164 #endif /* RUBY */ 00165 00166 #ifndef NOPROTO 00167 00168 void SHA256_Init(SHA256_CTX *); 00169 void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t); 00170 void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); 00171 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); 00172 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); 00173 00174 void SHA384_Init(SHA384_CTX*); 00175 void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t); 00176 void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); 00177 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); 00178 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); 00179 00180 void SHA512_Init(SHA512_CTX*); 00181 void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t); 00182 void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); 00183 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); 00184 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); 00185 00186 #else /* NOPROTO */ 00187 00188 void SHA256_Init(); 00189 void SHA256_Update(); 00190 #ifdef RUBY 00191 void SHA256_Finish(); 00192 #else 00193 void SHA256_Final(); 00194 #endif /* RUBY */ 00195 char* SHA256_End(); 00196 char* SHA256_Data(); 00197 00198 void SHA384_Init(); 00199 void SHA384_Update(); 00200 #ifdef RUBY 00201 void SHA384_Finish(); 00202 #else 00203 void SHA384_Final(); 00204 #endif /* RUBY */ 00205 char* SHA384_End(); 00206 char* SHA384_Data(); 00207 00208 void SHA512_Init(); 00209 void SHA512_Update(); 00210 #ifdef RUBY 00211 void SHA512_Finish(); 00212 #else 00213 void SHA512_Final(); 00214 #endif /* RUBY */ 00215 char* SHA512_End(); 00216 char* SHA512_Data(); 00217 00218 #endif /* NOPROTO */ 00219 00220 #ifdef __cplusplus 00221 } 00222 #endif /* __cplusplus */ 00223 00224 #endif /* __SHA2_H__ */ 00225 00226