cipher_aes_wrp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
  133. return -1;
  134. }
  135. /* If decrypting need at least 16 bytes and multiple of 8 */
  136. if (!ctx->enc && (inlen < 16 || inlen & 0x7)) {
  137. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
  138. return -1;
  139. }
  140. /* If not padding input must be multiple of 8 */
  141. if (!pad && inlen & 0x7) {
  142. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);
  143. return -1;
  144. }
  145. if (out == NULL) {
  146. if (ctx->enc) {
  147. /* If padding round up to multiple of 8 */
  148. if (pad)
  149. inlen = (inlen + 7) / 8 * 8;
  150. /* 8 byte prefix */
  151. return inlen + 8;
  152. } else {
  153. /*
  154. * If not padding output will be exactly 8 bytes smaller than
  155. * input. If padding it will be at least 8 bytes smaller but we
  156. * don't know how much.
  157. */
  158. return inlen - 8;
  159. }
  160. }
  161. rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,
  162. inlen, ctx->block);
  163. if (!rv) {
  164. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  165. return -1;
  166. }
  167. if (rv > INT_MAX) {
  168. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
  169. return -1;
  170. }
  171. return (int)rv;
  172. }
  173. static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,
  174. size_t outsize)
  175. {
  176. if (!ossl_prov_is_running())
  177. return 0;
  178. *outl = 0;
  179. return 1;
  180. }
  181. static int aes_wrap_cipher(void *vctx,
  182. unsigned char *out, size_t *outl, size_t outsize,
  183. const unsigned char *in, size_t inl)
  184. {
  185. PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;
  186. size_t len;
  187. if (!ossl_prov_is_running())
  188. return 0;
  189. if (inl == 0) {
  190. *outl = 0;
  191. return 1;
  192. }
  193. if (outsize < inl) {
  194. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  195. return 0;
  196. }
  197. len = aes_wrap_cipher_internal(ctx, out, in, inl);
  198. if (len <= 0)
  199. return 0;
  200. *outl = len;
  201. return 1;
  202. }
  203. static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  204. {
  205. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  206. const OSSL_PARAM *p;
  207. size_t keylen = 0;
  208. if (params == NULL)
  209. return 1;
  210. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  211. if (p != NULL) {
  212. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  213. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  214. return 0;
  215. }
  216. if (ctx->keylen != keylen) {
  217. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  218. return 0;
  219. }
  220. }
  221. return 1;
  222. }
  223. #define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits) \
  224. static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params; \
  225. static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[]) \
  226. { \
  227. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\
  228. flags, kbits, blkbits, ivbits); \
  229. } \
  230. static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx; \
  231. static void *aes_##kbits##fname##_newctx(void *provctx) \
  232. { \
  233. return aes_##mode##_newctx(kbits, blkbits, ivbits, \
  234. EVP_CIPH_##UCMODE##_MODE, flags); \
  235. } \
  236. const OSSL_DISPATCH ossl_##aes##kbits##fname##_functions[] = { \
  237. { OSSL_FUNC_CIPHER_NEWCTX, \
  238. (void (*)(void))aes_##kbits##fname##_newctx }, \
  239. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \
  240. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \
  241. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher }, \
  242. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final }, \
  243. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx }, \
  244. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  245. (void (*)(void))aes_##kbits##_##fname##_get_params }, \
  246. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  247. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  248. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  249. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  250. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  251. (void (*)(void))aes_wrap_set_ctx_params }, \
  252. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  253. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  254. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  255. (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
  256. { 0, NULL } \
  257. }
  258. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
  259. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
  260. IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
  261. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);
  262. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);
  263. IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);
  264. IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);
  265. IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);
  266. IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);
  267. IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_PAD_IVLEN * 8);
  268. IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_PAD_IVLEN * 8);
  269. IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_PAD_IVLEN * 8);