Пример #1
0
int main(int argc, char **argv)
{
    unsigned char *argbuf;
    size_t argbuflen;
    gcry_mpi_t our_x, our_y, their_y;
    unsigned char *pubbuf;
    size_t publen;
    unsigned char sessionid[20], sendenc[16], rcvenc[16];
    unsigned char sendmac[20], rcvmac[20];
    int is_high;

    if (argc != 3) {
	usage(argv[0]);
    }

    argv_to_buf(&argbuf, &argbuflen, argv[1]);
    /* Private keys are only 320 bits long, so check for that to make
     * sure they didn't get the args the wrong way around */
    if (!argbuf || argbuflen > 40) usage(argv[0]);
    gcry_mpi_scan(&our_x, GCRYMPI_FMT_USG, argbuf, argbuflen, NULL);
    free(argbuf);
    argv_to_buf(&argbuf, &argbuflen, argv[2]);
    if (!argbuf) usage(argv[0]);
    gcry_mpi_scan(&their_y, GCRYMPI_FMT_USG, argbuf, argbuflen, NULL);
    free(argbuf);

    sesskeys_gen(sessionid, sendenc, rcvenc, &is_high, &our_y, our_x, their_y);
    sesskeys_make_mac(sendmac, sendenc);
    sesskeys_make_mac(rcvmac, rcvenc);

    /* Print our public key into a buffer */
    gcry_mpi_print(GCRYMPI_FMT_USG, NULL, 0, &publen, our_y);
    pubbuf = malloc(publen);
    if (!pubbuf) {
	fprintf(stderr, "Out of memory!\n");
	exit(1);
    }
    gcry_mpi_print(GCRYMPI_FMT_USG, pubbuf, publen, NULL, our_y);

    puts("");
    printf("We are the %s end of this key exchange.\n",
	    is_high ? "high" : "low");
    puts("");
    dump_data(stdout, "Our public key", pubbuf, publen);
    puts("");
    dump_data(stdout, "Session id", sessionid, 20);
    puts("");
    dump_data(stdout, "Sending   AES key", sendenc, 16);
    dump_data(stdout, "Sending   MAC key", sendmac, 20);
    dump_data(stdout, "Receiving AES key", rcvenc, 16);
    dump_data(stdout, "Receiving MAC key", rcvmac, 20);
    puts("");
    fflush(stdout);

    return 0;
}
Пример #2
0
int main(int argc, char **argv)
{
    unsigned char *argbuf;
    size_t argbuflen;
    unsigned char mackey[20];

    if (argc != 2) {
	usage(argv[0]);
    }

    argv_to_buf(&argbuf, &argbuflen, argv[1]);
    /* AES keys are 128 bits long, so check for that */
    if (!argbuf) {
	usage(argv[0]);
    }
    
    if (argbuflen != 16) {
	fprintf(stderr, "The AES key must be 32 hex chars long.\n");
	usage(argv[0]);
    }

    sesskeys_make_mac(mackey, argbuf);

    dump_data(stdout, "AES key", argbuf, 16);
    dump_data(stdout, "MAC key", mackey, 20);

    free(argbuf);
    fflush(stdout);
    return 0;
}
Пример #3
0
int main(int argc, char **argv)
{
    unsigned char *mackey;
    size_t mackeylen;
    unsigned char macval[20];
    char *otrmsg = NULL;
    DataMsg datamsg;
    size_t textlen;
    unsigned int offset;
    const unsigned char *old_text, *new_text;
    char *newdatamsg;
    size_t i;

    if (argc != 5) {
	usage(argv[0]);
    }

    argv_to_buf(&mackey, &mackeylen, argv[1]);
    if (!mackey) {
	usage(argv[0]);
    }

    if (mackeylen != 20) {
	fprintf(stderr, "The MAC key must be 40 hex chars long.\n");
	usage(argv[0]);
    }

    textlen = strlen(argv[2]);
    if (textlen != strlen(argv[3])) {
	fprintf(stderr, "The old_text and new_text must be of the same "
		"length.\n");
	usage(argv[0]);
    }
    old_text = (const unsigned char *)argv[2];
    new_text = (const unsigned char *)argv[3];

    if (sscanf(argv[4], "%u", &offset) != 1) {
	fprintf(stderr, "Unparseable offset given.\n");
	usage(argv[0]);
    }

    otrmsg = readotr(stdin);
    if (otrmsg == NULL) {
	fprintf(stderr, "No OTR Data Message found on stdin.\n");
	exit(1);
    }

    if (otrl_proto_message_type(otrmsg) != OTRL_MSGTYPE_DATA) {
	fprintf(stderr, "OTR Non-Data Message found on stdin.\n");
	exit(1);
    }

    datamsg = parse_datamsg(otrmsg);
    free(otrmsg);
    if (datamsg == NULL) {
	fprintf(stderr, "Invalid OTR Data Message found on stdin.\n");
	exit(1);
    }

    /* Check the MAC */
    sha1hmac(macval, mackey, datamsg->macstart,
	    datamsg->macend - datamsg->macstart);
    if (memcmp(macval, datamsg->mac, 20)) {
	fprintf(stderr, "MAC does not verify: wrong MAC key?\n");
	exit(1);
    }

    /* Modify the ciphertext */
    for(i=0; i<textlen && offset+i < datamsg->encmsglen; ++i) {
	datamsg->encmsg[offset+i] ^= (old_text[i] ^ new_text[i]);
    }

    /* Recalculate the MAC */
    newdatamsg = remac_datamsg(datamsg, mackey);
    printf("%s\n", newdatamsg);
    free(newdatamsg);

    free_datamsg(datamsg);
    free(mackey);
    fflush(stdout);
    return 0;
}