crypto.c revision af901ca181d92aac3a7dc265144a9081a86d8f39
1d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
2d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Ultra Wide Band
3d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * AES-128 CCM Encryption
4d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
5d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Copyright (C) 2007 Intel Corporation
6d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
8d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * This program is free software; you can redistribute it and/or
9d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * modify it under the terms of the GNU General Public License version
10d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 2 as published by the Free Software Foundation.
11d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
12d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * This program is distributed in the hope that it will be useful,
13d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * but WITHOUT ANY WARRANTY; without even the implied warranty of
14d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * GNU General Public License for more details.
16d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
17d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * You should have received a copy of the GNU General Public License
18d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * along with this program; if not, write to the Free Software
19d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 02110-1301, USA.
21d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
22d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
23d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * We don't do any encryption here; we use the Linux Kernel's AES-128
24d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * crypto modules to construct keys and payload blocks in a way
25d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * defined by WUSB1.0[6]. Check the erratas, as typos are are patched
26d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * there.
27d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
28d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Thanks a zillion to John Keys for his help and clarifications over
29d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the designed-by-a-committee text.
30d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
31d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * So the idea is that there is this basic Pseudo-Random-Function
32d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * defined in WUSB1.0[6.5] which is the core of everything. It works
33d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * by tweaking some blocks, AES crypting them and then xoring
34d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * something else with them (this seems to be called CBC(AES) -- can
35d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * you tell I know jack about crypto?). So we just funnel it into the
36d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Linux Crypto API.
37d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
38d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * We leave a crypto test module so we can verify that vectors match,
39d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * every now and then.
40d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
41d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Block size: 16 bytes -- AES seems to do things in 'block sizes'. I
42d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *             am learning a lot...
43d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
44d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *             Conveniently, some data structures that need to be
45d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *             funneled through AES are...16 bytes in size!
46d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
47d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
48d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/crypto.h>
49d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/module.h>
50d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/err.h>
51d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/uwb.h>
52d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/usb/wusb.h>
53d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/scatterlist.h>
54d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
55d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabelstatic int debug_crypto_verify = 0;
56d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel
57d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabelmodule_param(debug_crypto_verify, int, 0);
58d409f3bf47c5e5ae10601d079204e263bc176bcfDavid VrabelMODULE_PARM_DESC(debug_crypto_verify, "verify the key generation algorithms");
59d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
60e43ace891229607c43d35597cbba77c2e40f48d4David Vrabelstatic void wusb_key_dump(const void *buf, size_t len)
61e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel{
62e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel	print_hex_dump(KERN_ERR, "  ", DUMP_PREFIX_OFFSET, 16, 1,
63e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		       buf, len, 0);
64e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel}
65e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel
66d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
67d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Block of data, as understood by AES-CCM
68d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
69d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * The code assumes this structure is nothing but a 16 byte array
70d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (packed in a struct to avoid common mess ups that I usually do with
71d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * arrays and enforcing type checking).
72d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
73d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_block {
74d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 data[16];
75d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed));
76d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
77d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
78d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Counter-mode Blocks (WUSB1.0[6.4])
79d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
80d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * According to CCM (or so it seems), for the purpose of calculating
81d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the MIC, the message is broken in N counter-mode blocks, B0, B1,
82d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * ... BN.
83d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
84d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * B0 contains flags, the CCM nonce and l(m).
85d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
86d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * B1 contains l(a), the MAC header, the encryption offset and padding.
87d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
88d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * If EO is nonzero, additional blocks are built from payload bytes
89d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * until EO is exahusted (FIXME: padding to 16 bytes, I guess). The
90d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * padding is not xmitted.
91d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
92d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
93d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* WUSB1.0[T6.4] */
94d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_b0 {
95d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 flags;	/* 0x59, per CCM spec */
96d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct aes_ccm_nonce ccm_nonce;
97d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	__be16 lm;
98d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed));
99d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
100d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* WUSB1.0[T6.5] */
101d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_b1 {
102d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	__be16 la;
103d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 mac_header[10];
104d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	__le16 eo;
105d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 security_reserved;	/* This is always zero */
106d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 padding;		/* 0 */
107d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed));
108d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
109d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
110d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Encryption Blocks (WUSB1.0[6.4.4])
111d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
112d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * CCM uses Ax blocks to generate a keystream with which the MIC and
113d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the message's payload are encoded. A0 always encrypts/decrypts the
114af901ca181d92aac3a7dc265144a9081a86d8f39André Goddard Rosa * MIC. Ax (x>0) are used for the successive payload blocks.
115d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
116d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * The x is the counter, and is increased for each block.
117d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
118d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_a {
119d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 flags;	/* 0x01, per CCM spec */
120d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct aes_ccm_nonce ccm_nonce;
121d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	__be16 counter;	/* Value of x */
122d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed));
123d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
124d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic void bytewise_xor(void *_bo, const void *_bi1, const void *_bi2,
125d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			 size_t size)
126d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{
127d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 *bo = _bo;
128d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	const u8 *bi1 = _bi1, *bi2 = _bi2;
129d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	size_t itr;
130d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	for (itr = 0; itr < size; itr++)
131d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		bo[itr] = bi1[itr] ^ bi2[itr];
132d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}
133d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
134d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
135d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * CC-MAC function WUSB1.0[6.5]
136d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
137d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Take a data string and produce the encrypted CBC Counter-mode MIC
138d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
139d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Note the names for most function arguments are made to (more or
140d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * less) match those used in the pseudo-function definition given in
141d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * WUSB1.0[6.5].
142d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
143d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @tfm_cbc: CBC(AES) blkcipher handle (initialized)
144d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
145d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @tfm_aes: AES cipher handle (initialized)
146d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
147d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @mic: buffer for placing the computed MIC (Message Integrity
148d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       Code). This is exactly 8 bytes, and we expect the buffer to
149d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       be at least eight bytes in length.
150d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
151d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @key: 128 bit symmetric key
152d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
153d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @n: CCM nonce
154d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
155d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @a: ASCII string, 14 bytes long (I guess zero padded if needed;
156d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *     we use exactly 14 bytes).
157d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
158d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @b: data stream to be processed; cannot be a global or const local
159d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *     (will confuse the scatterlists)
160d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
161d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @blen: size of b...
162d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
163d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Still not very clear how this is done, but looks like this: we
164d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * create block B0 (as WUSB1.0[6.5] says), then we AES-crypt it with
165d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @key. We bytewise xor B0 with B1 (1) and AES-crypt that. Then we
166d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * take the payload and divide it in blocks (16 bytes), xor them with
167d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the previous crypto result (16 bytes) and crypt it, repeat the next
168d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * block with the output of the previous one, rinse wash (I guess this
169d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * is what AES CBC mode means...but I truly have no idea). So we use
170d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the CBC(AES) blkcipher, that does precisely that. The IV (Initial
171d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Vector) is 16 bytes and is set to zero, so
172d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
173d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * See rfc3610. Linux crypto has a CBC implementation, but the
174d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * documentation is scarce, to say the least, and the example code is
175d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * so intricated that is difficult to understand how things work. Most
176d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * of this is guess work -- bite me.
177d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
178d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (1) Created as 6.5 says, again, using as l(a) 'Blen + 14', and
179d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *     using the 14 bytes of @a to fill up
180d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *     b1.{mac_header,e0,security_reserved,padding}.
181d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
182d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: The definiton of l(a) in WUSB1.0[6.5] vs the definition of
183d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       l(m) is orthogonal, they bear no relationship, so it is not
184d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       in conflict with the parameter's relation that
185d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       WUSB1.0[6.4.2]) defines.
186d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
187d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: WUSB1.0[A.1]: Host Nonce is missing a nibble? (1e); fixed in
188d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       first errata released on 2005/07.
189d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
190d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: we need to clean IV to zero at each invocation to make sure
191d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       we start with a fresh empty Initial Vector, so that the CBC
192d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       works ok.
193d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
194d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: blen is not aligned to a block size, we'll pad zeros, that's
195d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *       what sg[4] is for. Maybe there is a smarter way to do this.
196d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
197d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic int wusb_ccm_mac(struct crypto_blkcipher *tfm_cbc,
198d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			struct crypto_cipher *tfm_aes, void *mic,
199d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			const struct aes_ccm_nonce *n,
200d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			const struct aes_ccm_label *a, const void *b,
201d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			size_t blen)
202d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{
203d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	int result = 0;
204d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct blkcipher_desc desc;
205d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct aes_ccm_b0 b0;
206d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct aes_ccm_b1 b1;
207d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct aes_ccm_a ax;
208d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct scatterlist sg[4], sg_dst;
209d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	void *iv, *dst_buf;
210d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	size_t ivsize, dst_size;
211d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	const u8 bzero[16] = { 0 };
212d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	size_t zero_padding;
213d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
214d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/*
215d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * These checks should be compile time optimized out
216d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * ensure @a fills b1's mac_header and following fields
217d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 */
218d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	WARN_ON(sizeof(*a) != sizeof(b1) - sizeof(b1.la));
219d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	WARN_ON(sizeof(b0) != sizeof(struct aes_ccm_block));
220d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	WARN_ON(sizeof(b1) != sizeof(struct aes_ccm_block));
221d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	WARN_ON(sizeof(ax) != sizeof(struct aes_ccm_block));
222d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
223d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = -ENOMEM;
224d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	zero_padding = sizeof(struct aes_ccm_block)
225d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		- blen % sizeof(struct aes_ccm_block);
226d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	zero_padding = blen % sizeof(struct aes_ccm_block);
227d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (zero_padding)
228d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		zero_padding = sizeof(struct aes_ccm_block) - zero_padding;
229d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	dst_size = blen + sizeof(b0) + sizeof(b1) + zero_padding;
230d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	dst_buf = kzalloc(dst_size, GFP_KERNEL);
231d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (dst_buf == NULL) {
232d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: can't alloc destination buffer\n");
233d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		goto error_dst_buf;
234d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
235d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
236d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	iv = crypto_blkcipher_crt(tfm_cbc)->iv;
237d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	ivsize = crypto_blkcipher_ivsize(tfm_cbc);
238d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	memset(iv, 0, ivsize);
239d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
240d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/* Setup B0 */
241d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	b0.flags = 0x59;	/* Format B0 */
242d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	b0.ccm_nonce = *n;
243d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	b0.lm = cpu_to_be16(0);	/* WUSB1.0[6.5] sez l(m) is 0 */
244d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
245d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/* Setup B1
246d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 *
247d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * The WUSB spec is anything but clear! WUSB1.0[6.5]
248d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * says that to initialize B1 from A with 'l(a) = blen +
249d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * 14'--after clarification, it means to use A's contents
250d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * for MAC Header, EO, sec reserved and padding.
251d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 */
252d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	b1.la = cpu_to_be16(blen + 14);
253d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	memcpy(&b1.mac_header, a, sizeof(*a));
254d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
255d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	sg_init_table(sg, ARRAY_SIZE(sg));
256d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	sg_set_buf(&sg[0], &b0, sizeof(b0));
257d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	sg_set_buf(&sg[1], &b1, sizeof(b1));
258d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	sg_set_buf(&sg[2], b, blen);
259d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/* 0 if well behaved :) */
260d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	sg_set_buf(&sg[3], bzero, zero_padding);
261d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	sg_init_one(&sg_dst, dst_buf, dst_size);
262d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
263d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	desc.tfm = tfm_cbc;
264d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	desc.flags = 0;
265d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = crypto_blkcipher_encrypt(&desc, &sg_dst, sg, dst_size);
266d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (result < 0) {
267d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: can't compute CBC-MAC tag (MIC): %d\n",
268d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		       result);
269d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		goto error_cbc_crypt;
270d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
271d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
272d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/* Now we crypt the MIC Tag (*iv) with Ax -- values per WUSB1.0[6.5]
273d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * The procedure is to AES crypt the A0 block and XOR the MIC
274d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * Tag agains it; we only do the first 8 bytes and place it
275d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * directly in the destination buffer.
276d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 *
277d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * POS Crypto API: size is assumed to be AES's block size.
278d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * Thanks for documenting it -- tip taken from airo.c
279d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 */
280d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	ax.flags = 0x01;		/* as per WUSB 1.0 spec */
281d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	ax.ccm_nonce = *n;
282d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	ax.counter = 0;
283d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	crypto_cipher_encrypt_one(tfm_aes, (void *)&ax, (void *)&ax);
284d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	bytewise_xor(mic, &ax, iv, 8);
285d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = 8;
286d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_cbc_crypt:
287d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	kfree(dst_buf);
288d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_dst_buf:
289d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	return result;
290d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}
291d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
292d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
293d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * WUSB Pseudo Random Function (WUSB1.0[6.5])
294d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
295d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @b: buffer to the source data; cannot be a global or const local
296d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *     (will confuse the scatterlists)
297d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
298d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezssize_t wusb_prf(void *out, size_t out_size,
299d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		 const u8 key[16], const struct aes_ccm_nonce *_n,
300d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		 const struct aes_ccm_label *a,
301d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		 const void *b, size_t blen, size_t len)
302d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{
303d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	ssize_t result, bytes = 0, bitr;
304d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct aes_ccm_nonce n = *_n;
305d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct crypto_blkcipher *tfm_cbc;
306d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct crypto_cipher *tfm_aes;
307d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u64 sfn = 0;
308d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	__le64 sfn_le;
309d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
310d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	tfm_cbc = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
311d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (IS_ERR(tfm_cbc)) {
312d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		result = PTR_ERR(tfm_cbc);
313d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: can't load CBC(AES): %d\n", (int)result);
314d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		goto error_alloc_cbc;
315d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
316d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = crypto_blkcipher_setkey(tfm_cbc, key, 16);
317d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (result < 0) {
318d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: can't set CBC key: %d\n", (int)result);
319d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		goto error_setkey_cbc;
320d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
321d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
322d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
323d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (IS_ERR(tfm_aes)) {
324d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		result = PTR_ERR(tfm_aes);
325d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: can't load AES: %d\n", (int)result);
326d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		goto error_alloc_aes;
327d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
328d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = crypto_cipher_setkey(tfm_aes, key, 16);
329d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (result < 0) {
330d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: can't set AES key: %d\n", (int)result);
331d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		goto error_setkey_aes;
332d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
333d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
334d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	for (bitr = 0; bitr < (len + 63) / 64; bitr++) {
335d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		sfn_le = cpu_to_le64(sfn++);
336d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		memcpy(&n.sfn, &sfn_le, sizeof(n.sfn));	/* n.sfn++... */
337d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		result = wusb_ccm_mac(tfm_cbc, tfm_aes, out + bytes,
338d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez				      &n, a, b, blen);
339d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		if (result < 0)
340d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			goto error_ccm_mac;
341d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		bytes += result;
342d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
343d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = bytes;
344d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_ccm_mac:
345d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_setkey_aes:
346d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	crypto_free_cipher(tfm_aes);
347d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_alloc_aes:
348d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_setkey_cbc:
349d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	crypto_free_blkcipher(tfm_cbc);
350d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_alloc_cbc:
351d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	return result;
352d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}
353d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
354d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* WUSB1.0[A.2] test vectors */
355d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const u8 stv_hsmic_key[16] = {
356d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	0x4b, 0x79, 0xa3, 0xcf, 0xe5, 0x53, 0x23, 0x9d,
357d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	0xd7, 0xc1, 0x6d, 0x1c, 0x2d, 0xab, 0x6d, 0x3f
358d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez};
359d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
360d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const struct aes_ccm_nonce stv_hsmic_n = {
361d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.sfn = { 0 },
362d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.tkid = { 0x76, 0x98, 0x01,  },
363d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.dest_addr = { .data = { 0xbe, 0x00 } },
364d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.src_addr = { .data = { 0x76, 0x98 } },
365d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez};
366d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
367d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
368d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Out-of-band MIC Generation verification code
369d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
370d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
371d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic int wusb_oob_mic_verify(void)
372d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{
373d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	int result;
374d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	u8 mic[8];
375d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/* WUSB1.0[A.2] test vectors
376d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 *
377d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * Need to keep it in the local stack as GCC 4.1.3something
378d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * messes up and generates noise.
379d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 */
380d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct usb_handshake stv_hsmic_hs = {
381d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.bMessageNumber = 2,
382d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.bStatus 	= 00,
383d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.tTKID 		= { 0x76, 0x98, 0x01 },
384d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.bReserved 	= 00,
385d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.CDID 		= { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
386d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez				    0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
387d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez				    0x3c, 0x3d, 0x3e, 0x3f },
388d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.nonce	 	= { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
389d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez				    0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
390d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez				    0x2c, 0x2d, 0x2e, 0x2f },
391d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.MIC	 	= { 0x75, 0x6a, 0x97, 0x51, 0x0c, 0x8c,
392d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez				    0x14, 0x7b } ,
393d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	};
394d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	size_t hs_size;
395d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
396d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = wusb_oob_mic(mic, stv_hsmic_key, &stv_hsmic_n, &stv_hsmic_hs);
397d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (result < 0)
398d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: WUSB OOB MIC test: failed: %d\n", result);
399d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	else if (memcmp(stv_hsmic_hs.MIC, mic, sizeof(mic))) {
400d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: OOB MIC test: "
401d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		       "mismatch between MIC result and WUSB1.0[A2]\n");
402d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		hs_size = sizeof(stv_hsmic_hs) - sizeof(stv_hsmic_hs.MIC);
403d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: Handshake2 in: (%zu bytes)\n", hs_size);
404e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(&stv_hsmic_hs, hs_size);
405d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: CCM Nonce in: (%zu bytes)\n",
406d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		       sizeof(stv_hsmic_n));
407e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(&stv_hsmic_n, sizeof(stv_hsmic_n));
408d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: MIC out:\n");
409e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(mic, sizeof(mic));
410d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: MIC out (from WUSB1.0[A.2]):\n");
411e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(stv_hsmic_hs.MIC, sizeof(stv_hsmic_hs.MIC));
412d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		result = -EINVAL;
413d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	} else
414d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		result = 0;
415d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	return result;
416d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}
417d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
418d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
419d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Test vectors for Key derivation
420d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
421d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * These come from WUSB1.0[6.5.1], the vectors in WUSB1.0[A.1]
422d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (errata corrected in 2005/07).
423d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
424d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const u8 stv_key_a1[16] __attribute__ ((__aligned__(4))) = {
425d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87,
426d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f
427d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez};
428d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
429d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const struct aes_ccm_nonce stv_keydvt_n_a1 = {
430d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.sfn = { 0 },
431d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.tkid = { 0x76, 0x98, 0x01,  },
432d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.dest_addr = { .data = { 0xbe, 0x00 } },
433d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.src_addr = { .data = { 0x76, 0x98 } },
434d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez};
435d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
436d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const struct wusb_keydvt_out stv_keydvt_out_a1 = {
437d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.kck = {
438d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		0x4b, 0x79, 0xa3, 0xcf, 0xe5, 0x53, 0x23, 0x9d,
439d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		0xd7, 0xc1, 0x6d, 0x1c, 0x2d, 0xab, 0x6d, 0x3f
440d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	},
441d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	.ptk = {
442d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		0xc8, 0x70, 0x62, 0x82, 0xb6, 0x7c, 0xe9, 0x06,
443d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		0x7b, 0xc5, 0x25, 0x69, 0xf2, 0x36, 0x61, 0x2d
444d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	}
445d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez};
446d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
447d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
448d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Performa a test to make sure we match the vectors defined in
449d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * WUSB1.0[A.1](Errata2006/12)
450d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
451d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic int wusb_key_derive_verify(void)
452d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{
453d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	int result = 0;
454d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct wusb_keydvt_out keydvt_out;
455d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/* These come from WUSB1.0[A.1] + 2006/12 errata
456d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 * NOTE: can't make this const or global -- somehow it seems
457d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 *       the scatterlists for crypto get confused and we get
458d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	 *       bad data. There is no doc on this... */
459d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	struct wusb_keydvt_in stv_keydvt_in_a1 = {
460d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.hnonce = {
461d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
462d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
463d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		},
464d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		.dnonce = {
465d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
466d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez			0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
467d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		}
468d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	};
469d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
470d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	result = wusb_key_derive(&keydvt_out, stv_key_a1, &stv_keydvt_n_a1,
471d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez				 &stv_keydvt_in_a1);
472d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (result < 0)
473d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: WUSB key derivation test: "
474d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		       "derivation failed: %d\n", result);
475d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	if (memcmp(&stv_keydvt_out_a1, &keydvt_out, sizeof(keydvt_out))) {
476d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: WUSB key derivation test: "
477d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		       "mismatch between key derivation result "
478d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		       "and WUSB1.0[A1] Errata 2006/12\n");
479e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		printk(KERN_ERR "E: keydvt in: key\n");
480e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(stv_key_a1, sizeof(stv_key_a1));
481e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		printk(KERN_ERR "E: keydvt in: nonce\n");
482e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump( &stv_keydvt_n_a1, sizeof(stv_keydvt_n_a1));
483e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		printk(KERN_ERR "E: keydvt in: hnonce & dnonce\n");
484e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(&stv_keydvt_in_a1, sizeof(stv_keydvt_in_a1));
485d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: keydvt out: KCK\n");
486e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(&keydvt_out.kck, sizeof(keydvt_out.kck));
487d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		printk(KERN_ERR "E: keydvt out: PTK\n");
488e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel		wusb_key_dump(&keydvt_out.ptk, sizeof(keydvt_out.ptk));
489d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		result = -EINVAL;
490d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	} else
491d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez		result = 0;
492d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	return result;
493d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}
494d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
495d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/*
496d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Initialize crypto system
497d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez *
498d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * FIXME: we do nothing now, other than verifying. Later on we'll
499d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * cache the encryption stuff, so that's why we have a separate init.
500d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */
501d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezint wusb_crypto_init(void)
502d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{
503d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	int result;
504d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
505d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel	if (debug_crypto_verify) {
506d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel		result = wusb_key_derive_verify();
507d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel		if (result < 0)
508d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel			return result;
509d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel		return wusb_oob_mic_verify();
510d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel	}
511d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel	return 0;
512d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}
513d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez
514d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezvoid wusb_crypto_exit(void)
515d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{
516d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez	/* FIXME: free cached crypto transforms */
517d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}
518