cipher_aes_wrp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright 2019-2021 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 <openssl/proverr.h>
  15. #include "cipher_aes.h"
  16. #include "prov/providercommon.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. #define WRAP_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)
  22. #define WRAP_FLAGS_INV (WRAP_FLAGS | PROV_CIPHER_FLAG_INVERSE_CIPHER)
  23. typedef size_t (*aeswrap_fn)(void *key, const unsigned char *iv,
  24. unsigned char *out, const unsigned char *in,
  25. size_t inlen, block128_f block);
  26. static OSSL_FUNC_cipher_encrypt_init_fn aes_wrap_einit;
  27. static OSSL_FUNC_cipher_decrypt_init_fn aes_wrap_dinit;
  28. static OSSL_FUNC_cipher_update_fn aes_wrap_cipher;
  29. static OSSL_FUNC_cipher_final_fn aes_wrap_final;
  30. static OSSL_FUNC_cipher_freectx_fn aes_wrap_freectx;
  31. static OSSL_FUNC_cipher_set_ctx_params_fn aes_wrap_set_ctx_params;
  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, const OSSL_PARAM params[], 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. if (ctx->pad)
  72. wctx->wrapfn = enc ? CRYPTO_128_wrap_pad : CRYPTO_128_unwrap_pad;
  73. else
  74. wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;
  75. if (iv != NULL) {
  76. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  77. return 0;
  78. }
  79. if (key != NULL) {
  80. int use_forward_transform;
  81. if (keylen != ctx->keylen) {
  82. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  83. return 0;
  84. }
  85. /*
  86. * See SP800-38F : Section 5.1
  87. * The forward and inverse transformations for the AES block
  88. * cipher—called “cipher” and “inverse cipher” are informally known as
  89. * the AES encryption and AES decryption functions, respectively.
  90. * If the designated cipher function for a key-wrap algorithm is chosen
  91. * to be the AES decryption function, then CIPH-1K will be the AES
  92. * encryption function.
  93. */
  94. if (ctx->inverse_cipher == 0)
  95. use_forward_transform = ctx->enc;
  96. else
  97. use_forward_transform = !ctx->enc;
  98. if (use_forward_transform) {
  99. AES_set_encrypt_key(key, keylen * 8, &wctx->ks.ks);
  100. ctx->block = (block128_f)AES_encrypt;
  101. } else {
  102. AES_set_decrypt_key(key, keylen * 8, &wctx->ks.ks);
  103. ctx->block = (block128_f)AES_decrypt;
  104. }
  105. }
  106. return aes_wrap_set_ctx_params(ctx, params);
  107. }
  108. static int aes_wrap_einit(void *ctx, const unsigned char *key, size_t keylen,
  109. const unsigned char *iv, size_t ivlen,
  110. const OSSL_PARAM params[])
  111. {
  112. return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 1);
  113. }
  114. static int aes_wrap_dinit(void *ctx, const unsigned char *key, size_t keylen,
  115. const unsigned char *iv, size_t ivlen,
  116. const OSSL_PARAM params[])
  117. {
  118. return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 0);
  119. }
  120. static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,
  121. const unsigned char *in, size_t inlen)
  122. {
  123. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  124. PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;
  125. size_t rv;
  126. int pad = ctx->pad;
  127. /* No final operation so always return zero length */
  128. if (in == NULL)
  129. return 0;
  130. /* Input length must always be non-zero */
  131. if (inlen == 0)
  132. return -1;
  133. /* If decrypting need at least 16 bytes and multiple of 8 */
  134. if (!ctx->enc && (inlen < 16 || inlen & 0x7))
  135. return -1;
  136. /* If not padding input must be multiple of 8 */
  137. if (!pad && inlen & 0x7)
  138. return -1;
  139. if (out == NULL) {
  140. if (ctx->enc) {
  141. /* If padding round up to multiple of 8 */
  142. if (pad)
  143. inlen = (inlen + 7) / 8 * 8;
  144. /* 8 byte prefix */
  145. return inlen + 8;
  146. } else {
  147. /*
  148. * If not padding output will be exactly 8 bytes smaller than
  149. * input. If padding it will be at least 8 bytes smaller but we
  150. * don't know how much.
  151. */
  152. return inlen - 8;
  153. }
  154. }
  155. rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,
  156. inlen, ctx->block);
  157. return rv ? (int)rv : -1;
  158. }
  159. static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,
  160. size_t outsize)
  161. {
  162. if (!ossl_prov_is_running())
  163. return 0;
  164. *outl = 0;
  165. return 1;
  166. }
  167. static int aes_wrap_cipher(void *vctx,
  168. unsigned char *out, size_t *outl, size_t outsize,
  169. const unsigned char *in, size_t inl)
  170. {
  171. PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;
  172. size_t len;
  173. if (!ossl_prov_is_running())
  174. return 0;
  175. if (inl == 0) {
  176. *outl = 0;
  177. return 1;
  178. }
  179. if (outsize < inl) {
  180. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  181. return -1;
  182. }
  183. len = aes_wrap_cipher_internal(ctx, out, in, inl);
  184. if (len == 0)
  185. return -1;
  186. *outl = len;
  187. return 1;
  188. }
  189. static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  190. {
  191. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  192. const OSSL_PARAM *p;
  193. size_t keylen = 0;
  194. if (params == NULL)
  195. return 1;
  196. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  197. if (p != NULL) {
  198. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  199. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  200. return 0;
  201. }
  202. if (ctx->keylen != keylen) {
  203. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  204. return 0;
  205. }
  206. }
  207. return 1;
  208. }
  209. #define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits) \
  210. static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params; \
  211. static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[]) \
  212. { \
  213. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
  214. flags, kbits, blkbits, ivbits); \
  215. } \
  216. static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx; \
  217. static void *aes_##kbits##fname##_newctx(void *provctx) \
  218. { \
  219. return aes_##mode##_newctx(kbits, blkbits, ivbits, \
  220. EVP_CIPH_##UCMODE##_MODE, flags); \
  221. } \
  222. const OSSL_DISPATCH ossl_##aes##kbits##fname##_functions[] = { \
  223. { OSSL_FUNC_CIPHER_NEWCTX, \
  224. (void (*)(void))aes_##kbits##fname##_newctx }, \
  225. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \
  226. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \
  227. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher }, \
  228. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final }, \
  229. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx }, \
  230. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  231. (void (*)(void))aes_##kbits##_##fname##_get_params }, \
  232. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  233. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  234. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  235. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  236. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  237. (void (*)(void))aes_wrap_set_ctx_params }, \
  238. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  239. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  240. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  241. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  242. { 0, NULL } \
  243. }
  244. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
  245. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
  246. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
  247. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);
  248. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);
  249. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);
  250. IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
  251. IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
  252. IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
  253. IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_PAD_IVLEN * 8);
  254. IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_PAD_IVLEN * 8);
  255. IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_PAD_IVLEN * 8);