siv128.c 9.1 KB

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