2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-09-26 18:11:06 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
2007-10-09 11:35:47 +00:00
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-09-26 18:11:06 +00:00
|
|
|
|
|
|
|
|
2002-06-14 05:49:34 +00:00
|
|
|
// utility program for encryption.
|
|
|
|
//
|
|
|
|
// -genkey n private_keyfile public_keyfile
|
2002-07-05 05:33:40 +00:00
|
|
|
// create a key pair with n bits (512 <= n <= 1024)
|
2002-06-14 05:49:34 +00:00
|
|
|
// write it in hex notation
|
|
|
|
// -sign file private_keyfile
|
|
|
|
// create a signature for a given file
|
|
|
|
// write it in hex notation
|
2005-12-14 23:43:50 +00:00
|
|
|
// -sign_string string private_keyfile
|
|
|
|
// create a signature for a given string
|
|
|
|
// write it in hex notation
|
2002-06-14 05:49:34 +00:00
|
|
|
// -verify file signature_file public_keyfile
|
|
|
|
// verify a signature
|
2002-07-05 05:33:40 +00:00
|
|
|
// -test_crypt private_keyfile public_keyfile
|
2002-06-14 05:49:34 +00:00
|
|
|
// test encrypt/decrypt
|
|
|
|
|
2006-02-16 04:05:38 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2006-02-16 04:05:38 +00:00
|
|
|
#endif
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2005-08-01 22:44:40 +00:00
|
|
|
#include <cstring>
|
2002-06-14 05:49:34 +00:00
|
|
|
|
|
|
|
#include "crypt.h"
|
|
|
|
|
2005-02-16 23:17:43 +00:00
|
|
|
void die(const char* p) {
|
2002-07-09 00:10:58 +00:00
|
|
|
fprintf(stderr, "Error: %s\n", p);
|
2002-06-14 05:49:34 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-08-01 22:44:40 +00:00
|
|
|
unsigned int random_int() {
|
|
|
|
unsigned int n;
|
2006-02-16 04:05:38 +00:00
|
|
|
#if defined(_WIN32)
|
2006-06-30 20:45:16 +00:00
|
|
|
#if defined(__CYGWIN32__)
|
|
|
|
HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL");
|
|
|
|
#else
|
2006-02-16 04:05:38 +00:00
|
|
|
HMODULE hLib=LoadLibrary((LPCWSTR)"ADVAPI32.DLL");
|
2006-06-30 20:45:16 +00:00
|
|
|
#endif
|
2006-02-16 04:05:38 +00:00
|
|
|
if (!hLib) {
|
|
|
|
die("Can't load ADVAPI32.DLL");
|
|
|
|
}
|
|
|
|
BOOLEAN (APIENTRY *pfn)(void*, ULONG) =
|
|
|
|
(BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
|
|
|
|
if (pfn) {
|
|
|
|
char buff[32];
|
|
|
|
ULONG ulCbBuff = sizeof(buff);
|
|
|
|
if(pfn(buff,ulCbBuff)) {
|
|
|
|
// use buff full of random goop
|
|
|
|
memcpy(&n,buff,sizeof(n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FreeLibrary(hLib);
|
|
|
|
#else
|
2005-08-01 22:44:40 +00:00
|
|
|
FILE* f = fopen("/dev/random", "r");
|
|
|
|
if (!f) {
|
2006-02-16 04:05:38 +00:00
|
|
|
die("can't open /dev/random\n");
|
2005-08-01 22:44:40 +00:00
|
|
|
}
|
|
|
|
fread(&n, sizeof(n), 1, f);
|
2006-02-16 04:05:38 +00:00
|
|
|
#endif
|
2005-08-01 22:44:40 +00:00
|
|
|
return n;
|
|
|
|
}
|
2004-10-06 21:49:21 +00:00
|
|
|
|
2002-10-04 20:26:23 +00:00
|
|
|
int main(int argc, char** argv) {
|
2002-06-14 05:49:34 +00:00
|
|
|
R_RSA_PUBLIC_KEY public_key;
|
|
|
|
R_RSA_PRIVATE_KEY private_key;
|
|
|
|
int n, retval;
|
|
|
|
bool is_valid;
|
|
|
|
DATA_BLOCK signature, in, out;
|
|
|
|
unsigned char signature_buf[256], buf[256], buf2[256];
|
|
|
|
FILE *f, *fpriv, *fpub;
|
2005-12-14 23:43:50 +00:00
|
|
|
char cbuf[256];
|
2002-06-14 05:49:34 +00:00
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("missing command\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "-genkey")) {
|
2005-08-01 22:44:40 +00:00
|
|
|
if (argc < 5) {
|
|
|
|
fprintf(stderr, "missing cmdline args\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-08-26 22:57:17 +00:00
|
|
|
printf("creating keys in %s and %s\n", argv[3], argv[4]);
|
2002-06-14 05:49:34 +00:00
|
|
|
n = atoi(argv[2]);
|
|
|
|
|
2005-08-01 22:44:40 +00:00
|
|
|
srand(random_int());
|
|
|
|
RSA* rp = RSA_generate_key(n, 65537, 0, 0);
|
|
|
|
openssl_to_keys(rp, n, private_key, public_key);
|
2002-06-14 05:49:34 +00:00
|
|
|
fpriv = fopen(argv[3], "w");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
fpub = fopen(argv[4], "w");
|
|
|
|
if (!fpub) die("fopen");
|
|
|
|
print_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
print_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
2005-08-01 22:44:40 +00:00
|
|
|
|
2002-06-14 05:49:34 +00:00
|
|
|
} else if (!strcmp(argv[1], "-sign")) {
|
|
|
|
fpriv = fopen(argv[3], "r");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
if (retval) die("scan_key_hex\n");
|
|
|
|
signature.data = signature_buf;
|
|
|
|
signature.len = 256;
|
|
|
|
retval = sign_file(argv[2], private_key, signature);
|
|
|
|
print_hex_data(stdout, signature);
|
2005-12-14 23:43:50 +00:00
|
|
|
} else if (!strcmp(argv[1], "-sign_string")) {
|
|
|
|
fpriv = fopen(argv[3], "r");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
if (retval) die("scan_key_hex\n");
|
|
|
|
generate_signature(argv[2], cbuf, private_key);
|
|
|
|
puts(cbuf);
|
2002-06-14 05:49:34 +00:00
|
|
|
} else if (!strcmp(argv[1], "-verify")) {
|
|
|
|
fpub = fopen(argv[4], "r");
|
|
|
|
if (!fpub) die("fopen");
|
|
|
|
retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
|
|
|
if (retval) die("read_public_key");
|
|
|
|
f = fopen(argv[3], "r");
|
|
|
|
signature.data = signature_buf;
|
|
|
|
signature.len = 256;
|
|
|
|
retval = scan_hex_data(f, signature);
|
|
|
|
if (retval) die("scan_hex_data");
|
|
|
|
retval = verify_file(argv[2], public_key, signature, is_valid);
|
|
|
|
if (retval) die("verify_file");
|
|
|
|
printf("file is %s\n", is_valid?"valid":"invalid");
|
|
|
|
} else if (!strcmp(argv[1], "-test_crypt")) {
|
|
|
|
fpriv = fopen(argv[2], "r");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
if (retval) die("scan_key_hex\n");
|
|
|
|
fpub = fopen(argv[3], "r");
|
|
|
|
if (!fpub) die("fopen");
|
|
|
|
retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
|
|
|
if (retval) die("read_public_key");
|
2005-02-16 23:17:43 +00:00
|
|
|
strcpy((char*)buf2, "foobar");
|
|
|
|
in.data = buf2;
|
2002-06-14 05:49:34 +00:00
|
|
|
in.len = strlen((char*)in.data);
|
|
|
|
out.data = buf;
|
2005-08-11 17:41:08 +00:00
|
|
|
encrypt_private(private_key, in, out);
|
2002-06-14 05:49:34 +00:00
|
|
|
in = out;
|
|
|
|
out.data = buf2;
|
|
|
|
decrypt_public(public_key, in, out);
|
2005-08-01 22:44:40 +00:00
|
|
|
printf("out: %s\n", out.data);
|
2002-06-14 05:49:34 +00:00
|
|
|
} else {
|
|
|
|
printf("unrecognized command\n");
|
|
|
|
}
|
2002-10-04 20:26:23 +00:00
|
|
|
|
|
|
|
return 0;
|
2002-06-14 05:49:34 +00:00
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_6633b596b9 = "$Id$";
|