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
[] = {
30 &AM_RSA_STRONG_KEY_GEN
,
39 char in
[] = "something wicked this way comes, "
40 "and it's a private key!";
41 unsigned char crypt
[1024];
42 unsigned char out
[1024], out2
[1024];
44 unsigned char seed
[] = { 17, 22, 99, 205, 3 };
46 unsigned char exponent
[] = { 1, 0, 1 };
49 if (status = (expr)) { printf("error %d at %d\n", status, __LINE__); abort(); } else /* ok */
51 int main(int argc
, char *argv
[])
55 int keySize
= argv
[1] ? atoi(argv
[1]) : 512;
56 printf("Key size = %d bits\n", keySize
);
58 B_KEY_OBJ pubKey
= NULL
; check(B_CreateKeyObject(&pubKey
));
59 B_KEY_OBJ privKey
= NULL
; check(B_CreateKeyObject(&privKey
));
61 B_ALGORITHM_OBJ random
= NULL
; check(B_CreateAlgorithmObject(&random
));
62 check(B_SetAlgorithmInfo(random
, AI_X962Random_V0
, NULL
));
63 check(B_RandomInit(random
, chooser
, NULL
));
64 check(B_RandomUpdate(random
, seed
, sizeof(seed
), NULL
));
65 for (int n
= 0; n
< 5; n
++) {
67 check(B_GenerateRandomBytes(random
,
68 POINTER(buf
), sizeof(buf
), NULL
));
70 for (int n
= 0; n
< sizeof(buf
); n
++)
71 printf("%2.2x", buf
[n
]);
75 B_ALGORITHM_OBJ gen
= NULL
; check(B_CreateAlgorithmObject(&gen
));
76 A_RSA_KEY_GEN_PARAMS args
;
77 args
.modulusBits
= keySize
;
78 args
.publicExponent
.data
= exponent
;
79 args
.publicExponent
.len
= sizeof(exponent
);
80 check(B_SetAlgorithmInfo(gen
, AI_RSAStrongKeyGen
, POINTER(&args
)));
81 check(B_GenerateInit(gen
, chooser
, NULL
));
82 check(B_GenerateKeypair(gen
, pubKey
, privKey
, random
, NULL
));
84 B_ALGORITHM_OBJ enc
= NULL
; check(B_CreateAlgorithmObject(&enc
));
85 check(B_SetAlgorithmInfo(enc
, AI_PKCS_RSAPublic
, NULL
));
86 check(B_EncryptInit(enc
, pubKey
, chooser
, NULL
));
88 check(B_EncryptUpdate(enc
, crypt
, &inLen
, sizeof(crypt
),
89 POINTER(in
), sizeof(in
), random
, NULL
));
90 printf("EncryptUpdate output = %u\n", inLen
);
91 check(B_EncryptFinal(enc
, crypt
, &inLen
, sizeof(crypt
), random
, NULL
));
92 printf("EncryptFinal output=%u\n", inLen
);
94 B_ALGORITHM_OBJ dec
= NULL
; check(B_CreateAlgorithmObject(&dec
));
95 check(B_SetAlgorithmInfo(dec
, AI_PKCS_RSAPrivate
, NULL
));
96 check(B_DecryptInit(dec
, privKey
, chooser
, NULL
));
97 unsigned int outLen
, outLen2
;
98 check(B_DecryptUpdate(dec
, out
, &outLen
, sizeof(out
),
99 crypt
, inLen
, random
, NULL
));
100 printf("DecryptUpdate output = %u\n", outLen
);
101 check(B_DecryptFinal(dec
, out2
, &outLen2
, sizeof(out2
), random
, NULL
));
102 printf("DecryptFinal output=%u %s\n", outLen2
, (char*)out2
);
105 B_DestroyKeyObject(&pubKey
);
106 B_DestroyKeyObject(&privKey
);
107 B_DestroyAlgorithmObject(&random
);
111 void T_free(POINTER p
)
114 POINTER
T_malloc(unsigned int size
)
115 { return (POINTER
)malloc(size
); }
117 POINTER
T_realloc(POINTER p
, unsigned int size
)
118 { return (POINTER
)realloc(p
, size
); }
120 int T_memcmp(POINTER p1
, POINTER p2
, unsigned int size
)
121 { return memcmp(p1
, p2
, size
); }
122 void T_memcpy(POINTER p1
, POINTER p2
, unsigned int size
)
123 { memcpy(p1
, p2
, size
); }
124 void T_memmove(POINTER p1
, POINTER p2
, unsigned int size
)
125 { memmove(p1
, p2
, size
); }
126 void T_memset(POINTER p1
, int size
, unsigned int val
)
127 { memset(p1
, size
, val
); }
128 extern "C" int T_GetDynamicList()
129 { printf("GetDynamicList!\n"); abort(); }