aes_wrap.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* crypto/aes/aes_wrap.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include "cryptlib.h"
  54. #include <openssl/aes.h>
  55. #include <openssl/bio.h>
  56. static const unsigned char default_iv[] = {
  57. 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
  58. };
  59. int AES_wrap_key(AES_KEY *key, const unsigned char *iv,
  60. unsigned char *out,
  61. const unsigned char *in, unsigned int inlen)
  62. {
  63. unsigned char *A, B[16], *R;
  64. unsigned int i, j, t;
  65. if ((inlen & 0x7) || (inlen < 8))
  66. return -1;
  67. A = B;
  68. t = 1;
  69. memcpy(out + 8, in, inlen);
  70. if (!iv)
  71. iv = default_iv;
  72. memcpy(A, iv, 8);
  73. for (j = 0; j < 6; j++)
  74. {
  75. R = out + 8;
  76. for (i = 0; i < inlen; i += 8, t++, R += 8)
  77. {
  78. memcpy(B + 8, R, 8);
  79. AES_encrypt(B, B, key);
  80. A[7] ^= (unsigned char)(t & 0xff);
  81. if (t > 0xff)
  82. {
  83. A[6] ^= (unsigned char)((t >> 8) & 0xff);
  84. A[5] ^= (unsigned char)((t >> 16) & 0xff);
  85. A[4] ^= (unsigned char)((t >> 24) & 0xff);
  86. }
  87. memcpy(R, B + 8, 8);
  88. }
  89. }
  90. memcpy(out, A, 8);
  91. return inlen + 8;
  92. }
  93. int AES_unwrap_key(AES_KEY *key, const unsigned char *iv,
  94. unsigned char *out,
  95. const unsigned char *in, unsigned int inlen)
  96. {
  97. unsigned char *A, B[16], *R;
  98. unsigned int i, j, t;
  99. inlen -= 8;
  100. if (inlen & 0x7)
  101. return -1;
  102. if (inlen < 8)
  103. return -1;
  104. A = B;
  105. t = 6 * (inlen >> 3);
  106. memcpy(A, in, 8);
  107. memcpy(out, in + 8, inlen);
  108. for (j = 0; j < 6; j++)
  109. {
  110. R = out + inlen - 8;
  111. for (i = 0; i < inlen; i += 8, t--, R -= 8)
  112. {
  113. A[7] ^= (unsigned char)(t & 0xff);
  114. if (t > 0xff)
  115. {
  116. A[6] ^= (unsigned char)((t >> 8) & 0xff);
  117. A[5] ^= (unsigned char)((t >> 16) & 0xff);
  118. A[4] ^= (unsigned char)((t >> 24) & 0xff);
  119. }
  120. memcpy(B + 8, R, 8);
  121. AES_decrypt(B, B, key);
  122. memcpy(R, B + 8, 8);
  123. }
  124. }
  125. if (!iv)
  126. iv = default_iv;
  127. if (memcmp(A, iv, 8))
  128. {
  129. OPENSSL_cleanse(out, inlen);
  130. return 0;
  131. }
  132. return inlen;
  133. }
  134. #ifdef AES_WRAP_TEST
  135. int AES_wrap_unwrap_test(const unsigned char *kek, int keybits,
  136. const unsigned char *iv,
  137. const unsigned char *eout,
  138. const unsigned char *key, int keylen)
  139. {
  140. unsigned char *otmp = NULL, *ptmp = NULL;
  141. int r, ret = 0;
  142. AES_KEY wctx;
  143. otmp = OPENSSL_malloc(keylen + 8);
  144. ptmp = OPENSSL_malloc(keylen);
  145. if (!otmp || !ptmp)
  146. return 0;
  147. if (AES_set_encrypt_key(kek, keybits, &wctx))
  148. goto err;
  149. r = AES_wrap_key(&wctx, iv, otmp, key, keylen);
  150. if (r <= 0)
  151. goto err;
  152. if (eout && memcmp(eout, otmp, keylen))
  153. goto err;
  154. if (AES_set_decrypt_key(kek, keybits, &wctx))
  155. goto err;
  156. r = AES_unwrap_key(&wctx, iv, ptmp, otmp, r);
  157. if (memcmp(key, ptmp, keylen))
  158. goto err;
  159. ret = 1;
  160. err:
  161. if (otmp)
  162. OPENSSL_free(otmp);
  163. if (ptmp)
  164. OPENSSL_free(ptmp);
  165. return ret;
  166. }
  167. int main(int argc, char **argv)
  168. {
  169. static const unsigned char kek[] = {
  170. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  171. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  172. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  173. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  174. };
  175. static const unsigned char key[] = {
  176. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  177. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  178. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  179. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  180. };
  181. static const unsigned char e1[] = {
  182. 0x1f, 0xa6, 0x8b, 0x0a, 0x81, 0x12, 0xb4, 0x47,
  183. 0xae, 0xf3, 0x4b, 0xd8, 0xfb, 0x5a, 0x7b, 0x82,
  184. 0x9d, 0x3e, 0x86, 0x23, 0x71, 0xd2, 0xcf, 0xe5
  185. };
  186. static const unsigned char e2[] = {
  187. 0x96, 0x77, 0x8b, 0x25, 0xae, 0x6c, 0xa4, 0x35,
  188. 0xf9, 0x2b, 0x5b, 0x97, 0xc0, 0x50, 0xae, 0xd2,
  189. 0x46, 0x8a, 0xb8, 0xa1, 0x7a, 0xd8, 0x4e, 0x5d
  190. };
  191. static const unsigned char e3[] = {
  192. 0x64, 0xe8, 0xc3, 0xf9, 0xce, 0x0f, 0x5b, 0xa2,
  193. 0x63, 0xe9, 0x77, 0x79, 0x05, 0x81, 0x8a, 0x2a,
  194. 0x93, 0xc8, 0x19, 0x1e, 0x7d, 0x6e, 0x8a, 0xe7
  195. };
  196. static const unsigned char e4[] = {
  197. 0x03, 0x1d, 0x33, 0x26, 0x4e, 0x15, 0xd3, 0x32,
  198. 0x68, 0xf2, 0x4e, 0xc2, 0x60, 0x74, 0x3e, 0xdc,
  199. 0xe1, 0xc6, 0xc7, 0xdd, 0xee, 0x72, 0x5a, 0x93,
  200. 0x6b, 0xa8, 0x14, 0x91, 0x5c, 0x67, 0x62, 0xd2
  201. };
  202. static const unsigned char e5[] = {
  203. 0xa8, 0xf9, 0xbc, 0x16, 0x12, 0xc6, 0x8b, 0x3f,
  204. 0xf6, 0xe6, 0xf4, 0xfb, 0xe3, 0x0e, 0x71, 0xe4,
  205. 0x76, 0x9c, 0x8b, 0x80, 0xa3, 0x2c, 0xb8, 0x95,
  206. 0x8c, 0xd5, 0xd1, 0x7d, 0x6b, 0x25, 0x4d, 0xa1
  207. };
  208. static const unsigned char e6[] = {
  209. 0x28, 0xc9, 0xf4, 0x04, 0xc4, 0xb8, 0x10, 0xf4,
  210. 0xcb, 0xcc, 0xb3, 0x5c, 0xfb, 0x87, 0xf8, 0x26,
  211. 0x3f, 0x57, 0x86, 0xe2, 0xd8, 0x0e, 0xd3, 0x26,
  212. 0xcb, 0xc7, 0xf0, 0xe7, 0x1a, 0x99, 0xf4, 0x3b,
  213. 0xfb, 0x98, 0x8b, 0x9b, 0x7a, 0x02, 0xdd, 0x21
  214. };
  215. AES_KEY wctx, xctx;
  216. int ret;
  217. ret = AES_wrap_unwrap_test(kek, 128, NULL, e1, key, 16);
  218. fprintf(stderr, "Key test result %d\n", ret);
  219. ret = AES_wrap_unwrap_test(kek, 192, NULL, e2, key, 16);
  220. fprintf(stderr, "Key test result %d\n", ret);
  221. ret = AES_wrap_unwrap_test(kek, 256, NULL, e3, key, 16);
  222. fprintf(stderr, "Key test result %d\n", ret);
  223. ret = AES_wrap_unwrap_test(kek, 192, NULL, e4, key, 24);
  224. fprintf(stderr, "Key test result %d\n", ret);
  225. ret = AES_wrap_unwrap_test(kek, 256, NULL, e5, key, 24);
  226. fprintf(stderr, "Key test result %d\n", ret);
  227. ret = AES_wrap_unwrap_test(kek, 256, NULL, e6, key, 32);
  228. fprintf(stderr, "Key test result %d\n", ret);
  229. }
  230. #endif