Skip to content

Commit

Permalink
Add SHA512 compliant SRP client
Browse files Browse the repository at this point in the history
  • Loading branch information
moflo committed Jun 30, 2017
1 parent fe80c4c commit 709fe34
Show file tree
Hide file tree
Showing 13 changed files with 8,161 additions and 5 deletions.
28 changes: 28 additions & 0 deletions Homekit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
913ECAA21F05CD6000E910C5 /* srp.c in Sources */ = {isa = PBXBuildFile; fileRef = 913ECAA01F05CD6000E910C5 /* srp.c */; };
913ECAA71F05CD7000E910C5 /* sha1.c in Sources */ = {isa = PBXBuildFile; fileRef = 913ECAA41F05CD7000E910C5 /* sha1.c */; };
913ECAA81F05CD7000E910C5 /* sha256.c in Sources */ = {isa = PBXBuildFile; fileRef = 913ECAA51F05CD7000E910C5 /* sha256.c */; };
913ECAA91F05CD7000E910C5 /* sha512.c in Sources */ = {isa = PBXBuildFile; fileRef = 913ECAA61F05CD7000E910C5 /* sha512.c */; };
913ECAAC1F05CD7900E910C5 /* mini-gmp.c in Sources */ = {isa = PBXBuildFile; fileRef = 913ECAAA1F05CD7900E910C5 /* mini-gmp.c */; };
91BBDB181F02C5A2009CBF0A /* test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91BBDB171F02C5A2009CBF0A /* test.cpp */; };
91BBDB1C1F02C5B1009CBF0A /* homekit.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91BBDB1A1F02C5B1009CBF0A /* homekit.cpp */; };
91F870F21F0345BC00F77259 /* WebClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91F870F01F0345BC00F77259 /* WebClient.cpp */; };
Expand All @@ -27,6 +32,15 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
913ECAA01F05CD6000E910C5 /* srp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = srp.c; sourceTree = "<group>"; };
913ECAA11F05CD6000E910C5 /* srp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = srp.h; sourceTree = "<group>"; };
913ECAA31F05CD7000E910C5 /* sha.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sha.h; sourceTree = "<group>"; };
913ECAA41F05CD7000E910C5 /* sha1.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha1.c; sourceTree = "<group>"; };
913ECAA51F05CD7000E910C5 /* sha256.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha256.c; sourceTree = "<group>"; };
913ECAA61F05CD7000E910C5 /* sha512.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha512.c; sourceTree = "<group>"; };
913ECAAA1F05CD7900E910C5 /* mini-gmp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "mini-gmp.c"; sourceTree = "<group>"; };
913ECAAB1F05CD7900E910C5 /* mini-gmp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "mini-gmp.h"; sourceTree = "<group>"; };
913ECAAD1F05CD8C00E910C5 /* md32_common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = md32_common.h; sourceTree = "<group>"; };
9163EA621F02C5690098C4B8 /* Homekit */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Homekit; sourceTree = BUILT_PRODUCTS_DIR; };
91BBDB171F02C5A2009CBF0A /* test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test.cpp; sourceTree = SOURCE_ROOT; };
91BBDB1A1F02C5B1009CBF0A /* homekit.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = homekit.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -98,6 +112,15 @@
91F870F51F04205E00F77259 /* TLV8.h */,
91F870F71F045B9700F77259 /* SRPClient.cpp */,
91F870F81F045B9700F77259 /* SRPClient.h */,
913ECAA01F05CD6000E910C5 /* srp.c */,
913ECAA11F05CD6000E910C5 /* srp.h */,
913ECAA31F05CD7000E910C5 /* sha.h */,
913ECAAD1F05CD8C00E910C5 /* md32_common.h */,
913ECAA41F05CD7000E910C5 /* sha1.c */,
913ECAA51F05CD7000E910C5 /* sha256.c */,
913ECAA61F05CD7000E910C5 /* sha512.c */,
913ECAAA1F05CD7900E910C5 /* mini-gmp.c */,
913ECAAB1F05CD7900E910C5 /* mini-gmp.h */,
);
path = src;
sourceTree = "<group>";
Expand Down Expand Up @@ -162,7 +185,12 @@
files = (
91BBDB181F02C5A2009CBF0A /* test.cpp in Sources */,
91BBDB1C1F02C5B1009CBF0A /* homekit.cpp in Sources */,
913ECAA81F05CD7000E910C5 /* sha256.c in Sources */,
91F870F91F045B9700F77259 /* SRPClient.cpp in Sources */,
913ECAA91F05CD7000E910C5 /* sha512.c in Sources */,
913ECAAC1F05CD7900E910C5 /* mini-gmp.c in Sources */,
913ECAA21F05CD6000E910C5 /* srp.c in Sources */,
913ECAA71F05CD7000E910C5 /* sha1.c in Sources */,
91F870F21F0345BC00F77259 /* WebClient.cpp in Sources */,
91F870F61F04205E00F77259 /* TLV8.cpp in Sources */,
);
Expand Down
191 changes: 188 additions & 3 deletions src/SRPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,202 @@ SRPClient::~SRPClient()
{
}

int SRPClient::getChallenge(uint8_t **salt,uint16_t *salt_len, uint8_t **key, uint16_t *key_len)
{


size_t len_s = 16;
size_t len_v = 0;

const char *username = "alice";
const char *password = "password123";

SRP_HashAlgorithm alg = SRP_SHA512;
SRP_NGType ng_type = SRP_NG_3072; // was SRP_NG_1024; // TEST_NG;

// The test vectors from
// https://tools.ietf.org/html/rfc5054#appendix-B

static const uint8_t srp_5054_salt[] = {
0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E, 0xB5, 0xA7, 0x27, 0x67, 0x3A, 0x24,
0x41, 0xEE
};

static const uint8_t srp_3072_salt[] = {
0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E, 0xB5, 0xA7, 0x27, 0x67, 0x3A, 0x24,
0x41, 0xEE
};

unsigned char *bytes_s = 0;
bytes_s = (unsigned char *)malloc(sizeof(srp_3072_salt));
memcpy(bytes_s, srp_3072_salt, sizeof(srp_3072_salt));

unsigned char *bytes_v = 0;

srp_create_salted_verification_key(alg,
ng_type,
username,
(const unsigned char *)password,
strlen(password),
&bytes_s,
&len_s,
&bytes_v,
&len_v,
NULL,
NULL);

*salt = bytes_s;
*salt_len = sizeof(srp_3072_salt);
*key = bytes_v;
*key_len = len_v;

return 0;
}

int SRPClient::verifySession(uint8_t **serverKeyProof, uint16_t *proof_len, uint8_t *clientPublicKey, uint16_t client_key_len, uint8_t *clientKeyProof, uint16_t client_proof_len)
{
struct SRPVerifier *ver;
struct SRPUser *usr;

size_t len_s = 16;
size_t len_v = client_proof_len;
size_t len_A = 0;
size_t len_B = 0;

const char *username = "alice";
const char *password = "password123";

SRP_HashAlgorithm alg = SRP_SHA512;
SRP_NGType ng_type = SRP_NG_3072; // was SRP_NG_1024; // TEST_NG;

// The test vectors from
// https://tools.ietf.org/html/rfc5054#appendix-B

static const uint8_t srp_5054_salt[] = {
0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E, 0xB5, 0xA7, 0x27, 0x67, 0x3A, 0x24,
0x41, 0xEE
};

static const uint8_t srp_3072_salt[] = {
0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E, 0xB5, 0xA7, 0x27, 0x67, 0x3A, 0x24,
0x41, 0xEE
};

static const uint8_t srp_3072_a[] = {
0x60, 0x97, 0x55, 0x27, 0x03, 0x5C, 0xF2, 0xAD, 0x19, 0x89, 0x80, 0x6F, 0x04, 0x07,
0x21, 0x0B, 0xC8, 0x1E, 0xDC, 0x04, 0xE2, 0x76, 0x2A, 0x56, 0xAF, 0xD5, 0x29, 0xDD,
0xDA, 0x2D, 0x43, 0x93
};

static const uint8_t srp_5054_b[] = {
0xE4, 0x87, 0xCB, 0x59, 0xD3, 0x1A, 0xC5, 0x50, 0x47, 0x1E, 0x81, 0xF0, 0x0F, 0x69,
0x28, 0xE0, 0x1D, 0xDA, 0x08, 0xE9, 0x74, 0xA0, 0x04, 0xF4, 0x9E, 0x61, 0xF5, 0xD1,
0x05, 0x28, 0x4D, 0x20,
};



unsigned char *bytes_s = 0;
bytes_s = (unsigned char *)malloc(sizeof(srp_3072_salt));
memcpy(bytes_s, srp_3072_salt, sizeof(srp_3072_salt));

unsigned char *bytes_v = clientKeyProof;

unsigned char *bytes_A = 0;
unsigned char *bytes_B = 0;

usr = srp_user_new(alg, ng_type, username, username, (const unsigned char *)password, strlen(password), NULL, NULL);

srp_user_start_authentication(usr, NULL, (unsigned char *)srp_3072_a, 32, &bytes_A, &len_A);


static const uint8_t srp_5054_A[] = {
0x61, 0xD5, 0xE4, 0x90, 0xF6, 0xF1, 0xB7, 0x95, 0x47, 0xB0, 0x70, 0x4C, 0x43, 0x6F,
0x52, 0x3D, 0xD0, 0xE5, 0x60, 0xF0, 0xC6, 0x41, 0x15, 0xBB, 0x72, 0x55, 0x7E, 0xC4,
0x43, 0x52, 0xE8, 0x90, 0x32, 0x11, 0xC0, 0x46, 0x92, 0x27, 0x2D, 0x8B, 0x2D, 0x1A,
0x53, 0x58, 0xA2, 0xCF, 0x1B, 0x6E, 0x0B, 0xFC, 0xF9, 0x9F, 0x92, 0x15, 0x30, 0xEC,
0x8E, 0x39, 0x35, 0x61, 0x79, 0xEA, 0xE4, 0x5E, 0x42, 0xBA, 0x92, 0xAE, 0xAC, 0xED,
0x82, 0x51, 0x71, 0xE1, 0xE8, 0xB9, 0xAF, 0x6D, 0x9C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x7E, 0xE2, 0x44, 0x63, 0x54, 0x65, 0xF7, 0x10, 0x58,
0x42, 0x1A, 0x01, 0x84, 0xBE, 0x51, 0xDD, 0x10, 0xCC, 0x9D, 0x07, 0x9E, 0x6F, 0x16,
0x04, 0xE7, 0xAA, 0x9B, 0x7C, 0xF7, 0x88, 0x3C, 0x7D, 0x4C, 0xE1, 0x2B, 0x06, 0xEB,
0xE1, 0x60, 0x81, 0xE2, 0x3F, 0x27, 0xA2, 0x31, 0xD1, 0x84, 0x32, 0xD7, 0xD1, 0xBB,
0x55, 0xC2, 0x8A, 0xE2, 0x1F, 0xFC, 0xF0, 0x05, 0xF5, 0x75, 0x28, 0xD1, 0x5A, 0x88,
0x88, 0x1B, 0xB3, 0xBB, 0xB7, 0xFE
};

if (memcmp(&srp_3072_A, bytes_A, len_A) != 0) {
return -1;
}

ver = srp_verifier_new(alg,
ng_type,
username,
(unsigned char *)srp_5054_salt,
len_s,
bytes_v,
len_v,
bytes_A,
len_A,
(unsigned char *)srp_5054_b,
32,
&bytes_B,
&len_B,
NULL,
NULL);


*serverKeyProof = bytes_B;
*proof_len = len_B;

return 0;

}

void SRPClient::createSaltedVerificationKey(uint8_t * salt, uint8_t * verificationKey )
{
// let salt = salt ?? Data(bytes: try! Random.generate(byteCount: 16))
salt = (uint8_t *)malloc(16);
salt = (uint8_t *)malloc(16 + 20);
uint8_t defaultSalt[16] = {0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E, 0xB5, 0xA7, 0x27, 0x67, 0x3A, 0x24, 0x41, 0xEE};
memcpy(salt, defaultSalt, 16);


uint8_t * key = (uint8_t *)malloc(64);
uint8_t * key = (uint8_t *)malloc(20);
static char namePass[18] = "alice:password123";
crypto_hash_sha1((uint8_t *)key, (uint8_t *)namePass, 17);

memcpy(salt+16, key, 16);
crypto_hash_sha1((uint8_t *)key, (uint8_t *)salt, 16 + 20);

crypto_hash_sha512((uint8_t *)key, _username, 5);

// let x = calculate_x(algorithm: algorithm, salt: salt, username: username, password: password)

Expand Down
35 changes: 35 additions & 0 deletions src/SRPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,55 @@
# include "application.h"
#endif

#include "srp.h"

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

extern "C" SRP_Result srp_create_salted_verification_key(SRP_HashAlgorithm alg,
SRP_NGType ng_type, const char *username_for_verifier,
const unsigned char *password, size_t len_password,
unsigned char **bytes_s, size_t *len_s,
unsigned char **bytes_v, size_t *len_v,
const char *n_hex, const char *g_hex);


extern "C" struct SRPVerifier* srp_verifier_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
const char *username,
const unsigned char *bytes_s, size_t len_s,
const unsigned char *bytes_v, size_t len_v,
const unsigned char *bytes_A, size_t len_A,
const unsigned char *bytes_b, size_t len_b,
unsigned char** bytes_B, size_t *len_B,
const char* n_hex, const char* g_hex);

extern "C" struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
const char *username, const char *username_for_verifier,
const unsigned char *bytes_password, size_t len_password, const char *n_hex,
const char *g_hex);

extern "C" SRP_Result srp_user_start_authentication(struct SRPUser* usr, char **username,
const unsigned char *bytes_a, size_t len_a,
unsigned char **bytes_A, size_t* len_A);



class SRPClient
{
public:
SRPClient();
~SRPClient();

int getChallenge(uint8_t **salt,uint16_t *salt_len, uint8_t **key, uint16_t *key_len);

int verifySession(uint8_t **serverKeyProof, uint16_t *proof_len, uint8_t *clientPublicKey, uint16_t client_key_len, uint8_t *clientKeyProof, uint16_t client_proof_len);

void createSaltedVerificationKey(uint8_t * salt, uint8_t * verificationKey );
int crypto_hash_sha512(unsigned char *out, const unsigned char *in, unsigned long long inlen);
int crypto_hash_sha1(unsigned char *out, const unsigned char *in, unsigned long long inlen);


private:
uint8_t * _salt;
Expand Down
Loading

0 comments on commit 709fe34

Please sign in to comment.