2
0

siv128.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2018-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. #include <string.h>
  10. #include <stdlib.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/params.h>
  15. #include "internal/endian.h"
  16. #include "crypto/modes.h"
  17. #include "crypto/siv.h"
  18. #ifndef OPENSSL_NO_SIV
  19. __owur static ossl_inline uint32_t rotl8(uint32_t x)
  20. {
  21. return (x << 8) | (x >> 24);
  22. }
  23. __owur static ossl_inline uint32_t rotr8(uint32_t x)
  24. {
  25. return (x >> 8) | (x << 24);
  26. }
  27. __owur static ossl_inline uint64_t byteswap8(uint64_t x)
  28. {
  29. uint32_t high = (uint32_t)(x >> 32);
  30. uint32_t low = (uint32_t)x;
  31. high = (rotl8(high) & 0x00ff00ff) | (rotr8(high) & 0xff00ff00);
  32. low = (rotl8(low) & 0x00ff00ff) | (rotr8(low) & 0xff00ff00);
  33. return ((uint64_t)low) << 32 | (uint64_t)high;
  34. }
  35. __owur static ossl_inline uint64_t siv128_getword(SIV_BLOCK const *b, size_t i)
  36. {
  37. DECLARE_IS_ENDIAN;
  38. if (IS_LITTLE_ENDIAN)
  39. return byteswap8(b->word[i]);
  40. return b->word[i];
  41. }
  42. static ossl_inline void siv128_putword(SIV_BLOCK *b, size_t i, uint64_t x)
  43. {
  44. DECLARE_IS_ENDIAN;
  45. if (IS_LITTLE_ENDIAN)
  46. b->word[i] = byteswap8(x);
  47. else
  48. b->word[i] = x;
  49. }
  50. static ossl_inline void siv128_xorblock(SIV_BLOCK *x,
  51. SIV_BLOCK const *y)
  52. {
  53. x->word[0] ^= y->word[0];
  54. x->word[1] ^= y->word[1];
  55. }
  56. /*
  57. * Doubles |b|, which is 16 bytes representing an element
  58. * of GF(2**128) modulo the irreducible polynomial
  59. * x**128 + x**7 + x**2 + x + 1.
  60. * Assumes two's-complement arithmetic
  61. */
  62. static ossl_inline void siv128_dbl(SIV_BLOCK *b)
  63. {
  64. uint64_t high = siv128_getword(b, 0);
  65. uint64_t low = siv128_getword(b, 1);
  66. uint64_t high_carry = high & (((uint64_t)1) << 63);
  67. uint64_t low_carry = low & (((uint64_t)1) << 63);
  68. int64_t low_mask = -((int64_t)(high_carry >> 63)) & 0x87;
  69. uint64_t high_mask = low_carry >> 63;
  70. high = (high << 1) | high_mask;
  71. low = (low << 1) ^ (uint64_t)low_mask;
  72. siv128_putword(b, 0, high);
  73. siv128_putword(b, 1, low);
  74. }
  75. __owur static ossl_inline int siv128_do_s2v_p(SIV128_CONTEXT *ctx, SIV_BLOCK *out,
  76. unsigned char const* in, size_t len)
  77. {
  78. SIV_BLOCK t;
  79. size_t out_len = sizeof(out->byte);
  80. EVP_MAC_CTX *mac_ctx;
  81. int ret = 0;
  82. mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init);
  83. if (mac_ctx == NULL)
  84. return 0;
  85. if (len >= SIV_LEN) {
  86. if (!EVP_MAC_update(mac_ctx, in, len - SIV_LEN))
  87. goto err;
  88. memcpy(&t, in + (len-SIV_LEN), SIV_LEN);
  89. siv128_xorblock(&t, &ctx->d);
  90. if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
  91. goto err;
  92. } else {
  93. memset(&t, 0, sizeof(t));
  94. memcpy(&t, in, len);
  95. t.byte[len] = 0x80;
  96. siv128_dbl(&ctx->d);
  97. siv128_xorblock(&t, &ctx->d);
  98. if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
  99. goto err;
  100. }
  101. if (!EVP_MAC_final(mac_ctx, out->byte, &out_len, sizeof(out->byte))
  102. || out_len != SIV_LEN)
  103. goto err;
  104. ret = 1;
  105. err:
  106. EVP_MAC_CTX_free(mac_ctx);
  107. return ret;
  108. }
  109. __owur static ossl_inline int siv128_do_encrypt(EVP_CIPHER_CTX *ctx, unsigned char *out,
  110. unsigned char const *in, size_t len,
  111. SIV_BLOCK *icv)
  112. {
  113. int out_len = (int)len;
  114. if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, icv->byte, 1))
  115. return 0;
  116. return EVP_EncryptUpdate(ctx, out, &out_len, in, out_len);
  117. }
  118. /*
  119. * Create a new SIV128_CONTEXT
  120. */
  121. SIV128_CONTEXT *ossl_siv128_new(const unsigned char *key, int klen,
  122. EVP_CIPHER *cbc, EVP_CIPHER *ctr,
  123. OSSL_LIB_CTX *libctx, const char *propq)
  124. {
  125. SIV128_CONTEXT *ctx;
  126. int ret;
  127. if ((ctx = OPENSSL_malloc(sizeof(*ctx))) != NULL) {
  128. ret = ossl_siv128_init(ctx, key, klen, cbc, ctr, libctx, propq);
  129. if (ret)
  130. return ctx;
  131. OPENSSL_free(ctx);
  132. }
  133. return NULL;
  134. }
  135. /*
  136. * Initialise an existing SIV128_CONTEXT
  137. */
  138. int ossl_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
  139. const EVP_CIPHER *cbc, const EVP_CIPHER *ctr,
  140. OSSL_LIB_CTX *libctx, const char *propq)
  141. {
  142. static const unsigned char zero[SIV_LEN] = { 0 };
  143. size_t out_len = SIV_LEN;
  144. EVP_MAC_CTX *mac_ctx = NULL;
  145. OSSL_PARAM params[3];
  146. const char *cbc_name;
  147. if (ctx == NULL)
  148. return 0;
  149. memset(&ctx->d, 0, sizeof(ctx->d));
  150. EVP_CIPHER_CTX_free(ctx->cipher_ctx);
  151. EVP_MAC_CTX_free(ctx->mac_ctx_init);
  152. EVP_MAC_free(ctx->mac);
  153. ctx->mac = NULL;
  154. ctx->cipher_ctx = NULL;
  155. ctx->mac_ctx_init = NULL;
  156. if (key == NULL || cbc == NULL || ctr == NULL)
  157. return 0;
  158. cbc_name = EVP_CIPHER_get0_name(cbc);
  159. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
  160. (char *)cbc_name, 0);
  161. params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  162. (void *)key, klen);
  163. params[2] = OSSL_PARAM_construct_end();
  164. if ((ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
  165. || (ctx->mac =
  166. EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, propq)) == NULL
  167. || (ctx->mac_ctx_init = EVP_MAC_CTX_new(ctx->mac)) == NULL
  168. || !EVP_MAC_CTX_set_params(ctx->mac_ctx_init, params)
  169. || !EVP_EncryptInit_ex(ctx->cipher_ctx, ctr, NULL, key + klen, NULL)
  170. || (mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
  171. || !EVP_MAC_update(mac_ctx, zero, sizeof(zero))
  172. || !EVP_MAC_final(mac_ctx, ctx->d.byte, &out_len,
  173. sizeof(ctx->d.byte))) {
  174. EVP_CIPHER_CTX_free(ctx->cipher_ctx);
  175. EVP_MAC_CTX_free(ctx->mac_ctx_init);
  176. EVP_MAC_CTX_free(mac_ctx);
  177. EVP_MAC_free(ctx->mac);
  178. return 0;
  179. }
  180. EVP_MAC_CTX_free(mac_ctx);
  181. ctx->final_ret = -1;
  182. ctx->crypto_ok = 1;
  183. return 1;
  184. }
  185. /*
  186. * Copy an SIV128_CONTEXT object
  187. */
  188. int ossl_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src)
  189. {
  190. memcpy(&dest->d, &src->d, sizeof(src->d));
  191. if (dest->cipher_ctx == NULL) {
  192. dest->cipher_ctx = EVP_CIPHER_CTX_new();
  193. if (dest->cipher_ctx == NULL)
  194. return 0;
  195. }
  196. if (!EVP_CIPHER_CTX_copy(dest->cipher_ctx, src->cipher_ctx))
  197. return 0;
  198. EVP_MAC_CTX_free(dest->mac_ctx_init);
  199. dest->mac_ctx_init = EVP_MAC_CTX_dup(src->mac_ctx_init);
  200. if (dest->mac_ctx_init == NULL)
  201. return 0;
  202. dest->mac = src->mac;
  203. if (dest->mac != NULL)
  204. EVP_MAC_up_ref(dest->mac);
  205. return 1;
  206. }
  207. /*
  208. * Provide any AAD. This can be called multiple times.
  209. * Per RFC5297, the last piece of associated data
  210. * is the nonce, but it's not treated special
  211. */
  212. int ossl_siv128_aad(SIV128_CONTEXT *ctx, const unsigned char *aad,
  213. size_t len)
  214. {
  215. SIV_BLOCK mac_out;
  216. size_t out_len = SIV_LEN;
  217. EVP_MAC_CTX *mac_ctx;
  218. siv128_dbl(&ctx->d);
  219. if ((mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
  220. || !EVP_MAC_update(mac_ctx, aad, len)
  221. || !EVP_MAC_final(mac_ctx, mac_out.byte, &out_len,
  222. sizeof(mac_out.byte))
  223. || out_len != SIV_LEN) {
  224. EVP_MAC_CTX_free(mac_ctx);
  225. return 0;
  226. }
  227. EVP_MAC_CTX_free(mac_ctx);
  228. siv128_xorblock(&ctx->d, &mac_out);
  229. return 1;
  230. }
  231. /*
  232. * Provide any data to be encrypted. This can be called once.
  233. */
  234. int ossl_siv128_encrypt(SIV128_CONTEXT *ctx,
  235. const unsigned char *in, unsigned char *out,
  236. size_t len)
  237. {
  238. SIV_BLOCK q;
  239. /* can only do one crypto operation */
  240. if (ctx->crypto_ok == 0)
  241. return 0;
  242. ctx->crypto_ok--;
  243. if (!siv128_do_s2v_p(ctx, &q, in, len))
  244. return 0;
  245. memcpy(ctx->tag.byte, &q, SIV_LEN);
  246. q.byte[8] &= 0x7f;
  247. q.byte[12] &= 0x7f;
  248. if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q))
  249. return 0;
  250. ctx->final_ret = 0;
  251. return len;
  252. }
  253. /*
  254. * Provide any data to be decrypted. This can be called once.
  255. */
  256. int ossl_siv128_decrypt(SIV128_CONTEXT *ctx,
  257. const unsigned char *in, unsigned char *out,
  258. size_t len)
  259. {
  260. unsigned char* p;
  261. SIV_BLOCK t, q;
  262. int i;
  263. /* can only do one crypto operation */
  264. if (ctx->crypto_ok == 0)
  265. return 0;
  266. ctx->crypto_ok--;
  267. memcpy(&q, ctx->tag.byte, SIV_LEN);
  268. q.byte[8] &= 0x7f;
  269. q.byte[12] &= 0x7f;
  270. if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q)
  271. || !siv128_do_s2v_p(ctx, &t, out, len))
  272. return 0;
  273. p = ctx->tag.byte;
  274. for (i = 0; i < SIV_LEN; i++)
  275. t.byte[i] ^= p[i];
  276. if ((t.word[0] | t.word[1]) != 0) {
  277. OPENSSL_cleanse(out, len);
  278. return 0;
  279. }
  280. ctx->final_ret = 0;
  281. return len;
  282. }
  283. /*
  284. * Return the already calculated final result.
  285. */
  286. int ossl_siv128_finish(SIV128_CONTEXT *ctx)
  287. {
  288. return ctx->final_ret;
  289. }
  290. /*
  291. * Set the tag
  292. */
  293. int ossl_siv128_set_tag(SIV128_CONTEXT *ctx, const unsigned char *tag, size_t len)
  294. {
  295. if (len != SIV_LEN)
  296. return 0;
  297. /* Copy the tag from the supplied buffer */
  298. memcpy(ctx->tag.byte, tag, len);
  299. return 1;
  300. }
  301. /*
  302. * Retrieve the calculated tag
  303. */
  304. int ossl_siv128_get_tag(SIV128_CONTEXT *ctx, unsigned char *tag, size_t len)
  305. {
  306. if (len != SIV_LEN)
  307. return 0;
  308. /* Copy the tag into the supplied buffer */
  309. memcpy(tag, ctx->tag.byte, len);
  310. return 1;
  311. }
  312. /*
  313. * Release all resources
  314. */
  315. int ossl_siv128_cleanup(SIV128_CONTEXT *ctx)
  316. {
  317. if (ctx != NULL) {
  318. EVP_CIPHER_CTX_free(ctx->cipher_ctx);
  319. ctx->cipher_ctx = NULL;
  320. EVP_MAC_CTX_free(ctx->mac_ctx_init);
  321. ctx->mac_ctx_init = NULL;
  322. EVP_MAC_free(ctx->mac);
  323. ctx->mac = NULL;
  324. OPENSSL_cleanse(&ctx->d, sizeof(ctx->d));
  325. OPENSSL_cleanse(&ctx->tag, sizeof(ctx->tag));
  326. ctx->final_ret = -1;
  327. ctx->crypto_ok = 1;
  328. }
  329. return 1;
  330. }
  331. int ossl_siv128_speed(SIV128_CONTEXT *ctx, int arg)
  332. {
  333. ctx->crypto_ok = (arg == 1) ? -1 : 1;
  334. return 1;
  335. }
  336. #endif /* OPENSSL_NO_SIV */