Skip to content

Instantly share code, notes, and snippets.

@OhGodAPet
Created April 23, 2019 06:48
Show Gist options
  • Save OhGodAPet/e55c3a2f93bc6277d32a93a2a8a0c9d0 to your computer and use it in GitHub Desktop.
Save OhGodAPet/e55c3a2f93bc6277d32a93a2a8a0c9d0 to your computer and use it in GitHub Desktop.
VBK Check
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define ROTR32(x, n) ((x >> n) | (x << (32 - n)))
#define CH(x, y, z) ((x & y) ^ (~x & z))
#define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
#define SHA256_F1(x) (ROTR32(x, 2) ^ ROTR32(x, 13) ^ ROTR32(x, 22))
#define SHA256_F2(x) (ROTR32(x, 6) ^ ROTR32(x, 11) ^ ROTR32(x, 25))
#define SHA256_F3(x) (ROTR32(x, 7) ^ ROTR32(x, 18) ^ (x >> 3))
#define SHA256_F4(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ (x >> 10))
uint32_t sha256_h0[8] =
{
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};
uint32_t sha256_k[64] =
{
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
void SHA2_256_Block(uint32_t *InState, uint32_t *Msg)
{
uint32_t st[8], w[64], tmp0, tmp1;
for(int i = 0; i < 8; ++i) st[i] = InState[i];
for(int i = 0; i < 16; ++i) w[i] = Msg[i];
// SHA2-256 message expansion
for(int i = 16; i < 64; ++i)
w[i] = SHA256_F4(w[i - 2]) + w[i - 7] + SHA256_F3(w[i - 15]) + w[i - 16];
// Main rounds
for(int i = 0; i < 64; ++i)
{
tmp0 = st[7] + SHA256_F2(st[4]) + CH(st[4], st[5], st[6]) + sha256_k[i] + w[i];
tmp1 = SHA256_F1(st[0]) + MAJ(st[0], st[1], st[2]);
st[7] = st[6];
st[6] = st[5];
st[5] = st[4];
st[4] = st[3] + tmp0;
st[3] = st[2];
st[2] = st[1];
st[1] = st[0];
st[0] = tmp0 + tmp1;
}
for(int i = 0; i < 8; ++i) InState[i] += st[i];
}
void HashVBK(void *HashOut, uint8_t *PubKey)
{
uint32_t State[8], Buf[16];
for(int i = 0; i < 8; ++i) State[i] = sha256_h0[i];
for(int i = 0; i < 16; ++i) Buf[i] = __builtin_bswap32(((uint32_t *)PubKey)[i]);
SHA2_256_Block(State, Buf);
memset(Buf, 0x00, 64);
for(int i = 0; i < 6; ++i) Buf[i] = __builtin_bswap32(((uint32_t *)PubKey)[16 + i]);
Buf[6] = 0x80000000UL;
Buf[15] = 88UL << 3;
SHA2_256_Block(State, Buf);
for(int i = 0; i < 8; ++i) ((uint32_t *)HashOut)[i] = __builtin_bswap32(State[i]);
}
// Parameter len is bytes in rawstr, therefore, asciistr must have
// at least (len << 1) + 1 bytes allocated, the last for the NULL
void BinaryToASCIIHex(char *restrict asciistr, const void *restrict rawstr, size_t len)
{
for(int i = 0, j = 0; i < len; ++i)
{
asciistr[j++] = "0123456789ABCDEF"[((uint8_t *)rawstr)[i] >> 4];
asciistr[j++] = "0123456789ABCDEF"[((uint8_t *)rawstr)[i] & 0x0F];
}
asciistr[len << 1] = 0x00;
}
const uint8_t StupidEncodingHdr[23] =
{
0x30, 0x56, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a, 0x03, 0x42, 0x00
};
const uint8_t TestKey[65] =
{
0x04, 0x57, 0xf2, 0x09, 0x9c, 0x50, 0x2b, 0x5b, 0xd3, 0x59, 0xd2, 0x43,
0xc2, 0x84, 0x65, 0x08, 0x9b, 0x76, 0x62, 0xcf, 0x40, 0xfa, 0x7b, 0x14,
0x74, 0x36, 0x6b, 0x1a, 0x40, 0x8d, 0xae, 0x3a, 0x64, 0xf8, 0xf4, 0xcc,
0xbc, 0x8f, 0x29, 0x49, 0x77, 0x93, 0xb0, 0xee, 0x3e, 0xc3, 0xe6, 0xe8,
0xa9, 0x75, 0x4d, 0xc9, 0xd6, 0x2b, 0x87, 0x88, 0x1d, 0x85, 0x6a, 0x68,
0xe7, 0x4a, 0xce, 0x3b, 0x69
};
int main()
{
uint8_t InMsg[88], Digest[32];
char *Dbg;
memcpy(InMsg, StupidEncodingHdr, 23);
memcpy(InMsg + 23, TestKey, 65);
HashVBK(Digest, InMsg);
Dbg = (char *)malloc(((sizeof(char) * 32) << 1) + 1);
BinaryToASCIIHex(Dbg, Digest, 32);
printf("Digest: %s\n", Dbg);
free(Dbg);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment