int main(int argc, char **argv) { int idx, x, z; unsigned long w; unsigned char hash_buffer[MAXBLOCKSIZE]; hash_state md; /* You need to register algorithms before using them */ register_algs(); if (argc < 2) { printf("usage: ./hash algorithm file [file ...]\n"); printf("Algorithms:\n"); for (x = 0; hash_descriptor[x].name != NULL; x++) { printf(" %s (%d)\n", hash_descriptor[x].name, hash_descriptor[x].ID); } exit(EXIT_SUCCESS); } idx = find_hash(argv[1]); if (idx == -1) { fprintf(stderr, "\nInvalid hash specified on command line.\n"); return -1; } if (argc == 2) { hash_descriptor[idx].init(&md); do { x = fread(hash_buffer, 1, sizeof(hash_buffer), stdin); hash_descriptor[idx].process(&md, hash_buffer, x); } while (x == sizeof(hash_buffer)); hash_descriptor[idx].done(&md, hash_buffer); for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) { printf("%02x",hash_buffer[x]); } printf(" (stdin)\n"); } else { for (z = 2; z < argc; z++) { w = sizeof(hash_buffer); if ((errno = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) { printf("File hash error: %s\n", error_to_string(errno)); } else { for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) { printf("%02x",hash_buffer[x]); } printf(" %s\n", argv[z]); } } } return EXIT_SUCCESS; }
static void SecKeyRegisterClass(void) { static const CFRuntimeClass kSecKeyClass = { 0, /* version */ "SecKey", /* class name */ NULL, /* init */ NULL, /* copy */ SecKeyDestroy, /* dealloc */ SecKeyEqual, /* equal */ NULL, /* hash */ NULL, /* copyFormattingDesc */ SecKeyCopyDescription /* copyDebugDesc */ }; kSecKeyTypeID = _CFRuntimeRegisterClass(&kSecKeyClass); register_algs(); }
int main(int argc, char **argv) { #ifdef LTC_PTHREAD thread_info *tinfo, *res; #endif int x, pass = 0, fail = 0, nop = 0; size_t fn_len, i, dots; char *single_test = NULL; ulong64 ts; long delta, dur, real = 0; register_algs(); printf("LTC_VERSION = %s\n%s\n\n", GIT_VERSION, crypt_build_settings); #ifdef USE_LTM ltc_mp = ltm_desc; printf("MP_PROVIDER = LibTomMath\n"); #elif defined(USE_TFM) ltc_mp = tfm_desc; printf("MP_PROVIDER = TomsFastMath\n"); #elif defined(USE_GMP) ltc_mp = gmp_desc; printf("MP_PROVIDER = GnuMP\n"); #elif defined(EXT_MATH_LIB) { extern ltc_math_descriptor EXT_MATH_LIB; ltc_mp = EXT_MATH_LIB; } #define NAME_VALUE(s) #s"="NAME(s) #define NAME(s) #s printf("MP_PROVIDER = %s\n", NAME_VALUE(EXT_MATH_LIB)); #undef NAME_VALUE #undef NAME #endif #ifdef LTC_TEST_MPI printf("MP_DIGIT_BIT = %d\n", MP_DIGIT_BIT); #else printf("NO math provider selected, all tests requiring MPI were disabled and will 'nop'\n"); #endif printf("sizeof(ltc_mp_digit) = %d\n", (int)sizeof(ltc_mp_digit)); #ifdef LTC_PTHREAD tinfo = XCALLOC(sizeof(test_functions)/sizeof(test_functions[0]), sizeof(thread_info)); if (tinfo == NULL) { printf("\n\nFAILURE: XCALLOC\n"); return EXIT_FAILURE; } #endif fn_len = 0; for (i = 0; i < sizeof(test_functions) / sizeof(test_functions[0]); ++i) { size_t len = strlen(test_functions[i].name); if (fn_len < len) fn_len = len; #ifdef LTC_PTHREAD if(test_functions[i].fn == prng_test) continue; tinfo[i].t = &test_functions[i]; x = pthread_create(&tinfo[i].thread_id, NULL, run, &tinfo[i]); if (x != 0) { printf("\n\nFAILURE: pthread_create\n"); return EXIT_FAILURE; } #endif } fn_len = fn_len + (4 - (fn_len % 4)); /* single test name from commandline */ if (argc > 1) single_test = argv[1]; dur = epoch_usec(); for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) { if (single_test && strstr(test_functions[i].name, single_test) == NULL) { continue; } dots = fn_len - strlen(test_functions[i].name); printf("\n%s", test_functions[i].name); while(dots--) printf("."); fflush(stdout); #ifdef LTC_PTHREAD if(test_functions[i].fn != prng_test) { x = pthread_join(tinfo[i].thread_id, (void**)&res); if (x != 0){ printf("\n\nFAILURE: pthread_join\n"); return EXIT_FAILURE; } x = res->err; delta = res->delta; } else { ts = epoch_usec(); x = test_functions[i].fn(); delta = (long)(epoch_usec() - ts); } #else ts = epoch_usec(); x = test_functions[i].fn(); delta = (long)(epoch_usec() - ts); #endif real += delta; if (x == CRYPT_OK) { printf("passed %10.3fms", (double)(delta)/1000); pass++; } else if (x == CRYPT_NOP) { printf("nop"); nop++; } else { printf("failed (%s) %10.3fms", error_to_string(x), (double)(delta)/1000); fail++; } } dur = epoch_usec() - dur; #ifdef LTC_PTHREAD XFREE(tinfo); #endif x = (fail > 0 || fail+pass+nop == 0) ? EXIT_FAILURE : EXIT_SUCCESS; printf("\n\n%s: passed=%d failed=%d nop=%d duration=%.1fsec real=%.1fsec\n", x ? "FAILURE" : "SUCCESS", pass, fail, nop, (double)(dur)/(1000*1000), (double)(real)/(1000*1000)); return x; }
int main(int argc, char *argv[]) { unsigned char plaintext[512],ciphertext[512]; unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE]; unsigned char inbuf[512]; /* i/o block size */ unsigned long outlen, y, ivsize, x, decrypt; symmetric_CTR ctr; int cipher_idx, hash_idx, ks; char *infile, *outfile, *cipher; prng_state prng; FILE *fdin, *fdout; /* register algs, so they can be printed */ register_algs(); if (argc < 4) { return usage(argv[0]); } if (!strcmp(argv[1], "-d")) { decrypt = 1; cipher = argv[2]; infile = argv[3]; outfile = argv[4]; } else { decrypt = 0; cipher = argv[1]; infile = argv[2]; outfile = argv[3]; } /* file handles setup */ fdin = fopen(infile,"rb"); if (fdin == NULL) { perror("Can't open input for reading"); exit(-1); } fdout = fopen(outfile,"wb"); if (fdout == NULL) { perror("Can't open output for writing"); exit(-1); } cipher_idx = find_cipher(cipher); if (cipher_idx == -1) { printf("Invalid cipher entered on command line.\n"); exit(-1); } hash_idx = find_hash("sha256"); if (hash_idx == -1) { printf("LTC_SHA256 not found...?\n"); exit(-1); } ivsize = cipher_descriptor[cipher_idx].block_length; ks = hash_descriptor[hash_idx].hashsize; if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) { printf("Invalid keysize???\n"); exit(-1); } printf("\nEnter key: "); fgets((char *)tmpkey,sizeof(tmpkey), stdin); outlen = sizeof(key); if ((errno = hash_memory(hash_idx,tmpkey,strlen((char *)tmpkey),key,&outlen)) != CRYPT_OK) { printf("Error hashing key: %s\n", error_to_string(errno)); exit(-1); } if (decrypt) { /* Need to read in IV */ if (fread(IV,1,ivsize,fdin) != ivsize) { printf("Error reading IV from input.\n"); exit(-1); } if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) { printf("ctr_start error: %s\n",error_to_string(errno)); exit(-1); } /* IV done */ do { y = fread(inbuf,1,sizeof(inbuf),fdin); if ((errno = ctr_decrypt(inbuf,plaintext,y,&ctr)) != CRYPT_OK) { printf("ctr_decrypt error: %s\n", error_to_string(errno)); exit(-1); } if (fwrite(plaintext,1,y,fdout) != y) { printf("Error writing to file.\n"); exit(-1); } } while (y == sizeof(inbuf)); fclose(fdin); fclose(fdout); } else { /* encrypt */ /* Setup yarrow for random bytes for IV */ if ((errno = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) { printf("Error setting up PRNG, %s\n", error_to_string(errno)); } /* You can use rng_get_bytes on platforms that support it */ /* x = rng_get_bytes(IV,ivsize,NULL);*/ x = yarrow_read(IV,ivsize,&prng); if (x != ivsize) { printf("Error reading PRNG for IV required.\n"); exit(-1); } if (fwrite(IV,1,ivsize,fdout) != ivsize) { printf("Error writing IV to output.\n"); exit(-1); } if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) { printf("ctr_start error: %s\n",error_to_string(errno)); exit(-1); } do { y = fread(inbuf,1,sizeof(inbuf),fdin); if ((errno = ctr_encrypt(inbuf,ciphertext,y,&ctr)) != CRYPT_OK) { printf("ctr_encrypt error: %s\n", error_to_string(errno)); exit(-1); } if (fwrite(ciphertext,1,y,fdout) != y) { printf("Error writing to output.\n"); exit(-1); } } while (y == sizeof(inbuf)); fclose(fdout); fclose(fdin); } return 0; }
int main(int argc, char *argv[]) { char *infile, *outfile, cmdl[128], *partname; FILE *fdin, *fdout; unsigned long wlen; char *result_infile = "/tmp/mtd.verify"; char result[128]; /* register algs, so they can be printed */ register_algs(); if (argc < 2) { return usage(argv[0]); } infile = argv[1]; /* input file */ partname = "linux" ; /* Partition name */ /* Open the image file. */ fdin = fopen(infile,"rb"); if (fdin == NULL) { perror("Can't open input for reading"); exit(-1); } /* Invoke mtd verify. */ sprintf(cmdl, "mtd verify - > %s", result_infile) ; fdout = popen(cmdl, "w") ; if(fdout == NULL) { perror("popen") ; exit(-1); } /* Decode the incoming file and sent it to the standard output. */ wlen = decode(fdin, fdout); if (wlen < 4200) { fprintf(stderr, "Firmware too small.\n") ; exit(-1); /* too small, cannot be real firmware */ } /* pclose */ pclose(fdout); /* fclose */ fclose(fdin); /* check result of mtd verify */ fdin = fopen(result_infile, "r"); if (fdin == NULL) { perror("Can't open the result file for reading"); exit(-1); } fgets(result, sizeof(result), fdin); fclose(fdin); if((strcmp(result, "verify:ok")) != 0) { printf("Image file verify fail\n"); exit(-1); } /* file handles setup */ fdin = fopen(infile,"rb"); if (fdin == NULL) { perror("Can't open input for reading"); exit(-1); } /* Program decrypted firmware by calling 'mtd' */ sprintf(cmdl, "mtd write - %s", partname) ; if( argc < 3) /* Only one parameter */ fdout = popen(cmdl, "w") ; else /* an extra parameter as output filename for debugging purpose */ fdout = fopen(argv[2], "w") ; if(fdout == NULL) { fprintf(stderr, "Cannot open pipe to upgrade firmware\n") ; perror("popen") ; exit(-1) ; } /* TODO: check fdout */ /* Decode. */ wlen = decode(fdin, fdout); pclose(fdout); fclose(fdin); if (wlen < 4200) { fprintf(stderr, "Firmware too small.\n") ; exit(-1); /* too small, cannot be real firmware */ } return 0; }