1/* 2 * Copyright (c) 2000-2001 Apple Computer, 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, 29 &AM_SHA_RANDOM, 30 NULL 31}; 32 33void dumpItem(ITEM &item, const char *name); 34 35unsigned char seed[] = { 17, 205, 99, 13, 6, 199 }; 36char data[] = "These are the times that try men's souls."; 37 38 39#define check(expr) \ 40 if (status = (expr)) { printf("error %d at %d\n", status, __LINE__); abort(); } else /* ok */ 41 42int main(int argc, char *argv[]) 43{ 44 int status; 45 46 ITEM key; 47 key.data = (unsigned char *)"Walla Walla Washington! Yeah, yeah, yeah!"; 48 key.len = strlen((const char *)key.data); 49 B_KEY_OBJ bsKey = NULL; 50 check(B_CreateKeyObject(&bsKey)); 51 check(B_SetKeyInfo(bsKey, KI_Item, POINTER(&key))); 52 53 B_ALGORITHM_OBJ macAlg = NULL; 54 check(B_CreateAlgorithmObject(&macAlg)); 55 B_DIGEST_SPECIFIER macSpec; 56 macSpec.digestInfoType = AI_SHA1; 57 macSpec.digestInfoParams = NULL_PTR; 58 check(B_SetAlgorithmInfo(macAlg, AI_HMAC, POINTER(&macSpec))); 59 60 check(B_DigestInit(macAlg, bsKey, chooser, NULL)); 61 check(B_DigestUpdate(macAlg, 62 POINTER(data), sizeof(data), NULL)); 63 char mac[128]; 64 unsigned int length; 65 check(B_DigestFinal(macAlg, POINTER(mac), &length, sizeof(mac), 66 NULL)); 67 ITEM macItem; macItem.data = POINTER(mac); macItem.len = length; 68 dumpItem(macItem, "MAC"); 69 70 check(B_DigestUpdate(macAlg, POINTER(data), 10, NULL)); 71 check(B_DigestUpdate(macAlg, 72 POINTER(data+10), sizeof(data)-10, NULL)); 73 check(B_DigestFinal(macAlg, POINTER(mac), &length, sizeof(mac), 74 NULL)); 75 macItem.data = POINTER(mac); macItem.len = length; 76 dumpItem(macItem, "MAC"); 77 78 printf("Done.\n"); 79 80 exit(0); 81} 82 83void dumpItem(ITEM &item, const char *name) 84{ 85 printf("%s [%d] ", name, item.len); 86 for (unsigned char *p = item.data; p < item.data + item.len; p++) 87 printf("%2.2x", *p); 88 printf("\n"); 89} 90 91 92 93 94 95void T_free(POINTER p) 96{ free(p); } 97 98POINTER T_malloc(unsigned int size) 99{ return (POINTER)malloc(size); } 100 101POINTER T_realloc(POINTER p, unsigned int size) 102{ return (POINTER)realloc(p, size); } 103 104int T_memcmp(POINTER p1, POINTER p2, unsigned int size) 105{ return memcmp(p1, p2, size); } 106void T_memcpy(POINTER p1, POINTER p2, unsigned int size) 107{ memcpy(p1, p2, size); } 108void T_memmove(POINTER p1, POINTER p2, unsigned int size) 109{ memmove(p1, p2, size); } 110void T_memset(POINTER p1, int size, unsigned int val) 111{ memset(p1, size, val); } 112extern "C" int T_GetDynamicList() 113{ printf("GetDynamicList!\n"); abort(); } 114