crypto.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2010 - 2011 NVIDIA Corporation <www.nvidia.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/errno.h>
  25. #include "crypto.h"
  26. #include "aes.h"
  27. static u8 zero_key[16];
  28. #define AES_CMAC_CONST_RB 0x87 /* from RFC 4493, Figure 2.2 */
  29. enum security_op {
  30. SECURITY_SIGN = 1 << 0, /* Sign the data */
  31. SECURITY_ENCRYPT = 1 << 1, /* Encrypt the data */
  32. };
  33. static void debug_print_vector(char *name, u32 num_bytes, u8 *data)
  34. {
  35. u32 i;
  36. debug("%s [%d] @0x%08x", name, num_bytes, (u32)data);
  37. for (i = 0; i < num_bytes; i++) {
  38. if (i % 16 == 0)
  39. debug(" = ");
  40. debug("%02x", data[i]);
  41. if ((i+1) % 16 != 0)
  42. debug(" ");
  43. }
  44. debug("\n");
  45. }
  46. /**
  47. * Apply chain data to the destination using EOR
  48. *
  49. * Each array is of length AES_AES_KEY_LENGTH.
  50. *
  51. * \param cbc_chain_data Chain data
  52. * \param src Source data
  53. * \param dst Destination data, which is modified here
  54. */
  55. static void apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst)
  56. {
  57. int i;
  58. for (i = 0; i < 16; i++)
  59. *dst++ = *src++ ^ *cbc_chain_data++;
  60. }
  61. /**
  62. * Encrypt some data with AES.
  63. *
  64. * \param key_schedule Expanded key to use
  65. * \param src Source data to encrypt
  66. * \param dst Destination buffer
  67. * \param num_aes_blocks Number of AES blocks to encrypt
  68. */
  69. static void encrypt_object(u8 *key_schedule, u8 *src, u8 *dst,
  70. u32 num_aes_blocks)
  71. {
  72. u8 tmp_data[AES_KEY_LENGTH];
  73. u8 *cbc_chain_data;
  74. u32 i;
  75. cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
  76. for (i = 0; i < num_aes_blocks; i++) {
  77. debug("encrypt_object: block %d of %d\n", i, num_aes_blocks);
  78. debug_print_vector("AES Src", AES_KEY_LENGTH, src);
  79. /* Apply the chain data */
  80. apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
  81. debug_print_vector("AES Xor", AES_KEY_LENGTH, tmp_data);
  82. /* encrypt the AES block */
  83. aes_encrypt(tmp_data, key_schedule, dst);
  84. debug_print_vector("AES Dst", AES_KEY_LENGTH, dst);
  85. /* Update pointers for next loop. */
  86. cbc_chain_data = dst;
  87. src += AES_KEY_LENGTH;
  88. dst += AES_KEY_LENGTH;
  89. }
  90. }
  91. /**
  92. * Shift a vector left by one bit
  93. *
  94. * \param in Input vector
  95. * \param out Output vector
  96. * \param size Length of vector in bytes
  97. */
  98. static void left_shift_vector(u8 *in, u8 *out, int size)
  99. {
  100. int carry = 0;
  101. int i;
  102. for (i = size - 1; i >= 0; i--) {
  103. out[i] = (in[i] << 1) | carry;
  104. carry = in[i] >> 7; /* get most significant bit */
  105. }
  106. }
  107. /**
  108. * Sign a block of data, putting the result into dst.
  109. *
  110. * \param key Input AES key, length AES_KEY_LENGTH
  111. * \param key_schedule Expanded key to use
  112. * \param src Source data of length 'num_aes_blocks' blocks
  113. * \param dst Destination buffer, length AES_KEY_LENGTH
  114. * \param num_aes_blocks Number of AES blocks to encrypt
  115. */
  116. static void sign_object(u8 *key, u8 *key_schedule, u8 *src, u8 *dst,
  117. u32 num_aes_blocks)
  118. {
  119. u8 tmp_data[AES_KEY_LENGTH];
  120. u8 left[AES_KEY_LENGTH];
  121. u8 k1[AES_KEY_LENGTH];
  122. u8 *cbc_chain_data;
  123. unsigned i;
  124. cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
  125. /* compute K1 constant needed by AES-CMAC calculation */
  126. for (i = 0; i < AES_KEY_LENGTH; i++)
  127. tmp_data[i] = 0;
  128. encrypt_object(key_schedule, tmp_data, left, 1);
  129. debug_print_vector("AES(key, nonce)", AES_KEY_LENGTH, left);
  130. left_shift_vector(left, k1, sizeof(left));
  131. debug_print_vector("L", AES_KEY_LENGTH, left);
  132. if ((left[0] >> 7) != 0) /* get MSB of L */
  133. k1[AES_KEY_LENGTH-1] ^= AES_CMAC_CONST_RB;
  134. debug_print_vector("K1", AES_KEY_LENGTH, k1);
  135. /* compute the AES-CMAC value */
  136. for (i = 0; i < num_aes_blocks; i++) {
  137. /* Apply the chain data */
  138. apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
  139. /* for the final block, XOR K1 into the IV */
  140. if (i == num_aes_blocks - 1)
  141. apply_cbc_chain_data(tmp_data, k1, tmp_data);
  142. /* encrypt the AES block */
  143. aes_encrypt(tmp_data, key_schedule, dst);
  144. debug("sign_obj: block %d of %d\n", i, num_aes_blocks);
  145. debug_print_vector("AES-CMAC Src", AES_KEY_LENGTH, src);
  146. debug_print_vector("AES-CMAC Xor", AES_KEY_LENGTH, tmp_data);
  147. debug_print_vector("AES-CMAC Dst", AES_KEY_LENGTH, dst);
  148. /* Update pointers for next loop. */
  149. cbc_chain_data = dst;
  150. src += AES_KEY_LENGTH;
  151. }
  152. debug_print_vector("AES-CMAC Hash", AES_KEY_LENGTH, dst);
  153. }
  154. /**
  155. * Encrypt and sign a block of data (depending on security mode).
  156. *
  157. * \param key Input AES key, length AES_KEY_LENGTH
  158. * \param oper Security operations mask to perform (enum security_op)
  159. * \param src Source data
  160. * \param length Size of source data
  161. * \param sig_dst Destination address for signature, AES_KEY_LENGTH bytes
  162. */
  163. static int encrypt_and_sign(u8 *key, enum security_op oper, u8 *src,
  164. u32 length, u8 *sig_dst)
  165. {
  166. u32 num_aes_blocks;
  167. u8 key_schedule[AES_EXPAND_KEY_LENGTH];
  168. debug("encrypt_and_sign: length = %d\n", length);
  169. debug_print_vector("AES key", AES_KEY_LENGTH, key);
  170. /*
  171. * The only need for a key is for signing/checksum purposes, so
  172. * if not encrypting, expand a key of 0s.
  173. */
  174. aes_expand_key(oper & SECURITY_ENCRYPT ? key : zero_key, key_schedule);
  175. num_aes_blocks = (length + AES_KEY_LENGTH - 1) / AES_KEY_LENGTH;
  176. if (oper & SECURITY_ENCRYPT) {
  177. /* Perform this in place, resulting in src being encrypted. */
  178. debug("encrypt_and_sign: begin encryption\n");
  179. encrypt_object(key_schedule, src, src, num_aes_blocks);
  180. debug("encrypt_and_sign: end encryption\n");
  181. }
  182. if (oper & SECURITY_SIGN) {
  183. /* encrypt the data, overwriting the result in signature. */
  184. debug("encrypt_and_sign: begin signing\n");
  185. sign_object(key, key_schedule, src, sig_dst, num_aes_blocks);
  186. debug("encrypt_and_sign: end signing\n");
  187. }
  188. return 0;
  189. }
  190. int sign_data_block(u8 *source, unsigned length, u8 *signature)
  191. {
  192. return encrypt_and_sign(zero_key, SECURITY_SIGN, source,
  193. length, signature);
  194. }