€•T;Œsphinx.addnodes”Œdocument”“”)”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)”}”(hhh]”(hŒ pending_xref”“”)”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…””}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ&/translations/zh_CN/crypto/api-samples”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)”}”(hhh]”hŒChinese (Traditional)”…””}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/zh_TW/crypto/api-samples”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒItalian”…””}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/it_IT/crypto/api-samples”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒJapanese”…””}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/ja_JP/crypto/api-samples”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒKorean”…””}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/ko_KR/crypto/api-samples”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒSpanish”…””}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/sp_SP/crypto/api-samples”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒsection”“”)”}”(hhh]”(hŒtitle”“”)”}”(hŒ Code Examples”h]”hŒ Code Examples”…””}”(hh¨hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hh£hžhhŸŒ@/var/lib/git/docbuild/linux/Documentation/crypto/api-samples.rst”h Kubh¢)”}”(hhh]”(h§)”}”(hŒ/Code Example For Symmetric Key Cipher Operation”h]”hŒ/Code Example For Symmetric Key Cipher Operation”…””}”(hhºhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hh·hžhhŸh¶h KubhŒ paragraph”“”)”}”(hŒÈThis code encrypts some data with AES-256-XTS. For sake of example, all inputs are random bytes, the encryption is done in-place, and it's assumed the code is running in a context where it can sleep.”h]”hŒÊThis code encrypts some data with AES-256-XTS. For sake of example, all inputs are random bytes, the encryption is done in-place, and it’s assumed the code is running in a context where it can sleep.”…””}”(hhÊhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÈhŸh¶h Khh·hžhubhŒ literal_block”“”)”}”(hXs static int test_skcipher(void) { struct crypto_skcipher *tfm = NULL; struct skcipher_request *req = NULL; u8 *data = NULL; const size_t datasize = 512; /* data size in bytes */ struct scatterlist sg; DECLARE_CRYPTO_WAIT(wait); u8 iv[16]; /* AES-256-XTS takes a 16-byte IV */ u8 key[64]; /* AES-256-XTS takes a 64-byte key */ int err; /* * Allocate a tfm (a transformation object) and set the key. * * In real-world use, a tfm and key are typically used for many * encryption/decryption operations. But in this example, we'll just do a * single encryption operation with it (which is not very efficient). */ tfm = crypto_alloc_skcipher("xts(aes)", 0, 0); if (IS_ERR(tfm)) { pr_err("Error allocating xts(aes) handle: %ld\n", PTR_ERR(tfm)); return PTR_ERR(tfm); } get_random_bytes(key, sizeof(key)); err = crypto_skcipher_setkey(tfm, key, sizeof(key)); if (err) { pr_err("Error setting key: %d\n", err); goto out; } /* Allocate a request object */ req = skcipher_request_alloc(tfm, GFP_KERNEL); if (!req) { err = -ENOMEM; goto out; } /* Prepare the input data */ data = kmalloc(datasize, GFP_KERNEL); if (!data) { err = -ENOMEM; goto out; } get_random_bytes(data, datasize); /* Initialize the IV */ get_random_bytes(iv, sizeof(iv)); /* * Encrypt the data in-place. * * For simplicity, in this example we wait for the request to complete * before proceeding, even if the underlying implementation is asynchronous. * * To decrypt instead of encrypt, just change crypto_skcipher_encrypt() to * crypto_skcipher_decrypt(). */ sg_init_one(&sg, data, datasize); skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, crypto_req_done, &wait); skcipher_request_set_crypt(req, &sg, &sg, datasize, iv); err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); if (err) { pr_err("Error encrypting data: %d\n", err); goto out; } pr_debug("Encryption was successful\n"); out: crypto_free_skcipher(tfm); skcipher_request_free(req); kfree(data); return err; }”h]”hXs static int test_skcipher(void) { struct crypto_skcipher *tfm = NULL; struct skcipher_request *req = NULL; u8 *data = NULL; const size_t datasize = 512; /* data size in bytes */ struct scatterlist sg; DECLARE_CRYPTO_WAIT(wait); u8 iv[16]; /* AES-256-XTS takes a 16-byte IV */ u8 key[64]; /* AES-256-XTS takes a 64-byte key */ int err; /* * Allocate a tfm (a transformation object) and set the key. * * In real-world use, a tfm and key are typically used for many * encryption/decryption operations. But in this example, we'll just do a * single encryption operation with it (which is not very efficient). */ tfm = crypto_alloc_skcipher("xts(aes)", 0, 0); if (IS_ERR(tfm)) { pr_err("Error allocating xts(aes) handle: %ld\n", PTR_ERR(tfm)); return PTR_ERR(tfm); } get_random_bytes(key, sizeof(key)); err = crypto_skcipher_setkey(tfm, key, sizeof(key)); if (err) { pr_err("Error setting key: %d\n", err); goto out; } /* Allocate a request object */ req = skcipher_request_alloc(tfm, GFP_KERNEL); if (!req) { err = -ENOMEM; goto out; } /* Prepare the input data */ data = kmalloc(datasize, GFP_KERNEL); if (!data) { err = -ENOMEM; goto out; } get_random_bytes(data, datasize); /* Initialize the IV */ get_random_bytes(iv, sizeof(iv)); /* * Encrypt the data in-place. * * For simplicity, in this example we wait for the request to complete * before proceeding, even if the underlying implementation is asynchronous. * * To decrypt instead of encrypt, just change crypto_skcipher_encrypt() to * crypto_skcipher_decrypt(). */ sg_init_one(&sg, data, datasize); skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, crypto_req_done, &wait); skcipher_request_set_crypt(req, &sg, &sg, datasize, iv); err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); if (err) { pr_err("Error encrypting data: %d\n", err); goto out; } pr_debug("Encryption was successful\n"); out: crypto_free_skcipher(tfm); skcipher_request_free(req); kfree(data); return err; }”…””}”hhÚsbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1hØhŸh¶h K hh·hžhubeh}”(h]”Œ/code-example-for-symmetric-key-cipher-operation”ah ]”h"]”Œ/code example for symmetric key cipher operation”ah$]”h&]”uh1h¡hh£hžhhŸh¶h Kubh¢)”}”(hhh]”(h§)”}”(hŒ;Code Example For Use of Operational State Memory With SHASH”h]”hŒ;Code Example For Use of Operational State Memory With SHASH”…””}”(hhõhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hhòhžhhŸh¶h K^ubhÙ)”}”(hXêstruct sdesc { struct shash_desc shash; char ctx[]; }; static struct sdesc *init_sdesc(struct crypto_shash *alg) { struct sdesc *sdesc; int size; size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); sdesc = kmalloc(size, GFP_KERNEL); if (!sdesc) return ERR_PTR(-ENOMEM); sdesc->shash.tfm = alg; return sdesc; } static int calc_hash(struct crypto_shash *alg, const unsigned char *data, unsigned int datalen, unsigned char *digest) { struct sdesc *sdesc; int ret; sdesc = init_sdesc(alg); if (IS_ERR(sdesc)) { pr_info("can't alloc sdesc\n"); return PTR_ERR(sdesc); } ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest); kfree(sdesc); return ret; } static int test_hash(const unsigned char *data, unsigned int datalen, unsigned char *digest) { struct crypto_shash *alg; char *hash_alg_name = "sha1-padlock-nano"; int ret; alg = crypto_alloc_shash(hash_alg_name, 0, 0); if (IS_ERR(alg)) { pr_info("can't alloc alg %s\n", hash_alg_name); return PTR_ERR(alg); } ret = calc_hash(alg, data, datalen, digest); crypto_free_shash(alg); return ret; }”h]”hXêstruct sdesc { struct shash_desc shash; char ctx[]; }; static struct sdesc *init_sdesc(struct crypto_shash *alg) { struct sdesc *sdesc; int size; size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); sdesc = kmalloc(size, GFP_KERNEL); if (!sdesc) return ERR_PTR(-ENOMEM); sdesc->shash.tfm = alg; return sdesc; } static int calc_hash(struct crypto_shash *alg, const unsigned char *data, unsigned int datalen, unsigned char *digest) { struct sdesc *sdesc; int ret; sdesc = init_sdesc(alg); if (IS_ERR(sdesc)) { pr_info("can't alloc sdesc\n"); return PTR_ERR(sdesc); } ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest); kfree(sdesc); return ret; } static int test_hash(const unsigned char *data, unsigned int datalen, unsigned char *digest) { struct crypto_shash *alg; char *hash_alg_name = "sha1-padlock-nano"; int ret; alg = crypto_alloc_shash(hash_alg_name, 0, 0); if (IS_ERR(alg)) { pr_info("can't alloc alg %s\n", hash_alg_name); return PTR_ERR(alg); } ret = calc_hash(alg, data, datalen, digest); crypto_free_shash(alg); return ret; }”…””}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hèhéuh1hØhŸh¶h Kchhòhžhubeh}”(h]”Œ;code-example-for-use-of-operational-state-memory-with-shash”ah ]”h"]”Œ;code example for use of operational state memory with shash”ah$]”h&]”uh1h¡hh£hžhhŸh¶h K^ubh¢)”}”(hhh]”(h§)”}”(hŒ.Code Example For Random Number Generator Usage”h]”hŒ.Code Example For Random Number Generator Usage”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hjhžhhŸh¶h KšubhÙ)”}”(hXòstatic int get_random_numbers(u8 *buf, unsigned int len) { struct crypto_rng *rng = NULL; char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */ int ret; if (!buf || !len) { pr_debug("No output buffer provided\n"); return -EINVAL; } rng = crypto_alloc_rng(drbg, 0, 0); if (IS_ERR(rng)) { pr_debug("could not allocate RNG handle for %s\n", drbg); return PTR_ERR(rng); } ret = crypto_rng_get_bytes(rng, buf, len); if (ret < 0) pr_debug("generation of random numbers failed\n"); else if (ret == 0) pr_debug("RNG returned no data"); else pr_debug("RNG returned %d bytes of data\n", ret); out: crypto_free_rng(rng); return ret; }”h]”hXòstatic int get_random_numbers(u8 *buf, unsigned int len) { struct crypto_rng *rng = NULL; char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */ int ret; if (!buf || !len) { pr_debug("No output buffer provided\n"); return -EINVAL; } rng = crypto_alloc_rng(drbg, 0, 0); if (IS_ERR(rng)) { pr_debug("could not allocate RNG handle for %s\n", drbg); return PTR_ERR(rng); } ret = crypto_rng_get_bytes(rng, buf, len); if (ret < 0) pr_debug("generation of random numbers failed\n"); else if (ret == 0) pr_debug("RNG returned no data"); else pr_debug("RNG returned %d bytes of data\n", ret); out: crypto_free_rng(rng); return ret; }”…””}”hj*sbah}”(h]”h ]”h"]”h$]”h&]”hèhéuh1hØhŸh¶h KŸhjhžhubeh}”(h]”Œ.code-example-for-random-number-generator-usage”ah ]”h"]”Œ.code example for random number generator usage”ah$]”h&]”uh1h¡hh£hžhhŸh¶h Kšubeh}”(h]”Œ code-examples”ah ]”h"]”Œ code examples”ah$]”h&]”uh1h¡hhhžhhŸh¶h Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”h¶uh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)”}”(h¦NŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”jkŒerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”h¶Œ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”Œnameids”}”(jEjBhïhìjjj=j:uŒ nametypes”}”(jE‰hï‰j‰j=‰uh}”(jBh£hìh·jhòj:juŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nhžhub.