From 660b32c300981dae415b55afeafb529bafdad298 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Mon, 2 Apr 2018 20:44:10 -0300 Subject: [PATCH] cryptoconditions functions to create conditions --- .../include/cryptoconditions.h | 2 +- src/cryptoconditions/src/anon.c | 6 +++--- src/cryptoconditions/src/cryptoconditions.c | 20 ++++++++++++------- src/cryptoconditions/src/ed25519.c | 12 +++++------ src/cryptoconditions/src/eval.c | 10 +++++----- src/cryptoconditions/src/internal.h | 3 ++- src/cryptoconditions/src/prefix.c | 8 ++++---- src/cryptoconditions/src/preimage.c | 8 ++++---- src/cryptoconditions/src/secp256k1.c | 12 +++++------ src/cryptoconditions/src/threshold.c | 8 ++++---- 10 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/cryptoconditions/include/cryptoconditions.h b/src/cryptoconditions/include/cryptoconditions.h index a911ffd79..1af542b03 100644 --- a/src/cryptoconditions/include/cryptoconditions.h +++ b/src/cryptoconditions/include/cryptoconditions.h @@ -26,7 +26,6 @@ enum CCTypeId { }; - /* * Evaliliary verification callback */ @@ -88,6 +87,7 @@ struct CC* cc_conditionFromJSON(cJSON *params, char *err); struct CC* cc_conditionFromJSONString(const char *json, 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); +struct CC* cc_new(int typeId); struct cJSON* cc_conditionToJSON(const CC *cond); char* cc_conditionToJSONString(const CC *cond); char* cc_conditionUri(const CC *cond); diff --git a/src/cryptoconditions/src/anon.c b/src/cryptoconditions/src/anon.c index 0d10e0df0..c9baa6245 100644 --- a/src/cryptoconditions/src/anon.c +++ b/src/cryptoconditions/src/anon.c @@ -7,7 +7,7 @@ #include "cryptoconditions.h" -struct CCType cc_anonType; +struct CCType CC_AnonType; static CC *mkAnon(const Condition_t *asnCond) { @@ -18,7 +18,7 @@ static CC *mkAnon(const Condition_t *asnCond) { return 0; } CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_anonType; + cond->type = &CC_AnonType; cond->conditionType = realType; const CompoundSha256Condition_t *deets = &asnCond->choice.thresholdSha256; memcpy(cond->fingerprint, deets->fingerprint.buf, 32); @@ -71,4 +71,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 }; diff --git a/src/cryptoconditions/src/cryptoconditions.c b/src/cryptoconditions/src/cryptoconditions.c index 037946a5f..f993b277f 100644 --- a/src/cryptoconditions/src/cryptoconditions.c +++ b/src/cryptoconditions/src/cryptoconditions.c @@ -17,14 +17,14 @@ struct CCType *CCTypeRegistry[] = { - &cc_preimageType, - &cc_prefixType, - &cc_thresholdType, - NULL, /* &cc_rsaType */ - &cc_ed25519Type, - &cc_secp256k1Type, + &CC_PreimageType, + &CC_PrefixType, + &CC_ThresholdType, + NULL, /* &CC_rsaType */ + &CC_Ed25519Type, + &CC_Secp256k1Type, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 6-14 unused */ - &cc_evalType + &CC_EvalType }; @@ -281,6 +281,12 @@ char *cc_typeName(const CC *cond) { return cc_isAnon(cond) ? cond->conditionType->name : cond->type->name; } +CC *cc_new(int typeId) { + CC *cond = malloc(sizeof(CC*)); + cond->type = CCTypeRegistry[type]; + return cond; +} + void cc_free(CC *cond) { if (cond) diff --git a/src/cryptoconditions/src/ed25519.c b/src/cryptoconditions/src/ed25519.c index b835cefc0..541c9b896 100644 --- a/src/cryptoconditions/src/ed25519.c +++ b/src/cryptoconditions/src/ed25519.c @@ -7,7 +7,7 @@ #include "cryptoconditions.h" -struct CCType cc_ed25519Type; +struct CCType CC_Ed25519Type; static unsigned char *ed25519Fingerprint(const CC *cond) { @@ -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); } @@ -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); @@ -105,7 +105,7 @@ static CC *ed25519FromJSON(const cJSON *params, unsigned char *err) { } CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_ed25519Type; + cond->type = &CC_Ed25519Type; cond->publicKey = pk; cond->signature = sig; return cond; @@ -126,7 +126,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; + cond->type = &CC_Ed25519Type; cond->publicKey = malloc(32); memcpy(cond->publicKey, ffill->choice.ed25519Sha256.publicKey.buf, 32); cond->signature = malloc(64); @@ -166,4 +166,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 }; diff --git a/src/cryptoconditions/src/eval.c b/src/cryptoconditions/src/eval.c index 13388b117..14db14c70 100644 --- a/src/cryptoconditions/src/eval.c +++ b/src/cryptoconditions/src/eval.c @@ -8,7 +8,7 @@ #include "include/cJSON.h" -struct CCType cc_evalType; +struct CCType CC_EvalType; static unsigned char *evalFingerprint(const CC *cond) { @@ -46,7 +46,7 @@ static CC *evalFromJSON(const cJSON *params, unsigned char *err) { strcpy(cond->method, method_item->valuestring); cond->paramsBin = paramsBin; cond->paramsBinLength = paramsBinLength; - cond->type = &cc_evalType; + cond->type = &CC_EvalType; return cond; } @@ -65,7 +65,7 @@ static void evalToJSON(const CC *cond, cJSON *params) { static CC *evalFromFulfillment(const Fulfillment_t *ffill) { CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_evalType; + cond->type = &CC_EvalType; EvalFulfillment_t *eval = &ffill->choice.evalSha256; @@ -125,7 +125,7 @@ typedef struct CCEvalVerifyData { int evalVisit(CC *cond, CCVisitor visitor) { - if (cond->type->typeId != cc_evalType.typeId) return 1; + if (cond->type->typeId != CC_Eval) return 1; CCEvalVerifyData *evalData = visitor.context; return evalData->verify(cond, evalData->context); } @@ -138,4 +138,4 @@ int cc_verifyEval(const CC *cond, VerifyEval verify, void *context) { } -struct CCType cc_evalType = { 15, "eval-sha-256", Condition_PR_evalSha256, 0, &evalFingerprint, &evalCost, &evalSubtypes, &evalFromJSON, &evalToJSON, &evalFromFulfillment, &evalToFulfillment, &evalIsFulfilled, &evalFree }; +struct CCType CC_EvalType = { 15, "eval-sha-256", Condition_PR_evalSha256, 0, &evalFingerprint, &evalCost, &evalSubtypes, &evalFromJSON, &evalToJSON, &evalFromFulfillment, &evalToFulfillment, &evalIsFulfilled, &evalFree }; diff --git a/src/cryptoconditions/src/internal.h b/src/cryptoconditions/src/internal.h index e4cdd49ba..d8e09310a 100644 --- a/src/cryptoconditions/src/internal.h +++ b/src/cryptoconditions/src/internal.h @@ -17,7 +17,8 @@ extern "C" { /* - * Condition Type */ + * Condition Type + */ typedef struct CCType { int typeId; char name[100]; diff --git a/src/cryptoconditions/src/prefix.c b/src/cryptoconditions/src/prefix.c index 423ee3ecf..d8d8b7aad 100644 --- a/src/cryptoconditions/src/prefix.c +++ b/src/cryptoconditions/src/prefix.c @@ -7,7 +7,7 @@ #include "cryptoconditions.h" -struct CCType cc_prefixType; +struct CCType CC_PrefixType; static int prefixVisitChildren(CC *cond, CCVisitor visitor) { @@ -43,7 +43,7 @@ static CC *prefixFromFulfillment(const Fulfillment_t *ffill) { CC *sub = fulfillmentToCC(p->subfulfillment); if (!sub) return 0; CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_prefixType; + cond->type = &CC_PrefixType; cond->maxMessageLength = p->maxMessageLength; cond->prefix = calloc(1, p->prefix.size); memcpy(cond->prefix, p->prefix.buf, p->prefix.size); @@ -89,7 +89,7 @@ static CC *prefixFromJSON(const cJSON *params, unsigned char *err) { } CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_prefixType; + cond->type = &CC_PrefixType; cond->maxMessageLength = (unsigned long) mml_item->valuedouble; cond->subcondition = sub; @@ -122,4 +122,4 @@ static void prefixFree(CC *cond) { } -struct CCType cc_prefixType = { 1, "prefix-sha-256", Condition_PR_prefixSha256, &prefixVisitChildren, &prefixFingerprint, &prefixCost, &prefixSubtypes, &prefixFromJSON, &prefixToJSON, &prefixFromFulfillment, &prefixToFulfillment, &prefixIsFulfilled, &prefixFree }; +struct CCType CC_PrefixType = { 1, "prefix-sha-256", Condition_PR_prefixSha256, &prefixVisitChildren, &prefixFingerprint, &prefixCost, &prefixSubtypes, &prefixFromJSON, &prefixToJSON, &prefixFromFulfillment, &prefixToFulfillment, &prefixIsFulfilled, &prefixFree }; diff --git a/src/cryptoconditions/src/preimage.c b/src/cryptoconditions/src/preimage.c index 95f19c09d..639bd1009 100644 --- a/src/cryptoconditions/src/preimage.c +++ b/src/cryptoconditions/src/preimage.c @@ -7,7 +7,7 @@ #include "cryptoconditions.h" -struct CCType cc_preimageType; +struct CCType CC_PreimageType; static CC *preimageFromJSON(const cJSON *params, unsigned char *err) { @@ -19,7 +19,7 @@ static CC *preimageFromJSON(const cJSON *params, unsigned char *err) { unsigned char *preimage_b64 = preimage_item->valuestring; CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_preimageType; + cond->type = &CC_PreimageType; cond->preimage = base64_decode(preimage_b64, &cond->preimageLength); return cond; } @@ -46,7 +46,7 @@ static unsigned char *preimageFingerprint(const CC *cond) { static CC *preimageFromFulfillment(const Fulfillment_t *ffill) { CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_preimageType; + cond->type = &CC_PreimageType; PreimageFulfillment_t p = ffill->choice.preimageSha256; cond->preimage = calloc(1, p.preimage.size); memcpy(cond->preimage, p.preimage.buf, p.preimage.size); @@ -79,4 +79,4 @@ static uint32_t preimageSubtypes(const CC *cond) { } -struct CCType cc_preimageType = { 0, "preimage-sha-256", Condition_PR_preimageSha256, 0, &preimageFingerprint, &preimageCost, &preimageSubtypes, &preimageFromJSON, &preimageToJSON, &preimageFromFulfillment, &preimageToFulfillment, &preimageIsFulfilled, &preimageFree }; +struct CCType CC_PreimageType = { 0, "preimage-sha-256", Condition_PR_preimageSha256, 0, &preimageFingerprint, &preimageCost, &preimageSubtypes, &preimageFromJSON, &preimageToJSON, &preimageFromFulfillment, &preimageToFulfillment, &preimageIsFulfilled, &preimageFree }; diff --git a/src/cryptoconditions/src/secp256k1.c b/src/cryptoconditions/src/secp256k1.c index 4a7588a89..f83397db8 100644 --- a/src/cryptoconditions/src/secp256k1.c +++ b/src/cryptoconditions/src/secp256k1.c @@ -15,7 +15,7 @@ #include "internal.h" -struct CCType cc_secp256k1Type; +struct CCType CC_Secp256k1Type; static const size_t SECP256K1_PK_SIZE = 33; @@ -74,7 +74,7 @@ static unsigned char *secp256k1Fingerprint(const CC *cond) { int secp256k1Verify(CC *cond, CCVisitor visitor) { - if (cond->type->typeId != cc_secp256k1Type.typeId) return 1; + if (cond->type->typeId != CC_Secp256k1Type.typeId) return 1; initVerify(); int rc; @@ -99,8 +99,8 @@ int secp256k1Verify(CC *cond, CCVisitor visitor) { static int cc_secp256k1VerifyTreeMsg32(const CC *cond, const unsigned char *msg32) { int subtypes = cc_typeMask(cond); - if (subtypes & (1 << cc_prefixType.typeId) && - subtypes & (1 << cc_secp256k1Type.typeId)) { + if (subtypes & (1 << CC_PrefixType.typeId) && + subtypes & (1 << CC_Secp256k1Type.typeId)) { // No support for prefix currently, due to pending protocol decision on // how to combine message and prefix into 32 byte hash return 0; @@ -203,7 +203,7 @@ static CC *cc_secp256k1Condition(const unsigned char *publicKey, const unsigned } CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_secp256k1Type; + cond->type = &CC_Secp256k1Type; cond->publicKey = pk; cond->signature = sig; return cond; @@ -281,4 +281,4 @@ static uint32_t secp256k1Subtypes(const CC *cond) { } -struct CCType cc_secp256k1Type = { 5, "secp256k1-sha-256", Condition_PR_secp256k1Sha256, 0, &secp256k1Fingerprint, &secp256k1Cost, &secp256k1Subtypes, &secp256k1FromJSON, &secp256k1ToJSON, &secp256k1FromFulfillment, &secp256k1ToFulfillment, &secp256k1IsFulfilled, &secp256k1Free }; +struct CCType CC_Secp256k1Type = { 5, "secp256k1-sha-256", Condition_PR_secp256k1Sha256, 0, &secp256k1Fingerprint, &secp256k1Cost, &secp256k1Subtypes, &secp256k1FromJSON, &secp256k1ToJSON, &secp256k1FromFulfillment, &secp256k1ToFulfillment, &secp256k1IsFulfilled, &secp256k1Free }; diff --git a/src/cryptoconditions/src/threshold.c b/src/cryptoconditions/src/threshold.c index 1efe9f9e8..82ef3d736 100644 --- a/src/cryptoconditions/src/threshold.c +++ b/src/cryptoconditions/src/threshold.c @@ -8,7 +8,7 @@ #include "internal.h" -struct CCType cc_thresholdType; +struct CCType CC_ThresholdType; static uint32_t thresholdSubtypes(const CC *cond) { @@ -120,7 +120,7 @@ static CC *thresholdFromFulfillment(const Fulfillment_t *ffill) { } CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_thresholdType; + cond->type = &CC_ThresholdType; cond->threshold = threshold; cond->size = size; cond->subconditions = subconditions; @@ -174,7 +174,7 @@ static CC *thresholdFromJSON(const cJSON *params, unsigned char *err) { } CC *cond = calloc(1, sizeof(CC)); - cond->type = &cc_thresholdType; + cond->type = &CC_ThresholdType; cond->threshold = (long) threshold_item->valuedouble; cond->size = cJSON_GetArraySize(subfulfillments_item); cond->subconditions = calloc(cond->size, sizeof(CC*)); @@ -222,4 +222,4 @@ static void thresholdFree(CC *cond) { } -struct CCType cc_thresholdType = { 2, "threshold-sha-256", Condition_PR_thresholdSha256, &thresholdVisitChildren, &thresholdFingerprint, &thresholdCost, &thresholdSubtypes, &thresholdFromJSON, &thresholdToJSON, &thresholdFromFulfillment, &thresholdToFulfillment, &thresholdIsFulfilled, &thresholdFree }; +struct CCType CC_ThresholdType = { 2, "threshold-sha-256", Condition_PR_thresholdSha256, &thresholdVisitChildren, &thresholdFingerprint, &thresholdCost, &thresholdSubtypes, &thresholdFromJSON, &thresholdToJSON, &thresholdFromFulfillment, &thresholdToFulfillment, &thresholdIsFulfilled, &thresholdFree };