Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /********************************************************************* |
2 | * |
||
3 | * RSA Public Key Encryption Library Header |
||
4 | * |
||
5 | ********************************************************************* |
||
6 | * FileName: RSA.h |
||
7 | * Dependencies: BigInt.h |
||
8 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32 |
||
9 | * Compiler: Microchip C32 v1.05 or higher |
||
10 | * Microchip C30 v3.12 or higher |
||
11 | * Microchip C18 v3.30 or higher |
||
12 | * Company: Microchip Technology, Inc. |
||
13 | * |
||
14 | * Software License Agreement |
||
15 | * |
||
16 | * Copyright (C) 2002-2009 Microchip Technology Inc. All rights |
||
17 | * reserved. |
||
18 | * |
||
19 | * Microchip licenses to you the right to use, modify, copy, and |
||
20 | * distribute: |
||
21 | * (i) the Software when embedded on a Microchip microcontroller or |
||
22 | * digital signal controller product ("Device") which is |
||
23 | * integrated into Licensee's product; or |
||
24 | * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
25 | * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device |
||
26 | * used in conjunction with a Microchip ethernet controller for |
||
27 | * the sole purpose of interfacing with the ethernet controller. |
||
28 | * |
||
29 | * You should refer to the license agreement accompanying this |
||
30 | * Software for additional information regarding your rights and |
||
31 | * obligations. |
||
32 | * |
||
33 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT |
||
34 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
35 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
36 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
37 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
38 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
39 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
40 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
41 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
42 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
43 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
44 | * |
||
45 | * IMPORTANT: The implementation and use of third party algorithms, |
||
46 | * specifications and/or other technology may require a license from |
||
47 | * various third parties. It is your responsibility to obtain |
||
48 | * information regarding any applicable licensing obligations. |
||
49 | * |
||
50 | * |
||
51 | * Author Date Comment |
||
52 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
53 | * Elliott Wood 2/14/07 Original |
||
54 | * Elliott Wood 10/15/07 Modified API to support both ops |
||
55 | ********************************************************************/ |
||
56 | |||
57 | #ifndef __RSA_H |
||
58 | #define __RSA_H |
||
59 | |||
60 | #define RSA_KEY_WORDS (SSL_RSA_KEY_SIZE/BIGINT_DATA_SIZE) // Represents the number of words in a key |
||
61 | #define RSA_PRIME_WORDS (SSL_RSA_KEY_SIZE/BIGINT_DATA_SIZE/2) // Represents the number of words in an RSA prime |
||
62 | |||
63 | /**************************************************************************** |
||
64 | Section: |
||
65 | State Machines and Status Codes |
||
66 | ***************************************************************************/ |
||
67 | |||
68 | // State machine for RSA processes |
||
69 | typedef enum |
||
70 | { |
||
71 | SM_RSA_IDLE = 0u, // Data is being initialized by the application |
||
72 | SM_RSA_ENCRYPT_START, // Initial state for encryption processes; encryption is ready to begin |
||
73 | SM_RSA_ENCRYPT, // RSA encryption is proceeding |
||
74 | SM_RSA_DECRYPT_START, // Initial state for decryption processes; decryption is ready to begin |
||
75 | SM_RSA_DECRYPT_FIND_M1, // First stage in the CRT decryption algorithm |
||
76 | SM_RSA_DECRYPT_FIND_M2, // Second stage in the CRT decryption algorithm |
||
77 | SM_RSA_DECRYPT_FINISH, // CRT values have been calculated, so finalize the operation |
||
78 | SM_RSA_DONE // RSA process is complete |
||
79 | } SM_RSA; |
||
80 | |||
81 | // Status response from RSA procedures |
||
82 | typedef enum |
||
83 | { |
||
84 | RSA_WORKING = 0u, // RSA is working through a calculation |
||
85 | RSA_FINISHED_M1, // RSA decryption has just completed calculation of the M1 CRT value |
||
86 | RSA_FINISHED_M2, // RSA decryption has just completed calculation of the M2 CRT value |
||
87 | RSA_DONE // The RSA calculation is complete |
||
88 | } RSA_STATUS; |
||
89 | |||
90 | // Indicates the data format for any RSA integer |
||
91 | typedef enum |
||
92 | { |
||
93 | RSA_BIG_ENDIAN = 0u, // Data expressed with the most significant byte first |
||
94 | RSA_LITTLE_ENDIAN // Data expressed with the least significant byte first |
||
95 | } RSA_DATA_FORMAT; |
||
96 | |||
97 | // Indicates the RSA operation to be completed |
||
98 | typedef enum |
||
99 | { |
||
100 | RSA_OP_ENCRYPT = 0u, // This is an encryption procedure |
||
101 | RSA_OP_DECRYPT // This is a decryption procedure |
||
102 | } RSA_OP; |
||
103 | |||
104 | /**************************************************************************** |
||
105 | Section: |
||
106 | Function Prototypes |
||
107 | ***************************************************************************/ |
||
108 | |||
109 | void RSAInit(void); |
||
110 | BOOL RSABeginUsage(RSA_OP op, BYTE vKeyByteLen); |
||
111 | void RSAEndUsage(void); |
||
112 | void RSASetData(BYTE* data, BYTE len, RSA_DATA_FORMAT format); |
||
113 | void RSASetResult(BYTE* data, RSA_DATA_FORMAT format); |
||
114 | RSA_STATUS RSAStep(void); |
||
115 | |||
116 | #if defined(STACK_USE_RSA_ENCRYPT) |
||
117 | #define RSABeginEncrypt(a) RSABeginUsage(RSA_OP_ENCRYPT, a) |
||
118 | #define RSAEndEncrypt() RSAEndUsage() |
||
119 | void RSASetE(BYTE* data, BYTE len, RSA_DATA_FORMAT format); |
||
120 | void RSASetN(BYTE* data, RSA_DATA_FORMAT format); |
||
121 | #endif |
||
122 | |||
123 | #if defined(STACK_USE_RSA_DECRYPT) |
||
124 | #define RSABeginDecrypt() RSABeginUsage(RSA_OP_DECRYPT, SSL_RSA_KEY_SIZE/8) |
||
125 | #define RSAEndDecrypt() RSAEndUsage() |
||
126 | #endif |
||
127 | |||
128 | |||
129 | /**************************************************************************** |
||
130 | Section: |
||
131 | BigInt Function Selection |
||
132 | Determines which BigInt functions to compile. |
||
133 | ***************************************************************************/ |
||
134 | |||
135 | #if defined(STACK_USE_RSA_ENCRYPT) |
||
136 | #define BI_USE_CONSTRUCTOR |
||
137 | #define BI_USE_ZERO |
||
138 | #define BI_USE_MOD |
||
139 | #define BI_USE_COMPARE |
||
140 | #define BI_USE_MAG_DIFF |
||
141 | #define BI_USE_MAG |
||
142 | #define BI_USE_MSB |
||
143 | #define BI_USE_MULTIPLY |
||
144 | #define BI_USE_SQUARE |
||
145 | #define BI_USE_COPY |
||
146 | #endif |
||
147 | |||
148 | #if defined(STACK_USE_RSA_DECRYPT) |
||
149 | #define BI_USE_CONSTRUCTOR |
||
150 | #define BI_USE_ZERO |
||
151 | #define BI_USE_COMPARE |
||
152 | #define BI_USE_MAG |
||
153 | #define BI_USE_MSB |
||
154 | #define BI_USE_MULTIPLY |
||
155 | #define BI_USE_SQUARE |
||
156 | #define BI_USE_ADD |
||
157 | #define BI_USE_SUBTRACT |
||
158 | #define BI_USE_COPY |
||
159 | |||
160 | #if defined(__18CXX) |
||
161 | #define BI_USE_CONSTRUCTOR_ROM |
||
162 | #define BI_USE_COPY_ROM |
||
163 | #define BI_USE_MOD_ROM |
||
164 | #define BI_USE_COMPARE_ROM |
||
165 | #define BI_USE_MAG_DIFF_ROM |
||
166 | #define BI_USE_MAG_ROM |
||
167 | #define BI_USE_MULTIPLY_ROM |
||
168 | #else |
||
169 | #define BI_USE_MAG_DIFF |
||
170 | #define BI_USE_MOD |
||
171 | #endif |
||
172 | #endif |
||
173 | |||
174 | #endif |
||
175 |
Powered by WebSVN v2.8.3