ciphercommon.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. * Generic dispatch table functions for ciphers.
  11. */
  12. /* For SSL3_VERSION */
  13. #include <openssl/prov_ssl.h>
  14. #include <openssl/proverr.h>
  15. #include "ciphercommon_local.h"
  16. #include "prov/provider_ctx.h"
  17. #include "prov/providercommon.h"
  18. /*-
  19. * Generic cipher functions for OSSL_PARAM gettables and settables
  20. */
  21. static const OSSL_PARAM cipher_known_gettable_params[] = {
  22. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_MODE, NULL),
  23. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  24. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  25. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, NULL),
  26. OSSL_PARAM_int(OSSL_CIPHER_PARAM_AEAD, NULL),
  27. OSSL_PARAM_int(OSSL_CIPHER_PARAM_CUSTOM_IV, NULL),
  28. OSSL_PARAM_int(OSSL_CIPHER_PARAM_CTS, NULL),
  29. OSSL_PARAM_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK, NULL),
  30. OSSL_PARAM_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY, NULL),
  31. OSSL_PARAM_END
  32. };
  33. const OSSL_PARAM *ossl_cipher_generic_gettable_params(ossl_unused void *provctx)
  34. {
  35. return cipher_known_gettable_params;
  36. }
  37. int ossl_cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
  38. uint64_t flags,
  39. size_t kbits, size_t blkbits, size_t ivbits)
  40. {
  41. OSSL_PARAM *p;
  42. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_MODE);
  43. if (p != NULL && !OSSL_PARAM_set_uint(p, md)) {
  44. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  45. return 0;
  46. }
  47. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD);
  48. if (p != NULL
  49. && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_AEAD) != 0)) {
  50. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  51. return 0;
  52. }
  53. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CUSTOM_IV);
  54. if (p != NULL
  55. && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_CUSTOM_IV) != 0)) {
  56. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  57. return 0;
  58. }
  59. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CTS);
  60. if (p != NULL
  61. && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_CTS) != 0)) {
  62. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  63. return 0;
  64. }
  65. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK);
  66. if (p != NULL
  67. && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_TLS1_MULTIBLOCK) != 0)) {
  68. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  69. return 0;
  70. }
  71. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_HAS_RAND_KEY);
  72. if (p != NULL
  73. && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_RAND_KEY) != 0)) {
  74. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  75. return 0;
  76. }
  77. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  78. if (p != NULL && !OSSL_PARAM_set_size_t(p, kbits / 8)) {
  79. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  80. return 0;
  81. }
  82. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_BLOCK_SIZE);
  83. if (p != NULL && !OSSL_PARAM_set_size_t(p, blkbits / 8)) {
  84. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  85. return 0;
  86. }
  87. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  88. if (p != NULL && !OSSL_PARAM_set_size_t(p, ivbits / 8)) {
  89. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  90. return 0;
  91. }
  92. return 1;
  93. }
  94. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_cipher_generic)
  95. { OSSL_CIPHER_PARAM_TLS_MAC, OSSL_PARAM_OCTET_PTR, NULL, 0, OSSL_PARAM_UNMODIFIED },
  96. CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_cipher_generic)
  97. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(ossl_cipher_generic)
  98. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_USE_BITS, NULL),
  99. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_TLS_VERSION, NULL),
  100. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE, NULL),
  101. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(ossl_cipher_generic)
  102. /*
  103. * Variable key length cipher functions for OSSL_PARAM settables
  104. */
  105. int ossl_cipher_var_keylen_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  106. {
  107. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  108. const OSSL_PARAM *p;
  109. if (params == NULL)
  110. return 1;
  111. if (!ossl_cipher_generic_set_ctx_params(vctx, params))
  112. return 0;
  113. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  114. if (p != NULL) {
  115. size_t keylen;
  116. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  117. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  118. return 0;
  119. }
  120. ctx->keylen = keylen;
  121. }
  122. return 1;
  123. }
  124. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(ossl_cipher_var_keylen)
  125. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  126. CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(ossl_cipher_var_keylen)
  127. /*-
  128. * AEAD cipher functions for OSSL_PARAM gettables and settables
  129. */
  130. static const OSSL_PARAM cipher_aead_known_gettable_ctx_params[] = {
  131. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  132. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  133. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
  134. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
  135. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
  136. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
  137. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
  138. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, NULL, 0),
  139. OSSL_PARAM_END
  140. };
  141. const OSSL_PARAM *ossl_cipher_aead_gettable_ctx_params(
  142. ossl_unused void *cctx, ossl_unused void *provctx
  143. )
  144. {
  145. return cipher_aead_known_gettable_ctx_params;
  146. }
  147. static const OSSL_PARAM cipher_aead_known_settable_ctx_params[] = {
  148. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL),
  149. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
  150. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0),
  151. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0),
  152. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, NULL, 0),
  153. OSSL_PARAM_END
  154. };
  155. const OSSL_PARAM *ossl_cipher_aead_settable_ctx_params(
  156. ossl_unused void *cctx, ossl_unused void *provctx
  157. )
  158. {
  159. return cipher_aead_known_settable_ctx_params;
  160. }
  161. void ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx)
  162. {
  163. if (ctx != NULL && ctx->alloced) {
  164. OPENSSL_free(ctx->tlsmac);
  165. ctx->alloced = 0;
  166. ctx->tlsmac = NULL;
  167. }
  168. }
  169. static int cipher_generic_init_internal(PROV_CIPHER_CTX *ctx,
  170. const unsigned char *key, size_t keylen,
  171. const unsigned char *iv, size_t ivlen,
  172. const OSSL_PARAM params[], int enc)
  173. {
  174. ctx->num = 0;
  175. ctx->bufsz = 0;
  176. ctx->updated = 0;
  177. ctx->enc = enc ? 1 : 0;
  178. if (!ossl_prov_is_running())
  179. return 0;
  180. if (iv != NULL && ctx->mode != EVP_CIPH_ECB_MODE) {
  181. if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
  182. return 0;
  183. }
  184. if (iv == NULL && ctx->iv_set
  185. && (ctx->mode == EVP_CIPH_CBC_MODE
  186. || ctx->mode == EVP_CIPH_CFB_MODE
  187. || ctx->mode == EVP_CIPH_OFB_MODE))
  188. /* reset IV for these modes to keep compatibility with 1.1.1 */
  189. memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
  190. if (key != NULL) {
  191. if (ctx->variable_keylength == 0) {
  192. if (keylen != ctx->keylen) {
  193. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  194. return 0;
  195. }
  196. } else {
  197. ctx->keylen = keylen;
  198. }
  199. if (!ctx->hw->init(ctx, key, ctx->keylen))
  200. return 0;
  201. }
  202. return ossl_cipher_generic_set_ctx_params(ctx, params);
  203. }
  204. int ossl_cipher_generic_einit(void *vctx, const unsigned char *key,
  205. size_t keylen, const unsigned char *iv,
  206. size_t ivlen, const OSSL_PARAM params[])
  207. {
  208. return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
  209. iv, ivlen, params, 1);
  210. }
  211. int ossl_cipher_generic_dinit(void *vctx, const unsigned char *key,
  212. size_t keylen, const unsigned char *iv,
  213. size_t ivlen, const OSSL_PARAM params[])
  214. {
  215. return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
  216. iv, ivlen, params, 0);
  217. }
  218. /* Max padding including padding length byte */
  219. #define MAX_PADDING 256
  220. int ossl_cipher_generic_block_update(void *vctx, unsigned char *out,
  221. size_t *outl, size_t outsize,
  222. const unsigned char *in, size_t inl)
  223. {
  224. size_t outlint = 0;
  225. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  226. size_t blksz = ctx->blocksize;
  227. size_t nextblocks;
  228. if (ctx->tlsversion > 0) {
  229. /*
  230. * Each update call corresponds to a TLS record and is individually
  231. * padded
  232. */
  233. /* Sanity check inputs */
  234. if (in == NULL
  235. || in != out
  236. || outsize < inl
  237. || !ctx->pad) {
  238. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  239. return 0;
  240. }
  241. if (ctx->enc) {
  242. unsigned char padval;
  243. size_t padnum, loop;
  244. /* Add padding */
  245. padnum = blksz - (inl % blksz);
  246. if (outsize < inl + padnum) {
  247. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  248. return 0;
  249. }
  250. if (padnum > MAX_PADDING) {
  251. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  252. return 0;
  253. }
  254. padval = (unsigned char)(padnum - 1);
  255. if (ctx->tlsversion == SSL3_VERSION) {
  256. if (padnum > 1)
  257. memset(out + inl, 0, padnum - 1);
  258. *(out + inl + padnum - 1) = padval;
  259. } else {
  260. /* we need to add 'padnum' padding bytes of value padval */
  261. for (loop = inl; loop < inl + padnum; loop++)
  262. out[loop] = padval;
  263. }
  264. inl += padnum;
  265. }
  266. if ((inl % blksz) != 0) {
  267. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  268. return 0;
  269. }
  270. /* Shouldn't normally fail */
  271. if (!ctx->hw->cipher(ctx, out, in, inl)) {
  272. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  273. return 0;
  274. }
  275. if (ctx->alloced) {
  276. OPENSSL_free(ctx->tlsmac);
  277. ctx->alloced = 0;
  278. ctx->tlsmac = NULL;
  279. }
  280. /* This only fails if padding is publicly invalid */
  281. *outl = inl;
  282. if (!ctx->enc
  283. && !ossl_cipher_tlsunpadblock(ctx->libctx, ctx->tlsversion,
  284. out, outl,
  285. blksz, &ctx->tlsmac, &ctx->alloced,
  286. ctx->tlsmacsize, 0)) {
  287. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  288. return 0;
  289. }
  290. return 1;
  291. }
  292. if (ctx->bufsz != 0)
  293. nextblocks = ossl_cipher_fillblock(ctx->buf, &ctx->bufsz, blksz,
  294. &in, &inl);
  295. else
  296. nextblocks = inl & ~(blksz-1);
  297. /*
  298. * If we're decrypting and we end an update on a block boundary we hold
  299. * the last block back in case this is the last update call and the last
  300. * block is padded.
  301. */
  302. if (ctx->bufsz == blksz && (ctx->enc || inl > 0 || !ctx->pad)) {
  303. if (outsize < blksz) {
  304. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  305. return 0;
  306. }
  307. if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
  308. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  309. return 0;
  310. }
  311. ctx->bufsz = 0;
  312. outlint = blksz;
  313. out += blksz;
  314. }
  315. if (nextblocks > 0) {
  316. if (!ctx->enc && ctx->pad && nextblocks == inl) {
  317. if (!ossl_assert(inl >= blksz)) {
  318. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  319. return 0;
  320. }
  321. nextblocks -= blksz;
  322. }
  323. outlint += nextblocks;
  324. if (outsize < outlint) {
  325. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  326. return 0;
  327. }
  328. }
  329. if (nextblocks > 0) {
  330. if (!ctx->hw->cipher(ctx, out, in, nextblocks)) {
  331. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  332. return 0;
  333. }
  334. in += nextblocks;
  335. inl -= nextblocks;
  336. }
  337. if (inl != 0
  338. && !ossl_cipher_trailingdata(ctx->buf, &ctx->bufsz, blksz, &in, &inl)) {
  339. /* ERR_raise already called */
  340. return 0;
  341. }
  342. *outl = outlint;
  343. return inl == 0;
  344. }
  345. int ossl_cipher_generic_block_final(void *vctx, unsigned char *out,
  346. size_t *outl, size_t outsize)
  347. {
  348. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  349. size_t blksz = ctx->blocksize;
  350. if (!ossl_prov_is_running())
  351. return 0;
  352. if (ctx->tlsversion > 0) {
  353. /* We never finalize TLS, so this is an error */
  354. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  355. return 0;
  356. }
  357. if (ctx->enc) {
  358. if (ctx->pad) {
  359. ossl_cipher_padblock(ctx->buf, &ctx->bufsz, blksz);
  360. } else if (ctx->bufsz == 0) {
  361. *outl = 0;
  362. return 1;
  363. } else if (ctx->bufsz != blksz) {
  364. ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
  365. return 0;
  366. }
  367. if (outsize < blksz) {
  368. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  369. return 0;
  370. }
  371. if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
  372. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  373. return 0;
  374. }
  375. ctx->bufsz = 0;
  376. *outl = blksz;
  377. return 1;
  378. }
  379. /* Decrypting */
  380. if (ctx->bufsz != blksz) {
  381. if (ctx->bufsz == 0 && !ctx->pad) {
  382. *outl = 0;
  383. return 1;
  384. }
  385. ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
  386. return 0;
  387. }
  388. if (!ctx->hw->cipher(ctx, ctx->buf, ctx->buf, blksz)) {
  389. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  390. return 0;
  391. }
  392. if (ctx->pad && !ossl_cipher_unpadblock(ctx->buf, &ctx->bufsz, blksz)) {
  393. /* ERR_raise already called */
  394. return 0;
  395. }
  396. if (outsize < ctx->bufsz) {
  397. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  398. return 0;
  399. }
  400. memcpy(out, ctx->buf, ctx->bufsz);
  401. *outl = ctx->bufsz;
  402. ctx->bufsz = 0;
  403. return 1;
  404. }
  405. int ossl_cipher_generic_stream_update(void *vctx, unsigned char *out,
  406. size_t *outl, size_t outsize,
  407. const unsigned char *in, size_t inl)
  408. {
  409. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  410. if (inl == 0) {
  411. *outl = 0;
  412. return 1;
  413. }
  414. if (outsize < inl) {
  415. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  416. return 0;
  417. }
  418. if (!ctx->hw->cipher(ctx, out, in, inl)) {
  419. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  420. return 0;
  421. }
  422. *outl = inl;
  423. if (!ctx->enc && ctx->tlsversion > 0) {
  424. /*
  425. * Remove any TLS padding. Only used by cipher_aes_cbc_hmac_sha1_hw.c and
  426. * cipher_aes_cbc_hmac_sha256_hw.c
  427. */
  428. if (ctx->removetlspad) {
  429. /*
  430. * We should have already failed in the cipher() call above if this
  431. * isn't true.
  432. */
  433. if (!ossl_assert(*outl >= (size_t)(out[inl - 1] + 1)))
  434. return 0;
  435. /* The actual padding length */
  436. *outl -= out[inl - 1] + 1;
  437. }
  438. /* TLS MAC and explicit IV if relevant. We should have already failed
  439. * in the cipher() call above if *outl is too short.
  440. */
  441. if (!ossl_assert(*outl >= ctx->removetlsfixed))
  442. return 0;
  443. *outl -= ctx->removetlsfixed;
  444. /* Extract the MAC if there is one */
  445. if (ctx->tlsmacsize > 0) {
  446. if (*outl < ctx->tlsmacsize)
  447. return 0;
  448. ctx->tlsmac = out + *outl - ctx->tlsmacsize;
  449. *outl -= ctx->tlsmacsize;
  450. }
  451. }
  452. return 1;
  453. }
  454. int ossl_cipher_generic_stream_final(void *vctx, unsigned char *out,
  455. size_t *outl, size_t outsize)
  456. {
  457. if (!ossl_prov_is_running())
  458. return 0;
  459. *outl = 0;
  460. return 1;
  461. }
  462. int ossl_cipher_generic_cipher(void *vctx, unsigned char *out, size_t *outl,
  463. size_t outsize, const unsigned char *in,
  464. size_t inl)
  465. {
  466. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  467. if (!ossl_prov_is_running())
  468. return 0;
  469. if (outsize < inl) {
  470. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  471. return 0;
  472. }
  473. if (!ctx->hw->cipher(ctx, out, in, inl)) {
  474. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  475. return 0;
  476. }
  477. *outl = inl;
  478. return 1;
  479. }
  480. int ossl_cipher_generic_get_ctx_params(void *vctx, OSSL_PARAM params[])
  481. {
  482. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  483. OSSL_PARAM *p;
  484. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  485. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
  486. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  487. return 0;
  488. }
  489. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
  490. if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->pad)) {
  491. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  492. return 0;
  493. }
  494. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
  495. if (p != NULL
  496. && !OSSL_PARAM_set_octet_ptr(p, &ctx->oiv, ctx->ivlen)
  497. && !OSSL_PARAM_set_octet_string(p, &ctx->oiv, ctx->ivlen)) {
  498. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  499. return 0;
  500. }
  501. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
  502. if (p != NULL
  503. && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)
  504. && !OSSL_PARAM_set_octet_string(p, &ctx->iv, ctx->ivlen)) {
  505. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  506. return 0;
  507. }
  508. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_NUM);
  509. if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->num)) {
  510. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  511. return 0;
  512. }
  513. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  514. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
  515. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  516. return 0;
  517. }
  518. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS_MAC);
  519. if (p != NULL
  520. && !OSSL_PARAM_set_octet_ptr(p, ctx->tlsmac, ctx->tlsmacsize)) {
  521. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  522. return 0;
  523. }
  524. return 1;
  525. }
  526. int ossl_cipher_generic_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  527. {
  528. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  529. const OSSL_PARAM *p;
  530. if (params == NULL)
  531. return 1;
  532. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_PADDING);
  533. if (p != NULL) {
  534. unsigned int pad;
  535. if (!OSSL_PARAM_get_uint(p, &pad)) {
  536. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  537. return 0;
  538. }
  539. ctx->pad = pad ? 1 : 0;
  540. }
  541. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_USE_BITS);
  542. if (p != NULL) {
  543. unsigned int bits;
  544. if (!OSSL_PARAM_get_uint(p, &bits)) {
  545. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  546. return 0;
  547. }
  548. ctx->use_bits = bits ? 1 : 0;
  549. }
  550. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_VERSION);
  551. if (p != NULL) {
  552. if (!OSSL_PARAM_get_uint(p, &ctx->tlsversion)) {
  553. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  554. return 0;
  555. }
  556. }
  557. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_MAC_SIZE);
  558. if (p != NULL) {
  559. if (!OSSL_PARAM_get_size_t(p, &ctx->tlsmacsize)) {
  560. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  561. return 0;
  562. }
  563. }
  564. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_NUM);
  565. if (p != NULL) {
  566. unsigned int num;
  567. if (!OSSL_PARAM_get_uint(p, &num)) {
  568. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  569. return 0;
  570. }
  571. ctx->num = num;
  572. }
  573. return 1;
  574. }
  575. int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
  576. size_t ivlen)
  577. {
  578. if (ivlen != ctx->ivlen
  579. || ivlen > sizeof(ctx->iv)) {
  580. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  581. return 0;
  582. }
  583. ctx->iv_set = 1;
  584. memcpy(ctx->iv, iv, ivlen);
  585. memcpy(ctx->oiv, iv, ivlen);
  586. return 1;
  587. }
  588. void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
  589. size_t ivbits, unsigned int mode,
  590. uint64_t flags, const PROV_CIPHER_HW *hw,
  591. void *provctx)
  592. {
  593. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  594. if ((flags & PROV_CIPHER_FLAG_INVERSE_CIPHER) != 0)
  595. ctx->inverse_cipher = 1;
  596. if ((flags & PROV_CIPHER_FLAG_VARIABLE_LENGTH) != 0)
  597. ctx->variable_keylength = 1;
  598. ctx->pad = 1;
  599. ctx->keylen = ((kbits) / 8);
  600. ctx->ivlen = ((ivbits) / 8);
  601. ctx->hw = hw;
  602. ctx->mode = mode;
  603. ctx->blocksize = blkbits / 8;
  604. if (provctx != NULL)
  605. ctx->libctx = PROV_LIBCTX_OF(provctx); /* used for rand */
  606. }