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