che_api.h 59.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2007
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *    che_api.h
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   Cipher/Hash Engine API.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/


/*! \mainpage Cipher Hash Engine
 *  @author  Julie Yang
 *  @version 1.0
 *  @date    2007/06/14
 *
 * \section CHE CHE API Introduction
 *
 * This document is an introduction of MTK Cipher and Hash Engine API. There is an interface
 * between Application and Cipher/Hash engine. The interface layer includes four functions.
 * User and application can just use these four functions to set
 * cipher/hash condition, and trigger the process of the cipher/hash engine. Either the
 * bottom layer is hardware or software driver.\n\n
 *
 * CHE API also release four enum structures to define the setting parameters for AP using.
 * User can just use these structures to set cipher/hash conditions,
 * and trigger the process of the cipher/hash engine.\n
 *
 * The header file is in \\mcu\\applib\\misc\\include\\che_api.h\n
 *
 * The API is supported after 07A branch. \n
 *
 *
 */

/** @defgroup CHE Cipher/Hash Engine Interface
 *  @{
 */

/** @defgroup CHE Cipher/Hash Engine Interface
 *  @{
 */
#ifndef _CHE_API_H
#define _CHE_API_H

#include "kal_release.h"
#include "fsal.h"
//#include "md5.h"
//#include "sha1.h"
//#include "md4.h"

/* DOM-NOT_FOR_SDK-BEGIN */
/* DNS context structure */
typedef struct DES_CNXT_T
{
#if defined(__HW_CHE__)  
    kal_uint8 cheCKey[36];          /* des; 3des;convert_aes_128_key;convert_aes_192_key;convert_aes_256_key */
    kal_uint8 cheDataAddr[120];     /* che_hw_des;che_hw_aes;che_hw_hash */
#else /* defined(__HW_CHE__) */ 
    kal_uint8 cheLastBlock[16];     /* Last block used in counter mode (store the previous cipher text) */
    kal_uint8 cheLastReminder[16];  /* Last remainder block */
    kal_uint8 cheKeySchedule[128];  /* key schedule */
    kal_uint8 iv[64];               /* initial vector */
#endif /* defined(__HW_CHE__) */ 
}DES_CNXT;

/* AES context structure */
typedef struct AES_CNXT_T
{
#if defined(__HW_CHE__)  
    kal_uint8 cheCKey[36];          /* des; 3des;convert_aes_128_key;convert_aes_192_key;convert_aes_256_key */
    kal_uint8 cheDataAddr[120];     /* che_hw_des;che_hw_aes;che_hw_hash */
#else /* defined(__HW_CHE__) */ 
    kal_uint8 cheLastReminder[16];  /* Last remaining segment */
    kal_uint8 orgIV[64];            /* Initial vector */
#endif /* defined(__HW_CHE__) */ 
}AES_CNXT;

/* MD5 and SH1 structure */
typedef struct MD5_CNXT_T
{
#if defined(__HW_CHE__)  
    kal_uint8 cheDataAddr[120]; /* che_hw_des;che_hw_aes;che_hw_hash */
#else
    kal_uint8 *working_buffer; /* working buffer */
#endif
}MD5_CNXT, SHA1_CNXT;

/* structure used for MD5,SHA1, MAC_MD5, MAC_SHA1, MD4, DHM */
typedef struct CNXT_T
{
    kal_uint8 *working_buffer; /* working buffer */
}SW_MISC_CNXT;

/* RC4 structure */
typedef struct RC4_CNXT_T
{
    kal_uint32 x, y; /* the swap index */
    kal_uint8 keyStream[256]; /* key stream */
} RC4_CNXT;

/* used only for HW HASH ;che_hw_des;che_hw_aes;che_hw_hash */
typedef struct HASH_CNXT_T 
{
    kal_uint8 cheDataAddr[120]; /* Data address */
}HASH_CNXT;

/* RSA structure */
typedef struct RSA_CNXT_T
{
    /* RSA */
    kal_uint8 *modulusN; /* modulus N. N=P*Q. It is a prime number */
    kal_uint32 modulusNLen; /* the length of modulus N */
    kal_uint8 *pubExp; /* public exponent: E */
    kal_uint32 pubExpLen; /* the length of public exponent */
    kal_uint8 *privExpD; /* private exponent: D */
    kal_uint32 privExpDLen; /* the length of private exponent D */
    kal_uint8 *primeP; /* modulus P. It is a prime number */
    kal_uint32 primePLen; /* the length of P */
    kal_uint8 *primeQ; /* modulus Q. It is a prime number */
    kal_uint32 primeQLen; /* the length of Q */
    kal_uint8 *dModPm1; /* private exponent: DP */
    kal_uint32 dModPm1Len; /* the length of private exponent DP */
    kal_uint8 *dModQm1; /* private exponent: DQ */
    kal_uint32 dModQm1Len; /* the length of private exponent DQ */
    kal_uint8 *qInvModP; /* coefficient Integer: inverse Q */
    kal_uint32 qInvModPLen; /* the length of coefficient integer */
    kal_uint8 *hash;    /* Hash value used to verify the signature */
    kal_uint32 hashLen; /* Hash length */
    kal_bool rsaKeyFlag; /* RSA key flag */
}RSA_CNXT;

/* #define __CHE_RSA_GEN_KEY__ */

/** The structure STCHE is defined to record and maintain the context of CHE */
typedef struct STCHE_T 
{
    kal_uint32 cheLastLength; /* last block length */
    kal_uint8 *cheLastDstAddr; /* last destination address */
       
    kal_uint8 *cheWorkBuf;  /* pointe to a space used by each algorithm */
    kal_uint32 cheBufLen;   /* the length of cheWorkBuf. It depends on the encrypt algorithm */
    kal_uint32 cipher_type; /* cipher type */
    
    kal_uint8 *cheKey; /* key stream */
    kal_uint8 *cheIV; /* initial vector */
    kal_uint32 cheKeyLength; /* key stream length */
    kal_uint32 cheIVLength; /* initial vector length */

#if defined(__HW_CHE__) 
    kal_bool cheResetKeyFlag; /* Reset key flag */
    kal_bool cheResetIVFlag; /* Reset initial vector flag */
#endif /* defined(__HW_CHE__) */ 

    kal_bool cheFirstFlag; /* first block flag */
 } STCHE;

/* DOM-NOT_FOR_SDK-END */

/* CHE error cause */
typedef enum
{
    CHE_OK                    = 0,  /* success */
    CHE_ERROR                 = -1, /* error occurs */
    CHE_KEY_CHK_FAIL          = -2, /* public or private key check fail */
    CHE_INVALID_LENGTH        = -3, /* buffer length too small or too long */
    CHE_OPERATION_FAIL        = -4  /* che operation fails, such as cipher fail */
} che_result_error_enum;

/** CHE_TYPE define the cipher and hash function algorithm type */
typedef enum
{
    CHE_MD5 = 1, /* MD5 hash algorithm. The output length is fixed 16 bytes */
    CHE_SHA1, /* SHA1 hash algorithm. The output length is fixed 20 bytes */
    CHE_DES, /* DES block cipher algorithm. The CHE will do auto padding in DES type.
                The maximum padding length is 8 bytes */
    CHE_3DES, /* 3DES block cipher algorithm. The CHE will do auto padding in 3DES type.
                 The maximum padding length is 8 bytes */
    CHE_AES, /* AES block cipher algorithm. The CHE will do auto padding in AES(ECB, CBC) type.
                The maximum padding length is 16 bytes */

    CHE_MD4, /* MD4 hash algorithm. The output length is fixed 16 bytes */
    CHE_MAC_MD5, /* HMAC_MD5 hash algorithm. The output length is fixed 16 bytes */
    CHE_MAC_SHA1, /* HMAC_SHA1 hash algorithm. The output length is fixed 20 bytes */
    CHE_RC4, /* RC4 stream cipher algorithm. No padding */

    CHE_RSA, /* RSA asym cipher algorithm. */
    CHE_DH, /* Diffie Hellman cipher algorithm used to generate the key. No padding */
    CHE_MAC_SHA224, /* HMAC_SHA224 hash algorithm. The output length is fixed 28 bytes */
    CHE_MAC_SHA256, /* HMAC_SHA256 hash algorithm. The output length is fixed 32 bytes */
    CHE_SHA224, /* SHA 224 algorithm */
    CHE_SHA256 /* SHA 256 algorithm */
} CHE_TYPE;

/* CHE_OPERATION_MODE define the cipher function mode. For hash function, it could be set CHE_MODE_NULL */
typedef enum
{
    CHE_MODE_NULL = 0, /* null mode. This mode is used when there is no mode operation */
    CHE_ECB, /* AES/DES ECB mode */
    CHE_CBC, /* AES/DES CBC mode */
    CHE_CTR, /* AES CTR(counter) mode */
    CHE_F8, /* AES F8 mode */

    CHE_SKIP, /* RC4 Skip mode */
    CHE_KEY_WRAP, /* AES key wrap mode */
    CHE_KEY_UNWRAP /* AES key unwrap mode */

} CHE_OPERATION_MODE;

/* che_rsa_padding_mode_enum define the RSA padding mode. */
typedef enum
{
    CHE_RSA_MODE_NULL = 0, /* null mode. This mode is used when there is no padding */
    CHE_RSA_PKCS1_V15, /* RSA PKCS#1 v1.5 padding mode  */
    CHE_RSA_MODE_TOTAL
} che_rsa_padding_mode_enum;

/* CHE_ACTION define this CHE process should run hash, encipher or decipher */
typedef enum
{
    CHE_HASH, /* generate hash value */
    CHE_ENC, /* encryption data */
    CHE_DEC, /* decryption data */
    CHE_MAC_HASH, /* generate HMAC value */

    DH_INIT, /* initialize the Diffie-Hellman */
    DH_MAKE_PARAMS, /* Suppose A=G^a mod p. generate discrete logarithms(a) and A */
    DH_MAKE_PUBLIC, /* Suppose A=G^a mod p. generate discrete logarithms(a) and A */
    DH_CALC_SECRET, /* Suppose B=G^b mod p. Calculate the shared secret (G^b)^a mod P */
    DH_FREE /* finialize the Diffie-Hellman */
} CHE_ACTION;

/* CHE_KEY_ACTION define the CHE Key process. The life cycle of input key buffer should
 * be maintained until che_deinit except RSA.
 */
typedef enum
{
    CHE_GEN_KEY, /* generate the key exclude asynmetry and RC4 algirhtm */
    CHE_SET_KEY, /* set the key exclude asynmetry and RC4 algirhtm */
    CHE_GET_KEY, /* get the key exclude asynmetry and RC4 algirhtm*/
    CHE_DEL_KEY, /* delete the key exclude asynmetry and RC4 algirhtm */

    CHE_SET_RC4_KEY, /* set the RC4 key */

#if defined(__CHE_RSA_GEN_KEY__)
    CHE_GEN_RSA_KEY, /* generate RSA key for hardware */
    CHE_DEL_RSA_KEY, /* delete the RSA key for hardward */
#endif /* defined(__CHE_RSA_GEN_KEY__) */ 
    CHE_SET_RSA_N, /* set the RSA modulus N. N=P*Q. N is prime number. 
                      The input N is a binary data */
    CHE_SET_RSA_E, /* set the RSA public exponent: E. 
                      The input E is a binary data */
    CHE_SET_RSA_D, /* set the RSA private exponent: D. 
                      The input D is a binary data */
    CHE_SET_RSA_P, /* set the RSA modulus P(CRT format). 
                      The input P is a binary data */
    CHE_SET_RSA_Q, /* set the RSA modulus Q(CRT format). 
                      The input Q is the binary data */
    CHE_SET_RSA_dModPm1, /* set the RSA private exponent(CRT format): DP. 
                            The input DP is a binary data */
    CHE_SET_RSA_dModQm1, /* set the RSA private exponent(CRT format): DQ. 
                            The input DQ is a binary data */
    CHE_SET_RSA_qInvModP, /* set the coefficient Integer(CRT format): inverse Q. 
                             The input InvQ is a binary data */

    CHE_SET_DH_P, /* set the DH modulus: P. The input P is a binary data. */
    CHE_SET_DH_G, /* set the DH prime root: G. The input G is a binary data */
    CHE_SET_DH_SELF_PUBLIC, /* set the self public value. G^X mod P. The input is a binary data. */
    CHE_SET_DH_PEER_PUBLIC, /* set the peer public value. G^Y mod P. The input is a binary data. */
    CHE_SET_DH_PRIVATE, /* set the discrete logarithms(private key): X. 
                           The input X is a binary data. */

    CHE_GET_DH_SELF_PUBLIC_LEN, /* get the self public length */
    CHE_GET_DH_PRIVATE_LEN, /* get the discrete logarithm length */
    CHE_GET_DH_SECRET_LEN, /* get the secret length */

    CHE_GET_DH_SELF_PUBLIC, /* get self public value */
    CHE_GET_DH_PRIVATE, /* get the discrete logarithm */
    CHE_GET_DH_SECRET, /* get the shared key,i.e. key. key = GY^X mod P */

    CHE_GET_RSA_N, /* get the RSA modulus N. N=P*Q. N is prime number. The output N is binary data */
    CHE_GET_RSA_E, /* get the RSA public exponent: E. The output E is a binary data */
    CHE_GET_RSA_D, /* get the RSA private exponent: D. The output D is a binary data */
    CHE_GET_RSA_P, /* get the RSA modulus P(CRT format). The output P is a binary data */
    CHE_GET_RSA_Q, /* get the RSA modulus Q(CRT format). The output Q is the binary data */
    CHE_GET_RSA_dModPm1, /* get the RSA private exponent(CRT format): DP. The output DP is a binary data */
    CHE_GET_RSA_dModQm1, /* get the RSA private exponent(CRT format): DQ. The output DQ is a binary data */
    CHE_GET_RSA_qInvModP, /* get the coefficient Integer(CRT format): inverse Q. The output InvQ is a binary data */

    CHE_GET_RSA_N_LEN, /* get the RSA modulus N. N=P*Q. N is prime number. The output N is binary data */
    CHE_GET_RSA_E_LEN, /* get the RSA public exponent: E. The output E is a binary data */
    CHE_GET_RSA_D_LEN, /* get the RSA private exponent: D. The output D is a binary data */
    CHE_GET_RSA_P_LEN, /* get the RSA modulus P(CRT format). The output P is a binary data */
    CHE_GET_RSA_Q_LEN, /* get the RSA modulus Q(CRT format). The output Q is the binary data */
    CHE_GET_RSA_dModPm1_LEN, /* get the RSA private exponent(CRT format): DP. The output DP is a binary data */
    CHE_GET_RSA_dModQm1_LEN, /* get the RSA private exponent(CRT format): DQ. The output DQ is a binary data */
    CHE_GET_RSA_qInvModP_LEN /* get the coefficient Integer(CRT format): inverse Q. The output InvQ is a binary data */

} CHE_KEY_ACTION;

#include "che_trick.h"

        
/*****************************************************************************
 * FUNCTION
 *  che_init
 * DESCRIPTION
 *  Initialize the CHE_CONTEXT data structure created from AP
 * PARAMETERS
 *  che_context  : [IN]   CHE context
 *  type         : [IN]   CHE type (CHE_TYPE)
 * RETURN VALUES
 *  void
 * SEE ALSO
 *  che_deinit
 * EXAMPLE
 * <code>
 * STCHE context;
 * che_init(&context, CHE_SHA1);
 * </code>
 *****************************************************************************/
extern void che_init(STCHE *che_context, kal_uint32 type);

/*****************************************************************************
 * FUNCTION
 *  che_deinit
 * DESCRIPTION
 *  Deinitialize the CHE_CONTEXT
 * PARAMETERS
 *  che_context  : [IN]   CHE context
 * RETURN VALUES
 *  void
 * SEE ALSO
 *  che_init
 * EXAMPLE
 * <code>
 * STCHE context;
 * che_init(&context, CHE_SHA1);
 * ...
 * che_deinit(&context);
 * </code>
 *****************************************************************************/
extern void che_deinit(STCHE *che_context);

/*****************************************************************************
 * DESCRIPTION
 * API used to set, get, delete, generate the key value.
 * If user uses this API to get the key, CHE only stores the pointer of the key.
 * Thus, the key has to be kept until the che_deinit is called. There is only 
 * One exception, in the RSA, we will allocate another space to store the key.
 * Thus, user doesn't have to care if they can release the buffer of key.
 * PARAMETERS 
 *    che_context : [IN]        che context
 *    key_act :     [IN]        key action (CHE_KEY_ACTION)
 *    key :         [IN/OUT]    key stream
 *    key_ley :     [IN]        key stream length
 * EXAMPLE
 *    <code>
 *    STCHE che_context;
 *    che_init(&che_context, CHE_MAC_MD5);
 *    che_key_action(&che_context, CHE_SET_KEY,  (kal_uint8 *) key, key_len);
 *    </code>
 *****************************************************************************/
#define che_key_action(che_context, key_act, key, key_len)                 \
                                                                           \
   ((void)TRICK_##key_act, run_##key_act##_process(che_context, key, key_len))
                                                                                                                
/*****************************************************************************
 * FUNCTION
 *  che_set_iv
 * DESCRIPTION
 *  Set initial vector for CBC or CTR or AES_F8 mode. CHE only stores the pointer address.
 *  The iv buffer should be kept by the user until the first block is computed.
 *  To avoid confusion, user can keep the buffer until the che_deinit is called.
 * PARAMETERS
 *  che_context  : [IN]   CHE context
 *  iv           : [IN]   initial vector. 
 *                        the input has to be 4-byte alignment on ARM platform
 *  iv_len       : [IN]   initial vector length
 * RETURN VALUES
 *  void
 * EXAMPLE
 * <code>
 * STCHE context;
 * che_init(&context, CHE_AES);
 *  che_set_iv(&che_context, iv, 16);
 * ...
 * che_deinit(&context);
 * </code>
 *****************************************************************************/
extern void che_set_iv(STCHE *che_context, kal_uint8 *iv, kal_uint32 iv_len);


/*****************************************************************************
 * DESCRIPTION
 *    API used to execute cipher and hash process function.
 *    Since using Marco to reduce linkage code size, please pass the "type" parameter 
 *    as the constant announced in CHE_TYPE enum. It doesn't allow passing variable.
 *    The dest buffer size should be refer to the RFC standard specification.
 *    The DES/AES in CBC/ECB mode will do the auto padding if application uses the CHE's auto padding, 
 *    the destination buffer address has to be at least source_length + 1 DES block size(8 bytes) . 
 *    Otherwise, there will be a memory corruption. If application doesn't want to use auto padding,
 *    it cannot set the last_block to "TRUE". That is to say, as long as the last_block is not set to
 *    TRUE, there will no auto padding. The auto padding CHE uses is PKCS#1 v15. This padding is standard 
 *    padding used in PCKS#5.
 *    P.S. The input and output buffer should be different buffer. Don't use the same buffer.
 *
 * PARAMETERS 
 *    che_context   : [IN]        che context
 *    type          : [IN]        che type (CHE_TYPE)
 *    mode          : [IN]        che operation mode (CHE_OPERATION_MODE)
 *    act           : [IN]        che action (CHE_ACTION)
 *    source        : [IN]        source input data buffer (it has to be 4-byte alignment for DES and AES)
 *    dest          : [OUT]       destination buffer (it has to be 4-byte alignment for DES)
 *    source_length : [IN]        source input buffer length
 *    final         : [IN]        KAL_TRUE: do the standard auto padding(PKCS#5). 
 *                                KAL_FLASE: don't do the standard auto padding(PKCS#5).
 *                                If user wants the auto padding, s/he has to set the final to TRUE in its final block
 *                                operation. On the contrary, if s/he doesn't want the auto padding, please do not set 
 *                                final to TRUE.
 * RETURN VALUES
 *    KAL_TRUE  : CHE process successful
 *    KAL_FLASE : CHE process fail
 * SEE ALSO
 *  che_process_from_file
 * EXAMPLE
 *    Symmetry Block Cipher Examples:
 *
 *    1. DES Encryption (CBC mode):
 *    The input, initial vector and destination address have to be in 4-byte alignment.
 *
 *    With auto padding:
 *    <code>
 *    STCHE che_context;
 *    che_init(&che_context, CHE_DES);
 *    che_key_action(&che_context, CHE_SET_KEY, key, key_length);
 *    che_set_iv(&che_context, init_vector, vec_length);
 *    che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
 *    ...
 *    //if the last segmemnt in source buffer is intended to be processed and we want to use the auto padding,
 *    //we set final to TRUE
 *    che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_context);
 *    </code>
 *    
 *    Without auto padding:
 *    <code>
 *    STCHE che_context;
 *    che_init(&che_context, CHE_DES);
 *    che_key_action(&che_context, CHE_SET_KEY, key, key_length);
 *    che_set_iv(&che_context, init_vector, vec_length);
 *    che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
 *    ...
 *    //if the last segmemnt in source buffer is intended to be processed and we don't want the auto padding,
 *    //we set final to FALSE
 *    che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE); 
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_context);
 *    </code>
 *    2. DES Encryption (ECB mode):
 *    With auto padding:
 *    <code>
 *    STCHE che_context;
 *    che_init(&che_context, CHE_DES);
 *    che_key_action(&che_context, CHE_SET_KEY, key, key_length);
 *    che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
 *    ...
 *    //if the last segmemnt in source buffer is intended to be processed and we want to use the auto padding,
 *    //we set final to TRUE
 *    che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_context);
 *    </code>
 *
 *    Without auto padding:
 *    <code>
 *    STCHE che_context;
 *    che_init(&che_context, CHE_DES);
 *    che_key_action(&che_context, CHE_SET_KEY, key, key_length);
 *    che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
 *    ...
 *    //if the last segmemnt in source buffer is intended to be processed and we don't want to use the 
 *    //auto padding, we set final to TRUE
 *    che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_context);
 *    </code>
 *    3. AES Encryption (Counter mode):
 *    Because this mode doesn't need padding, we don't care the "final" parameter in this mode. 
 *    Its input data can not in block boundary size.
 *    <code>
 *    STCHE che_cntx;
 *    che_init(&che_cntx, CHE_AES);
 *    che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
 *    che_set_iv(&che_cntx, iv, 16);
 *    che_process(&che_cntx, CHE_AES, CHE_CTR, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_cntx);
 *    </code>
 *
 *    4. AES Encryption (Key WRAP mode):
 *    The input data has to be in multiple of 64 bits block size. 
 *    Because this mode doesn't need padding, we don't care the "final" parameter in this mode. 
 *    Because one more block is generated for checking in KEY WRAP mode,
 *    the input and output buffer should be different one in KEY_WRAP and KEY_UNWRAP mode.
 *    <code>
 *    STCHE che_cntx;
 *    che_init(&che_cntx, CHE_AES);
 *    che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
 *    che_set_iv(&che_cntx, iv, 16);
 *    che_process(&che_cntx, CHE_AES, CHE_KEY_WRAP, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_cntx);
 *    </code>
 *
 *    5. AES Encryption (CBC mode):
 *    The input and initial vector address have to be in 4-byte alignment.
 *
 *    With auto padding:
 *    <code>
 *    STCHE che_cntx;
 *    che_init(&che_cntx, CHE_AES);
 *    che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
 *    che_set_iv(&che_cntx, iv, 16);
 *    che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
 *    //final block
 *    che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_cntx);
 *    </code>
 *
 *    Without auto padding:
 *    <code>
 *    STCHE che_cntx;
 *    che_init(&che_cntx, CHE_AES);
 *    che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
 *    che_set_iv(&che_cntx, iv, 16);
 *    che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
 *    // final block
 *    che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_cntx);
 *    </code>
 *    6. AES Encryption (ECB mode):
 *    The input address have to be in 4-byte alignment.
 *
 *    With auto padding:
 *    <code>
 *    STCHE che_cntx;
 *    che_init(&che_cntx, CHE_AES);
 *    che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
 *    che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
 *    //final block
 *    che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_cntx);
 *    </code>    
 *
 *    Without auto padding:
 *    <code>
 *    STCHE che_cntx;
 *    che_init(&che_cntx, CHE_AES);
 *    che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
 *    che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
 *    //final block
 *    che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_cntx);
 *    </code>    
 *  
 *    Symmetry Stream Cipher Example:
 *
 *    1. RC4 Encryption:
 *    Because this mode doesn't need padding, we don't care the "final" parameter in this mode. 
 *
 *    Use default RC4 mode:
 *    <code>
 *    STCHE che_ctx;
 *    che_init(&che_ctx, CHE_RC4);    
 *    che_key_action(&che_ctx, CHE_SET_KEY, (kal_uint8 *)key, key_len);
 *    che_process(&che_ctx, CHE_RC4, CHE_MODE_NULL, CHE_ENC, (kal_uint8 *)input, 
 *              (kal_uint8 *)output, (kal_int32)input_size, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_ctx);
 *    </code>
 *
 *    Use CHE_SKIP mode:
 *    skip the first 256 byts key data because first 256 bytes are not random enough.
 *    <code>
 *    STCHE che_ctx;
 *    che_init(&che_ctx, CHE_RC4);    
 *    che_key_action(&che_ctx, CHE_SET_KEY, (kal_uint8 *)key, key_len);
 *    che_process(&che_ctx, CHE_RC4, CHE_SKIP, CHE_ENC, (kal_uint8 *)input, 
 *              (kal_uint8 *)output, (kal_int32)input_size, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all cipher operation is finish
 *    che_deinit(&che_ctx);
 *    </code>
 *
 *    Hash Operation:
 *
 *    1. MD5 Hash generation:
 *    we have to set the final block to TRUE for hash operation. The mode field is useless for hash algorithm.
 *    HASH doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
 *
 *    <code>
 *    STCHE che_context;
 *    kal_uint8 digest[16];
 *    che_init(&che_context, CHE_MD5); 
 *    che_process(&che_context,CHE_MD5,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_FALSE);
 *    ...
 *    che_process(&che_context,CHE_MD5,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_TRUE);
 *    // do other operation
 *    ...
 *    //Deinitialize when all hash operation is finish
 *    che_deinit(&che_context);
 *    </code>
 *
 *    2. SHA1 Hash generation:
 *    we have to set the final block to TRUE for hash operation. The mode field is useless for hash algorithm.
 *    HASH doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
 *
 *    <code>
 *    STCHE che_context;
 *    kal_uint8 digest[20];
 *    che_init(&che_context, CHE_SHA1); 
 *    che_process(&che_context,CHE_SHA1,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_FALSE);
 *    ...
 *    che_process(&che_context,CHE_SHA1,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_TRUE);
 *    che_deinit(&che_context);
 *    // do other operation
 *    ...
 *    //Deinitialize when all hash operation is finish
 *    </code>    
 *
 *    3. HMAC MD5 generation:
 *    we have to set the final block to TRUE for HMAC operation because HMAC doesn't have padding consideartion for last block. 
 *    The mode field is useless for HMAC algorithm. HMAC doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
 *
 *    <code>
 *    STCHE che_context;
 *    kal_uint8 digest[16];
 *    che_init(&che_context, CHE_MAC_MD5);
 *    che_key_action(&che_context, CHE_SET_KEY,  (kal_uint8 *) key, key_len);
 *    che_process(&che_context, CHE_MAC_MD5, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_FALSE);
 *    ...
 *    che_process(&che_context, CHE_MAC_MD5, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_TRUE);    
 *    // do other operation
 *    ...
 *    //Deinitialize when all hash operation is finish
 *    che_deinit(&che_context);
 *    </code>
 *
 *    4. SHA2 Hash generation:
 *    we have to set the final block to TRUE for hash operation. The mode field is useless for hash algorithm.
 *    HASH doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
 *
 *    <code>
 *    STCHE che_context;
 *    kal_uint8 digest[32];
 *    che_init(&che_context, CHE_SHA256);
 *    che_process(&che_context, CHE_SHA256, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_FALSE);
 *    ...
 *    che_process(&che_context, CHE_SHA256, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_TRUE);    
 *    // do other operation
 *    ...
 *    //Deinitialize when all hash operation is finish
 *    che_deinit(&che_context);
 *    </code>
 *
 *    Public Key or Key Agreement:
 *
 *    1. Diffie-Hellman: 
 *    The source and dest parameter is useless for DH. 
 *    The output data is get through che_key_action.
 *    <code>
 *    STCHE che_ctx_dh;
 *    kal_uint32 dh_secret_len;
 *    kal_uint8 *dh_secret_key = NULL;
 *
 *    che_init(&che_ctx_dh, CHE_DH);
 *    che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_INIT, NULL, NULL, 0, KAL_TRUE);
 *    // set the p and g. p and g must in binary format
 *    che_key_action(&che_ctx_dh, CHE_SET_DH_P, (kal_uint8*) P, P_Len);
 *    che_key_action(&che_ctx_dh, CHE_SET_DH_G, G, G_Len);
 *    // Compute self public B = (g^b mod p) and private (b)
 *    che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_MAKE_PARAMS, NULL, NULL, 0, KAL_TRUE);
 *    // Set the peer public key (A) in binary format
 *    che_key_action(&che_ctx_dh, CHE_SET_DH_PEER_PUBLIC, (kal_uint8*)peer_public, peer_public_len);
 *    // calculate the shared secret: K
 *    che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_CALC_SECRET, NULL, NULL, 0, KAL_TRUE);
 *    //get the shared secret key
 *    che_key_action(&che_ctx_dh, CHE_GET_DH_SECRET_LEN, (void*)&dh_secret_len, sizeof(int));
 *    dh_secret_key = malloc(dh_secret_len);
 *    che_key_action(&che_ctx_dh, CHE_GET_DH_SECRET, (kal_uint8*) dh_secret_key, dh_secret_len);
 *    //call deinit when all operations are finish
 *    che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_FREE, NULL, NULL, 0, KAL_TRUE);
 *    che_deinit(&che_ctx_dh);
 *    </code>
 *****************************************************************************/
#define che_process(che_context, type, mode, act, source, dest, source_length, final)                       \
                                                                                                                 \
   ((void)TRICK_##type, run_##type##_process(che_context, type, mode, act, source, dest, source_length, final, 0))
                                                                                                                
/*****************************************************************************
 * DESCRIPTION
 *    API used to do the CHE computation. The input message is from the "file".
 *    The usage is the same as che_process except the source of input data.
 *    For detail algorithm usage, please also take the reference of "che_process"
 * PARAMETERS 
 *    che_context    : [IN]        che context
 *    type           : [IN]        che type (CHE_TYPE)
 *    mode           : [IN]        che operation mode (CHE_OPERATION_MODE)
 *    act            : [IN]        che action (CHE_ACTION)
 *    pstFSAL        : [IN]        source file handler
 *    dest          : [OUT]       destination buffer (it has to be 4-byte alignment for DES)
 * RETURN VALUES
 *    KAL_TRUE  : CHE process successful
 *    KAL_FLASE : CHE process fail
 * SEE ALSO
 *  che_process
 * EXAMPLE
 *    1. DES Encryption (CBC mode):
 *    The initial vector and destination address have to be in 4-byte alignment.
 *   
 *    <code>
 *    STFSAL pstFSAL;
 *    kal_uint8 pbBuf[512];
 *    STCHE che_str;
 *    che_init(&che_str, CHE_DES); 
 *    che_key_action(&che_str, CHE_SET_KEY, key, key_length);
 *    che_set_iv(&che_str, init_vector, vec_length);
 *    FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
 *    FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
 *    if (che_process_from_file(&che_str, CHE_DES, CHE_CBC, CHE_ENC,
 *                              &pstFSAL, dst_buf) == KAL_FALSE)
 *    {
 *        FSAL_Close(&pstFSAL);
 *        che_deinit(&che_str);
 *        return ERROR;
 *    }
 *    ...
 *    FSAL_Close(&pstFSAL);
 *    // do other operations
 *    ...
 *    //Deinitialize when all operations are finish
 *    che_deinit(&che_str);
 *    </code>
 *    
 *    2. DES Encryption (ECB mode):
 *    <code>
 *    STFSAL pstFSAL;
 *    kal_uint8 pbBuf[512];
 *    STCHE che_str;
 *    che_init(&che_str, CHE_DES); 
 *    che_key_action(&che_str, CHE_SET_KEY, key, key_length);
 *    FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
 *    FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
 *    if (che_process_from_file(&che_str, CHE_DES, CHE_ECB, CHE_ENC,
 *                              &pstFSAL, dst_buf) == KAL_FALSE)
 *    {
 *        FSAL_Close(&pstFSAL);
 *        che_deinit(&che_str);
 *        return ERROR;
 *    }
 *    ...
 *    FSAL_Close(&pstFSAL);
 *    // do other operations
 *    ...
 *    //Deinitialize when all operations are finish
 *    che_deinit(&che_str);
 *    </code>
 *
 *    3. AES Encryption (CBC mode):
 *    <code>
 *    STFSAL pstFSAL;
 *    kal_uint8 pbBuf[512];
 *    STCHE che_str;
 *    che_init(&che_str, CHE_AES); 
 *    che_key_action(&che_str, CHE_SET_KEY, key, key_length);
 *    che_set_iv(&che_str, init_vector, vec_length);
 *    FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
 *    FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
 *    if (che_process_from_file(&che_str, CHE_AES, CHE_CBC, CHE_ENC,
 *                              &pstFSAL, dst_buf) == KAL_FALSE)
 *    {
 *        FSAL_Close(&pstFSAL);
 *        che_deinit(&che_str);
 *        return ERROR;
 *    }
 *    ...
 *    FSAL_Close(&pstFSAL);
 *    // do other operations
 *    ...
 *    //Deinitialize when all operations are finish
 *    che_deinit(&che_str);
 *    </code>
 *
 *    4. MD5 Hash generation:
 *    <code>
 *    STFSAL pstFSAL;
 *    kal_uint8 pbBuf[512];
 *    STCHE che_str;
 *    kal_uint8 digest[16];
 *    che_init(&che_str, CHE_MD5); 
 *    FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
 *    FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
 *    if (che_process_from_file(&che_str, CHE_MD5, CHE_MODE_NULL, CHE_HASH,
 *                              &pstFSAL, digest) == KAL_FALSE)
 *    {
 *        FSAL_Close(&pstFSAL);
 *        che_deinit(&che_str);
 *        return ERROR;
 *    }
 *    ...
 *    FSAL_Close(&pstFSAL);
 *    // do other operations
 *    ...
 *    //Deinitialize when all operations are finish
 *    che_deinit(&che_str);
 *    </code>
 *
 *    5. HMAC MD5 generation:
 *    <code>
 *    STFSAL pstFSAL;
 *    kal_uint8 pbBuf[512];
 *    STCHE che_str;
 *    kal_uint8 digest[16];
 *    che_init(&che_str, CHE_MAC_MD5); 
 *    che_key_action(&che_context, CHE_SET_KEY,  (kal_uint8 *) key, key_len);
 *    FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
 *    FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
 *    if (che_process_from_file(&che_str, CHE_MAC_MD5, CHE_MODE_NULL, CHE_HASH,
 *                              &pstFSAL, digest) == KAL_FALSE)
 *    {
 *        FSAL_Close(&pstFSAL);
 *        che_deinit(&che_str);
 *        return ERROR;
 *    }
 *    ...
 *    FSAL_Close(&pstFSAL);
 *    // do other operations
 *    ...
 *    //Deinitialize when all operations are finish
 *    che_deinit(&che_str);
 *    </code>
 *   
 *    6. Diffie-Hellman: (key = (G^Y)^X mod P)
 *    get the discrete logarithms (X) and compute self public value (G^X mod P)
 *    <code>
 *    STFSAL pstFSAL;
 *    kal_uint8 pbBuf[512];
 *    STCHE che_str;
 *    che_init(&che_str, CHE_DH);
 *    che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_INIT, NULL, NULL, 0, KAL_TRUE);
 *    // set the P and G that P and G must in binary format
 *    che_key_action(&che_ctx_dh, CHE_SET_DH_P, (kal_uint8*) P, P_Len);
 *    che_key_action(&che_ctx_dh, CHE_SET_DH_G, G, G_Len);
 *    // make self public (G^X mod P) and private (X)
 *    che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_MAKE_PARAMS, NULL, NULL, 0, KAL_TRUE);
 *    // get the self public
 *    che_key_action(&che_ctx_dh, CHE_GET_DH_SELF_PUBLIC_LEN, (void*)&self_public_len, sizeof(int));
 *    // get the self public
 *    *self_private = malloc(self_private_len);
 *    che_key_action(&che_ctx_dh, CHE_GET_DH_PRIVATE, (kal_uint8*) (*priv)->v, dh_private_len);
 *    FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
 *    FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
 *    if (che_process_from_file(&che_str, CHE_MD5, CHE_MODE_NULL, CHE_ENC,
 *                              &pstFSAL, digest) == KAL_FALSE)
 *    {
 *        FSAL_Close(&pstFSAL);
 *        che_deinit(&che_str);
 *        return ERROR;
 *    }
 *    ...
 *    FSAL_Close(&pstFSAL);
 *    // do other operations
 *    ...
 *    //Deinitialize when all operations are finish
 *    che_deinit(&che_str);
 *    </code>
 *****************************************************************************/
#define che_process_from_file(che_context, type, mode, act, pstFSAL, dest)                                      \
                                                                                                                \
   ((void)TRICK_##type, run_##type##_process(che_context, type, mode, act, 0, dest, 0, KAL_FALSE, pstFSAL))                    

/*****************************************************************************
 * FUNCTION
 *  che_duplicate
 * DESCRIPTION
 *  Duplicate the CHE structure and current content. 
 *  This API only supports CHE_MD5, CHE_SHA1, CHE_MD4, CHE_MAC_MD5, CHE_MAC_SHA1.
 *  The situation application may encounter and wish to use this API:
 *                   packet1
 *  Step 1: A ------------------------->  B  (B receives packet1 and generates the hash value)
 *                   packet2
 *  Step 2: A ------------------------->  B  (B receives another packet2 and generates the hash value again)
 *  
 *  In this case, application may wish the result of step 2 is the continue of step 1. 
 *  To fulfill user's need, we provide this API for user to store the temporary CHE cotext for
 *  later use.
 * PARAMETERS
 *  src_che_context  : [IN]   source CHE context
 *  dst_che_context  : [IN]   destination CHE context 
 *                       
 * RETURN VALUES
 *  KAL_TRUE : success
 *  KAL_FALSE : fail
 * EXAMPLE
 * <code>
 * STCHE dst_context;
 * che_duplicate(&dst_context, &src_context);
 * </code>
 *****************************************************************************/
extern kal_bool che_duplicate(STCHE *src_che_context, STCHE *dst_che_context);

/*****************************************************************************
 * FUNCTION
 *  che_rsa_generate_key
 * DESCRIPTION
 *  genereate RSA key by public exponent (e).
 * PARAMETERS
 *  ctx      : [IN]     CHE context
 *  nbits   : [IN]   number of bits in the modulus
 *  exponent :[IN] public exponent value
 *  f_rng       :[IN] random number generator function
 *  p_rng      :[IN] input parameter for random number generator function
 *                       
 * RETURN VALUES
 *  CHE_OK : success
 *  CHE_ERROR : parameter error
 *  CHE_OPERATION_FAIL : algorithm operation is failed
 * EXAMPLE
 * <code>
 *     kal_int32 ret;
 *     STCHE * che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     ret = che_rsa_generate_key(&che_ctx, 1024, 37, random_gen_func_p, random_para);
 *     if (ret == CHE_OK)
 *     {
 *         // generate key successfully
 *         // can get key from che_ctx
 *         
 *     }
 * </code>
 *****************************************************************************/
extern kal_int32 che_rsa_generate_key( STCHE *ctx, kal_int32 nbits, kal_int32 exponent,  kal_int32 (*f_rng)(void *), void *p_rng);

/*****************************************************************************
 * FUNCTION
 *  che_rsa_public_encrypt
 * DESCRIPTION
 *  This API is used when we want to use public key to encrypt the data.
 *  Application can specify the way of padding based PKCS#1 standard.
 *  If application doesn't want to do the padding, s/he can set the padding to 
 *  CHE_RSA_MODE_NULL.
 *
 *  Based on PKCS#1v1.5 standard, the smallest padding size is 11 bytes.
 *  Thus, the destination buffer should be:
 *  Destination_buffer_len = input_data_len + 11(smallest padding data)
 *  In addition, the output buffer size is equal to the size of N.
 *  If the input data length is too large or destination buffer is too small,
 *  the error cause will be returned. The output buffer length will be checked only
 *  If the passed in length is not 0. If it is 0, this means user doesn't want to check
 *  the destination buffer length.
 *  The input and output buffer can be the same one.
 *
 *  RSA valid public key length:
 *  128 bits < Modulus (N) < 4096 bits
 *  2 bits < public exponent (E) < 64  bits
 * PARAMETERS
 *  ctx      : [IN]     CHE context
 *  src      : [IN]     source buffer
 *  src_len  : [IN]     source buffer length (the length sould < length of N)
 *  dst      : [OUT]    destination buffer (must pass in)
 *  dst_len  : [IN/OUT] encrypted destination length. If user pass in non zero value,
 *                      CHE will do the destination buffer length check. If the length
 *                      smaller than the length of N. The error is returned. However,
 *                      If user pass in value 0, che will not do the length checking.
 *  padding  : [IN]     the way of padding. Currently we only support PKCS#1v1.5 padding.
 *                      If user doesn't want any padding, s/he can pass in CHE_RSA_MODE_NULL.
 *                      Please take reference of the enum che_rsa_padding_mode_enum
 *                       
 * RETURN VALUES
 *  take the reference of che_result_error_enum.
 *  CHE_OK : success
 *  CHE_ERROR : parameter error
 *  CHE_KEY_CHK_FAIL : input key is not correct
 *  CHE_INVALID_LENGTH : the input data length is too large or 
 *                       destination buffer is too small
 *  CHE_OPERATION_FAIL : algorithm operation is failed
 * SEE ALSO
 *  che_rsa_private_decrypt
 * EXAMPLE
 *  (a). Encrypt the data by public key without padding
 *     <code>
 *     STCHE * che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set public Exponent: E
 *     che_key_action(&che_ctx, CHE_SET_RSA_E, pubExp, e_len);
 *     //Encrypt Data by the public key
 *     che_rsa_public_encrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_MODE_NULL);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *
 *  (b). Encrypt the data by public key with PKCS#1v1.5 padding
 *     <code>
 *     STCHE * che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set public Exponent: E
 *     che_key_action(&che_ctx, CHE_SET_RSA_E, pubExp, e_len);
 *     //Encrypt Data by the public key
 *     che_rsa_public_encrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_PKCS1_V15);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *****************************************************************************/
extern kal_int32 che_rsa_public_encrypt(STCHE *ctx, kal_uint8 *src, kal_uint32 src_len, kal_uint8 *dst, kal_uint32 *dst_len, che_rsa_padding_mode_enum padding);

/*****************************************************************************
 * FUNCTION
 *  che_rsa_private_encrypt
 * DESCRIPTION
 *  This API is used when we want to use private key to encrypt the data.
 *  Application can specify the way of padding based PKCS#1 standard.
 *  If users don't want to do the padding, they can set the padding to 
 *  CHE_RSA_MODE_NULL.
 *
 *  Based on PKCS#1v1.5 standard, the smallest padding size is 11 bytes.
 *  Thus, the destination buffer should be:
 *  Destination_buffer_len = input_data_len + 11(smallest padding data)
 *  In addition, the output buffer size is equal to the size of N.
 *  If the input data length is too large or destination buffer is too small,
 *  the error cause will be returned. The output buffer length will be checked only
 *  If the passed in length is not 0. If it is 0, this means user doesn't want to check
 *  the destination buffer length.
 *  The input and output buffer can be the same one.
 *
 * PARAMETERS
 *  ctx      : [IN]     CHE context
 *  src      : [IN]     source buffer
 *  src_len  : [IN]     source buffer length (the length sould < length of N)
 *  dst      : [OUT]    destination buffer (must pass in)
 *  dst_len  : [IN/OUT] encrypted destination length. If user pass in non zero value,
 *                      CHE will do the destination buffer length check. If the length
 *                      smaller than the length of N. The error is returned. However,
 *                      If user pass in value 0, che will not do the length checking.
 *  padding  : [IN]     the way of padding. Currently we only support PKCS#1v1.5 padding.
 *                      If users don't want any padding, they can pass in CHE_RSA_MODE_NULL.
 *                      (che_rsa_padding_mode_enum)
 *                       
 * RETURN VALUES
 *  take the reference of che_result_error_enum.
 *  CHE_OK : success
 *  CHE_ERROR : parameter error
 *  CHE_KEY_CHK_FAIL : input key is not correct
 *  CHE_INVALID_LENGTH : the input data length is too large or 
 *                       destination buffer is too small
 *  CHE_OPERATION_FAIL : algorithm operation is failed
 * SEE ALSO
 *  che_rsa_public_decrypt
 * EXAMPLE
 *  (a). Encrypt the data by private key without padding
 *   We set the mode to CHE_RSA_MODE_NULL to indicate that we don't 
 *   encode the input data with any padding method
 *     <code>
 *     STCHE che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set private key: D
 *     che_key_action(&che_ctx, CHE_SET_RSA_D, privD, d_len);
 *     //Encrypt Data by the private key
 *     che_rsa_private_encrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_MODE_NULL);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *
 *  (b). Encrypt the data by private key with PKCS#1v1.5 padding
 *     <code>
 *     STCHE che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set private key: D
 *     che_key_action(&che_ctx, CHE_SET_RSA_D, privD, d_len);
 *     //Encrypt Data by the private key
 *     che_rsa_private_encrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_PKCS1_V15);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *
 *   (c). Encryption the data by the private key (CRT format) with PKCS1#v15 padding
 *    <code>
 *     STCHE che_ctx;
 *    // initialize
 *    che_init(&che_ctx, CHE_RSA);
 *    // set modulus: N
 *    che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *    // set P for private key checking
 *    che_key_action(&che_ctx, CHE_SET_RSA_P, (kal_uint8 *)primeP, P_len);
 *    // set Q for private key checking
 *    che_key_action(&che_ctx, CHE_SET_RSA_Q, (kal_uint8 *)primeQ, q_len);
 *    // Set private exponent: d mod (p-1)
 *    che_key_action(&che_ctx, CHE_SET_RSA_dModPm1, dModPm1, dp_len);
 *    // Set private exponent: d mod (q-1)
 *    che_key_action(&che_ctx, CHE_SET_RSA_dModQm1, dModQm1, 32);
 *    // Set coefficient Integer: inverse of q mod p
 *    che_key_action(&che_ctx, CHE_SET_RSA_qInvModP, qInvModP, 32);
 *    //Encrypt Data by the private key
 *    che_rsa_private_encrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_PKCS1_V15);
 *    // do other operation
 *    ...
 *    //Deinitialize when all RSA operation is finish
 *    che_deinit(&che_ctx);
 *    </code>
 *****************************************************************************/
extern kal_int32 che_rsa_private_encrypt(STCHE *ctx, kal_uint8 *src, kal_uint32 src_len, kal_uint8 *dst, kal_uint32 *dst_len, che_rsa_padding_mode_enum padding);

/*****************************************************************************
 * FUNCTION
 *  che_rsa_public_decrypt
 * DESCRIPTION
 *  This API is used when we want to use public key to decrypt the data.
 *  Application can specify the way of padding based PCKS#1 standard.
 *  If users don't want to do the padding, they can set the padding to 
 *  CHE_RSA_MODE_NULL.
 *  If the input data is too large (> size of N), the error will be returned.
 *  If the input data is too small ( < smallest padding length(11)), 
 *  the error will be returned as well. 
 *  The input and output buffer can be the same one.
 *
 *  RSA valid public key length:
 *  128 bits < Modulus (N) < 4096 bits
 *  2 bits < public exponent (E) < 64  bits
 * PARAMETERS
 *  ctx      : [IN]     CHE context
 *  src      : [IN]     source buffer
 *  src_len  : [IN]     source buffer length (the length sould < length of N)
 *  dst      : [OUT]    destination buffer (must pass in)
 *  dst_len  : [IN/OUT] encrypted destination length. If user pass in non zero value,
 *                      CHE will do the destination buffer length check. If the length
 *                      smaller than the length of N. The error is returned. However,
 *                      If user pass in value 0, che will not do the length checking.
 *  padding  : [IN]     the way of padding. Currently we only support PKCS#1v1.5 padding.
 *                      If users don't want any padding, they can pass in CHE_RSA_MODE_NULL.
 *                      (che_rsa_padding_mode_enum)
 *                       
 * RETURN VALUES
 *  take the reference of che_result_error_enum.
 *  CHE_OK : success
 *  CHE_ERROR : parameter error
 *  CHE_KEY_CHK_FAIL : input key is not correct
 *  CHE_INVALID_LENGTH : input data is too small or too long
 *  CHE_OPERATION_FAIL : algorithm operation is failed
 * SEE ALSO
 *  che_rsa_private_encrypt
 * EXAMPLE
 *  (a). Decrypt the data by public key without padding
 *     <code>
 *     STCHE che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set public Exponent: E
 *     che_key_action(&che_ctx, CHE_SET_RSA_E, pubExp, e_len);
 *     //Decrypt Data by the public key
 *     che_rsa_public_decrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_MODE_NULL);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *
 *  (b). Decrypt the data by public key with PKCS#1v1.5 padding
 *     <code>
 *     STCHE che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set public Exponent: E
 *     che_key_action(&che_ctx, CHE_SET_RSA_E, pubExp, e_len);
 *     //Decrypt Data by the public key
 *     che_rsa_public_decrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_PKCS1_V15);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *****************************************************************************/
extern kal_int32 che_rsa_public_decrypt(STCHE *ctx, kal_uint8 *src, kal_uint32 src_len, kal_uint8 *dst, kal_uint32 *dst_len, che_rsa_padding_mode_enum padding);

/*****************************************************************************
 * FUNCTION
 *  che_rsa_private_decrypt
 * DESCRIPTION
 *  This API is used when we want to use private key to decrypt the data.
 *  Application can specify the way of padding based PCKS#1 standard.
 *  If users don't want to do the padding, they can set the padding to 
 *  CHE_RSA_MODE_NULL.
 *
 *  If the input data is too large (> size of N), the error will be returned.
 *  If the input data is too small ( < smallest padding length(11)), 
 *  the error will be returned as well. 
 *  The input and output buffer can be the same one.
 *
 * PARAMETERS
 *  ctx      : [IN]     CHE context
 *  src      : [IN]     source buffer
 *  src_len  : [IN]     source buffer length (the length sould < length of N)
 *  dst      : [OUT]    destination buffer (must pass in)
 *  dst_len  : [IN/OUT] encrypted destination length. If user pass in non zero value,
 *                      CHE will do the destination buffer length check. If the length
 *                      smaller than the length of N. The error is returned. However,
 *                      If user pass in value 0, che will not do the length checking.
 *  padding  : [IN]     the way of padding. Currently we only support PKCS#1v1.5 padding.
 *                      If users don't want any padding, they can pass in CHE_RSA_MODE_NULL.
 *                      (che_rsa_padding_mode_enum)
 *                       
 * RETURN VALUES
 *  take the reference of che_result_error_enum.
 *  CHE_OK : success
 *  CHE_ERROR : parameter error
 *  CHE_KEY_CHK_FAIL : input key is not correct
 *  CHE_INVALID_LENGTH : input data is too small or too long
 *  CHE_OPERATION_FAIL : algorithm operation is failed
 * SEE ALSO
 *  che_rsa_public_encrypt
 * EXAMPLE
 *  (a). Decrypt the data by private key without padding
 *   We set the mode to CHE_RSA_MODE_NULL to indicate that we don't encode the input data with any padding method
 *     <code>
 *     STCHE che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set private key: D
 *     che_key_action(&che_ctx, CHE_SET_RSA_D, privD, d_len);
 *     //Decrypt Data by the private key
 *     che_rsa_private_decrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_MODE_NULL);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *
 *  (b). Decrypt the data by private key with PKCS#1v1.5 padding
 *     <code>
 *     STCHE che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     //Set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     //Set private key: D
 *     che_key_action(&che_ctx, CHE_SET_RSA_D, privD, d_len);
 *     //decrypt Data by the private key
 *     che_rsa_private_decrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_PKCS1_V15);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *
 *  (c). Decrypt the data by private key (CRT format) with PKCS#1v1.5 padding
 *     <code>
 *     STCHE che_ctx;
 *     //initialize
 *     che_init(&che_ctx, CHE_RSA);
 *     // set modulus: N
 *     che_key_action(&che_ctx, CHE_SET_RSA_N, modulusN, n_len);
 *     // set P for private key checking
 *     che_key_action(&che_ctx, CHE_SET_RSA_P, (kal_uint8 *)primeP, P_len);
 *     // set Q for private key checking
 *     che_key_action(&che_ctx, CHE_SET_RSA_Q, (kal_uint8 *)primeQ, q_len);
 *     // Set private exponent: d mod (p-1)
 *     che_key_action(&che_ctx, CHE_SET_RSA_dModPm1, dModPm1, dp_len);
 *     // Set private exponent: d mod (q-1)
 *     che_key_action(&che_ctx, CHE_SET_RSA_dModQm1, dModQm1, 32);
 *     // Set coefficient Integer: inverse of q mod p
 *     che_key_action(&che_ctx, CHE_SET_RSA_qInvModP, qInvModP, 32);
 *     //decrypt Data by the private key
 *     che_rsa_private_decrypt(&che_ctx, src, src_len, dst, &dst_len, CHE_RSA_PKCS1_V15);
 *     // do other operation
 *     ...
 *     //Deinitialize when all RSA operation is finish
 *     che_deinit(&che_ctx);
 *     </code>
 *****************************************************************************/
extern kal_int32 che_rsa_private_decrypt(STCHE *ctx, kal_uint8 *src, kal_uint32 src_len, kal_uint8 *dst, kal_uint32 *dst_len, che_rsa_padding_mode_enum padding);

#endif /* _CHE_API_H */ 

/*
 * ! \page page3 Reference
 * 
 * 1) FEDERAL INFORMATION PROCESSING STANDARDS PUBLICATION U.S. DEPARTMENT OF COMMERCE/national Institute of Standards and Technology 46-3 : DATA ENCRYPTION STANDARD (DES)\n
 * 2) FEDERAL INFORMATION  PROCESSING STANDARDS PUBLICATION 197 : ADVANCED ENCRYPTION STANDARD (AES)\n
 * 3) FEDERAL INFORMATION  PROCESSING STANDARDS PUBLICATION 180-2 SECURE HASH STANDARD (SHA)\n
 * 4) MIT Laboratory for Computer Science and RSA Data Security, Inc. : The MD5 Message-Digest Algorithm\n
 * 
 */