crypto.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2010 - 2011 NVIDIA Corporation <www.nvidia.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/errno.h>
  9. #include "crypto.h"
  10. #include "aes.h"
  11. static u8 zero_key[16];
  12. #define AES_CMAC_CONST_RB 0x87 /* from RFC 4493, Figure 2.2 */
  13. enum security_op {
  14. SECURITY_SIGN = 1 << 0, /* Sign the data */
  15. SECURITY_ENCRYPT = 1 << 1, /* Encrypt the data */
  16. };
  17. /**
  18. * Shift a vector left by one bit
  19. *
  20. * \param in Input vector
  21. * \param out Output vector
  22. * \param size Length of vector in bytes
  23. */
  24. static void left_shift_vector(u8 *in, u8 *out, int size)
  25. {
  26. int carry = 0;
  27. int i;
  28. for (i = size - 1; i >= 0; i--) {
  29. out[i] = (in[i] << 1) | carry;
  30. carry = in[i] >> 7; /* get most significant bit */
  31. }
  32. }
  33. /**
  34. * Sign a block of data, putting the result into dst.
  35. *
  36. * \param key Input AES key, length AES_KEY_LENGTH
  37. * \param key_schedule Expanded key to use
  38. * \param src Source data of length 'num_aes_blocks' blocks
  39. * \param dst Destination buffer, length AES_KEY_LENGTH
  40. * \param num_aes_blocks Number of AES blocks to encrypt
  41. */
  42. static void sign_object(u8 *key, u8 *key_schedule, u8 *src, u8 *dst,
  43. u32 num_aes_blocks)
  44. {
  45. u8 tmp_data[AES_KEY_LENGTH];
  46. u8 left[AES_KEY_LENGTH];
  47. u8 k1[AES_KEY_LENGTH];
  48. u8 *cbc_chain_data;
  49. unsigned i;
  50. cbc_chain_data = zero_key; /* Convenient array of 0's for IV */
  51. /* compute K1 constant needed by AES-CMAC calculation */
  52. for (i = 0; i < AES_KEY_LENGTH; i++)
  53. tmp_data[i] = 0;
  54. aes_cbc_encrypt_blocks(key_schedule, tmp_data, left, 1);
  55. debug_print_vector("AES(key, nonce)", AES_KEY_LENGTH, left);
  56. left_shift_vector(left, k1, sizeof(left));
  57. debug_print_vector("L", AES_KEY_LENGTH, left);
  58. if ((left[0] >> 7) != 0) /* get MSB of L */
  59. k1[AES_KEY_LENGTH-1] ^= AES_CMAC_CONST_RB;
  60. debug_print_vector("K1", AES_KEY_LENGTH, k1);
  61. /* compute the AES-CMAC value */
  62. for (i = 0; i < num_aes_blocks; i++) {
  63. /* Apply the chain data */
  64. aes_apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
  65. /* for the final block, XOR K1 into the IV */
  66. if (i == num_aes_blocks - 1)
  67. aes_apply_cbc_chain_data(tmp_data, k1, tmp_data);
  68. /* encrypt the AES block */
  69. aes_encrypt(tmp_data, key_schedule, dst);
  70. debug("sign_obj: block %d of %d\n", i, num_aes_blocks);
  71. debug_print_vector("AES-CMAC Src", AES_KEY_LENGTH, src);
  72. debug_print_vector("AES-CMAC Xor", AES_KEY_LENGTH, tmp_data);
  73. debug_print_vector("AES-CMAC Dst", AES_KEY_LENGTH, dst);
  74. /* Update pointers for next loop. */
  75. cbc_chain_data = dst;
  76. src += AES_KEY_LENGTH;
  77. }
  78. debug_print_vector("AES-CMAC Hash", AES_KEY_LENGTH, dst);
  79. }
  80. /**
  81. * Encrypt and sign a block of data (depending on security mode).
  82. *
  83. * \param key Input AES key, length AES_KEY_LENGTH
  84. * \param oper Security operations mask to perform (enum security_op)
  85. * \param src Source data
  86. * \param length Size of source data
  87. * \param sig_dst Destination address for signature, AES_KEY_LENGTH bytes
  88. */
  89. static int encrypt_and_sign(u8 *key, enum security_op oper, u8 *src,
  90. u32 length, u8 *sig_dst)
  91. {
  92. u32 num_aes_blocks;
  93. u8 key_schedule[AES_EXPAND_KEY_LENGTH];
  94. debug("encrypt_and_sign: length = %d\n", length);
  95. debug_print_vector("AES key", AES_KEY_LENGTH, key);
  96. /*
  97. * The only need for a key is for signing/checksum purposes, so
  98. * if not encrypting, expand a key of 0s.
  99. */
  100. aes_expand_key(oper & SECURITY_ENCRYPT ? key : zero_key, key_schedule);
  101. num_aes_blocks = (length + AES_KEY_LENGTH - 1) / AES_KEY_LENGTH;
  102. if (oper & SECURITY_ENCRYPT) {
  103. /* Perform this in place, resulting in src being encrypted. */
  104. debug("encrypt_and_sign: begin encryption\n");
  105. aes_cbc_encrypt_blocks(key_schedule, src, src, num_aes_blocks);
  106. debug("encrypt_and_sign: end encryption\n");
  107. }
  108. if (oper & SECURITY_SIGN) {
  109. /* encrypt the data, overwriting the result in signature. */
  110. debug("encrypt_and_sign: begin signing\n");
  111. sign_object(key, key_schedule, src, sig_dst, num_aes_blocks);
  112. debug("encrypt_and_sign: end signing\n");
  113. }
  114. return 0;
  115. }
  116. int sign_data_block(u8 *source, unsigned length, u8 *signature)
  117. {
  118. return encrypt_and_sign(zero_key, SECURITY_SIGN, source,
  119. length, signature);
  120. }