1/* 2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved. 3 * 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 8 * using this file. 9 * 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. 16 */ 17 18 19#include <bsafe.h> 20#include <aglobal.h> 21 22#include <stdlib.h> 23#include <stdio.h> 24#include <string.h> 25 26 27B_ALGORITHM_METHOD *chooser[] = { 28 &AM_SHA_RANDOM, 29 &AM_RSA_KEY_GEN, 30 &AM_RSA_STRONG_KEY_GEN, 31 &AM_RSA_ENCRYPT, 32 &AM_RSA_DECRYPT, 33 &AM_RSA_CRT_ENCRYPT, 34 &AM_RSA_CRT_DECRYPT, 35 NULL 36}; 37 38 39char in[] = "something wicked this way comes, " 40 "and it's a private key!"; 41unsigned char crypt[1024]; 42unsigned char out[1024], out2[1024]; 43 44unsigned char seed[] = { 17, 22, 99, 205, 3 }; 45 46unsigned char exponent[] = { 1, 0, 1 }; 47 48#define check(expr) \ 49 if (status = (expr)) { printf("error %d at %d\n", status, __LINE__); abort(); } else /* ok */ 50 51int main(int argc, char *argv[]) 52{ 53 int status; 54 55 int keySize = argv[1] ? atoi(argv[1]) : 512; 56 printf("Key size = %d bits\n", keySize); 57 58 B_KEY_OBJ pubKey = NULL; check(B_CreateKeyObject(&pubKey)); 59 B_KEY_OBJ privKey = NULL; check(B_CreateKeyObject(&privKey)); 60 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++) { 66 unsigned char buf[4]; 67 check(B_GenerateRandomBytes(random, 68 POINTER(buf), sizeof(buf), NULL)); 69 printf("Randoms = "); 70 for (int n = 0; n < sizeof(buf); n++) 71 printf("%2.2x", buf[n]); 72 printf("\n"); 73 } 74 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)); 83 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)); 87 unsigned int inLen; 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); 93 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); 103 104 105 B_DestroyKeyObject(&pubKey); 106 B_DestroyKeyObject(&privKey); 107 B_DestroyAlgorithmObject(&random); 108 exit(0); 109} 110 111void T_free(POINTER p) 112{ free(p); } 113 114POINTER T_malloc(unsigned int size) 115{ return (POINTER)malloc(size); } 116 117POINTER T_realloc(POINTER p, unsigned int size) 118{ return (POINTER)realloc(p, size); } 119 120int T_memcmp(POINTER p1, POINTER p2, unsigned int size) 121{ return memcmp(p1, p2, size); } 122void T_memcpy(POINTER p1, POINTER p2, unsigned int size) 123{ memcpy(p1, p2, size); } 124void T_memmove(POINTER p1, POINTER p2, unsigned int size) 125{ memmove(p1, p2, size); } 126void T_memset(POINTER p1, int size, unsigned int val) 127{ memset(p1, size, val); } 128extern "C" int T_GetDynamicList() 129{ printf("GetDynamicList!\n"); abort(); } 130