/** * @file ristretto.cxx * @author Mike Hamburg * * @copyright * Copyright (c) 2015 Cryptography Research, Inc. \n * Released under the MIT License. See LICENSE.txt for license information. * * @brief Ristretto implementation widget */ #include #include using namespace decaf; static inline int hexi(char c) { if (c >= '0' && c <= '9') return c-'0'; if (c >= 'a' && c <= 'f') return c-'a'+0xa; if (c >= 'A' && c <= 'F') return c-'A'+0xa; return -1; } static int parsehex(uint8_t *out, size_t sizeof_out, const char *hex) { size_t l = strlen(hex); if (l%2 != 0) { fprintf(stderr,"String should be hex, but has odd length\n: %s\n", hex); return -1; } else if (l/2 > sizeof_out) { fprintf(stderr,"Argument is too long: %s\n", hex); return -1; } memset(out,0,sizeof_out); int ret1,ret2; for (size_t i=0; i 0; in++,sizeof_in--) { printf("%02x",*in); } } static int g_argc = 0; static char **g_argv = NULL; static int error = 0; static int done = 0; static void usage() { const char *me=g_argv[0]; if (!me) me = "ristretto"; for (unsigned i=0; g_argv[0][i]; i++) { if (g_argv[0][i] == '/' && g_argv[0][i+1] != 0 && g_argv[0][i+1] != '/') { me = &g_argv[0][i]; } } fprintf(stderr,"Usage: %s [points] [operations] ...\n", me); fprintf(stderr," -b 255|448: Set which group to use (sometimes inferred from lengths)\n"); fprintf(stderr," -E: Display output as Elligator inverses\n"); fprintf(stderr," -D: Display output in EdDSA format (times clearing ratio)\n"); fprintf(stderr," -R: Display raw xyzt\n"); fprintf(stderr," -C: Display output in X[25519|448] format\n"); fprintf(stderr," -H: ... divide by encoding ratio first\n"); fprintf(stderr,"\n"); fprintf(stderr," Ways to create points:\n"); fprintf(stderr," [hex]: Point from point data as hex\n"); fprintf(stderr," -e [hex]: Create point by hashing to curve using elligator\n"); fprintf(stderr," base: Base point of curve\n"); fprintf(stderr," identity: Identity point of curve\n"); fprintf(stderr,"\n"); fprintf(stderr," Operations:\n"); fprintf(stderr," -n [point]: negative of point\n"); fprintf(stderr," -s [scalar] * [point]: Hash to curve using elligator\n"); fprintf(stderr," [point] + [point]: Add two points\n"); fprintf(stderr,"\n"); fprintf(stderr," NB: this is a debugging widget. It doesn't yet have order of operations.\n"); fprintf(stderr," *** DON'T USE THIS UTILITY FOR ACTUAL CRYPTO! ***\n"); fprintf(stderr," It's only for debugging!\n"); fprintf(stderr,"\n"); exit(-2); } template class Run { public: static void run() { uint8_t tmp[Group::Point::SER_BYTES]; typename Group::Point a,b; typename Group::Scalar s; bool plus=false, empty=true, elligator=false, mul=false, scalar=false, div=false, torque=false, scalarempty=true, neg=false, einv=false, like_eddsa=false, like_x=false, decoeff=false, raw=false; if (done || error) return; for (int i=1; i(); if (!done) usage(); return (error<0) ? -error : error; }