api-samples.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Code Examples
  2. =============
  3. Code Example For Symmetric Key Cipher Operation
  4. -----------------------------------------------
  5. This code encrypts some data with AES-256-XTS. For sake of example,
  6. all inputs are random bytes, the encryption is done in-place, and it's
  7. assumed the code is running in a context where it can sleep.
  8. ::
  9. static int test_skcipher(void)
  10. {
  11. struct crypto_skcipher *tfm = NULL;
  12. struct skcipher_request *req = NULL;
  13. u8 *data = NULL;
  14. const size_t datasize = 512; /* data size in bytes */
  15. struct scatterlist sg;
  16. DECLARE_CRYPTO_WAIT(wait);
  17. u8 iv[16]; /* AES-256-XTS takes a 16-byte IV */
  18. u8 key[64]; /* AES-256-XTS takes a 64-byte key */
  19. int err;
  20. /*
  21. * Allocate a tfm (a transformation object) and set the key.
  22. *
  23. * In real-world use, a tfm and key are typically used for many
  24. * encryption/decryption operations. But in this example, we'll just do a
  25. * single encryption operation with it (which is not very efficient).
  26. */
  27. tfm = crypto_alloc_skcipher("xts(aes)", 0, 0);
  28. if (IS_ERR(tfm)) {
  29. pr_err("Error allocating xts(aes) handle: %ld\n", PTR_ERR(tfm));
  30. return PTR_ERR(tfm);
  31. }
  32. get_random_bytes(key, sizeof(key));
  33. err = crypto_skcipher_setkey(tfm, key, sizeof(key));
  34. if (err) {
  35. pr_err("Error setting key: %d\n", err);
  36. goto out;
  37. }
  38. /* Allocate a request object */
  39. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  40. if (!req) {
  41. err = -ENOMEM;
  42. goto out;
  43. }
  44. /* Prepare the input data */
  45. data = kmalloc(datasize, GFP_KERNEL);
  46. if (!data) {
  47. err = -ENOMEM;
  48. goto out;
  49. }
  50. get_random_bytes(data, datasize);
  51. /* Initialize the IV */
  52. get_random_bytes(iv, sizeof(iv));
  53. /*
  54. * Encrypt the data in-place.
  55. *
  56. * For simplicity, in this example we wait for the request to complete
  57. * before proceeding, even if the underlying implementation is asynchronous.
  58. *
  59. * To decrypt instead of encrypt, just change crypto_skcipher_encrypt() to
  60. * crypto_skcipher_decrypt().
  61. */
  62. sg_init_one(&sg, data, datasize);
  63. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  64. CRYPTO_TFM_REQ_MAY_SLEEP,
  65. crypto_req_done, &wait);
  66. skcipher_request_set_crypt(req, &sg, &sg, datasize, iv);
  67. err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  68. if (err) {
  69. pr_err("Error encrypting data: %d\n", err);
  70. goto out;
  71. }
  72. pr_debug("Encryption was successful\n");
  73. out:
  74. crypto_free_skcipher(tfm);
  75. skcipher_request_free(req);
  76. kfree(data);
  77. return err;
  78. }
  79. Code Example For Use of Operational State Memory With SHASH
  80. -----------------------------------------------------------
  81. ::
  82. struct sdesc {
  83. struct shash_desc shash;
  84. char ctx[];
  85. };
  86. static struct sdesc *init_sdesc(struct crypto_shash *alg)
  87. {
  88. struct sdesc *sdesc;
  89. int size;
  90. size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
  91. sdesc = kmalloc(size, GFP_KERNEL);
  92. if (!sdesc)
  93. return ERR_PTR(-ENOMEM);
  94. sdesc->shash.tfm = alg;
  95. return sdesc;
  96. }
  97. static int calc_hash(struct crypto_shash *alg,
  98. const unsigned char *data, unsigned int datalen,
  99. unsigned char *digest)
  100. {
  101. struct sdesc *sdesc;
  102. int ret;
  103. sdesc = init_sdesc(alg);
  104. if (IS_ERR(sdesc)) {
  105. pr_info("can't alloc sdesc\n");
  106. return PTR_ERR(sdesc);
  107. }
  108. ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
  109. kfree(sdesc);
  110. return ret;
  111. }
  112. static int test_hash(const unsigned char *data, unsigned int datalen,
  113. unsigned char *digest)
  114. {
  115. struct crypto_shash *alg;
  116. char *hash_alg_name = "sha1-padlock-nano";
  117. int ret;
  118. alg = crypto_alloc_shash(hash_alg_name, 0, 0);
  119. if (IS_ERR(alg)) {
  120. pr_info("can't alloc alg %s\n", hash_alg_name);
  121. return PTR_ERR(alg);
  122. }
  123. ret = calc_hash(alg, data, datalen, digest);
  124. crypto_free_shash(alg);
  125. return ret;
  126. }
  127. Code Example For Random Number Generator Usage
  128. ----------------------------------------------
  129. ::
  130. static int get_random_numbers(u8 *buf, unsigned int len)
  131. {
  132. struct crypto_rng *rng = NULL;
  133. char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
  134. int ret;
  135. if (!buf || !len) {
  136. pr_debug("No output buffer provided\n");
  137. return -EINVAL;
  138. }
  139. rng = crypto_alloc_rng(drbg, 0, 0);
  140. if (IS_ERR(rng)) {
  141. pr_debug("could not allocate RNG handle for %s\n", drbg);
  142. return PTR_ERR(rng);
  143. }
  144. ret = crypto_rng_get_bytes(rng, buf, len);
  145. if (ret < 0)
  146. pr_debug("generation of random numbers failed\n");
  147. else if (ret == 0)
  148. pr_debug("RNG returned no data");
  149. else
  150. pr_debug("RNG returned %d bytes of data\n", ret);
  151. out:
  152. crypto_free_rng(rng);
  153. return ret;
  154. }