g_eli_key.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/param.h>
  29. #ifdef _KERNEL
  30. #include <sys/malloc.h>
  31. #include <sys/systm.h>
  32. #include <geom/geom.h>
  33. #else
  34. #include <stdio.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <strings.h>
  39. #include <errno.h>
  40. #endif
  41. #include <geom/eli/g_eli.h>
  42. #ifdef _KERNEL
  43. MALLOC_DECLARE(M_ELI);
  44. #endif
  45. /*
  46. * Verify if the given 'key' is correct.
  47. * Return 1 if it is correct and 0 otherwise.
  48. */
  49. static int
  50. g_eli_mkey_verify(const unsigned char *mkey, const unsigned char *key)
  51. {
  52. const unsigned char *odhmac; /* On-disk HMAC. */
  53. unsigned char chmac[SHA512_MDLEN]; /* Calculated HMAC. */
  54. unsigned char hmkey[SHA512_MDLEN]; /* Key for HMAC. */
  55. /*
  56. * The key for HMAC calculations is: hmkey = HMAC_SHA512(Derived-Key, 0)
  57. */
  58. g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x00", 1, hmkey, 0);
  59. odhmac = mkey + G_ELI_DATAIVKEYLEN;
  60. /* Calculate HMAC from Data-Key and IV-Key. */
  61. g_eli_crypto_hmac(hmkey, sizeof(hmkey), mkey, G_ELI_DATAIVKEYLEN,
  62. chmac, 0);
  63. explicit_bzero(hmkey, sizeof(hmkey));
  64. /*
  65. * Compare calculated HMAC with HMAC from metadata.
  66. * If two HMACs are equal, 'key' is correct.
  67. */
  68. return (!bcmp(odhmac, chmac, SHA512_MDLEN));
  69. }
  70. /*
  71. * Calculate HMAC from Data-Key and IV-Key.
  72. */
  73. void
  74. g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key)
  75. {
  76. unsigned char hmkey[SHA512_MDLEN]; /* Key for HMAC. */
  77. unsigned char *odhmac; /* On-disk HMAC. */
  78. /*
  79. * The key for HMAC calculations is: hmkey = HMAC_SHA512(Derived-Key, 0)
  80. */
  81. g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x00", 1, hmkey, 0);
  82. odhmac = mkey + G_ELI_DATAIVKEYLEN;
  83. /* Calculate HMAC from Data-Key and IV-Key. */
  84. g_eli_crypto_hmac(hmkey, sizeof(hmkey), mkey, G_ELI_DATAIVKEYLEN,
  85. odhmac, 0);
  86. explicit_bzero(hmkey, sizeof(hmkey));
  87. }
  88. /*
  89. * Find and decrypt Master Key encrypted with 'key' at slot 'nkey'.
  90. * Return 0 on success, > 0 on failure, -1 on bad key.
  91. */
  92. int
  93. g_eli_mkey_decrypt(const struct g_eli_metadata *md, const unsigned char *key,
  94. unsigned char *mkey, unsigned nkey)
  95. {
  96. unsigned char tmpmkey[G_ELI_MKEYLEN];
  97. unsigned char enckey[SHA512_MDLEN]; /* Key for encryption. */
  98. const unsigned char *mmkey;
  99. int bit, error;
  100. if (nkey > G_ELI_MKEYLEN)
  101. return (-1);
  102. /*
  103. * The key for encryption is: enckey = HMAC_SHA512(Derived-Key, 1)
  104. */
  105. g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x01", 1, enckey, 0);
  106. mmkey = md->md_mkeys + G_ELI_MKEYLEN * nkey;
  107. bit = (1 << nkey);
  108. if (!(md->md_keys & bit))
  109. return (-1);
  110. bcopy(mmkey, tmpmkey, G_ELI_MKEYLEN);
  111. error = g_eli_crypto_decrypt(md->md_ealgo, tmpmkey,
  112. G_ELI_MKEYLEN, enckey, md->md_keylen);
  113. if (error != 0) {
  114. explicit_bzero(tmpmkey, sizeof(tmpmkey));
  115. explicit_bzero(enckey, sizeof(enckey));
  116. return (error);
  117. }
  118. if (g_eli_mkey_verify(tmpmkey, key)) {
  119. bcopy(tmpmkey, mkey, G_ELI_DATAIVKEYLEN);
  120. explicit_bzero(tmpmkey, sizeof(tmpmkey));
  121. explicit_bzero(enckey, sizeof(enckey));
  122. return (0);
  123. }
  124. explicit_bzero(enckey, sizeof(enckey));
  125. explicit_bzero(tmpmkey, sizeof(tmpmkey));
  126. return (-1);
  127. }
  128. /*
  129. * Find and decrypt Master Key encrypted with 'key'.
  130. * Return decrypted Master Key number in 'nkeyp' if not NULL.
  131. * Return 0 on success, > 0 on failure, -1 on bad key.
  132. */
  133. int
  134. g_eli_mkey_decrypt_any(const struct g_eli_metadata *md,
  135. const unsigned char *key, unsigned char *mkey, unsigned *nkeyp)
  136. {
  137. int error, nkey;
  138. if (nkeyp != NULL)
  139. *nkeyp = -1;
  140. error = -1;
  141. for (nkey = 0; nkey < G_ELI_MAXMKEYS; nkey++) {
  142. error = g_eli_mkey_decrypt(md, key, mkey, nkey);
  143. if (error == 0) {
  144. if (nkeyp != NULL)
  145. *nkeyp = nkey;
  146. break;
  147. } else if (error > 0) {
  148. break;
  149. }
  150. }
  151. return (error);
  152. }
  153. /*
  154. * Encrypt the Master-Key and calculate HMAC to be able to verify it in the
  155. * future.
  156. */
  157. int
  158. g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
  159. unsigned char *mkey)
  160. {
  161. unsigned char enckey[SHA512_MDLEN]; /* Key for encryption. */
  162. int error;
  163. /*
  164. * To calculate HMAC, the whole key (G_ELI_USERKEYLEN bytes long) will
  165. * be used.
  166. */
  167. g_eli_mkey_hmac(mkey, key);
  168. /*
  169. * The key for encryption is: enckey = HMAC_SHA512(Derived-Key, 1)
  170. */
  171. g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x01", 1, enckey, 0);
  172. /*
  173. * Encrypt the Master-Key and HMAC() result with the given key (this
  174. * time only 'keylen' bits from the key are used).
  175. */
  176. error = g_eli_crypto_encrypt(algo, mkey, G_ELI_MKEYLEN, enckey, keylen);
  177. explicit_bzero(enckey, sizeof(enckey));
  178. return (error);
  179. }
  180. #ifdef _KERNEL
  181. /*
  182. * When doing encryption only, copy IV key and encryption key.
  183. * When doing encryption and authentication, copy IV key, generate encryption
  184. * key and generate authentication key.
  185. */
  186. void
  187. g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey)
  188. {
  189. /* Remember the Master Key. */
  190. bcopy(mkey, sc->sc_mkey, sizeof(sc->sc_mkey));
  191. bcopy(mkey, sc->sc_ivkey, sizeof(sc->sc_ivkey));
  192. mkey += sizeof(sc->sc_ivkey);
  193. /*
  194. * The authentication key is: akey = HMAC_SHA512(Data-Key, 0x11)
  195. */
  196. if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
  197. g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x11", 1,
  198. sc->sc_akey, 0);
  199. } else {
  200. arc4rand(sc->sc_akey, sizeof(sc->sc_akey), 0);
  201. }
  202. /* Initialize encryption keys. */
  203. g_eli_key_init(sc);
  204. if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
  205. /*
  206. * Precalculate SHA256 for HMAC key generation.
  207. * This is expensive operation and we can do it only once now or
  208. * for every access to sector, so now will be much better.
  209. */
  210. SHA256_Init(&sc->sc_akeyctx);
  211. SHA256_Update(&sc->sc_akeyctx, sc->sc_akey,
  212. sizeof(sc->sc_akey));
  213. }
  214. /*
  215. * Precalculate SHA256 for IV generation.
  216. * This is expensive operation and we can do it only once now or for
  217. * every access to sector, so now will be much better.
  218. */
  219. switch (sc->sc_ealgo) {
  220. case CRYPTO_AES_XTS:
  221. break;
  222. default:
  223. SHA256_Init(&sc->sc_ivctx);
  224. SHA256_Update(&sc->sc_ivctx, sc->sc_ivkey,
  225. sizeof(sc->sc_ivkey));
  226. break;
  227. }
  228. }
  229. #endif