siv128.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright 2018-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. #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_dup_ctx(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_free_ctx(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 *CRYPTO_siv128_new(const unsigned char *key, int klen, EVP_CIPHER* cbc, EVP_CIPHER* ctr)
  122. {
  123. SIV128_CONTEXT *ctx;
  124. int ret;
  125. if ((ctx = OPENSSL_malloc(sizeof(*ctx))) != NULL) {
  126. ret = CRYPTO_siv128_init(ctx, key, klen, cbc, ctr);
  127. if (ret)
  128. return ctx;
  129. OPENSSL_free(ctx);
  130. }
  131. return NULL;
  132. }
  133. /*
  134. * Initialise an existing SIV128_CONTEXT
  135. */
  136. int CRYPTO_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
  137. const EVP_CIPHER* cbc, const EVP_CIPHER* ctr)
  138. {
  139. static const unsigned char zero[SIV_LEN] = { 0 };
  140. size_t out_len = SIV_LEN;
  141. EVP_MAC_CTX *mac_ctx = NULL;
  142. OSSL_PARAM params[3];
  143. const char *cbc_name = EVP_CIPHER_name(cbc);
  144. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
  145. (char *)cbc_name, 0);
  146. params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  147. (void *)key, klen);
  148. params[2] = OSSL_PARAM_construct_end();
  149. memset(&ctx->d, 0, sizeof(ctx->d));
  150. ctx->cipher_ctx = NULL;
  151. ctx->mac_ctx_init = NULL;
  152. if (key == NULL || cbc == NULL || ctr == NULL
  153. || (ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
  154. /* TODO(3.0) library context */
  155. || (ctx->mac =
  156. EVP_MAC_fetch(NULL, OSSL_MAC_NAME_CMAC, NULL)) == NULL
  157. || (ctx->mac_ctx_init = EVP_MAC_new_ctx(ctx->mac)) == NULL
  158. || !EVP_MAC_set_ctx_params(ctx->mac_ctx_init, params)
  159. || !EVP_EncryptInit_ex(ctx->cipher_ctx, ctr, NULL, key + klen, NULL)
  160. || (mac_ctx = EVP_MAC_dup_ctx(ctx->mac_ctx_init)) == NULL
  161. || !EVP_MAC_update(mac_ctx, zero, sizeof(zero))
  162. || !EVP_MAC_final(mac_ctx, ctx->d.byte, &out_len,
  163. sizeof(ctx->d.byte))) {
  164. EVP_CIPHER_CTX_free(ctx->cipher_ctx);
  165. EVP_MAC_free_ctx(ctx->mac_ctx_init);
  166. EVP_MAC_free_ctx(mac_ctx);
  167. EVP_MAC_free(ctx->mac);
  168. return 0;
  169. }
  170. EVP_MAC_free_ctx(mac_ctx);
  171. ctx->final_ret = -1;
  172. ctx->crypto_ok = 1;
  173. return 1;
  174. }
  175. /*
  176. * Copy an SIV128_CONTEXT object
  177. */
  178. int CRYPTO_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src)
  179. {
  180. memcpy(&dest->d, &src->d, sizeof(src->d));
  181. if (!EVP_CIPHER_CTX_copy(dest->cipher_ctx, src->cipher_ctx))
  182. return 0;
  183. EVP_MAC_free_ctx(dest->mac_ctx_init);
  184. dest->mac_ctx_init = EVP_MAC_dup_ctx(src->mac_ctx_init);
  185. if (dest->mac_ctx_init == NULL)
  186. return 0;
  187. return 1;
  188. }
  189. /*
  190. * Provide any AAD. This can be called multiple times.
  191. * Per RFC5297, the last piece of associated data
  192. * is the nonce, but it's not treated special
  193. */
  194. int CRYPTO_siv128_aad(SIV128_CONTEXT *ctx, const unsigned char *aad,
  195. size_t len)
  196. {
  197. SIV_BLOCK mac_out;
  198. size_t out_len = SIV_LEN;
  199. EVP_MAC_CTX *mac_ctx;
  200. siv128_dbl(&ctx->d);
  201. if ((mac_ctx = EVP_MAC_dup_ctx(ctx->mac_ctx_init)) == NULL
  202. || !EVP_MAC_update(mac_ctx, aad, len)
  203. || !EVP_MAC_final(mac_ctx, mac_out.byte, &out_len,
  204. sizeof(mac_out.byte))
  205. || out_len != SIV_LEN) {
  206. EVP_MAC_free_ctx(mac_ctx);
  207. return 0;
  208. }
  209. EVP_MAC_free_ctx(mac_ctx);
  210. siv128_xorblock(&ctx->d, &mac_out);
  211. return 1;
  212. }
  213. /*
  214. * Provide any data to be encrypted. This can be called once.
  215. */
  216. int CRYPTO_siv128_encrypt(SIV128_CONTEXT *ctx,
  217. const unsigned char *in, unsigned char *out,
  218. size_t len)
  219. {
  220. SIV_BLOCK q;
  221. /* can only do one crypto operation */
  222. if (ctx->crypto_ok == 0)
  223. return 0;
  224. ctx->crypto_ok--;
  225. if (!siv128_do_s2v_p(ctx, &q, in, len))
  226. return 0;
  227. memcpy(ctx->tag.byte, &q, SIV_LEN);
  228. q.byte[8] &= 0x7f;
  229. q.byte[12] &= 0x7f;
  230. if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q))
  231. return 0;
  232. ctx->final_ret = 0;
  233. return len;
  234. }
  235. /*
  236. * Provide any data to be decrypted. This can be called once.
  237. */
  238. int CRYPTO_siv128_decrypt(SIV128_CONTEXT *ctx,
  239. const unsigned char *in, unsigned char *out,
  240. size_t len)
  241. {
  242. unsigned char* p;
  243. SIV_BLOCK t, q;
  244. int i;
  245. /* can only do one crypto operation */
  246. if (ctx->crypto_ok == 0)
  247. return 0;
  248. ctx->crypto_ok--;
  249. memcpy(&q, ctx->tag.byte, SIV_LEN);
  250. q.byte[8] &= 0x7f;
  251. q.byte[12] &= 0x7f;
  252. if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q)
  253. || !siv128_do_s2v_p(ctx, &t, out, len))
  254. return 0;
  255. p = ctx->tag.byte;
  256. for (i = 0; i < SIV_LEN; i++)
  257. t.byte[i] ^= p[i];
  258. if ((t.word[0] | t.word[1]) != 0) {
  259. OPENSSL_cleanse(out, len);
  260. return 0;
  261. }
  262. ctx->final_ret = 0;
  263. return len;
  264. }
  265. /*
  266. * Return the already calculated final result.
  267. */
  268. int CRYPTO_siv128_finish(SIV128_CONTEXT *ctx)
  269. {
  270. return ctx->final_ret;
  271. }
  272. /*
  273. * Set the tag
  274. */
  275. int CRYPTO_siv128_set_tag(SIV128_CONTEXT *ctx, const unsigned char *tag, size_t len)
  276. {
  277. if (len != SIV_LEN)
  278. return 0;
  279. /* Copy the tag from the supplied buffer */
  280. memcpy(ctx->tag.byte, tag, len);
  281. return 1;
  282. }
  283. /*
  284. * Retrieve the calculated tag
  285. */
  286. int CRYPTO_siv128_get_tag(SIV128_CONTEXT *ctx, unsigned char *tag, size_t len)
  287. {
  288. if (len != SIV_LEN)
  289. return 0;
  290. /* Copy the tag into the supplied buffer */
  291. memcpy(tag, ctx->tag.byte, len);
  292. return 1;
  293. }
  294. /*
  295. * Release all resources
  296. */
  297. int CRYPTO_siv128_cleanup(SIV128_CONTEXT *ctx)
  298. {
  299. if (ctx != NULL) {
  300. EVP_CIPHER_CTX_free(ctx->cipher_ctx);
  301. ctx->cipher_ctx = NULL;
  302. EVP_MAC_free_ctx(ctx->mac_ctx_init);
  303. ctx->mac_ctx_init = NULL;
  304. EVP_MAC_free(ctx->mac);
  305. ctx->mac = NULL;
  306. OPENSSL_cleanse(&ctx->d, sizeof(ctx->d));
  307. OPENSSL_cleanse(&ctx->tag, sizeof(ctx->tag));
  308. ctx->final_ret = -1;
  309. ctx->crypto_ok = 1;
  310. }
  311. return 1;
  312. }
  313. int CRYPTO_siv128_speed(SIV128_CONTEXT *ctx, int arg)
  314. {
  315. ctx->crypto_ok = (arg == 1) ? -1 : 1;
  316. return 1;
  317. }
  318. #endif /* OPENSSL_NO_SIV */