api-samples.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. Code Examples
  2. =============
  3. Code Example For Symmetric Key Cipher Operation
  4. -----------------------------------------------
  5. ::
  6. /* tie all data structures together */
  7. struct skcipher_def {
  8. struct scatterlist sg;
  9. struct crypto_skcipher *tfm;
  10. struct skcipher_request *req;
  11. struct crypto_wait wait;
  12. };
  13. /* Perform cipher operation */
  14. static unsigned int test_skcipher_encdec(struct skcipher_def *sk,
  15. int enc)
  16. {
  17. int rc;
  18. if (enc)
  19. rc = crypto_wait_req(crypto_skcipher_encrypt(sk->req), &sk->wait);
  20. else
  21. rc = crypto_wait_req(crypto_skcipher_decrypt(sk->req), &sk->wait);
  22. if (rc)
  23. pr_info("skcipher encrypt returned with result %d\n", rc);
  24. return rc;
  25. }
  26. /* Initialize and trigger cipher operation */
  27. static int test_skcipher(void)
  28. {
  29. struct skcipher_def sk;
  30. struct crypto_skcipher *skcipher = NULL;
  31. struct skcipher_request *req = NULL;
  32. char *scratchpad = NULL;
  33. char *ivdata = NULL;
  34. unsigned char key[32];
  35. int ret = -EFAULT;
  36. skcipher = crypto_alloc_skcipher("cbc-aes-aesni", 0, 0);
  37. if (IS_ERR(skcipher)) {
  38. pr_info("could not allocate skcipher handle\n");
  39. return PTR_ERR(skcipher);
  40. }
  41. req = skcipher_request_alloc(skcipher, GFP_KERNEL);
  42. if (!req) {
  43. pr_info("could not allocate skcipher request\n");
  44. ret = -ENOMEM;
  45. goto out;
  46. }
  47. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  48. crypto_req_done,
  49. &sk.wait);
  50. /* AES 256 with random key */
  51. get_random_bytes(&key, 32);
  52. if (crypto_skcipher_setkey(skcipher, key, 32)) {
  53. pr_info("key could not be set\n");
  54. ret = -EAGAIN;
  55. goto out;
  56. }
  57. /* IV will be random */
  58. ivdata = kmalloc(16, GFP_KERNEL);
  59. if (!ivdata) {
  60. pr_info("could not allocate ivdata\n");
  61. goto out;
  62. }
  63. get_random_bytes(ivdata, 16);
  64. /* Input data will be random */
  65. scratchpad = kmalloc(16, GFP_KERNEL);
  66. if (!scratchpad) {
  67. pr_info("could not allocate scratchpad\n");
  68. goto out;
  69. }
  70. get_random_bytes(scratchpad, 16);
  71. sk.tfm = skcipher;
  72. sk.req = req;
  73. /* We encrypt one block */
  74. sg_init_one(&sk.sg, scratchpad, 16);
  75. skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata);
  76. crypto_init_wait(&sk.wait);
  77. /* encrypt data */
  78. ret = test_skcipher_encdec(&sk, 1);
  79. if (ret)
  80. goto out;
  81. pr_info("Encryption triggered successfully\n");
  82. out:
  83. if (skcipher)
  84. crypto_free_skcipher(skcipher);
  85. if (req)
  86. skcipher_request_free(req);
  87. if (ivdata)
  88. kfree(ivdata);
  89. if (scratchpad)
  90. kfree(scratchpad);
  91. return ret;
  92. }
  93. Code Example For Use of Operational State Memory With SHASH
  94. -----------------------------------------------------------
  95. ::
  96. struct sdesc {
  97. struct shash_desc shash;
  98. char ctx[];
  99. };
  100. static struct sdesc *init_sdesc(struct crypto_shash *alg)
  101. {
  102. struct sdesc *sdesc;
  103. int size;
  104. size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
  105. sdesc = kmalloc(size, GFP_KERNEL);
  106. if (!sdesc)
  107. return ERR_PTR(-ENOMEM);
  108. sdesc->shash.tfm = alg;
  109. sdesc->shash.flags = 0x0;
  110. return sdesc;
  111. }
  112. static int calc_hash(struct crypto_shash *alg,
  113. const unsigned char *data, unsigned int datalen,
  114. unsigned char *digest)
  115. {
  116. struct sdesc *sdesc;
  117. int ret;
  118. sdesc = init_sdesc(alg);
  119. if (IS_ERR(sdesc)) {
  120. pr_info("can't alloc sdesc\n");
  121. return PTR_ERR(sdesc);
  122. }
  123. ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
  124. kfree(sdesc);
  125. return ret;
  126. }
  127. static int test_hash(const unsigned char *data, unsigned int datalen,
  128. unsigned char *digest)
  129. {
  130. struct crypto_shash *alg;
  131. char *hash_alg_name = "sha1-padlock-nano";
  132. int ret;
  133. alg = crypto_alloc_shash(hash_alg_name, 0, 0);
  134. if (IS_ERR(alg)) {
  135. pr_info("can't alloc alg %s\n", hash_alg_name);
  136. return PTR_ERR(alg);
  137. }
  138. ret = calc_hash(alg, data, datalen, digest);
  139. crypto_free_shash(alg);
  140. return ret;
  141. }
  142. Code Example For Random Number Generator Usage
  143. ----------------------------------------------
  144. ::
  145. static int get_random_numbers(u8 *buf, unsigned int len)
  146. {
  147. struct crypto_rng *rng = NULL;
  148. char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
  149. int ret;
  150. if (!buf || !len) {
  151. pr_debug("No output buffer provided\n");
  152. return -EINVAL;
  153. }
  154. rng = crypto_alloc_rng(drbg, 0, 0);
  155. if (IS_ERR(rng)) {
  156. pr_debug("could not allocate RNG handle for %s\n", drbg);
  157. return PTR_ERR(rng);
  158. }
  159. ret = crypto_rng_get_bytes(rng, buf, len);
  160. if (ret < 0)
  161. pr_debug("generation of random numbers failed\n");
  162. else if (ret == 0)
  163. pr_debug("RNG returned no data");
  164. else
  165. pr_debug("RNG returned %d bytes of data\n", ret);
  166. out:
  167. crypto_free_rng(rng);
  168. return ret;
  169. }