2

I have been trying to use the crypto api in the linux kernel, what i need to do is sha a file that is being opened. I am using the LSM to catch those file opens.

What I have so far is creating a struct crypto_shash using

struct crypto_shash *tfm;
struct shash_desc desc;
tfm = crypto_alloc_shash("sha1", 0, CRYPTO_ALG_ASYNC);

and i assume i am supposed to init it after that using

desc.tfm = tfm;
desc.flags = 0;

err = crypto_shash_init(&desc);

that all works fine, but then i want to use

crypto_shash_digest(&desc, ??, ??, sha_hash);

and i realize that it expects a scatterlist as its second argument and the length of that scatter list as the third argument. What i cant figure out is how I am supposed to load the file into a scatterlist in order to give it to the crypto system.

I have done quite a bit of reading but have thus far been unable to find any details about getting a files contents loaded into a scatterlist. So any pointers in the right direction would be appreciated.

Thanks

Digital Powers
  • 460
  • 6
  • 23

1 Answers1

2

I have done something similar some time ago. The only difference is that I calculated a hash of ELF sections.

  1. Probably your desc.flags should be CRYPTO_TFM_REQ_MAY_SLEEP until you have really good reason to prevent crypto operation from blocking.
  2. Are you sure you didn't confuse crypto_shash_digest with crypto_hash_digest? Because crypto_*s*hash_digest() receives a pointer to data as its 3rd argument. If it's not true for you, what linux kenrel version are you talking about?
Dan Kruchinin
  • 2,945
  • 1
  • 17
  • 21
  • i am using 2.6.38 and you are right, i switched to using shash because of various things i had read saying that was what i wanted. Oddly enough I am still getting null pointer error when i try crypto_shash_digest(&desc, teststr, strlen(teststr), sha_hash); – Digital Powers Nov 09 '11 at 00:18
  • also i am checking that the tfm i get is not null and there is no error from init. – Digital Powers Nov 09 '11 at 00:20
  • @DigitalPowers May you show me the code, I need to see how you use crypto_shash_digest(), how you initialize TFM and so on? I assume there's an error with preparation of TFM, but I can't say anything until I see the code. – Dan Kruchinin Nov 09 '11 at 12:06
  • the code i pasted originally is it, well i have a call to final at the end and error checking but other than that those are my calls to crypto_shash functions. I am currently using a string literal teststr to try and hash that as a digest, then do final. – Digital Powers Nov 09 '11 at 17:06
  • @DigitalPowers 1) Have you tried to set CRYPTO_TFM_REQ_MAY_SLEEP TFM flag? 2) How do you allocate sha_hash? It must be least crypto_shash_digestsize bytes. 3) Where exactly did get nullptr error? Can you show a stack trace? – Dan Kruchinin Nov 10 '11 at 13:51