2
0

siphash_pmeth.c 5.6 KB

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