/* * Demo on how to use /dev/crypto device for ciphering. * * Placed under public domain. * */ #include #include #include #include #include #include #include #define DATA_SIZE (8*1024) #define AUTH_SIZE 31 #define BLOCK_SIZE 16 #define KEY_SIZE 16 #define MAC_SIZE 20 /* SHA1 */ static int debug = 0; static int get_sha1_hmac(int cfd, void* key, int key_size, void* data1, int data1_size, void* data2, int data2_size, void* mac) { struct session_op sess; struct crypt_op cryp; memset(&sess, 0, sizeof(sess)); memset(&cryp, 0, sizeof(cryp)); sess.cipher = 0; sess.mac = CRYPTO_SHA1_HMAC; sess.mackeylen = key_size; sess.mackey = key; if (ioctl(cfd, CIOCGSESSION, &sess)) { perror("ioctl(CIOCGSESSION)"); return 1; } /* Encrypt data.in to data.encrypted */ cryp.ses = sess.ses; cryp.len = data1_size; cryp.src = data1; cryp.dst = NULL; cryp.iv = NULL; cryp.mac = mac; cryp.op = COP_ENCRYPT; cryp.flags = COP_FLAG_UPDATE; if (ioctl(cfd, CIOCCRYPT, &cryp)) { perror("ioctl(CIOCCRYPT)"); return 1; } cryp.ses = sess.ses; cryp.len = data2_size; cryp.src = data2; cryp.dst = NULL; cryp.iv = NULL; cryp.mac = mac; cryp.op = COP_ENCRYPT; cryp.flags = COP_FLAG_FINAL; if (ioctl(cfd, CIOCCRYPT, &cryp)) { perror("ioctl(CIOCCRYPT)"); return 1; } /* Finish crypto session */ if (ioctl(cfd, CIOCFSESSION, &sess.ses)) { perror("ioctl(CIOCFSESSION)"); return 1; } return 0; } static void print_buf(char* desc, unsigned char* buf, int size) { int i; fputs(desc, stdout); for (i=0;i