UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sha2.cpp File Reference
#include <string.h>
#include <stdio.h>
#include "sha2.h"
#include "filesys.h"
#include "../shared/shared.h"

Go to the source code of this file.

Macros

#define _CRT_SECURE_NO_DEPRECATE   1
 
#define GET_UINT32_BE(n, b, i)
 
#define PUT_UINT32_BE(n, b, i)
 
#define SHR(x, n)   ((x & 0xFFFFFFFF) >> n)
 
#define ROTR(x, n)   (SHR(x,n) | (x << (32 - n)))
 
#define S0(x)   (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
 
#define S1(x)   (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
 
#define S2(x)   (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
 
#define S3(x)   (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
 
#define F0(x, y, z)   ((x & y) | (z & (x | y)))
 
#define F1(x, y, z)   (z ^ (x & (y ^ z)))
 
#define R(t)
 
#define P(a, b, c, d, e, f, g, h, x, K)
 

Functions

void Com_SHA2Starts (sha2_context *ctx)
 
static void Com_SHA2Process (sha2_context *ctx, const byte data[64])
 
void Com_SHA2Update (sha2_context *ctx, const byte *input, uint32_t length)
 
void Com_SHA2Finish (sha2_context *ctx, byte digest[32])
 
bool Com_SHA2File (const char *filename, byte digest[32])
 Output SHA-256(file contents) More...
 
void Com_SHA2Csum (const byte *buf, uint32_t buflen, byte digest[32])
 Output SHA-256(buf) More...
 
void Com_SHA2ToHex (const byte digest[32], char final[65])
 
void Com_SHA2Hmac (const byte *buf, uint32_t buflen, const byte *key, uint32_t keylen, byte digest[32])
 Output HMAC-SHA-256(buf,key) More...
 

Variables

static const byte sha2_padding [64]
 

Macro Definition Documentation

#define _CRT_SECURE_NO_DEPRECATE   1

Definition at line 27 of file sha2.cpp.

#define F0 (   x,
  y,
 
)    ((x & y) | (z & (x | y)))
#define F1 (   x,
  y,
 
)    (z ^ (x & (y ^ z)))
#define GET_UINT32_BE (   n,
  b,
  i 
)
Value:
{ \
(n) = ( (uint64_t) (b)[(i) ] << 24 ) \
| ( (uint64_t) (b)[(i) + 1] << 16 ) \
| ( (uint64_t) (b)[(i) + 2] << 8 ) \
| ( (uint64_t) (b)[(i) + 3] ); \
}
QGL_EXTERN GLint i
Definition: r_gl.h:113

32-bit integer manipulation macros (big endian)

Definition at line 41 of file sha2.cpp.

Referenced by Com_SHA2Process().

#define P (   a,
  b,
  c,
  d,
  e,
  f,
  g,
  h,
  x,
 
)
Value:
{ \
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
temp2 = S2(a) + F0(a,b,c); \
d += temp1; h = temp1 + temp2; \
}
#define S2(x)
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
#define F0(x, y, z)
#define S3(x)
#define F1(x, y, z)

Referenced by Com_SHA2Process().

#define PUT_UINT32_BE (   n,
  b,
  i 
)
Value:
{ \
(b)[(i) ] = (byte) ( (n) >> 24 ); \
(b)[(i) + 1] = (byte) ( (n) >> 16 ); \
(b)[(i) + 2] = (byte) ( (n) >> 8 ); \
(b)[(i) + 3] = (byte) ( (n) ); \
}
QGL_EXTERN GLint i
Definition: r_gl.h:113
uint8_t byte
Definition: ufotypes.h:34

Definition at line 50 of file sha2.cpp.

Referenced by Com_SHA2Finish().

#define R (   t)
Value:
( \
W[t] = S1(W[t - 2]) + W[t - 7] + \
S0(W[t - 15]) + W[t - 16] \
)
#define S1(x)
#define S0(x)

Referenced by Com_SHA2Process().

#define ROTR (   x,
 
)    (SHR(x,n) | (x << (32 - n)))
#define S0 (   x)    (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
#define S1 (   x)    (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
#define S2 (   x)    (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
#define S3 (   x)    (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
#define SHR (   x,
 
)    ((x & 0xFFFFFFFF) >> n)

Function Documentation

void Com_SHA2Csum ( const byte buf,
uint32_t  buflen,
byte  digest[32] 
)

Output SHA-256(buf)

Definition at line 292 of file sha2.cpp.

References Com_SHA2Finish(), Com_SHA2Starts(), and Com_SHA2Update().

Referenced by TEST_F().

bool Com_SHA2File ( const char *  filename,
byte  digest[32] 
)

Output SHA-256(file contents)

Returns
true if successful

Definition at line 270 of file sha2.cpp.

References Com_SHA2Finish(), Com_SHA2Starts(), Com_SHA2Update(), f, FILE_READ, FS_OpenFile(), and FS_Read().

void Com_SHA2Finish ( sha2_context ctx,
byte  digest[32] 
)
void Com_SHA2Hmac ( const byte buf,
uint32_t  buflen,
const byte key,
uint32_t  keylen,
byte  digest[32] 
)

Output HMAC-SHA-256(buf,key)

Definition at line 308 of file sha2.cpp.

References Com_SHA2Finish(), Com_SHA2Starts(), Com_SHA2Update(), and i.

static void Com_SHA2Process ( sha2_context ctx,
const byte  data[64] 
)
static

Definition at line 77 of file sha2.cpp.

References E, F, G, GET_UINT32_BE, H, P, R, and sha2_context::state.

Referenced by Com_SHA2Update().

void Com_SHA2Starts ( sha2_context ctx)

Core SHA-256 functions

Definition at line 62 of file sha2.cpp.

References sha2_context::state, and sha2_context::total.

Referenced by Com_SHA2Csum(), Com_SHA2File(), and Com_SHA2Hmac().

void Com_SHA2ToHex ( const byte  digest[32],
char  final[65] 
)

Definition at line 301 of file sha2.cpp.

References i, and Q_strcat().

Referenced by TEST_F().

void Com_SHA2Update ( sha2_context ctx,
const byte input,
uint32_t  length 
)

Variable Documentation

const byte sha2_padding[64]
static
Initial value:
=
{ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

Definition at line 240 of file sha2.cpp.

Referenced by Com_SHA2Finish().