ciphercommon_ccm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. /* Dispatch functions for ccm mode */
  10. #include "prov/ciphercommon.h"
  11. #include "prov/ciphercommon_ccm.h"
  12. #include "prov/providercommon.h"
  13. #include "prov/providercommonerr.h"
  14. static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
  15. size_t *padlen, const unsigned char *in,
  16. size_t len);
  17. static int ccm_tls_init(PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen)
  18. {
  19. size_t len;
  20. if (!ossl_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN)
  21. return 0;
  22. /* Save the aad for later use. */
  23. memcpy(ctx->buf, aad, alen);
  24. ctx->tls_aad_len = alen;
  25. len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1];
  26. if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
  27. return 0;
  28. /* Correct length for explicit iv. */
  29. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  30. if (!ctx->enc) {
  31. if (len < ctx->m)
  32. return 0;
  33. /* Correct length for tag. */
  34. len -= ctx->m;
  35. }
  36. ctx->buf[alen - 2] = (unsigned char)(len >> 8);
  37. ctx->buf[alen - 1] = (unsigned char)(len & 0xff);
  38. /* Extra padding: tag appended to record. */
  39. return ctx->m;
  40. }
  41. static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed,
  42. size_t flen)
  43. {
  44. if (flen != EVP_CCM_TLS_FIXED_IV_LEN)
  45. return 0;
  46. /* Copy to first part of the iv. */
  47. memcpy(ctx->iv, fixed, flen);
  48. return 1;
  49. }
  50. static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx)
  51. {
  52. return 15 - ctx->l;
  53. }
  54. int ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  55. {
  56. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  57. const OSSL_PARAM *p;
  58. size_t sz;
  59. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  60. if (p != NULL) {
  61. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  62. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  63. return 0;
  64. }
  65. if ((p->data_size & 1) || (p->data_size < 4) || p->data_size > 16) {
  66. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAGLEN);
  67. return 0;
  68. }
  69. if (p->data != NULL) {
  70. if (ctx->enc) {
  71. ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
  72. return 0;
  73. }
  74. memcpy(ctx->buf, p->data, p->data_size);
  75. ctx->tag_set = 1;
  76. }
  77. ctx->m = p->data_size;
  78. }
  79. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
  80. if (p != NULL) {
  81. size_t ivlen;
  82. if (!OSSL_PARAM_get_size_t(p, &sz)) {
  83. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  84. return 0;
  85. }
  86. ivlen = 15 - sz;
  87. if (ivlen < 2 || ivlen > 8) {
  88. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
  89. return 0;
  90. }
  91. ctx->l = ivlen;
  92. }
  93. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
  94. if (p != NULL) {
  95. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  96. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  97. return 0;
  98. }
  99. sz = ccm_tls_init(ctx, p->data, p->data_size);
  100. if (sz == 0) {
  101. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
  102. return 0;
  103. }
  104. ctx->tls_aad_pad_sz = sz;
  105. }
  106. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
  107. if (p != NULL) {
  108. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  109. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  110. return 0;
  111. }
  112. if (ccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
  113. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
  114. return 0;
  115. }
  116. }
  117. return 1;
  118. }
  119. int ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
  120. {
  121. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  122. OSSL_PARAM *p;
  123. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  124. if (p != NULL && !OSSL_PARAM_set_size_t(p, ccm_get_ivlen(ctx))) {
  125. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  126. return 0;
  127. }
  128. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
  129. if (p != NULL) {
  130. size_t m = ctx->m;
  131. if (!OSSL_PARAM_set_size_t(p, m)) {
  132. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  133. return 0;
  134. }
  135. }
  136. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
  137. if (p != NULL) {
  138. if (ccm_get_ivlen(ctx) > p->data_size) {
  139. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
  140. return 0;
  141. }
  142. if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
  143. && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
  144. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  145. return 0;
  146. }
  147. }
  148. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV_STATE);
  149. if (p != NULL) {
  150. if (ccm_get_ivlen(ctx) > p->data_size) {
  151. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
  152. return 0;
  153. }
  154. if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
  155. && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
  156. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  157. return 0;
  158. }
  159. }
  160. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  161. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
  162. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  163. return 0;
  164. }
  165. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
  166. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
  167. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  168. return 0;
  169. }
  170. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  171. if (p != NULL) {
  172. if (!ctx->enc || !ctx->tag_set) {
  173. ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOTSET);
  174. return 0;
  175. }
  176. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  177. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  178. return 0;
  179. }
  180. if (!ctx->hw->gettag(ctx, p->data, p->data_size))
  181. return 0;
  182. ctx->tag_set = 0;
  183. ctx->iv_set = 0;
  184. ctx->len_set = 0;
  185. }
  186. return 1;
  187. }
  188. static int ccm_init(void *vctx, const unsigned char *key, size_t keylen,
  189. const unsigned char *iv, size_t ivlen, int enc)
  190. {
  191. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  192. if (!ossl_prov_is_running())
  193. return 0;
  194. ctx->enc = enc;
  195. if (iv != NULL) {
  196. if (ivlen != ccm_get_ivlen(ctx)) {
  197. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN);
  198. return 0;
  199. }
  200. memcpy(ctx->iv, iv, ivlen);
  201. ctx->iv_set = 1;
  202. }
  203. if (key != NULL) {
  204. if (keylen != ctx->keylen) {
  205. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
  206. return 0;
  207. }
  208. return ctx->hw->setkey(ctx, key, keylen);
  209. }
  210. return 1;
  211. }
  212. int ccm_einit(void *vctx, const unsigned char *key, size_t keylen,
  213. const unsigned char *iv, size_t ivlen)
  214. {
  215. return ccm_init(vctx, key, keylen, iv, ivlen, 1);
  216. }
  217. int ccm_dinit(void *vctx, const unsigned char *key, size_t keylen,
  218. const unsigned char *iv, size_t ivlen)
  219. {
  220. return ccm_init(vctx, key, keylen, iv, ivlen, 0);
  221. }
  222. int ccm_stream_update(void *vctx, unsigned char *out, size_t *outl,
  223. size_t outsize, const unsigned char *in,
  224. size_t inl)
  225. {
  226. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  227. if (outsize < inl) {
  228. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  229. return 0;
  230. }
  231. if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
  232. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  233. return 0;
  234. }
  235. return 1;
  236. }
  237. int ccm_stream_final(void *vctx, unsigned char *out, size_t *outl,
  238. size_t outsize)
  239. {
  240. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  241. int i;
  242. if (!ossl_prov_is_running())
  243. return 0;
  244. i = ccm_cipher_internal(ctx, out, outl, NULL, 0);
  245. if (i <= 0)
  246. return 0;
  247. *outl = 0;
  248. return 1;
  249. }
  250. int ccm_cipher(void *vctx,
  251. unsigned char *out, size_t *outl, size_t outsize,
  252. const unsigned char *in, size_t inl)
  253. {
  254. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  255. if (!ossl_prov_is_running())
  256. return 0;
  257. if (outsize < inl) {
  258. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  259. return 0;
  260. }
  261. if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
  262. return 0;
  263. *outl = inl;
  264. return 1;
  265. }
  266. /* Copy the buffered iv */
  267. static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen)
  268. {
  269. const PROV_CCM_HW *hw = ctx->hw;
  270. if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
  271. return 0;
  272. ctx->len_set = 1;
  273. return 1;
  274. }
  275. static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
  276. unsigned char *out, size_t *padlen,
  277. const unsigned char *in, size_t len)
  278. {
  279. int rv = 0;
  280. size_t olen = 0;
  281. if (!ossl_prov_is_running())
  282. goto err;
  283. /* Encrypt/decrypt must be performed in place */
  284. if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
  285. goto err;
  286. /* If encrypting set explicit IV from sequence number (start of AAD) */
  287. if (ctx->enc)
  288. memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
  289. /* Get rest of IV from explicit IV */
  290. memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
  291. /* Correct length value */
  292. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
  293. if (!ccm_set_iv(ctx, len))
  294. goto err;
  295. /* Use saved AAD */
  296. if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
  297. goto err;
  298. /* Fix buffer to point to payload */
  299. in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  300. out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  301. if (ctx->enc) {
  302. if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m))
  303. goto err;
  304. olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
  305. } else {
  306. if (!ctx->hw->auth_decrypt(ctx, in, out, len,
  307. (unsigned char *)in + len, ctx->m))
  308. goto err;
  309. olen = len;
  310. }
  311. rv = 1;
  312. err:
  313. *padlen = olen;
  314. return rv;
  315. }
  316. static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
  317. size_t *padlen, const unsigned char *in,
  318. size_t len)
  319. {
  320. int rv = 0;
  321. size_t olen = 0;
  322. const PROV_CCM_HW *hw = ctx->hw;
  323. /* If no key set, return error */
  324. if (!ctx->key_set)
  325. return 0;
  326. if (ctx->tls_aad_len != UNINITIALISED_SIZET)
  327. return ccm_tls_cipher(ctx, out, padlen, in, len);
  328. /* EVP_*Final() doesn't return any data */
  329. if (in == NULL && out != NULL)
  330. goto finish;
  331. if (!ctx->iv_set)
  332. goto err;
  333. if (out == NULL) {
  334. if (in == NULL) {
  335. if (!ccm_set_iv(ctx, len))
  336. goto err;
  337. } else {
  338. /* If we have AAD, we need a message length */
  339. if (!ctx->len_set && len)
  340. goto err;
  341. if (!hw->setaad(ctx, in, len))
  342. goto err;
  343. }
  344. } else {
  345. /* If not set length yet do it */
  346. if (!ctx->len_set && !ccm_set_iv(ctx, len))
  347. goto err;
  348. if (ctx->enc) {
  349. if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))
  350. goto err;
  351. ctx->tag_set = 1;
  352. } else {
  353. /* The tag must be set before actually decrypting data */
  354. if (!ctx->tag_set)
  355. goto err;
  356. if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
  357. goto err;
  358. /* Finished - reset flags so calling this method again will fail */
  359. ctx->iv_set = 0;
  360. ctx->tag_set = 0;
  361. ctx->len_set = 0;
  362. }
  363. }
  364. olen = len;
  365. finish:
  366. rv = 1;
  367. err:
  368. *padlen = olen;
  369. return rv;
  370. }
  371. void ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
  372. {
  373. ctx->keylen = keybits / 8;
  374. ctx->key_set = 0;
  375. ctx->iv_set = 0;
  376. ctx->tag_set = 0;
  377. ctx->len_set = 0;
  378. ctx->l = 8;
  379. ctx->m = 12;
  380. ctx->tls_aad_len = UNINITIALISED_SIZET;
  381. ctx->hw = hw;
  382. }