Skip to content

Commit

Permalink
merge the good stuff from komodo, move vectors and remove submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
ssadler committed Apr 8, 2018
2 parents 006d244 + 457f9bb commit 2f7aa85
Show file tree
Hide file tree
Showing 36 changed files with 797 additions and 194 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
*~
converter-sample.c
config.*
.pytest_cache/
.pytest_cache
src/asn/asn_system.h
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

1 change: 0 additions & 1 deletion ext/crypto-conditions
Submodule crypto-conditions deleted from 189f0b
53 changes: 26 additions & 27 deletions include/cryptoconditions.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <Condition.h>
#include <Fulfillment.h>
#include <cJSON.h>
#include <stdint.h>

Expand All @@ -18,31 +16,32 @@ struct CCType;


enum CCTypeId {
CC_Condition = -1,
CC_Anon = -1,
CC_Preimage = 0,
CC_Prefix = 1,
CC_Threshold = 2,
CC_Ed25519 = 4
};



/*
* Crypto Condition
*/
typedef struct CC {
struct CCType *type;
union {
// public key types
struct { unsigned char *publicKey, *signature; };
struct { uint8_t *publicKey, *signature; };
// preimage
struct { unsigned char *preimage; size_t preimageLength; };
struct { uint8_t *preimage; size_t preimageLength; };
// threshold
struct { long threshold; int size; struct CC **subconditions; };
struct { long threshold; uint8_t size; struct CC **subconditions; };
// prefix
struct { unsigned char *prefix; size_t prefixLength; struct CC *subcondition;
unsigned long maxMessageLength; };
struct { uint8_t *prefix; size_t prefixLength; struct CC *subcondition;
size_t maxMessageLength; };
// anon
struct { unsigned char fingerprint[32]; uint32_t subtypes; unsigned long cost;
struct { uint8_t fingerprint[32]; uint32_t subtypes; unsigned long cost;
struct CCType *conditionType; };
};
} CC;
Expand All @@ -54,7 +53,7 @@ typedef struct CC {
*/
typedef struct CCVisitor {
int (*visit)(CC *cond, struct CCVisitor visitor);
const unsigned char *msg;
const uint8_t *msg;
size_t msgLength;
void *context;
} CCVisitor;
Expand All @@ -64,27 +63,27 @@ typedef struct CCVisitor {
* Public methods
*/
int cc_isFulfilled(const CC *cond);
int cc_verify(const struct CC *cond, const unsigned char *msg, size_t msgLength,
int doHashMessage, const unsigned char *condBin, size_t condBinLength);
int cc_verify(const struct CC *cond, const uint8_t *msg, size_t msgLength,
const uint8_t *condBin, size_t condBinLength);
int cc_visit(CC *cond, struct CCVisitor visitor);
int cc_signTreeEd25519(CC *cond, const unsigned char *privateKey,
const unsigned char *msg, size_t msgLength);
int cc_signTreeSecp256k1Msg32(CC *cond, const unsigned char *privateKey, const unsigned char *msg32);
size_t cc_conditionBinary(const CC *cond, unsigned char *buf);
size_t cc_fulfillmentBinary(const CC *cond, unsigned char *buf, size_t bufLength);
static int cc_secp256k1VerifyTreeMsg32(const CC *cond, const unsigned char *msg32);
struct CC* cc_conditionFromJSON(cJSON *params, unsigned char *err);
struct CC* cc_conditionFromJSONString(const unsigned char *json, unsigned char *err);
struct CC* cc_readConditionBinary(const unsigned char *cond_bin, size_t cond_bin_len);
struct CC* cc_readFulfillmentBinary(const unsigned char *ffill_bin, size_t ffill_bin_len);
int cc_signTreeEd25519(CC *cond, const uint8_t *privateKey, const uint8_t *msg,
const size_t msgLength);
size_t cc_conditionBinary(const CC *cond, uint8_t *buf);
size_t cc_fulfillmentBinary(const CC *cond, uint8_t *buf, size_t bufLength);
struct CC* cc_conditionFromJSON(cJSON *params, char *err);
struct CC* cc_conditionFromJSONString(const char *json, char *err);
struct CC* cc_readConditionBinary(const uint8_t *cond_bin, size_t cond_bin_len);
struct CC* cc_readFulfillmentBinary(const uint8_t *ffill_bin, size_t ffill_bin_len);
struct CC* cc_new(int typeId);
struct cJSON* cc_conditionToJSON(const CC *cond);
unsigned char* cc_conditionToJSONString(const CC *cond);
unsigned char* cc_conditionUri(const CC *cond);
unsigned char* cc_jsonRPC(unsigned char *request);
unsigned long cc_getCost(const CC *cond);
enum CCTypeId cc_typeId(const CC *cond);
char* cc_conditionToJSONString(const CC *cond);
char* cc_conditionUri(const CC *cond);
char* cc_jsonRPC(char *request);
char* cc_typeName(const CC *cond);
enum CCTypeId cc_typeId(const CC *cond);
unsigned long cc_getCost(const CC *cond);
uint32_t cc_typeMask(const CC *cond);
int cc_isAnon(const CC *cond);
void cc_free(struct CC *cond);

#ifdef __cplusplus
Expand Down
11 changes: 5 additions & 6 deletions src/anon.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
#include "cryptoconditions.h"


struct CCType cc_anonType;
struct CCType CC_AnonType;


static CC *mkAnon(const Condition_t *asnCond) {
CC *mkAnon(const Condition_t *asnCond) {

CCType *realType = getTypeByAsnEnum(asnCond->present);
if (!realType) {
printf("Unknown ASN type: %i", asnCond->present);
fprintf(stderr, "Unknown ASN type: %i", asnCond->present);
return 0;
}
CC *cond = calloc(1, sizeof(CC));
cond->type = &cc_anonType;
CC *cond = cc_new(CC_Anon);
cond->conditionType = realType;
const CompoundSha256Condition_t *deets = &asnCond->choice.thresholdSha256;
memcpy(cond->fingerprint, deets->fingerprint.buf, 32);
Expand Down Expand Up @@ -71,4 +70,4 @@ static int anonIsFulfilled(const CC *cond) {
}


struct CCType cc_anonType = { -1, "(anon)", Condition_PR_NOTHING, NULL, &anonFingerprint, &anonCost, &anonSubtypes, NULL, &anonToJSON, NULL, &anonFulfillment, &anonIsFulfilled, &anonFree };
struct CCType CC_AnonType = { -1, "(anon)", Condition_PR_NOTHING, NULL, &anonFingerprint, &anonCost, &anonSubtypes, NULL, &anonToJSON, NULL, &anonFulfillment, &anonIsFulfilled, &anonFree };
54 changes: 29 additions & 25 deletions src/cryptoconditions.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
#include <malloc.h>


static struct CCType *typeRegistry[] = {
&cc_preimageType,
&cc_prefixType,
&cc_thresholdType,
NULL, /* &cc_rsaType */
&cc_ed25519Type
struct CCType *CCTypeRegistry[] = {
&CC_PreimageType,
&CC_PrefixType,
&CC_ThresholdType,
NULL, /* &CC_rsaType */
&CC_Ed25519Type
};


static int typeRegistryLength = sizeof(typeRegistry) / sizeof(typeRegistry[0]);
int CCTypeRegistryLength = sizeof(CCTypeRegistry) / sizeof(CCTypeRegistry[0]);


void appendUriSubtypes(uint32_t mask, unsigned char *buf) {
Expand All @@ -32,18 +32,18 @@ void appendUriSubtypes(uint32_t mask, unsigned char *buf) {
if (mask & 1 << i) {
if (append) {
strcat(buf, ",");
strcat(buf, typeRegistry[i]->name);
strcat(buf, CCTypeRegistry[i]->name);
} else {
strcat(buf, "&subtypes=");
strcat(buf, typeRegistry[i]->name);
strcat(buf, CCTypeRegistry[i]->name);
append = 1;
}
}
}
}


unsigned char *cc_conditionUri(const CC *cond) {
char *cc_conditionUri(const CC *cond) {
unsigned char *fp = cond->type->fingerprint(cond);
if (!fp) return NULL;

Expand All @@ -64,7 +64,7 @@ unsigned char *cc_conditionUri(const CC *cond) {
}


static ConditionTypes_t asnSubtypes(uint32_t mask) {
ConditionTypes_t asnSubtypes(uint32_t mask) {
ConditionTypes_t types;
uint8_t buf[4] = {0,0,0,0};
int maxId = 0;
Expand All @@ -84,7 +84,7 @@ static ConditionTypes_t asnSubtypes(uint32_t mask) {
}


static uint32_t fromAsnSubtypes(const ConditionTypes_t types) {
uint32_t fromAsnSubtypes(const ConditionTypes_t types) {
uint32_t mask = 0;
for (int i=0; i<types.size*8; i++) {
if (types.buf[i >> 3] & (1 << (7 - i % 8))) {
Expand Down Expand Up @@ -120,7 +120,7 @@ size_t cc_fulfillmentBinary(const CC *cond, unsigned char *buf, size_t length) {
}


static void asnCondition(const CC *cond, Condition_t *asn) {
void asnCondition(const CC *cond, Condition_t *asn) {
asn->present = cc_isAnon(cond) ? cond->conditionType->asnType : cond->type->asnType;

// This may look a little weird - we dont have a reference here to the correct
Expand All @@ -135,14 +135,14 @@ static void asnCondition(const CC *cond, Condition_t *asn) {
}


static Condition_t *asnConditionNew(const CC *cond) {
Condition_t *asnConditionNew(const CC *cond) {
Condition_t *asn = calloc(1, sizeof(Condition_t));
asnCondition(cond, asn);
return asn;
}


static Fulfillment_t *asnFulfillmentNew(const CC *cond) {
Fulfillment_t *asnFulfillmentNew(const CC *cond) {
return cond->type->toFulfillment(cond);
}

Expand All @@ -153,16 +153,16 @@ unsigned long cc_getCost(const CC *cond) {


CCType *getTypeByAsnEnum(Condition_PR present) {
for (int i=0; i<typeRegistryLength; i++) {
if (typeRegistry[i] != NULL && typeRegistry[i]->asnType == present) {
return typeRegistry[i];
for (int i=0; i<CCTypeRegistryLength; i++) {
if (CCTypeRegistry[i] != NULL && CCTypeRegistry[i]->asnType == present) {
return CCTypeRegistry[i];
}
}
return NULL;
}


static CC *fulfillmentToCC(Fulfillment_t *ffill) {
CC *fulfillmentToCC(Fulfillment_t *ffill) {
CCType *type = getTypeByAsnEnum(ffill->present);
if (!type) {
fprintf(stderr, "Unknown fulfillment type: %i\n", ffill->present);
Expand Down Expand Up @@ -207,7 +207,7 @@ int cc_visit(CC *cond, CCVisitor visitor) {
}


int cc_verify(const struct CC *cond, const unsigned char *msg, size_t msgLength, int doHashMsg,
int cc_verify(const struct CC *cond, const unsigned char *msg, size_t msgLength,
const unsigned char *condBin, size_t condBinLength) {
unsigned char targetBinary[1000];
const size_t binLength = cc_conditionBinary(cond, targetBinary);
Expand All @@ -219,9 +219,6 @@ int cc_verify(const struct CC *cond, const unsigned char *msg, size_t msgLength,
return 0;
}

unsigned char msgHash[32];
if (doHashMsg) sha256(msg, msgLength, msgHash);
else memcpy(msgHash, msg, 32);
return 1;
}

Expand All @@ -231,7 +228,7 @@ CC *cc_readConditionBinary(const unsigned char *cond_bin, size_t length) {
asn_dec_rval_t rval;
rval = ber_decode(0, &asn_DEF_Condition, (void **)&asnCond, cond_bin, length);
if (rval.code != RC_OK) {
printf("Failed reading condition binary\n");
fprintf(stderr, "Failed reading condition binary\n");
return NULL;
}
CC *cond = mkAnon(asnCond);
Expand All @@ -241,7 +238,7 @@ CC *cc_readConditionBinary(const unsigned char *cond_bin, size_t length) {


int cc_isAnon(const CC *cond) {
return cond->type->typeId == CC_Condition;
return cond->type->typeId == CC_Anon;
}


Expand All @@ -268,6 +265,13 @@ char *cc_typeName(const CC *cond) {
}


CC *cc_new(int typeId) {
CC *cond = calloc(1, sizeof(CC));
cond->type = typeId == CC_Anon ? &CC_AnonType : CCTypeRegistry[typeId];
return cond;
}


void cc_free(CC *cond) {
if (cond)
cond->type->free(cond);
Expand Down
19 changes: 9 additions & 10 deletions src/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "cryptoconditions.h"


struct CCType cc_ed25519Type;
struct CCType CC_Ed25519Type;


static unsigned char *ed25519Fingerprint(const CC *cond) {
Expand All @@ -18,7 +18,7 @@ static unsigned char *ed25519Fingerprint(const CC *cond) {


int ed25519Verify(CC *cond, CCVisitor visitor) {
if (cond->type->typeId != cc_ed25519Type.typeId) return 1;
if (cond->type->typeId != CC_Ed25519Type.typeId) return 1;
// TODO: test failure mode: empty sig / null pointer
return ed25519_verify(cond->signature, visitor.msg, visitor.msgLength, cond->publicKey);
}
Expand All @@ -44,7 +44,7 @@ typedef struct CCEd25519SigningData {
* Visitor that signs an ed25519 condition if it has a matching public key
*/
static int ed25519Sign(CC *cond, CCVisitor visitor) {
if (cond->type->typeId != cc_ed25519Type.typeId) return 1;
if (cond->type->typeId != CC_Ed25519Type.typeId) return 1;
CCEd25519SigningData *signing = (CCEd25519SigningData*) visitor.context;
if (0 != memcmp(cond->publicKey, signing->pk, 32)) return 1;
if (!cond->signature) cond->signature = malloc(64);
Expand All @@ -58,7 +58,8 @@ static int ed25519Sign(CC *cond, CCVisitor visitor) {
/*
* Sign ed25519 conditions in a tree
*/
int cc_signTreeEd25519(CC *cond, const unsigned char *privateKey, const unsigned char *msg, size_t msgLength) {
int cc_signTreeEd25519(CC *cond, const unsigned char *privateKey, const unsigned char *msg,
const size_t msgLength) {
unsigned char pk[32], skpk[64];
ed25519_create_keypair(pk, skpk, privateKey);

Expand All @@ -74,7 +75,7 @@ static unsigned long ed25519Cost(const CC *cond) {
}


static CC *ed25519FromJSON(const cJSON *params, unsigned char *err) {
static CC *ed25519FromJSON(const cJSON *params, char *err) {
size_t binsz;

cJSON *pk_item = cJSON_GetObjectItem(params, "publicKey");
Expand Down Expand Up @@ -104,8 +105,7 @@ static CC *ed25519FromJSON(const cJSON *params, unsigned char *err) {
}
}

CC *cond = calloc(1, sizeof(CC));
cond->type = &cc_ed25519Type;
CC *cond = cc_new(CC_Ed25519);
cond->publicKey = pk;
cond->signature = sig;
return cond;
Expand All @@ -125,8 +125,7 @@ static void ed25519ToJSON(const CC *cond, cJSON *params) {


static CC *ed25519FromFulfillment(const Fulfillment_t *ffill) {
CC *cond = calloc(1, sizeof(CC));
cond->type = &cc_ed25519Type;
CC *cond = cc_new(CC_Ed25519);
cond->publicKey = malloc(32);
memcpy(cond->publicKey, ffill->choice.ed25519Sha256.publicKey.buf, 32);
cond->signature = malloc(64);
Expand Down Expand Up @@ -166,4 +165,4 @@ static uint32_t ed25519Subtypes(const CC *cond) {
}


struct CCType cc_ed25519Type = { 4, "ed25519-sha-256", Condition_PR_ed25519Sha256, 0, &ed25519Fingerprint, &ed25519Cost, &ed25519Subtypes, &ed25519FromJSON, &ed25519ToJSON, &ed25519FromFulfillment, &ed25519ToFulfillment, &ed25519IsFulfilled, &ed25519Free };
struct CCType CC_Ed25519Type = { 4, "ed25519-sha-256", Condition_PR_ed25519Sha256, 0, &ed25519Fingerprint, &ed25519Cost, &ed25519Subtypes, &ed25519FromJSON, &ed25519ToJSON, &ed25519FromFulfillment, &ed25519ToFulfillment, &ed25519IsFulfilled, &ed25519Free };
Loading

0 comments on commit 2f7aa85

Please sign in to comment.