key_wrap.c 2.9 KB

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