📄 rsatest.c
字号:
/* $Id: rsatest.c,v 1.7 2000/04/06 07:26:53 jm Exp $ * RSA module tests * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2000, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#include <stdlib.h>#include <stdio.h>#include <time.h>#include <string.h>#include <assert.h>#include "rsa_dyn.h"#include "jmrsa.h"void file_test(int bits){ FILE *f; rsa_secret_key sk, sk2; printf("file_test(%i)\n", bits); assert(rsa_generate_key(bits, &sk) == 0); rsa_debug_print_sec(&sk); f = fopen("rsakey.dat", "wb"); assert(f != NULL); rsa_write_file_sec(f, &sk); fclose(f); rsa_debug_print_sec(&sk); f = fopen("rsakey.dat", "rb"); assert(f != NULL); rsa_read_file_sec(f, &sk2); fclose(f); rsa_debug_print_sec(&sk2);}void rsa_test(){ rsa_secret_key sec; rsa_public_key pub; mpz_t a, b, c; int i; FILE *f; printf("\nrsa_test()\n"); f = fopen("rsakey.dat", "rb"); if (f == NULL) { fprintf(stderr, "Cannot open rsakey.dat\n"); return; } if (rsa_read_file_sec(f, &sec) != 0) { fprintf(stderr, "rsa_read_file_sec() failed\n"); fclose(f); return; } fclose(f); rsa_debug_print_sec(&sec); rsa_secret_public(&sec, &pub); mpz_init(a); mpz_init(b); mpz_init(c); srand(time(NULL)); for (i = 0; i < 20; i++) { mpz_set_ui(a, rand()); mpz_out_str(stdout, 10, a); printf(" => "); rsa_encrypt(&pub, a, b); mpz_out_str(stdout, 10, b); printf(" => "); rsa_decrypt(&sec, b, c); mpz_out_str(stdout, 10, c); if (mpz_cmp(a, c) != 0) printf(" ARGH!"); printf("\n"); } mpz_clear(a); mpz_clear(b); mpz_clear(c);}void rsa_main_tests(){ const unsigned char *tmp; unsigned int len; int i, res; unsigned char sk[16]; unsigned char *encrypted; unsigned int encrypted_len; fprintf(stderr, "Making new key..\n"); if (rsa_make_key("test_key.dat", 256) != 0) { fprintf(stderr, "rsa_make_key() failed\n"); exit(1); } fprintf(stderr, "Reading the key file..\n"); if (rsa_initialize("test_key.dat") != 0) { fprintf(stderr, "rsa_initialize() failed\n"); exit(1); } fprintf(stderr, "Key:\n"); rsa_debug_print_host_key(); len = rsa_get_public_key_len(); tmp = rsa_get_public_key(); if (tmp == NULL) { printf("rsa_get_public_key() failed\n"); exit(1); } printf("\npublic key len=%i\npublic key buf: ", len); for (i = 0; i < len; i++) { printf("%02x", tmp[i]); } printf("\n"); sk[0] = 0x12; sk[1] = 0x34; sk[2] = 0x56; sk[3] = 0x78; sk[4] = 0x9a; sk[5] = 0xbc; sk[6] = 0xde; sk[7] = 0xf0; sk[8] = 0x21; sk[9] = 0x43; sk[10] = 0x65; sk[11] = 0x87; sk[12] = 0xa9; sk[13] = 0xcb; sk[14] = 0xed; sk[15] = 0x0f; printf("sk="); for (i = 0; i < 16; i++) printf("%02x", sk[i]); printf("\n"); encrypted = rsa_encrypt_session_key(sk, 16, rsa_get_public_key(), rsa_get_public_key_len(), &encrypted_len); printf("rsa_encrypt_session_key: len=%i\n", encrypted_len); if (encrypted == NULL) { printf("failed!\n"); exit(1); } memset(sk, 0, 16); res = rsa_decrypt_session_key(encrypted, encrypted_len, sk, 16); if (res < 0) { printf("rsa_decrypt_session_key() failed\n"); exit(1); } printf("sk="); for (i = 0; i < 16; i++) printf("%02x", sk[i]); printf("\n"); free(encrypted);}void buf_test(){ mpz_t a, b; unsigned char buf[1024]; int len, i; printf("buf_test()\n"); mpz_init(a); mpz_init(b); assert(rsa_random_prime(a, 128) == 0); printf("\nrsa_random_prime => "); mpz_out_str(stdout, 16, a); len = jm_mpz_to_buf(a, buf); assert(len > 0); printf("\njm_mpz_to_buf: len=%i: ", len); if (len > 0) { for (i = 0; i < len; i++) printf("%02x", buf[i]); } i = jm_buf_to_mpz(buf, len, b); printf("\njm_buf_to_mpz: used=%i: ", i); if (i > 0) { mpz_out_str(stdout, 16, b); } if (mpz_cmp(a,b) != 0) printf(" ARGH!"); printf("\n"); mpz_clear(a); mpz_clear(b);}void keybuf_test(){ rsa_public_key pub, out; unsigned char *buf; unsigned int len; int i; printf("keybuf_test()\n"); rsa_init_pub(&pub); mpz_set_str(pub.n, "123456789abcdef", 16); mpz_set_str(pub.e, "fedcba987654321", 16); rsa_debug_print_pub(&pub); buf = rsa_public_key_buffer(&pub, &len); printf("rsa_public_key_buffer: len=%i\n", len); if (len <= 0) { printf("Failed!\n"); exit(1); } printf("buf: "); for (i = 0; i < len; i++) printf("%02x", buf[i]); printf("\n"); rsa_buffer_public_key(buf, len, &out); printf("rsa_buffer_public_key:\n"); rsa_debug_print_pub(&out); rsa_clear_pub(&pub); rsa_clear_pub(&out); free(buf);}int main(int argc, char *argv[]){ if (argc == 2) { if (strcmp(argv[1], "-test") == 0) rsa_main_tests(); else if (strcmp(argv[1], "-buf") == 0) buf_test(); else if (strcmp(argv[1], "-keybuf") == 0) keybuf_test(); else if (strcmp(argv[1], "-read") == 0) rsa_test(); else { int bits = atoi(argv[1]); if (bits == 0) fprintf(stderr, "Unknown test command %s\n", argv[1]); else if (bits < 128) fprintf(stderr, "Too few (< 128) bits for key\n"); else file_test(bits); } } else { fprintf(stderr, "rsatest <command>\n" "command:\n" " <bits> = generate a RSA key of given number of " "bits and write it to\n a key file\n" " -read = read the key file and do some encryption/" "decryption tests\n" " -buf = test mpz <-> char buf code\n" " -keybuf = test RSA public key <-> char buf code\n" " -test = interface routine tests\n" "\n"); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -