1 /*############################################################################
2   # Copyright 2016-2017 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  * \file
19  * \brief OctString handling utility implementation.
20  */
21 #include "epid/common-testhelper/octstr-testhelper.h"
22 extern "C" {
23 #include "epid/common/src/memory.h"
24 }
25 #include "epid/common/types.h"
26 #include "ext/ipp/include/ippcp.h"
27 
28 typedef Ipp8u* IppOctStr;
29 
30 /// Internal function to delete BigNum
delete_BigNum(IppsBigNumState ** bn)31 void delete_BigNum(IppsBigNumState** bn) {
32   if (*bn) {
33     SAFE_FREE(*bn);
34   }
35 }
36 /// Internal function to create BigNum from an OctStr256
create_BigNum(IppsBigNumState ** bn,const OctStr256 * str)37 EpidStatus create_BigNum(IppsBigNumState** bn, const OctStr256* str) {
38   EpidStatus result = kEpidErr;
39   IppsBigNumState* ipp_bn_ctx = nullptr;
40   do {
41     IppStatus sts = ippStsNoErr;
42     unsigned int byte_size = sizeof(OctStr256);
43     unsigned int word_size =
44         (unsigned int)((byte_size + sizeof(Ipp32u) - 1) / sizeof(Ipp32u));
45     int bignum_ctx_size = 0;
46 
47     if (!bn || !str) {
48       return kEpidBadArgErr;
49     }
50 
51     sts = ippsBigNumGetSize(word_size, &bignum_ctx_size);
52     if (ippStsNoErr != sts) {
53       if (ippStsLengthErr == sts) {
54         result = kEpidBadArgErr;
55       } else {
56         result = kEpidMathErr;
57       }
58       break;
59     }
60     // Allocate space for ipp bignum context
61     ipp_bn_ctx = (IppsBigNumState*)SAFE_ALLOC(bignum_ctx_size);
62     if (!ipp_bn_ctx) {
63       result = kEpidMemAllocErr;
64       break;
65     }
66     // Initialize ipp bignum context
67     sts = ippsBigNumInit(word_size, ipp_bn_ctx);
68     if (sts != ippStsNoErr) {
69       if (sts == ippStsLengthErr) {
70         result = kEpidBadArgErr;
71       } else {
72         result = kEpidMathErr;
73       }
74       break;
75     }
76 
77     sts = ippsSetOctString_BN((IppOctStr)str, byte_size, ipp_bn_ctx);
78     if (sts != ippStsNoErr) {
79       if (sts == ippStsLengthErr) {
80         result = kEpidBadArgErr;
81       } else {
82         result = kEpidMathErr;
83       }
84       break;
85     }
86     *bn = ipp_bn_ctx;
87     result = kEpidNoErr;
88   } while (0);
89 
90   if (result != kEpidNoErr) {
91     SAFE_FREE(ipp_bn_ctx);
92   }
93   return result;
94 }
95 
Cmp_OctStr256(const OctStr256 * pA,const OctStr256 * pB,unsigned int * pResult)96 EpidStatus Cmp_OctStr256(const OctStr256* pA, const OctStr256* pB,
97                          unsigned int* pResult) {
98   EpidStatus result = kEpidErr;
99   IppsBigNumState* ipp_a_ctx = nullptr;
100   IppsBigNumState* ipp_b_ctx = nullptr;
101 
102   do {
103     IppStatus sts = ippStsNoErr;
104     if (!pA || !pB || !pResult) {
105       return kEpidBadArgErr;
106     }
107     result = create_BigNum(&ipp_a_ctx, pA);
108     if (kEpidNoErr != result) {
109       break;
110     }
111     result = create_BigNum(&ipp_b_ctx, pB);
112     if (kEpidNoErr != result) {
113       break;
114     }
115     sts = ippsCmp_BN(ipp_a_ctx, ipp_b_ctx, pResult);
116     if (ippStsNoErr != sts) {
117       if (ippStsContextMatchErr == sts || ippStsRangeErr == sts ||
118           ippStsLengthErr == sts || ippStsOutOfRangeErr == sts) {
119         result = kEpidBadArgErr;
120       } else {
121         result = kEpidMathErr;
122       }
123     }
124   } while (0);
125 
126   delete_BigNum(&ipp_a_ctx);
127   delete_BigNum(&ipp_b_ctx);
128   return result;
129 }
130