starcrack / LaunchHelper / SRP6a.cpp

/*
Copyright 2010 Robert O'Neal <rob@rebelworks.com>. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, this list of
      conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice, this list
      of conditions and the following disclaimer in the documentation and/or other materials
      provided with the distribution.

THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
#include "SRP6a.h"
#include "CryptoSha256.h"

BYTE N[] = {
0xAB, 0x24, 0x43, 0x63, 0xA9, 0xC2, 0xA6, 0xC3, 0x3B, 0x37, 0xE4, 0x61, 0x84, 0x25, 0x9F, 0x8B,
0x3F, 0xCB, 0x8A, 0x85, 0x27, 0xFC, 0x3D, 0x87, 0xBE, 0xA0, 0x54, 0xD2, 0x38, 0x5D, 0x12, 0xB7,
0x61, 0x44, 0x2E, 0x83, 0xFA, 0xC2, 0x21, 0xD9, 0x10, 0x9F, 0xC1, 0x9F, 0xEA, 0x50, 0xE3, 0x09,
0xA6, 0xE5, 0x5E, 0x23, 0xA7, 0x77, 0xEB, 0x00, 0xC7, 0xBA, 0xBF, 0xF8, 0x55, 0x8A, 0x0E, 0x80,
0x2B, 0x14, 0x1A, 0xA2, 0xD4, 0x43, 0xA9, 0xD4, 0xAF, 0xAD, 0xB5, 0xE1, 0xF5, 0xAC, 0xA6, 0x13,
0x1C, 0x69, 0x78, 0x64, 0x0B, 0x7B, 0xAF, 0x9C, 0xC5, 0x50, 0x31, 0x8A, 0x23, 0x08, 0x01, 0xA1,
0xF5, 0xFE, 0x31, 0x32, 0x7F, 0xE2, 0x05, 0x82, 0xD6, 0x0B, 0xED, 0x4D, 0x55, 0x32, 0x41, 0x94,
0x29, 0x6F, 0x55, 0x7D, 0xE3, 0x0F, 0x77, 0x19, 0xE5, 0x6C, 0x30, 0xEB, 0xDE, 0xF6, 0xA7, 0x86 };


BYTE g[] = { 0x02 };

SRP6a::SRP6a(){

	
	PrintBytes(N, 128, "N");
	PrintBytes(g, 1, "g");
	
	BYTE digest_g[32];
	BYTE digest_N[32];

	CryptoSha256 *sha = new CryptoSha256();
	sha->Hash(N, 128, digest_N);
	sha->Hash(g, 1, digest_g);

	for(int i = 0; i < 32; i++){
		m_bHNxorg[i] = digest_g[i] ^ digest_N[i];	
	}

	PrintBytes(m_bHNxorg, 32, "H(g) xor H(N)");

	delete sha;

}
SRP6a::~SRP6a(){}
void SRP6a::SetUsername(PSTR Username){
	
	int len = strlen(Username);
	m_Username = new char[len];
	strcpy(m_Username, Username);

	printf("Username\n%s\n", m_Username);
	PrintBytes((PBYTE)m_Username, strlen(m_Username), "Username");

	CryptoSha256 *sha = new CryptoSha256();
	
	sha->Hash(m_Username, len, m_bI);

	PrintBytes(m_bI, 32, "Username Hashed / I");

	delete sha;

}
void SRP6a::SetPassword(PSTR Password){
	int len = strlen(Password)+1;
	m_Password = new char[len];
	strcpy(m_Password, Password);

	for( int i = 0; i < len; i++)
		m_Password[i] = toupper( m_Password[i] );



}
void SRP6a::SetSRPa(PBYTE a){
	CopyBytes(m_ba, a, 128);	

}
void SRP6a::SetSRPB(PBYTE B){
	CopyBytes(m_bB, B, 128);
	PrintBytes(m_bB, 128, "B");
}
void SRP6a::SetSRPs(PBYTE s){
	CopyBytes(m_bs, s, 32);
	PrintBytes(m_bs, 32, "s");
}
void SRP6a::SetSRPA(PBYTE A){
	CopyBytes(m_bA, A, 128);
	PrintBytes(m_bA, 128, "A");
}
void SRP6a::SetSRPK(PBYTE K){
	CopyBytes(m_bK, K, 64);
	PrintBytes(m_bK, 64, "K");
}
void SRP6a::SetSRPM1(PBYTE M1){
	CopyBytes(m_bM1, M1, 32);
	PrintBytes(m_bM1, 32, "M1");
}
BOOL SRP6a::VerifyM2(PBYTE M2){
	CalculateM2();
	return Equals(M2, m_bM2, 32);
}

void SRP6a::GetK(PBYTE pbOut){
	CopyBytes(pbOut, m_bK, 64);
}
void SRP6a::GetA(PBYTE pbOut){
	CopyBytes(pbOut, m_bA, 128);
}
void SRP6a::GetM1(PBYTE pbOut){
	CopyBytes(pbOut, m_bM1, 32);
}

void SRP6a::CalculateA(){
	
	BigIntegerFromBytes(m_a, m_ba, 128);
	BigIntegerFromBytes(m_g, g, 1);
	BigIntegerFromBytes(m_N, N, 128);
	
	BigIntegerFromInt(m_A, 0);
	BigIntegerPowMod(m_A, m_g, m_a, m_N);

	BigIntegerToBytes(m_A, m_bA);

	PrintBytes(m_bA, 128, "A = g ^ a mod N");
	
}
void SRP6a::CalculateK(){
	
	CalculateA();
	Calculate_x();
	Calculate_v();
	Calculate_k();
	Calculate_u();


	BigIntegerFromBytes(m_N, N, 128);
	BigIntegerFromBytes(m_B, m_bB, 128);
	BigIntegerFromBytes(m_v, m_bv, 128);
	BigIntegerFromBytes(m_a, m_ba, 128);
	BigIntegerFromBytes(m_k, m_bk, 32);
	BigIntegerFromBytes(m_x, m_bx, 32);
	BigIntegerFromBytes(m_u, m_bu, 32);
	
	BigIntegerSubMul(m_B, m_B, m_v, m_k);
	BigIntegerMod(m_B, m_B, m_N);
	BigIntegerAddMul(m_a, m_a, m_u, m_x);
	BigIntegerPowMod(m_B, m_B, m_a, m_N);

	//printf("Bytes = %d\n", BN_num_bytes(m_B));
	BigIntegerToBytes(m_B, m_bS);
		
	PrintBytes(m_bS, 128, "S = (B - v * k) ^ (a + u * x)");

	SHA256Interleave(m_bK, m_bS, 128);

	PrintBytes(m_bK, 64, "K = H(S)");

	BigIntegerFree( m_N);
	BigIntegerFree( m_B);
	BigIntegerFree( m_v);
	BigIntegerFree( m_a);

	BigIntegerFree( m_k);
	BigIntegerFree( m_x) ;
	BigIntegerFree( m_u);

		
}
void SRP6a::CalculateM1(){
	
	CalculateK();

	CryptoSha256 *sha = new CryptoSha256();
	sha->Init();
	sha->Update(m_bHNxorg, 32);
	sha->Update(m_bI, 32);
	sha->Update(m_bs, 32);
	sha->Update(m_bA, 128);
	sha->Update(m_bB, 128);
	sha->Update(m_bK, 64);
	sha->Final(m_bM1);
	
	PrintBytes(m_bM1, 32, "M1 = H(H(N) xor H(g), H(I), s, A, B, K)");
	
	delete sha;

}
void SRP6a::CalculateM2(){
	CryptoSha256 *sha = new CryptoSha256();
	sha->Init();
	sha->Update(m_bA, 128);
	sha->Update(m_bM1, 32);
	sha->Update(m_bK, 64);	
	sha->Final(m_bM2);
	
	PrintBytes(m_bM2, 32, "M2 = H(A, M1, K)");

	delete sha;
}
void SRP6a::Calculate_v(){
	
	BigIntegerFromInt(m_v, 0);
	

	BigIntegerFromBytes(m_g, g, 1);
	BigIntegerFromBytes(m_N, N, 128);
	BigIntegerFromBytes(m_x, m_bx, 32);
	
	BigIntegerPowMod(m_v, m_g, m_x, m_N);
	BigIntegerToBytes(m_v, m_bv);

	PrintBytes(m_bv, 128, "v = g ^ x mod N");

}
void SRP6a::Calculate_u(){
	CryptoSha256 *sha = new CryptoSha256();
	sha->Init();
	sha->Update(m_bA, 128);
	sha->Update(m_bB, 128);
	sha->Final(m_bu);	
	delete sha;

	PrintBytes(m_bu, 32, "u = H(A, B)");

}
void SRP6a::Calculate_k(){
	CryptoSha256 *sha = new CryptoSha256();
	sha->Init();
	sha->Update(N, 128);
	sha->Update(g, 1);
	sha->Final(m_bk);
	delete sha;

	PrintBytes(m_bk, 32, "k = H(N, g)");
}
void SRP6a::Calculate_x(){
	CryptoSha256 *sha = new CryptoSha256();
	
	sha->Init();
	sha->Update(m_Username, strlen(m_Username));
	sha->Update(":", 1);	
	sha->Update(m_Password, strlen(m_Password));
	sha->Final(m_bp);	

	PrintBytes(m_bp, 32, "p = H(User, :, Password)");

	sha->Init();
	sha->Update(m_bs, 32);
	sha->Update(m_bp, 32);
	sha->Final(m_bx);
	
	PrintBytes(m_bx, 32, "x = H(s, p)");

	delete sha;
}

void SRP6a::CopyBytes(PBYTE dest, PBYTE source, int Length){
	for(int i = 0; i < Length; i++){
		dest[i] = source[i];
	}
}
BOOL SRP6a::Equals(PBYTE b1, PBYTE b2, int Length){
	for(int i = 0; i < Length; i++){
		if(b1[i] != b2[i]){
			return FALSE;
		}
	}
	return TRUE;
}
void SRP6a::SHA256Interleave(PBYTE pbOut, PBYTE pbIn, int Length){
	
	PBYTE odd = new BYTE[64];
	PBYTE even = new BYTE[64];
	
	PBYTE oddHash = new BYTE[32];
	PBYTE evenHash = new BYTE[32];

	CryptoSha256 *sha = new CryptoSha256();

	for (int i=0; i < 64; i++)
	{
		even[i] = pbIn[i*2];
		odd[i] = pbIn[i*2 + 1];
	}


	sha->Hash(even, 64, evenHash);
	sha->Hash(odd, 64, oddHash);
	
	for (int i=0; i < 32; i++)
	{
		pbOut[i*2] = evenHash[i];
		pbOut[i*2 + 1] = oddHash[i];
	}
	
	
	delete sha;
}

void SRP6a::PrintBytes(PBYTE Bytes, int Length, PSTR Value){
	printf("Value: %s; Len = %d\n", Value, Length);

	for(int i = 0; i < Length; i++){
		printf("%02x", Bytes[i]);
	}

	printf("\n\n\n");
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.