blob: 893da35e0b9dfcdc5cba70a7e0f5f37ec0f2b87a [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09009#include <linux/errno.h>
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +020010#include <asm/arch-tegra/crypto.h>
Stefano Babicb80c0b92017-04-05 18:08:00 +020011#include "uboot_aes.h"
Yen Lin2a6f0362012-04-02 13:18:48 +000012
13static u8 zero_key[16];
14
15#define AES_CMAC_CONST_RB 0x87 /* from RFC 4493, Figure 2.2 */
16
17enum security_op {
18 SECURITY_SIGN = 1 << 0, /* Sign the data */
19 SECURITY_ENCRYPT = 1 << 1, /* Encrypt the data */
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +020020 SECURITY_DECRYPT = 1 << 2, /* Dectypt the data */
Yen Lin2a6f0362012-04-02 13:18:48 +000021};
22
Yen Lin2a6f0362012-04-02 13:18:48 +000023/**
24 * Shift a vector left by one bit
25 *
26 * \param in Input vector
27 * \param out Output vector
28 * \param size Length of vector in bytes
29 */
30static void left_shift_vector(u8 *in, u8 *out, int size)
31{
32 int carry = 0;
33 int i;
34
35 for (i = size - 1; i >= 0; i--) {
36 out[i] = (in[i] << 1) | carry;
37 carry = in[i] >> 7; /* get most significant bit */
38 }
39}
40
41/**
42 * Sign a block of data, putting the result into dst.
43 *
Philippe Reynes8302d172020-01-06 15:22:35 +010044 * \param key Input AES key, length AES128_KEY_LENGTH
Yen Lin2a6f0362012-04-02 13:18:48 +000045 * \param key_schedule Expanded key to use
46 * \param src Source data of length 'num_aes_blocks' blocks
Philippe Reynes8302d172020-01-06 15:22:35 +010047 * \param dst Destination buffer, length AES128_KEY_LENGTH
Yen Lin2a6f0362012-04-02 13:18:48 +000048 * \param num_aes_blocks Number of AES blocks to encrypt
49 */
50static void sign_object(u8 *key, u8 *key_schedule, u8 *src, u8 *dst,
51 u32 num_aes_blocks)
52{
Philippe Reynes8302d172020-01-06 15:22:35 +010053 u8 tmp_data[AES128_KEY_LENGTH];
54 u8 iv[AES128_KEY_LENGTH] = {0};
55 u8 left[AES128_KEY_LENGTH];
56 u8 k1[AES128_KEY_LENGTH];
Yen Lin2a6f0362012-04-02 13:18:48 +000057 u8 *cbc_chain_data;
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +020058 unsigned int i;
Yen Lin2a6f0362012-04-02 13:18:48 +000059
60 cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
61
62 /* compute K1 constant needed by AES-CMAC calculation */
Philippe Reynes8302d172020-01-06 15:22:35 +010063 for (i = 0; i < AES128_KEY_LENGTH; i++)
Yen Lin2a6f0362012-04-02 13:18:48 +000064 tmp_data[i] = 0;
65
Philippe Reynes8302d172020-01-06 15:22:35 +010066 aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv,
67 tmp_data, left, 1);
Yen Lin2a6f0362012-04-02 13:18:48 +000068
69 left_shift_vector(left, k1, sizeof(left));
Yen Lin2a6f0362012-04-02 13:18:48 +000070
71 if ((left[0] >> 7) != 0) /* get MSB of L */
Philippe Reynes8302d172020-01-06 15:22:35 +010072 k1[AES128_KEY_LENGTH - 1] ^= AES_CMAC_CONST_RB;
Yen Lin2a6f0362012-04-02 13:18:48 +000073
74 /* compute the AES-CMAC value */
75 for (i = 0; i < num_aes_blocks; i++) {
76 /* Apply the chain data */
Stephen Warren53eb7682014-04-18 10:28:58 -060077 aes_apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
Yen Lin2a6f0362012-04-02 13:18:48 +000078
79 /* for the final block, XOR K1 into the IV */
80 if (i == num_aes_blocks - 1)
Stephen Warren53eb7682014-04-18 10:28:58 -060081 aes_apply_cbc_chain_data(tmp_data, k1, tmp_data);
Yen Lin2a6f0362012-04-02 13:18:48 +000082
83 /* encrypt the AES block */
Philippe Reynes8302d172020-01-06 15:22:35 +010084 aes_encrypt(AES128_KEY_LENGTH, tmp_data,
85 key_schedule, dst);
Yen Lin2a6f0362012-04-02 13:18:48 +000086
87 debug("sign_obj: block %d of %d\n", i, num_aes_blocks);
Yen Lin2a6f0362012-04-02 13:18:48 +000088
89 /* Update pointers for next loop. */
90 cbc_chain_data = dst;
Philippe Reynes8302d172020-01-06 15:22:35 +010091 src += AES128_KEY_LENGTH;
Yen Lin2a6f0362012-04-02 13:18:48 +000092 }
Yen Lin2a6f0362012-04-02 13:18:48 +000093}
94
95/**
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +020096 * Decrypt, encrypt or sign a block of data (depending on security mode).
Yen Lin2a6f0362012-04-02 13:18:48 +000097 *
Philippe Reynes8302d172020-01-06 15:22:35 +010098 * \param key Input AES key, length AES128_KEY_LENGTH
Yen Lin2a6f0362012-04-02 13:18:48 +000099 * \param oper Security operations mask to perform (enum security_op)
100 * \param src Source data
101 * \param length Size of source data
Philippe Reynes8302d172020-01-06 15:22:35 +0100102 * \param sig_dst Destination address for signature, AES128_KEY_LENGTH bytes
Yen Lin2a6f0362012-04-02 13:18:48 +0000103 */
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200104static int tegra_crypto_core(u8 *key, enum security_op oper, u8 *src,
105 u32 length, u8 *sig_dst)
Yen Lin2a6f0362012-04-02 13:18:48 +0000106{
107 u32 num_aes_blocks;
Philippe Reynes8302d172020-01-06 15:22:35 +0100108 u8 key_schedule[AES128_EXPAND_KEY_LENGTH];
109 u8 iv[AES128_KEY_LENGTH] = {0};
Yen Lin2a6f0362012-04-02 13:18:48 +0000110
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200111 debug("%s: length = %d\n", __func__, length);
Yen Lin2a6f0362012-04-02 13:18:48 +0000112
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200113 aes_expand_key(key, AES128_KEY_LENGTH, key_schedule);
Yen Lin2a6f0362012-04-02 13:18:48 +0000114
Philippe Reynes8302d172020-01-06 15:22:35 +0100115 num_aes_blocks = (length + AES128_KEY_LENGTH - 1) / AES128_KEY_LENGTH;
Yen Lin2a6f0362012-04-02 13:18:48 +0000116
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200117 if (oper & SECURITY_DECRYPT) {
118 /* Perform this in place, resulting in src being decrypted. */
119 debug("%s: begin decryption\n", __func__);
120 aes_cbc_decrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, src,
121 src, num_aes_blocks);
122 debug("%s: end decryption\n", __func__);
123 }
124
Yen Lin2a6f0362012-04-02 13:18:48 +0000125 if (oper & SECURITY_ENCRYPT) {
126 /* Perform this in place, resulting in src being encrypted. */
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200127 debug("%s: begin encryption\n", __func__);
Philippe Reynes8302d172020-01-06 15:22:35 +0100128 aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, src,
129 src, num_aes_blocks);
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200130 debug("%s: end encryption\n", __func__);
Yen Lin2a6f0362012-04-02 13:18:48 +0000131 }
132
133 if (oper & SECURITY_SIGN) {
134 /* encrypt the data, overwriting the result in signature. */
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200135 debug("%s: begin signing\n", __func__);
Yen Lin2a6f0362012-04-02 13:18:48 +0000136 sign_object(key, key_schedule, src, sig_dst, num_aes_blocks);
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200137 debug("%s: end signing\n", __func__);
Yen Lin2a6f0362012-04-02 13:18:48 +0000138 }
139
140 return 0;
141}
142
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200143/**
144 * Tegra crypto group
145 */
146int sign_data_block(u8 *source, unsigned int length, u8 *signature)
Yen Lin2a6f0362012-04-02 13:18:48 +0000147{
Svyatoslav Ryhelbab08782023-02-14 19:35:36 +0200148 return tegra_crypto_core(zero_key, SECURITY_SIGN, source,
149 length, signature);
150}
151
152int sign_enc_data_block(u8 *source, unsigned int length, u8 *signature, u8 *key)
153{
154 return tegra_crypto_core(key, SECURITY_SIGN, source,
155 length, signature);
156}
157
158int encrypt_data_block(u8 *source, unsigned int length, u8 *key)
159{
160 return tegra_crypto_core(key, SECURITY_ENCRYPT, source,
161 length, NULL);
162}
163
164int decrypt_data_block(u8 *source, unsigned int length, u8 *key)
165{
166 return tegra_crypto_core(key, SECURITY_DECRYPT, source,
167 length, NULL);
Yen Lin2a6f0362012-04-02 13:18:48 +0000168}