blob: b91191eba37729c6620b345d2474b748f4dffccc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Yen Lin2a6f0362012-04-02 13:18:48 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
4 * (C) Copyright 2010 - 2011 NVIDIA Corporation <www.nvidia.com>
Yen Lin2a6f0362012-04-02 13:18:48 +00005 */
6
7#include <common.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09008#include <linux/errno.h>
Yen Lin2a6f0362012-04-02 13:18:48 +00009#include "crypto.h"
Stefano Babicb80c0b92017-04-05 18:08:00 +020010#include "uboot_aes.h"
Yen Lin2a6f0362012-04-02 13:18:48 +000011
12static u8 zero_key[16];
13
14#define AES_CMAC_CONST_RB 0x87 /* from RFC 4493, Figure 2.2 */
15
16enum security_op {
17 SECURITY_SIGN = 1 << 0, /* Sign the data */
18 SECURITY_ENCRYPT = 1 << 1, /* Encrypt the data */
19};
20
Yen Lin2a6f0362012-04-02 13:18:48 +000021/**
22 * Shift a vector left by one bit
23 *
24 * \param in Input vector
25 * \param out Output vector
26 * \param size Length of vector in bytes
27 */
28static void left_shift_vector(u8 *in, u8 *out, int size)
29{
30 int carry = 0;
31 int i;
32
33 for (i = size - 1; i >= 0; i--) {
34 out[i] = (in[i] << 1) | carry;
35 carry = in[i] >> 7; /* get most significant bit */
36 }
37}
38
39/**
40 * Sign a block of data, putting the result into dst.
41 *
Philippe Reynes8302d172020-01-06 15:22:35 +010042 * \param key Input AES key, length AES128_KEY_LENGTH
Yen Lin2a6f0362012-04-02 13:18:48 +000043 * \param key_schedule Expanded key to use
44 * \param src Source data of length 'num_aes_blocks' blocks
Philippe Reynes8302d172020-01-06 15:22:35 +010045 * \param dst Destination buffer, length AES128_KEY_LENGTH
Yen Lin2a6f0362012-04-02 13:18:48 +000046 * \param num_aes_blocks Number of AES blocks to encrypt
47 */
48static void sign_object(u8 *key, u8 *key_schedule, u8 *src, u8 *dst,
49 u32 num_aes_blocks)
50{
Philippe Reynes8302d172020-01-06 15:22:35 +010051 u8 tmp_data[AES128_KEY_LENGTH];
52 u8 iv[AES128_KEY_LENGTH] = {0};
53 u8 left[AES128_KEY_LENGTH];
54 u8 k1[AES128_KEY_LENGTH];
Yen Lin2a6f0362012-04-02 13:18:48 +000055 u8 *cbc_chain_data;
56 unsigned i;
57
58 cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
59
60 /* compute K1 constant needed by AES-CMAC calculation */
Philippe Reynes8302d172020-01-06 15:22:35 +010061 for (i = 0; i < AES128_KEY_LENGTH; i++)
Yen Lin2a6f0362012-04-02 13:18:48 +000062 tmp_data[i] = 0;
63
Philippe Reynes8302d172020-01-06 15:22:35 +010064 aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv,
65 tmp_data, left, 1);
Yen Lin2a6f0362012-04-02 13:18:48 +000066
67 left_shift_vector(left, k1, sizeof(left));
Yen Lin2a6f0362012-04-02 13:18:48 +000068
69 if ((left[0] >> 7) != 0) /* get MSB of L */
Philippe Reynes8302d172020-01-06 15:22:35 +010070 k1[AES128_KEY_LENGTH - 1] ^= AES_CMAC_CONST_RB;
Yen Lin2a6f0362012-04-02 13:18:48 +000071
72 /* compute the AES-CMAC value */
73 for (i = 0; i < num_aes_blocks; i++) {
74 /* Apply the chain data */
Stephen Warren53eb7682014-04-18 10:28:58 -060075 aes_apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
Yen Lin2a6f0362012-04-02 13:18:48 +000076
77 /* for the final block, XOR K1 into the IV */
78 if (i == num_aes_blocks - 1)
Stephen Warren53eb7682014-04-18 10:28:58 -060079 aes_apply_cbc_chain_data(tmp_data, k1, tmp_data);
Yen Lin2a6f0362012-04-02 13:18:48 +000080
81 /* encrypt the AES block */
Philippe Reynes8302d172020-01-06 15:22:35 +010082 aes_encrypt(AES128_KEY_LENGTH, tmp_data,
83 key_schedule, dst);
Yen Lin2a6f0362012-04-02 13:18:48 +000084
85 debug("sign_obj: block %d of %d\n", i, num_aes_blocks);
Yen Lin2a6f0362012-04-02 13:18:48 +000086
87 /* Update pointers for next loop. */
88 cbc_chain_data = dst;
Philippe Reynes8302d172020-01-06 15:22:35 +010089 src += AES128_KEY_LENGTH;
Yen Lin2a6f0362012-04-02 13:18:48 +000090 }
Yen Lin2a6f0362012-04-02 13:18:48 +000091}
92
93/**
94 * Encrypt and sign a block of data (depending on security mode).
95 *
Philippe Reynes8302d172020-01-06 15:22:35 +010096 * \param key Input AES key, length AES128_KEY_LENGTH
Yen Lin2a6f0362012-04-02 13:18:48 +000097 * \param oper Security operations mask to perform (enum security_op)
98 * \param src Source data
99 * \param length Size of source data
Philippe Reynes8302d172020-01-06 15:22:35 +0100100 * \param sig_dst Destination address for signature, AES128_KEY_LENGTH bytes
Yen Lin2a6f0362012-04-02 13:18:48 +0000101 */
102static int encrypt_and_sign(u8 *key, enum security_op oper, u8 *src,
103 u32 length, u8 *sig_dst)
104{
105 u32 num_aes_blocks;
Philippe Reynes8302d172020-01-06 15:22:35 +0100106 u8 key_schedule[AES128_EXPAND_KEY_LENGTH];
107 u8 iv[AES128_KEY_LENGTH] = {0};
Yen Lin2a6f0362012-04-02 13:18:48 +0000108
109 debug("encrypt_and_sign: length = %d\n", length);
Yen Lin2a6f0362012-04-02 13:18:48 +0000110
111 /*
112 * The only need for a key is for signing/checksum purposes, so
113 * if not encrypting, expand a key of 0s.
114 */
Philippe Reynes8302d172020-01-06 15:22:35 +0100115 aes_expand_key(oper & SECURITY_ENCRYPT ? key : zero_key,
116 AES128_KEY_LENGTH, key_schedule);
Yen Lin2a6f0362012-04-02 13:18:48 +0000117
Philippe Reynes8302d172020-01-06 15:22:35 +0100118 num_aes_blocks = (length + AES128_KEY_LENGTH - 1) / AES128_KEY_LENGTH;
Yen Lin2a6f0362012-04-02 13:18:48 +0000119
120 if (oper & SECURITY_ENCRYPT) {
121 /* Perform this in place, resulting in src being encrypted. */
122 debug("encrypt_and_sign: begin encryption\n");
Philippe Reynes8302d172020-01-06 15:22:35 +0100123 aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, src,
124 src, num_aes_blocks);
Yen Lin2a6f0362012-04-02 13:18:48 +0000125 debug("encrypt_and_sign: end encryption\n");
126 }
127
128 if (oper & SECURITY_SIGN) {
129 /* encrypt the data, overwriting the result in signature. */
130 debug("encrypt_and_sign: begin signing\n");
131 sign_object(key, key_schedule, src, sig_dst, num_aes_blocks);
132 debug("encrypt_and_sign: end signing\n");
133 }
134
135 return 0;
136}
137
138int sign_data_block(u8 *source, unsigned length, u8 *signature)
139{
140 return encrypt_and_sign(zero_key, SECURITY_SIGN, source,
141 length, signature);
142}