aes_wrap.c 8.3 KB

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