siphash_pmeth.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2007-2017 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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/evp.h>
  14. #include "internal/siphash.h"
  15. #include "siphash_local.h"
  16. #include "internal/evp_int.h"
  17. /* SIPHASH pkey context structure */
  18. typedef struct siphash_pkey_ctx_st {
  19. ASN1_OCTET_STRING ktmp; /* Temp storage for key */
  20. SIPHASH ctx;
  21. } SIPHASH_PKEY_CTX;
  22. static int pkey_siphash_init(EVP_PKEY_CTX *ctx)
  23. {
  24. SIPHASH_PKEY_CTX *pctx;
  25. pctx = OPENSSL_zalloc(sizeof(*pctx));
  26. if (pctx == NULL)
  27. return 0;
  28. pctx->ktmp.type = V_ASN1_OCTET_STRING;
  29. EVP_PKEY_CTX_set_data(ctx, pctx);
  30. EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
  31. return 1;
  32. }
  33. static void pkey_siphash_cleanup(EVP_PKEY_CTX *ctx)
  34. {
  35. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  36. if (pctx != NULL) {
  37. OPENSSL_clear_free(pctx->ktmp.data, pctx->ktmp.length);
  38. OPENSSL_clear_free(pctx, sizeof(*pctx));
  39. EVP_PKEY_CTX_set_data(ctx, NULL);
  40. }
  41. }
  42. static int pkey_siphash_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  43. {
  44. SIPHASH_PKEY_CTX *sctx, *dctx;
  45. /* allocate memory for dst->data and a new SIPHASH_CTX in dst->data->ctx */
  46. if (!pkey_siphash_init(dst))
  47. return 0;
  48. sctx = EVP_PKEY_CTX_get_data(src);
  49. dctx = EVP_PKEY_CTX_get_data(dst);
  50. if (ASN1_STRING_get0_data(&sctx->ktmp) != NULL &&
  51. !ASN1_STRING_copy(&dctx->ktmp, &sctx->ktmp)) {
  52. /* cleanup and free the SIPHASH_PKEY_CTX in dst->data */
  53. pkey_siphash_cleanup(dst);
  54. return 0;
  55. }
  56. memcpy(&dctx->ctx, &sctx->ctx, sizeof(SIPHASH));
  57. return 1;
  58. }
  59. static int pkey_siphash_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  60. {
  61. ASN1_OCTET_STRING *key;
  62. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  63. if (ASN1_STRING_get0_data(&pctx->ktmp) == NULL)
  64. return 0;
  65. key = ASN1_OCTET_STRING_dup(&pctx->ktmp);
  66. if (key == NULL)
  67. return 0;
  68. return EVP_PKEY_assign_SIPHASH(pkey, key);
  69. }
  70. static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  71. {
  72. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
  73. SipHash_Update(&pctx->ctx, data, count);
  74. return 1;
  75. }
  76. static int siphash_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  77. {
  78. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  79. const unsigned char* key;
  80. size_t len;
  81. int hash_size;
  82. key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
  83. if (key == NULL || len != SIPHASH_KEY_SIZE)
  84. return 0;
  85. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  86. EVP_MD_CTX_set_update_fn(mctx, int_update);
  87. /* use default rounds (2,4) */
  88. hash_size = SipHash_hash_size(&pctx->ctx);
  89. return SipHash_Init(&pctx->ctx, key, hash_size, 0, 0);
  90. }
  91. static int siphash_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  92. EVP_MD_CTX *mctx)
  93. {
  94. SIPHASH_PKEY_CTX *pctx = ctx->data;
  95. *siglen = SipHash_hash_size(&pctx->ctx);
  96. if (sig != NULL)
  97. return SipHash_Final(&pctx->ctx, sig, *siglen);
  98. return 1;
  99. }
  100. static int pkey_siphash_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  101. {
  102. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  103. const unsigned char *key;
  104. size_t len;
  105. int hash_size;
  106. switch (type) {
  107. case EVP_PKEY_CTRL_MD:
  108. /* ignore */
  109. break;
  110. case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
  111. if (p1 != SIPHASH_MIN_DIGEST_SIZE &&
  112. p1 != SIPHASH_MAX_DIGEST_SIZE) {
  113. return 0;
  114. }
  115. /* use default rounds (2,4) */
  116. return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp), p1, 0, 0);
  117. case EVP_PKEY_CTRL_SET_MAC_KEY:
  118. case EVP_PKEY_CTRL_DIGESTINIT:
  119. if (type == EVP_PKEY_CTRL_SET_MAC_KEY) {
  120. /* user explicitly setting the key */
  121. key = p2;
  122. len = p1;
  123. } else {
  124. /* user indirectly setting the key via EVP_DigestSignInit */
  125. key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
  126. }
  127. if (key == NULL || len != SIPHASH_KEY_SIZE ||
  128. !ASN1_OCTET_STRING_set(&pctx->ktmp, key, len))
  129. return 0;
  130. /* use default rounds (2,4) */
  131. hash_size = SipHash_hash_size(&pctx->ctx);
  132. return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp), hash_size, 0, 0);
  133. default:
  134. return -2;
  135. }
  136. return 1;
  137. }
  138. static int pkey_siphash_ctrl_str(EVP_PKEY_CTX *ctx,
  139. const char *type, const char *value)
  140. {
  141. if (value == NULL)
  142. return 0;
  143. if (strcmp(type, "key") == 0)
  144. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  145. if (strcmp(type, "hexkey") == 0)
  146. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  147. return -2;
  148. }
  149. const EVP_PKEY_METHOD siphash_pkey_meth = {
  150. EVP_PKEY_SIPHASH,
  151. EVP_PKEY_FLAG_SIGCTX_CUSTOM, /* we don't deal with a separate MD */
  152. pkey_siphash_init,
  153. pkey_siphash_copy,
  154. pkey_siphash_cleanup,
  155. 0, 0,
  156. 0,
  157. pkey_siphash_keygen,
  158. 0, 0,
  159. 0, 0,
  160. 0, 0,
  161. siphash_signctx_init,
  162. siphash_signctx,
  163. 0, 0,
  164. 0, 0,
  165. 0, 0,
  166. 0, 0,
  167. pkey_siphash_ctrl,
  168. pkey_siphash_ctrl_str
  169. };