cipher_aes_wrp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * This file uses the low level AES functions (which are deprecated for
  11. * non-internal use) in order to implement provider AES ciphers.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "cipher_aes.h"
  15. #include "prov/providercommon.h"
  16. #include "prov/providercommonerr.h"
  17. #include "prov/implementations.h"
  18. /* AES wrap with padding has IV length of 4, without padding 8 */
  19. #define AES_WRAP_PAD_IVLEN 4
  20. #define AES_WRAP_NOPAD_IVLEN 8
  21. /* TODO(3.0) Figure out what flags need to be passed */
  22. #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV \
  23. | EVP_CIPH_ALWAYS_CALL_INIT)
  24. typedef size_t (*aeswrap_fn)(void *key, const unsigned char *iv,
  25. unsigned char *out, const unsigned char *in,
  26. size_t inlen, block128_f block);
  27. static OSSL_FUNC_cipher_encrypt_init_fn aes_wrap_einit;
  28. static OSSL_FUNC_cipher_decrypt_init_fn aes_wrap_dinit;
  29. static OSSL_FUNC_cipher_update_fn aes_wrap_cipher;
  30. static OSSL_FUNC_cipher_final_fn aes_wrap_final;
  31. static OSSL_FUNC_cipher_freectx_fn aes_wrap_freectx;
  32. typedef struct prov_aes_wrap_ctx_st {
  33. PROV_CIPHER_CTX base;
  34. union {
  35. OSSL_UNION_ALIGN;
  36. AES_KEY ks;
  37. } ks;
  38. aeswrap_fn wrapfn;
  39. } PROV_AES_WRAP_CTX;
  40. static void *aes_wrap_newctx(size_t kbits, size_t blkbits,
  41. size_t ivbits, unsigned int mode, uint64_t flags)
  42. {
  43. PROV_AES_WRAP_CTX *wctx;
  44. PROV_CIPHER_CTX *ctx;
  45. if (!ossl_prov_is_running())
  46. return NULL;
  47. wctx = OPENSSL_zalloc(sizeof(*wctx));
  48. ctx = (PROV_CIPHER_CTX *)wctx;
  49. if (ctx != NULL) {
  50. ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,
  51. NULL, NULL);
  52. ctx->pad = (ctx->ivlen == AES_WRAP_PAD_IVLEN);
  53. }
  54. return wctx;
  55. }
  56. static void aes_wrap_freectx(void *vctx)
  57. {
  58. PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
  59. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  60. OPENSSL_clear_free(wctx, sizeof(*wctx));
  61. }
  62. static int aes_wrap_init(void *vctx, const unsigned char *key,
  63. size_t keylen, const unsigned char *iv,
  64. size_t ivlen, int enc)
  65. {
  66. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  67. PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
  68. if (!ossl_prov_is_running())
  69. return 0;
  70. ctx->enc = enc;
  71. ctx->block = enc ? (block128_f)AES_encrypt : (block128_f)AES_decrypt;
  72. if (ctx->pad)
  73. wctx->wrapfn = enc ? CRYPTO_128_wrap_pad : CRYPTO_128_unwrap_pad;
  74. else
  75. wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;
  76. if (iv != NULL) {
  77. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  78. return 0;
  79. }
  80. if (key != NULL) {
  81. if (keylen != ctx->keylen) {
  82. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  83. return 0;
  84. }
  85. if (ctx->enc)
  86. AES_set_encrypt_key(key, keylen * 8, &wctx->ks.ks);
  87. else
  88. AES_set_decrypt_key(key, keylen * 8, &wctx->ks.ks);
  89. }
  90. return 1;
  91. }
  92. static int aes_wrap_einit(void *ctx, const unsigned char *key, size_t keylen,
  93. const unsigned char *iv, size_t ivlen)
  94. {
  95. return aes_wrap_init(ctx, key, keylen, iv, ivlen, 1);
  96. }
  97. static int aes_wrap_dinit(void *ctx, const unsigned char *key, size_t keylen,
  98. const unsigned char *iv, size_t ivlen)
  99. {
  100. return aes_wrap_init(ctx, key, keylen, iv, ivlen, 0);
  101. }
  102. static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,
  103. const unsigned char *in, size_t inlen)
  104. {
  105. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  106. PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
  107. size_t rv;
  108. int pad = ctx->pad;
  109. /* No final operation so always return zero length */
  110. if (in == NULL)
  111. return 0;
  112. /* Input length must always be non-zero */
  113. if (inlen == 0)
  114. return -1;
  115. /* If decrypting need at least 16 bytes and multiple of 8 */
  116. if (!ctx->enc && (inlen < 16 || inlen & 0x7))
  117. return -1;
  118. /* If not padding input must be multiple of 8 */
  119. if (!pad && inlen & 0x7)
  120. return -1;
  121. if (out == NULL) {
  122. if (ctx->enc) {
  123. /* If padding round up to multiple of 8 */
  124. if (pad)
  125. inlen = (inlen + 7) / 8 * 8;
  126. /* 8 byte prefix */
  127. return inlen + 8;
  128. } else {
  129. /*
  130. * If not padding output will be exactly 8 bytes smaller than
  131. * input. If padding it will be at least 8 bytes smaller but we
  132. * don't know how much.
  133. */
  134. return inlen - 8;
  135. }
  136. }
  137. rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,
  138. inlen, ctx->block);
  139. return rv ? (int)rv : -1;
  140. }
  141. static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,
  142. size_t outsize)
  143. {
  144. if (!ossl_prov_is_running())
  145. return 0;
  146. *outl = 0;
  147. return 1;
  148. }
  149. static int aes_wrap_cipher(void *vctx,
  150. unsigned char *out, size_t *outl, size_t outsize,
  151. const unsigned char *in, size_t inl)
  152. {
  153. PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;
  154. size_t len;
  155. if (!ossl_prov_is_running())
  156. return 0;
  157. if (inl == 0) {
  158. *outl = 0;
  159. return 1;
  160. }
  161. if (outsize < inl) {
  162. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  163. return -1;
  164. }
  165. len = aes_wrap_cipher_internal(ctx, out, in, inl);
  166. if (len == 0)
  167. return -1;
  168. *outl = len;
  169. return 1;
  170. }
  171. static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  172. {
  173. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  174. const OSSL_PARAM *p;
  175. size_t keylen = 0;
  176. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  177. if (p != NULL) {
  178. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  179. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  180. return 0;
  181. }
  182. if (ctx->keylen != keylen) {
  183. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  184. return 0;
  185. }
  186. }
  187. return 1;
  188. }
  189. #define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits) \
  190. static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params; \
  191. static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[]) \
  192. { \
  193. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
  194. flags, kbits, blkbits, ivbits); \
  195. } \
  196. static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx; \
  197. static void *aes_##kbits##fname##_newctx(void *provctx) \
  198. { \
  199. return aes_##mode##_newctx(kbits, blkbits, ivbits, \
  200. EVP_CIPH_##UCMODE##_MODE, flags); \
  201. } \
  202. const OSSL_DISPATCH ossl_##aes##kbits##fname##_functions[] = { \
  203. { OSSL_FUNC_CIPHER_NEWCTX, \
  204. (void (*)(void))aes_##kbits##fname##_newctx }, \
  205. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \
  206. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \
  207. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher }, \
  208. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final }, \
  209. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx }, \
  210. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  211. (void (*)(void))aes_##kbits##_##fname##_get_params }, \
  212. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  213. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  214. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  215. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  216. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  217. (void (*)(void))aes_wrap_set_ctx_params }, \
  218. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  219. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  220. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  221. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  222. { 0, NULL } \
  223. }
  224. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
  225. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
  226. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
  227. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);
  228. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);
  229. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);