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 /* pke_bn.c: OPENSSL big number library wrappers and big number <-> big cache functions */
37 #include "n8_OS_intf.h"
38 #include "test_case.h"
42 #define CHECKFSCANFRETURN(r,n) if ((r) != (n)){\
43 DBG(("error in fscanf at %d of %s, expected %d got %d\n", \
44 __LINE__, __FILE__,(n),(r)));\
45 return TC_FSCANF_ERR;}
47 #define CHECKFSCANFRETURN(r,n) if ((r) != (n)) return TC_FSCANF_ERR;
50 #define CHECKREADATTRRETURN(r) if((r) != TC_SUCCESS) return (r);
53 int pkeFormattedReadNum(FILE *fp
, int length
, int offset
, char *valuePtr
)
62 for (i
=0; i
< length
; i
++) {
63 for (j
=0; j
< 15; j
++) {
64 iResult
= fscanf(fp
, "%2x", &temp
);
65 CHECKFSCANFRETURN(iResult
, 1);
66 *tmpPtr
++ = (unsigned char) (temp
& 0xff);
68 iResult
= fscanf(fp
,"%2x\n", &temp
);
69 CHECKFSCANFRETURN(iResult
, 1);
70 *tmpPtr
++ = (unsigned char) (temp
& 0xff);
75 int pkeReadAttr(FILE *fp
, ATTR
*thisone
, char *name
)
81 strcat(fmt
, " %d %d\n");
82 r
= fscanf(fp
, fmt
, &(thisone
->length
), &(thisone
->offset
));
83 CHECKFSCANFRETURN(r
,2);
84 return pkeFormattedReadNum(fp
,thisone
->length
,thisone
->offset
,thisone
->value
);
86 int pkeReadModm(FILE *fp
, TEST_CASE
*thisone
)
88 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
89 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
90 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
94 int pkeReadRmod(FILE *fp
,TEST_CASE
*thisone
)
96 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
97 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
100 int pkeReadAddm(FILE *fp
,TEST_CASE
*thisone
)
102 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
103 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
104 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->b
),"b"));
105 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
108 int pkeReadSubm(FILE *fp
,TEST_CASE
*thisone
)
110 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
111 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
112 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->b
),"b"));
113 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
116 int pkeReadAinv(FILE *fp
,TEST_CASE
*thisone
)
118 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
119 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
120 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
123 int pkeReadMulm(FILE *fp
,TEST_CASE
*thisone
)
125 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
126 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
127 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->b
),"b"));
128 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
131 int pkeReadMinv(FILE *fp
,TEST_CASE
*thisone
)
133 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
134 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
135 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
138 int pkeReadExpm(FILE *fp
, TEST_CASE
*thisone
)
140 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
141 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
142 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->b
),"b"));
143 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->c
),"c"));
144 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
147 int pkeReadExp(FILE *fp
, TEST_CASE
*thisone
)
149 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->m
),"m"));
150 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->a
),"a"));
151 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->b
),"b"));
152 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->r
),"r"));
155 int pkeReadRSA(FILE *fp
, TEST_CASE
*thisone
)
157 if (fscanf(fp
, "sks %d %d\n",
158 &(thisone
->use_sks
), &(thisone
->sks_addr
)) != 2) {
159 return TC_FSCANF_ERR
;
161 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[RSA_P_Operand
]),"p"));
162 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[RSA_Q_Operand
]),"q"));
163 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[RSA_D_Operand
]),"d"));
164 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[RSA_N_Operand
]),"n"));
165 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[RSA_U_Operand
]),"u"));
166 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[RSA_E_Operand
]),"e"));
167 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[RSA_I_Operand
]),"i"));
170 int pkeReadDSA(FILE *fp
, TEST_CASE
*thisone
)
172 if (fscanf(fp
, "sks %d %d\n",
173 &(thisone
->use_sks
), &(thisone
->sks_addr
)) != 2) {
174 return TC_FSCANF_ERR
;
176 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[DSA_N_Operand
]),"n"));
177 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[DSA_E_Operand
]),"e"));
178 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[DSA_P_Operand
]),"p"));
179 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[DSA_G_Operand
]),"g"));
180 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[DSA_Q_Operand
]),"q"));
181 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[DSA_X_Operand
]),"x"));
182 CHECKREADATTRRETURN(pkeReadAttr(fp
,&(thisone
->opnd
[DSA_Y_Operand
]),"y"));
185 int read_test_case(FILE *fp
, TEST_CASE
*thisI
)
190 memset(thisI
, 0, sizeof(TEST_CASE
));
191 token
= (char *) &(thisI
->token
[0]);
193 iResult
= fscanf(fp
, "%s %d\n", (char *)&(token
[0]), &(thisI
->seq_number
));
196 if(iResult
== EOF
) return TC_EOF
;
197 else return TC_FSCANF_ERR
;
199 if (strcmp(token
,"modm") == 0)
201 return pkeReadModm(fp
, thisI
);
203 else if (strcmp(token
,"rmod") == 0) {
204 return pkeReadRmod(fp
, thisI
);
206 else if (strcmp(token
,"addm") == 0) {
207 return pkeReadAddm(fp
, thisI
);
209 else if (strcmp(token
,"subm") == 0) {
210 return pkeReadSubm(fp
, thisI
);
212 else if (strcmp(token
,"ainv") == 0) {
213 return pkeReadAinv(fp
, thisI
);
215 else if (strcmp(token
,"mulm") == 0) {
216 return pkeReadMulm(fp
, thisI
);
218 else if (strcmp(token
,"minv") == 0) {
219 return pkeReadMinv(fp
, thisI
);
221 else if (strcmp(token
,"expm") == 0) {
222 return pkeReadExpm(fp
, thisI
);
224 else if (strcmp(token
,"exp") == 0) { /* added for performance testing 020509 MWW */
225 return pkeReadExp(fp
, thisI
);
227 else if (strcmp(token
, "rsa") == 0) {
228 return pkeReadRSA(fp
, thisI
);
230 else if (strcmp(token
, "dsa") == 0) {
231 return pkeReadDSA(fp
, thisI
);
233 return TC_GENERIC_ERROR
;