X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/fe8ab488e9161c46dd9885d58fc52996dc0249ff..HEAD:/EXTERNAL_HEADERS/corecrypto/ccdigest.h diff --git a/EXTERNAL_HEADERS/corecrypto/ccdigest.h b/EXTERNAL_HEADERS/corecrypto/ccdigest.h index 9079c4a18..0edd80261 100644 --- a/EXTERNAL_HEADERS/corecrypto/ccdigest.h +++ b/EXTERNAL_HEADERS/corecrypto/ccdigest.h @@ -1,10 +1,12 @@ -/* - * ccdigest.h - * corecrypto - * - * Created by Michael Brouwer on 11/30/10. - * Copyright 2010,2011 Apple Inc. All rights reserved. +/* Copyright (c) (2010,2011,2012,2014,2015,2016,2017,2018,2019) Apple Inc. All rights reserved. * + * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which + * is contained in the License.txt file distributed with corecrypto) and only to + * people who accept that license. IMPORTANT: Any license rights granted to you by + * Apple Inc. (if any) are limited to internal use within your organization only on + * devices and computers you own or control, for the sole purpose of verifying the + * security characteristics and correct functioning of the Apple Software. You may + * not, directly or indirectly, redistribute the Apple Software or any portions thereof. */ #ifndef _CORECRYPTO_CCDIGEST_H_ @@ -12,10 +14,7 @@ #include #include -#ifdef USE_SUPER_COOL_NEW_CCOID_T -#include -#endif /* USE_SUPER_COOL_NEW_CCOID_T */ - + /* To malloc a digest context for a given di, use malloc(ccdigest_di_size(di)) and assign the result to a pointer to a struct ccdigest_ctx. */ struct ccdigest_ctx { @@ -25,11 +24,9 @@ struct ccdigest_ctx { uint64_t u64; cc_unit ccn; } state; -} __attribute((aligned(8))); +} CC_ALIGNED(8); -typedef union { - struct ccdigest_ctx *hdr; -} ccdigest_ctx_t __attribute__((transparent_union)); +typedef struct ccdigest_ctx *ccdigest_ctx_t ; struct ccdigest_state { union { @@ -38,28 +35,20 @@ struct ccdigest_state { uint64_t u64; cc_unit ccn; } state; -} __attribute((aligned(8))); +} CC_ALIGNED(8); -typedef union { - struct ccdigest_state *hdr; - struct ccdigest_ctx *_ctx; - ccdigest_ctx_t _ctxt; -} ccdigest_state_t __attribute__((transparent_union)); +typedef struct ccdigest_state *ccdigest_state_t; struct ccdigest_info { - unsigned long output_size; - unsigned long state_size; - unsigned long block_size; - unsigned long oid_size; -#ifdef USE_SUPER_COOL_NEW_CCOID_T - ccoid_t oid; -#else - unsigned char *oid; -#endif + size_t output_size; + size_t state_size; + size_t block_size; + size_t oid_size; + const unsigned char *oid; const void *initial_state; - void(*compress)(ccdigest_state_t state, unsigned long nblocks, + void(* CC_SPTR(ccdigest_info, compress))(ccdigest_state_t state, size_t nblocks, const void *data); - void(*final)(const struct ccdigest_info *di, ccdigest_ctx_t ctx, + void(* CC_SPTR(ccdigest_info, final))(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest); }; @@ -73,42 +62,33 @@ struct ccdigest_info { size_t _block_size_, named _name_. Can be used in structs or on the stack. */ #define ccdigest_ctx_decl(_state_size_, _block_size_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_ctx_size(_state_size_, _block_size_), _name_) -#define ccdigest_ctx_clear(_state_size_, _block_size_, _name_) cc_zero(ccdigest_ctx_size(_state_size_, _block_size_), _name_) +#define ccdigest_ctx_clear(_state_size_, _block_size_, _name_) cc_clear(ccdigest_ctx_size(_state_size_, _block_size_), _name_) /* Declare a ccdigest_ctx for a given size_t _state_size_ and size_t _block_size_, named _name_. Can be used on the stack. */ #define ccdigest_di_decl(_di_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_di_size(_di_), _name_) -#define ccdigest_di_clear(_di_, _name_) cc_zero(ccdigest_di_size(_di_), _name_) +#define ccdigest_di_clear(_di_, _name_) cc_clear(ccdigest_di_size(_di_), _name_) /* Digest context field accessors. Consider the implementation private. */ +#define ccdigest_state(_di_, _ctx_) ((struct ccdigest_state *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + sizeof(uint64_t))) -#define ccdigest_state(_di_, _ctx_) ((struct ccdigest_state *)(&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8 + sizeof(uint64_t))) #define ccdigest_state_u8(_di_, _ctx_) ccdigest_u8(ccdigest_state((_di_), (_ctx_))) #define ccdigest_state_u32(_di_, _ctx_) ccdigest_u32(ccdigest_state((_di_), (_ctx_))) #define ccdigest_state_u64(_di_, _ctx_) ccdigest_u64(ccdigest_state((_di_), (_ctx_))) #define ccdigest_state_ccn(_di_, _ctx_) ccdigest_ccn(ccdigest_state((_di_), (_ctx_))) -#define ccdigest_nbits(_di_, _ctx_) (((uint64_t *)(&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8))[0]) -#define ccdigest_data(_di_, _ctx_) (&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8 + (_di_)->state_size + sizeof(uint64_t)) -#define ccdigest_num(_di_, _ctx_) (((unsigned int *)(&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8 + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size))[0]) +#define ccdigest_nbits(_di_, _ctx_) (((uint64_t *)(&((ccdigest_ctx_t)(_ctx_))->state.u8))[0]) +#define ccdigest_data(_di_, _ctx_) (&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t)) +#define ccdigest_num(_di_, _ctx_) (((unsigned int *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size))[0]) /* Digest state field accessors. Consider the implementation private. */ -#define ccdigest_u8(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.u8) -#define ccdigest_u32(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.u32) -#define ccdigest_u64(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.u64) -#define ccdigest_ccn(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.ccn) - -/* We could just use memcpy instead of this special macro, but this allows us - to use the optimized ccn_set() assembly routine if we have one, which for - 32 bit arm is about 200% quicker than generic memcpy(). */ -#if CCN_SET_ASM && CCN_UNIT_SIZE <= 4 -#define ccdigest_copy_state(_di_, _dst_, _src_) ccn_set((_di_)->state_size / CCN_UNIT_SIZE, _dst_, _src_) -#else -#define ccdigest_copy_state(_di_, _dst_, _src_) CC_MEMCPY(_dst_, _src_, (_di_)->state_size) -#endif +#define ccdigest_u8(_state_) (&((ccdigest_state_t)(_state_))->state.u8) +#define ccdigest_u32(_state_) (&((ccdigest_state_t)(_state_))->state.u32) +#define ccdigest_u64(_state_) (&((ccdigest_state_t)(_state_))->state.u64) +#define ccdigest_ccn(_state_) (&((ccdigest_state_t)(_state_))->state.ccn) void ccdigest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx); void ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, - unsigned long len, const void *data); + size_t len, const void *data); CC_INLINE void ccdigest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest) @@ -116,64 +96,20 @@ void ccdigest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned di->final(di,ctx,digest); } -void ccdigest(const struct ccdigest_info *di, unsigned long len, +void ccdigest(const struct ccdigest_info *di, size_t len, const void *data, void *digest); -/* test functions */ -int ccdigest_test(const struct ccdigest_info *di, unsigned long len, - const void *data, const void *digest); - -int ccdigest_test_chunk(const struct ccdigest_info *di, unsigned long len, - const void *data, const void *digest, unsigned long chunk); - -struct ccdigest_vector { - unsigned long len; - const void *message; - const void *digest; -}; - -int ccdigest_test_vector(const struct ccdigest_info *di, const struct ccdigest_vector *v); -int ccdigest_test_chunk_vector(const struct ccdigest_info *di, const struct ccdigest_vector *v, unsigned long chunk); - -#ifdef USE_SUPER_COOL_NEW_CCOID_T -#define OID_DEF(_NAME_, _VALUE_) _NAME_ {((unsigned char *) _VALUE_)} -#define CC_DIGEST_OID_MD2 {((unsigned char *)"\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x02")} -#define CC_DIGEST_OID_MD4 {((unsigned char *)"\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x04")} -#define CC_DIGEST_OID_MD5 {((unsigned char *)"\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05")} -#define CC_DIGEST_OID_SHA1 {((unsigned char *)"\x06\x05\x2b\x0e\x03\x02\x1a")} -#define CC_DIGEST_OID_SHA224 {((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")} -#define CC_DIGEST_OID_SHA256 {((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")} -#define CC_DIGEST_OID_SHA384 {((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")} -#define CC_DIGEST_OID_SHA512 {((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")} -#define CC_DIGEST_OID_RMD128 {((unsigned char *)"\x06\x06\x28\xCF\x06\x03\x00\x32")} -#define CC_DIGEST_OID_RMD160 {((unsigned char *)"\x06\x05\x2B\x24\x03\x02\x01")} -#define CC_DIGEST_OID_RMD256 {((unsigned char *)"\x06\x05\x2B\x24\x03\x02\x03")} -#define CC_DIGEST_OID_RMD320 {((unsigned char *)NULL)} -#else -#define CC_DIGEST_OID_MD2 "\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x02" -#define CC_DIGEST_OID_MD4 "\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x04" -#define CC_DIGEST_OID_MD5 "\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05" -#define CC_DIGEST_OID_SHA1 "\x06\x05\x2b\x0e\x03\x02\x1a" -#define CC_DIGEST_OID_SHA224 "\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04" -#define CC_DIGEST_OID_SHA256 "\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01" -#define CC_DIGEST_OID_SHA384 "\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02" -#define CC_DIGEST_OID_SHA512 "\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03" -#define CC_DIGEST_OID_RMD128 "\x06\x06\x28\xCF\x06\x03\x00\x32" -#define CC_DIGEST_OID_RMD160 "\x06\x05\x2B\x24\x03\x02\x01" -#define CC_DIGEST_OID_RMD256 "\x06\x05\x2B\x24\x03\x02\x03" -#define CC_DIGEST_OID_RMD320 NULL -#endif - -#ifdef USE_SUPER_COOL_NEW_CCOID_T -CC_INLINE CC_NONNULL_TU((1)) CC_NONNULL_TU((2)) -bool ccdigest_oid_equal(const struct ccdigest_info *di, ccoid_t oid) { - if(di->oid.oid == NULL && oid.oid == NULL) return true; - return ccoid_equal(di->oid, oid); -} +#define OID_DEF(_VALUE_) ((const unsigned char *)_VALUE_) -typedef const struct ccdigest_info *(ccdigest_lookup)(ccoid_t oid); +#define CC_DIGEST_OID_MD2 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x02") +#define CC_DIGEST_OID_MD4 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x04") +#define CC_DIGEST_OID_MD5 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05") +#define CC_DIGEST_OID_SHA1 OID_DEF("\x06\x05\x2b\x0e\x03\x02\x1a") +#define CC_DIGEST_OID_SHA224 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04") +#define CC_DIGEST_OID_SHA256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01") +#define CC_DIGEST_OID_SHA384 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02") +#define CC_DIGEST_OID_SHA512 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03") +#define CC_DIGEST_OID_SHA512_256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x06") +#define CC_DIGEST_OID_RMD160 OID_DEF("\x06\x05\x2B\x24\x03\x02\x01") -#include -const struct ccdigest_info *ccdigest_oid_lookup(ccoid_t oid, ...); -#endif /* USE_SUPER_COOL_NEW_CCOID_T*/ #endif /* _CORECRYPTO_CCDIGEST_H_ */