Guest User

FreeBSD_crypto_example_error

a guest
Sep 3rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. /* tentative include files */
  2. #include <sys/param.h>
  3. #include <sys/module.h>
  4. #include <sys/kernel.h>
  5. #include <sys/systm.h>
  6. #include <sys/malloc.h>
  7. #include <sys/lock.h>
  8. #include <sys/mutex.h>
  9. #include <opencrypto/cryptodev.h>
  10.  
  11. struct mtx ds_lock;
  12. static int
  13. hmac_cmplted_cb(struct cryptop *crp)
  14. {
  15.     uprintf("hmac_cmplted_cb\n");
  16.     int error;
  17.     error = crp->crp_etype;
  18.     uprintf("Error:CB:%d\n", error);
  19.     if (error == EAGAIN)
  20.     {
  21.         uprintf("EAGAIN!\n");
  22.         error = crypto_dispatch(crp);
  23.     }
  24.     if (error || (crp->crp_flags & CRYPTO_F_DONE))
  25.     {
  26.         uprintf("Wakeup!\n");
  27.         wakeup(crp);
  28.     }
  29.     return (0);
  30. }
  31.  
  32. static void hmac_test()
  33. {
  34.     uprintf("Entering HMAC_test function\n");
  35.     crypto_session_t ds_session;
  36.     struct cryptoini cri[1];
  37.     char block_to_encrypt[24] = "ABCDABCD12341234ABCDABCD";
  38.     char key[11] = "neverGiveUp";
  39.     int error;
  40.    
  41.     bzero(cri, sizeof(cri));
  42.     //bzero(key, sizeof(key));
  43.  
  44.     cri[0].cri_alg = CRYPTO_SHA2_256_HMAC;
  45.     cri[0].cri_klen = 11;
  46.     cri[0].cri_mlen = 0;
  47.     cri[0].cri_key = key;
  48.     cri[0].cri_next = NULL;
  49.  
  50.     crypto_newsession(&ds_session, cri,
  51.             CRYPTOCAP_F_SOFTWARE);
  52.     //    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
  53.     uprintf("%p\n", (caddr_t)ds_session);
  54.  
  55.  
  56.     //Do the HMAC computation here.
  57.     struct cryptop *crp;
  58.     struct cryptodesc *crd;
  59.  
  60.     crp = crypto_getreq(1); //Allocating crp with one crp_desc.
  61.     crd = crp->crp_desc;
  62.  
  63.     crd->crd_skip = 0;
  64.     crd->crd_key = key;
  65.     crd->crd_klen = sizeof(key);
  66.     crd->crd_len = sizeof(block_to_encrypt);
  67.     crd->crd_flags = CRD_F_KEY_EXPLICIT;
  68.    
  69.     crd->crd_next = NULL;
  70.     crd->crd_alg = CRYPTO_SHA2_256_HMAC;
  71.  
  72.     crp->crp_session = ds_session;
  73.     crp->crp_flags = CRYPTO_F_CBIFSYNC;
  74.     //crp->crp_flags = 0;
  75.     crp->crp_buf = block_to_encrypt;
  76.     //crp->crp_ilen = sizeof(block_to_encrypt);
  77.     crp->crp_opaque = ds_session;
  78.     crp->crp_callback = hmac_cmplted_cb;
  79.  
  80.     error = crypto_dispatch(crp);
  81.     uprintf("Error %d\n", error);
  82.  
  83.     if ((crypto_ses2caps(ds_session) & CRYPTOCAP_F_SYNC) == 0) {
  84.         uprintf("OMG\n");
  85.         mtx_lock(&ds_lock);
  86.         if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
  87.             error = msleep(crp, &ds_lock, 0, "gssdes", 0);
  88.         mtx_unlock(&ds_lock);
  89.     }
  90.     u_int8_t *result = crp->crp_buf;
  91.     result = NULL;
  92.     uprintf("Error %d\n", error);
  93.     uprintf("output length %d\n", crp->crp_olen);
  94.     /*for(int i=0; i < crp->crp_olen ; i++)
  95.     {
  96.         uprintf("%x\n", result[i]);
  97.     }*/
  98.  
  99.     crypto_freereq(crp);
  100.     crypto_freesession(ds_session);
  101.     uprintf("Exiting HMAC_test_function\n");
  102. }
  103.  
  104. /* The function called at load/unload */
  105. static int event_handler(struct module *mod, int event, void *arg) {
  106.     int e = 0; /* Error, 0 for normal return status */
  107.     switch (event) {
  108.     case MOD_LOAD:
  109.         uprintf("Hello!\n");
  110.         hmac_test();
  111.         break;
  112.     case MOD_UNLOAD:
  113.         uprintf("Bye!\n");
  114.         break;
  115.     default:
  116.         e = EOPNOTSUPP;
  117.         break;
  118.     }
  119.  
  120.     return (e);
  121. }
  122.  
  123. /* The second argument of DECLARE_MODULE */
  124. static moduledata_t kdw_conf = {
  125.     "kdw",  /* module name */
  126.     event_handler,
  127.     NULL    /* extra data */
  128. };
  129.  
  130. DECLARE_MODULE(kdw, kdw_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
Add Comment
Please, Sign In to add comment