siv128.c 9.9 KB

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