ffc_internal_test.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019-2020, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "internal/nelem.h"
  14. #include <openssl/crypto.h>
  15. #include <openssl/bio.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/rand.h>
  18. #include <openssl/err.h>
  19. #include "testutil.h"
  20. #include "internal/ffc.h"
  21. #ifndef OPENSSL_NO_DSA
  22. static const unsigned char dsa_2048_224_sha224_p[] = {
  23. 0x93, 0x57, 0x93, 0x62, 0x1b, 0x9a, 0x10, 0x9b, 0xc1, 0x56, 0x0f, 0x24,
  24. 0x71, 0x76, 0x4e, 0xd3, 0xed, 0x78, 0x78, 0x7a, 0xbf, 0x89, 0x71, 0x67,
  25. 0x8e, 0x03, 0xd8, 0x5b, 0xcd, 0x22, 0x8f, 0x70, 0x74, 0xff, 0x22, 0x05,
  26. 0x07, 0x0c, 0x4c, 0x60, 0xed, 0x41, 0xe1, 0x9e, 0x9c, 0xaa, 0x3e, 0x19,
  27. 0x5c, 0x3d, 0x80, 0x58, 0xb2, 0x7f, 0x5f, 0x89, 0xec, 0xb5, 0x19, 0xdb,
  28. 0x06, 0x11, 0xe9, 0x78, 0x5c, 0xf9, 0xa0, 0x9e, 0x70, 0x62, 0x14, 0x7b,
  29. 0xda, 0x92, 0xbf, 0xb2, 0x6b, 0x01, 0x6f, 0xb8, 0x68, 0x9c, 0x89, 0x36,
  30. 0x89, 0x72, 0x79, 0x49, 0x93, 0x3d, 0x14, 0xb2, 0x2d, 0xbb, 0xf0, 0xdf,
  31. 0x94, 0x45, 0x0b, 0x5f, 0xf1, 0x75, 0x37, 0xeb, 0x49, 0xb9, 0x2d, 0xce,
  32. 0xb7, 0xf4, 0x95, 0x77, 0xc2, 0xe9, 0x39, 0x1c, 0x4e, 0x0c, 0x40, 0x62,
  33. 0x33, 0x0a, 0xe6, 0x29, 0x6f, 0xba, 0xef, 0x02, 0xdd, 0x0d, 0xe4, 0x04,
  34. 0x01, 0x70, 0x40, 0xb9, 0xc9, 0x7e, 0x2f, 0x10, 0x37, 0xe9, 0xde, 0xb0,
  35. 0xf6, 0xeb, 0x71, 0x7f, 0x9c, 0x35, 0x16, 0xf3, 0x0d, 0xc4, 0xe8, 0x02,
  36. 0x37, 0x6c, 0xdd, 0xb3, 0x8d, 0x2d, 0x1e, 0x28, 0x13, 0x22, 0x89, 0x40,
  37. 0xe5, 0xfa, 0x16, 0x67, 0xd6, 0xda, 0x12, 0xa2, 0x38, 0x83, 0x25, 0xcc,
  38. 0x26, 0xc1, 0x27, 0x74, 0xfe, 0xf6, 0x7a, 0xb6, 0xa1, 0xe4, 0xe8, 0xdf,
  39. 0x5d, 0xd2, 0x9c, 0x2f, 0xec, 0xea, 0x08, 0xca, 0x48, 0xdb, 0x18, 0x4b,
  40. 0x12, 0xee, 0x16, 0x9b, 0xa6, 0x00, 0xa0, 0x18, 0x98, 0x7d, 0xce, 0x6c,
  41. 0x6d, 0xf8, 0xfc, 0x95, 0x51, 0x1b, 0x0a, 0x40, 0xb6, 0xfc, 0xe5, 0xe2,
  42. 0xb0, 0x26, 0x53, 0x4c, 0xd7, 0xfe, 0xaa, 0x6d, 0xbc, 0xdd, 0xc0, 0x61,
  43. 0x65, 0xe4, 0x89, 0x44, 0x18, 0x6f, 0xd5, 0x39, 0xcf, 0x75, 0x6d, 0x29,
  44. 0xcc, 0xf8, 0x40, 0xab
  45. };
  46. static const unsigned char dsa_2048_224_sha224_q[] = {
  47. 0xf2, 0x5e, 0x4e, 0x9a, 0x15, 0xa8, 0x13, 0xdf, 0xa3, 0x17, 0x90, 0xc6,
  48. 0xd6, 0x5e, 0xb1, 0xfb, 0x31, 0xf8, 0xb5, 0xb1, 0x4b, 0xa7, 0x6d, 0xde,
  49. 0x57, 0x76, 0x6f, 0x11
  50. };
  51. static const unsigned char dsa_2048_224_sha224_seed[] = {
  52. 0xd2, 0xb1, 0x36, 0xd8, 0x5b, 0x8e, 0xa4, 0xb2, 0x6a, 0xab, 0x4e, 0x85,
  53. 0x8b, 0x49, 0xf9, 0xdd, 0xe6, 0xa1, 0xcd, 0xad, 0x49, 0x52, 0xe9, 0xb3,
  54. 0x36, 0x17, 0x06, 0xcf
  55. };
  56. static const unsigned char dsa_2048_224_sha224_bad_seed[] = {
  57. 0xd2, 0xb1, 0x36, 0xd8, 0x5b, 0x8e, 0xa4, 0xb2, 0x6a, 0xab, 0x4e, 0x85,
  58. 0x8b, 0x49, 0xf9, 0xdd, 0xe6, 0xa1, 0xcd, 0xad, 0x49, 0x52, 0xe9, 0xb3,
  59. 0x36, 0x17, 0x06, 0xd0
  60. };
  61. static int dsa_2048_224_sha224_counter = 2878;
  62. static const unsigned char dsa_3072_256_sha512_p[] = {
  63. 0x9a, 0x82, 0x8b, 0x8d, 0xea, 0xd0, 0x56, 0x23, 0x88, 0x2d, 0x5d, 0x41,
  64. 0x42, 0x4c, 0x13, 0x5a, 0x15, 0x81, 0x59, 0x02, 0xc5, 0x00, 0x82, 0x28,
  65. 0x01, 0xee, 0x8f, 0x99, 0xfd, 0x6a, 0x95, 0xf2, 0x0f, 0xae, 0x34, 0x77,
  66. 0x29, 0xcc, 0xc7, 0x50, 0x0e, 0x03, 0xef, 0xb0, 0x4d, 0xe5, 0x10, 0x00,
  67. 0xa8, 0x7b, 0xce, 0x8c, 0xc6, 0xb2, 0x01, 0x74, 0x23, 0x1b, 0x7f, 0xe8,
  68. 0xf9, 0x71, 0x28, 0x39, 0xcf, 0x18, 0x04, 0xb2, 0x95, 0x61, 0x2d, 0x11,
  69. 0x71, 0x6b, 0xdd, 0x0d, 0x0b, 0xf0, 0xe6, 0x97, 0x52, 0x29, 0x9d, 0x45,
  70. 0xb1, 0x23, 0xda, 0xb0, 0xd5, 0xcb, 0x51, 0x71, 0x8e, 0x40, 0x9c, 0x97,
  71. 0x13, 0xea, 0x1f, 0x4b, 0x32, 0x5d, 0x27, 0x74, 0x81, 0x8d, 0x47, 0x8a,
  72. 0x08, 0xce, 0xf4, 0xd1, 0x28, 0xa2, 0x0f, 0x9b, 0x2e, 0xc9, 0xa3, 0x0e,
  73. 0x5d, 0xde, 0x47, 0x19, 0x6d, 0x5f, 0x98, 0xe0, 0x8e, 0x7f, 0x60, 0x8f,
  74. 0x25, 0xa7, 0xa4, 0xeb, 0xb9, 0xf3, 0x24, 0xa4, 0x9e, 0xc1, 0xbd, 0x14,
  75. 0x27, 0x7c, 0x27, 0xc8, 0x4f, 0x5f, 0xed, 0xfd, 0x86, 0xc8, 0xf1, 0xd7,
  76. 0x82, 0xe2, 0xeb, 0xe5, 0xd2, 0xbe, 0xb0, 0x65, 0x28, 0xab, 0x99, 0x9e,
  77. 0xcd, 0xd5, 0x22, 0xf8, 0x1b, 0x3b, 0x01, 0xe9, 0x20, 0x3d, 0xe4, 0x98,
  78. 0x22, 0xfe, 0xfc, 0x09, 0x7e, 0x95, 0x20, 0xda, 0xb6, 0x12, 0x2c, 0x94,
  79. 0x5c, 0xea, 0x74, 0x71, 0xbd, 0x19, 0xac, 0x78, 0x43, 0x02, 0x51, 0xb8,
  80. 0x5f, 0x06, 0x1d, 0xea, 0xc8, 0xa4, 0x3b, 0xc9, 0x78, 0xa3, 0x2b, 0x09,
  81. 0xdc, 0x76, 0x74, 0xc4, 0x23, 0x14, 0x48, 0x2e, 0x84, 0x2b, 0xa3, 0x82,
  82. 0xc1, 0xba, 0x0b, 0x39, 0x2a, 0x9f, 0x24, 0x7b, 0xd6, 0xc2, 0xea, 0x5a,
  83. 0xb6, 0xbd, 0x15, 0x82, 0x21, 0x85, 0xe0, 0x6b, 0x12, 0x4f, 0x8d, 0x64,
  84. 0x75, 0xeb, 0x7e, 0xa1, 0xdb, 0xe0, 0x9d, 0x25, 0xae, 0x3b, 0xe9, 0x9b,
  85. 0x21, 0x7f, 0x9a, 0x3d, 0x66, 0xd0, 0x52, 0x1d, 0x39, 0x8b, 0xeb, 0xfc,
  86. 0xec, 0xbe, 0x72, 0x20, 0x5a, 0xdf, 0x1b, 0x00, 0xf1, 0x0e, 0xed, 0xc6,
  87. 0x78, 0x6f, 0xc9, 0xab, 0xe4, 0xd6, 0x81, 0x8b, 0xcc, 0xf6, 0xd4, 0x6a,
  88. 0x31, 0x62, 0x08, 0xd9, 0x38, 0x21, 0x8f, 0xda, 0x9e, 0xb1, 0x2b, 0x9c,
  89. 0xc0, 0xbe, 0xf7, 0x9a, 0x43, 0x2d, 0x07, 0x59, 0x46, 0x0e, 0xd5, 0x23,
  90. 0x4e, 0xaa, 0x4a, 0x04, 0xc2, 0xde, 0x33, 0xa6, 0x34, 0xba, 0xac, 0x4f,
  91. 0x78, 0xd8, 0xca, 0x76, 0xce, 0x5e, 0xd4, 0xf6, 0x85, 0x4c, 0x6a, 0x60,
  92. 0x08, 0x5d, 0x0e, 0x34, 0x8b, 0xf2, 0xb6, 0xe3, 0xb7, 0x51, 0xca, 0x43,
  93. 0xaa, 0x68, 0x7b, 0x0a, 0x6e, 0xea, 0xce, 0x1e, 0x2c, 0x34, 0x8e, 0x0f,
  94. 0xe2, 0xcc, 0x38, 0xf2, 0x9a, 0x98, 0xef, 0xe6, 0x7f, 0xf6, 0x62, 0xbb
  95. };
  96. static const unsigned char dsa_3072_256_sha512_q[] = {
  97. 0xc1, 0xdb, 0xc1, 0x21, 0x50, 0x49, 0x63, 0xa3, 0x77, 0x6d, 0x4c, 0x92,
  98. 0xed, 0x58, 0x9e, 0x98, 0xea, 0xac, 0x7a, 0x90, 0x13, 0x24, 0xf7, 0xcd,
  99. 0xd7, 0xe6, 0xd4, 0x8f, 0xf0, 0x45, 0x4b, 0xf7
  100. };
  101. static const unsigned char dsa_3072_256_sha512_seed[] = {
  102. 0x35, 0x24, 0xb5, 0x59, 0xd5, 0x27, 0x58, 0x10, 0xf6, 0xa2, 0x7c, 0x9a,
  103. 0x0d, 0xc2, 0x70, 0x8a, 0xb0, 0x41, 0x4a, 0x84, 0x0b, 0xfe, 0x66, 0xf5,
  104. 0x3a, 0xbf, 0x4a, 0xa9, 0xcb, 0xfc, 0xa6, 0x22
  105. };
  106. static int dsa_3072_256_sha512_counter = 1604;
  107. static const unsigned char dsa_2048_224_sha256_p[] = {
  108. 0xe9, 0x13, 0xbc, 0xf2, 0x14, 0x5d, 0xf9, 0x79, 0xd6, 0x6d, 0xf5, 0xc5,
  109. 0xbe, 0x7b, 0x6f, 0x90, 0x63, 0xd0, 0xfd, 0xee, 0x4f, 0xc4, 0x65, 0x83,
  110. 0xbf, 0xec, 0xc3, 0x2c, 0x5d, 0x30, 0xc8, 0xa4, 0x3b, 0x2f, 0x3b, 0x29,
  111. 0x43, 0x69, 0xfb, 0x6e, 0xa9, 0xa4, 0x07, 0x6c, 0xcd, 0xb0, 0xd2, 0xd9,
  112. 0xd3, 0xe6, 0xf4, 0x87, 0x16, 0xb7, 0xe5, 0x06, 0xb9, 0xba, 0xd6, 0x87,
  113. 0xbc, 0x01, 0x9e, 0xba, 0xc2, 0xcf, 0x39, 0xb6, 0xec, 0xdc, 0x75, 0x07,
  114. 0xc1, 0x39, 0x2d, 0x6a, 0x95, 0x31, 0x97, 0xda, 0x54, 0x20, 0x29, 0xe0,
  115. 0x1b, 0xf9, 0x74, 0x65, 0xaa, 0xc1, 0x47, 0xd3, 0x9e, 0xb4, 0x3c, 0x1d,
  116. 0xe0, 0xdc, 0x2d, 0x21, 0xab, 0x12, 0x3b, 0xa5, 0x51, 0x1e, 0xc6, 0xbc,
  117. 0x6b, 0x4c, 0x22, 0xd1, 0x7c, 0xc6, 0xce, 0xcb, 0x8c, 0x1d, 0x1f, 0xce,
  118. 0x1c, 0xe2, 0x75, 0x49, 0x6d, 0x2c, 0xee, 0x7f, 0x5f, 0xb8, 0x74, 0x42,
  119. 0x5c, 0x96, 0x77, 0x13, 0xff, 0x80, 0xf3, 0x05, 0xc7, 0xfe, 0x08, 0x3b,
  120. 0x25, 0x36, 0x46, 0xa2, 0xc4, 0x26, 0xb4, 0xb0, 0x3b, 0xd5, 0xb2, 0x4c,
  121. 0x13, 0x29, 0x0e, 0x47, 0x31, 0x66, 0x7d, 0x78, 0x57, 0xe6, 0xc2, 0xb5,
  122. 0x9f, 0x46, 0x17, 0xbc, 0xa9, 0x9a, 0x49, 0x1c, 0x0f, 0x45, 0xe0, 0x88,
  123. 0x97, 0xa1, 0x30, 0x7c, 0x42, 0xb7, 0x2c, 0x0a, 0xce, 0xb3, 0xa5, 0x7a,
  124. 0x61, 0x8e, 0xab, 0x44, 0xc1, 0xdc, 0x70, 0xe5, 0xda, 0x78, 0x2a, 0xb4,
  125. 0xe6, 0x3c, 0xa0, 0x58, 0xda, 0x62, 0x0a, 0xb2, 0xa9, 0x3d, 0xaa, 0x49,
  126. 0x7e, 0x7f, 0x9a, 0x19, 0x67, 0xee, 0xd6, 0xe3, 0x67, 0x13, 0xe8, 0x6f,
  127. 0x79, 0x50, 0x76, 0xfc, 0xb3, 0x9d, 0x7e, 0x9e, 0x3e, 0x6e, 0x47, 0xb1,
  128. 0x11, 0x5e, 0xc8, 0x83, 0x3a, 0x3c, 0xfc, 0x82, 0x5c, 0x9d, 0x34, 0x65,
  129. 0x73, 0xb4, 0x56, 0xd5
  130. };
  131. static const unsigned char dsa_2048_224_sha256_q[] = {
  132. 0xb0, 0xdf, 0xa1, 0x7b, 0xa4, 0x77, 0x64, 0x0e, 0xb9, 0x28, 0xbb, 0xbc,
  133. 0xd4, 0x60, 0x02, 0xaf, 0x21, 0x8c, 0xb0, 0x69, 0x0f, 0x8a, 0x7b, 0xc6,
  134. 0x80, 0xcb, 0x0a, 0x45
  135. };
  136. static const unsigned char dsa_2048_224_sha256_g[] = {
  137. 0x11, 0x7c, 0x5f, 0xf6, 0x99, 0x44, 0x67, 0x5b, 0x69, 0xa3, 0x83, 0xef,
  138. 0xb5, 0x85, 0xa2, 0x19, 0x35, 0x18, 0x2a, 0xf2, 0x58, 0xf4, 0xc9, 0x58,
  139. 0x9e, 0xb9, 0xe8, 0x91, 0x17, 0x2f, 0xb0, 0x60, 0x85, 0x95, 0xa6, 0x62,
  140. 0x36, 0xd0, 0xff, 0x94, 0xb9, 0xa6, 0x50, 0xad, 0xa6, 0xf6, 0x04, 0x28,
  141. 0xc2, 0xc9, 0xb9, 0x75, 0xf3, 0x66, 0xb4, 0xeb, 0xf6, 0xd5, 0x06, 0x13,
  142. 0x01, 0x64, 0x82, 0xa9, 0xf1, 0xd5, 0x41, 0xdc, 0xf2, 0x08, 0xfc, 0x2f,
  143. 0xc4, 0xa1, 0x21, 0xee, 0x7d, 0xbc, 0xda, 0x5a, 0xa4, 0xa2, 0xb9, 0x68,
  144. 0x87, 0x36, 0xba, 0x53, 0x9e, 0x14, 0x4e, 0x76, 0x5c, 0xba, 0x79, 0x3d,
  145. 0x0f, 0xe5, 0x99, 0x1c, 0x27, 0xfc, 0xaf, 0x10, 0x63, 0x87, 0x68, 0x0e,
  146. 0x3e, 0x6e, 0xaa, 0xf3, 0xdf, 0x76, 0x7e, 0x02, 0x9a, 0x41, 0x96, 0xa1,
  147. 0x6c, 0xbb, 0x67, 0xee, 0x0c, 0xad, 0x72, 0x65, 0xf1, 0x70, 0xb0, 0x39,
  148. 0x9b, 0x54, 0x5f, 0xd7, 0x6c, 0xc5, 0x9a, 0x90, 0x53, 0x18, 0xde, 0x5e,
  149. 0x62, 0x89, 0xb9, 0x2f, 0x66, 0x59, 0x3a, 0x3d, 0x10, 0xeb, 0xa5, 0x99,
  150. 0xf6, 0x21, 0x7d, 0xf2, 0x7b, 0x42, 0x15, 0x1c, 0x55, 0x79, 0x15, 0xaa,
  151. 0xa4, 0x17, 0x2e, 0x48, 0xc3, 0xa8, 0x36, 0xf5, 0x1a, 0x97, 0xce, 0xbd,
  152. 0x72, 0xef, 0x1d, 0x50, 0x5b, 0xb1, 0x60, 0x0a, 0x5c, 0x0b, 0xa6, 0x21,
  153. 0x38, 0x28, 0x4e, 0x89, 0x33, 0x1d, 0xb5, 0x7e, 0x5c, 0xf1, 0x6b, 0x2c,
  154. 0xbd, 0xad, 0x84, 0xb2, 0x8e, 0x96, 0xe2, 0x30, 0xe7, 0x54, 0xb8, 0xc9,
  155. 0x70, 0xcb, 0x10, 0x30, 0x63, 0x90, 0xf4, 0x45, 0x64, 0x93, 0x09, 0x38,
  156. 0x6a, 0x47, 0x58, 0x31, 0x04, 0x1a, 0x18, 0x04, 0x1a, 0xe0, 0xd7, 0x0b,
  157. 0x3c, 0xbe, 0x2a, 0x9c, 0xec, 0xcc, 0x0d, 0x0c, 0xed, 0xde, 0x54, 0xbc,
  158. 0xe6, 0x93, 0x59, 0xfc
  159. };
  160. static int ffc_params_validate_g_unverified_test(void)
  161. {
  162. int ret = 0, res;
  163. FFC_PARAMS params;
  164. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  165. BIGNUM *p1 = NULL, *g1 = NULL;
  166. ossl_ffc_params_init(&params);
  167. if (!TEST_ptr(p = BN_bin2bn(dsa_2048_224_sha256_p,
  168. sizeof(dsa_2048_224_sha256_p), NULL)))
  169. goto err;
  170. p1 = p;
  171. if (!TEST_ptr(q = BN_bin2bn(dsa_2048_224_sha256_q,
  172. sizeof(dsa_2048_224_sha256_q), NULL)))
  173. goto err;
  174. if (!TEST_ptr(g = BN_bin2bn(dsa_2048_224_sha256_g,
  175. sizeof(dsa_2048_224_sha256_g), NULL)))
  176. goto err;
  177. g1 = g;
  178. /* Fail if g is NULL */
  179. ossl_ffc_params_set0_pqg(&params, p, q, NULL);
  180. p = NULL;
  181. q = NULL;
  182. ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_G);
  183. ossl_ffc_set_digest(&params, "SHA256", NULL);
  184. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  185. FFC_PARAM_TYPE_DSA,
  186. &res, NULL)))
  187. goto err;
  188. ossl_ffc_params_set0_pqg(&params, p, q, g);
  189. g = NULL;
  190. if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  191. FFC_PARAM_TYPE_DSA,
  192. &res, NULL)))
  193. goto err;
  194. /* incorrect g */
  195. BN_add_word(g1, 1);
  196. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  197. FFC_PARAM_TYPE_DSA,
  198. &res, NULL)))
  199. goto err;
  200. /* fail if g < 2 */
  201. BN_set_word(g1, 1);
  202. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  203. FFC_PARAM_TYPE_DSA,
  204. &res, NULL)))
  205. goto err;
  206. BN_copy(g1, p1);
  207. /* Fail if g >= p */
  208. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  209. FFC_PARAM_TYPE_DSA,
  210. &res, NULL)))
  211. goto err;
  212. ret = 1;
  213. err:
  214. ossl_ffc_params_cleanup(&params);
  215. BN_free(p);
  216. BN_free(q);
  217. BN_free(g);
  218. return ret;
  219. }
  220. static int ffc_params_validate_pq_test(void)
  221. {
  222. int ret = 0, res = -1;
  223. FFC_PARAMS params;
  224. BIGNUM *p = NULL, *q = NULL;
  225. ossl_ffc_params_init(&params);
  226. if (!TEST_ptr(p = BN_bin2bn(dsa_2048_224_sha224_p,
  227. sizeof(dsa_2048_224_sha224_p),
  228. NULL)))
  229. goto err;
  230. if (!TEST_ptr(q = BN_bin2bn(dsa_2048_224_sha224_q,
  231. sizeof(dsa_2048_224_sha224_q),
  232. NULL)))
  233. goto err;
  234. /* No p */
  235. ossl_ffc_params_set0_pqg(&params, NULL, q, NULL);
  236. q = NULL;
  237. ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_PQ);
  238. ossl_ffc_set_digest(&params, "SHA224", NULL);
  239. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  240. FFC_PARAM_TYPE_DSA,
  241. &res, NULL)))
  242. goto err;
  243. /* Test valid case */
  244. ossl_ffc_params_set0_pqg(&params, p, NULL, NULL);
  245. p = NULL;
  246. ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed,
  247. sizeof(dsa_2048_224_sha224_seed),
  248. dsa_2048_224_sha224_counter);
  249. if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  250. FFC_PARAM_TYPE_DSA,
  251. &res, NULL)))
  252. goto err;
  253. /* Bad counter - so p is not prime */
  254. ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed,
  255. sizeof(dsa_2048_224_sha224_seed),
  256. 1);
  257. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  258. FFC_PARAM_TYPE_DSA,
  259. &res, NULL)))
  260. goto err;
  261. /* seedlen smaller than N */
  262. ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed,
  263. sizeof(dsa_2048_224_sha224_seed)-1,
  264. dsa_2048_224_sha224_counter);
  265. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  266. FFC_PARAM_TYPE_DSA,
  267. &res, NULL)))
  268. goto err;
  269. /* Provided seed doesnt produce a valid prime q */
  270. ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_bad_seed,
  271. sizeof(dsa_2048_224_sha224_bad_seed),
  272. dsa_2048_224_sha224_counter);
  273. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  274. FFC_PARAM_TYPE_DSA,
  275. &res, NULL)))
  276. goto err;
  277. if (!TEST_ptr(p = BN_bin2bn(dsa_3072_256_sha512_p,
  278. sizeof(dsa_3072_256_sha512_p), NULL)))
  279. goto err;
  280. if (!TEST_ptr(q = BN_bin2bn(dsa_3072_256_sha512_q,
  281. sizeof(dsa_3072_256_sha512_q),
  282. NULL)))
  283. goto err;
  284. ossl_ffc_params_set0_pqg(&params, p, q, NULL);
  285. p = q = NULL;
  286. ossl_ffc_set_digest(&params, "SHA512", NULL);
  287. ossl_ffc_params_set_validate_params(&params, dsa_3072_256_sha512_seed,
  288. sizeof(dsa_3072_256_sha512_seed),
  289. dsa_3072_256_sha512_counter);
  290. /* Q doesn't div P-1 */
  291. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  292. FFC_PARAM_TYPE_DSA,
  293. &res, NULL)))
  294. goto err;
  295. /* Bad L/N for FIPS DH */
  296. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  297. FFC_PARAM_TYPE_DH,
  298. &res, NULL)))
  299. goto err;
  300. ret = 1;
  301. err:
  302. ossl_ffc_params_cleanup(&params);
  303. BN_free(p);
  304. BN_free(q);
  305. return ret;
  306. }
  307. #endif /* OPENSSL_NO_DSA */
  308. #ifndef OPENSSL_NO_DH
  309. static int ffc_params_gen_test(void)
  310. {
  311. int ret = 0, res = -1;
  312. FFC_PARAMS params;
  313. ossl_ffc_params_init(&params);
  314. if (!TEST_true(ossl_ffc_params_FIPS186_4_generate(NULL, &params,
  315. FFC_PARAM_TYPE_DH,
  316. 2048, 256, &res, NULL)))
  317. goto err;
  318. if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  319. FFC_PARAM_TYPE_DH,
  320. &res, NULL)))
  321. goto err;
  322. ret = 1;
  323. err:
  324. ossl_ffc_params_cleanup(&params);
  325. return ret;
  326. }
  327. static int ffc_params_gen_canonicalg_test(void)
  328. {
  329. int ret = 0, res = -1;
  330. FFC_PARAMS params;
  331. ossl_ffc_params_init(&params);
  332. params.gindex = 1;
  333. if (!TEST_true(ossl_ffc_params_FIPS186_4_generate(NULL, &params,
  334. FFC_PARAM_TYPE_DH,
  335. 2048, 256, &res, NULL)))
  336. goto err;
  337. if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  338. FFC_PARAM_TYPE_DH,
  339. &res, NULL)))
  340. goto err;
  341. if (!TEST_true(ossl_ffc_params_print(bio_out, &params, 4)))
  342. goto err;
  343. ret = 1;
  344. err:
  345. ossl_ffc_params_cleanup(&params);
  346. return ret;
  347. }
  348. static int ffc_params_fips186_2_gen_validate_test(void)
  349. {
  350. int ret = 0, res = -1;
  351. FFC_PARAMS params;
  352. BIGNUM *bn = NULL;
  353. ossl_ffc_params_init(&params);
  354. if (!TEST_ptr(bn = BN_new()))
  355. goto err;
  356. if (!TEST_true(ossl_ffc_params_FIPS186_2_generate(NULL, &params,
  357. FFC_PARAM_TYPE_DH,
  358. 1024, 160, &res, NULL)))
  359. goto err;
  360. if (!TEST_true(ossl_ffc_params_FIPS186_2_validate(NULL, &params,
  361. FFC_PARAM_TYPE_DH,
  362. &res, NULL)))
  363. goto err;
  364. /*
  365. * The fips186-2 generation should produce a different q compared to
  366. * fips 186-4 given the same seed value. So validation of q will fail.
  367. */
  368. if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  369. FFC_PARAM_TYPE_DSA,
  370. &res, NULL)))
  371. goto err;
  372. /* As the params are randomly generated the error is one of the following */
  373. if (!TEST_true(res == FFC_CHECK_Q_MISMATCH || res == FFC_CHECK_Q_NOT_PRIME))
  374. goto err;
  375. ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_G);
  376. /* Partially valid g test will still pass */
  377. if (!TEST_int_eq(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
  378. FFC_PARAM_TYPE_DSA,
  379. &res, NULL), 2))
  380. goto err;
  381. if (!TEST_true(ossl_ffc_params_print(bio_out, &params, 4)))
  382. goto err;
  383. ret = 1;
  384. err:
  385. BN_free(bn);
  386. ossl_ffc_params_cleanup(&params);
  387. return ret;
  388. }
  389. extern FFC_PARAMS *dh_get0_params(DH *dh);
  390. static int ffc_public_validate_test(void)
  391. {
  392. int ret = 0, res = -1;
  393. FFC_PARAMS *params;
  394. BIGNUM *pub = NULL;
  395. DH *dh = NULL;
  396. if (!TEST_ptr(pub = BN_new()))
  397. goto err;
  398. if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048)))
  399. goto err;
  400. params = dh_get0_params(dh);
  401. if (!TEST_true(BN_set_word(pub, 1)))
  402. goto err;
  403. BN_set_negative(pub, 1);
  404. /* Fail if public key is negative */
  405. if (!TEST_false(ossl_ffc_validate_public_key(params, pub, &res)))
  406. goto err;
  407. if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res))
  408. goto err;
  409. if (!TEST_true(BN_set_word(pub, 0)))
  410. goto err;
  411. if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res))
  412. goto err;
  413. /* Fail if public key is zero */
  414. if (!TEST_false(ossl_ffc_validate_public_key(params, pub, &res)))
  415. goto err;
  416. if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res))
  417. goto err;
  418. /* Fail if public key is 1 */
  419. if (!TEST_false(ossl_ffc_validate_public_key(params, BN_value_one(), &res)))
  420. goto err;
  421. if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res))
  422. goto err;
  423. if (!TEST_true(BN_add_word(pub, 2)))
  424. goto err;
  425. /* Pass if public key >= 2 */
  426. if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
  427. goto err;
  428. if (!TEST_ptr(BN_copy(pub, params->p)))
  429. goto err;
  430. /* Fail if public key = p */
  431. if (!TEST_false(ossl_ffc_validate_public_key(params, pub, &res)))
  432. goto err;
  433. if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_LARGE, res))
  434. goto err;
  435. if (!TEST_true(BN_sub_word(pub, 1)))
  436. goto err;
  437. /* Fail if public key = p - 1 */
  438. if (!TEST_false(ossl_ffc_validate_public_key(params, pub, &res)))
  439. goto err;
  440. if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_LARGE, res))
  441. goto err;
  442. if (!TEST_true(BN_sub_word(pub, 1)))
  443. goto err;
  444. /* Fail if public key is not related to p & q */
  445. if (!TEST_false(ossl_ffc_validate_public_key(params, pub, &res)))
  446. goto err;
  447. if (!TEST_int_eq(FFC_ERROR_PUBKEY_INVALID, res))
  448. goto err;
  449. if (!TEST_true(BN_sub_word(pub, 5)))
  450. goto err;
  451. /* Pass if public key is valid */
  452. if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
  453. goto err;
  454. ret = 1;
  455. err:
  456. DH_free(dh);
  457. BN_free(pub);
  458. return ret;
  459. }
  460. static int ffc_private_validate_test(void)
  461. {
  462. int ret = 0, res = -1;
  463. FFC_PARAMS *params;
  464. BIGNUM *priv = NULL;
  465. DH *dh = NULL;
  466. if (!TEST_ptr(priv = BN_new()))
  467. goto err;
  468. if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048)))
  469. goto err;
  470. params = dh_get0_params(dh);
  471. if (!TEST_true(BN_set_word(priv, 1)))
  472. goto err;
  473. BN_set_negative(priv, 1);
  474. /* Fail if priv key is negative */
  475. if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res)))
  476. goto err;
  477. if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_SMALL, res))
  478. goto err;
  479. if (!TEST_true(BN_set_word(priv, 0)))
  480. goto err;
  481. /* Fail if priv key is zero */
  482. if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res)))
  483. goto err;
  484. if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_SMALL, res))
  485. goto err;
  486. /* Pass if priv key >= 1 */
  487. if (!TEST_true(ossl_ffc_validate_private_key(params->q, BN_value_one(),
  488. &res)))
  489. goto err;
  490. if (!TEST_ptr(BN_copy(priv, params->q)))
  491. goto err;
  492. /* Fail if priv key = upper */
  493. if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res)))
  494. goto err;
  495. if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_LARGE, res))
  496. goto err;
  497. if (!TEST_true(BN_sub_word(priv, 1)))
  498. goto err;
  499. /* Pass if priv key <= upper - 1 */
  500. if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
  501. goto err;
  502. ret = 1;
  503. err:
  504. DH_free(dh);
  505. BN_free(priv);
  506. return ret;
  507. }
  508. static int ffc_private_gen_test(int index)
  509. {
  510. int ret = 0, res = -1, N;
  511. FFC_PARAMS *params;
  512. BIGNUM *priv = NULL;
  513. DH *dh = NULL;
  514. BN_CTX *ctx = NULL;
  515. if (!TEST_ptr(ctx = BN_CTX_new_ex(NULL)))
  516. goto err;
  517. if (!TEST_ptr(priv = BN_new()))
  518. goto err;
  519. if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048)))
  520. goto err;
  521. params = dh_get0_params(dh);
  522. N = BN_num_bits(params->q);
  523. /* Fail since N < 2*s - where s = 112*/
  524. if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, 220, 112, priv)))
  525. goto err;
  526. /* fail since N > len(q) */
  527. if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, N + 1, 112, priv)))
  528. goto err;
  529. /* pass since 2s <= N <= len(q) */
  530. if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, N, 112, priv)))
  531. goto err;
  532. /* pass since N = len(q) */
  533. if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
  534. goto err;
  535. /* pass since 2s <= N < len(q) */
  536. if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, N / 2, 112, priv)))
  537. goto err;
  538. if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
  539. goto err;
  540. /* N and s are ignored in this case */
  541. if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, 0, 0, priv)))
  542. goto err;
  543. if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
  544. goto err;
  545. ret = 1;
  546. err:
  547. DH_free(dh);
  548. BN_free(priv);
  549. BN_CTX_free(ctx);
  550. return ret;
  551. }
  552. #endif /* OPENSSL_NO_DH */
  553. int setup_tests(void)
  554. {
  555. #ifndef OPENSSL_NO_DSA
  556. ADD_TEST(ffc_params_validate_pq_test);
  557. ADD_TEST(ffc_params_validate_g_unverified_test);
  558. #endif /* OPENSSL_NO_DSA */
  559. #ifndef OPENSSL_NO_DH
  560. ADD_TEST(ffc_params_gen_test);
  561. ADD_TEST(ffc_params_gen_canonicalg_test);
  562. ADD_TEST(ffc_params_fips186_2_gen_validate_test);
  563. ADD_TEST(ffc_public_validate_test);
  564. ADD_TEST(ffc_private_validate_test);
  565. ADD_ALL_TESTS(ffc_private_gen_test, 10);
  566. #endif /* OPENSSL_NO_DH */
  567. return 1;
  568. }