1*04171cfcSAugustin Cavalier /* $OpenBSD: key_wrap.c,v 1.5 2017/05/02 17:07:06 mikeb Exp $ */
2*04171cfcSAugustin Cavalier
3*04171cfcSAugustin Cavalier /*-
4*04171cfcSAugustin Cavalier * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
5*04171cfcSAugustin Cavalier *
6*04171cfcSAugustin Cavalier * Permission to use, copy, modify, and distribute this software for any
7*04171cfcSAugustin Cavalier * purpose with or without fee is hereby granted, provided that the above
8*04171cfcSAugustin Cavalier * copyright notice and this permission notice appear in all copies.
9*04171cfcSAugustin Cavalier *
10*04171cfcSAugustin Cavalier * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*04171cfcSAugustin Cavalier * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*04171cfcSAugustin Cavalier * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*04171cfcSAugustin Cavalier * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*04171cfcSAugustin Cavalier * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*04171cfcSAugustin Cavalier * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*04171cfcSAugustin Cavalier * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*04171cfcSAugustin Cavalier */
18*04171cfcSAugustin Cavalier
19*04171cfcSAugustin Cavalier /*
20*04171cfcSAugustin Cavalier * This code implements the AES Key Wrap algorithm described in RFC 3394.
21*04171cfcSAugustin Cavalier */
22*04171cfcSAugustin Cavalier
23*04171cfcSAugustin Cavalier #include <sys/param.h>
24*04171cfcSAugustin Cavalier #include <sys/systm.h>
25*04171cfcSAugustin Cavalier
26*04171cfcSAugustin Cavalier #include <crypto/aes.h>
27*04171cfcSAugustin Cavalier #include <crypto/key_wrap.h>
28*04171cfcSAugustin Cavalier
29*04171cfcSAugustin Cavalier static const u_int8_t IV[8] =
30*04171cfcSAugustin Cavalier { 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6 };
31*04171cfcSAugustin Cavalier
32*04171cfcSAugustin Cavalier void
aes_key_wrap_set_key(aes_key_wrap_ctx * ctx,const u_int8_t * K,size_t K_len)33*04171cfcSAugustin Cavalier aes_key_wrap_set_key(aes_key_wrap_ctx *ctx, const u_int8_t *K, size_t K_len)
34*04171cfcSAugustin Cavalier {
35*04171cfcSAugustin Cavalier AES_Setkey(&ctx->ctx, K, K_len);
36*04171cfcSAugustin Cavalier }
37*04171cfcSAugustin Cavalier
38*04171cfcSAugustin Cavalier void
aes_key_wrap_set_key_wrap_only(aes_key_wrap_ctx * ctx,const u_int8_t * K,size_t K_len)39*04171cfcSAugustin Cavalier aes_key_wrap_set_key_wrap_only(aes_key_wrap_ctx *ctx, const u_int8_t *K,
40*04171cfcSAugustin Cavalier size_t K_len)
41*04171cfcSAugustin Cavalier {
42*04171cfcSAugustin Cavalier AES_Setkey(&ctx->ctx, K, K_len);
43*04171cfcSAugustin Cavalier }
44*04171cfcSAugustin Cavalier
45*04171cfcSAugustin Cavalier void
aes_key_wrap(aes_key_wrap_ctx * ctx,const u_int8_t * P,size_t n,u_int8_t * C)46*04171cfcSAugustin Cavalier aes_key_wrap(aes_key_wrap_ctx *ctx, const u_int8_t *P, size_t n, u_int8_t *C)
47*04171cfcSAugustin Cavalier {
48*04171cfcSAugustin Cavalier u_int64_t B[2], t;
49*04171cfcSAugustin Cavalier u_int8_t *A, *R;
50*04171cfcSAugustin Cavalier size_t i;
51*04171cfcSAugustin Cavalier int j;
52*04171cfcSAugustin Cavalier
53*04171cfcSAugustin Cavalier memmove(C + 8, P, n * 8); /* P and C may overlap */
54*04171cfcSAugustin Cavalier A = C; /* A points to C[0] */
55*04171cfcSAugustin Cavalier memcpy(A, IV, 8); /* A = IV, an initial value */
56*04171cfcSAugustin Cavalier
57*04171cfcSAugustin Cavalier for (j = 0, t = 1; j <= 5; j++) {
58*04171cfcSAugustin Cavalier R = C + 8;
59*04171cfcSAugustin Cavalier for (i = 1; i <= n; i++, t++) {
60*04171cfcSAugustin Cavalier /* B = A | R[i] */
61*04171cfcSAugustin Cavalier memcpy(&B[0], A, 8);
62*04171cfcSAugustin Cavalier memcpy(&B[1], R, 8);
63*04171cfcSAugustin Cavalier /* B = AES(K, B) */
64*04171cfcSAugustin Cavalier AES_Encrypt(&ctx->ctx, (caddr_t)B, (caddr_t)B);
65*04171cfcSAugustin Cavalier /* MSB(64, B) = MSB(64, B) ^ t */
66*04171cfcSAugustin Cavalier B[0] ^= htobe64(t);
67*04171cfcSAugustin Cavalier /* A = MSB(64, B) */
68*04171cfcSAugustin Cavalier memcpy(A, &B[0], 8);
69*04171cfcSAugustin Cavalier /* R[i] = LSB(64, B) */
70*04171cfcSAugustin Cavalier memcpy(R, &B[1], 8);
71*04171cfcSAugustin Cavalier
72*04171cfcSAugustin Cavalier R += 8;
73*04171cfcSAugustin Cavalier }
74*04171cfcSAugustin Cavalier }
75*04171cfcSAugustin Cavalier explicit_bzero(B, sizeof B);
76*04171cfcSAugustin Cavalier }
77*04171cfcSAugustin Cavalier
78*04171cfcSAugustin Cavalier int
aes_key_unwrap(aes_key_wrap_ctx * ctx,const u_int8_t * C,u_int8_t * P,size_t n)79*04171cfcSAugustin Cavalier aes_key_unwrap(aes_key_wrap_ctx *ctx, const u_int8_t *C, u_int8_t *P, size_t n)
80*04171cfcSAugustin Cavalier {
81*04171cfcSAugustin Cavalier u_int64_t B[2], t;
82*04171cfcSAugustin Cavalier u_int8_t A[8], *R;
83*04171cfcSAugustin Cavalier size_t i;
84*04171cfcSAugustin Cavalier int j;
85*04171cfcSAugustin Cavalier
86*04171cfcSAugustin Cavalier memcpy(A, C, 8); /* A = C[0] */
87*04171cfcSAugustin Cavalier memmove(P, C + 8, n * 8); /* P and C may overlap */
88*04171cfcSAugustin Cavalier
89*04171cfcSAugustin Cavalier for (j = 5, t = 6 * n; j >= 0; j--) {
90*04171cfcSAugustin Cavalier R = P + (n - 1) * 8;
91*04171cfcSAugustin Cavalier for (i = n; i >= 1; i--, t--) {
92*04171cfcSAugustin Cavalier /* MSB(64, B) = A */
93*04171cfcSAugustin Cavalier memcpy(&B[0], A, 8);
94*04171cfcSAugustin Cavalier /* MSB(64, B) = MSB(64, B) ^ t */
95*04171cfcSAugustin Cavalier B[0] ^= htobe64(t);
96*04171cfcSAugustin Cavalier /* B = MSB(64, B) | R[i] */
97*04171cfcSAugustin Cavalier memcpy(&B[1], R, 8);
98*04171cfcSAugustin Cavalier /* B = AES-1(K, B) */
99*04171cfcSAugustin Cavalier AES_Decrypt(&ctx->ctx, (caddr_t)B, (caddr_t)B);
100*04171cfcSAugustin Cavalier /* A = MSB(64, B) */
101*04171cfcSAugustin Cavalier memcpy(A, &B[0], 8);
102*04171cfcSAugustin Cavalier /* R[i] = LSB(64, B) */
103*04171cfcSAugustin Cavalier memcpy(R, &B[1], 8);
104*04171cfcSAugustin Cavalier
105*04171cfcSAugustin Cavalier R -= 8;
106*04171cfcSAugustin Cavalier }
107*04171cfcSAugustin Cavalier }
108*04171cfcSAugustin Cavalier explicit_bzero(B, sizeof B);
109*04171cfcSAugustin Cavalier
110*04171cfcSAugustin Cavalier /* check that A is an appropriate initial value */
111*04171cfcSAugustin Cavalier return timingsafe_bcmp(A, IV, 8) != 0;
112*04171cfcSAugustin Cavalier }
113