/* Poly1305Do writes the Poly1305 authenticator of the given additional data * and ciphertext to |out|. */ static void Poly1305Do(unsigned char *out, const unsigned char *ad, unsigned int adLen, const unsigned char *ciphertext, unsigned int ciphertextLen, const unsigned char key[32]) { poly1305_state state; unsigned int j; unsigned char lengthBytes[8]; unsigned int i; Poly1305Init(&state, key); j = adLen; for (i = 0; i < sizeof(lengthBytes); i++) { lengthBytes[i] = j; j >>= 8; } Poly1305Update(&state, ad, adLen); Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); j = ciphertextLen; for (i = 0; i < sizeof(lengthBytes); i++) { lengthBytes[i] = j; j >>= 8; } Poly1305Update(&state, ciphertext, ciphertextLen); Poly1305Update(&state, lengthBytes, sizeof(lengthBytes)); Poly1305Finish(&state, out); }
void bench_poly1305() { Poly1305 enc; byte mac[16]; double start, total, persec; int i; int ret; ret = Poly1305SetKey(&enc, key, 32); if (ret != 0) { printf("Poly1305SetKey failed, ret = %d\n", ret); return; } start = current_time(1); for(i = 0; i < numBlocks; i++) Poly1305Update(&enc, plain, sizeof(plain)); Poly1305Final(&enc, mac); total = current_time(0) - start; persec = 1 / total * numBlocks; #ifdef BENCH_EMBEDDED /* since using kB, convert to MB/s */ persec = persec / 1024; #endif printf("POLY1305 %d %s took %5.3f seconds, %7.3f MB/s\n", numBlocks, blockType, total, persec); }