/* * Demo on how to use /dev/crypto device for HMAC. * * Placed under public domain. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../alg.h" struct hash_vectors_st { const char *algorithm; const uint8_t *key; /* if hmac */ int key_size; const uint8_t *plaintext; int plaintext_size; const uint8_t *output; int output_size; } hash_vectors[] = { { .algorithm = "sha1",.key = NULL,.plaintext = (uint8_t *) "what do ya want for nothing?",. plaintext_size = sizeof("what do ya want for nothing?") - 1,.output = (uint8_t *) "\x8f\x82\x03\x94\xf9\x53\x35\x18\x20\x45\xda\x24\xf3\x4d\xe5\x2b\xf8\xbc\x34\x32",. output_size = 20} , #if 0 { .algorithm = "hmac(md5)",.key = (uint8_t *) "Jefe",.key_size = 4,.plaintext = (uint8_t *) "what do ya want for nothing?",. plaintext_size = sizeof("what do ya want for nothing?") - 1,.output = (uint8_t *) "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38",. output_size = 16} , /* from rfc4231 */ { .algorithm = "hmac(sha224)",.key = (uint8_t *) "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",. key_size = 20,.plaintext = (uint8_t *) "Hi There",.plaintext_size = sizeof("Hi There") - 1,.output = (uint8_t *) "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22",. output_size = 28} , { .algorithm = "hmac(sha256)",.key = (uint8_t *) "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",. key_size = 20,.plaintext = (uint8_t *) "Hi There",.plaintext_size = sizeof("Hi There") - 1,.output = (uint8_t *) "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7",. output_size = 32} , { .algorithm = "hmac(sha384)",.key = (uint8_t *) "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",. key_size = 20,.plaintext = (uint8_t *) "Hi There",.plaintext_size = sizeof("Hi There") - 1,.output = (uint8_t *) "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2\xfa\x9c\xb6",. output_size = 48} , { .algorithm = "hmac(sha512)",.key = (uint8_t *) "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",. key_size = 20,.plaintext = (uint8_t *) "Hi There",.plaintext_size = sizeof("Hi There") - 1,.output = (uint8_t *) "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54",. output_size = 64} #endif }; #define HASH_DATA_SIZE 64 /* SHA1 and other hashes */ static int test_ncr_hash(int with_accept) { uint8_t data[HASH_DATA_SIZE]; int i, j; ssize_t data_size; /* convert it to key */ if (with_accept) fprintf(stdout, "Tests on Hashes with accept()\n"); else fprintf(stdout, "Tests on Hashes without accept()\n"); for (i = 0; i < sizeof(hash_vectors) / sizeof(hash_vectors[0]); i++) { struct sockaddr_alg salg; int fd, hfd; fprintf(stdout, "\t%s:\n", hash_vectors[i].algorithm); fd = socket(PF_ALG, SOCK_STREAM, 0); if (fd < 0) { perror("socket()"); return 1; } salg.salg_family = AF_ALG; strcpy(salg.salg_type, "hash"); strcpy(salg.salg_tfm, hash_vectors[i].algorithm); if (bind(fd, (struct sockaddr *)&salg, sizeof(salg)) != 0) { perror("bind()"); return 1; } if (with_accept) { if (listen(fd, 1) != 0) { perror("listen()"); return 1; } hfd = accept(fd, NULL, NULL); if (hfd < 0) { perror("accept()"); return 1; } close(fd); } else hfd = fd; errno = 0; if (write(hfd, hash_vectors[i].plaintext, hash_vectors[i].plaintext_size) != hash_vectors[i].plaintext_size) { perror("write()"); return 1; } errno = 0; data_size = read(hfd, data, sizeof(data)); if (data_size < 0) { perror("read()"); return 1; } close(hfd); if (data_size != hash_vectors[i].output_size || memcmp(data, hash_vectors[i].output, hash_vectors[i].output_size) != 0) { fprintf(stderr, "HASH test vector %d failed!\n", i); fprintf(stderr, "Output[%d]: ", (int)data_size); for (j = 0; j < data_size; j++) fprintf(stderr, "%.2x:", (int)data[j]); fprintf(stderr, "\n"); fprintf(stderr, "Expected[%d]: ", hash_vectors[i].output_size); for (j = 0; j < hash_vectors[i].output_size; j++) fprintf(stderr, "%.2x:", (int)hash_vectors[i].output[j]); fprintf(stderr, "\n"); return 1; } } fprintf(stdout, "\n"); return 0; } int main() { if (test_ncr_hash(0)) return 1; if (test_ncr_hash(1)) return 1; return 0; }