/* * Demo on how to use /dev/crypto device for ciphering. * * Placed under public domain. * */ #include #include #include #include #include #include "crypto/cryptodev.h" #include "sha.h" int sha_ctx_init(struct cryptodev_ctx* ctx, int cfd, const uint8_t *key, unsigned int key_size) { #ifdef CIOCGSESSINFO struct session_info_op siop; #endif memset(ctx, 0, sizeof(*ctx)); ctx->cfd = cfd; if (key == NULL) ctx->sess.mac = CRYPTO_SHA2_256; else { ctx->sess.mac = CRYPTO_SHA1_HMAC; ctx->sess.mackeylen = key_size; ctx->sess.mackey = (void*)key; } if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) { perror("ioctl(CIOCGSESSION)"); return -1; } #ifdef CIOCGSESSINFO siop.ses = ctx->sess.ses; if (ioctl(ctx->cfd, CIOCGSESSINFO, &siop)) { perror("ioctl(CIOCGSESSINFO)"); return -1; } printf("Got %s with driver %s\n", siop.hash_info.cra_name, siop.hash_info.cra_driver_name); //if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)) { //printf("Note: This is not an accelerated cipher\n"); //} /*printf("Alignmask is %x\n", (unsigned int)siop.alignmask);*/ ctx->alignmask = siop.alignmask; #endif return 0; } void sha_ctx_deinit(struct cryptodev_ctx* ctx) { if (ioctl(ctx->cfd, CIOCFSESSION, &ctx->sess.ses)) { perror("333333333ioctl(CIOCFSESSION)"); } } int sha_hash(struct cryptodev_ctx* ctx, const void* text, size_t size, void* digest) { struct crypt_op cryp; void* p; /* check text and ciphertext alignment */ if (ctx->alignmask) { p = (void*)(((unsigned long)text + ctx->alignmask) & ~ctx->alignmask); if (text != p) { fprintf(stderr, "text is not aligned\n"); return -1; } } memset(&cryp, 0, sizeof(cryp)); /* Encrypt data.in to data.encrypted */ cryp.ses = ctx->sess.ses; cryp.len = size; cryp.src = (void*)text; cryp.mac = digest; if (ioctl(ctx->cfd, CIOCCRYPT, &cryp)) { perror("1111111111111ioctl(CIOCCRYPT)"); return -1; } return 0; } int main() { int cfd = -1, i; struct cryptodev_ctx ctx; uint8_t digest[32]; char text[] = "\x08\x9f\x13\xaa\x41\xd8\x4c\xe3" "\x7a\x11\x85\x1c\xb3\x27\xbe\x55" "\xec\x60\xf7\x8e\x02\x99\x30\xc7" "\x3b\xd2\x69\x00\x74\x0b\xa2\x16" "\xad\x44\xdb\x4f\xe6\x7d\x14\x88" "\x1f\xb6\x2a\xc1\x58\xef\x63\xfa" "\x91\x05\x9c\x33\xca\x3e\xd5\x6c" "\x03\x77\x0e\xa5\x19\xb0\x47\xde" "\x52\xe9\x80\x17\x8b\x22\xb9\x2d" "\xc4\x5b\xf2\x66\xfd\x94\x08\x9f" "\x36\xcd\x41\xd8\x6f\x06\x7a\x11" "\xa8\x1c\xb3\x4a\xe1\x55\xec\x83" "\x1a\x8e\x25\xbc\x30\xc7\x5e\xf5" "\x69\x00\x97\x0b\xa2\x39\xd0\x44" "\xdb\x72\x09\x7d\x14\xab\x1f\xb6" "\x4d\xe4\x58\xef\x86\x1d\x91\x28" "\xbf\x33\xca\x61\xf8\x6c\x03\x9a" "\x0e\xa5\x3c\xd3\x47\xde\x75\x0c" "\x80\x17\xae\x22\xb9\x50\xe7\x5b" "\xf2\x89\x20\x94\x2b\xc2\x36\xcd" "\x64\xfb\x6f\x06\x9d\x11\xa8\x3f" "\xd6\x4a\xe1\x78\x0f\x83\x1a\xb1" "\x25\xbc\x53\xea\x5e\xf5\x8c\x00" "\x97\x2e\xc5\x39\xd0\x67\xfe\x72" "\x09\xa0\x14\xab\x42\xd9\x4d\xe4" "\x7b\x12\x86\x1d\xb4\x28\xbf\x56" "\xed\x61\xf8\x8f\x03\x9a\x31\xc8" "\x3c\xd3\x6a\x01\x75\x0c\xa3\x17" "\xae\x45\xdc\x50\xe7\x7e\x15\x89" "\x20\xb7\x2b\xc2\x59\xf0\x64\xfb" "\x92\x06\x9d\x34\xcb\x3f\xd6\x6d" "\x04\x78\x0f\xa6\x1a\xb1\x48\xdf" "\x53\xea\x81\x18\x8c\x23\xba\x2e" "\xc5\x5c\xf3\x67\xfe\x95\x09\xa0" "\x37\xce\x42\xd9\x70\x07\x7b\x12" "\xa9\x1d\xb4\x4b\xe2\x56\xed\x84" "\x1b\x8f\x26\xbd\x31\xc8\x5f\xf6" "\x6a\x01\x98\x0c\xa3\x3a\xd1\x45" "\xdc\x73\x0a\x7e\x15\xac\x20\xb7" "\x4e\xe5\x59\xf0\x87\x1e\x92\x29" "\xc0\x34\xcb\x62\xf9\x6d\x04\x9b" "\x0f\xa6\x3d\xd4\x48\xdf\x76\x0d" "\x81\x18\xaf\x23\xba\x51\xe8\x5c" "\xf3\x8a\x21\x95\x2c\xc3\x37\xce" "\x65\xfc\x70\x07\x9e\x12\xa9\x40" "\xd7\x4b\xe2\x79\x10\x84\x1b\xb2" "\x26\xbd\x54\xeb\x5f\xf6\x8d\x01" "\x98\x2f\xc6\x3a\xd1\x68\xff\x73" "\x0a\xa1\x15\xac\x43\xda\x4e\xe5" "\x7c\x13\x87\x1e\xb5\x29\xc0\x57" "\xee\x62\xf9\x90\x04\x9b\x32\xc9" "\x3d\xd4\x6b\x02\x76\x0d\xa4\x18" "\xaf\x46\xdd\x51\xe8\x7f\x16\x8a" "\x21\xb8\x2c\xc3\x5a\xf1\x65\xfc" "\x93\x07\x9e\x35\xcc\x40\xd7\x6e" "\x05\x79\x10\xa7\x1b\xb2\x49\xe0" "\x54\xeb\x82\x19\x8d\x24\xbb\x2f" "\xc6\x5d\xf4\x68\xff\x96\x0a\xa1" "\x38\xcf\x43\xda\x71\x08\x7c\x13" "\xaa\x1e\xb5\x4c\xe3\x57\xee\x85" "\x1c\x90\x27\xbe\x32\xc9\x60\xf7" "\x6b\x02\x99\x0d\xa4\x3b\xd2\x46" "\xdd\x74\x0b\x7f\x16\xad\x21\xb8" "\x4f\xe6\x5a\xf1\x88\x1f\x93\x2a" "\xc1\x35\xcc\x63\xfa\x6e\x05\x9c" "\x10\xa7\x3e\xd5\x49\xe0\x77\x0e" "\x82\x19\xb0\x24\xbb\x52\xe9\x5d" "\xf4\x8b\x22\x96\x2d\xc4\x38\xcf" "\x66\xfd\x71\x08\x9f\x13\xaa\x41" "\xd8\x4c\xe3\x7a\x11\x85\x1c\xb3" "\x27\xbe\x55\xec\x60\xf7\x8e\x02" "\x99\x30\xc7\x3b\xd2\x69\x00\x74" "\x0b\xa2\x16\xad\x44\xdb\x4f\xe6" "\x7d\x14\x88\x1f\xb6\x2a\xc1\x58" "\xef\x63\xfa\x91\x05\x9c\x33\xca" "\x3e\xd5\x6c\x03\x77\x0e\xa5\x19" "\xb0\x47\xde\x52\xe9\x80\x17\x8b" "\x22\xb9\x2d\xc4\x5b\xf2\x66\xfd" "\x94\x08\x9f\x36\xcd\x41\xd8\x6f" "\x06\x7a\x11\xa8\x1c\xb3\x4a\xe1" "\x55\xec\x83\x1a\x8e\x25\xbc\x30" "\xc7\x5e\xf5\x69\x00\x97\x0b\xa2" "\x39\xd0\x44\xdb\x72\x09\x7d\x14" "\xab\x1f\xb6\x4d\xe4\x58\xef\x86" "\x1d\x91\x28\xbf\x33\xca\x61\xf8" "\x6c\x03\x9a\x0e\xa5\x3c\xd3\x47" "\xde\x75\x0c\x80\x17\xae\x22\xb9" "\x50\xe7\x5b\xf2\x89\x20\x94\x2b" "\xc2\x36\xcd\x64\xfb\x6f\x06\x9d" "\x11\xa8\x3f\xd6\x4a\xe1\x78\x0f" "\x83\x1a\xb1\x25\xbc\x53\xea\x5e" "\xf5\x8c\x00\x97\x2e\xc5\x39\xd0" "\x67\xfe\x72\x09\xa0\x14\xab\x42" "\xd9\x4d\xe4\x7b\x12\x86\x1d\xb4" "\x28\xbf\x56\xed\x61\xf8\x8f\x03" "\x9a\x31\xc8\x3c\xd3\x6a\x01\x75" "\x0c\xa3\x17\xae\x45\xdc\x50\xe7" "\x7e\x15\x89\x20\xb7\x2b\xc2\x59" "\xf0\x64\xfb\x92\x06\x9d\x34\xcb" "\x3f\xd6\x6d\x04\x78\x0f\xa6\x1a" "\xb1\x48\xdf\x53\xea\x81\x18\x8c" "\x23\xba\x2e\xc5\x5c\xf3\x67\xfe" "\x95\x09\xa0\x37\xce\x42\xd9\x70" "\x07\x7b\x12\xa9\x1d\xb4\x4b\xe2" "\x56\xed\x84\x1b\x8f\x26\xbd\x31" "\xc8\x5f\xf6\x6a\x01\x98\x0c\xa3" "\x3a\xd1\x45\xdc\x73\x0a\x7e\x15" "\xac\x20\xb7\x4e\xe5\x59\xf0\x87" "\x1e\x92\x29\xc0\x34\xcb\x62\xf9" "\x6d\x04\x9b\x0f\xa6\x3d\xd4\x48" "\xdf\x76\x0d\x81\x18\xaf\x23\xba" "\x51\xe8\x5c\xf3\x8a\x21\x95\x2c" "\xc3\x37\xce\x65\xfc\x70\x07\x9e" "\x12\xa9\x40\xd7\x4b\xe2\x79\x10" "\x84\x1b\xb2\x26\xbd\x54\xeb\x5f" "\xf6\x8d\x01\x98\x2f\xc6\x3a\xd1" "\x68\xff\x73\x0a\xa1\x15\xac\x43" "\xda\x4e\xe5\x7c\x13\x87\x1e\xb5" "\x29\xc0\x57\xee\x62\xf9\x90\x04" "\x9b\x32\xc9\x3d\xd4\x6b\x02\x76" "\x0d\xa4\x18\xaf\x46\xdd\x51\xe8" "\x7f\x16\x8a\x21\xb8\x2c\xc3\x5a" "\xf1\x65\xfc\x93\x07\x9e\x35\xcc" "\x40\xd7\x6e\x05\x79\x10\xa7\x1b" "\xb2\x49\xe0\x54\xeb\x82\x19\x8d" "\x24\xbb\x2f\xc6\x5d\xf4\x68\xff" "\x96\x0a\xa1\x38\xcf\x43\xda\x71" "\x08\x7c\x13\xaa\x1e\xb5\x4c"; uint8_t expected[] = "\xc5\xce\x0c\xca\x01\x4f\x53\x3a" "\x32\x32\x17\xcc\xd4\x6a\x71\xa9" "\xf3\xed\x50\x10\x64\x8e\x06\xbe" "\x9b\x4a\xa6\xbb\x05\x89\x59\x51"; /* Open the crypto device */ cfd = open("/dev/crypto", O_RDWR, 0); if (cfd < 0) { perror("open(/dev/crypto)"); return 1; } /* Set close-on-exec (not really neede here) */ if (fcntl(cfd, F_SETFD, 1) == -1) { perror("fcntl(F_SETFD)"); return 1; } sha_ctx_init(&ctx, cfd, NULL, 0); sha_hash(&ctx, text,sizeof(text)-1, digest); sha_ctx_deinit(&ctx); printf("digest: "); for (i = 0; i < 20; i++) { printf("%02x:", digest[i]); } printf("\n"); if (memcmp(digest, expected, 32) != 0) { fprintf(stderr, "SHA1 hashing failed\n"); return 1; }else{ printf("hash test pass\r\n"); } /* Close the original descriptor */ if (close(cfd)) { perror("close(cfd)"); return 1; } return 0; }