/* Copyright (c) (2016,2017,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. */ #include #include #include #include "thermalCrypto.h" #include // extern void ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, size_t len, const void *data); typedef struct { const struct ccmode_ctr *ctr; void *key; size_t len; const void *dataIn; void *dataOut; } Parameters; void BlockCTR(const Parameters *parameters); // this templtae works for both encrypt and decrypt void BlockCTR(const Parameters *parameters) { parameters->ctr->ctr((ccctr_ctx *)parameters->key, parameters->len, parameters->dataIn, parameters->dataOut); } static void Driver(unsigned int iterations, void *parameters) { Parameters *p = (Parameters *) parameters; while (iterations--) { BlockCTR(p); } } extern uint32_t single_test; void thermalAES_CTR(uint32_t ITERATIONS, uint32_t data_size) { char *dataIn, *dataEncrypted, *dataDecrypted; uint32_t i; double TotalETime = 0; double TotalDTime = 0; uint32_t NUM_BLOCKS = data_size/16; if ((NUM_BLOCKS*16)!=data_size) { data_size = 16*NUM_BLOCKS; fprintf(stderr,"warning : adjusted data_size to %d\n", data_size); } const struct ccmode_ctr *encrypt = ccaes_ctr_crypt_mode(); const struct ccmode_ctr *decrypt = ccaes_ctr_crypt_mode(); unsigned int keyLen=16; uint8_t gkey[32]; if (!single_test) for (i=0;i<32;i++) gkey[i] = (uint8_t)arc4random(); ccctr_ctx_decl(encrypt->size, ekey); encrypt->init(encrypt, ekey, keyLen, (const uint8_t *) gkey, "66E69A111892584FB5ED52070383EDBF"); ccctr_ctx_decl(decrypt->size, dkey); decrypt->init(decrypt, dkey, keyLen, (const uint8_t *) gkey, "66E69A111892584FB5ED52070383EDBF"); if (!(dataIn = calloc(data_size, 1))) { fprintf(stderr,"error : calloc dataIn %d \n", data_size); exit(1); } if (!(dataEncrypted = calloc(data_size, 1))) { fprintf(stderr,"error : calloc dataEncrypted %d \n", data_size); exit(1); } if (!(dataDecrypted = calloc(data_size, 1))) { fprintf(stderr,"error : calloc dataDecrypted %d \n", data_size); exit(1); } if (!single_test) { for (i=0;i /* function that tests corner cases (various number of input blocks) with known constants in the key and data */ #define Dsize 4096 #define nBlocks (Dsize/16) uint32_t validate_ctr_checksum[] = { 0xecffb7ba, 0x01b271df, 0xbc1f1137 }; void validateAES_CTR(void); void validateAES_CTR(void) { unsigned char dataIn[Dsize], dataEncrypted[Dsize], dataDecrypted[Dsize]; size_t i, j; const struct ccmode_ctr *encrypt = ccaes_ctr_crypt_mode(); const struct ccmode_ctr *decrypt = ccaes_ctr_crypt_mode(); ccctr_ctx_decl(encrypt->size, ekey); ccctr_ctx_decl(decrypt->size, dkey); unsigned int keyLen; uint8_t gkey[32]; for (i=0;i<32;i++) gkey[i] = (uint8_t)i; for (i=0;iinit(encrypt, ekey, keyLen, (const uint8_t *) gkey, "66E69A111892584FB5ED52070383EDBF"); decrypt->init(decrypt, dkey, keyLen, (const uint8_t *) gkey, "66E69A111892584FB5ED52070383EDBF"); /* encrypt in nblocks of 1,2,...,10,(256-55) */ j=0; for (i=1;i<=10;i++) { encrypt->ctr(ekey, i<<4, &dataIn[j], &dataEncrypted[j]); j+=(i<<4); } i = (4096-j)>>4; encrypt->ctr(ekey, i<<4, &dataIn[j], &dataEncrypted[j]); /* decrypt in nblocks of 1,2,...,10,(256-55) */ j=0; for (i=1;i<=10;i++) { decrypt->ctr(dkey, i<<4, &dataEncrypted[j], &dataDecrypted[j]); j+=(i<<4); } i = (4096-j)>>4; decrypt->ctr(dkey, i<<4, &dataEncrypted[j], &dataDecrypted[j]); /* check whether dataIn == dataDecrypted */ for (i=0;i