All of lore.kernel.org
 help / color / mirror / Atom feed
* RE: RFC: Crypto API User-interface
@ 2014-05-30 11:22 Jitendra Lulla
  0 siblings, 0 replies; 29+ messages in thread
From: Jitendra Lulla @ 2014-05-30 11:22 UTC (permalink / raw)
  To: linux-crypto

Hi,

http://lwn.net/Articles/410848/
The following code is taken from the above page:

int main(void)
{
int opfd;
int tfmfd;
struct sockaddr_alg sa = {
.salg_family = AF_ALG,
.salg_type = "skcipher",
.salg_name = "cbc(aes)"
};
struct msghdr msg = {};
struct cmsghdr *cmsg;
char cbuf[CMSG_SPACE(4) + CMSG_SPACE(20)];
char buf[16];
struct af_alg_iv *iv;
struct iovec iov;
int i;
tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);

bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa));

setsockopt(tfmfd, SOL_ALG, ALG_SET_KEY,
  "\x06\xa9\x21\x40\x36\xb8\xa1\x5b"
  "\x51\x2e\x03\xd5\x34\x12\x00\x06", 16);

opfd = accept(tfmfd, NULL, 0);

msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);

cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_OP;
cmsg->cmsg_len = CMSG_LEN(4);
*(__u32 *)CMSG_DATA(cmsg) = ALG_OP_ENCRYPT;

cmsg = CMSG_NXTHDR(&msg, cmsg);
cmsg->cmsg_level = SOL_ALG;
cmsg->cmsg_type = ALG_SET_IV;
cmsg->cmsg_len = CMSG_LEN(20);
iv = (void *)CMSG_DATA(cmsg);
iv->ivlen = 16;
memcpy(iv->iv, "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30"
      "\xb4\x22\xda\x80\x2c\x9f\xac\x41", 16);

iov.iov_base = "Single block msg";
iov.iov_len = 16;

msg.msg_iov = &iov;
msg.msg_iovlen = 1;

sendmsg(opfd, &msg, 0);
read(opfd, buf, 16);

for (i = 0; i < 16; i++) {
printf("%02x", (unsigned char)buf[i]);
}
printf("\n");
close(opfd);
close(tfmfd);

return 0;
}


Here the following small change is needed for this program to work:
memset(cbuf, 0, CMSG_SPACE(4) + CMSG_SPACE(20));
This memset is required otherwise the CMSG_NXTHDR  may return a NULL
causing a seg fault in the following line:
cmsg->cmsg_level = SOL_ALG;

I have tried this on 3.3.4-5.fc17.x86_64.

Posting this as it may help people who want to use/refer this example code.

However, can somebody please point me to some more examples which use
af_alg socket (without Openssl! as the af_alg engine for openssl
(http://src.carnivore.it/users/common/af_alg/) is incomplete
supporting only aes-cbc,sha1,sha2 only as of today. No other aes
variants supported in af_alg engine.)

I am particulart wanting to know how I can compute hmac and aes-xts or
ctr modes with af_alg without having to go via openssl.

~Jitendra

^ permalink raw reply	[flat|nested] 29+ messages in thread
* RE: RFC: Crypto API User-interface
@ 2014-04-23 10:37 Jitendra Lulla
  0 siblings, 0 replies; 29+ messages in thread
From: Jitendra Lulla @ 2014-04-23 10:37 UTC (permalink / raw)
  To: linux-crypto

Hi,

This is regarding the hash computation over a file with AF_ALG from
user space. [without OpenSSL]

The following link has the mail from Herbert with subject : "RFC:
Crypto API User-interface"
http://lwn.net/Articles/410848/

I was trying to take help from the code snippet he has put in his mail
and I was doing the following to compute
hash over a file of 16 bytes. The file contents are "123456789012345\n".
I read the first 10 bytes first and computed hash over it. Then I read
the next 6 bytes from
the file and computed the hash and I had taken care of using the flags
MSG_MORE and MSG_OOB.

Following is what I am doing:

1. send(opfd, sbuf, len, MSG_MORE);
2. read(opfd, state, 20);
3. send(opfd, state, 20, MSG_OOB);

4. send(opfd, sbuf, len, MSG_MORE);
5. read(opfd, state, 20);
6. /*restore from a partial hash state */
7. send(opfd, state, 20, MSG_OOB);

8.  /* finalize */
9. send(opfd, sbuf, 0, 0);
10. read(opfd, buf, 20);

sbuf
in line 1 contains: "1234567890" (the " is not part of the data, the
data is plain 10 ascii bytes for 1 through 9), len is 10.
the state after line 2 contains correct hash of the above data.

sbuf in line 4 contains: "12345\n". Len is 6.

The hash I am seeing in buf after line 10 is not correct one.

Is there anything obviously wrong with the above code? I am using
2.6.39.4 kernel on rhel 6.4 (santiago)

Thanks for the help!

~Jitendra

^ permalink raw reply	[flat|nested] 29+ messages in thread
[parent not found: <20101104173156.GA1255@gondor.apana.org.au>]
[parent not found: <834983542.1086561283871074929.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>]
[parent not found: <1590523029.1055831283858598965.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>]
* RFC: Crypto API User-interface
@ 2010-09-07  8:42 Herbert Xu
  2010-09-07  9:18 ` Tomas Mraz
                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Herbert Xu @ 2010-09-07  8:42 UTC (permalink / raw)
  To: Linux Crypto Mailing List, netdev

Hi:

This is what I am proposing for the Crypto API user-interface.

Note that this is the interface for operations.  There will be
a separate interface (most likely netlink) for configuring crypto
algorithms, e.g., picking a specific AES implementation as the
system default.

First of all let's have a quick look at what the user-space side
looks like for AEAD:

	int op;

	/* This fd corresponds to a tfm object. */
	tfmfd = socket(AF_ALG, SOCK_STREAM, 0);

	alg.type = "aead";
	alg.name = "ccm(aes)";
	bind(tfmfd, &alg, sizeof(alg));

	setsockopt(tfmfd, SOL_ALG, ALG_AEAD_SET_KEY, key, keylen);

The idea here is that each tfm corresponds to a listening socket.

	/* Each listen call generates one or more fds for input/output
	 * that behave like pipes.
	 */
	listen(tfmfd, 0);
	/* fd for encryption/decryption */
	opfd = accept(tfmfd, NULL, 0);
	/* fd for associated data */
	adfd = accept(tfmfd, NULL, 0);

Each session corresponds to one or more connections obtained from
that socket.  The number depends on the number of inputs/outputs
of that particular type of operation.  For most types, there will
be a s ingle connection/file descriptor that is used for both input
and output.  AEAD is one of the few that require two inputs.

	/* These may also be set through sendmsg(2) cmsgs. */
	op = ALG_AEAD_OP_ENCRYPT;
	setsockopt(opfd, SOL_ALG, ALG_AEAD_OP, op, sizeof(op));
	setsockopt(opfd, SOL_ALG, ALG_AEAD_SET_IV, iv, ivlen);

	/* Like pipes, larges writes will block!
	 * For AEAD, ensure the socket buffer is large enough.
	 * For ciphers, whenever the write blocks start reading.
	 * For hashes, writes should never block.
	 */
	write(opfd, plain, datalen);
	write(adfd, ad, adlen);

	/* The first read triggers the operation. */
	read(opfd, crypt, datalen);

	op = ALG_AEAD_OP_DECRYPT;
	setsockopt(opfd, SOL_ALG, ALG_AEAD_OP, op, sizeof(op));

	write(opfd, crypt, datalen);
	write(adfd, ad, adlen);

	/* Returns -1 with errno EBADMSG if auth fails */
	read(defd, plain, datalen);

	/* Zero-copy */
	splice(cryptfd, NULL, opfd, NULL, datalen, SPLICE_F_MOVE|SPLIFE_F_MORE);
	/* We allow writes to be split into multiple system calls. */
	splice(cryptfd2, NULL, opfd, NULL, datalen, SPLICE_F_MOVE);
	splice(adatafd, NULL, adfd, NULL, adlen, SPLICE_F_MOVE);

	/* For now reading is copy-only, if and when vmsplice
	 * starts supporting zero-copy to user then we can do it
	 * as well.
	 */
	read(opfd, plain, datalen);

Ciphers/compression are pretty much the same sans adfd.

For hashes:

	/* This fd corresponds to a tfm object. */
	tfmfd = socket(AF_ALG, SOCK_STREAM, 0);

	alg.type = "hash";
	alg.name = "xcbc(aes)";
	bind(tfmfd, &alg, sizeof(alg));

	setsockopt(tfmfd, SOL_ALG, ALG_HASH_SET_KEY, key, keylen);

	/* Each listen call generates one or more fds for input/output
	 * that behave like pipes.
	 */
	listen(tfmfd, 0);
	/* fd for hashing */
	opfd = accept(tfmfd, NULL, 0);

	/* MSG_MORE prevents finalisation */
	send(opfd, plain, datalen, MSG_MORE);

	/* Reads partial hash state */
	read(opfd, state, statelen);

	/* Restore from a partial hash state */
	send(opfd, state, statelen, MSG_OOB);

	/* Finalise */
	send(opfd, plain, 0, 0);
	read(opfd, hash, hashlen);

Please comment.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2014-05-30 11:23 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1847066281.1081601283869883727.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>
2010-09-07 14:34 ` RFC: Crypto API User-interface Miloslav Trmac
2010-09-07 14:41   ` Herbert Xu
2010-09-07 14:51   ` Christoph Hellwig
2010-09-07 14:54     ` Miloslav Trmac
2014-05-30 11:22 Jitendra Lulla
  -- strict thread matches above, loose matches on Subject: below --
2014-04-23 10:37 Jitendra Lulla
     [not found] <20101104173156.GA1255@gondor.apana.org.au>
     [not found] ` <1944766358.1362881288892596101.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>
2010-11-04 18:02   ` Herbert Xu
     [not found] <834983542.1086561283871074929.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>
2010-09-07 14:52 ` Miloslav Trmac
2010-09-07 14:55   ` Christoph Hellwig
     [not found] <1590523029.1055831283858598965.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>
2010-09-07 11:27 ` Miloslav Trmac
2010-09-07 14:07   ` Herbert Xu
2010-09-07  8:42 Herbert Xu
2010-09-07  9:18 ` Tomas Mraz
2010-09-07 14:06 ` Christoph Hellwig
2010-09-07 14:06   ` Christoph Hellwig
2010-09-07 14:11   ` Herbert Xu
2010-09-07 14:11     ` Herbert Xu
2010-09-07 14:24     ` Christoph Hellwig
2010-09-07 14:24       ` Christoph Hellwig
2010-09-07 14:39       ` Herbert Xu
2010-09-07 14:39         ` Herbert Xu
2010-09-07 14:49     ` Nikos Mavrogiannopoulos
2010-09-07 14:57   ` Nikos Mavrogiannopoulos
2010-09-07 14:59     ` Christoph Hellwig
2010-10-19 13:44 ` Herbert Xu
2010-10-19 13:44   ` Herbert Xu
2010-10-20 10:24   ` Nikos Mavrogiannopoulos
2010-11-04 17:34   ` Herbert Xu
2010-11-04 17:34     ` Herbert Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.