scyther/src/term.h

221 lines
6.7 KiB
C
Raw Normal View History

/*
* Scyther : An automatic verifier for security protocols.
* Copyright (C) 2007-2020 Cas Cremers
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
2004-04-23 11:58:43 +01:00
#ifndef TERMS
#define TERMS
#include "symbol.h"
#include "bool.h"
2004-07-29 12:15:07 +01:00
// type <= LEAF means it's a leaf, nkay?
enum termtypes
{ GLOBAL, VARIABLE, LEAF, ENCRYPT, TUPLE };
2004-04-23 11:58:43 +01:00
2004-05-15 13:33:01 +01:00
//! The most basic datatype in the modelchecker.
/**
* Describes a single term.
*/
2004-04-23 11:58:43 +01:00
struct term
{
/* basic : name,runid
encrypt: op,key
tuple : op,next
*/
2004-05-15 13:33:01 +01:00
//! The type of term.
/**
* \sa GLOBAL, VARIABLE, LEAF, ENCRYPT, TUPLE
*/
2004-04-23 11:58:43 +01:00
int type;
2004-05-15 13:33:01 +01:00
//! Data Type termlist (e.g. agent or nonce)
/** Only for leaves. */
void *stype; // list of types
union
{
int roleVar; //!< only for leaf, arachne engine: role variable flag
int fcall; //!< only for 'encryption' to mark actual function call f(t)
} helper;
2004-05-15 13:33:01 +01:00
//! Substitution term.
/**
* If this is non-NULL, this leaf term is apparently substituted by
* this term.
*/
2004-04-23 11:58:43 +01:00
struct term *subst; // only for variable/leaf, substitution term
union
2004-04-23 11:58:43 +01:00
{
2006-01-02 21:06:08 +00:00
//! Pointer to the symbol for leaves
2004-04-23 11:58:43 +01:00
Symbol symb;
2004-05-15 13:33:01 +01:00
//! Encrypted subterm.
2004-04-23 11:58:43 +01:00
struct term *op;
2004-05-15 13:33:01 +01:00
//! Left-hand side of tuple pair.
2004-04-23 11:58:43 +01:00
struct term *op1;
2006-01-02 21:06:08 +00:00
struct term *next; //!< for alternative memory management
} left;
2004-04-23 11:58:43 +01:00
union
{
2006-01-02 21:06:08 +00:00
//! run identifier for leaves
2004-04-23 11:58:43 +01:00
int runid;
2004-05-15 13:33:01 +01:00
//! Key used to encrypt subterm.
2004-04-23 11:58:43 +01:00
struct term *key;
2004-05-15 13:33:01 +01:00
//! Right-hand side of tuple pair.
2004-04-23 11:58:43 +01:00
struct term *op2;
} right;
2004-04-23 11:58:43 +01:00
};
//! Component macros (left)
#define TermSymb(t) (t->left.symb)
#define TermOp1(t) (t->left.op1)
#define TermOp(t) (t->left.op)
//! Component macros (right)
#define TermRunid(t) (t->right.runid)
#define TermOp2(t) (t->right.op2)
#define TermKey(t) (t->right.key)
//! Flag for term status
extern int rolelocal_variable;
2004-05-15 13:33:01 +01:00
//! Pointer shorthand.
2004-04-23 11:58:43 +01:00
typedef struct term *Term;
void termsInit (void);
void termsDone (void);
Term makeTermEncrypt (Term t1, Term t2);
Term makeTermFcall (Term t1, Term t2);
2004-04-23 11:58:43 +01:00
Term makeTermTuple (Term t1, Term t2);
Term makeTermType (const int type, const Symbol symb, const int runid);
Term deVarScan (Term t);
2004-04-23 11:58:43 +01:00
#define realTermLeaf(t) (t != NULL && t->type <= LEAF)
#define realTermTuple(t) (t != NULL && t->type == TUPLE)
#define realTermEncrypt(t) (t != NULL && t->type == ENCRYPT)
#define realTermVariable(t) (t != NULL && (t->type == VARIABLE || (t->type <= LEAF && rolelocal_variable && TermRunid(t) == -3)))
#define substVar(t) ((realTermVariable (t) && t->subst != NULL) ? 1 : 0)
#define deVar(t) ( substVar(t) ? deVarScan(t->subst) : t)
2004-04-23 11:58:43 +01:00
#define isTermLeaf(t) realTermLeaf(deVar(t))
#define isTermTuple(t) realTermTuple(deVar(t))
#define isTermEncrypt(t) realTermEncrypt(deVar(t))
#define isTermVariable(t) realTermVariable(deVar(t))
#ifdef DEBUG
#define isTermEqual(t1,t2) isTermEqualDebug(t1,t2)
int isTermEqualDebug (Term t1, Term t2);
#else
#define isTermEqual1(t1,t2) ((substVar(t1) || substVar(t2)) \
2004-04-23 11:58:43 +01:00
? isTermEqualFn(t1,t2) \
: ( \
(t1 == t2) \
? 1 \
: ( \
(t1 == NULL || t2 == NULL || t1->type != t2->type) \
? 0 \
: ( \
realTermLeaf(t1) \
? isTermEqualFn(t1,t2) \
2004-04-23 11:58:43 +01:00
: ( \
realTermEncrypt(t2) \
? (isTermEqualFn(TermKey(t1), TermKey(t2)) && \
isTermEqualFn(TermOp(t1), TermOp(t2))) \
: (isTermEqualFn(TermOp1(t1), TermOp1(t2)) && \
isTermEqualFn(TermOp2(t1), TermOp2(t2))) \
2004-04-23 11:58:43 +01:00
) \
) \
) \
) \
)
#define isTermEqual2(t1,t2) ((substVar(t1) || substVar(t2)) \
? isTermEqualFn(t1,t2) \
: ( \
(t1 == t2) \
? 1 \
: ( \
(t1 == NULL || t2 == NULL || t1->type != t2->type) \
? 0 \
: ( \
realTermLeaf(t1) \
? isTermEqualFn(t1,t2) \
: ( \
realTermEncrypt(t2) \
? (isTermEqual1(TermKey(t1), TermKey(t2)) && \
isTermEqual1(TermOp(t1), TermOp(t2))) \
: (isTermEqual1(TermOp1(t1), TermOp1(t2)) && \
isTermEqual1(TermOp2(t1), TermOp2(t2))) \
) \
) \
) \
) \
)
#define isTermEqual(t1,t2) isTermEqual2(t1,t2)
#endif
2004-04-23 11:58:43 +01:00
int hasTermVariable (Term term);
int isTermEqualFn (Term term1, Term term2);
int termSubTerm (Term t, Term tsub);
int termInTerm (Term t, Term tsub);
void termPrintCustom (Term term, char *leftvar, char *rightvar, char *lefttup,
char *righttup, char *leftenc, char *rightenc,
void (*callback) (const Term t));
2004-04-23 11:58:43 +01:00
void termPrint (Term term);
void termTuplePrintCustom (Term term, char *leftvar, char *rightvar,
char *lefttup, char *righttup, char *leftenc,
char *rightenc, void (*callback) (const Term t));
2004-05-19 09:39:39 +01:00
void termTuplePrint (Term term);
2004-04-23 11:58:43 +01:00
Term termDuplicate (const Term term);
Term termNodeDuplicate (const Term term);
2004-04-23 11:58:43 +01:00
Term termDuplicateDeep (const Term term);
Term termDuplicateUV (Term term);
void termDelete (const Term term);
void termNormalize (Term term);
Term termRunid (Term term, int runid);
int tupleCount (Term tt);
Term tupleProject (Term tt, int n);
int termSize (Term t);
float termDistance (Term t1, Term t2);
int termOrder (Term t1, Term t2);
int term_iterate (const Term term, int (*leaf) (Term t),
int (*nodel) (Term t), int (*nodem) (Term t),
int (*noder) (Term t));
int term_iterate_state_deVar (Term term, int (*leaf) (),
int (*nodel) (),
int (*nodem) (), int (*noder) (), void *state);
int term_iterate_state_leaves (const Term term, int (*func) (), void *state);
int term_iterate_state_open_leaves (const Term term, int (*func) (),
void *state);
void term_rolelocals_are_variables ();
int term_encryption_level (const Term term);
float term_constrain_level (const Term term);
void term_set_keylevels (const Term term);
2004-11-01 14:06:26 +00:00
void termPrintDiff (Term t1, Term t2);
int isLeafNameEqual (Term t1, Term t2);
Term freshTermPrefix (Term prefixterm);
Term intTermPrefix (const int n, Term prefixterm);
2006-01-02 19:55:34 +00:00
int isTermFunctionName (Term t);
Term getTermFunction (Term t);
unsigned int termHidelevel (const Term tsmall, Term tbig);
void termSubstPrint (Term t);
2006-01-02 19:55:34 +00:00
int iterateTermOther (const int myrun, Term t, int (*callback) (), void *s);
2004-04-23 11:58:43 +01:00
2006-03-08 15:12:58 +00:00
extern char *RUNSEP; // by default, set to "#"
2004-04-23 11:58:43 +01:00
#endif