3 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version. The Blender
9 * Foundation also sells licenses for use in proprietary software under
10 * the Blender License. See http://www.blender.org/BL/ for information
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 * Copyright (C) 2001 NaN Technologies B.V.
37 * Blender Key Read-tester
48 #include "key_pyc.h" /* the Python byte code */
49 #include "blenkey.h" /* the external interface */
50 #include "key_internal.h"
52 #define TESTReadKeyFile 1
54 int Check_All_Byte_Calculus_Data(char *KeyBytePtr) {
57 /* create some unique number arrays */
58 int NoRealRandomArray[MAXBYTEDATABLOCK];
60 typedef byte (*funcpoin)(byte, byte);
61 funcpoin checkfunc[] = {&checkfunc0, &checkfunc1, &checkfunc2,
62 &checkfunc3, &checkfunc4};
64 byte *KeyByteData = DeHexify(KeyBytePtr);
66 /* first create a fixed seed random number generator */
67 sgenrand(666); /* seed it fixed */
69 /* initialize arrays with unique numbers */
70 for (i=0; i<MAXBYTEDATABLOCK; i++) {
71 NoRealRandomArray[i] = i;
73 /* then stir the unique number lists */
74 for (i=0; i<MAXBYTEDATABLOCK; i++) {
75 unsigned long randswap = genrand();
76 int swap1 = (int) (randswap % MAXBYTEDATABLOCK);
77 int swap2 = (int) ((randswap>>16) % MAXBYTEDATABLOCK);
78 int store = NoRealRandomArray[swap1];
79 /* printf("%lu %d %d\n", randswap, swap1, swap2); */
80 NoRealRandomArray[swap1] = NoRealRandomArray[swap2];
81 NoRealRandomArray[swap2] = store;
84 printf("\nFixed seed unique number random data: ");
85 for (i=0; i<MAXBYTEDATABLOCK; i++) {
86 printf("%d ", NoRealRandomArray[i]);
91 /* check our byte calculus functions on the random data */
92 for (i=0; i<(MAXBYTEDATABLOCK-3); i+=3) {
94 char *Pcheckfunc[] = {"max", " - ", " + ", " * ", " / "};
95 printf("[%3d]=[%3d]%s[%3d] ",
96 NoRealRandomArray[i], NoRealRandomArray[i+1],
97 Pcheckfunc[NoRealRandomArray[i]%5], NoRealRandomArray[i+2]);
98 printf("%3d%s%3d: %3d == %3d\n",
99 KeyByteData[NoRealRandomArray[i+1]],
100 Pcheckfunc[NoRealRandomArray[i]%5],
101 KeyByteData[NoRealRandomArray[i+2]],
102 KeyByteData[NoRealRandomArray[i]],
103 checkfunc[(NoRealRandomArray[i]%5)](
104 KeyByteData[NoRealRandomArray[i+1]],
105 KeyByteData[NoRealRandomArray[i+2]]));
107 if (KeyByteData[NoRealRandomArray[i]] !=
108 checkfunc[(NoRealRandomArray[i]%5)](
109 KeyByteData[NoRealRandomArray[i+1]],
110 KeyByteData[NoRealRandomArray[i+2]])) {
111 printf("\nByte Checksum failed !\n");
118 void pub_priv_test(char *HexPriv, char *HexPub)
121 /* static unsigned char rsa_e[] = "\x11"; */
122 static unsigned char rsa_e[] = "\x01\x00\x01";
125 byte *pubKey, *privKey;
126 int pubKeyLen, privKeyLen;
128 unsigned char *deCryptKey;
129 int cryptKeyLen = 16;
132 strcpy(cryptKey, "abcdefghijklmno");
134 pubKey = DeHexify(HexPub);
135 pubKeyLen = strlen(HexPub) / 2;
137 privKey = DeHexify(HexPriv);
138 privKeyLen = strlen(HexPriv) / 2;
142 fprintf(stderr, "Error in RSA_new\n");
145 rsa->e = BN_bin2bn(rsa_e, sizeof(rsa_e)-1, rsa->e);
146 rsa->n = BN_bin2bn(pubKey, pubKeyLen, rsa->n);
147 rsa->d = BN_bin2bn(privKey, privKeyLen, rsa->d);
149 fprintf(stderr, "NOTE e %d, n %d, d %d rsa_size %d\n",
150 sizeof(rsa_e)-1, pubKeyLen, privKeyLen, RSA_size(rsa));
152 cryptedKey = malloc(RSA_size(rsa) * sizeof(byte));
153 cryptedKeyLen = RSA_private_encrypt(cryptKeyLen, cryptKey,
154 cryptedKey, rsa, RSA_PKCS1_PADDING);
156 deCryptKey = malloc(RSA_size(rsa) * sizeof(unsigned char));
157 deCryptKeyLen = RSA_public_decrypt(cryptedKeyLen, cryptedKey,
158 deCryptKey, rsa, RSA_PKCS1_PADDING);
159 if (deCryptKeyLen == -1) {
160 printf("Error in RSA_public_decrypt: %s\n",
161 ERR_error_string(ERR_get_error(), NULL));
164 printf("RSA_public_decrypt test SUCCEEDED\n");
169 #ifdef TESTReadKeyFile
170 int main(int argc, char **argv) {
173 char *HexPriv = NULL, *HexPub = NULL, *HexPython = NULL;
175 byte *PythonData = NULL;
177 char *HexByte = NULL;
180 printf("usage: %s keyfile\n", argv[0]);
184 result = ReadKeyFile(argv[1], &User, &HexPriv, &HexPub, &Byte, &HexPython);
186 printf("\nReadKeyFile error %d\n", result);
189 printf("\nReadKeyFile OK\n");
192 /* just print the rsaPrivString and rsaPubString */
193 if (DEBUG) printf("\nrsaPrivString: %s\n", HexPriv);
194 if (DEBUG) printf("\nrsaPubString: %s\n", HexPub);
196 /* try to private encrypt-public decrypt something */
197 if (DEBUG) pub_priv_test(HexPriv, HexPub);
199 /* check all the Byte checksums
200 rehexify it for our Check_All_Byte_Calculus_Data function ... */
201 HexByte = Hexify(Byte, 1000);
202 if (Check_All_Byte_Calculus_Data(HexByte) != 0) {
203 printf("\nByte_Calculus_Data checksums do not match !\n");
206 if (DEBUG) printf("\nThe Byte Calculus Data checksums match\n");
209 /* Check the KeyPythonPtr */
210 PythonLength = strlen(HexPython)/2;
211 PythonData = DeHexify(HexPython);
212 if (memcmp(PythonData, g_keycode, PythonLength) != 0) {
213 printf("\nPython Byte code datablocks do not match !\n");
216 if (DEBUG) printf("\nThe Python Byte code datablock matches\n");
222 int main(int argc, char **argv) {
225 char *HexCryptedData, *HexCryptedKey;
226 int newlinetracker = 0; /* line position, counts from 0-71 */
240 printf("usage: %s keyfile\n", argv[0]);
244 /* open keyfile for reading */
245 if ((rawkeyfile = fopen(argv[1], "r")) == NULL) {
246 printf("error, cannot read %s\n", argv[1]);
250 /* Scan and interpret the ASCII part */
251 AsciiHash = scan_ascii(rawkeyfile, &User);
252 if (DEBUG) printf("\nHexHash: %s\n", AsciiHash);
254 /* Read the HexCryptedData */
255 HexCryptedData = ReadHexCryptedData(rawkeyfile, &newlinetracker);
256 if (DEBUG) printf("\nHexCryptedData: %s\n", HexCryptedData);
258 /* Read the HexCryptedKey */
259 HexCryptedKey = ReadHexCryptedKey(rawkeyfile, &newlinetracker);
260 if (DEBUG) printf("\nHexCryptedKey: %s\n", HexCryptedKey);
265 /* Decrypt HexCryptedKey */
266 CryptKey = RSADecryptKey(HexCryptedKey);
268 /* Decrypt HexCryptedData */
269 KeyDataString = DeCryptDatablock(CryptKey, 16, HexCryptedData);
271 free(HexCryptedData);
273 if (DEBUG) printf("\nKeyDataString: %s\n", KeyDataString);
275 /* Extract data from KeyDataString */
276 KeyDataPtr = KeyDataString;
278 mdhex = get_from_datablock(&KeyDataPtr, "01");
279 rsaPrivString = get_from_datablock(&KeyDataPtr, "02");
280 rsaPubString = get_from_datablock(&KeyDataPtr, "03");
281 KeyBytePtr = get_from_datablock(&KeyDataPtr, "04");
282 KeyPythonPtr = get_from_datablock(&KeyDataPtr, "05");
285 /* Check ascii hash */
286 if (strcmp(mdhex, AsciiHash) != 0) {
287 printf("Ascii part checksums do not match !\n");
288 printf("found: %s\n", mdhex);
289 printf("check: %s\n", AsciiHash);
292 if (DEBUG) printf("\nThe ascii part checksum matches\n");
295 /* just print the rsaPrivString and rsaPubString */
296 if (DEBUG) printf("\nrsaPrivString: %s\n", rsaPrivString);
297 if (DEBUG) printf("\nrsaPubString: %s\n", rsaPubString);
299 /* check all the Byte checksums */
300 if (Check_All_Byte_Calculus_Data(KeyBytePtr) != 0) {
301 printf("Byte_Calculus_Data checksums do not match !\n");
304 if (DEBUG) printf("\nThe Byte Calculus Data checksums match\n");
307 /* Check the KeyPythonPtr */
308 PythonLength = strlen(KeyPythonPtr)/2;
309 PythonData = DeHexify(KeyPythonPtr);
310 if (memcmp(PythonData, g_keycode, PythonLength) != 0) {
311 printf("Python Byte code datablocks do not match !\n");
314 if (DEBUG) printf("\nThe Python Byte code datablock matches\n");