static struct crypt *setup_cipher(int type, int id, int mac) { struct cipher_list *c; int klen = 20; void *key; struct crypt_sym *cs; struct crypt *ci; c = crypt_find_cipher(type, id); if (!c) errx(1, "Can't find cipher %d (type %d)", id, type); cs = crypt_new(c->c_ctr); if (mac) ci = cs->cs_mac; else ci = cs->cs_cipher; key = alloca(klen); assert(key); memset(key, 0, klen); crypt_set_key(ci, key, klen); /* XXX cs is leaked */ return ci; }
int main() { RSA *rsa, *rsa_pub; DES_cblock des_key; rsa = crypt_new( des_key, "tst-des.key", "tst-pub.pem", "tst-priv.pem"); rsa_pub = crypt_read( des_key, "tst-des.key", "tst-pub.pem"); test_crypt( rsa, rsa_pub, des_key, "0123456789", 10); remove("tst-des.key"); remove("tst-pub.pem"); remove("tst-priv.pem"); }
int main(int argc, char *argv[]) { FILE *fp = NULL; unsigned char *buf = NULL; int filesize; int mode = MODE_NEW; if (argc < 3) { fprintf(stderr, "usage: %s <input file> <output file> [-d|-e]\n", argv[0]); return EXIT_FAILURE; } if ((argc > 3) && (argv[3][0] == '-')) { switch (argv[3][1]) { case 'd': mode = MODE_DEC_OLD; break; case 'e': mode = MODE_ENC_OLD; break; } } fp = fopen(argv[1], "rb"); if (fp == NULL) { fprintf(stderr, "Error: could not open input file '%s'\n", argv[1]); return EXIT_FAILURE; } fseek(fp, 0, SEEK_END); filesize = ftell(fp); buf = (unsigned char*)malloc(filesize); if (buf == NULL) { fprintf(stderr, "Error: could not allocate %i bytes\n", filesize); fclose(fp); return EXIT_FAILURE; } fseek(fp, 0, SEEK_SET); if (fread(buf, filesize, 1, fp) != 1) { fprintf(stderr, "Error: could not read from input file\n"); fclose(fp); return EXIT_FAILURE; } fclose(fp); fp = fopen(argv[2], "wb"); if (fp == NULL) { fprintf(stderr, "Error: could not open output file '%s'\n", argv[2]); return EXIT_FAILURE; } switch (mode) { case MODE_NEW: crypt_new(buf, filesize); break; case MODE_DEC_OLD: decrypt_old(buf, filesize); break; case MODE_ENC_OLD: encrypt_old(buf, filesize); break; } fwrite(buf, filesize, 1, fp); fclose(fp); free(buf); return EXIT_SUCCESS; }