Sirikata
libcore/src/util/internal_sha2.hpp
Go to the documentation of this file.
00001 /*
00002  * FILE:    sha2.hpp
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  * $Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
00033  */
00034 
00035 #ifndef __SHA2_H__
00036 #define __SHA2_H__
00037 #if 0
00038 #define SHA2_USE_INTTYPES_H
00039 #endif
00040 /*
00041  * Import u_intXX_t size_t type definitions from system headers.  You
00042  * may need to change this, or define these things yourself in this
00043  * file.
00044  */
00045 #include <sys/types.h>
00046 
00047 #ifdef SHA2_USE_INTTYPES_H
00048 
00049 #include <inttypes.h>
00050 
00051 #endif /* SHA2_USE_INTTYPES_H */
00052 namespace Sirikata {
00053 namespace Util {
00054 namespace Internal{
00055 
00056 
00057 /*** SHA-256/384/512 Various Length Definitions ***********************/
00058 #define SHA256_BLOCK_LENGTH     64
00059 #define SHA256_DIGEST_LENGTH        32
00060 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
00061 #define SHA384_BLOCK_LENGTH     128
00062 #define SHA384_DIGEST_LENGTH        48
00063 #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
00064 #define SHA512_BLOCK_LENGTH     128
00065 #define SHA512_DIGEST_LENGTH        64
00066 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
00067 
00068 
00069 /*** SHA-256/384/512 Context Structures *******************************/
00070 /* NOTE: If your architecture does not define either u_intXX_t types or
00071  * uintXX_t (from inttypes.h), you may need to define things by hand
00072  * for your system:
00073  */
00074 #if 1
00075 typedef uint8 u_int8_t;     /* 1-byte  (8-bits)  */
00076 typedef uint32 u_int32_t;       /* 4-bytes (32-bits) */
00077 typedef uint64 u_int64_t;   /* 8-bytes (64-bits) */
00078 #endif
00079 /*
00080  * Most BSD systems already define u_intXX_t types, as does Linux.
00081  * Some systems, however, like Compaq's Tru64 Unix instead can use
00082  * uintXX_t types defined by very recent ANSI C standards and included
00083  * in the file:
00084  *
00085  *   #include <inttypes.h>
00086  *
00087  * If you choose to use <inttypes.h> then please define:
00088  *
00089  *   #define SHA2_USE_INTTYPES_H
00090  *
00091  * Or on the command line during compile:
00092  *
00093  *   cc -DSHA2_USE_INTTYPES_H ...
00094  */
00095 #ifdef SHA2_USE_INTTYPES_H
00096 
00097 typedef struct _SHA256_CTX {
00098     uint32_t    state[8];
00099     uint64_t    bitcount;
00100     uint8_t buffer[SHA256_BLOCK_LENGTH];
00101 } SHA256_CTX;
00102 typedef struct _SHA512_CTX {
00103     uint64_t    state[8];
00104     uint64_t    bitcount[2];
00105     uint8_t buffer[SHA512_BLOCK_LENGTH];
00106 } SHA512_CTX;
00107 
00108 #else /* SHA2_USE_INTTYPES_H */
00109 
00110 typedef struct _SHA256_CTX {
00111     u_int32_t   state[8];
00112     u_int64_t   bitcount;
00113     u_int8_t    buffer[SHA256_BLOCK_LENGTH];
00114 } SHA256_CTX;
00115 typedef struct _SHA512_CTX {
00116     u_int64_t   state[8];
00117     u_int64_t   bitcount[2];
00118     u_int8_t    buffer[SHA512_BLOCK_LENGTH];
00119 } SHA512_CTX;
00120 
00121 #endif /* SHA2_USE_INTTYPES_H */
00122 
00123 typedef SHA512_CTX SHA384_CTX;
00124 
00125 
00126 /*** SHA-256/384/512 Function Prototypes ******************************/
00127 #ifndef NOPROTO
00128 #ifdef SHA2_USE_INTTYPES_H
00129 
00130 void SHA256_Init(SHA256_CTX *);
00131 void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
00132 void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
00133 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
00134 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
00135 
00136 void SHA384_Init(SHA384_CTX*);
00137 void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
00138 void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
00139 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
00140 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
00141 
00142 void SHA512_Init(SHA512_CTX*);
00143 void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
00144 void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
00145 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
00146 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
00147 
00148 #else /* SHA2_USE_INTTYPES_H */
00149 
00150 void SHA256_Init(SHA256_CTX *);
00151 void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t);
00152 void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
00153 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
00154 char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
00155 
00156 void SHA384_Init(SHA384_CTX*);
00157 void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t);
00158 void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
00159 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
00160 char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
00161 
00162 void SHA512_Init(SHA512_CTX*);
00163 void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t);
00164 void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
00165 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
00166 char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
00167 
00168 #endif /* SHA2_USE_INTTYPES_H */
00169 
00170 #else /* NOPROTO */
00171 
00172 void SHA256_Init();
00173 void SHA256_Update();
00174 void SHA256_Final();
00175 char* SHA256_End();
00176 char* SHA256_Data();
00177 
00178 void SHA384_Init();
00179 void SHA384_Update();
00180 void SHA384_Final();
00181 char* SHA384_End();
00182 char* SHA384_Data();
00183 
00184 void SHA512_Init();
00185 void SHA512_Update();
00186 void SHA512_Final();
00187 char* SHA512_End();
00188 char* SHA512_Data();
00189 
00190 #endif /* NOPROTO */
00191 
00192 }
00193 }
00194 }
00195 
00196 #endif /* __SHA2_H__ */