1 #ifndef POLARSSL_DES_H 2 #define POLARSSL_DES_H 3 4 #define DES_ENCRYPT 1 5 #define DES_DECRYPT 0 6 7 #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00 8 9 /** 10 * rief DES context structure 11 */ 12 typedef struct 13 { 14 int mode; /*!< encrypt/decrypt */ 15 unsigned long sk[32]; /*!< DES subkeys */ 16 } 17 des_context; 18 19 /** 20 * rief Triple-DES context structure 21 */ 22 typedef struct 23 { 24 int mode; /*!< encrypt/decrypt */ 25 unsigned long sk[96]; /*!< 3DES subkeys */ 26 } 27 des3_context; 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 /** 34 * rief DES key schedule (56-bit, encryption) 35 * 36 * param ctx DES context to be initialized 37 * param key 8-byte secret key 38 */ 39 void des_setkey_enc( des_context *ctx, const unsigned char key[8] ); 40 41 /** 42 * rief DES key schedule (56-bit, decryption) 43 * 44 * param ctx DES context to be initialized 45 * param key 8-byte secret key 46 */ 47 void des_setkey_dec( des_context *ctx, const unsigned char key[8] ); 48 49 /** 50 * rief Triple-DES key schedule (112-bit, encryption) 51 * 52 * param ctx 3DES context to be initialized 53 * param key 16-byte secret key 54 */ 55 void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] ); 56 57 /** 58 * rief Triple-DES key schedule (112-bit, decryption) 59 * 60 * param ctx 3DES context to be initialized 61 * param key 16-byte secret key 62 */ 63 void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] ); 64 65 /** 66 * rief Triple-DES key schedule (168-bit, encryption) 67 * 68 * param ctx 3DES context to be initialized 69 * param key 24-byte secret key 70 */ 71 void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] ); 72 73 /** 74 * rief Triple-DES key schedule (168-bit, decryption) 75 * 76 * param ctx 3DES context to be initialized 77 * param key 24-byte secret key 78 */ 79 void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] ); 80 81 /** 82 * rief DES-ECB block encryption/decryption 83 * 84 * param ctx DES context 85 * param input 64-bit input block 86 * param output 64-bit output block 87 * 88 * eturn 0 if successful 89 */ 90 int des_crypt_ecb( des_context *ctx, 91 const unsigned char input[8], 92 unsigned char output[8] ); 93 94 /** 95 * rief DES-CBC buffer encryption/decryption 96 * 97 * param ctx DES context 98 * param mode DES_ENCRYPT or DES_DECRYPT 99 * param length length of the input data 100 * param iv initialization vector (updated after use) 101 * param input buffer holding the input data 102 * param output buffer holding the output data 103 */ 104 int des_crypt_cbc( des_context *ctx, 105 int mode, 106 int length, 107 unsigned char iv[8], 108 const unsigned char *input, 109 unsigned char *output ); 110 111 /** 112 * rief 3DES-ECB block encryption/decryption 113 * 114 * param ctx 3DES context 115 * param input 64-bit input block 116 * param output 64-bit output block 117 * 118 * eturn 0 if successful 119 */ 120 int des3_crypt_ecb( des3_context *ctx, 121 const unsigned char input[8], 122 unsigned char output[8] ); 123 124 /** 125 * rief 3DES-CBC buffer encryption/decryption 126 * 127 * param ctx 3DES context 128 * param mode DES_ENCRYPT or DES_DECRYPT 129 * param length length of the input data 130 * param iv initialization vector (updated after use) 131 * param input buffer holding the input data 132 * param output buffer holding the output data 133 * 134 * eturn 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH 135 */ 136 int des3_crypt_cbc( des3_context *ctx, 137 int mode, 138 int length, 139 unsigned char iv[8], 140 const unsigned char *input, 141 unsigned char *output ); 142 143 /* 144 * rief Checkup routine 145 * 146 * eturn 0 if successful, or 1 if the test failed 147 */ 148 int des_self_test( int verbose ); 149 150 #ifdef __cplusplus 151 } 152 #endif 153 154 #endif /* des.h */ 155 #ifndef POLARSSL_CONFIG_H 156 #define POLARSSL_CONFIG_H 157 158 #ifndef _CRT_SECURE_NO_DEPRECATE 159 #define _CRT_SECURE_NO_DEPRECATE 1 160 #endif 161 162 /* 163 * Uncomment if native integers are 8-bit wide. 164 * 165 #define POLARSSL_HAVE_INT8 166 */ 167 168 /* 169 * Uncomment if native integers are 16-bit wide. 170 * 171 #define POLARSSL_HAVE_INT16 172 */ 173 174 /* 175 * Uncomment if the compiler supports long long. 176 * 177 #define POLARSSL_HAVE_LONGLONG 178 */ 179 180 /* 181 * Uncomment to enable the use of assembly code. 182 * 183 * Requires support for asm() in compiler. 184 * 185 * Used in: 186 * library/timing.c 187 * library/padlock.c 188 * include/polarssl/bn_mul.h 189 * 190 */ 191 #define POLARSSL_HAVE_ASM 192 193 /* 194 * Uncomment if the CPU supports SSE2 (IA-32 specific). 195 * 196 #define POLARSSL_HAVE_SSE2 197 */ 198 199 /* 200 * Enable all SSL/TLS debugging messages. 201 */ 202 #define POLARSSL_DEBUG_MSG 203 204 /* 205 * Enable the checkup functions (*_self_test). 206 */ 207 #define POLARSSL_SELF_TEST 208 209 /* 210 * Enable run-time version information functions 211 */ 212 #define POLARSSL_VERSION_C 213 214 /* 215 * Enable the prime-number generation code. 216 */ 217 #define POLARSSL_GENPRIME 218 219 /* 220 * Uncomment this macro to store the AES tables in ROM. 221 * 222 #define POLARSSL_AES_ROM_TABLES 223 */ 224 225 /* 226 * Module: library/aes.c 227 * Caller: library/ssl_tls.c 228 * 229 * This module enables the following ciphersuites: 230 * SSL_RSA_AES_128_SHA 231 * SSL_RSA_AES_256_SHA 232 * SSL_EDH_RSA_AES_256_SHA 233 */ 234 #define POLARSSL_AES_C 235 236 /* 237 * Module: library/arc4.c 238 * Caller: library/ssl_tls.c 239 * 240 * This module enables the following ciphersuites: 241 * SSL_RSA_RC4_128_MD5 242 * SSL_RSA_RC4_128_SHA 243 */ 244 #define POLARSSL_ARC4_C 245 246 /* 247 * Module: library/base64.c 248 * Caller: library/x509parse.c 249 * 250 * This module is required for X.509 support. 251 */ 252 #define POLARSSL_BASE64_C 253 254 /* 255 * Module: library/bignum.c 256 * Caller: library/dhm.c 257 * library/rsa.c 258 * library/ssl_tls.c 259 * library/x509parse.c 260 * 261 * This module is required for RSA and DHM support. 262 */ 263 #define POLARSSL_BIGNUM_C 264 265 /* 266 * Module: library/camellia.c 267 * Caller: library/ssl_tls.c 268 * 269 * This module enabled the following cipher suites: 270 * SSL_RSA_CAMELLIA_128_SHA 271 * SSL_RSA_CAMELLIA_256_SHA 272 * SSL_EDH_RSA_CAMELLIA_256_SHA 273 */ 274 #define POLARSSL_CAMELLIA_C 275 276 /* 277 * Module: library/certs.c 278 * Caller: 279 * 280 * This module is used for testing (ssl_client/server). 281 */ 282 #define POLARSSL_CERTS_C 283 284 /* 285 * Module: library/debug.c 286 * Caller: library/ssl_cli.c 287 * library/ssl_srv.c 288 * library/ssl_tls.c 289 * 290 * This module provides debugging functions. 291 */ 292 #define POLARSSL_DEBUG_C 293 294 /* 295 * Module: library/des.c 296 * Caller: library/ssl_tls.c 297 * 298 * This module enables the following ciphersuites: 299 * SSL_RSA_DES_168_SHA 300 * SSL_EDH_RSA_DES_168_SHA 301 */ 302 #define POLARSSL_DES_C 303 304 /* 305 * Module: library/dhm.c 306 * Caller: library/ssl_cli.c 307 * library/ssl_srv.c 308 * 309 * This module enables the following ciphersuites: 310 * SSL_EDH_RSA_DES_168_SHA 311 * SSL_EDH_RSA_AES_256_SHA 312 * SSL_EDH_RSA_CAMELLIA_256_SHA 313 */ 314 #define POLARSSL_DHM_C 315 316 /* 317 * Module: library/havege.c 318 * Caller: 319 * 320 * This module enables the HAVEGE random number generator. 321 */ 322 #define POLARSSL_HAVEGE_C 323 324 /* 325 * Module: library/md2.c 326 * Caller: library/x509parse.c 327 * 328 * Uncomment to enable support for (rare) MD2-signed X.509 certs. 329 * 330 #define POLARSSL_MD2_C 331 */ 332 333 /* 334 * Module: library/md4.c 335 * Caller: library/x509parse.c 336 * 337 * Uncomment to enable support for (rare) MD4-signed X.509 certs. 338 * 339 #define POLARSSL_MD4_C 340 */ 341 342 /* 343 * Module: library/md5.c 344 * Caller: library/ssl_tls.c 345 * library/x509parse.c 346 * 347 * This module is required for SSL/TLS and X.509. 348 */ 349 #define POLARSSL_MD5_C 350 351 /* 352 * Module: library/net.c 353 * Caller: 354 * 355 * This module provides TCP/IP networking routines. 356 */ 357 #define POLARSSL_NET_C 358 359 /* 360 * Module: library/padlock.c 361 * Caller: library/aes.c 362 * 363 * This modules adds support for the VIA PadLock on x86. 364 */ 365 #define POLARSSL_PADLOCK_C 366 367 /* 368 * Module: library/rsa.c 369 * Caller: library/ssl_cli.c 370 * library/ssl_srv.c 371 * library/ssl_tls.c 372 * library/x509.c 373 * 374 * This module is required for SSL/TLS and MD5-signed certificates. 375 */ 376 #define POLARSSL_RSA_C 377 378 /* 379 * Module: library/sha1.c 380 * Caller: library/ssl_cli.c 381 * library/ssl_srv.c 382 * library/ssl_tls.c 383 * library/x509parse.c 384 * 385 * This module is required for SSL/TLS and SHA1-signed certificates. 386 */ 387 #define POLARSSL_SHA1_C 388 389 /* 390 * Module: library/sha2.c 391 * Caller: 392 * 393 * This module adds support for SHA-224 and SHA-256. 394 */ 395 #define POLARSSL_SHA2_C 396 397 /* 398 * Module: library/sha4.c 399 * Caller: 400 * 401 * This module adds support for SHA-384 and SHA-512. 402 */ 403 #define POLARSSL_SHA4_C 404 405 /* 406 * Module: library/ssl_cli.c 407 * Caller: 408 * 409 * This module is required for SSL/TLS client support. 410 */ 411 #define POLARSSL_SSL_CLI_C 412 413 /* 414 * Module: library/ssl_srv.c 415 * Caller: 416 * 417 * This module is required for SSL/TLS server support. 418 */ 419 #define POLARSSL_SSL_SRV_C 420 421 /* 422 * Module: library/ssl_tls.c 423 * Caller: library/ssl_cli.c 424 * library/ssl_srv.c 425 * 426 * This module is required for SSL/TLS. 427 */ 428 #define POLARSSL_SSL_TLS_C 429 430 /* 431 * Module: library/timing.c 432 * Caller: library/havege.c 433 * 434 * This module is used by the HAVEGE random number generator. 435 */ 436 #define POLARSSL_TIMING_C 437 438 /* 439 * Module: library/x509parse.c 440 * Caller: library/ssl_cli.c 441 * library/ssl_srv.c 442 * library/ssl_tls.c 443 * 444 * This module is required for X.509 certificate parsing. 445 */ 446 #define POLARSSL_X509_PARSE_C 447 448 /* 449 * Module: library/x509_write.c 450 * Caller: 451 * 452 * This module is required for X.509 certificate writing. 453 */ 454 #define POLARSSL_X509_WRITE_C 455 456 /* 457 * Module: library/xtea.c 458 * Caller: 459 */ 460 #define POLARSSL_XTEA_C 461 462 #endif /* config.h */ 463 464 465 466 467 #ifndef POLARSSL_TIMING_H 468 #define POLARSSL_TIMING_H 469 470 471 struct hr_time 472 { 473 unsigned char opaque[32]; 474 }; 475 476 #ifdef __cplusplus 477 extern "C" { 478 #endif 479 480 extern int alarmed; 481 unsigned long hardclock( void ); 482 483 484 unsigned long get_timer( struct hr_time *val, int reset ); 485 486 487 void set_alarm( int seconds ); 488 489 490 void m_sleep( int milliseconds ); 491 492 #ifdef __cplusplus 493 } 494 #endif 495 496 #endif 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 #define BUFSIZE 32768 512 513 514 515 #if defined(POLARSSL_TIMING_C) 516 517 518 519 #if defined(_WIN32) 520 521 #include <windows.h> 522 #include <winbase.h> 523 524 struct _hr_time 525 { 526 LARGE_INTEGER start; 527 }; 528 529 #else 530 531 #include <unistd.h> 532 #include <sys/types.h> 533 #include <sys/time.h> 534 #include <signal.h> 535 #include <time.h> 536 537 struct _hr_time 538 { 539 struct timeval start; 540 }; 541 542 #endif 543 544 #if defined(POLARSSL_HAVE_ASM) && 545 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) 546 547 unsigned long hardclock( void ) 548 { 549 unsigned long tsc; 550 __asm rdtsc 551 __asm mov [tsc], eax 552 return( tsc ); 553 } 554 555 #else 556 #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) 557 558 unsigned long hardclock( void ) 559 { 560 unsigned long tsc; 561 asm( "rdtsc" : "=a" (tsc) ); 562 return( tsc ); 563 } 564 565 #else 566 #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && 567 (defined(__amd64__) || defined(__x86_64__)) 568 569 unsigned long hardclock( void ) 570 { 571 unsigned long lo, hi; 572 asm( "rdtsc" : "=a" (lo), "=d" (hi) ); 573 return( lo | (hi << 32) ); 574 } 575 576 #else 577 #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && 578 (defined(__powerpc__) || defined(__ppc__)) 579 580 unsigned long hardclock( void ) 581 { 582 unsigned long tbl, tbu0, tbu1; 583 584 do 585 { 586 asm( "mftbu %0" : "=r" (tbu0) ); 587 asm( "mftb %0" : "=r" (tbl ) ); 588 asm( "mftbu %0" : "=r" (tbu1) ); 589 } 590 while( tbu0 != tbu1 ); 591 592 return( tbl ); 593 } 594 595 #else 596 #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__sparc__) 597 598 unsigned long hardclock( void ) 599 { 600 unsigned long tick; 601 asm( ".byte 0x83, 0x41, 0x00, 0x00" ); 602 asm( "mov %%g1, %0" : "=r" (tick) ); 603 return( tick ); 604 } 605 606 #else 607 #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__alpha__) 608 609 unsigned long hardclock( void ) 610 { 611 unsigned long cc; 612 asm( "rpcc %0" : "=r" (cc) ); 613 return( cc & 0xFFFFFFFF ); 614 } 615 616 #else 617 #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__ia64__) 618 619 unsigned long hardclock( void ) 620 { 621 unsigned long itc; 622 asm( "mov %0 = ar.itc" : "=r" (itc) ); 623 return( itc ); 624 } 625 626 #else 627 628 static int hardclock_init = 0; 629 static struct timeval tv_init; 630 631 unsigned long hardclock( void ) 632 { 633 struct timeval tv_cur; 634 635 if( hardclock_init == 0 ) 636 { 637 gettimeofday( &tv_init, NULL ); 638 hardclock_init = 1; 639 } 640 641 gettimeofday( &tv_cur, NULL ); 642 return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000 643 + ( tv_cur.tv_usec - tv_init.tv_usec ) ); 644 } 645 646 #endif /* generic */ 647 #endif /* IA-64 */ 648 #endif /* Alpha */ 649 #endif /* SPARC8 */ 650 #endif /* PowerPC */ 651 #endif /* AMD64 */ 652 #endif /* i586+ */ 653 654 int alarmed = 0; 655 656 #if defined(_WIN32) 657 658 unsigned long get_timer( struct hr_time *val, int reset ) 659 { 660 unsigned long delta; 661 LARGE_INTEGER offset, hfreq; 662 struct _hr_time *t = (struct _hr_time *) val; 663 664 QueryPerformanceCounter( &offset ); 665 QueryPerformanceFrequency( &hfreq ); 666 667 delta = (unsigned long)( ( 1000 * 668 ( offset.QuadPart - t->start.QuadPart ) ) / 669 hfreq.QuadPart ); 670 671 if( reset ) 672 QueryPerformanceCounter( &t->start ); 673 674 return( delta ); 675 } 676 677 DWORD WINAPI TimerProc( LPVOID uElapse ) 678 { 679 Sleep( (DWORD) uElapse ); 680 alarmed = 1; 681 return( TRUE ); 682 } 683 684 void set_alarm( int seconds ) 685 { 686 DWORD ThreadId; 687 688 alarmed = 0; 689 CloseHandle( CreateThread( NULL, 0, TimerProc, 690 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) ); 691 } 692 693 void m_sleep( int milliseconds ) 694 { 695 Sleep( milliseconds ); 696 } 697 698 #else 699 700 unsigned long get_timer( struct hr_time *val, int reset ) 701 { 702 unsigned long delta; 703 struct timeval offset; 704 struct _hr_time *t = (struct _hr_time *) val; 705 706 gettimeofday( &offset, NULL ); 707 708 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000 709 + ( offset.tv_usec - t->start.tv_usec ) / 1000; 710 711 if( reset ) 712 { 713 t->start.tv_sec = offset.tv_sec; 714 t->start.tv_usec = offset.tv_usec; 715 } 716 717 return( delta ); 718 } 719 720 static void sighandler( int signum ) 721 { 722 alarmed = 1; 723 signal( signum, sighandler ); 724 } 725 726 void set_alarm( int seconds ) 727 { 728 alarmed = 0; 729 signal( SIGALRM, sighandler ); 730 alarm( seconds ); 731 } 732 733 void m_sleep( int milliseconds ) 734 { 735 struct timeval tv; 736 737 tv.tv_sec = milliseconds / 1000; 738 tv.tv_usec = milliseconds * 1000; 739 740 select( 0, NULL, NULL, NULL, &tv ); 741 } 742 743 #endif 744 745 #endif 746 747 748 749 750 751 #if defined(POLARSSL_DES_C) 752 753 754 755 #include <string.h> 756 757 /* 758 * 32-bit integer manipulation macros (big endian) 759 */ 760 #ifndef GET_ULONG_BE 761 #define GET_ULONG_BE(n,b,i) 762 { 763 (n) = ( (unsigned long) (b)[(i) ] << 24 ) 764 | ( (unsigned long) (b)[(i) + 1] << 16 ) 765 | ( (unsigned long) (b)[(i) + 2] << 8 ) 766 | ( (unsigned long) (b)[(i) + 3] ); 767 } 768 #endif 769 770 #ifndef PUT_ULONG_BE 771 #define PUT_ULONG_BE(n,b,i) 772 { 773 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); 774 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); 775 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); 776 (b)[(i) + 3] = (unsigned char) ( (n) ); 777 } 778 #endif 779 780 /* 781 * Expanded DES S-boxes 782 */ 783 static const unsigned long SB1[64] = 784 { 785 0x01010400, 0x00000000, 0x00010000, 0x01010404, 786 0x01010004, 0x00010404, 0x00000004, 0x00010000, 787 0x00000400, 0x01010400, 0x01010404, 0x00000400, 788 0x01000404, 0x01010004, 0x01000000, 0x00000004, 789 0x00000404, 0x01000400, 0x01000400, 0x00010400, 790 0x00010400, 0x01010000, 0x01010000, 0x01000404, 791 0x00010004, 0x01000004, 0x01000004, 0x00010004, 792 0x00000000, 0x00000404, 0x00010404, 0x01000000, 793 0x00010000, 0x01010404, 0x00000004, 0x01010000, 794 0x01010400, 0x01000000, 0x01000000, 0x00000400, 795 0x01010004, 0x00010000, 0x00010400, 0x01000004, 796 0x00000400, 0x00000004, 0x01000404, 0x00010404, 797 0x01010404, 0x00010004, 0x01010000, 0x01000404, 798 0x01000004, 0x00000404, 0x00010404, 0x01010400, 799 0x00000404, 0x01000400, 0x01000400, 0x00000000, 800 0x00010004, 0x00010400, 0x00000000, 0x01010004 801 }; 802 803 static const unsigned long SB2[64] = 804 { 805 0x80108020, 0x80008000, 0x00008000, 0x00108020, 806 0x00100000, 0x00000020, 0x80100020, 0x80008020, 807 0x80000020, 0x80108020, 0x80108000, 0x80000000, 808 0x80008000, 0x00100000, 0x00000020, 0x80100020, 809 0x00108000, 0x00100020, 0x80008020, 0x00000000, 810 0x80000000, 0x00008000, 0x00108020, 0x80100000, 811 0x00100020, 0x80000020, 0x00000000, 0x00108000, 812 0x00008020, 0x80108000, 0x80100000, 0x00008020, 813 0x00000000, 0x00108020, 0x80100020, 0x00100000, 814 0x80008020, 0x80100000, 0x80108000, 0x00008000, 815 0x80100000, 0x80008000, 0x00000020, 0x80108020, 816 0x00108020, 0x00000020, 0x00008000, 0x80000000, 817 0x00008020, 0x80108000, 0x00100000, 0x80000020, 818 0x00100020, 0x80008020, 0x80000020, 0x00100020, 819 0x00108000, 0x00000000, 0x80008000, 0x00008020, 820 0x80000000, 0x80100020, 0x80108020, 0x00108000 821 }; 822 823 static const unsigned long SB3[64] = 824 { 825 0x00000208, 0x08020200, 0x00000000, 0x08020008, 826 0x08000200, 0x00000000, 0x00020208, 0x08000200, 827 0x00020008, 0x08000008, 0x08000008, 0x00020000, 828 0x08020208, 0x00020008, 0x08020000, 0x00000208, 829 0x08000000, 0x00000008, 0x08020200, 0x00000200, 830 0x00020200, 0x08020000, 0x08020008, 0x00020208, 831 0x08000208, 0x00020200, 0x00020000, 0x08000208, 832 0x00000008, 0x08020208, 0x00000200, 0x08000000, 833 0x08020200, 0x08000000, 0x00020008, 0x00000208, 834 0x00020000, 0x08020200, 0x08000200, 0x00000000, 835 0x00000200, 0x00020008, 0x08020208, 0x08000200, 836 0x08000008, 0x00000200, 0x00000000, 0x08020008, 837 0x08000208, 0x00020000, 0x08000000, 0x08020208, 838 0x00000008, 0x00020208, 0x00020200, 0x08000008, 839 0x08020000, 0x08000208, 0x00000208, 0x08020000, 840 0x00020208, 0x00000008, 0x08020008, 0x00020200 841 }; 842 843 static const unsigned long SB4[64] = 844 { 845 0x00802001, 0x00002081, 0x00002081, 0x00000080, 846 0x00802080, 0x00800081, 0x00800001, 0x00002001, 847 0x00000000, 0x00802000, 0x00802000, 0x00802081, 848 0x00000081, 0x00000000, 0x00800080, 0x00800001, 849 0x00000001, 0x00002000, 0x00800000, 0x00802001, 850 0x00000080, 0x00800000, 0x00002001, 0x00002080, 851 0x00800081, 0x00000001, 0x00002080, 0x00800080, 852 0x00002000, 0x00802080, 0x00802081, 0x00000081, 853 0x00800080, 0x00800001, 0x00802000, 0x00802081, 854 0x00000081, 0x00000000, 0x00000000, 0x00802000, 855 0x00002080, 0x00800080, 0x00800081, 0x00000001, 856 0x00802001, 0x00002081, 0x00002081, 0x00000080, 857 0x00802081, 0x00000081, 0x00000001, 0x00002000, 858 0x00800001, 0x00002001, 0x00802080, 0x00800081, 859 0x00002001, 0x00002080, 0x00800000, 0x00802001, 860 0x00000080, 0x00800000, 0x00002000, 0x00802080 861 }; 862 863 static const unsigned long SB5[64] = 864 { 865 0x00000100, 0x02080100, 0x02080000, 0x42000100, 866 0x00080000, 0x00000100, 0x40000000, 0x02080000, 867 0x40080100, 0x00080000, 0x02000100, 0x40080100, 868 0x42000100, 0x42080000, 0x00080100, 0x40000000, 869 0x02000000, 0x40080000, 0x40080000, 0x00000000, 870 0x40000100, 0x42080100, 0x42080100, 0x02000100, 871 0x42080000, 0x40000100, 0x00000000, 0x42000000, 872 0x02080100, 0x02000000, 0x42000000, 0x00080100, 873 0x00080000, 0x42000100, 0x00000100, 0x02000000, 874 0x40000000, 0x02080000, 0x42000100, 0x40080100, 875 0x02000100, 0x40000000, 0x42080000, 0x02080100, 876 0x40080100, 0x00000100, 0x02000000, 0x42080000, 877 0x42080100, 0x00080100, 0x42000000, 0x42080100, 878 0x02080000, 0x00000000, 0x40080000, 0x42000000, 879 0x00080100, 0x02000100, 0x40000100, 0x00080000, 880 0x00000000, 0x40080000, 0x02080100, 0x40000100 881 }; 882 883 static const unsigned long SB6[64] = 884 { 885 0x20000010, 0x20400000, 0x00004000, 0x20404010, 886 0x20400000, 0x00000010, 0x20404010, 0x00400000, 887 0x20004000, 0x00404010, 0x00400000, 0x20000010, 888 0x00400010, 0x20004000, 0x20000000, 0x00004010, 889 0x00000000, 0x00400010, 0x20004010, 0x00004000, 890 0x00404000, 0x20004010, 0x00000010, 0x20400010, 891 0x20400010, 0x00000000, 0x00404010, 0x20404000, 892 0x00004010, 0x00404000, 0x20404000, 0x20000000, 893 0x20004000, 0x00000010, 0x20400010, 0x00404000, 894 0x20404010, 0x00400000, 0x00004010, 0x20000010, 895 0x00400000, 0x20004000, 0x20000000, 0x00004010, 896 0x20000010, 0x20404010, 0x00404000, 0x20400000, 897 0x00404010, 0x20404000, 0x00000000, 0x20400010, 898 0x00000010, 0x00004000, 0x20400000, 0x00404010, 899 0x00004000, 0x00400010, 0x20004010, 0x00000000, 900 0x20404000, 0x20000000, 0x00400010, 0x20004010 901 }; 902 903 static const unsigned long SB7[64] = 904 { 905 0x00200000, 0x04200002, 0x04000802, 0x00000000, 906 0x00000800, 0x04000802, 0x00200802, 0x04200800, 907 0x04200802, 0x00200000, 0x00000000, 0x04000002, 908 0x00000002, 0x04000000, 0x04200002, 0x00000802, 909 0x04000800, 0x00200802, 0x00200002, 0x04000800, 910 0x04000002, 0x04200000, 0x04200800, 0x00200002, 911 0x04200000, 0x00000800, 0x00000802, 0x04200802, 912 0x00200800, 0x00000002, 0x04000000, 0x00200800, 913 0x04000000, 0x00200800, 0x00200000, 0x04000802, 914 0x04000802, 0x04200002, 0x04200002, 0x00000002, 915 0x00200002, 0x04000000, 0x04000800, 0x00200000, 916 0x04200800, 0x00000802, 0x00200802, 0x04200800, 917 0x00000802, 0x04000002, 0x04200802, 0x04200000, 918 0x00200800, 0x00000000, 0x00000002, 0x04200802, 919 0x00000000, 0x00200802, 0x04200000, 0x00000800, 920 0x04000002, 0x04000800, 0x00000800, 0x00200002 921 }; 922 923 static const unsigned long SB8[64] = 924 { 925 0x10001040, 0x00001000, 0x00040000, 0x10041040, 926 0x10000000, 0x10001040, 0x00000040, 0x10000000, 927 0x00040040, 0x10040000, 0x10041040, 0x00041000, 928 0x10041000, 0x00041040, 0x00001000, 0x00000040, 929 0x10040000, 0x10000040, 0x10001000, 0x00001040, 930 0x00041000, 0x00040040, 0x10040040, 0x10041000, 931 0x00001040, 0x00000000, 0x00000000, 0x10040040, 932 0x10000040, 0x10001000, 0x00041040, 0x00040000, 933 0x00041040, 0x00040000, 0x10041000, 0x00001000, 934 0x00000040, 0x10040040, 0x00001000, 0x00041040, 935 0x10001000, 0x00000040, 0x10000040, 0x10040000, 936 0x10040040, 0x10000000, 0x00040000, 0x10001040, 937 0x00000000, 0x10041040, 0x00040040, 0x10000040, 938 0x10040000, 0x10001000, 0x10001040, 0x00000000, 939 0x10041040, 0x00041000, 0x00041000, 0x00001040, 940 0x00001040, 0x00040040, 0x10000000, 0x10041000 941 }; 942 943 /* 944 * PC1: left and right halves bit-swap 945 */ 946 static const unsigned long LHs[16] = 947 { 948 0x00000000, 0x00000001, 0x00000100, 0x00000101, 949 0x00010000, 0x00010001, 0x00010100, 0x00010101, 950 0x01000000, 0x01000001, 0x01000100, 0x01000101, 951 0x01010000, 0x01010001, 0x01010100, 0x01010101 952 }; 953 954 static const unsigned long RHs[16] = 955 { 956 0x00000000, 0x01000000, 0x00010000, 0x01010000, 957 0x00000100, 0x01000100, 0x00010100, 0x01010100, 958 0x00000001, 0x01000001, 0x00010001, 0x01010001, 959 0x00000101, 0x01000101, 0x00010101, 0x01010101, 960 }; 961 962 /* 963 * Initial Permutation macro 964 */ 965 #define DES_IP(X,Y) 966 { 967 T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); 968 T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); 969 T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); 970 T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); 971 Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; 972 T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; 973 X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; 974 } 975 976 /* 977 * Final Permutation macro 978 */ 979 #define DES_FP(X,Y) 980 { 981 X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; 982 T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; 983 Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; 984 T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); 985 T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); 986 T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); 987 T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); 988 } 989 990 /* 991 * DES round macro 992 */ 993 #define DES_ROUND(X,Y) 994 { 995 T = *SK++ ^ X; 996 Y ^= SB8[ (T ) & 0x3F ] ^ 997 SB6[ (T >> 8) & 0x3F ] ^ 998 SB4[ (T >> 16) & 0x3F ] ^ 999 SB2[ (T >> 24) & 0x3F ]; 1000 1001 T = *SK++ ^ ((X << 28) | (X >> 4)); 1002 Y ^= SB7[ (T ) & 0x3F ] ^ 1003 SB5[ (T >> 8) & 0x3F ] ^ 1004 SB3[ (T >> 16) & 0x3F ] ^ 1005 SB1[ (T >> 24) & 0x3F ]; 1006 } 1007 1008 #define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; } 1009 1010 static void des_setkey( unsigned long SK[32], const unsigned char key[8] ) 1011 { 1012 int i; 1013 unsigned long X, Y, T; 1014 1015 GET_ULONG_BE( X, key, 0 ); 1016 GET_ULONG_BE( Y, key, 4 ); 1017 1018 /* 1019 * Permuted Choice 1 1020 */ 1021 T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4); 1022 T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T ); 1023 1024 X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2) 1025 | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] ) 1026 | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6) 1027 | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4); 1028 1029 Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2) 1030 | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] ) 1031 | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6) 1032 | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4); 1033 1034 X &= 0x0FFFFFFF; 1035 Y &= 0x0FFFFFFF; 1036 1037 /* 1038 * calculate subkeys 1039 */ 1040 for( i = 0; i < 16; i++ ) 1041 { 1042 if( i < 2 || i == 8 || i == 15 ) 1043 { 1044 X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF; 1045 Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF; 1046 } 1047 else 1048 { 1049 X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF; 1050 Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF; 1051 } 1052 1053 *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000) 1054 | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000) 1055 | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000) 1056 | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000) 1057 | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000) 1058 | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000) 1059 | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400) 1060 | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100) 1061 | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010) 1062 | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004) 1063 | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001); 1064 1065 *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000) 1066 | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000) 1067 | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000) 1068 | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000) 1069 | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000) 1070 | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000) 1071 | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000) 1072 | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400) 1073 | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100) 1074 | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011) 1075 | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002); 1076 } 1077 } 1078 1079 /* 1080 * DES key schedule (56-bit, encryption) 1081 */ 1082 void des_setkey_enc( des_context *ctx, const unsigned char key[8] ) 1083 { 1084 des_setkey( ctx->sk, key ); 1085 } 1086 1087 /* 1088 * DES key schedule (56-bit, decryption) 1089 */ 1090 void des_setkey_dec( des_context *ctx, const unsigned char key[8] ) 1091 { 1092 int i; 1093 1094 des_setkey( ctx->sk, key ); 1095 1096 for( i = 0; i < 16; i += 2 ) 1097 { 1098 SWAP( ctx->sk[i ], ctx->sk[30 - i] ); 1099 SWAP( ctx->sk[i + 1], ctx->sk[31 - i] ); 1100 } 1101 } 1102 1103 static void des3_set2key( unsigned long esk[96], 1104 unsigned long dsk[96], 1105 const unsigned char key[16] ) 1106 { 1107 int i; 1108 1109 des_setkey( esk, key ); 1110 des_setkey( dsk + 32, key + 8 ); 1111 1112 for( i = 0; i < 32; i += 2 ) 1113 { 1114 dsk[i ] = esk[30 - i]; 1115 dsk[i + 1] = esk[31 - i]; 1116 1117 esk[i + 32] = dsk[62 - i]; 1118 esk[i + 33] = dsk[63 - i]; 1119 1120 esk[i + 64] = esk[i ]; 1121 esk[i + 65] = esk[i + 1]; 1122 1123 dsk[i + 64] = dsk[i ]; 1124 dsk[i + 65] = dsk[i + 1]; 1125 } 1126 } 1127 1128 /* 1129 * Triple-DES key schedule (112-bit, encryption) 1130 */ 1131 void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] ) 1132 { 1133 unsigned long sk[96]; 1134 1135 des3_set2key( ctx->sk, sk, key ); 1136 memset( sk, 0, sizeof( sk ) ); 1137 } 1138 1139 /* 1140 * Triple-DES key schedule (112-bit, decryption) 1141 */ 1142 void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] ) 1143 { 1144 unsigned long sk[96]; 1145 1146 des3_set2key( sk, ctx->sk, key ); 1147 memset( sk, 0, sizeof( sk ) ); 1148 } 1149 1150 static void des3_set3key( unsigned long esk[96], 1151 unsigned long dsk[96], 1152 const unsigned char key[24] ) 1153 { 1154 int i; 1155 1156 des_setkey( esk, key ); 1157 des_setkey( dsk + 32, key + 8 ); 1158 des_setkey( esk + 64, key + 16 ); 1159 1160 for( i = 0; i < 32; i += 2 ) 1161 { 1162 dsk[i ] = esk[94 - i]; 1163 dsk[i + 1] = esk[95 - i]; 1164 1165 esk[i + 32] = dsk[62 - i]; 1166 esk[i + 33] = dsk[63 - i]; 1167 1168 dsk[i + 64] = esk[30 - i]; 1169 dsk[i + 65] = esk[31 - i]; 1170 } 1171 } 1172 1173 /* 1174 * Triple-DES key schedule (168-bit, encryption) 1175 */ 1176 void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] ) 1177 { 1178 unsigned long sk[96]; 1179 1180 des3_set3key( ctx->sk, sk, key ); 1181 memset( sk, 0, sizeof( sk ) ); 1182 } 1183 1184 /* 1185 * Triple-DES key schedule (168-bit, decryption) 1186 */ 1187 void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] ) 1188 { 1189 unsigned long sk[96]; 1190 1191 des3_set3key( sk, ctx->sk, key ); 1192 memset( sk, 0, sizeof( sk ) ); 1193 } 1194 1195 /* 1196 * DES-ECB block encryption/decryption 1197 */ 1198 int des_crypt_ecb( des_context *ctx, 1199 const unsigned char input[8], 1200 unsigned char output[8] ) 1201 { 1202 int i; 1203 unsigned long X, Y, T, *SK; 1204 1205 SK = ctx->sk; 1206 1207 GET_ULONG_BE( X, input, 0 ); 1208 GET_ULONG_BE( Y, input, 4 ); 1209 1210 DES_IP( X, Y ); 1211 1212 for( i = 0; i < 8; i++ ) 1213 { 1214 DES_ROUND( Y, X ); 1215 DES_ROUND( X, Y ); 1216 } 1217 1218 DES_FP( Y, X ); 1219 1220 PUT_ULONG_BE( Y, output, 0 ); 1221 PUT_ULONG_BE( X, output, 4 ); 1222 1223 return( 0 ); 1224 } 1225 1226 /* 1227 * DES-CBC buffer encryption/decryption 1228 */ 1229 int des_crypt_cbc( des_context *ctx, 1230 int mode, 1231 int length, 1232 unsigned char iv[8], 1233 const unsigned char *input, 1234 unsigned char *output ) 1235 { 1236 int i; 1237 unsigned char temp[8]; 1238 1239 if( length % 8 ) 1240 return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH ); 1241 1242 if( mode == DES_ENCRYPT ) 1243 { 1244 while( length > 0 ) 1245 { 1246 for( i = 0; i < 8; i++ ) 1247 output[i] = (unsigned char)( input[i] ^ iv[i] ); 1248 1249 des_crypt_ecb( ctx, output, output ); 1250 memcpy( iv, output, 8 ); 1251 1252 input += 8; 1253 output += 8; 1254 length -= 8; 1255 } 1256 } 1257 else /* DES_DECRYPT */ 1258 { 1259 while( length > 0 ) 1260 { 1261 memcpy( temp, input, 8 ); 1262 des_crypt_ecb( ctx, input, output ); 1263 1264 for( i = 0; i < 8; i++ ) 1265 output[i] = (unsigned char)( output[i] ^ iv[i] ); 1266 1267 memcpy( iv, temp, 8 ); 1268 1269 input += 8; 1270 output += 8; 1271 length -= 8; 1272 } 1273 } 1274 1275 return( 0 ); 1276 } 1277 1278 /* 1279 * 3DES-ECB block encryption/decryption 1280 */ 1281 int des3_crypt_ecb( des3_context *ctx, 1282 const unsigned char input[8], 1283 unsigned char output[8] ) 1284 { 1285 int i; 1286 unsigned long X, Y, T, *SK; 1287 1288 SK = ctx->sk; 1289 1290 GET_ULONG_BE( X, input, 0 ); 1291 GET_ULONG_BE( Y, input, 4 ); 1292 1293 DES_IP( X, Y ); 1294 1295 for( i = 0; i < 8; i++ ) 1296 { 1297 DES_ROUND( Y, X ); 1298 DES_ROUND( X, Y ); 1299 } 1300 1301 for( i = 0; i < 8; i++ ) 1302 { 1303 DES_ROUND( X, Y ); 1304 DES_ROUND( Y, X ); 1305 } 1306 1307 for( i = 0; i < 8; i++ ) 1308 { 1309 DES_ROUND( Y, X ); 1310 DES_ROUND( X, Y ); 1311 } 1312 1313 DES_FP( Y, X ); 1314 1315 PUT_ULONG_BE( Y, output, 0 ); 1316 PUT_ULONG_BE( X, output, 4 ); 1317 1318 return( 0 ); 1319 } 1320 1321 /* 1322 * 3DES-CBC buffer encryption/decryption 1323 */ 1324 int des3_crypt_cbc( des3_context *ctx, 1325 int mode, 1326 int length, 1327 unsigned char iv[8], 1328 const unsigned char *input, 1329 unsigned char *output ) 1330 { 1331 int i; 1332 unsigned char temp[8]; 1333 1334 if( length % 8 ) 1335 return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH ); 1336 1337 if( mode == DES_ENCRYPT ) 1338 { 1339 while( length > 0 ) 1340 { 1341 for( i = 0; i < 8; i++ ) 1342 output[i] = (unsigned char)( input[i] ^ iv[i] ); 1343 1344 des3_crypt_ecb( ctx, output, output ); 1345 memcpy( iv, output, 8 ); 1346 1347 input += 8; 1348 output += 8; 1349 length -= 8; 1350 } 1351 } 1352 else /* DES_DECRYPT */ 1353 { 1354 while( length > 0 ) 1355 { 1356 memcpy( temp, input, 8 ); 1357 des3_crypt_ecb( ctx, input, output ); 1358 1359 for( i = 0; i < 8; i++ ) 1360 output[i] = (unsigned char)( output[i] ^ iv[i] ); 1361 1362 memcpy( iv, temp, 8 ); 1363 1364 input += 8; 1365 output += 8; 1366 length -= 8; 1367 } 1368 } 1369 1370 return( 0 ); 1371 } 1372 1373 #if defined(POLARSSL_SELF_TEST) 1374 1375 #include <stdio.h> 1376 1377 /* 1378 * DES and 3DES test vectors from: 1379 1381 */ 1382 static const unsigned char des3_test_keys[24] = 1383 { 1384 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 1385 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 1386 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 1387 }; 1388 1389 static const unsigned char des3_test_iv[8] = 1390 { 1391 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 1392 }; 1393 1394 static const unsigned char des3_test_buf[8] = 1395 { 1396 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74 1397 }; 1398 1399 static const unsigned char des3_test_ecb_dec[3][8] = 1400 { 1401 { 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D }, 1402 { 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB }, 1403 { 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A } 1404 }; 1405 1406 static const unsigned char des3_test_ecb_enc[3][8] = 1407 { 1408 { 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B }, 1409 { 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 }, 1410 { 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 } 1411 }; 1412 1413 static const unsigned char des3_test_cbc_dec[3][8] = 1414 { 1415 { 0x12, 0x9F, 0x40, 0xB9, 0xD2, 0x00, 0x56, 0xB3 }, 1416 { 0x47, 0x0E, 0xFC, 0x9A, 0x6B, 0x8E, 0xE3, 0x93 }, 1417 { 0xC5, 0xCE, 0xCF, 0x63, 0xEC, 0xEC, 0x51, 0x4C } 1418 }; 1419 1420 static const unsigned char des3_test_cbc_enc[3][8] = 1421 { 1422 { 0x54, 0xF1, 0x5A, 0xF6, 0xEB, 0xE3, 0xA4, 0xB4 }, 1423 { 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D }, 1424 { 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 } 1425 }; 1426 1427 /* 1428 * Checkup routine 1429 */ 1430 int des_self_test( int verbose ) 1431 { 1432 int i, j, u, v; 1433 des_context ctx; 1434 des3_context ctx3; 1435 unsigned char key[24]; 1436 unsigned char buf[8]; 1437 unsigned char prv[8]; 1438 unsigned char iv[8]; 1439 1440 memset( key, 0, 24 ); 1441 1442 /* 1443 * ECB mode 1444 */ 1445 for( i = 0; i < 6; i++ ) 1446 { 1447 u = i >> 1; 1448 v = i & 1; 1449 1450 if( verbose != 0 ) 1451 printf( " DES%c-ECB-%3d (%s): ", 1452 ( u == 0 ) ? ' ' : '3', 56 + u * 56, 1453 ( v == DES_DECRYPT ) ? "dec" : "enc" ); 1454 1455 memcpy( buf, des3_test_buf, 8 ); 1456 1457 switch( i ) 1458 { 1459 case 0: 1460 des_setkey_dec( &ctx, (unsigned char *) des3_test_keys ); 1461 break; 1462 1463 case 1: 1464 des_setkey_enc( &ctx, (unsigned char *) des3_test_keys ); 1465 break; 1466 1467 case 2: 1468 des3_set2key_dec( &ctx3, (unsigned char *) des3_test_keys ); 1469 break; 1470 1471 case 3: 1472 des3_set2key_enc( &ctx3, (unsigned char *) des3_test_keys ); 1473 break; 1474 1475 case 4: 1476 des3_set3key_dec( &ctx3, (unsigned char *) des3_test_keys ); 1477 break; 1478 1479 case 5: 1480 des3_set3key_enc( &ctx3, (unsigned char *) des3_test_keys ); 1481 break; 1482 1483 default: 1484 return( 1 ); 1485 } 1486 1487 for( j = 0; j < 10000; j++ ) 1488 { 1489 if( u == 0 ) 1490 des_crypt_ecb( &ctx, buf, buf ); 1491 else 1492 des3_crypt_ecb( &ctx3, buf, buf ); 1493 } 1494 1495 if( ( v == DES_DECRYPT && 1496 memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) || 1497 ( v != DES_DECRYPT && 1498 memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) ) 1499 { 1500 if( verbose != 0 ) 1501 printf( "failed " ); 1502 1503 return( 1 ); 1504 } 1505 1506 if( verbose != 0 ) 1507 printf( "passed " ); 1508 } 1509 1510 if( verbose != 0 ) 1511 printf( " " ); 1512 1513 /* 1514 * CBC mode 1515 */ 1516 for( i = 0; i < 6; i++ ) 1517 { 1518 u = i >> 1; 1519 v = i & 1; 1520 1521 if( verbose != 0 ) 1522 printf( " DES%c-CBC-%3d (%s): ", 1523 ( u == 0 ) ? ' ' : '3', 56 + u * 56, 1524 ( v == DES_DECRYPT ) ? "dec" : "enc" ); 1525 1526 memcpy( iv, des3_test_iv, 8 ); 1527 memcpy( prv, des3_test_iv, 8 ); 1528 memcpy( buf, des3_test_buf, 8 ); 1529 1530 switch( i ) 1531 { 1532 case 0: 1533 des_setkey_dec( &ctx, (unsigned char *) des3_test_keys ); 1534 break; 1535 1536 case 1: 1537 des_setkey_enc( &ctx, (unsigned char *) des3_test_keys ); 1538 break; 1539 1540 case 2: 1541 des3_set2key_dec( &ctx3, (unsigned char *) des3_test_keys ); 1542 break; 1543 1544 case 3: 1545 des3_set2key_enc( &ctx3, (unsigned char *) des3_test_keys ); 1546 break; 1547 1548 case 4: 1549 des3_set3key_dec( &ctx3, (unsigned char *) des3_test_keys ); 1550 break; 1551 1552 case 5: 1553 des3_set3key_enc( &ctx3, (unsigned char *) des3_test_keys ); 1554 break; 1555 1556 default: 1557 return( 1 ); 1558 } 1559 1560 if( v == DES_DECRYPT ) 1561 { 1562 for( j = 0; j < 10000; j++ ) 1563 { 1564 if( u == 0 ) 1565 des_crypt_cbc( &ctx, v, 8, iv, buf, buf ); 1566 else 1567 des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf ); 1568 } 1569 } 1570 else 1571 { 1572 for( j = 0; j < 10000; j++ ) 1573 { 1574 unsigned char tmp[8]; 1575 1576 if( u == 0 ) 1577 des_crypt_cbc( &ctx, v, 8, iv, buf, buf ); 1578 else 1579 des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf ); 1580 1581 memcpy( tmp, prv, 8 ); 1582 memcpy( prv, buf, 8 ); 1583 memcpy( buf, tmp, 8 ); 1584 } 1585 1586 memcpy( buf, prv, 8 ); 1587 } 1588 1589 if( ( v == DES_DECRYPT && 1590 memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) || 1591 ( v != DES_DECRYPT && 1592 memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) ) 1593 { 1594 if( verbose != 0 ) 1595 printf( "failed " ); 1596 1597 return( 1 ); 1598 } 1599 1600 if( verbose != 0 ) 1601 printf( "passed " ); 1602 } 1603 1604 if( verbose != 0 ) 1605 printf( " " ); 1606 1607 return( 0 ); 1608 } 1609 1610 #endif 1611 1612 #endif 1613 /*里面包含加密 解密 des DES3 */ 1614 /* 1615 int for_encrypt_des3(char *data, unsigned int len) 1616 { 1617 int i, j; 1618 unsigned char buf[BUFSIZE]; 1619 unsigned long tsc; 1620 unsigned char tmp[64]; 1621 FILE *fp; 1622 1623 des3_context des3; 1624 1625 1626 // des_context des; 1627 1628 des_setkey_enc( &des, tmp); 1629 set_alarm( 1 ); 1630 for(i=1; !alarmed; i++) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); 1631 tsc = hardclock(); 1632 for(j=0; j<1024; j++) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); 1633 printf("DES speed: %9lu Kb/s, %9lu cycles/byte ", i*BUFSIZE /1024, (hardclock()-tsc)/(j*BUFSIZE)); 1634 1635 1636 1637 1638 des3_set3key_enc( &des3, tmp ); 1639 set_alarm( 1 ); 1640 for(i=1; !alarmed; i++) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); 1641 tsc = hardclock(); 1642 for(j=0; j<1024; j++) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); 1643 1644 DO_LOG("DES3 speed: %9lu Kb/s, %9lu cycles/byte ", i*BUFSIZE/1024, (hardclock()-tsc)/(j*BUFSIZE)); 1645 1646 1647 1648 memset(tmp, 0, sizeof(tmp)); strcpy(tmp, "01234567abcdefgh"); 1649 memset(buf, 0, sizeof(buf)); strncpy(buf, data,len); 1650 des3_set3key_enc( &des3, tmp); 1651 DO_LOG("DES3 pwd=%s, txt=%s ", tmp, buf); 1652 des3_crypt_cbc(&des3, DES_ENCRYPT, 32768, tmp, buf, buf ); 1653 DO_LOG("encrypt txt=%s ",buf); 1654 fp = fopen("/D1", "wb"); 1655 if(fp!=NULL) 1656 { 1657 fprintf(fp,"%s",buf); 1658 fclose(fp); 1659 } 1660 1661 1662 1663 1664 memset(tmp, 0, sizeof(tmp)); strcpy(tmp, "01234567abcdefgh"); 1665 des3_set3key_dec(&des3, tmp); 1666 des3_crypt_cbc(&des3, DES_DECRYPT, 32768, tmp, buf, buf ); 1667 //DO_LOG("decrypt txt= %s ",buf); 1668 1669 1670 memset(tmp, 0, sizeof(tmp)); strcpy(tmp, "01234567abcdefgh"); 1671 des_setkey_dec(&des, tmp); 1672 des_crypt_cbc(&des, DES_DECRYPT, 32768, tmp, buf, buf ); 1673 fp = fopen("/D2", "wb"); 1674 if(fp!=NULL) 1675 { 1676 fprintf(fp,"%s",buf); 1677 fclose(fp); 1678 } 1679 return 0; 1680 } 1681 */ 1682 int hexcharToInt(char c) 1683 { 1684 if (c >= '0' && c <= '9') return (c - '0'); 1685 if (c >= 'A' && c <= 'F') return (c - 'A' + 10); 1686 if (c >= 'a' && c <= 'f') return (c - 'a' + 10); 1687 return 0; 1688 } 1689 1690 void hexstringToBytes(char* hexstring,char* bytes,int hexlength) 1691 { 1692 int i=0 ; 1693 //cout<<"length is :"<<sizeof(hexstring)/sizeof(char)<<endl; 1694 for (i=0 ; i <hexlength ; i+=2) 1695 { 1696 bytes[i/2] = (char) ((hexcharToInt(hexstring[i]) << 4)| hexcharToInt(hexstring[i+1])); 1697 } 1698 } 1699 1700 void for_decode_des3(unsigned char *data, unsigned int len)//解密 1701 { 1702 char buf[3000]; 1703 unsigned char tmp[64]; 1704 des3_context des3; 1705 memset(buf, 0, 3000); 1706 hexstringToBytes((char *)data, buf, 3000); 1707 memset(data,0,3000); 1708 strncpy((char*)data,buf,strlen(buf)); 1709 1710 1711 memset(tmp, 0, sizeof(tmp)); strcpy((char*)tmp, "JDWA2003"); 1712 des3_set3key_dec(&des3, tmp); 1713 des3_crypt_cbc(&des3, DES_DECRYPT, 1024, tmp, data,data); 1714 }