2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
27 B_ALGORITHM_METHOD
*chooser
[] = {
34 void dumpItem(ITEM
&item
, const char *name
);
36 unsigned char seed
[] = { 17, 205, 99, 13, 6, 199 };
40 if (status = (expr)) { printf("error %d at %d\n", status, __LINE__); abort(); } else /* ok */
42 int main(int argc
, char *argv
[])
46 int keySize
= argv
[1] ? atoi(argv
[1]) : 512;
47 printf("Key size = %d bits\n", keySize
);
49 B_ALGORITHM_OBJ pGen
= NULL
;
50 check(B_CreateAlgorithmObject(&pGen
));
51 B_DSA_PARAM_GEN_PARAMS gParams
;
52 gParams
.primeBits
= keySize
;
53 check(B_SetAlgorithmInfo(pGen
, AI_DSAParamGen
, POINTER(&gParams
)));
55 B_ALGORITHM_OBJ random
= NULL
; check(B_CreateAlgorithmObject(&random
));
56 check(B_SetAlgorithmInfo(random
, AI_X962Random_V0
, NULL
));
57 check(B_RandomInit(random
, chooser
, NULL
));
58 check(B_RandomUpdate(random
, seed
, sizeof(seed
), NULL
));
60 check(B_GenerateInit(pGen
, chooser
, NULL
));
61 B_ALGORITHM_OBJ result
= NULL
;
62 check(B_CreateAlgorithmObject(&result
));
63 printf("Generating DSA parameters\n");
64 check(B_GenerateParameters(pGen
, result
, random
, NULL
));
65 printf("DSA generate complete, writing...\n");
67 A_DSA_PARAMS
*dParams
;
68 memset(&dParams
, 0, sizeof(dParams
));
69 check(B_GetAlgorithmInfo((POINTER
*)&dParams
, result
, AI_DSAKeyGen
));
70 dumpItem(dParams
->prime
, "prime");
71 dumpItem(dParams
->subPrime
, "subprime");
72 dumpItem(dParams
->base
, "base");
75 B_KEY_OBJ pubKey
= NULL
; check(B_CreateKeyObject(&pubKey
));
76 B_KEY_OBJ privKey
= NULL
; check(B_CreateKeyObject(&privKey
));
78 B_ALGORITHM_OBJ gen
= NULL
; check(B_CreateAlgorithmObject(&gen
));
79 A_RSA_KEY_GEN_PARAMS args
;
80 args
.modulusBits
= keySize
;
81 args
.publicExponent
.data
= exponent
;
82 args
.publicExponent
.len
= sizeof(exponent
);
83 check(B_SetAlgorithmInfo(gen
, AI_RSAStrongKeyGen
, POINTER(&args
)));
84 check(B_GenerateInit(gen
, chooser
, NULL
));
85 check(B_GenerateKeypair(gen
, pubKey
, privKey
, random
, NULL
));
87 B_ALGORITHM_OBJ enc
= NULL
; check(B_CreateAlgorithmObject(&enc
));
88 check(B_SetAlgorithmInfo(enc
, AI_PKCS_RSAPublic
, NULL
));
89 check(B_EncryptInit(enc
, pubKey
, chooser
, NULL
));
91 check(B_EncryptUpdate(enc
, crypt
, &inLen
, sizeof(crypt
),
92 POINTER(in
), sizeof(in
), random
, NULL
));
93 printf("EncryptUpdate output = %u\n", inLen
);
94 check(B_EncryptFinal(enc
, crypt
, &inLen
, sizeof(crypt
), random
, NULL
));
95 printf("EncryptFinal output=%u\n", inLen
);
97 B_ALGORITHM_OBJ dec
= NULL
; check(B_CreateAlgorithmObject(&dec
));
98 check(B_SetAlgorithmInfo(dec
, AI_PKCS_RSAPrivate
, NULL
));
99 check(B_DecryptInit(dec
, privKey
, chooser
, NULL
));
100 unsigned int outLen
, outLen2
;
101 check(B_DecryptUpdate(dec
, out
, &outLen
, sizeof(out
),
102 crypt
, inLen
, random
, NULL
));
103 printf("DecryptUpdate output = %u\n", outLen
);
104 check(B_DecryptFinal(dec
, out2
, &outLen2
, sizeof(out2
), random
, NULL
));
105 printf("DecryptFinal output=%u %s\n", outLen2
, (char*)out2
);
106 B_DestroyKeyObject(&pubKey
);
107 B_DestroyKeyObject(&privKey
);
113 void dumpItem(ITEM
&item
, const char *name
)
115 printf("%s [%d] ", name
, item
.len
);
116 for (unsigned char *p
= item
.data
; p
< item
.data
+ item
.len
; p
++)
125 void T_free(POINTER p
)
128 POINTER
T_malloc(unsigned int size
)
129 { return (POINTER
)malloc(size
); }
131 POINTER
T_realloc(POINTER p
, unsigned int size
)
132 { return (POINTER
)realloc(p
, size
); }
134 int T_memcmp(POINTER p1
, POINTER p2
, unsigned int size
)
135 { return memcmp(p1
, p2
, size
); }
136 void T_memcpy(POINTER p1
, POINTER p2
, unsigned int size
)
137 { memcpy(p1
, p2
, size
); }
138 void T_memmove(POINTER p1
, POINTER p2
, unsigned int size
)
139 { memmove(p1
, p2
, size
); }
140 void T_memset(POINTER p1
, int size
, unsigned int val
)
141 { memset(p1
, size
, val
); }
142 extern "C" int T_GetDynamicList()
143 { printf("GetDynamicList!\n"); abort(); }