| 1 | /* -*- Mode: C; c-file-style: "stroustrup" -*- */ |
| 2 | |
| 3 | #if !defined( _hashcash_h ) |
| 4 | #define _hashcash_h |
| 5 | |
| 6 | #if defined( __cplusplus ) |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
| 10 | #include <time.h> |
| 11 | |
| 12 | #if defined(WIN32) && !defined(MONOLITHIC) |
| 13 | #include <windows.h> |
| 14 | #endif |
| 15 | |
| 16 | #if !defined(HCEXPORT) |
| 17 | #if !defined(WIN32) || defined(MONOLITHIC) |
| 18 | #define HCEXPORT |
| 19 | #elif defined(BUILD_DLL) |
| 20 | #define HCEXPORT __declspec(dllexport) |
| 21 | #else /* USE_DLL */ |
| 22 | #define HCEXPORT extern __declspec(dllimport) |
| 23 | #endif |
| 24 | #endif |
| 25 | |
| 26 | extern int verbose_flag; |
| 27 | extern int no_purge_flag; |
| 28 | |
| 29 | #define HASHCASH_VERSION 1.22 |
| 30 | #define HASHCASH_FORMAT_VERSION 1 |
| 31 | #define stringify( x ) stringify2( x ) |
| 32 | #define stringify2( x ) #x |
| 33 | #define HASHCASH_VERSION_STRING stringify(HASHCASH_VERSION) |
| 34 | |
| 35 | #define TYPE_STR 0 |
| 36 | #define TYPE_WILD 1 |
| 37 | #define TYPE_REGEXP 2 |
| 38 | |
| 39 | #define MAX_UTC 13 |
| 40 | #define MAX_CTR 64 |
| 41 | #define MAX_RES 256 |
| 42 | #define MAX_STR 256 |
| 43 | #define MAX_VER 2 |
| 44 | #define MAX_EXT 10240 |
| 45 | #define MAX_TOK ((MAX_VER)+1+(MAX_RES)+1+(MAX_EXT)+1+(MAX_UTC)+1+(MAX_STR)) |
| 46 | |
| 47 | #define TIME_MINUTE 60 |
| 48 | #define TIME_HOUR (TIME_MINUTE*60) |
| 49 | #define TIME_DAY (TIME_HOUR*24) |
| 50 | #define TIME_YEAR (TIME_DAY*365) |
| 51 | #define TIME_MONTH (TIME_YEAR/12) |
| 52 | #define TIME_AEON (TIME_YEAR*1000000000.0) |
| 53 | #define TIME_MILLI_SECOND (1.0/1000) |
| 54 | #define TIME_MICRO_SECOND (TIME_MILLI_SECOND/1000) |
| 55 | #define TIME_NANO_SECOND (TIME_MICRO_SECOND/1000) |
| 56 | |
| 57 | #define HASHCASH_OK 1 |
| 58 | #define HASHCASH_FAIL 0 |
| 59 | #define HASHCASH_VALID_FOREVER 0 |
| 60 | #define HASHCASH_INVALID_TOK_LEN -1 |
| 61 | #define HASHCASH_RNG_FAILED -2 |
| 62 | #define HASHCASH_INVALID_TIME -3 |
| 63 | #define HASHCASH_TOO_MANY_TRIES -4 |
| 64 | #define HASHCASH_EXPIRED_ON_CREATION -5 |
| 65 | #define HASHCASH_INVALID_VALIDITY_PERIOD -6 |
| 66 | #define HASHCASH_INTERNAL_ERROR -7 |
| 67 | #define HASHCASH_INVALID_TIME_WIDTH -8 |
| 68 | #define HASHCASH_VALID_IN_FUTURE -9 |
| 69 | #define HASHCASH_EXPIRED -10 |
| 70 | #define HASHCASH_INVALID -11 |
| 71 | #define HASHCASH_WRONG_RESOURCE -12 |
| 72 | #define HASHCASH_INSUFFICIENT_BITS -13 |
| 73 | #define HASHCASH_UNSUPPORTED_VERSION -14 |
| 74 | #define HASHCASH_SPENT -15 |
| 75 | #define HASHCASH_NO_TOKEN -16 |
| 76 | #define HASHCASH_REGEXP_ERROR -17 |
| 77 | #define HASHCASH_OUT_OF_MEMORY -18 |
| 78 | #define HASHCASH_USER_ABORT -19 |
| 79 | |
| 80 | /* return hashcash version number major.minor eg "1.13" */ |
| 81 | |
| 82 | HCEXPORT |
| 83 | const char* hashcash_version( void ); |
| 84 | |
| 85 | /* function hashcash_mint calling info: |
| 86 | * |
| 87 | * returns HASHCASH_OK on success or one of the error codes above on |
| 88 | * failure (values < 0) |
| 89 | * |
| 90 | * arguments are: |
| 91 | * |
| 92 | * now_time -- should be time in UTC, use time(2) |
| 93 | * |
| 94 | * time_width -- how many chars to abbreviate the time to |
| 95 | * default 0 is 6 chars YYMMDD |
| 96 | * |
| 97 | * resource -- resource name, unique descriptor for resource you're |
| 98 | * trying to protect |
| 99 | * |
| 100 | * bits -- bits of partial preimage the resource demands |
| 101 | * |
| 102 | * anon_period -- add (or subtract if negative) a random period |
| 103 | * in seconds between this value and 0 |
| 104 | * to get same as mixmaster does set this to -3days |
| 105 | * (default 0) |
| 106 | * |
| 107 | * stamp -- the stamp output |
| 108 | * |
| 109 | * anon_random -- default NULL, if set to pointer to long |
| 110 | * returns actual random offset added to time |
| 111 | * |
| 112 | * tries_taken -- default NULL, if set to pointer to double |
| 113 | * returns number of stamps tested before finding |
| 114 | * returned stamp. |
| 115 | * |
| 116 | * ext -- extension field |
| 117 | * |
| 118 | * compress -- compress the stamp 0 = fast (no compression), 1 = medium |
| 119 | * (some compression), 2 = very compressed (but slow) |
| 120 | * |
| 121 | * small -- optimize for stamp size (true) or stamp generation speed |
| 122 | * (false) |
| 123 | * |
| 124 | * cb -- user defined callback function which is called every |
| 125 | * 1/10th second; if it returns false hashcash_mint |
| 126 | * will return HASHCASH_USER_ABORT; if you do not need |
| 127 | * the callback function, pass NULL |
| 128 | * |
| 129 | * user_arg -- user argument passed to the callback function, if you |
| 130 | * do not need the callback function or if you do not need |
| 131 | * the arguement pass NULL. |
| 132 | * |
| 133 | */ |
| 134 | |
| 135 | /* note: it is the callers responsibility to call hashcash_free on |
| 136 | * stamp when finished */ |
| 137 | |
| 138 | typedef int (*hashcash_callback)(int percent, int largest, int target, |
| 139 | double count, double expected, |
| 140 | void* user); |
| 141 | |
| 142 | HCEXPORT |
| 143 | int hashcash_mint( time_t now_time, int time_width, const char* resource, |
| 144 | unsigned bits, long anon_period, char** stamp, |
| 145 | long* anon_random, double* tries_taken, char* ext, |
| 146 | int compress, hashcash_callback cb, void* user_arg ); |
| 147 | |
| 148 | /* simpler API for minting */ |
| 149 | |
| 150 | HCEXPORT |
| 151 | char* hashcash_simple_mint( const char* resource, unsigned bits, |
| 152 | long anon_period, char* ext, int compress ); |
| 153 | |
| 154 | /* convert a stamp into a X-Hashcash: header wrapping lines at line_len |
| 155 | * chars. If line_len = 0, does not wrap, just prepends header. |
| 156 | * |
| 157 | * arguments are: |
| 158 | * |
| 159 | * stamp -- the stamp to put in the header |
| 160 | * line_len -- the number of chars you want to wrap after (or no wrapping |
| 161 | * if 0 is given) |
| 162 | * lf -- what kind of line feed you want "\r\n" (DOS), "\n" (UNIX), |
| 163 | * "\r" (MAC). Actually I think RFC822 requires DOS lf. If |
| 164 | * you pass NULL it uses the standard RFC822 lf ("\r\n"). |
| 165 | * |
| 166 | */ |
| 167 | |
| 168 | HCEXPORT |
| 169 | char* hashcash_make_header( const char* stamp, int line_len, |
| 170 | const char* header, char cont, |
| 171 | const char* lf ); |
| 172 | |
| 173 | /* return time field width necessary to express time in sufficient |
| 174 | * resolution to avoid premature expiry of stamp minted |
| 175 | * for a resource which advertises the given validity period |
| 176 | * |
| 177 | */ |
| 178 | |
| 179 | HCEXPORT |
| 180 | int hashcash_validity_to_width( long validity_period ); |
| 181 | |
| 182 | /* count bits of preimage in stamp */ |
| 183 | |
| 184 | HCEXPORT |
| 185 | unsigned hashcash_count( const char* stamp ); |
| 186 | |
| 187 | /* parse stamp into time field and resouce name |
| 188 | * max lengths exclude space for trailing \0 |
| 189 | * |
| 190 | * arguments are: |
| 191 | * |
| 192 | * stamp -- stamp to parse |
| 193 | * vers -- stamp version (currently 0 or 1) |
| 194 | * bits -- (used in vers 1 only; with ver 0 returns -1) |
| 195 | * utct -- utc time field |
| 196 | * utct_max -- max width of utct field (definition is 13 max) |
| 197 | * stamp_resource -- resource name from stamp |
| 198 | * res_max -- max width of resource name |
| 199 | * ext -- extension field |
| 200 | * ext_max -- max width of extension field |
| 201 | */ |
| 202 | |
| 203 | /* note: if don't care about ext field pass NULL argument */ |
| 204 | |
| 205 | /* note: if you pass non-NULL in ext, but *ext = NULL, hashcash_parse |
| 206 | * will allocate the space, in this case it is the callers |
| 207 | * responsibility to call hashcash_free on ext when finished */ |
| 208 | |
| 209 | HCEXPORT |
| 210 | int hashcash_parse( const char* stamp, int* vers, int* bits, char* utct, |
| 211 | int utct_max, char* stamp_resource, int res_max, |
| 212 | char** ext, int ext_max ); |
| 213 | |
| 214 | /* return how many seconds the stamp remains valid for |
| 215 | * return value HASHCASH_VALID_FOREVER (value 0) means forever |
| 216 | * return value < 0 is error code |
| 217 | * codes are: HASHCASH_VALID_IN_FUTURE |
| 218 | * HASHCASH_EXPIRED |
| 219 | * |
| 220 | * arguments are: |
| 221 | * |
| 222 | * stamp_time -- time field from stamp |
| 223 | * validity_period -- how long stamps are valid for |
| 224 | * grace_period -- how much clock error to tolerate |
| 225 | * now_time -- current time (UTC) |
| 226 | */ |
| 227 | |
| 228 | HCEXPORT |
| 229 | long hashcash_valid_for( time_t stamp_time, long validity_period, |
| 230 | long grace_period, time_t now_time ); |
| 231 | |
| 232 | /* simple function calling hashcash_parse, hashcash_count for convenience |
| 233 | * |
| 234 | * on error returns -ve values: |
| 235 | * |
| 236 | * HASHCASH_INVALID |
| 237 | * HASHCASH_UNSUPPORTED_VERSION, |
| 238 | * HASHCASH_WRONG_RESOURCE |
| 239 | * HASHCASH_REGEXP_ERROR |
| 240 | * HASHCASH_INSUFFICIENT_BITS |
| 241 | * |
| 242 | * on success returns time: left |
| 243 | * |
| 244 | * HASHCASH_FOREVER (actually 0) |
| 245 | * +ve number being remaining validity time in seconds |
| 246 | * |
| 247 | * arguments are: |
| 248 | * |
| 249 | * stamp -- stamp to check |
| 250 | * case_flag -- if set true case-sensitive (false case insensitive) |
| 251 | * resource -- resource expecting (maybe wildcard, regexp...) |
| 252 | * compile -- cache for compiled regexp (caller free afterwards) |
| 253 | * re_err -- if get HASHCASH_REGEXP_ERROR this contains error |
| 254 | * type -- type of string (TYPE_STR, TYPE_WILD, TYPE_REGEXP) |
| 255 | * now_time -- current time (UTC) |
| 256 | * validity_period -- how long stamps are valid for |
| 257 | * grace_period -- how much clock error to tolerate |
| 258 | * required_bits -- how many bits the stamp must have |
| 259 | * stamp_time -- returns: parsed and decoded time field from stamp |
| 260 | */ |
| 261 | |
| 262 | /* note: compile argument can be NULL in which case regexps |
| 263 | * compilations are not cached (and compile arg does not have to be |
| 264 | * hashcash_free'd) |
| 265 | */ |
| 266 | |
| 267 | /* note: it is the callers responsibility to call hashcash_free on |
| 268 | * compile when finished if regexps are used */ |
| 269 | |
| 270 | HCEXPORT |
| 271 | int hashcash_check( const char* stamp, int case_flag, const char* resource, |
| 272 | void **compile, char** re_err, int type, time_t now_time, |
| 273 | long validity_period, long grace_period, |
| 274 | int required_bits, time_t* stamp_time ); |
| 275 | |
| 276 | /* return how many tries per second the machine can do */ |
| 277 | |
| 278 | HCEXPORT |
| 279 | unsigned long hashcash_per_sec( void ); |
| 280 | |
| 281 | /* estimate how many seconds it would take to mint a stamp of given size */ |
| 282 | |
| 283 | HCEXPORT |
| 284 | double hashcash_estimate_time( int b ); |
| 285 | |
| 286 | /* expected number of tries to mint stamp of size b */ |
| 287 | /* note: equivalent to pow( 2, b ) but without using libm */ |
| 288 | |
| 289 | HCEXPORT |
| 290 | double hashcash_expected_tries( int b ); |
| 291 | |
| 292 | /* hashcash_resource_match |
| 293 | * |
| 294 | * For comparing received stamps to addresses the user is willing to |
| 295 | * receive email as. |
| 296 | * |
| 297 | * returns 0 and *err == NULL if no match |
| 298 | * returns 0 and *err != NULL if regexp error, regexp explanatory error msg |
| 299 | * held in *err |
| 300 | * returns 1 on match |
| 301 | * |
| 302 | * arguments are: |
| 303 | * |
| 304 | * type -- type of comparison: TYPE_STR (simple string), |
| 305 | * TYPE_WILD (wildcard '*'), TYPE_REGEXP |
| 306 | * (full regular expression) |
| 307 | * |
| 308 | * stamp_res -- resource from the stamp being compared |
| 309 | * |
| 310 | * res -- resource we're comparing against |
| 311 | * |
| 312 | * compile -- cache for regexp compilation result |
| 313 | * (hashcash_free it when you've finished) |
| 314 | * |
| 315 | * err -- NULL on success, regexp error string if regexp fails |
| 316 | */ |
| 317 | |
| 318 | HCEXPORT |
| 319 | int hashcash_resource_match( int type, const char* stamp_res, const char* res, |
| 320 | void** compile, char** err ); |
| 321 | |
| 322 | /* free memory which may beallocated by: |
| 323 | * |
| 324 | * hashcash_check |
| 325 | * hashcash_mint |
| 326 | * hashcash_parse |
| 327 | * hashcash_simple_mint |
| 328 | * hashcash_resource_match |
| 329 | */ |
| 330 | |
| 331 | HCEXPORT |
| 332 | void hashcash_free( void* ptr); |
| 333 | |
| 334 | |
| 335 | /* bit of redundancy to avoid needing to distribute multiple header |
| 336 | * files with the DLL (this is an excerpt from utct.h) |
| 337 | */ |
| 338 | |
| 339 | #define MAX_UTC 13 |
| 340 | |
| 341 | /* convert utct string into a time_t |
| 342 | * |
| 343 | * returns (time_t)-1 on error |
| 344 | * |
| 345 | * arguments are: |
| 346 | * |
| 347 | * utct -- utct string to convert |
| 348 | * |
| 349 | * utc -- if true use UTC, else express in local time zone |
| 350 | */ |
| 351 | |
| 352 | HCEXPORT |
| 353 | time_t hashcash_from_utctimestr( const char utct[MAX_UTC+1], int utc ); |
| 354 | |
| 355 | /* convert time_t into utct string (note utct is always in utc time) |
| 356 | * |
| 357 | * returns 1 on success, 0 on failure |
| 358 | * |
| 359 | * arguments are: |
| 360 | * |
| 361 | * utct -- return utct converted string |
| 362 | * |
| 363 | * len -- how many digits of utc to use; valid utc widths are: |
| 364 | * 6,10,12 (though the code can generate 2,4,6,8,10,12 |
| 365 | */ |
| 366 | |
| 367 | HCEXPORT |
| 368 | int hashcash_to_utctimestr( char utct[MAX_UTC+1], int len, time_t t ); |
| 369 | |
| 370 | /* returns max rate of hashcash / sec using a slower but more accurate |
| 371 | * benchmarking method |
| 372 | * |
| 373 | * arguments are: |
| 374 | * |
| 375 | * verbose -- verbosity level 0 = no output, 1 = some, 2 = more, 3 = most |
| 376 | * core -- core number CORE_ALL (= -1) = all cores, or specific |
| 377 | */ |
| 378 | |
| 379 | /* returns speed of hashcash */ |
| 380 | |
| 381 | HCEXPORT |
| 382 | unsigned long hashcash_benchtest( int verbose, int core ); |
| 383 | |
| 384 | /* returns current core */ |
| 385 | |
| 386 | HCEXPORT |
| 387 | int hashcash_core(void); |
| 388 | |
| 389 | /* use specified core */ |
| 390 | |
| 391 | HCEXPORT |
| 392 | int hashcash_use_core(int); |
| 393 | |
| 394 | /* give name of specified core */ |
| 395 | |
| 396 | HCEXPORT |
| 397 | const char* hashcash_core_name(int); |
| 398 | |
| 399 | |
| 400 | #if defined( __cplusplus ) |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | #endif |