static CSSM_RETURN hashDataRateCryptKit(
	TestParams	*params)
{
	ckUpdateFcn		updatePtr = NULL;
	ckFinalFcn		finalPtr = NULL;
	ckInitFcn			initPtr = NULL;
	struct MD5Context	md5;
	sha1Obj				sha;
	void			*ctx;

	unsigned 		loop;
	unsigned		iloop;
	double			startTime, endTime;
	double			timeSpent, timeSpentMs;
	uint8			ptext[PTEXT_SIZE];
	uint8			digest[MAX_DIGEST_SIZE];
	unsigned		digestLen = 16;
	
	/* we reuse this one inside the loop */
	switch(params->algId) {
		case CSSM_ALGID_SHA1:
			sha = sha1Alloc();
			ctx = sha;
			initPtr = (ckInitFcn)sha1Reinit;
			updatePtr = (ckUpdateFcn)sha1AddData;
			finalPtr = (ckFinalFcn)ckSha1Final;
			digestLen = 20;
			break;
		case CSSM_ALGID_MD5:
			ctx = &md5;
			initPtr = (ckInitFcn)MD5Init;
			updatePtr = (ckUpdateFcn)MD5Update;
			finalPtr = (ckFinalFcn)MD5Final;
			break;
		default:
			printf("***Sorry, CryptKit can only do SHA1 and MD5.\n");
			return 1;
	}
		
	/* random data, const thru the loops */
	initPtext(ptext, PTEXT_SIZE);
	
	/* start critical timing loop */
	startTime = CFAbsoluteTimeGetCurrent();
	for(loop=0; loop<params->loops; loop++) {
		initPtr(ctx);
		for(iloop=0; iloop<INNER_LOOPS; iloop++) {
			updatePtr(ctx, ptext, PTEXT_SIZE);
		}
		finalPtr(ctx, digest);
	}
	endTime = CFAbsoluteTimeGetCurrent();
	timeSpent = endTime - startTime;
	timeSpentMs = timeSpent * 1000.0;
	
	float bytesPerLoop = INNER_LOOPS * PTEXT_SIZE;
	float totalBytes   = params->loops * bytesPerLoop;
	
	/* careful, KByte = 1024, ms = 1/1000 */
	printf("   Digest %.0f bytes : %u ops in %.2f ms; %f ms/op, %.0f KBytes/s\n",
		bytesPerLoop, params->loops, 
		timeSpentMs, timeSpentMs / (double)params->loops,
		((float)totalBytes / 1024.0) / timeSpent);
	if(params->dumpDigest) {
		dumpDigest(digest, digestLen);
	}
	return CSSM_OK;
}
예제 #2
0
int main()
{
	DDRB |= 1<<LED_PIN; // led pin as output
	PORTB |= 1<<SWITCH_PIN; // pullup for switch
	LED_OFF;
	_delay_ms(2);
	initPtr(); // cherche le point d'écriture dans le tampon
	
	if(ACQ_MODE) {
		int cnt = 0;
		int seconds = LOG_INTERVAL - 5; // first mesurement: 5 seconds after power on
		write_record(0x7F00, 0); // place a power on mark
		for(;;) {			
			if(ACQ_MODE) {				

				_delay_ms(20); // TODO work with timer interrupt

				if(cnt++>50) { // augmente le compteur de secondes
					seconds += 1;
					cnt = 0;
				}
								
				if(seconds>=LOG_INTERVAL) {
					if(sensorRead()) {
						write_record(sensor_bytes[0]<<8 | sensor_bytes[1], sensor_bytes[2]<<8 | sensor_bytes[3]);
						// clear le compteur de seconde uniquement en cas de succès de lecture (ceci permet de tolérer des échec de lecture du capteur)
						seconds = 0; 
					}
				}

				// Led clignotante pour signaler le mode aquisition
				if((cnt%25)>22) LED_ON; 
				else LED_OFF;

			}
			else {
				LED_OFF;
			}	
		}
	
	}		
	else {
		#if defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny25__)
		uint8_t calibrationValue = eeprom_read_byte(0); /* calibration value from last time */
		if (calibrationValue != 0xFF)
		{
			OSCCAL = calibrationValue;
		}
		#endif
	
		stdout = &mystdout; // set default stream
	
		// initialize report (I never assume it's initialized to 0 automatically)
		keyboard_report_reset();
	
		wdt_disable(); // disable watchdog, good habit if you don't use it
	
		// enforce USB re-enumeration by pretending to disconnect and reconnect
		usbDeviceDisconnect();
		_delay_ms(250);
		usbDeviceConnect();
	
		// initialize various modules
		usbInit();
	
		sei(); // enable interrupts

		while (1) // main loop, do forever
		{
			if (blink_count > 2) // activated by blinking lights
			{
				#if !defined(__AVR_ATtiny85__) && !defined(__AVR_ATtiny45__) && !defined(__AVR_ATtiny25__)
				DDRD |= _BV(1); // LED lights for debug
				PORTD |= _BV(1);
				#endif
				puts_P(PSTR("----------------------------------"));
				printf("LogStick 1.0 interval %d seconds\n", LOG_INTERVAL);
				if(ACQ_MODE) printf("Ptr %x\n", ptr);
				puts_P(PSTR("----------------------------------"));
				// PLACE TEXT HERE
				//puts_P(PSTR(" ")); // test size
				int humidity;
				int temperature;
				int p = ptr; // pointeur de la prochaine écriture en mode ACQ
				for(;;) {
					p -= 4;
					if(p<START_PTR) p = MAX_PTR-4;
					if(p==ptr) break;
					humidity = eeprom_read_word(p);
					temperature = eeprom_read_word(p+2);
					if(humidity==0x7F00) {
						if(ACQ_MODE) printf("%x:", p);
						puts_P(PSTR("---POWER ON---"));
					}						
					else if(humidity==0x7FFF) {
						puts_P(PSTR("---PTR MARK---")); // ne devrait pas se produire
					}						
					else {
						if(ACQ_MODE) printf("%x:%d.%d,%d.%d\n", p, humidity/10, humidity%10, temperature/10, temperature%10);
						else printf("%d.%d,%d.%d\n", humidity/10, humidity%10, temperature/10, temperature%10);
					}					
				}
				
				puts_P(PSTR("------------------------------END-"));
			
				blink_count = 0; // reset
			}
		
			// perform usb related background tasks
			usbPoll(); // this needs to be called at least once every 10 ms
			// this is also called in send_report_once
		}
	}	
	return 0;
}
static CSSM_RETURN hashDataRateCommonCrypto(
	TestParams	*params)
{
	CC_CTX			ctx;
	ccUpdateFcn		updatePtr = NULL;
	ccFinalFcn		finalPtr = NULL;
	ccInitFcn		initPtr = NULL;
	unsigned 		loop;
	unsigned		iloop;
	double			startTime, endTime;
	double			timeSpent, timeSpentMs;
	uint8			ptext[PTEXT_SIZE];
	uint8			digest[MAX_DIGEST_SIZE];
	unsigned		digestLen = 16;
	
	/* we reuse this one inside the loop */
	switch(params->algId) {
		case CSSM_ALGID_SHA1:
			initPtr = (ccInitFcn)CC_SHA1_Init;
			updatePtr = (ccUpdateFcn)CC_SHA1_Update;
			finalPtr = (ccFinalFcn)CC_SHA1_Final;
			digestLen = 20;
			break;
		case CSSM_ALGID_SHA224:
			initPtr = (ccInitFcn)CC_SHA224_Init;
			updatePtr = (ccUpdateFcn)CC_SHA224_Update;
			finalPtr = (ccFinalFcn)CC_SHA224_Final;
			digestLen = 28;
			break;
		case CSSM_ALGID_SHA256:
			initPtr = (ccInitFcn)CC_SHA256_Init;
			updatePtr = (ccUpdateFcn)CC_SHA256_Update;
			finalPtr = (ccFinalFcn)CC_SHA256_Final;
			digestLen = 32;
			break;
		case CSSM_ALGID_SHA384:
			initPtr = (ccInitFcn)CC_SHA384_Init;
			updatePtr = (ccUpdateFcn)CC_SHA384_Update;
			finalPtr = (ccFinalFcn)CC_SHA384_Final;
			digestLen = 48;
			break;
		case CSSM_ALGID_SHA512:
			initPtr = (ccInitFcn)CC_SHA512_Init;
			updatePtr = (ccUpdateFcn)CC_SHA512_Update;
			finalPtr = (ccFinalFcn)CC_SHA512_Final;
			digestLen = 64;
			break;
		case CSSM_ALGID_MD5:
			initPtr = (ccInitFcn)CC_MD5_Init;
			updatePtr = (ccUpdateFcn)CC_MD5_Update;
			finalPtr = (ccFinalFcn)CC_MD5_Final;
			digestLen = 16;
			break;
		default:
			printf("***BRRRZAP!\n");
			return 1;
	}
		
	/* random data, const thru the loops */
	initPtext(ptext, PTEXT_SIZE);
	
	/* start critical timing loop */
	startTime = CFAbsoluteTimeGetCurrent();
	for(loop=0; loop<params->loops; loop++) {
		initPtr(&ctx);
		for(iloop=0; iloop<INNER_LOOPS; iloop++) {
			updatePtr(&ctx, ptext, PTEXT_SIZE);
		}
		finalPtr(digest, &ctx);
	}
	endTime = CFAbsoluteTimeGetCurrent();
	timeSpent = endTime - startTime;
	timeSpentMs = timeSpent * 1000.0;
	
	float bytesPerLoop = INNER_LOOPS * PTEXT_SIZE;
	float totalBytes   = params->loops * bytesPerLoop;
	
	/* careful, KByte = 1024, ms = 1/1000 */
	printf("   Digest %.0f bytes : %u ops in %.2f ms; %f ms/op, %.0f KBytes/s\n",
		bytesPerLoop, params->loops, 
		timeSpentMs, timeSpentMs / (double)params->loops,
		((float)totalBytes / 1024.0) / timeSpent);
	if(params->dumpDigest) {
		dumpDigest(digest, digestLen);
	}
	return CSSM_OK;
}