2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software. Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /*****************************************************************************
36 *****************************************************************************/
38 /*****************************************************************************/
40 * @brief RSA Function support.
42 * Support for all RSA functionality.
44 *****************************************************************************/
46 /*****************************************************************************
48 * 05/19/03 brr Clean up include files.
49 * 05/07/02 bac Added N8_RSA_SKS_KEY_LENGTH_DIGITS_MIN and
50 * N8_RSA_SKS_KEY_LENGTH_DIGITS_MAX for proper key screening for
52 * 04/08/02 bac Reimplemented byte length macros to be more efficient.
53 * 02/20/02 brr Removed references to the queue structure.
54 * 10/15/01 bac Fixed a bug where many macros were somehow multiply defined.
55 * 10/14/01 bac Fixed PK_RSA_?_Param_Length() macros to be more efficient.
56 * 10/12/01 dkm Moved public portion to n8_pub_common.h and n8_pub_pk.h.
57 * 10/11/01 bac Added a prototype for N8_print_rsa_parameters
58 * 10/11/01 bac More correctons for BUG #180.
59 * 10/08/01 bac Changes for the case where len(p) != len(q) (BUG #180). Most
60 * of the RSA Byte Length and RSA Param Byte Length macros
62 * 10/01/01 hml Added unitID and SKSHandle to keyObject and moved
63 * material structure in here.
64 * 08/22/01 bac Replaced use of ceil() with homemade integer version.
65 * 08/21/01 bac Fixes to allow the use of odd key lengths (Bug #174).
66 * 08/17/01 hml Fixed PK_RSA_HALF_LENGTH macro for bug 172
67 * 08/16/01 mmd Now including n8_driver_api.h instead of simon_driver_api.h.
68 * 08/21/01 bac Fixes to allow the use of odd key lengths (Bug #174).
69 * 08/17/01 hml Fixed PK_RSA_HALF_LENGTH macro for bug 172
70 * 07/30/01 bac Added queue pointer to length calculations.
71 * 06/25/01 bac Changed N8_RSAKeyObject_t to use physical addresses. Added
72 * N8_RSAKeyObjectVirtual_t to be the virtual address counterpart.
73 * 06/19/01 bac Correct use of kernel memory.
74 * 05/30/01 bac Structure documentation.
75 * 04/10/01 bac Changes to come up to standard.
76 * 03/21/01 bac Original version.
77 ****************************************************************************/
81 #include "n8_common.h"
82 #include "n8_pub_pk.h"
83 #include "n8_enqueue_common.h"
84 #include "n8_pk_common.h"
85 #include "n8_pub_errors.h"
86 #include "n8_driver_api.h"
90 #define N8_RSA_KEY_LENGTH_MIN 1 /* length in bytes */
91 #define N8_RSA_KEY_LENGTH_MAX 512 /* length in bytes */
93 #define N8_RSA_SKS_KEY_LENGTH_DIGITS_MIN 2 /* sks must be at least two BNC digits */
94 #define N8_RSA_SKS_KEY_LENGTH_DIGITS_MAX 32 /* and less than 32 */
96 /* PKE Parameter lengths in bytes */
97 #define PK_RSA_HALF_DIGITS(__KL) (((__KL) + 1) / 2)
98 #define PK_RSA_FULL_LENGTH(__KL) ((__KL)*PK_Bytes_Per_BigNum_Digit)
99 #define PK_RSA_FULL_LENGTH(__KL) ((__KL)*PK_Bytes_Per_BigNum_Digit)
100 #define PK_RSA_HALF_LENGTH(__KL) ((((__KL)+1)/2) * PK_Bytes_Per_BigNum_Digit)
101 #define PK_RSA_Input_Byte_Length(__KL) PK_RSA_FULL_LENGTH((__KL))
102 #define PK_RSA_Output_Byte_Length(__KL) PK_RSA_FULL_LENGTH((__KL))
104 #define PK_RSA_P_Byte_Length(__KP) ((__KP)->pDigits)*PK_Bytes_Per_BigNum_Digit
105 #define PK_RSA_Q_Byte_Length(__KP) ((__KP)->qDigits)*PK_Bytes_Per_BigNum_Digit
106 #define PK_RSA_DP_Byte_Length(__KP) PK_RSA_P_Byte_Length((__KP))
107 #define PK_RSA_DQ_Byte_Length(__KP) PK_RSA_Q_Byte_Length((__KP))
108 #define PK_RSA_R_MOD_P_Byte_Length(__KP) PK_RSA_P_Byte_Length((__KP))
109 #define PK_RSA_R_MOD_Q_Byte_Length(__KP) PK_RSA_Q_Byte_Length((__KP))
110 #define PK_RSA_R_MOD_N_Byte_Length(__KP) PK_RSA_FULL_LENGTH((__KP)->privateKeyDigits)
111 #define PK_RSA_N_Byte_Length(__KP) PK_RSA_FULL_LENGTH((__KP)->privateKeyDigits)
112 #define PK_RSA_U_Byte_Length(__KP) PK_RSA_Q_Byte_Length((__KP))
113 #define PK_RSA_CP_Byte_Length PK_Bytes_Per_BigNum_Digit
114 #define PK_RSA_CQ_Byte_Length PK_Bytes_Per_BigNum_Digit
115 #define PK_RSA_CN_Byte_Length PK_Bytes_Per_BigNum_Digit
117 #define PK_RSA_Param_Byte_Length(__KP) \
118 PK_RSA_P_Byte_Length((__KP)) + \
119 PK_RSA_Q_Byte_Length((__KP)) + \
120 PK_RSA_DP_Byte_Length((__KP)) + \
121 PK_RSA_DQ_Byte_Length((__KP)) + \
122 PK_RSA_R_MOD_P_Byte_Length((__KP)) + \
123 PK_RSA_R_MOD_Q_Byte_Length((__KP)) + \
124 PK_RSA_N_Byte_Length((__KP)) + \
125 PK_RSA_U_Byte_Length((__KP)) + \
126 PK_RSA_CP_Byte_Length + \
127 PK_RSA_CQ_Byte_Length
130 * Offsets into the PKE RSA Parameter block
132 * Note most parameters are KeyLength/2 digits
134 * N (KeyLength digits),
138 /* Parameter lengths in digits.
139 * Params: (__KP) N8_RSAKeyObject_t pointer.
140 * __QP QueueControl_t pointer.
142 #define PK_RSA_P_Param_Length(__KP) (__KP)->pDigits
143 #define PK_RSA_Q_Param_Length(__KP) (__KP)->qDigits
144 #define PK_RSA_DP_Param_Length(__KP) PK_RSA_P_Param_Length(__KP)
145 #define PK_RSA_DQ_Param_Length(__KP) PK_RSA_Q_Param_Length(__KP)
146 #define PK_RSA_R_MOD_P_Param_Length(__KP) PK_RSA_P_Param_Length(__KP)
147 #define PK_RSA_R_MOD_Q_Param_Length(__KP) PK_RSA_Q_Param_Length(__KP)
148 #define PK_RSA_N_Param_Length(__KP) (__KP)->privateKeyDigits
149 #define PK_RSA_U_Param_Length(__KP) PK_RSA_Q_Param_Length(__KP)
150 #define PK_RSA_CP_Param_Length 1
151 #define PK_RSA_CQ_Param_Length 1
153 /* parameter offsets in digits */
154 #define PK_RSA_P_Param_Offset(__KP) (0)
155 #define PK_RSA_Q_Param_Offset(__KP) \
156 (PK_RSA_P_Param_Offset(__KP) + PK_RSA_P_Param_Length(__KP))
157 #define PK_RSA_DP_Param_Offset(__KP) \
158 (PK_RSA_Q_Param_Offset(__KP) + PK_RSA_Q_Param_Length(__KP))
159 #define PK_RSA_DQ_Param_Offset(__KP) \
160 (PK_RSA_DP_Param_Offset(__KP) + PK_RSA_DP_Param_Length(__KP))
161 #define PK_RSA_R_MOD_P_Param_Offset(__KP) \
162 (PK_RSA_DQ_Param_Offset(__KP) + PK_RSA_DQ_Param_Length(__KP))
163 #define PK_RSA_R_MOD_Q_Param_Offset(__KP) \
164 (PK_RSA_R_MOD_P_Param_Offset(__KP) + PK_RSA_R_MOD_P_Param_Length(__KP))
165 #define PK_RSA_N_Param_Offset(__KP) \
166 (PK_RSA_R_MOD_Q_Param_Offset(__KP) + PK_RSA_R_MOD_Q_Param_Length(__KP))
167 #define PK_RSA_U_Param_Offset(__KP) \
168 (PK_RSA_N_Param_Offset(__KP) + PK_RSA_N_Param_Length(__KP))
169 #define PK_RSA_CP_Param_Offset(__KP) \
170 (PK_RSA_U_Param_Offset(__KP) + PK_RSA_U_Param_Length(__KP))
171 #define PK_RSA_CQ_Param_Offset(__KP) \
172 (PK_RSA_CP_Param_Offset(__KP) + PK_RSA_CP_Param_Length)
173 /* parameter offsets in bytes */
174 #define PK_RSA_P_Param_Byte_Offset(__KP) (0)
176 #define PK_RSA_Q_Param_Byte_Offset(__KP) \
177 (PK_RSA_P_Param_Byte_Offset(__KP) + PK_RSA_P_Byte_Length(__KP))
178 #define PK_RSA_DP_Param_Byte_Offset(__KP) \
179 (PK_RSA_Q_Param_Byte_Offset(__KP) + PK_RSA_Q_Byte_Length(__KP))
180 #define PK_RSA_DQ_Param_Byte_Offset(__KP) \
181 (PK_RSA_DP_Param_Byte_Offset(__KP) + PK_RSA_DP_Byte_Length(__KP))
182 #define PK_RSA_R_MOD_P_Param_Byte_Offset(__KP) \
183 (PK_RSA_DQ_Param_Byte_Offset(__KP) + PK_RSA_DQ_Byte_Length(__KP))
184 #define PK_RSA_R_MOD_Q_Param_Byte_Offset(__KP) \
185 (PK_RSA_R_MOD_P_Param_Byte_Offset(__KP) + PK_RSA_R_MOD_P_Byte_Length(__KP))
186 #define PK_RSA_N_Param_Byte_Offset(__KP) \
187 (PK_RSA_R_MOD_Q_Param_Byte_Offset(__KP) + PK_RSA_R_MOD_Q_Byte_Length(__KP))
188 #define PK_RSA_U_Param_Byte_Offset(__KP) \
189 (PK_RSA_N_Param_Byte_Offset(__KP) + PK_RSA_N_Byte_Length(__KP))
190 #define PK_RSA_CP_Param_Byte_Offset(__KP) \
191 (PK_RSA_U_Param_Byte_Offset(__KP) + PK_RSA_U_Byte_Length(__KP))
192 #define PK_RSA_CQ_Param_Byte_Offset(__KP) \
193 (PK_RSA_CP_Param_Byte_Offset(__KP) + PK_RSA_CP_Byte_Length)
197 N8_Buffer_t
*p
; /* secret prime number. used for
198 * private operations. */
199 N8_Buffer_t
*q
; /* secret prime number. used for
200 * private operations. */
201 /* d is the private key */
202 N8_Buffer_t
*dp
; /* d mod ((p-1) mod p) */
203 N8_Buffer_t
*dq
; /* d mod ((q-1) mod q) */
204 N8_Buffer_t
*R_mod_p
; /* R mod p. (private) */
205 N8_Buffer_t
*R_mod_q
; /* R mod q. (private) */
206 N8_Buffer_t
*R_mod_n
; /* R mod n. (public) */
207 N8_Buffer_t
*n
; /* modulus: n=pq */
208 N8_Buffer_t
*u
; /* used for Chinese Remainder Theorem:
209 * u = p^-1 mod q. private
210 * operations only. */
211 N8_Buffer_t
*cp
; /* additive inverse of the
212 * multiplicative inverse of the least
213 * significant digit of p, mod 2^128.
214 * used for private operations. */
215 N8_Buffer_t
*cq
; /* same as cp but for q. used for
216 * private operations. */
217 N8_Buffer_t
*cn
; /* same as cp but for n. used for
218 * public operations. */
220 N8_Buffer_t
*key
; /* this is either e, the public key or
221 * d, the private key, depending upon
222 * the context this object is being
224 N8_Buffer_t
*paramBlock
; /* parameter block in PK format,
225 * suitable for loading directly into
226 * the Big Num Cache (BNC). */
227 N8_MemoryHandle_t kMemStruct
; /* kernel memory structure for the
229 } N8_RSAKeyObjectVirtual_t
;
231 /**********************************************************************
232 * function prototypes
233 **********************************************************************/
234 void N8_print_rsa_parameters(unsigned char *p_block
,
235 N8_RSAKeyObject_t
*key_p
);
236 #endif /* N8_RSA_H */