|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright 2021 Intel Corporation |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*******************************************************************************/ |
| 16 | + |
| 17 | +/*! |
| 18 | + * |
| 19 | + * \file |
| 20 | + * |
| 21 | + * \brief DSA-DLP verification scheme example |
| 22 | + * |
| 23 | + * This example demonstrates message verification according to |
| 24 | + * DSA-DLP scheme with (L = 1024, N = 160) DSA parameters and SHA-1 hash function. |
| 25 | + * |
| 26 | + * The DSA-DLP scheme is implemented according : Digital Signature Standard (DSS) (FIPS PUB 186-4) (July 2013) |
| 27 | + * |
| 28 | + * available at: |
| 29 | + * |
| 30 | + * http://dx.doi.org/10.6028/NIST.FIPS.186-4. |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +#include <string.h> |
| 35 | + |
| 36 | +#include "bignum.h" |
| 37 | +#include "examples_common.h" |
| 38 | +#include "ippcp.h" |
| 39 | + |
| 40 | +/*! Parameters DSA-DLP scheme */ |
| 41 | +static const int L_BIT = 1024; |
| 42 | +static const int N_BIT = 160; |
| 43 | + |
| 44 | +/*! Message size in bytes */ |
| 45 | +static const int MSG_LEN_BYTE = 6; |
| 46 | + |
| 47 | +/*! Message text */ |
| 48 | +static Ipp8u MSG[MSG_LEN_BYTE] = {0x31, 0x32, 0x33, |
| 49 | + 0x34, 0x30, 0x30}; |
| 50 | + |
| 51 | +/*! The generator of the multiplicative subgroup */ |
| 52 | +static const BigNumber G( |
| 53 | + "0x" |
| 54 | + "0835AA8C358BBF01A1846D1206323FABE408B0E98789FCC6239DA14D4B3F86C2" |
| 55 | + "76A8F48AA85A59507E620AD1BC745F0F1CBF63EC98C229C2610D77C634D1642E" |
| 56 | + "404354771655B2D5662F7A45227178CE3430AF0F6B3BB94B52F7F51E97BAD659" |
| 57 | + "B1BA0684E208BE624C28D82FB1162F18DD9DCE45216461654CF3374624D15A8D"); |
| 58 | + |
| 59 | +/*! The modulus p */ |
| 60 | +static const BigNumber P( |
| 61 | + "0x" |
| 62 | + "B34CE9C1E78294D3258473842005D2A48C8C566CFCA8F84C0606F2529B59A6D3" |
| 63 | + "8AAE071B53BB2167EAA4FC3B01FE176E787E481B6037AAC62CBC3D089799536A" |
| 64 | + "869FA8CDFEA1E8B1FD2D1CD3A30350859A2CD6B3EC2F9BFBB68BB11B4BBE2ADA" |
| 65 | + "A18D64A93639543AE5E16293E311C0CF8C8D6E180DF05D08C2FD2D93D570751F"); |
| 66 | + |
| 67 | +/*! The order of the generator g */ |
| 68 | +static const BigNumber Q("0xB90B38BA0A50A43EC6898D3F9B68049777F489B1"); |
| 69 | + |
| 70 | +/*! The public key value */ |
| 71 | +static const BigNumber Y( |
| 72 | + "0x" |
| 73 | + "173931DDA31EFF32F24B383091BF77EACDC6EFD557624911D8E9B9DEBF0F256D" |
| 74 | + "0CFFAC5567B33F6EAAE9D3275BBED7EF9F5F94C4003C959E49A1ED3F58C31B21" |
| 75 | + "BACCC0ED8840B46145F121B8906D072129BAE01F071947997E8EF760D2D9EA21" |
| 76 | + "D08A5EB7E89390B21A85664713C549E25FEDA6E9E6C31970866BDFBC8FA981F6"); |
| 77 | + |
| 78 | +/*! R digital signature component */ |
| 79 | +static const BigNumber sigR("0xAA6A258FBF7D90E15614676D377DF8B10E38DB4A"); |
| 80 | + |
| 81 | +/*! S digital signature component */ |
| 82 | +static const BigNumber sigS("0x496D5220B5F67D3532D1F991203BC3523B964C3B"); |
| 83 | + |
| 84 | +int main(void) { |
| 85 | + /*! Internal function status */ |
| 86 | + IppStatus status = ippStsNoErr; |
| 87 | + |
| 88 | + /* Pointer to DSA DLP context structure */ |
| 89 | + IppsDLPState* pDL = NULL; |
| 90 | + |
| 91 | + /* Result verification DSA DLP */ |
| 92 | + IppDLResult result = ippDLValid; |
| 93 | + |
| 94 | + /* Size of DSA-DLP context structure. It will be set up in ippsDLPGetSize(). */ |
| 95 | + int DLSize = 0; |
| 96 | + |
| 97 | + /* Digest size */ |
| 98 | + const int digestSizeBit = IPP_SHA1_DIGEST_BITSIZE; |
| 99 | + const int digestSizeByte = digestSizeBit / 8; |
| 100 | + /* Pointer to the SHA-1 hash method */ |
| 101 | + const IppsHashMethod* hashMethod = ippsHashMethod_SHA1(); |
| 102 | + |
| 103 | + /*! Algorithm */ |
| 104 | + do { |
| 105 | + /* 1. Create a digest by message */ |
| 106 | + /*! Buffer create digest */ |
| 107 | + Ipp8u md[digestSizeByte] = {}; |
| 108 | + |
| 109 | + /*! Create digest by message */ |
| 110 | + status = ippsHashMessage_rmf(MSG, |
| 111 | + MSG_LEN_BYTE, |
| 112 | + md, |
| 113 | + hashMethod); |
| 114 | + /*! Check status create digest */ |
| 115 | + if (ippStsNoErr != status) |
| 116 | + break; |
| 117 | + |
| 118 | + /*! |
| 119 | + * (!) Allocate BigNumber container for the shrank message digest. |
| 120 | + * Note, the DSA algorithm uses only leftmost |minSizeDigestBit| bits of the original message digest |
| 121 | + */ |
| 122 | + const int minSizeDigestBit = IPP_MIN(N_BIT, digestSizeBit); |
| 123 | + BigNumber digest(NULL, bitSizeInWords(minSizeDigestBit)); |
| 124 | + |
| 125 | + /*! Set digest to BigNumber */ |
| 126 | + status = ippsSetOctString_BN(md, |
| 127 | + bitSizeInBytes(minSizeDigestBit), |
| 128 | + digest); |
| 129 | + if (ippStsNoErr != status) |
| 130 | + break; |
| 131 | + |
| 132 | + /* 2. Get size needed for DSA DLP context structure */ |
| 133 | + status = ippsDLPGetSize(L_BIT, N_BIT, |
| 134 | + &DLSize); |
| 135 | + if (ippStsNoErr != status) |
| 136 | + break; |
| 137 | + |
| 138 | + /* 3. Allocate memory for DSA DLP context structure */ |
| 139 | + pDL = (IppsDLPState*)(new Ipp8u[DLSize]); |
| 140 | + if (NULL == pDL) { |
| 141 | + printf("ERROR: Cannot allocate memory (%d bytes) for DSA DLP context\n", DLSize); |
| 142 | + return -1; |
| 143 | + } |
| 144 | + |
| 145 | + /* 4. Initialize DSA DLP context */ |
| 146 | + status = ippsDLPInit(L_BIT, N_BIT, |
| 147 | + pDL); |
| 148 | + if (ippStsNoErr != status) |
| 149 | + break; |
| 150 | + |
| 151 | + /* 5. Set DL Domain Parameters */ |
| 152 | + status = ippsDLPSet(P, Q, G, pDL); |
| 153 | + if (ippStsNoErr != status) |
| 154 | + break; |
| 155 | + |
| 156 | + /* 6. Set up Key Pair into the DL context */ |
| 157 | + status = ippsDLPSetKeyPair(NULL, /* optional Private Key Set */ |
| 158 | + Y, |
| 159 | + pDL); |
| 160 | + if (ippStsNoErr != status) |
| 161 | + break; |
| 162 | + |
| 163 | + /* 7. Verify Signature DSA DLP */ |
| 164 | + status = ippsDLPVerifyDSA(digest, |
| 165 | + sigR, sigS, |
| 166 | + &result, |
| 167 | + pDL); |
| 168 | + if (ippStsNoErr != status) |
| 169 | + break; |
| 170 | + if (ippDLValid != result) |
| 171 | + status = ippStsErr; |
| 172 | + |
| 173 | + } while (0); /* end Algorithm */ |
| 174 | + |
| 175 | + /* 8. Remove secret and release resources */ |
| 176 | + if (NULL != pDL) |
| 177 | + delete[](Ipp8u*) pDL; |
| 178 | + |
| 179 | + PRINT_EXAMPLE_STATUS("ippsDLPVerifyDSA", "DSA-DLP Verification Hash Method Message SHA-1", ippStsNoErr == status); |
| 180 | + |
| 181 | + return status; |
| 182 | +} |
0 commit comments