id
int64 1
36.7k
| label
int64 0
1
| bug_url
stringlengths 91
134
| bug_function
stringlengths 13
72.7k
| functions
stringlengths 17
79.2k
|
|---|---|---|---|---|
2,801
| 0
|
https://github.com/openssl/openssl/blob/d6ee8f3dc4414cd97bd63b801f8644f0ff8a1f17/crypto/bio/b_print.c/#L353
|
static int
_dopr(char **sbuffer,
char **buffer,
size_t *maxlen,
size_t *retlen, int *truncated, const char *format, va_list args)
{
char ch;
int64_t value;
LDOUBLE fvalue;
char *strvalue;
int min;
int max;
int state;
int flags;
int cflags;
size_t currlen;
state = DP_S_DEFAULT;
flags = currlen = cflags = min = 0;
max = -1;
ch = *format++;
while (state != DP_S_DONE) {
if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
state = DP_S_DONE;
switch (state) {
case DP_S_DEFAULT:
if (ch == '%')
state = DP_S_FLAGS;
else
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
return 0;
ch = *format++;
break;
case DP_S_FLAGS:
switch (ch) {
case '-':
flags |= DP_F_MINUS;
ch = *format++;
break;
case '+':
flags |= DP_F_PLUS;
ch = *format++;
break;
case ' ':
flags |= DP_F_SPACE;
ch = *format++;
break;
case '#':
flags |= DP_F_NUM;
ch = *format++;
break;
case '0':
flags |= DP_F_ZERO;
ch = *format++;
break;
default:
state = DP_S_MIN;
break;
}
break;
case DP_S_MIN:
if (ossl_isdigit(ch)) {
min = 10 * min + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
min = va_arg(args, int);
ch = *format++;
state = DP_S_DOT;
} else
state = DP_S_DOT;
break;
case DP_S_DOT:
if (ch == '.') {
state = DP_S_MAX;
ch = *format++;
} else
state = DP_S_MOD;
break;
case DP_S_MAX:
if (ossl_isdigit(ch)) {
if (max < 0)
max = 0;
max = 10 * max + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
max = va_arg(args, int);
ch = *format++;
state = DP_S_MOD;
} else
state = DP_S_MOD;
break;
case DP_S_MOD:
switch (ch) {
case 'h':
cflags = DP_C_SHORT;
ch = *format++;
break;
case 'l':
if (*format == 'l') {
cflags = DP_C_LLONG;
format++;
} else
cflags = DP_C_LONG;
ch = *format++;
break;
case 'q':
case 'j':
cflags = DP_C_LLONG;
ch = *format++;
break;
case 'L':
cflags = DP_C_LDOUBLE;
ch = *format++;
break;
case 'z':
cflags = DP_C_SIZE;
ch = *format++;
break;
default:
break;
}
state = DP_S_CONV;
break;
case DP_S_CONV:
switch (ch) {
case 'd':
case 'i':
switch (cflags) {
case DP_C_SHORT:
value = (short int)va_arg(args, int);
break;
case DP_C_LONG:
value = va_arg(args, long int);
break;
case DP_C_LLONG:
value = va_arg(args, int64_t);
break;
case DP_C_SIZE:
value = va_arg(args, ossl_ssize_t);
break;
default:
value = va_arg(args, int);
break;
}
if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
max, flags))
return 0;
break;
case 'X':
flags |= DP_F_UP;
case 'x':
case 'o':
case 'u':
flags |= DP_F_UNSIGNED;
switch (cflags) {
case DP_C_SHORT:
value = (unsigned short int)va_arg(args, unsigned int);
break;
case DP_C_LONG:
value = va_arg(args, unsigned long int);
break;
case DP_C_LLONG:
value = va_arg(args, uint64_t);
break;
case DP_C_SIZE:
value = va_arg(args, size_t);
break;
default:
value = va_arg(args, unsigned int);
break;
}
if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
min, max, flags))
return 0;
break;
case 'f':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
flags, F_FORMAT))
return 0;
break;
case 'E':
flags |= DP_F_UP;
case 'e':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
flags, E_FORMAT))
return 0;
break;
case 'G':
flags |= DP_F_UP;
case 'g':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
flags, G_FORMAT))
return 0;
break;
case 'c':
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
va_arg(args, int)))
return 0;
break;
case 's':
strvalue = va_arg(args, char *);
if (max < 0) {
if (buffer)
max = INT_MAX;
else
max = *maxlen;
}
if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
flags, min, max))
return 0;
break;
case 'p':
value = (size_t)va_arg(args, void *);
if (!fmtint(sbuffer, buffer, &currlen, maxlen,
value, 16, min, max, flags | DP_F_NUM))
return 0;
break;
case 'n':
{
int *num;
num = va_arg(args, int *);
*num = currlen;
}
break;
case '%':
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
return 0;
break;
case 'w':
ch = *format++;
break;
default:
break;
}
ch = *format++;
state = DP_S_DEFAULT;
flags = cflags = min = 0;
max = -1;
break;
case DP_S_DONE:
break;
default:
break;
}
}
if (buffer == NULL) {
*truncated = (currlen > *maxlen - 1);
if (*truncated)
currlen = *maxlen - 1;
}
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
return 0;
*retlen = currlen - 1;
return 1;
}
|
['static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(\n X509V3_EXT_METHOD *method, AUTHORITY_INFO_ACCESS *ainfo,\n STACK_OF(CONF_VALUE) *ret)\n{\n ACCESS_DESCRIPTION *desc;\n int i, nlen;\n char objtmp[80], *ntmp;\n CONF_VALUE *vtmp;\n STACK_OF(CONF_VALUE) *tret = ret;\n for (i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) {\n STACK_OF(CONF_VALUE) *tmp;\n desc = sk_ACCESS_DESCRIPTION_value(ainfo, i);\n tmp = i2v_GENERAL_NAME(method, desc->location, tret);\n if (tmp == NULL)\n goto err;\n tret = tmp;\n vtmp = sk_CONF_VALUE_value(tret, i);\n i2t_ASN1_OBJECT(objtmp, sizeof objtmp, desc->method);\n nlen = strlen(objtmp) + 3 + strlen(vtmp->name) + 1;\n ntmp = OPENSSL_malloc(nlen);\n if (ntmp == NULL)\n goto err;\n BIO_snprintf(ntmp, nlen, "%s - %s", objtmp, vtmp->name);\n OPENSSL_free(vtmp->name);\n vtmp->name = ntmp;\n }\n if (ret == NULL && tret == NULL)\n return sk_CONF_VALUE_new_null();\n return tret;\n err:\n X509V3err(X509V3_F_I2V_AUTHORITY_INFO_ACCESS, ERR_R_MALLOC_FAILURE);\n if (ret == NULL && tret != NULL)\n sk_CONF_VALUE_pop_free(tret, X509V3_conf_free);\n return NULL;\n}', 'STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,\n GENERAL_NAME *gen,\n STACK_OF(CONF_VALUE) *ret)\n{\n unsigned char *p;\n char oline[256], htmp[5];\n int i;\n switch (gen->type) {\n case GEN_OTHERNAME:\n if (!X509V3_add_value("othername", "<unsupported>", &ret))\n return NULL;\n break;\n case GEN_X400:\n if (!X509V3_add_value("X400Name", "<unsupported>", &ret))\n return NULL;\n break;\n case GEN_EDIPARTY:\n if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))\n return NULL;\n break;\n case GEN_EMAIL:\n if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret))\n return NULL;\n break;\n case GEN_DNS:\n if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret))\n return NULL;\n break;\n case GEN_URI:\n if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret))\n return NULL;\n break;\n case GEN_DIRNAME:\n if (X509_NAME_oneline(gen->d.dirn, oline, 256) == NULL\n || !X509V3_add_value("DirName", oline, &ret))\n return NULL;\n break;\n case GEN_IPADD:\n p = gen->d.ip->data;\n if (gen->d.ip->length == 4)\n BIO_snprintf(oline, sizeof(oline), "%d.%d.%d.%d",\n p[0], p[1], p[2], p[3]);\n else if (gen->d.ip->length == 16) {\n oline[0] = 0;\n for (i = 0; i < 8; i++) {\n BIO_snprintf(htmp, sizeof(htmp), "%X", p[0] << 8 | p[1]);\n p += 2;\n strcat(oline, htmp);\n if (i != 7)\n strcat(oline, ":");\n }\n } else {\n if (!X509V3_add_value("IP Address", "<invalid>", &ret))\n return NULL;\n break;\n }\n if (!X509V3_add_value("IP Address", oline, &ret))\n return NULL;\n break;\n case GEN_RID:\n i2t_ASN1_OBJECT(oline, 256, gen->d.rid);\n if (!X509V3_add_value("Registered ID", oline, &ret))\n return NULL;\n break;\n }\n return ret;\n}', 'char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)\n{\n const X509_NAME_ENTRY *ne;\n int i;\n int n, lold, l, l1, l2, num, j, type;\n const char *s;\n char *p;\n unsigned char *q;\n BUF_MEM *b = NULL;\n static const char hex[17] = "0123456789ABCDEF";\n int gs_doit[4];\n char tmp_buf[80];\n#ifdef CHARSET_EBCDIC\n unsigned char ebcdic_buf[1024];\n#endif\n if (buf == NULL) {\n if ((b = BUF_MEM_new()) == NULL)\n goto err;\n if (!BUF_MEM_grow(b, 200))\n goto err;\n b->data[0] = \'\\0\';\n len = 200;\n } else if (len == 0) {\n return NULL;\n }\n if (a == NULL) {\n if (b) {\n buf = b->data;\n OPENSSL_free(b);\n }\n strncpy(buf, "NO X509_NAME", len);\n buf[len - 1] = \'\\0\';\n return buf;\n }\n len--;\n l = 0;\n for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {\n ne = sk_X509_NAME_ENTRY_value(a->entries, i);\n n = OBJ_obj2nid(ne->object);\n if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {\n i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);\n s = tmp_buf;\n }\n l1 = strlen(s);\n type = ne->value->type;\n num = ne->value->length;\n if (num > NAME_ONELINE_MAX) {\n X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);\n goto end;\n }\n q = ne->value->data;\n#ifdef CHARSET_EBCDIC\n if (type == V_ASN1_GENERALSTRING ||\n type == V_ASN1_VISIBLESTRING ||\n type == V_ASN1_PRINTABLESTRING ||\n type == V_ASN1_TELETEXSTRING ||\n type == V_ASN1_IA5STRING) {\n if (num > (int)sizeof(ebcdic_buf))\n num = sizeof(ebcdic_buf);\n ascii2ebcdic(ebcdic_buf, q, num);\n q = ebcdic_buf;\n }\n#endif\n if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;\n for (j = 0; j < num; j++)\n if (q[j] != 0)\n gs_doit[j & 3] = 1;\n if (gs_doit[0] | gs_doit[1] | gs_doit[2])\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n else {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;\n gs_doit[3] = 1;\n }\n } else\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n for (l2 = j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n l2++;\n#ifndef CHARSET_EBCDIC\n if ((q[j] < \' \') || (q[j] > \'~\'))\n l2 += 3;\n#else\n if ((os_toascii[q[j]] < os_toascii[\' \']) ||\n (os_toascii[q[j]] > os_toascii[\'~\']))\n l2 += 3;\n#endif\n }\n lold = l;\n l += 1 + l1 + 1 + l2;\n if (l > NAME_ONELINE_MAX) {\n X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);\n goto end;\n }\n if (b != NULL) {\n if (!BUF_MEM_grow(b, l + 1))\n goto err;\n p = &(b->data[lold]);\n } else if (l > len) {\n break;\n } else\n p = &(buf[lold]);\n *(p++) = \'/\';\n memcpy(p, s, (unsigned int)l1);\n p += l1;\n *(p++) = \'=\';\n#ifndef CHARSET_EBCDIC\n q = ne->value->data;\n#endif\n for (j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n#ifndef CHARSET_EBCDIC\n n = q[j];\n if ((n < \' \') || (n > \'~\')) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = n;\n#else\n n = os_toascii[q[j]];\n if ((n < os_toascii[\' \']) || (n > os_toascii[\'~\'])) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = q[j];\n#endif\n }\n *p = \'\\0\';\n }\n if (b != NULL) {\n p = b->data;\n OPENSSL_free(b);\n } else\n p = buf;\n if (i == 0)\n *p = \'\\0\';\n return p;\n err:\n X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);\n end:\n BUF_MEM_free(b);\n return NULL;\n}', 'int BIO_snprintf(char *buf, size_t n, const char *format, ...)\n{\n va_list args;\n int ret;\n va_start(args, format);\n ret = BIO_vsnprintf(buf, n, format, args);\n va_end(args);\n return ret;\n}', 'int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)\n{\n size_t retlen;\n int truncated;\n if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))\n return -1;\n if (truncated)\n return -1;\n else\n return (retlen <= INT_MAX) ? (int)retlen : -1;\n}', "static int\n_dopr(char **sbuffer,\n char **buffer,\n size_t *maxlen,\n size_t *retlen, int *truncated, const char *format, va_list args)\n{\n char ch;\n int64_t value;\n LDOUBLE fvalue;\n char *strvalue;\n int min;\n int max;\n int state;\n int flags;\n int cflags;\n size_t currlen;\n state = DP_S_DEFAULT;\n flags = currlen = cflags = min = 0;\n max = -1;\n ch = *format++;\n while (state != DP_S_DONE) {\n if (ch == '\\0' || (buffer == NULL && currlen >= *maxlen))\n state = DP_S_DONE;\n switch (state) {\n case DP_S_DEFAULT:\n if (ch == '%')\n state = DP_S_FLAGS;\n else\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n ch = *format++;\n break;\n case DP_S_FLAGS:\n switch (ch) {\n case '-':\n flags |= DP_F_MINUS;\n ch = *format++;\n break;\n case '+':\n flags |= DP_F_PLUS;\n ch = *format++;\n break;\n case ' ':\n flags |= DP_F_SPACE;\n ch = *format++;\n break;\n case '#':\n flags |= DP_F_NUM;\n ch = *format++;\n break;\n case '0':\n flags |= DP_F_ZERO;\n ch = *format++;\n break;\n default:\n state = DP_S_MIN;\n break;\n }\n break;\n case DP_S_MIN:\n if (ossl_isdigit(ch)) {\n min = 10 * min + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n min = va_arg(args, int);\n ch = *format++;\n state = DP_S_DOT;\n } else\n state = DP_S_DOT;\n break;\n case DP_S_DOT:\n if (ch == '.') {\n state = DP_S_MAX;\n ch = *format++;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MAX:\n if (ossl_isdigit(ch)) {\n if (max < 0)\n max = 0;\n max = 10 * max + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n max = va_arg(args, int);\n ch = *format++;\n state = DP_S_MOD;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MOD:\n switch (ch) {\n case 'h':\n cflags = DP_C_SHORT;\n ch = *format++;\n break;\n case 'l':\n if (*format == 'l') {\n cflags = DP_C_LLONG;\n format++;\n } else\n cflags = DP_C_LONG;\n ch = *format++;\n break;\n case 'q':\n case 'j':\n cflags = DP_C_LLONG;\n ch = *format++;\n break;\n case 'L':\n cflags = DP_C_LDOUBLE;\n ch = *format++;\n break;\n case 'z':\n cflags = DP_C_SIZE;\n ch = *format++;\n break;\n default:\n break;\n }\n state = DP_S_CONV;\n break;\n case DP_S_CONV:\n switch (ch) {\n case 'd':\n case 'i':\n switch (cflags) {\n case DP_C_SHORT:\n value = (short int)va_arg(args, int);\n break;\n case DP_C_LONG:\n value = va_arg(args, long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, int64_t);\n break;\n case DP_C_SIZE:\n value = va_arg(args, ossl_ssize_t);\n break;\n default:\n value = va_arg(args, int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,\n max, flags))\n return 0;\n break;\n case 'X':\n flags |= DP_F_UP;\n case 'x':\n case 'o':\n case 'u':\n flags |= DP_F_UNSIGNED;\n switch (cflags) {\n case DP_C_SHORT:\n value = (unsigned short int)va_arg(args, unsigned int);\n break;\n case DP_C_LONG:\n value = va_arg(args, unsigned long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, uint64_t);\n break;\n case DP_C_SIZE:\n value = va_arg(args, size_t);\n break;\n default:\n value = va_arg(args, unsigned int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,\n ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),\n min, max, flags))\n return 0;\n break;\n case 'f':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags, F_FORMAT))\n return 0;\n break;\n case 'E':\n flags |= DP_F_UP;\n case 'e':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags, E_FORMAT))\n return 0;\n break;\n case 'G':\n flags |= DP_F_UP;\n case 'g':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags, G_FORMAT))\n return 0;\n break;\n case 'c':\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,\n va_arg(args, int)))\n return 0;\n break;\n case 's':\n strvalue = va_arg(args, char *);\n if (max < 0) {\n if (buffer)\n max = INT_MAX;\n else\n max = *maxlen;\n }\n if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,\n flags, min, max))\n return 0;\n break;\n case 'p':\n value = (size_t)va_arg(args, void *);\n if (!fmtint(sbuffer, buffer, &currlen, maxlen,\n value, 16, min, max, flags | DP_F_NUM))\n return 0;\n break;\n case 'n':\n {\n int *num;\n num = va_arg(args, int *);\n *num = currlen;\n }\n break;\n case '%':\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n break;\n case 'w':\n ch = *format++;\n break;\n default:\n break;\n }\n ch = *format++;\n state = DP_S_DEFAULT;\n flags = cflags = min = 0;\n max = -1;\n break;\n case DP_S_DONE:\n break;\n default:\n break;\n }\n }\n if (buffer == NULL) {\n *truncated = (currlen > *maxlen - 1);\n if (*truncated)\n currlen = *maxlen - 1;\n }\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\\0'))\n return 0;\n *retlen = currlen - 1;\n return 1;\n}"]
|
2,802
| 0
|
https://github.com/openssl/openssl/blob/85bcf27cccd8f5f569886479ad96a0c33444404c/crypto/bn/bn_mul.c/#L100
|
BN_ULONG bn_sub_part_words(BN_ULONG *r,
const BN_ULONG *a, const BN_ULONG *b,
int cl, int dl)
{
BN_ULONG c, t;
assert(cl >= 0);
c = bn_sub_words(r, a, b, cl);
if (dl == 0)
return c;
r += cl;
a += cl;
b += cl;
if (dl < 0)
{
for (;;)
{
t = b[0];
r[0] = (0-t-c)&BN_MASK2;
if (t != 0) c=1;
if (++dl >= 0) break;
t = b[1];
r[1] = (0-t-c)&BN_MASK2;
if (t != 0) c=1;
if (++dl >= 0) break;
t = b[2];
r[2] = (0-t-c)&BN_MASK2;
if (t != 0) c=1;
if (++dl >= 0) break;
t = b[3];
r[3] = (0-t-c)&BN_MASK2;
if (t != 0) c=1;
if (++dl >= 0) break;
b += 4;
r += 4;
}
}
else
{
int save_dl = dl;
while(c)
{
t = a[0];
r[0] = (t-c)&BN_MASK2;
if (t != 0) c=0;
if (--dl <= 0) break;
t = a[1];
r[1] = (t-c)&BN_MASK2;
if (t != 0) c=0;
if (--dl <= 0) break;
t = a[2];
r[2] = (t-c)&BN_MASK2;
if (t != 0) c=0;
if (--dl <= 0) break;
t = a[3];
r[3] = (t-c)&BN_MASK2;
if (t != 0) c=0;
if (--dl <= 0) break;
save_dl = dl;
a += 4;
r += 4;
}
if (dl > 0)
{
if (save_dl > dl)
{
switch (save_dl - dl)
{
case 1:
r[1] = a[1];
if (--dl <= 0) break;
case 2:
r[2] = a[2];
if (--dl <= 0) break;
case 3:
r[3] = a[3];
if (--dl <= 0) break;
}
a += 4;
r += 4;
}
}
if (dl > 0)
{
for(;;)
{
r[0] = a[0];
if (--dl <= 0) break;
r[1] = a[1];
if (--dl <= 0) break;
r[2] = a[2];
if (--dl <= 0) break;
r[3] = a[3];
if (--dl <= 0) break;
a += 4;
r += 4;
}
}
}
return c;
}
|
['int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1;\n\tBIGNUM *d,*r;\n\tconst BIGNUM *aa;\n\tBIGNUM *val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tif (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)\n\t\t{\n\t\treturn BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!BN_is_odd(m))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\td = BN_CTX_get(ctx);\n\tr = BN_CTX_get(ctx);\n\tval[0] = BN_CTX_get(ctx);\n\tif (!d || !r || !val[0]) goto err;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_nnmod(val[0],a,m,ctx))\n\t\t\tgoto err;\n\t\taa= val[0];\n\t\t}\n\telse\n\t\taa=a;\n\tif (BN_is_zero(aa))\n\t\t{\n\t\tBN_zero(rr);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\tif (!BN_to_montgomery(val[0],aa,mont,ctx)) goto err;\n\twindow = BN_window_bits_for_exponent_size(bits);\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(d,val[0],val[0],mont,ctx)) goto err;\n\t\tj=1<<(window-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_montgomery(val[i],val[i-1],\n\t\t\t\t\t\td,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n#if 1\n\tj = m->top;\n\tif (m->d[j-1] & (((BN_ULONG)1)<<(BN_BITS2-1)))\n\t\t{\n\t\tif (bn_wexpand(r,j) == NULL) goto err;\n\t\tr->d[0] = (0-m->d[0])&BN_MASK2;\n\t\tfor(i=1;i<j;i++) r->d[i] = (~m->d[i])&BN_MASK2;\n\t\tr->top = j;\n\t\tbn_correct_top(r);\n\t\t}\n\telse\n#endif\n\tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_montgomery(r,r,val[wvalue>>1],mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n#if defined(SPARC_T4_MONT)\n\tif (OPENSSL_sparcv9cap_P[0]&(SPARCV9_VIS3|SPARCV9_PREFER_FPU))\n\t\t{\n\t\tj = mont->N.top;\n\t\tval[0]->d[0] = 1;\n\t\tfor (i=1;i<j;i++) val[0]->d[i] = 0;\n\t\tval[0]->top = j;\n\t\tif (!BN_mod_mul_montgomery(rr,r,val[0],mont,ctx)) goto err;\n\t\t}\n\telse\n#endif\n\tif (!BN_from_montgomery(rr,r,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tBN_CTX_end(ctx);\n\tbn_check_top(rr);\n\treturn(ret);\n\t}', 'const BIGNUM *BN_value_one(void)\n\t{\n\tstatic const BN_ULONG data_one=1L;\n\tstatic const BIGNUM const_one={(BN_ULONG *)&data_one,1,1,0,BN_FLG_STATIC_DATA};\n\treturn(&const_one);\n\t}', 'int BN_to_montgomery(BIGNUM *r,const BIGNUM *a,\tBN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\treturn BN_mod_mul_montgomery(r,a,&(mont->RR),mont,ctx);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp;\n\tint ret=0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n\tint num = mont->N.top;\n\tif (num>1 && a->top==num && b->top==num)\n\t\t{\n\t\tif (bn_wexpand(r,num) == NULL) return(0);\n\t\tif (bn_mul_mont(r->d,a->d,b->d,mont->N.d,mont->n0,num))\n\t\t\t{\n\t\t\tr->neg = a->neg^b->neg;\n\t\t\tr->top = num;\n\t\t\tbn_correct_top(r);\n\t\t\treturn(1);\n\t\t\t}\n\t\t}\n#endif\n\tBN_CTX_start(ctx);\n\ttmp = BN_CTX_get(ctx);\n\tif (tmp == NULL) goto err;\n\tbn_check_top(tmp);\n\tif (a == b)\n\t\t{\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n#ifdef MONT_WORD\n\tif (!BN_from_montgomery_word(r,tmp,mont)) goto err;\n#else\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n#endif\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (t == NULL)\n\t\t\t\tgoto err;\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\n\t\t\t}\n\t\telse if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n\t int tna, int tnb, BN_ULONG *t)\n\t{\n\tint i,j,n2=n*2;\n\tint c1,c2,neg;\n\tBN_ULONG ln,lo,*p;\n\tif (n < 8)\n\t\t{\n\t\tbn_mul_normal(r,a,n+tna,b,n+tnb);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_part_words(a,&(a[n]),tna,n-tna);\n\tc2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n);\n\tneg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tbreak;\n\tcase -3:\n\tcase -2:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\tcase 2:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\tcase 4:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tbreak;\n\t\t}\n# if 0\n\tif (n == 4)\n\t\t{\n\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);\n\t\tmemset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));\n\t\t}\n\telse\n# endif\n\tif (n == 8)\n\t\t{\n\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\tmemset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t}\n\telse\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p);\n\t\tbn_mul_recursive(r,a,b,n,0,0,p);\n\t\ti=n/2;\n\t\tif (tna > tnb)\n\t\t\tj = tna - i;\n\t\telse\n\t\t\tj = tnb - i;\n\t\tif (j == 0)\n\t\t\t{\n\t\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\tmemset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2));\n\t\t\t}\n\t\telse if (j > 0)\n\t\t\t\t{\n\t\t\t\tbn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\tmemset(&(r[n2+tna+tnb]),0,\n\t\t\t\t\tsizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tmemset(&(r[n2]),0,sizeof(BN_ULONG)*n2);\n\t\t\tif (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n\t\t\t\t&& tnb < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t\t\t{\n\t\t\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tfor (;;)\n\t\t\t\t\t{\n\t\t\t\t\ti/=2;\n\t\t\t\t\tif (i < tna || i < tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_part_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (i == tna || i == tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tint n,i;\n\tn = cl-1;\n\tif (dl < 0)\n\t\t{\n\t\tfor (i=dl; i<0; i++)\n\t\t\t{\n\t\t\tif (b[n-i] != 0)\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t}\n\tif (dl > 0)\n\t\t{\n\t\tfor (i=dl; i>0; i--)\n\t\t\t{\n\t\t\tif (a[n+i] != 0)\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\treturn bn_cmp_words(a,b,cl);\n\t}', 'BN_ULONG bn_sub_part_words(BN_ULONG *r,\n\tconst BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tBN_ULONG c, t;\n\tassert(cl >= 0);\n\tc = bn_sub_words(r, a, b, cl);\n\tif (dl == 0)\n\t\treturn c;\n\tr += cl;\n\ta += cl;\n\tb += cl;\n\tif (dl < 0)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt = b[0];\n\t\t\tr[0] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[1];\n\t\t\tr[1] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[2];\n\t\t\tr[2] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[3];\n\t\t\tr[3] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tb += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tint save_dl = dl;\n\t\twhile(c)\n\t\t\t{\n\t\t\tt = a[0];\n\t\t\tr[0] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[1];\n\t\t\tr[1] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[2];\n\t\t\tr[2] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[3];\n\t\t\tr[3] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tsave_dl = dl;\n\t\t\ta += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n\t\t\tif (save_dl > dl)\n\t\t\t\t{\n\t\t\t\tswitch (save_dl - dl)\n\t\t\t\t\t{\n\t\t\t\tcase 1:\n\t\t\t\t\tr[1] = a[1];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 2:\n\t\t\t\t\tr[2] = a[2];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 3:\n\t\t\t\t\tr[3] = a[3];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\t\t}\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n\t\t\tfor(;;)\n\t\t\t\t{\n\t\t\t\tr[0] = a[0];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[1] = a[1];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[2] = a[2];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[3] = a[3];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\treturn c;\n\t}']
|
2,803
| 0
|
https://github.com/openssl/openssl/blob/c504a5e78386aa9f02462d18a90da759f9131321/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,\n\t\t\t\t\t\tunsigned int bitlen, int ispub)\n\t{\n\tconst unsigned char *p = *in;\n\tEVP_PKEY *ret = NULL;\n\tDSA *dsa = NULL;\n\tBN_CTX *ctx = NULL;\n\tunsigned int nbyte;\n\tnbyte = (bitlen + 7) >> 3;\n\tdsa = DSA_new();\n\tret = EVP_PKEY_new();\n\tif (!dsa || !ret)\n\t\tgoto memerr;\n\tif (!read_lebn(&p, nbyte, &dsa->p))\n\t\tgoto memerr;\n\tif (!read_lebn(&p, 20, &dsa->q))\n\t\tgoto memerr;\n\tif (!read_lebn(&p, nbyte, &dsa->g))\n\t\tgoto memerr;\n\tif (ispub)\n\t\t{\n\t\tif (!read_lebn(&p, nbyte, &dsa->pub_key))\n\t\t\tgoto memerr;\n\t\t}\n\telse\n\t\t{\n\t\tif (!read_lebn(&p, 20, &dsa->priv_key))\n\t\t\tgoto memerr;\n\t\tif (!(dsa->pub_key = BN_new()))\n\t\t\tgoto memerr;\n\t\tif (!(ctx = BN_CTX_new()))\n\t\t\tgoto memerr;\n\t\tif (!BN_mod_exp(dsa->pub_key, dsa->g,\n\t\t\t\t\t\t dsa->priv_key, dsa->p, ctx))\n\t\t\tgoto memerr;\n\t\tBN_CTX_free(ctx);\n\t\t}\n\tEVP_PKEY_set1_DSA(ret, dsa);\n\tDSA_free(dsa);\n\t*in = p;\n\treturn ret;\n\tmemerr:\n\tPEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);\n\tif (dsa)\n\t\tDSA_free(dsa);\n\tif (ret)\n\t\tEVP_PKEY_free(ret);\n\tif (ctx)\n\t\tBN_CTX_free(ctx);\n\treturn NULL;\n\t}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n\t BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{\n# ifdef MONT_EXP_WORD\n\t\tif (a->top == 1 && !a->neg && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0))\n\t\t\t{\n\t\t\tBN_ULONG A = a->d[0];\n\t\t\tret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);\n\t\t\t}\n\t\telse\n# endif\n\t\t\tret=BN_mod_exp_mont(r,a,p,m,ctx,NULL);\n\t\t}\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\tbn_check_top(r);\n\treturn(ret);\n\t}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1;\n\tBIGNUM *aa;\n\tBIGNUM *val[TABLE_SIZE];\n\tBN_RECP_CTX recp;\n\tif (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_RECP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n\t\treturn -1;\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(r);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\taa = BN_CTX_get(ctx);\n\tval[0] = BN_CTX_get(ctx);\n\tif(!aa || !val[0]) goto err;\n\tBN_RECP_CTX_init(&recp);\n\tif (m->neg)\n\t\t{\n\t\tif (!BN_copy(aa, m)) goto err;\n\t\taa->neg = 0;\n\t\tif (BN_RECP_CTX_set(&recp,aa,ctx) <= 0) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;\n\t\t}\n\tif (!BN_nnmod(val[0],a,m,ctx)) goto err;\n\tif (BN_is_zero(val[0]))\n\t\t{\n\t\tBN_zero(r);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\twindow = BN_window_bits_for_exponent_size(bits);\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_reciprocal(aa,val[0],val[0],&recp,ctx))\n\t\t\tgoto err;\n\t\tj=1<<(window-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_reciprocal(val[i],val[i-1],\n\t\t\t\t\t\taa,&recp,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n\tif (!BN_one(r)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_reciprocal(r,r,val[wvalue>>1],&recp,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\tBN_RECP_CTX_free(&recp);\n\tbn_check_top(r);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
|
2,804
| 0
|
https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_mul.c/#L728
|
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
{
BN_ULONG *rr;
#ifdef BN_COUNT
printf(" bn_mul_normal %d * %d\n",na,nb);
#endif
if (na < nb)
{
int itmp;
BN_ULONG *ltmp;
itmp=na; na=nb; nb=itmp;
ltmp=a; a=b; b=ltmp;
}
rr= &(r[na]);
rr[0]=bn_mul_words(r,a,na,b[0]);
for (;;)
{
if (--nb <= 0) return;
rr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]);
if (--nb <= 0) return;
rr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]);
if (--nb <= 0) return;
rr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]);
if (--nb <= 0) return;
rr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]);
rr+=4;
r+=4;
b+=4;
}
}
|
['DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,\n\t int *counter_ret, unsigned long *h_ret, void (*callback)(),\n\t char *cb_arg)\n\t{\n\tint ok=0;\n\tunsigned char seed[SHA_DIGEST_LENGTH];\n\tunsigned char md[SHA_DIGEST_LENGTH];\n\tunsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH];\n\tBIGNUM *r0,*W,*X,*c,*test;\n\tBIGNUM *g=NULL,*q=NULL,*p=NULL;\n\tBN_MONT_CTX *mont=NULL;\n\tint k,n=0,i,b,m=0;\n\tint counter=0;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tunsigned int h=2;\n\tDSA *ret=NULL;\n\tif (bits < 512) bits=512;\n\tbits=(bits+63)/64*64;\n\tif ((seed_in != NULL) && (seed_len == 20))\n\t\tmemcpy(seed,seed_in,seed_len);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif ((ctx2=BN_CTX_new()) == NULL) goto err;\n\tif ((ret=DSA_new()) == NULL) goto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\tr0= &(ctx2->bn[0]);\n\tg= &(ctx2->bn[1]);\n\tW= &(ctx2->bn[2]);\n\tq= &(ctx2->bn[3]);\n\tX= &(ctx2->bn[4]);\n\tc= &(ctx2->bn[5]);\n\tp= &(ctx2->bn[6]);\n\ttest= &(ctx2->bn[7]);\n\tBN_lshift(test,BN_value_one(),bits-1);\n\tfor (;;)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (callback != NULL) callback(0,m++,cb_arg);\n\t\t\tif (!seed_len)\n\t\t\t\tRAND_bytes(seed,SHA_DIGEST_LENGTH);\n\t\t\telse\n\t\t\t\tseed_len=0;\n\t\t\tmemcpy(buf,seed,SHA_DIGEST_LENGTH);\n\t\t\tmemcpy(buf2,seed,SHA_DIGEST_LENGTH);\n\t\t\tfor (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)\n\t\t\t\t{\n\t\t\t\tbuf[i]++;\n\t\t\t\tif (buf[i] != 0) break;\n\t\t\t\t}\n\t\t\tHASH(seed,SHA_DIGEST_LENGTH,md);\n\t\t\tHASH(buf,SHA_DIGEST_LENGTH,buf2);\n\t\t\tfor (i=0; i<SHA_DIGEST_LENGTH; i++)\n\t\t\t\tmd[i]^=buf2[i];\n\t\t\tmd[0]|=0x80;\n\t\t\tmd[SHA_DIGEST_LENGTH-1]|=0x01;\n\t\t\tif (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) abort();\n\t\t\tif (DSA_is_prime(q,callback,cb_arg) > 0) break;\n\t\t\t}\n\t\tif (callback != NULL) callback(2,0,cb_arg);\n\t\tif (callback != NULL) callback(3,0,cb_arg);\n\t\tcounter=0;\n\t\tn=(bits-1)/160;\n\t\tb=(bits-1)-n*160;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tBN_zero(W);\n\t\t\tfor (k=0; k<=n; k++)\n\t\t\t\t{\n\t\t\t\tfor (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)\n\t\t\t\t\t{\n\t\t\t\t\tbuf[i]++;\n\t\t\t\t\tif (buf[i] != 0) break;\n\t\t\t\t\t}\n\t\t\t\tHASH(buf,SHA_DIGEST_LENGTH,md);\n\t\t\t\tif (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) abort();\n\t\t\t\tBN_lshift(r0,r0,160*k);\n\t\t\t\tBN_add(W,W,r0);\n\t\t\t\t}\n\t\t\tBN_mask_bits(W,bits-1);\n\t\t\tBN_copy(X,W);\n\t\t\tBN_add(X,X,test);\n\t\t\tBN_lshift1(r0,q);\n\t\t\tBN_mod(c,X,r0,ctx);\n\t\t\tBN_sub(r0,c,BN_value_one());\n\t\t\tBN_sub(p,X,r0);\n\t\t\tif (BN_cmp(p,test) >= 0)\n\t\t\t\t{\n\t\t\t\tif (DSA_is_prime(p,callback,cb_arg) > 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tcounter++;\n\t\t\tif (counter >= 4096) break;\n\t\t\tif (callback != NULL) callback(0,counter,cb_arg);\n\t\t\t}\n\t\t}\nend:\n\tif (callback != NULL) callback(2,1,cb_arg);\n BN_sub(test,p,BN_value_one());\n BN_div(r0,NULL,test,q,ctx);\n\tBN_set_word(test,h);\n\tBN_MONT_CTX_set(mont,p,ctx);\n\tfor (;;)\n\t\t{\n\t\tBN_mod_exp_mont(g,test,r0,p,ctx,mont);\n\t\tif (!BN_is_one(g)) break;\n\t\tBN_add(test,test,BN_value_one());\n\t\th++;\n\t\t}\n\tif (callback != NULL) callback(3,1,cb_arg);\n\tok=1;\nerr:\n\tif (!ok)\n\t\t{\n\t\tif (ret != NULL) DSA_free(ret);\n\t\t}\n\telse\n\t\t{\n\t\tret->p=BN_dup(p);\n\t\tret->q=BN_dup(q);\n\t\tret->g=BN_dup(g);\n\t\tif ((m > 1) && (seed_in != NULL)) memcpy(seed_in,seed,20);\n\t\tif (counter_ret != NULL) *counter_ret=counter;\n\t\tif (h_ret != NULL) *h_ret=h;\n\t\t}\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tif (ctx != NULL) BN_CTX_free(ctx2);\n\tif (mont != NULL) BN_MONT_CTX_free(mont);\n\treturn(ok?ret:NULL);\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n#ifdef BN_RECURSION_MONT\n\tif (mont->N.top < BN_MONT_CTX_SET_SIZE_WORD)\n#endif\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->use_word=1;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.max=mod->max;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n#if 1\n\t\t\tBN_sub_word(&Ri,1);\n#else\n\t\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\t\t}\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#ifdef BN_RECURSION_MONT\n\telse\n\t\t{\n\t\tmont->use_word=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if 1\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n#else\n\t\tBN_lshift(R,BN_value_one(),mont->ri);\n#endif\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n#if 1\n\t\tBN_sub_word(&Ri,1);\n#else\n\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n#if 1\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n#else\n\tBN_lshift(mont->RR,BN_value_one(),mont->ri*2);\n#endif\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#else\n\t\tw=0;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'int BN_set_bit(BIGNUM *a, int n)\n\t{\n\tint i,j,k;\n\ti=n/BN_BITS2;\n\tj=n%BN_BITS2;\n\tif (a->top <= i)\n\t\t{\n\t\tif (bn_wexpand(a,i+1) == NULL) return(0);\n\t\tfor(k=a->top; k<i+1; k++)\n\t\t\ta->d[k]=0;\n\t\ta->top=i+1;\n\t\t}\n\ta->d[i]|=(((BN_ULONG)1)<<j);\n\treturn(1);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *d,*r;\n\tBIGNUM *aa;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\td= &(ctx->bn[ctx->tos++]);\n\tr= &(ctx->bn[ctx->tos++]);\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n#if 1\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n#endif\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tBN_init(&val[0]);\n\tts=1;\n\tif (BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0]),a,m,ctx);\n\t\taa= &(val[0]);\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err;\n\tif (bits <= 20)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&(val[i]));\n\t\tif (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tctx->tos-=2;\n\tfor (i=0; i<ts; i++)\n\t\tBN_clear_free(&(val[i]));\n\treturn(ret);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n\t{\n\tBN_ULONG *rr;\n#ifdef BN_COUNT\nprintf(" bn_mul_normal %d * %d\\n",na,nb);\n#endif\n\tif (na < nb)\n\t\t{\n\t\tint itmp;\n\t\tBN_ULONG *ltmp;\n\t\titmp=na; na=nb; nb=itmp;\n\t\tltmp=a; a=b; b=ltmp;\n\t\t}\n\trr= &(r[na]);\n\trr[0]=bn_mul_words(r,a,na,b[0]);\n\tfor (;;)\n\t\t{\n\t\tif (--nb <= 0) return;\n\t\trr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]);\n\t\tif (--nb <= 0) return;\n\t\trr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]);\n\t\tif (--nb <= 0) return;\n\t\trr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]);\n\t\tif (--nb <= 0) return;\n\t\trr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]);\n\t\trr+=4;\n\t\tr+=4;\n\t\tb+=4;\n\t\t}\n\t}']
|
2,805
| 0
|
https://github.com/apache/httpd/blob/8fbe0892ec6e552a1cfe43a71c3e65b4ae8c5b9c/server/mpm/event/event.c/#L320
|
static void TO_QUEUE_REMOVE(struct timeout_queue *q, event_conn_state_t *el)
{
APR_RING_REMOVE(el, timeout_list);
APR_RING_ELEM_INIT(el, timeout_list);
--*q->total;
--q->count;
}
|
['static void process_lingering_close(event_conn_state_t *cs, const apr_pollfd_t *pfd)\n{\n apr_socket_t *csd = ap_get_conn_socket(cs->c);\n char dummybuf[2048];\n apr_size_t nbytes;\n apr_status_t rv;\n struct timeout_queue *q;\n q = (cs->pub.state == CONN_STATE_LINGER_SHORT) ? short_linger_q : linger_q;\n do {\n nbytes = sizeof(dummybuf);\n rv = apr_socket_recv(csd, dummybuf, &nbytes);\n } while (rv == APR_SUCCESS);\n if (APR_STATUS_IS_EAGAIN(rv)) {\n return;\n }\n apr_thread_mutex_lock(timeout_mutex);\n TO_QUEUE_REMOVE(q, cs);\n rv = apr_pollset_remove(event_pollset, pfd);\n apr_thread_mutex_unlock(timeout_mutex);\n AP_DEBUG_ASSERT(rv == APR_SUCCESS || APR_STATUS_IS_NOTFOUND(rv));\n rv = apr_socket_close(csd);\n AP_DEBUG_ASSERT(rv == APR_SUCCESS);\n ap_push_pool(worker_queue_info, cs->p);\n if (dying)\n ap_queue_interrupt_one(worker_queue);\n}', 'static void TO_QUEUE_REMOVE(struct timeout_queue *q, event_conn_state_t *el)\n{\n APR_RING_REMOVE(el, timeout_list);\n APR_RING_ELEM_INIT(el, timeout_list);\n --*q->total;\n --q->count;\n}']
|
2,806
| 0
|
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/x509/x509_vfy.c/#L425
|
int X509_cmp_current_time(ASN1_UTCTIME *ctm)
{
char *str;
ASN1_UTCTIME atm;
time_t offset;
char buff1[24],buff2[24],*p;
int i,j;
p=buff1;
i=ctm->length;
str=(char *)ctm->data;
if ((i < 11) || (i > 17)) return(0);
memcpy(p,str,10);
p+=10;
str+=10;
if ((*str == 'Z') || (*str == '-') || (*str == '+'))
{ *(p++)='0'; *(p++)='0'; }
else { *(p++)= *(str++); *(p++)= *(str++); }
*(p++)='Z';
*(p++)='\0';
if (*str == 'Z')
offset=0;
else
{
if ((*str != '+') && (str[5] != '-'))
return(0);
offset=((str[1]-'0')*10+(str[2]-'0'))*60;
offset+=(str[3]-'0')*10+(str[4]-'0');
if (*str == '-')
offset= -offset;
}
atm.type=V_ASN1_UTCTIME;
atm.length=sizeof(buff2);
atm.data=(unsigned char *)buff2;
X509_gmtime_adj(&atm,-offset);
i=(buff1[0]-'0')*10+(buff1[1]-'0');
if (i < 50) i+=100;
j=(buff2[0]-'0')*10+(buff2[1]-'0');
if (j < 50) j+=100;
if (i < j) return (-1);
if (i > j) return (1);
i=strcmp(buff1,buff2);
if (i == 0)
return(-1);
else
return(i);
}
|
['static int internal_verify(X509_STORE_CTX *ctx)\n\t{\n\tint i,ok=0,n;\n\tX509 *xs,*xi;\n\tEVP_PKEY *pkey=NULL;\n\tint (*cb)();\n\tcb=ctx->ctx->verify_cb;\n\tif (cb == NULL) cb=null_callback;\n\tn=sk_num(ctx->chain);\n\tctx->error_depth=n-1;\n\tn--;\n\txi=(X509 *)sk_value(ctx->chain,n);\n\tif (X509_NAME_cmp(X509_get_subject_name(xi),\n\t\tX509_get_issuer_name(xi)) == 0)\n\t\txs=xi;\n\telse\n\t\t{\n\t\tif (n <= 0)\n\t\t\t{\n\t\t\tctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;\n\t\t\tctx->current_cert=xi;\n\t\t\tok=cb(0,ctx);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tn--;\n\t\t\tctx->error_depth=n;\n\t\t\txs=(X509 *)sk_value(ctx->chain,n);\n\t\t\t}\n\t\t}\n\twhile (n >= 0)\n\t\t{\n\t\tctx->error_depth=n;\n\t\tif (!xs->valid)\n\t\t\t{\n\t\t\tif ((pkey=X509_get_pubkey(xi)) == NULL)\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;\n\t\t\t\tctx->current_cert=xi;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\tif (X509_verify(xs,pkey) <= 0)\n\t\t\t\t{\n\t\t\t\tEVP_PKEY_free(pkey);\n\t\t\t\tctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;\n\t\t\t\tctx->current_cert=xs;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\tEVP_PKEY_free(pkey);\n\t\t\tpkey=NULL;\n\t\t\ti=X509_cmp_current_time(X509_get_notBefore(xs));\n\t\t\tif (i == 0)\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;\n\t\t\t\tctx->current_cert=xs;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\tif (i > 0)\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_CERT_NOT_YET_VALID;\n\t\t\t\tctx->current_cert=xs;\n\t\t\t\tok=(*cb)(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\txs->valid=1;\n\t\t\t}\n\t\ti=X509_cmp_current_time(X509_get_notAfter(xs));\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;\n\t\t\tctx->current_cert=xs;\n\t\t\tok=(*cb)(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (i < 0)\n\t\t\t{\n\t\t\tctx->error=X509_V_ERR_CERT_HAS_EXPIRED;\n\t\t\tctx->current_cert=xs;\n\t\t\tok=(*cb)(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tctx->current_cert=xs;\n\t\tok=(*cb)(1,ctx);\n\t\tif (!ok) goto end;\n\t\tn--;\n\t\tif (n >= 0)\n\t\t\t{\n\t\t\txi=xs;\n\t\t\txs=(X509 *)sk_value(ctx->chain,n);\n\t\t\t}\n\t\t}\n\tok=1;\nend:\n\treturn(ok);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tlong j;\n\tint type;\n\tunsigned char *p;\n#ifndef NO_DSA\n\tX509_ALGOR *a;\n#endif\n\tif (key == NULL) goto err;\n\tif (key->pkey != NULL)\n\t {\n\t CRYPTO_add(&key->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);\n\t return(key->pkey);\n\t }\n\tif (key->public_key == NULL) goto err;\n\ttype=OBJ_obj2nid(key->algor->algorithm);\n\tp=key->public_key->data;\n j=key->public_key->length;\n if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB);\n\t\tgoto err;\n\t\t}\n\tret->save_parameters=0;\n#ifndef NO_DSA\n\ta=key->algor;\n\tif (ret->type == EVP_PKEY_DSA)\n\t\t{\n\t\tif (a->parameter->type == V_ASN1_SEQUENCE)\n\t\t\t{\n\t\t\tret->pkey.dsa->write_params=0;\n\t\t\tp=a->parameter->value.sequence->data;\n\t\t\tj=a->parameter->value.sequence->length;\n\t\t\tif (!d2i_DSAparams(&ret->pkey.dsa,&p,(long)j))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tret->save_parameters=1;\n\t\t}\n#endif\n\tkey->pkey=ret;\n\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_EVP_PKEY);\n\treturn(ret);\nerr:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'DSA *d2i_DSAparams(DSA **a, unsigned char **pp, long length)\n\t{\n\tint i=ERR_R_NESTED_ASN1_ERROR;\n\tASN1_INTEGER *bs=NULL;\n\tM_ASN1_D2I_vars(a,DSA *,DSA_new);\n\tM_ASN1_D2I_Init();\n\tM_ASN1_D2I_start_sequence();\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) goto err_bn;\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->q=BN_bin2bn(bs->data,bs->length,ret->q)) == NULL) goto err_bn;\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->g=BN_bin2bn(bs->data,bs->length,ret->g)) == NULL) goto err_bn;\n\tASN1_BIT_STRING_free(bs);\n\tM_ASN1_D2I_Finish_2(a);\nerr_bn:\n\ti=ERR_R_BN_LIB;\nerr:\n\tASN1err(ASN1_F_D2I_DSAPARAMS,i);\n\tif ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_free(ret);\n\tif (bs != NULL) ASN1_BIT_STRING_free(bs);\n\treturn(NULL);\n\t}', 'ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp,\n\t long length)\n\t{\n\tASN1_INTEGER *ret=NULL;\n\tunsigned char *p,*to,*s;\n\tlong len;\n\tint inf,tag,xclass;\n\tint i;\n\tif ((a == NULL) || ((*a) == NULL))\n\t\t{\n\t\tif ((ret=ASN1_INTEGER_new()) == NULL) return(NULL);\n\t\tret->type=V_ASN1_INTEGER;\n\t\t}\n\telse\n\t\tret=(*a);\n\tp= *pp;\n\tinf=ASN1_get_object(&p,&len,&tag,&xclass,length);\n\tif (inf & 0x80)\n\t\t{\n\t\ti=ASN1_R_BAD_OBJECT_HEADER;\n\t\tgoto err;\n\t\t}\n\tif (tag != V_ASN1_INTEGER)\n\t\t{\n\t\ti=ASN1_R_EXPECTING_AN_INTEGER;\n\t\tgoto err;\n\t\t}\n\ts=(unsigned char *)Malloc((int)len+1);\n\tif (s == NULL)\n\t\t{\n\t\ti=ERR_R_MALLOC_FAILURE;\n\t\tgoto err;\n\t\t}\n\tto=s;\n\tif (*p & 0x80)\n\t\t{\n\t\tret->type=V_ASN1_NEG_INTEGER;\n\t\tif (*p == 0xff)\n\t\t\t{\n\t\t\tp++;\n\t\t\tlen--;\n\t\t\t}\n\t\tfor (i=(int)len; i>0; i--)\n\t\t\t*(to++)= (*(p++)^0xFF)+1;\n\t\t}\n\telse\n\t\t{\n\t\tret->type=V_ASN1_INTEGER;\n\t\tif ((*p == 0) && (len != 1))\n\t\t\t{\n\t\t\tp++;\n\t\t\tlen--;\n\t\t\t}\n\t\tmemcpy(s,p,(int)len);\n\t\tp+=len;\n\t\t}\n\tif (ret->data != NULL) Free((char *)ret->data);\n\tret->data=s;\n\tret->length=(int)len;\n\tif (a != NULL) (*a)=ret;\n\t*pp=p;\n\treturn(ret);\nerr:\n\tASN1err(ASN1_F_D2I_ASN1_INTEGER,i);\n\tif ((ret != NULL) && ((a == NULL) || (*a != ret)))\n\t\tASN1_INTEGER_free(ret);\n\treturn(NULL);\n\t}', "int X509_cmp_current_time(ASN1_UTCTIME *ctm)\n\t{\n\tchar *str;\n\tASN1_UTCTIME atm;\n\ttime_t offset;\n\tchar buff1[24],buff2[24],*p;\n\tint i,j;\n\tp=buff1;\n\ti=ctm->length;\n\tstr=(char *)ctm->data;\n\tif ((i < 11) || (i > 17)) return(0);\n\tmemcpy(p,str,10);\n\tp+=10;\n\tstr+=10;\n\tif ((*str == 'Z') || (*str == '-') || (*str == '+'))\n\t\t{ *(p++)='0'; *(p++)='0'; }\n\telse\t{ *(p++)= *(str++); *(p++)= *(str++); }\n\t*(p++)='Z';\n\t*(p++)='\\0';\n\tif (*str == 'Z')\n\t\toffset=0;\n\telse\n\t\t{\n\t\tif ((*str != '+') && (str[5] != '-'))\n\t\t\treturn(0);\n\t\toffset=((str[1]-'0')*10+(str[2]-'0'))*60;\n\t\toffset+=(str[3]-'0')*10+(str[4]-'0');\n\t\tif (*str == '-')\n\t\t\toffset= -offset;\n\t\t}\n\tatm.type=V_ASN1_UTCTIME;\n\tatm.length=sizeof(buff2);\n\tatm.data=(unsigned char *)buff2;\n\tX509_gmtime_adj(&atm,-offset);\n\ti=(buff1[0]-'0')*10+(buff1[1]-'0');\n\tif (i < 50) i+=100;\n\tj=(buff2[0]-'0')*10+(buff2[1]-'0');\n\tif (j < 50) j+=100;\n\tif (i < j) return (-1);\n\tif (i > j) return (1);\n\ti=strcmp(buff1,buff2);\n\tif (i == 0)\n\t\treturn(-1);\n\telse\n\t\treturn(i);\n\t}"]
|
2,807
| 0
|
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L342
|
static void BN_POOL_release(BN_POOL *p, unsigned int num)
{
unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
p->used -= num;
while (num--) {
bn_check_top(p->current->vals + offset);
if (offset == 0) {
offset = BN_CTX_POOL_SIZE - 1;
p->current = p->current->prev;
} else
offset--;
}
}
|
['int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;\n if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) {\n if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)\n return 0;\n return 1;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n p1 = BN_CTX_get(ctx);\n q1 = BN_CTX_get(ctx);\n ret = (q1 != NULL)\n && (BN_copy(p1, rsa->p) != NULL)\n && BN_sub_word(p1, 1)\n && (BN_copy(q1, rsa->q) != NULL)\n && BN_sub_word(q1, 1)\n && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)\n && (BN_cmp(rsa->dmp1, p1) < 0)\n && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)\n && (BN_cmp(rsa->dmq1, q1) < 0)\n && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)\n && (BN_cmp(rsa->iqmp, rsa->p) < 0)\n && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)\n && BN_is_one(r)\n && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)\n && BN_is_one(r)\n && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)\n && BN_is_one(r);\n BN_clear(p1);\n BN_clear(q1);\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = bn_mul_fixed_top(r, a, b, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n{\n unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n p->used -= num;\n while (num--) {\n bn_check_top(p->current->vals + offset);\n if (offset == 0) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
|
2,808
| 0
|
https://github.com/libav/libav/blob/9104cd5161ec7cb31361f3dabd73a8a813d4f7d0/libavformat/rmdec.c/#L826
|
int
ff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,
AVStream *st, RMStream *ast, AVPacket *pkt)
{
RMDemuxContext *rm = s->priv_data;
assert (rm->audio_pkt_cnt > 0);
if (st->codec->codec_id == CODEC_ID_AAC)
av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
else {
av_new_packet(pkt, st->codec->block_align);
memcpy(pkt->data, ast->pkt.data + st->codec->block_align *
(ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
st->codec->block_align);
}
rm->audio_pkt_cnt--;
if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
ast->audiotimestamp = AV_NOPTS_VALUE;
pkt->flags = PKT_FLAG_KEY;
} else
pkt->flags = 0;
pkt->stream_index = st->index;
return rm->audio_pkt_cnt;
}
|
['int\nff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,\n AVStream *st, RMStream *ast, AVPacket *pkt)\n{\n RMDemuxContext *rm = s->priv_data;\n assert (rm->audio_pkt_cnt > 0);\n if (st->codec->codec_id == CODEC_ID_AAC)\n av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);\n else {\n av_new_packet(pkt, st->codec->block_align);\n memcpy(pkt->data, ast->pkt.data + st->codec->block_align *\n (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),\n st->codec->block_align);\n }\n rm->audio_pkt_cnt--;\n if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {\n ast->audiotimestamp = AV_NOPTS_VALUE;\n pkt->flags = PKT_FLAG_KEY;\n } else\n pkt->flags = 0;\n pkt->stream_index = st->index;\n return rm->audio_pkt_cnt;\n}', 'int av_new_packet(AVPacket *pkt, int size)\n{\n uint8_t *data= NULL;\n if((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)\n data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (data){\n memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);\n }else\n size=0;\n av_init_packet(pkt);\n pkt->data = data;\n pkt->size = size;\n pkt->destruct = av_destruct_packet;\n if(!data)\n return AVERROR(ENOMEM);\n return 0;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'void av_init_packet(AVPacket *pkt)\n{\n pkt->pts = AV_NOPTS_VALUE;\n pkt->dts = AV_NOPTS_VALUE;\n pkt->pos = -1;\n pkt->duration = 0;\n pkt->convergence_duration = 0;\n pkt->flags = 0;\n pkt->stream_index = 0;\n pkt->destruct= NULL;\n}']
|
2,809
| 0
|
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
|
2,810
| 0
|
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_mul.c/#L1071
|
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
{
BN_ULONG *rr;
if (na < nb) {
int itmp;
BN_ULONG *ltmp;
itmp = na;
na = nb;
nb = itmp;
ltmp = a;
a = b;
b = ltmp;
}
rr = &(r[na]);
if (nb <= 0) {
(void)bn_mul_words(r, a, na, 0);
return;
} else
rr[0] = bn_mul_words(r, a, na, b[0]);
for (;;) {
if (--nb <= 0)
return;
rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
if (--nb <= 0)
return;
rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
if (--nb <= 0)
return;
rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
if (--nb <= 0)
return;
rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
rr += 4;
r += 4;
b += 4;
}
}
|
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n rr->neg = a->neg ^ b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n{\n BN_ULONG *rr;\n if (na < nb) {\n int itmp;\n BN_ULONG *ltmp;\n itmp = na;\n na = nb;\n nb = itmp;\n ltmp = a;\n a = b;\n b = ltmp;\n }\n rr = &(r[na]);\n if (nb <= 0) {\n (void)bn_mul_words(r, a, na, 0);\n return;\n } else\n rr[0] = bn_mul_words(r, a, na, b[0]);\n for (;;) {\n if (--nb <= 0)\n return;\n rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);\n if (--nb <= 0)\n return;\n rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);\n if (--nb <= 0)\n return;\n rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);\n if (--nb <= 0)\n return;\n rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);\n rr += 4;\n r += 4;\n b += 4;\n }\n}']
|
2,811
| 0
|
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/bn/bn_ctx.c/#L268
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,812
| 0
|
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int ec_key_simple_generate_key(EC_KEY *eckey)\n{\n int ok = 0;\n BN_CTX *ctx = NULL;\n BIGNUM *priv_key = NULL;\n const BIGNUM *order = NULL;\n EC_POINT *pub_key = NULL;\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n if (eckey->priv_key == NULL) {\n priv_key = BN_new();\n if (priv_key == NULL)\n goto err;\n } else\n priv_key = eckey->priv_key;\n order = EC_GROUP_get0_order(eckey->group);\n if (order == NULL)\n goto err;\n do\n if (!BN_priv_rand_range(priv_key, order))\n goto err;\n while (BN_is_zero(priv_key)) ;\n if (eckey->pub_key == NULL) {\n pub_key = EC_POINT_new(eckey->group);\n if (pub_key == NULL)\n goto err;\n } else\n pub_key = eckey->pub_key;\n if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))\n goto err;\n eckey->priv_key = priv_key;\n eckey->pub_key = pub_key;\n ok = 1;\n err:\n if (eckey->pub_key == NULL)\n EC_POINT_free(pub_key);\n if (eckey->priv_key != priv_key)\n BN_free(priv_key);\n BN_CTX_free(ctx);\n return ok;\n}', 'int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)\n{\n return bnrand_range(PRIVATE, r, range);\n}', 'static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)\n{\n int b, n;\n int count = 100;\n if (range->neg || BN_is_zero(range)) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_INVALID_RANGE);\n return 0;\n }\n n = BN_num_bits(range);\n if (n == 1)\n BN_zero(r);\n else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {\n do {\n b = flag == NORMAL\n ? BN_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)\n : BN_priv_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);\n if (!b)\n return 0;\n if (BN_cmp(r, range) >= 0) {\n if (!BN_sub(r, r, range))\n return 0;\n if (BN_cmp(r, range) >= 0)\n if (!BN_sub(r, r, range))\n return 0;\n }\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n } else {\n do {\n if (!BN_rand(r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n return 0;\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n }\n bn_check_top(r);\n return 1;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,813
| 0
|
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_sqr.c/#L118
|
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
}
|
['int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
|
2,814
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_sqr.c/#L168
|
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
}
|
['int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a;\n const BIGNUM *ca;\n BN_CTX_start(ctx);\n if ((a = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (y != NULL) {\n if (x == y) {\n if (!BN_sqr(a, x, ctx))\n goto err;\n } else {\n if (!BN_mul(a, x, y, ctx))\n goto err;\n }\n ca = a;\n } else\n ca = x;\n ret = BN_div_recp(NULL, r, ca, recp, ctx);\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !tmp)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
|
2,815
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/mpegaudioenc.c/#L735
|
static void encode_frame(MpegAudioContext *s,
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
int padding)
{
int i, j, k, l, bit_alloc_bits, b, ch;
unsigned char *sf;
int q[3];
PutBitContext *p = &s->pb;
put_bits(p, 12, 0xfff);
put_bits(p, 1, 1 - s->lsf);
put_bits(p, 2, 4-2);
put_bits(p, 1, 1);
put_bits(p, 4, s->bitrate_index);
put_bits(p, 2, s->freq_index);
put_bits(p, 1, s->do_padding);
put_bits(p, 1, 0);
put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
put_bits(p, 2, 0);
put_bits(p, 1, 0);
put_bits(p, 1, 1);
put_bits(p, 2, 0);
j = 0;
for(i=0;i<s->sblimit;i++) {
bit_alloc_bits = s->alloc_table[j];
for(ch=0;ch<s->nb_channels;ch++) {
put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
}
j += 1 << bit_alloc_bits;
}
for(i=0;i<s->sblimit;i++) {
for(ch=0;ch<s->nb_channels;ch++) {
if (bit_alloc[ch][i])
put_bits(p, 2, s->scale_code[ch][i]);
}
}
for(i=0;i<s->sblimit;i++) {
for(ch=0;ch<s->nb_channels;ch++) {
if (bit_alloc[ch][i]) {
sf = &s->scale_factors[ch][i][0];
switch(s->scale_code[ch][i]) {
case 0:
put_bits(p, 6, sf[0]);
put_bits(p, 6, sf[1]);
put_bits(p, 6, sf[2]);
break;
case 3:
case 1:
put_bits(p, 6, sf[0]);
put_bits(p, 6, sf[2]);
break;
case 2:
put_bits(p, 6, sf[0]);
break;
}
}
}
}
for(k=0;k<3;k++) {
for(l=0;l<12;l+=3) {
j = 0;
for(i=0;i<s->sblimit;i++) {
bit_alloc_bits = s->alloc_table[j];
for(ch=0;ch<s->nb_channels;ch++) {
b = bit_alloc[ch][i];
if (b) {
int qindex, steps, m, sample, bits;
qindex = s->alloc_table[j+b];
steps = ff_mpa_quant_steps[qindex];
for(m=0;m<3;m++) {
sample = s->sb_samples[ch][k][l + m][i];
#ifdef USE_FLOATS
{
float a;
a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
q[m] = (int)((a + 1.0) * steps * 0.5);
}
#else
{
int q1, e, shift, mult;
e = s->scale_factors[ch][i][k];
shift = scale_factor_shift[e];
mult = scale_factor_mult[e];
if (shift < 0)
q1 = sample << (-shift);
else
q1 = sample >> shift;
q1 = (q1 * mult) >> P;
q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
}
#endif
if (q[m] >= steps)
q[m] = steps - 1;
assert(q[m] >= 0 && q[m] < steps);
}
bits = ff_mpa_quant_bits[qindex];
if (bits < 0) {
put_bits(p, -bits,
q[0] + steps * (q[1] + steps * q[2]));
#if 0
printf("%d: gr1 %d\n",
i, q[0] + steps * (q[1] + steps * q[2]));
#endif
} else {
#if 0
printf("%d: gr3 %d %d %d\n",
i, q[0], q[1], q[2]);
#endif
put_bits(p, bits, q[0]);
put_bits(p, bits, q[1]);
put_bits(p, bits, q[2]);
}
}
}
j += 1 << bit_alloc_bits;
}
}
}
for(i=0;i<padding;i++)
put_bits(p, 1, 0);
flush_put_bits(p);
}
|
['static void encode_frame(MpegAudioContext *s,\n unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],\n int padding)\n{\n int i, j, k, l, bit_alloc_bits, b, ch;\n unsigned char *sf;\n int q[3];\n PutBitContext *p = &s->pb;\n put_bits(p, 12, 0xfff);\n put_bits(p, 1, 1 - s->lsf);\n put_bits(p, 2, 4-2);\n put_bits(p, 1, 1);\n put_bits(p, 4, s->bitrate_index);\n put_bits(p, 2, s->freq_index);\n put_bits(p, 1, s->do_padding);\n put_bits(p, 1, 0);\n put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);\n put_bits(p, 2, 0);\n put_bits(p, 1, 0);\n put_bits(p, 1, 1);\n put_bits(p, 2, 0);\n j = 0;\n for(i=0;i<s->sblimit;i++) {\n bit_alloc_bits = s->alloc_table[j];\n for(ch=0;ch<s->nb_channels;ch++) {\n put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);\n }\n j += 1 << bit_alloc_bits;\n }\n for(i=0;i<s->sblimit;i++) {\n for(ch=0;ch<s->nb_channels;ch++) {\n if (bit_alloc[ch][i])\n put_bits(p, 2, s->scale_code[ch][i]);\n }\n }\n for(i=0;i<s->sblimit;i++) {\n for(ch=0;ch<s->nb_channels;ch++) {\n if (bit_alloc[ch][i]) {\n sf = &s->scale_factors[ch][i][0];\n switch(s->scale_code[ch][i]) {\n case 0:\n put_bits(p, 6, sf[0]);\n put_bits(p, 6, sf[1]);\n put_bits(p, 6, sf[2]);\n break;\n case 3:\n case 1:\n put_bits(p, 6, sf[0]);\n put_bits(p, 6, sf[2]);\n break;\n case 2:\n put_bits(p, 6, sf[0]);\n break;\n }\n }\n }\n }\n for(k=0;k<3;k++) {\n for(l=0;l<12;l+=3) {\n j = 0;\n for(i=0;i<s->sblimit;i++) {\n bit_alloc_bits = s->alloc_table[j];\n for(ch=0;ch<s->nb_channels;ch++) {\n b = bit_alloc[ch][i];\n if (b) {\n int qindex, steps, m, sample, bits;\n qindex = s->alloc_table[j+b];\n steps = ff_mpa_quant_steps[qindex];\n for(m=0;m<3;m++) {\n sample = s->sb_samples[ch][k][l + m][i];\n#ifdef USE_FLOATS\n {\n float a;\n a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];\n q[m] = (int)((a + 1.0) * steps * 0.5);\n }\n#else\n {\n int q1, e, shift, mult;\n e = s->scale_factors[ch][i][k];\n shift = scale_factor_shift[e];\n mult = scale_factor_mult[e];\n if (shift < 0)\n q1 = sample << (-shift);\n else\n q1 = sample >> shift;\n q1 = (q1 * mult) >> P;\n q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);\n }\n#endif\n if (q[m] >= steps)\n q[m] = steps - 1;\n assert(q[m] >= 0 && q[m] < steps);\n }\n bits = ff_mpa_quant_bits[qindex];\n if (bits < 0) {\n put_bits(p, -bits,\n q[0] + steps * (q[1] + steps * q[2]));\n#if 0\n printf("%d: gr1 %d\\n",\n i, q[0] + steps * (q[1] + steps * q[2]));\n#endif\n } else {\n#if 0\n printf("%d: gr3 %d %d %d\\n",\n i, q[0], q[1], q[2]);\n#endif\n put_bits(p, bits, q[0]);\n put_bits(p, bits, q[1]);\n put_bits(p, bits, q[2]);\n }\n }\n }\n j += 1 << bit_alloc_bits;\n }\n }\n }\n for(i=0;i<padding;i++)\n put_bits(p, 1, 0);\n flush_put_bits(p);\n}']
|
2,816
| 0
|
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L270
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,\n int strength, int nbits)\n{\n int ret = 0;\n BN_CTX *ctx = NULL;\n BIGNUM *r = NULL;\n if (rsa->p == NULL\n || rsa->q == NULL\n || rsa->e == NULL\n || rsa->d == NULL\n || rsa->n == NULL) {\n RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);\n return 0;\n }\n if (!rsa_sp800_56b_validate_strength(nbits, strength))\n return 0;\n if (efixed != NULL) {\n if (BN_cmp(efixed, rsa->e) != 0) {\n RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);\n return 0;\n }\n }\n if (!rsa_check_public_exponent(rsa->e)) {\n RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR,\n RSA_R_PUB_EXPONENT_OUT_OF_RANGE);\n return 0;\n }\n if (nbits != BN_num_bits(rsa->n)) {\n RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);\n return 0;\n }\n ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n if (r == NULL || !BN_mul(r, rsa->p, rsa->q, ctx))\n goto err;\n if (BN_cmp(rsa->n, r) != 0) {\n RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);\n goto err;\n }\n ret = rsa_check_prime_factor(rsa->p, rsa->e, nbits, ctx)\n && rsa_check_prime_factor(rsa->q, rsa->e, nbits, ctx)\n && (rsa_check_pminusq_diff(r, rsa->p, rsa->q, nbits) > 0)\n && rsa_check_private_exponent(rsa, nbits, ctx)\n && rsa_check_crt_components(rsa, ctx);\n if (ret != 1)\n RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);\nerr:\n BN_clear(r);\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;\n if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) {\n if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)\n return 0;\n return 1;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n p1 = BN_CTX_get(ctx);\n q1 = BN_CTX_get(ctx);\n ret = (q1 != NULL)\n && (BN_copy(p1, rsa->p) != NULL)\n && BN_sub_word(p1, 1)\n && (BN_copy(q1, rsa->q) != NULL)\n && BN_sub_word(q1, 1)\n && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)\n && (BN_cmp(rsa->dmp1, p1) < 0)\n && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)\n && (BN_cmp(rsa->dmq1, q1) < 0)\n && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)\n && (BN_cmp(rsa->iqmp, rsa->p) < 0)\n && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)\n && BN_is_one(r)\n && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)\n && BN_is_one(r)\n && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)\n && BN_is_one(r);\n BN_clear(p1);\n BN_clear(q1);\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,817
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/elbg.c/#L170
|
static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i,
int *newcentroid_p)
{
cell *tempcell;
int min[elbg->dim];
int max[elbg->dim];
int i;
for (i=0; i< elbg->dim; i++) {
min[i]=INT_MAX;
max[i]=0;
}
for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
for(i=0; i<elbg->dim; i++) {
min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
}
for (i=0; i<elbg->dim; i++) {
newcentroid_i[i] = min[i] + (max[i] - min[i])/3;
newcentroid_p[i] = min[i] + (2*(max[i] - min[i]))/3;
}
}
|
['static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i,\n int *newcentroid_p)\n{\n cell *tempcell;\n int min[elbg->dim];\n int max[elbg->dim];\n int i;\n for (i=0; i< elbg->dim; i++) {\n min[i]=INT_MAX;\n max[i]=0;\n }\n for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)\n for(i=0; i<elbg->dim; i++) {\n min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);\n max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);\n }\n for (i=0; i<elbg->dim; i++) {\n newcentroid_i[i] = min[i] + (max[i] - min[i])/3;\n newcentroid_p[i] = min[i] + (2*(max[i] - min[i]))/3;\n }\n}']
|
2,818
| 0
|
https://github.com/openssl/openssl/blob/ff80280b017ffcb045141a7863013c4af56e26fc/crypto/lhash/lhash.c/#L240
|
void *lh_delete(LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
void *ret;
lh->error=0;
rn=getrn(lh,data,&hash);
if (*rn == NULL)
{
lh->num_no_delete++;
return(NULL);
}
else
{
nn= *rn;
*rn=nn->next;
ret=nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))
contract(lh);
return(ret);
}
|
['static APP_INFO *pop_info(void)\n\t{\n\tAPP_INFO tmp;\n\tAPP_INFO *ret = NULL;\n\tif (amih != NULL)\n\t\t{\n\t\tCRYPTO_THREADID_set(&tmp.threadid);\n\t\tif ((ret=(APP_INFO *)lh_delete(amih,&tmp)) != NULL)\n\t\t\t{\n\t\t\tAPP_INFO *next=ret->next;\n\t\t\tif (next != NULL)\n\t\t\t\t{\n\t\t\t\tnext->references++;\n\t\t\t\tlh_insert(amih,(char *)next);\n\t\t\t\t}\n#ifdef LEVITTE_DEBUG_MEM\n\t\t\tif (CRYPTO_THREADID_cmp(&ret->threadid, &tmp.threadid))\n\t\t\t\t{\n\t\t\t\tfprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\\n",\n\t\t\t\t\tCRYPTO_THREADID_hash(&ret->threadid),\n\t\t\t\t\tCRYPTO_THREADID_hash(&tmp.threadid));\n\t\t\t\tabort();\n\t\t\t\t}\n#endif\n\t\t\tif (--(ret->references) <= 0)\n\t\t\t\t{\n\t\t\t\tret->next = NULL;\n\t\t\t\tif (next != NULL)\n\t\t\t\t\tnext->references--;\n\t\t\t\tOPENSSL_free(ret);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
|
2,819
| 0
|
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L377
|
BIGNUM *bn_expand2(BIGNUM *b, int words)
{
BN_ULONG *A,*B,*a;
int i,j;
bn_check_top(b);
if (words > b->max)
{
bn_check_top(b);
if (BN_get_flags(b,BN_FLG_STATIC_DATA))
{
BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return(NULL);
}
a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));
if (A == NULL)
{
BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);
return(NULL);
}
memset(A,0x5c,sizeof(BN_ULONG)*(words+1));
#if 1
B=b->d;
if (B != NULL)
{
for (i=b->top&(~7); i>0; i-=8)
{
A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];
A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];
A+=8;
B+=8;
}
switch (b->top&7)
{
case 7:
A[6]=B[6];
case 6:
A[5]=B[5];
case 5:
A[4]=B[4];
case 4:
A[3]=B[3];
case 3:
A[2]=B[2];
case 2:
A[1]=B[1];
case 1:
A[0]=B[0];
case 0:
;
}
Free(b->d);
}
b->d=a;
b->max=words;
B= &(b->d[b->top]);
j=(b->max - b->top) & ~7;
for (i=0; i<j; i+=8)
{
B[0]=0; B[1]=0; B[2]=0; B[3]=0;
B[4]=0; B[5]=0; B[6]=0; B[7]=0;
B+=8;
}
j=(b->max - b->top) & 7;
for (i=0; i<j; i++)
{
B[0]=0;
B++;
}
#else
memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
#endif
}
return(b);
}
|
['int test_mod_exp(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b,*c,*d,*e;\n\tint i;\n\ta=BN_new();\n\tb=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\te=BN_new();\n\tBN_rand(c,30,0,1);\n\tfor (i=0; i<6; i++)\n\t\t{\n\t\tBN_rand(a,20+i*5,0,0);\n\t\tBN_rand(b,2+i,0,0);\n\t\tif (!BN_mod_exp(d,a,b,c,ctx))\n\t\t\treturn(00);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,a);\n\t\t\t\tBIO_puts(bp," ^ ");\n\t\t\t\tBN_print(bp,b);\n\t\t\t\tBIO_puts(bp," % ");\n\t\t\t\tBN_print(bp,c);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,d);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\t}\n\tBN_free(a);\n\tBN_free(b);\n\tBN_free(c);\n\tBN_free(d);\n\tBN_free(e);\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'int BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{ ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx,\n\t BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *d,*aa,*r;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\td= &(ctx->bn[ctx->tos++]);\n\tr= &(ctx->bn[ctx->tos++]);\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n#if 1\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n#endif\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tBN_init(&val[0]);\n\tts=1;\n\tif (BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0]),a,m,ctx);\n\t\taa= &(val[0]);\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err;\n\tif (bits <= 20)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&(val[i]));\n\t\tif (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tctx->tos-=2;\n\tfor (i=0; i<ts; i++)\n\t\tBN_clear_free(&(val[i]));\n\treturn(ret);\n\t}', 'int BN_mod(BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tdv= &(ctx->bn[ctx->tos]);\n\tif (!BN_copy(rem,m)) return(0);\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) return(0);\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) return(0);\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) return(0);\n\t\t}\n\treturn(1);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, BIGNUM *num, BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\ttmp= &(ctx->bn[ctx->tos]);\n\ttmp->neg=0;\n\tsnum= &(ctx->bn[ctx->tos+1]);\n\tsdiv= &(ctx->bn[ctx->tos+2]);\n\tif (dv == NULL)\n\t\tres= &(ctx->bn[ctx->tos+3]);\n\telse\tres=dv;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.max= snum->max+1;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,n0,n1;\n\t\tBN_ULONG l0;\n\t\twnum.d--; wnum.top++;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\tq=bn_div_words(n0,n1,d0);\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t1,t2,rem;\n\t\tt1=((BN_ULLONG)n0<<BN_BITS2)|n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\trem=t1-(BN_ULLONG)q*d0;\n\t\t\tif ((rem>>BN_BITS2) ||\n\t\t\t\t(t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\t}\n#else\n\t\tBN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;\n\t\tt1h=n0;\n\t\tt1l=n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\tt3t=LBITS(d0); t3h=HBITS(d0);\n\t\t\tmul64(t3t,t3h,ql,qh);\n\t\t\tt3l=(t1l-t3t)&BN_MASK2;\n\t\t\tif (t3l > t1l) t3h++;\n\t\t\tt3h=(t1h-t3h)&BN_MASK2;\n\t\t\tif (t3h) break;\n\t\t\tif (t2h < t3l) break;\n\t\t\tif ((t2h == t3l) && (t2l <= wnump[-2])) break;\n\t\t\tq--;\n\t\t\t}\n#endif\n\t\t}\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
|
2,820
| 0
|
https://github.com/openssl/openssl/blob/176f31ddec84a51d35871dc021a013df9f3cbccd/ssl/s3_srvr.c/#L2104
|
static int ssl3_get_client_key_exchange(SSL *s)
{
int i,al,ok;
long n;
unsigned long l;
unsigned char *p;
#ifndef OPENSSL_NO_RSA
RSA *rsa=NULL;
EVP_PKEY *pkey=NULL;
#endif
#ifndef OPENSSL_NO_DH
BIGNUM *pub=NULL;
DH *dh_srvr;
#endif
#ifndef OPENSSL_NO_KRB5
KSSL_ERR kssl_err;
#endif
#ifndef OPENSSL_NO_ECDH
EC_KEY *srvr_ecdh = NULL;
EVP_PKEY *clnt_pub_pkey = NULL;
EC_POINT *clnt_ecpoint = NULL;
BN_CTX *bn_ctx = NULL;
#endif
n=ssl3_get_message(s,
SSL3_ST_SR_KEY_EXCH_A,
SSL3_ST_SR_KEY_EXCH_B,
SSL3_MT_CLIENT_KEY_EXCHANGE,
2048,
&ok);
if (!ok) return((int)n);
p=(unsigned char *)s->init_msg;
l=s->s3->tmp.new_cipher->algorithms;
#ifndef OPENSSL_NO_RSA
if (l & SSL_kRSA)
{
if (s->s3->tmp.use_rsa_tmp)
{
if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
rsa=s->cert->rsa_tmp;
if (rsa == NULL)
{
al=SSL_AD_HANDSHAKE_FAILURE;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
goto f_err;
}
}
else
{
pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
if ( (pkey == NULL) ||
(pkey->type != EVP_PKEY_RSA) ||
(pkey->pkey.rsa == NULL))
{
al=SSL_AD_HANDSHAKE_FAILURE;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
goto f_err;
}
rsa=pkey->pkey.rsa;
}
if (s->version > SSL3_VERSION)
{
n2s(p,i);
if (n != i+2)
{
if (!(s->options & SSL_OP_TLS_D5_BUG))
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
goto err;
}
else
p-=2;
}
else
n=i;
}
i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
al = -1;
if (i != SSL_MAX_MASTER_KEY_LENGTH)
{
al=SSL_AD_DECODE_ERROR;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
}
if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
{
if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
(p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
{
al=SSL_AD_DECODE_ERROR;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
goto f_err;
}
}
if (al != -1)
{
#if 0
goto f_err;
#else
ERR_clear_error();
i = SSL_MAX_MASTER_KEY_LENGTH;
p[0] = s->client_version >> 8;
p[1] = s->client_version & 0xff;
RAND_pseudo_bytes(p+2, i-2);
#endif
}
s->session->master_key_length=
s->method->ssl3_enc->generate_master_secret(s,
s->session->master_key,
p,i);
OPENSSL_cleanse(p,i);
}
else
#endif
#ifndef OPENSSL_NO_DH
if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
{
n2s(p,i);
if (n != i+2)
{
if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
goto err;
}
else
{
p-=2;
i=(int)n;
}
}
if (n == 0L)
{
al=SSL_AD_HANDSHAKE_FAILURE;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
goto f_err;
}
else
{
if (s->s3->tmp.dh == NULL)
{
al=SSL_AD_HANDSHAKE_FAILURE;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
goto f_err;
}
else
dh_srvr=s->s3->tmp.dh;
}
pub=BN_bin2bn(p,i,NULL);
if (pub == NULL)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
goto err;
}
i=DH_compute_key(p,pub,dh_srvr);
if (i <= 0)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
goto err;
}
DH_free(s->s3->tmp.dh);
s->s3->tmp.dh=NULL;
BN_clear_free(pub);
pub=NULL;
s->session->master_key_length=
s->method->ssl3_enc->generate_master_secret(s,
s->session->master_key,p,i);
OPENSSL_cleanse(p,i);
}
else
#endif
#ifndef OPENSSL_NO_KRB5
if (l & SSL_kKRB5)
{
krb5_error_code krb5rc;
krb5_data enc_ticket;
krb5_data authenticator;
krb5_data enc_pms;
KSSL_CTX *kssl_ctx = s->kssl_ctx;
EVP_CIPHER_CTX ciph_ctx;
EVP_CIPHER *enc = NULL;
unsigned char iv[EVP_MAX_IV_LENGTH];
unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
+ EVP_MAX_BLOCK_LENGTH];
int padl, outl;
krb5_timestamp authtime = 0;
krb5_ticket_times ttimes;
EVP_CIPHER_CTX_init(&ciph_ctx);
if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
n2s(p,i);
enc_ticket.length = i;
enc_ticket.data = (char *)p;
p+=enc_ticket.length;
n2s(p,i);
authenticator.length = i;
authenticator.data = (char *)p;
p+=authenticator.length;
n2s(p,i);
enc_pms.length = i;
enc_pms.data = (char *)p;
p+=enc_pms.length;
if(enc_pms.length > sizeof pms)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_DATA_LENGTH_TOO_LONG);
goto err;
}
if (n != enc_ticket.length + authenticator.length +
enc_pms.length + 6)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_DATA_LENGTH_TOO_LONG);
goto err;
}
if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
&kssl_err)) != 0)
{
#ifdef KSSL_DEBUG
printf("kssl_sget_tkt rtn %d [%d]\n",
krb5rc, kssl_err.reason);
if (kssl_err.text)
printf("kssl_err text= %s\n", kssl_err.text);
#endif
SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
kssl_err.reason);
goto err;
}
if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
&authtime, &kssl_err)) != 0)
{
#ifdef KSSL_DEBUG
printf("kssl_check_authent rtn %d [%d]\n",
krb5rc, kssl_err.reason);
if (kssl_err.text)
printf("kssl_err text= %s\n", kssl_err.text);
#endif
SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
kssl_err.reason);
goto err;
}
if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
{
SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
goto err;
}
#ifdef KSSL_DEBUG
kssl_ctx_show(kssl_ctx);
#endif
enc = kssl_map_enc(kssl_ctx->enctype);
if (enc == NULL)
goto err;
memset(iv, 0, sizeof iv);
if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_DECRYPTION_FAILED);
goto err;
}
if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
(unsigned char *)enc_pms.data, enc_pms.length))
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_DECRYPTION_FAILED);
goto err;
}
if (outl > SSL_MAX_MASTER_KEY_LENGTH)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_DATA_LENGTH_TOO_LONG);
goto err;
}
if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_DECRYPTION_FAILED);
goto err;
}
outl += padl;
if (outl > SSL_MAX_MASTER_KEY_LENGTH)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_DATA_LENGTH_TOO_LONG);
goto err;
}
EVP_CIPHER_CTX_cleanup(&ciph_ctx);
s->session->master_key_length=
s->method->ssl3_enc->generate_master_secret(s,
s->session->master_key, pms, outl);
if (kssl_ctx->client_princ)
{
int len = strlen(kssl_ctx->client_princ);
if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
{
s->session->krb5_client_princ_len = len;
memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
}
}
}
else
#endif
#ifndef OPENSSL_NO_ECDH
if ((l & SSL_kECDH) || (l & SSL_kECDHE))
{
int ret = 1;
if ((srvr_ecdh = EC_KEY_new()) == NULL)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
ERR_R_MALLOC_FAILURE);
goto err;
}
if (l & SSL_kECDH)
{
srvr_ecdh->group = s->cert->key->privatekey-> \
pkey.eckey->group;
srvr_ecdh->priv_key = s->cert->key->privatekey-> \
pkey.eckey->priv_key;
}
else
{
srvr_ecdh->group = s->s3->tmp.ecdh->group;
srvr_ecdh->priv_key = s->s3->tmp.ecdh->priv_key;
}
if ((clnt_ecpoint = EC_POINT_new(srvr_ecdh->group))
== NULL)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
ERR_R_MALLOC_FAILURE);
goto err;
}
if (n == 0L)
{
if (l & SSL_kECDHE)
{
al=SSL_AD_HANDSHAKE_FAILURE;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
goto f_err;
}
if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
== NULL) ||
(clnt_pub_pkey->type != EVP_PKEY_EC))
{
al=SSL_AD_HANDSHAKE_FAILURE;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
goto f_err;
}
EC_POINT_copy(clnt_ecpoint,
clnt_pub_pkey->pkey.eckey->pub_key);
ret = 2;
}
else
{
if ((bn_ctx = BN_CTX_new()) == NULL)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
ERR_R_MALLOC_FAILURE);
goto err;
}
i = *p;
p += 1;
if (EC_POINT_oct2point(srvr_ecdh->group,
clnt_ecpoint, p, i, bn_ctx) == 0)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
ERR_R_EC_LIB);
goto err;
}
p=(unsigned char *)s->init_buf->data;
}
i = ECDH_compute_key(p, KDF1_SHA1_len, clnt_ecpoint, srvr_ecdh, KDF1_SHA1);
if (i <= 0)
{
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
ERR_R_ECDH_LIB);
goto err;
}
EVP_PKEY_free(clnt_pub_pkey);
EC_POINT_free(clnt_ecpoint);
if (srvr_ecdh != NULL)
{
srvr_ecdh->priv_key = NULL;
srvr_ecdh->group = NULL;
EC_KEY_free(srvr_ecdh);
}
BN_CTX_free(bn_ctx);
s->session->master_key_length = s->method->ssl3_enc-> \
generate_master_secret(s, s->session->master_key, p, i);
OPENSSL_cleanse(p, i);
return (ret);
}
else
#endif
{
al=SSL_AD_HANDSHAKE_FAILURE;
SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
SSL_R_UNKNOWN_CIPHER_TYPE);
goto f_err;
}
return(1);
f_err:
ssl3_send_alert(s,SSL3_AL_FATAL,al);
#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
err:
#endif
#ifndef OPENSSL_NO_ECDH
EVP_PKEY_free(clnt_pub_pkey);
EC_POINT_free(clnt_ecpoint);
if (srvr_ecdh != NULL)
{
srvr_ecdh->priv_key = NULL;
srvr_ecdh->group = NULL;
EC_KEY_free(srvr_ecdh);
}
BN_CTX_free(bn_ctx);
#endif
return(-1);
}
|
['static int ssl3_get_client_key_exchange(SSL *s)\n\t{\n\tint i,al,ok;\n\tlong n;\n\tunsigned long l;\n\tunsigned char *p;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa=NULL;\n\tEVP_PKEY *pkey=NULL;\n#endif\n#ifndef OPENSSL_NO_DH\n\tBIGNUM *pub=NULL;\n\tDH *dh_srvr;\n#endif\n#ifndef OPENSSL_NO_KRB5\n KSSL_ERR kssl_err;\n#endif\n#ifndef OPENSSL_NO_ECDH\n\tEC_KEY *srvr_ecdh = NULL;\n\tEVP_PKEY *clnt_pub_pkey = NULL;\n\tEC_POINT *clnt_ecpoint = NULL;\n\tBN_CTX *bn_ctx = NULL;\n#endif\n\tn=ssl3_get_message(s,\n\t\tSSL3_ST_SR_KEY_EXCH_A,\n\t\tSSL3_ST_SR_KEY_EXCH_B,\n\t\tSSL3_MT_CLIENT_KEY_EXCHANGE,\n\t\t2048,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\tp=(unsigned char *)s->init_msg;\n\tl=s->s3->tmp.new_cipher->algorithms;\n#ifndef OPENSSL_NO_RSA\n\tif (l & SSL_kRSA)\n\t\t{\n\t\tif (s->s3->tmp.use_rsa_tmp)\n\t\t\t{\n\t\t\tif ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))\n\t\t\t\trsa=s->cert->rsa_tmp;\n\t\t\tif (rsa == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;\n\t\t\tif (\t(pkey == NULL) ||\n\t\t\t\t(pkey->type != EVP_PKEY_RSA) ||\n\t\t\t\t(pkey->pkey.rsa == NULL))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\trsa=pkey->pkey.rsa;\n\t\t\t}\n\t\tif (s->version > SSL3_VERSION)\n\t\t\t{\n\t\t\tn2s(p,i);\n\t\t\tif (n != i+2)\n\t\t\t\t{\n\t\t\t\tif (!(s->options & SSL_OP_TLS_D5_BUG))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tp-=2;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tn=i;\n\t\t\t}\n\t\ti=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);\n\t\tal = -1;\n\t\tif (i != SSL_MAX_MASTER_KEY_LENGTH)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);\n\t\t\t}\n\t\tif ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))\n\t\t\t{\n\t\t\tif (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&\n\t\t\t\t(p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\tif (al != -1)\n\t\t\t{\n#if 0\n\t\t\tgoto f_err;\n#else\n\t\t\tERR_clear_error();\n\t\t\ti = SSL_MAX_MASTER_KEY_LENGTH;\n\t\t\tp[0] = s->client_version >> 8;\n\t\t\tp[1] = s->client_version & 0xff;\n\t\t\tRAND_pseudo_bytes(p+2, i-2);\n#endif\n\t\t\t}\n\t\ts->session->master_key_length=\n\t\t\ts->method->ssl3_enc->generate_master_secret(s,\n\t\t\t\ts->session->master_key,\n\t\t\t\tp,i);\n\t\tOPENSSL_cleanse(p,i);\n\t\t}\n\telse\n#endif\n#ifndef OPENSSL_NO_DH\n\t\tif (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t{\n\t\tn2s(p,i);\n\t\tif (n != i+2)\n\t\t\t{\n\t\t\tif (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tp-=2;\n\t\t\t\ti=(int)n;\n\t\t\t\t}\n\t\t\t}\n\t\tif (n == 0L)\n\t\t\t{\n\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (s->s3->tmp.dh == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tdh_srvr=s->s3->tmp.dh;\n\t\t\t}\n\t\tpub=BN_bin2bn(p,i,NULL);\n\t\tif (pub == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\ti=DH_compute_key(p,pub,dh_srvr);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tDH_free(s->s3->tmp.dh);\n\t\ts->s3->tmp.dh=NULL;\n\t\tBN_clear_free(pub);\n\t\tpub=NULL;\n\t\ts->session->master_key_length=\n\t\t\ts->method->ssl3_enc->generate_master_secret(s,\n\t\t\t\ts->session->master_key,p,i);\n\t\tOPENSSL_cleanse(p,i);\n\t\t}\n\telse\n#endif\n#ifndef OPENSSL_NO_KRB5\n if (l & SSL_kKRB5)\n {\n krb5_error_code\t\tkrb5rc;\n\t\tkrb5_data\t\tenc_ticket;\n\t\tkrb5_data\t\tauthenticator;\n\t\tkrb5_data\t\tenc_pms;\n KSSL_CTX\t\t*kssl_ctx = s->kssl_ctx;\n\t\tEVP_CIPHER_CTX\t\tciph_ctx;\n\t\tEVP_CIPHER\t\t*enc = NULL;\n\t\tunsigned char\t\tiv[EVP_MAX_IV_LENGTH];\n\t\tunsigned char\t\tpms[SSL_MAX_MASTER_KEY_LENGTH\n + EVP_MAX_BLOCK_LENGTH];\n\t\tint padl, outl;\n\t\tkrb5_timestamp\t\tauthtime = 0;\n\t\tkrb5_ticket_times\tttimes;\n\t\tEVP_CIPHER_CTX_init(&ciph_ctx);\n if (!kssl_ctx) kssl_ctx = kssl_ctx_new();\n\t\tn2s(p,i);\n\t\tenc_ticket.length = i;\n\t\tenc_ticket.data = (char *)p;\n\t\tp+=enc_ticket.length;\n\t\tn2s(p,i);\n\t\tauthenticator.length = i;\n\t\tauthenticator.data = (char *)p;\n\t\tp+=authenticator.length;\n\t\tn2s(p,i);\n\t\tenc_pms.length = i;\n\t\tenc_pms.data = (char *)p;\n\t\tp+=enc_pms.length;\n\t\tif(enc_pms.length > sizeof pms)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t SSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (n != enc_ticket.length + authenticator.length +\n\t\t\t\t\t\tenc_pms.length + 6)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,\n\t\t\t\t\t&kssl_err)) != 0)\n {\n#ifdef KSSL_DEBUG\n printf("kssl_sget_tkt rtn %d [%d]\\n",\n krb5rc, kssl_err.reason);\n if (kssl_err.text)\n printf("kssl_err text= %s\\n", kssl_err.text);\n#endif\n SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n kssl_err.reason);\n goto err;\n }\n\t\tif ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,\n\t\t\t\t\t&authtime, &kssl_err)) != 0)\n\t\t\t{\n#ifdef KSSL_DEBUG\n printf("kssl_check_authent rtn %d [%d]\\n",\n krb5rc, kssl_err.reason);\n if (kssl_err.text)\n printf("kssl_err text= %s\\n", kssl_err.text);\n#endif\n SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,\n kssl_err.reason);\n goto err;\n\t\t\t}\n\t\tif ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);\n goto err;\n\t\t\t}\n#ifdef KSSL_DEBUG\n kssl_ctx_show(kssl_ctx);\n#endif\n\t\tenc = kssl_map_enc(kssl_ctx->enctype);\n if (enc == NULL)\n goto err;\n\t\tmemset(iv, 0, sizeof iv);\n\t\tif (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DECRYPTION_FAILED);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,\n\t\t\t\t\t(unsigned char *)enc_pms.data, enc_pms.length))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DECRYPTION_FAILED);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (outl > SSL_MAX_MASTER_KEY_LENGTH)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DECRYPTION_FAILED);\n\t\t\tgoto err;\n\t\t\t}\n\t\toutl += padl;\n\t\tif (outl > SSL_MAX_MASTER_KEY_LENGTH)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_DATA_LENGTH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tEVP_CIPHER_CTX_cleanup(&ciph_ctx);\n s->session->master_key_length=\n s->method->ssl3_enc->generate_master_secret(s,\n s->session->master_key, pms, outl);\n if (kssl_ctx->client_princ)\n {\n int len = strlen(kssl_ctx->client_princ);\n if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )\n {\n s->session->krb5_client_princ_len = len;\n memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);\n }\n }\n }\n\telse\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\tif ((l & SSL_kECDH) || (l & SSL_kECDHE))\n\t\t{\n\t\tint ret = 1;\n\t\tif ((srvr_ecdh = EC_KEY_new()) == NULL)\n\t\t\t{\n \tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t ERR_R_MALLOC_FAILURE);\n \tgoto err;\n\t\t\t}\n\t\tif (l & SSL_kECDH)\n\t\t\t{\n\t\t\tsrvr_ecdh->group = s->cert->key->privatekey-> \\\n\t\t\t pkey.eckey->group;\n\t\t\tsrvr_ecdh->priv_key = s->cert->key->privatekey-> \\\n\t\t\t pkey.eckey->priv_key;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tsrvr_ecdh->group = s->s3->tmp.ecdh->group;\n\t\t\tsrvr_ecdh->priv_key = s->s3->tmp.ecdh->priv_key;\n\t\t\t}\n\t\tif ((clnt_ecpoint = EC_POINT_new(srvr_ecdh->group))\n\t\t == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n if (n == 0L)\n {\n\t\t\t if (l & SSL_kECDHE)\n\t\t\t\t {\n\t\t\t\t al=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\t SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);\n\t\t\t\t goto f_err;\n\t\t\t\t }\n if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))\n\t\t\t == NULL) ||\n\t\t\t (clnt_pub_pkey->type != EVP_PKEY_EC))\n \t{\n \tal=SSL_AD_HANDSHAKE_FAILURE;\n \tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);\n \tgoto f_err;\n \t}\n\t\t\tEC_POINT_copy(clnt_ecpoint,\n\t\t\t clnt_pub_pkey->pkey.eckey->pub_key);\n ret = 2;\n }\n else\n {\n\t\t\tif ((bn_ctx = BN_CTX_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n i = *p;\n\t\t\tp += 1;\n if (EC_POINT_oct2point(srvr_ecdh->group,\n\t\t\t clnt_ecpoint, p, i, bn_ctx) == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\t ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n p=(unsigned char *)s->init_buf->data;\n }\n i = ECDH_compute_key(p, KDF1_SHA1_len, clnt_ecpoint, srvr_ecdh, KDF1_SHA1);\n if (i <= 0)\n {\n SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t ERR_R_ECDH_LIB);\n goto err;\n }\n\t\tEVP_PKEY_free(clnt_pub_pkey);\n\t\tEC_POINT_free(clnt_ecpoint);\n\t\tif (srvr_ecdh != NULL)\n\t\t\t{\n\t\t\tsrvr_ecdh->priv_key = NULL;\n\t\t\tsrvr_ecdh->group = NULL;\n\t\t\tEC_KEY_free(srvr_ecdh);\n\t\t\t}\n\t\tBN_CTX_free(bn_ctx);\n s->session->master_key_length = s->method->ssl3_enc-> \\\n\t\t generate_master_secret(s, s->session->master_key, p, i);\n OPENSSL_cleanse(p, i);\n return (ret);\n\t\t}\n\telse\n#endif\n\t\t{\n\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\tSSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,\n\t\t\t\tSSL_R_UNKNOWN_CIPHER_TYPE);\n\t\tgoto f_err;\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\n#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)\nerr:\n#endif\n#ifndef OPENSSL_NO_ECDH\n\tEVP_PKEY_free(clnt_pub_pkey);\n\tEC_POINT_free(clnt_ecpoint);\n\tif (srvr_ecdh != NULL)\n\t\t{\n\t\tsrvr_ecdh->priv_key = NULL;\n\t\tsrvr_ecdh->group = NULL;\n\t\tEC_KEY_free(srvr_ecdh);\n\t\t}\n\tBN_CTX_free(bn_ctx);\n#endif\n\treturn(-1);\n\t}', 'int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)\n\t{\n\treturn dh->meth->compute_key(key, pub_key, dh);\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
|
2,821
| 0
|
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L115
|
void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)
{
register DES_LONG l,r,t,u;
#ifdef DES_PTR
register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
#endif
#ifndef DES_UNROLL
register int i;
#endif
register DES_LONG *s;
r=data[0];
l=data[1];
IP(r,l);
r=ROTATE(r,29)&0xffffffffL;
l=ROTATE(l,29)&0xffffffffL;
s=(DES_LONG *)ks;
if (enc)
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r, 0);
D_ENCRYPT(r,l, 2);
D_ENCRYPT(l,r, 4);
D_ENCRYPT(r,l, 6);
D_ENCRYPT(l,r, 8);
D_ENCRYPT(r,l,10);
D_ENCRYPT(l,r,12);
D_ENCRYPT(r,l,14);
D_ENCRYPT(l,r,16);
D_ENCRYPT(r,l,18);
D_ENCRYPT(l,r,20);
D_ENCRYPT(r,l,22);
D_ENCRYPT(l,r,24);
D_ENCRYPT(r,l,26);
D_ENCRYPT(l,r,28);
D_ENCRYPT(r,l,30);
#else
for (i=0; i<32; i+=8)
{
D_ENCRYPT(l,r,i+0);
D_ENCRYPT(r,l,i+2);
D_ENCRYPT(l,r,i+4);
D_ENCRYPT(r,l,i+6);
}
#endif
}
else
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r,30);
D_ENCRYPT(r,l,28);
D_ENCRYPT(l,r,26);
D_ENCRYPT(r,l,24);
D_ENCRYPT(l,r,22);
D_ENCRYPT(r,l,20);
D_ENCRYPT(l,r,18);
D_ENCRYPT(r,l,16);
D_ENCRYPT(l,r,14);
D_ENCRYPT(r,l,12);
D_ENCRYPT(l,r,10);
D_ENCRYPT(r,l, 8);
D_ENCRYPT(l,r, 6);
D_ENCRYPT(r,l, 4);
D_ENCRYPT(l,r, 2);
D_ENCRYPT(r,l, 0);
#else
for (i=30; i>0; i-=8)
{
D_ENCRYPT(l,r,i-0);
D_ENCRYPT(r,l,i-2);
D_ENCRYPT(l,r,i-4);
D_ENCRYPT(r,l,i-6);
}
#endif
}
l=ROTATE(l,3)&0xffffffffL;
r=ROTATE(r,3)&0xffffffffL;
FP(r,l);
data[0]=l;
data[1]=r;
l=r=t=u=0;
}
|
['void des_string_to_2keys(const char *str, des_cblock key1, des_cblock key2)\n\t{\n\tdes_key_schedule ks;\n\tint i,length;\n\tregister unsigned char j;\n\tmemset(key1,0,8);\n\tmemset(key2,0,8);\n\tlength=strlen(str);\n#ifdef OLD_STR_TO_KEY\n\tif (length <= 8)\n\t\t{\n\t\tfor (i=0; i<length; i++)\n\t\t\t{\n\t\t\tkey2[i]=key1[i]=(str[i]<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tfor (i=0; i<length; i++)\n\t\t\t{\n\t\t\tif ((i/8)&1)\n\t\t\t\tkey2[i%8]^=(str[i]<<1);\n\t\t\telse\n\t\t\t\tkey1[i%8]^=(str[i]<<1);\n\t\t\t}\n\t\t}\n#else\n\tfor (i=0; i<length; i++)\n\t\t{\n\t\tj=str[i];\n\t\tif ((i%32) < 16)\n\t\t\t{\n\t\t\tif ((i%16) < 8)\n\t\t\t\tkey1[i%8]^=(j<<1);\n\t\t\telse\n\t\t\t\tkey2[i%8]^=(j<<1);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tj=((j<<4)&0xf0)|((j>>4)&0x0f);\n\t\t\tj=((j<<2)&0xcc)|((j>>2)&0x33);\n\t\t\tj=((j<<1)&0xaa)|((j>>1)&0x55);\n\t\t\tif ((i%16) < 8)\n\t\t\t\tkey1[7-(i%8)]^=j;\n\t\t\telse\n\t\t\t\tkey2[7-(i%8)]^=j;\n\t\t\t}\n\t\t}\n\tif (length <= 8) memcpy(key2,key1,8);\n#endif\n\tdes_set_odd_parity(key1);\n\tdes_set_odd_parity(key2);\n\ti=des_check_key;\n\tdes_check_key=0;\n\tdes_set_key(key1,ks);\n\tdes_cbc_cksum((unsigned char*)str,key1,length,ks,key1);\n\tdes_set_key(key2,ks);\n\tdes_cbc_cksum((unsigned char*)str,key2,length,ks,key2);\n\tdes_check_key=i;\n\tmemset(ks,0,sizeof(ks));\n\tdes_set_odd_parity(key1);\n\tdes_set_odd_parity(key2);\n\t}', 'DES_LONG des_cbc_cksum(const unsigned char *in, des_cblock out, long length,\n\t des_key_schedule schedule, const des_cblock iv)\n\t{\n\tregister DES_LONG tout0,tout1,tin0,tin1;\n\tregister long l=length;\n\tDES_LONG tin[2];\n\tc2l(iv,tout0);\n\tc2l(iv,tout1);\n\tfor (; l>0; l-=8)\n\t\t{\n\t\tif (l >= 8)\n\t\t\t{\n\t\t\tc2l(in,tin0);\n\t\t\tc2l(in,tin1);\n\t\t\t}\n\t\telse\n\t\t\tc2ln(in,tin0,tin1,l);\n\t\ttin0^=tout0; tin[0]=tin0;\n\t\ttin1^=tout1; tin[1]=tin1;\n\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\ttout0=tin[0];\n\t\ttout1=tin[1];\n\t\t}\n\tif (out != NULL)\n\t\t{\n\t\tl2c(tout0,out);\n\t\tl2c(tout1,out);\n\t\t}\n\ttout0=tin0=tin1=tin[0]=tin[1]=0;\n\treturn(tout1);\n\t}', 'void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)\n\t{\n\tregister DES_LONG l,r,t,u;\n#ifdef DES_PTR\n\tregister const unsigned char *des_SP=(const unsigned char *)des_SPtrans;\n#endif\n#ifndef DES_UNROLL\n\tregister int i;\n#endif\n\tregister DES_LONG *s;\n\tr=data[0];\n\tl=data[1];\n\tIP(r,l);\n\tr=ROTATE(r,29)&0xffffffffL;\n\tl=ROTATE(l,29)&0xffffffffL;\n\ts=(DES_LONG *)ks;\n\tif (enc)\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r, 0);\n\t\tD_ENCRYPT(r,l, 2);\n\t\tD_ENCRYPT(l,r, 4);\n\t\tD_ENCRYPT(r,l, 6);\n\t\tD_ENCRYPT(l,r, 8);\n\t\tD_ENCRYPT(r,l,10);\n\t\tD_ENCRYPT(l,r,12);\n\t\tD_ENCRYPT(r,l,14);\n\t\tD_ENCRYPT(l,r,16);\n\t\tD_ENCRYPT(r,l,18);\n\t\tD_ENCRYPT(l,r,20);\n\t\tD_ENCRYPT(r,l,22);\n\t\tD_ENCRYPT(l,r,24);\n\t\tD_ENCRYPT(r,l,26);\n\t\tD_ENCRYPT(l,r,28);\n\t\tD_ENCRYPT(r,l,30);\n#else\n\t\tfor (i=0; i<32; i+=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i+0);\n\t\t\tD_ENCRYPT(r,l,i+2);\n\t\t\tD_ENCRYPT(l,r,i+4);\n\t\t\tD_ENCRYPT(r,l,i+6);\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r,30);\n\t\tD_ENCRYPT(r,l,28);\n\t\tD_ENCRYPT(l,r,26);\n\t\tD_ENCRYPT(r,l,24);\n\t\tD_ENCRYPT(l,r,22);\n\t\tD_ENCRYPT(r,l,20);\n\t\tD_ENCRYPT(l,r,18);\n\t\tD_ENCRYPT(r,l,16);\n\t\tD_ENCRYPT(l,r,14);\n\t\tD_ENCRYPT(r,l,12);\n\t\tD_ENCRYPT(l,r,10);\n\t\tD_ENCRYPT(r,l, 8);\n\t\tD_ENCRYPT(l,r, 6);\n\t\tD_ENCRYPT(r,l, 4);\n\t\tD_ENCRYPT(l,r, 2);\n\t\tD_ENCRYPT(r,l, 0);\n#else\n\t\tfor (i=30; i>0; i-=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i-0);\n\t\t\tD_ENCRYPT(r,l,i-2);\n\t\t\tD_ENCRYPT(l,r,i-4);\n\t\t\tD_ENCRYPT(r,l,i-6);\n\t\t\t}\n#endif\n\t\t}\n\tl=ROTATE(l,3)&0xffffffffL;\n\tr=ROTATE(r,3)&0xffffffffL;\n\tFP(r,l);\n\tdata[0]=l;\n\tdata[1]=r;\n\tl=r=t=u=0;\n\t}']
|
2,822
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/ssl/t1_ext.c/#L234
|
static int custom_ext_meth_add(custom_ext_methods *exts,
unsigned int ext_type,
custom_ext_add_cb add_cb,
custom_ext_free_cb free_cb,
void *add_arg,
custom_ext_parse_cb parse_cb, void *parse_arg)
{
custom_ext_method *meth;
if (!add_cb && free_cb)
return 0;
if (SSL_extension_supported(ext_type))
return 0;
if (ext_type > 0xffff)
return 0;
if (custom_ext_find(exts, ext_type))
return 0;
exts->meths = OPENSSL_realloc(exts->meths,
(exts->meths_count +
1) * sizeof(custom_ext_method));
if (!exts->meths) {
exts->meths_count = 0;
return 0;
}
meth = exts->meths + exts->meths_count;
memset(meth, 0, sizeof(*meth));
meth->parse_cb = parse_cb;
meth->add_cb = add_cb;
meth->free_cb = free_cb;
meth->ext_type = ext_type;
meth->add_arg = add_arg;
meth->parse_arg = parse_arg;
exts->meths_count++;
return 1;
}
|
['static int serverinfo_process_buffer(const unsigned char *serverinfo,\n size_t serverinfo_length, SSL_CTX *ctx)\n{\n if (serverinfo == NULL || serverinfo_length == 0)\n return 0;\n for (;;) {\n unsigned int ext_type = 0;\n size_t len = 0;\n if (serverinfo_length == 0)\n return 1;\n if (serverinfo_length < 2)\n return 0;\n ext_type = (serverinfo[0] << 8) + serverinfo[1];\n if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,\n serverinfo_srv_add_cb,\n NULL, NULL,\n serverinfo_srv_parse_cb,\n NULL))\n return 0;\n serverinfo += 2;\n serverinfo_length -= 2;\n if (serverinfo_length < 2)\n return 0;\n len = (serverinfo[0] << 8) + serverinfo[1];\n serverinfo += 2;\n serverinfo_length -= 2;\n if (len > serverinfo_length)\n return 0;\n serverinfo += len;\n serverinfo_length -= len;\n }\n}', 'int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,\n custom_ext_add_cb add_cb,\n custom_ext_free_cb free_cb,\n void *add_arg,\n custom_ext_parse_cb parse_cb,\n void *parse_arg)\n{\n return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,\n add_cb, free_cb, add_arg, parse_cb, parse_arg);\n}', 'static int custom_ext_meth_add(custom_ext_methods *exts,\n unsigned int ext_type,\n custom_ext_add_cb add_cb,\n custom_ext_free_cb free_cb,\n void *add_arg,\n custom_ext_parse_cb parse_cb, void *parse_arg)\n{\n custom_ext_method *meth;\n if (!add_cb && free_cb)\n return 0;\n if (SSL_extension_supported(ext_type))\n return 0;\n if (ext_type > 0xffff)\n return 0;\n if (custom_ext_find(exts, ext_type))\n return 0;\n exts->meths = OPENSSL_realloc(exts->meths,\n (exts->meths_count +\n 1) * sizeof(custom_ext_method));\n if (!exts->meths) {\n exts->meths_count = 0;\n return 0;\n }\n meth = exts->meths + exts->meths_count;\n memset(meth, 0, sizeof(*meth));\n meth->parse_cb = parse_cb;\n meth->add_cb = add_cb;\n meth->free_cb = free_cb;\n meth->ext_type = ext_type;\n meth->add_arg = add_arg;\n meth->parse_arg = parse_arg;\n exts->meths_count++;\n return 1;\n}', 'static custom_ext_method *custom_ext_find(custom_ext_methods *exts,\n unsigned int ext_type)\n{\n size_t i;\n custom_ext_method *meth = exts->meths;\n for (i = 0; i < exts->meths_count; i++, meth++) {\n if (ext_type == meth->ext_type)\n return meth;\n }\n return NULL;\n}']
|
2,823
| 0
|
https://github.com/nginx/nginx/blob/4fe0a09942f8aed90f84c77969847980e9aadd98/src/core/ngx_string.c/#L1034
|
off_t
ngx_atoof(u_char *line, size_t n)
{
off_t value, cutoff, cutlim;
if (n == 0) {
return NGX_ERROR;
}
cutoff = NGX_MAX_OFF_T_VALUE / 10;
cutlim = NGX_MAX_OFF_T_VALUE % 10;
for (value = 0; n--; line++) {
if (*line < '0' || *line > '9') {
return NGX_ERROR;
}
if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
return NGX_ERROR;
}
value = value * 10 + (*line - '0');
}
return value;
}
|
['ngx_int_t\nngx_http_process_request_header(ngx_http_request_t *r)\n{\n if (r->headers_in.server.len == 0\n && ngx_http_set_virtual_server(r, &r->headers_in.server)\n == NGX_ERROR)\n {\n return NGX_ERROR;\n }\n if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {\n ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,\n "client sent HTTP/1.1 request without \\"Host\\" header");\n ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);\n return NGX_ERROR;\n }\n if (r->headers_in.content_length) {\n r->headers_in.content_length_n =\n ngx_atoof(r->headers_in.content_length->value.data,\n r->headers_in.content_length->value.len);\n if (r->headers_in.content_length_n == NGX_ERROR) {\n ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,\n "client sent invalid \\"Content-Length\\" header");\n ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);\n return NGX_ERROR;\n }\n }\n if (r->method & NGX_HTTP_TRACE) {\n ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,\n "client sent TRACE method");\n ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);\n return NGX_ERROR;\n }\n if (r->headers_in.transfer_encoding) {\n if (r->headers_in.transfer_encoding->value.len == 7\n && ngx_strncasecmp(r->headers_in.transfer_encoding->value.data,\n (u_char *) "chunked", 7) == 0)\n {\n r->headers_in.content_length = NULL;\n r->headers_in.content_length_n = -1;\n r->headers_in.chunked = 1;\n } else if (r->headers_in.transfer_encoding->value.len != 8\n || ngx_strncasecmp(r->headers_in.transfer_encoding->value.data,\n (u_char *) "identity", 8) != 0)\n {\n ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,\n "client sent unknown \\"Transfer-Encoding\\": \\"%V\\"",\n &r->headers_in.transfer_encoding->value);\n ngx_http_finalize_request(r, NGX_HTTP_NOT_IMPLEMENTED);\n return NGX_ERROR;\n }\n }\n if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {\n if (r->headers_in.keep_alive) {\n r->headers_in.keep_alive_n =\n ngx_atotm(r->headers_in.keep_alive->value.data,\n r->headers_in.keep_alive->value.len);\n }\n }\n return NGX_OK;\n}', 'static ngx_int_t\nngx_http_set_virtual_server(ngx_http_request_t *r, ngx_str_t *host)\n{\n ngx_int_t rc;\n ngx_http_connection_t *hc;\n ngx_http_core_loc_conf_t *clcf;\n ngx_http_core_srv_conf_t *cscf;\n#if (NGX_SUPPRESS_WARN)\n cscf = NULL;\n#endif\n hc = r->http_connection;\n#if (NGX_HTTP_SSL && defined SSL_CTRL_SET_TLSEXT_HOSTNAME)\n if (hc->ssl_servername) {\n if (hc->ssl_servername->len == host->len\n && ngx_strncmp(hc->ssl_servername->data,\n host->data, host->len) == 0)\n {\n#if (NGX_PCRE)\n if (hc->ssl_servername_regex\n && ngx_http_regex_exec(r, hc->ssl_servername_regex,\n hc->ssl_servername) != NGX_OK)\n {\n ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_ERROR;\n }\n#endif\n return NGX_OK;\n }\n }\n#endif\n rc = ngx_http_find_virtual_server(r->connection,\n hc->addr_conf->virtual_names,\n host, r, &cscf);\n if (rc == NGX_ERROR) {\n ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_ERROR;\n }\n#if (NGX_HTTP_SSL && defined SSL_CTRL_SET_TLSEXT_HOSTNAME)\n if (hc->ssl_servername) {\n ngx_http_ssl_srv_conf_t *sscf;\n if (rc == NGX_DECLINED) {\n cscf = hc->addr_conf->default_server;\n rc = NGX_OK;\n }\n sscf = ngx_http_get_module_srv_conf(cscf->ctx, ngx_http_ssl_module);\n if (sscf->verify) {\n ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,\n "client attempted to request the server name "\n "different from that one was negotiated");\n ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);\n return NGX_ERROR;\n }\n }\n#endif\n if (rc == NGX_DECLINED) {\n return NGX_OK;\n }\n r->srv_conf = cscf->ctx->srv_conf;\n r->loc_conf = cscf->ctx->loc_conf;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n ngx_http_set_connection_log(r->connection, clcf->error_log);\n return NGX_OK;\n}', 'static ngx_int_t\nngx_http_find_virtual_server(ngx_connection_t *c,\n ngx_http_virtual_names_t *virtual_names, ngx_str_t *host,\n ngx_http_request_t *r, ngx_http_core_srv_conf_t **cscfp)\n{\n ngx_http_core_srv_conf_t *cscf;\n if (virtual_names == NULL) {\n return NGX_DECLINED;\n }\n cscf = ngx_hash_find_combined(&virtual_names->names,\n ngx_hash_key(host->data, host->len),\n host->data, host->len);\n if (cscf) {\n *cscfp = cscf;\n return NGX_OK;\n }\n#if (NGX_PCRE)\n if (host->len && virtual_names->nregex) {\n ngx_int_t n;\n ngx_uint_t i;\n ngx_http_server_name_t *sn;\n sn = virtual_names->regex;\n#if (NGX_HTTP_SSL && defined SSL_CTRL_SET_TLSEXT_HOSTNAME)\n if (r == NULL) {\n ngx_http_connection_t *hc;\n for (i = 0; i < virtual_names->nregex; i++) {\n n = ngx_regex_exec(sn[i].regex->regex, host, NULL, 0);\n if (n == NGX_REGEX_NO_MATCHED) {\n continue;\n }\n if (n >= 0) {\n hc = c->data;\n hc->ssl_servername_regex = sn[i].regex;\n *cscfp = sn[i].server;\n return NGX_OK;\n }\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n ngx_regex_exec_n " failed: %i "\n "on \\"%V\\" using \\"%V\\"",\n n, host, &sn[i].regex->name);\n return NGX_ERROR;\n }\n return NGX_DECLINED;\n }\n#endif\n for (i = 0; i < virtual_names->nregex; i++) {\n n = ngx_http_regex_exec(r, sn[i].regex, host);\n if (n == NGX_DECLINED) {\n continue;\n }\n if (n == NGX_OK) {\n *cscfp = sn[i].server;\n return NGX_OK;\n }\n return NGX_ERROR;\n }\n }\n#endif\n return NGX_DECLINED;\n}', "off_t\nngx_atoof(u_char *line, size_t n)\n{\n off_t value, cutoff, cutlim;\n if (n == 0) {\n return NGX_ERROR;\n }\n cutoff = NGX_MAX_OFF_T_VALUE / 10;\n cutlim = NGX_MAX_OFF_T_VALUE % 10;\n for (value = 0; n--; line++) {\n if (*line < '0' || *line > '9') {\n return NGX_ERROR;\n }\n if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {\n return NGX_ERROR;\n }\n value = value * 10 + (*line - '0');\n }\n return value;\n}"]
|
2,824
| 0
|
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/bn/bn_ctx.c/#L327
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int DH_check(const DH *dh, int *ret)\n{\n int ok = 0;\n BN_CTX *ctx = NULL;\n BN_ULONG l;\n BIGNUM *t1 = NULL, *t2 = NULL;\n *ret = 0;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n if (t1 == NULL)\n goto err;\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (dh->q) {\n if (BN_cmp(dh->g, BN_value_one()) <= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else if (BN_cmp(dh->g, dh->p) >= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else {\n if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))\n goto err;\n if (!BN_is_one(t1))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n }\n if (!BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL))\n *ret |= DH_CHECK_Q_NOT_PRIME;\n if (!BN_div(t1, t2, dh->p, dh->q, ctx))\n goto err;\n if (!BN_is_one(t2))\n *ret |= DH_CHECK_INVALID_Q_VALUE;\n if (dh->j && BN_cmp(dh->j, t1))\n *ret |= DH_CHECK_INVALID_J_VALUE;\n } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {\n l = BN_mod_word(dh->p, 24);\n if (l != 11)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {\n l = BN_mod_word(dh->p, 10);\n if ((l != 3) && (l != 7))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n } else\n *ret |= DH_UNABLE_TO_CHECK_GENERATOR;\n if (!BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL))\n *ret |= DH_CHECK_P_NOT_PRIME;\n else if (!dh->q) {\n if (!BN_rshift1(t1, dh->p))\n goto err;\n if (!BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL))\n *ret |= DH_CHECK_P_NOT_SAFE_PRIME;\n }\n ok = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return (ok);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (!aa || !val[0])\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,825
| 0
|
https://github.com/libav/libav/blob/a20639017bfca0490bb1799575714f22bf470b4f/libavcodec/mpegaudiodec.c/#L714
|
static void dct32(INTFLOAT *out, const INTFLOAT *tab)
{
INTFLOAT tmp0, tmp1;
INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 ,
val8 , val9 , val10, val11, val12, val13, val14, val15,
val16, val17, val18, val19, val20, val21, val22, val23,
val24, val25, val26, val27, val28, val29, val30, val31;
BF0( 0, 31, COS0_0 , 1);
BF0(15, 16, COS0_15, 5);
BF( 0, 15, COS1_0 , 1);
BF(16, 31,-COS1_0 , 1);
BF0( 7, 24, COS0_7 , 1);
BF0( 8, 23, COS0_8 , 1);
BF( 7, 8, COS1_7 , 4);
BF(23, 24,-COS1_7 , 4);
BF( 0, 7, COS2_0 , 1);
BF( 8, 15,-COS2_0 , 1);
BF(16, 23, COS2_0 , 1);
BF(24, 31,-COS2_0 , 1);
BF0( 3, 28, COS0_3 , 1);
BF0(12, 19, COS0_12, 2);
BF( 3, 12, COS1_3 , 1);
BF(19, 28,-COS1_3 , 1);
BF0( 4, 27, COS0_4 , 1);
BF0(11, 20, COS0_11, 2);
BF( 4, 11, COS1_4 , 1);
BF(20, 27,-COS1_4 , 1);
BF( 3, 4, COS2_3 , 3);
BF(11, 12,-COS2_3 , 3);
BF(19, 20, COS2_3 , 3);
BF(27, 28,-COS2_3 , 3);
BF( 0, 3, COS3_0 , 1);
BF( 4, 7,-COS3_0 , 1);
BF( 8, 11, COS3_0 , 1);
BF(12, 15,-COS3_0 , 1);
BF(16, 19, COS3_0 , 1);
BF(20, 23,-COS3_0 , 1);
BF(24, 27, COS3_0 , 1);
BF(28, 31,-COS3_0 , 1);
BF0( 1, 30, COS0_1 , 1);
BF0(14, 17, COS0_14, 3);
BF( 1, 14, COS1_1 , 1);
BF(17, 30,-COS1_1 , 1);
BF0( 6, 25, COS0_6 , 1);
BF0( 9, 22, COS0_9 , 1);
BF( 6, 9, COS1_6 , 2);
BF(22, 25,-COS1_6 , 2);
BF( 1, 6, COS2_1 , 1);
BF( 9, 14,-COS2_1 , 1);
BF(17, 22, COS2_1 , 1);
BF(25, 30,-COS2_1 , 1);
BF0( 2, 29, COS0_2 , 1);
BF0(13, 18, COS0_13, 3);
BF( 2, 13, COS1_2 , 1);
BF(18, 29,-COS1_2 , 1);
BF0( 5, 26, COS0_5 , 1);
BF0(10, 21, COS0_10, 1);
BF( 5, 10, COS1_5 , 2);
BF(21, 26,-COS1_5 , 2);
BF( 2, 5, COS2_2 , 1);
BF(10, 13,-COS2_2 , 1);
BF(18, 21, COS2_2 , 1);
BF(26, 29,-COS2_2 , 1);
BF( 1, 2, COS3_1 , 2);
BF( 5, 6,-COS3_1 , 2);
BF( 9, 10, COS3_1 , 2);
BF(13, 14,-COS3_1 , 2);
BF(17, 18, COS3_1 , 2);
BF(21, 22,-COS3_1 , 2);
BF(25, 26, COS3_1 , 2);
BF(29, 30,-COS3_1 , 2);
BF1( 0, 1, 2, 3);
BF2( 4, 5, 6, 7);
BF1( 8, 9, 10, 11);
BF2(12, 13, 14, 15);
BF1(16, 17, 18, 19);
BF2(20, 21, 22, 23);
BF1(24, 25, 26, 27);
BF2(28, 29, 30, 31);
ADD( 8, 12);
ADD(12, 10);
ADD(10, 14);
ADD(14, 9);
ADD( 9, 13);
ADD(13, 11);
ADD(11, 15);
out[ 0] = val0;
out[16] = val1;
out[ 8] = val2;
out[24] = val3;
out[ 4] = val4;
out[20] = val5;
out[12] = val6;
out[28] = val7;
out[ 2] = val8;
out[18] = val9;
out[10] = val10;
out[26] = val11;
out[ 6] = val12;
out[22] = val13;
out[14] = val14;
out[30] = val15;
ADD(24, 28);
ADD(28, 26);
ADD(26, 30);
ADD(30, 25);
ADD(25, 29);
ADD(29, 27);
ADD(27, 31);
out[ 1] = val16 + val24;
out[17] = val17 + val25;
out[ 9] = val18 + val26;
out[25] = val19 + val27;
out[ 5] = val20 + val28;
out[21] = val21 + val29;
out[13] = val22 + val30;
out[29] = val23 + val31;
out[ 3] = val24 + val20;
out[19] = val25 + val21;
out[11] = val26 + val22;
out[27] = val27 + val23;
out[ 7] = val28 + val18;
out[23] = val29 + val19;
out[15] = val30 + val17;
out[31] = val31;
}
|
['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n ff_mpa_synth_window, &dither_state,\n samples_ptr, 2,\n c->sb_samples[ch][i]);\n samples_ptr += 64;\n }\n }\n for(i = 0; i < MPC_FRAME_SIZE*2; i++)\n *out++=samples[i];\n}', 'void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,\n MPA_INT *window, int *dither_state,\n OUT_INT *samples, int incr,\n INTFLOAT sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n int offset;\n#if FRAC_BITS <= 15\n int32_t tmp[32];\n#endif\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n#if FRAC_BITS <= 15 && !CONFIG_FLOAT\n dct32(tmp, sb_samples);\n for(j=0;j<32;j++) {\n synth_buf[j] = av_clip_int16(tmp[j]);\n }\n#else\n dct32(synth_buf, sb_samples);\n#endif\n apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}', 'static void dct32(INTFLOAT *out, const INTFLOAT *tab)\n{\n INTFLOAT tmp0, tmp1;\n INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 ,\n val8 , val9 , val10, val11, val12, val13, val14, val15,\n val16, val17, val18, val19, val20, val21, val22, val23,\n val24, val25, val26, val27, val28, val29, val30, val31;\n BF0( 0, 31, COS0_0 , 1);\n BF0(15, 16, COS0_15, 5);\n BF( 0, 15, COS1_0 , 1);\n BF(16, 31,-COS1_0 , 1);\n BF0( 7, 24, COS0_7 , 1);\n BF0( 8, 23, COS0_8 , 1);\n BF( 7, 8, COS1_7 , 4);\n BF(23, 24,-COS1_7 , 4);\n BF( 0, 7, COS2_0 , 1);\n BF( 8, 15,-COS2_0 , 1);\n BF(16, 23, COS2_0 , 1);\n BF(24, 31,-COS2_0 , 1);\n BF0( 3, 28, COS0_3 , 1);\n BF0(12, 19, COS0_12, 2);\n BF( 3, 12, COS1_3 , 1);\n BF(19, 28,-COS1_3 , 1);\n BF0( 4, 27, COS0_4 , 1);\n BF0(11, 20, COS0_11, 2);\n BF( 4, 11, COS1_4 , 1);\n BF(20, 27,-COS1_4 , 1);\n BF( 3, 4, COS2_3 , 3);\n BF(11, 12,-COS2_3 , 3);\n BF(19, 20, COS2_3 , 3);\n BF(27, 28,-COS2_3 , 3);\n BF( 0, 3, COS3_0 , 1);\n BF( 4, 7,-COS3_0 , 1);\n BF( 8, 11, COS3_0 , 1);\n BF(12, 15,-COS3_0 , 1);\n BF(16, 19, COS3_0 , 1);\n BF(20, 23,-COS3_0 , 1);\n BF(24, 27, COS3_0 , 1);\n BF(28, 31,-COS3_0 , 1);\n BF0( 1, 30, COS0_1 , 1);\n BF0(14, 17, COS0_14, 3);\n BF( 1, 14, COS1_1 , 1);\n BF(17, 30,-COS1_1 , 1);\n BF0( 6, 25, COS0_6 , 1);\n BF0( 9, 22, COS0_9 , 1);\n BF( 6, 9, COS1_6 , 2);\n BF(22, 25,-COS1_6 , 2);\n BF( 1, 6, COS2_1 , 1);\n BF( 9, 14,-COS2_1 , 1);\n BF(17, 22, COS2_1 , 1);\n BF(25, 30,-COS2_1 , 1);\n BF0( 2, 29, COS0_2 , 1);\n BF0(13, 18, COS0_13, 3);\n BF( 2, 13, COS1_2 , 1);\n BF(18, 29,-COS1_2 , 1);\n BF0( 5, 26, COS0_5 , 1);\n BF0(10, 21, COS0_10, 1);\n BF( 5, 10, COS1_5 , 2);\n BF(21, 26,-COS1_5 , 2);\n BF( 2, 5, COS2_2 , 1);\n BF(10, 13,-COS2_2 , 1);\n BF(18, 21, COS2_2 , 1);\n BF(26, 29,-COS2_2 , 1);\n BF( 1, 2, COS3_1 , 2);\n BF( 5, 6,-COS3_1 , 2);\n BF( 9, 10, COS3_1 , 2);\n BF(13, 14,-COS3_1 , 2);\n BF(17, 18, COS3_1 , 2);\n BF(21, 22,-COS3_1 , 2);\n BF(25, 26, COS3_1 , 2);\n BF(29, 30,-COS3_1 , 2);\n BF1( 0, 1, 2, 3);\n BF2( 4, 5, 6, 7);\n BF1( 8, 9, 10, 11);\n BF2(12, 13, 14, 15);\n BF1(16, 17, 18, 19);\n BF2(20, 21, 22, 23);\n BF1(24, 25, 26, 27);\n BF2(28, 29, 30, 31);\n ADD( 8, 12);\n ADD(12, 10);\n ADD(10, 14);\n ADD(14, 9);\n ADD( 9, 13);\n ADD(13, 11);\n ADD(11, 15);\n out[ 0] = val0;\n out[16] = val1;\n out[ 8] = val2;\n out[24] = val3;\n out[ 4] = val4;\n out[20] = val5;\n out[12] = val6;\n out[28] = val7;\n out[ 2] = val8;\n out[18] = val9;\n out[10] = val10;\n out[26] = val11;\n out[ 6] = val12;\n out[22] = val13;\n out[14] = val14;\n out[30] = val15;\n ADD(24, 28);\n ADD(28, 26);\n ADD(26, 30);\n ADD(30, 25);\n ADD(25, 29);\n ADD(29, 27);\n ADD(27, 31);\n out[ 1] = val16 + val24;\n out[17] = val17 + val25;\n out[ 9] = val18 + val26;\n out[25] = val19 + val27;\n out[ 5] = val20 + val28;\n out[21] = val21 + val29;\n out[13] = val22 + val30;\n out[29] = val23 + val31;\n out[ 3] = val24 + val20;\n out[19] = val25 + val21;\n out[11] = val26 + val22;\n out[27] = val27 + val23;\n out[ 7] = val28 + val18;\n out[23] = val29 + val19;\n out[15] = val30 + val17;\n out[31] = val31;\n}']
|
2,826
| 0
|
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_shift.c/#L110
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
r->neg = a->neg;
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
}
|
['int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_lshift1(r, a))\n return 0;\n bn_check_top(r);\n return BN_nnmod(r, r, m, ctx);\n}', 'int BN_lshift1(BIGNUM *r, const BIGNUM *a)\n{\n register BN_ULONG *ap, *rp, t, c;\n int i;\n bn_check_top(r);\n bn_check_top(a);\n if (r != a) {\n r->neg = a->neg;\n if (bn_wexpand(r, a->top + 1) == NULL)\n return (0);\n r->top = a->top;\n } else {\n if (bn_wexpand(r, a->top + 1) == NULL)\n return (0);\n }\n ap = a->d;\n rp = r->d;\n c = 0;\n for (i = 0; i < a->top; i++) {\n t = *(ap++);\n *(rp++) = ((t << 1) | c) & BN_MASK2;\n c = (t & BN_TBIT) ? 1 : 0;\n }\n if (c) {\n *rp = 1;\n r->top++;\n }\n bn_check_top(r);\n return (1);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
2,827
| 0
|
https://github.com/openssl/openssl/blob/6d0b5ee1d6163732b886bc0567dbce08aeade4c1/test/evp_test.c/#L1843
|
static int encode_test_init(struct evp_test *t, const char *encoding)
{
struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
if (strcmp(encoding, "canonical") == 0) {
edata->encoding = BASE64_CANONICAL_ENCODING;
} else if (strcmp(encoding, "valid") == 0) {
edata->encoding = BASE64_VALID_ENCODING;
} else if (strcmp(encoding, "invalid") == 0) {
edata->encoding = BASE64_INVALID_ENCODING;
t->expected_err = OPENSSL_strdup("DECODE_ERROR");
if (t->expected_err == NULL)
return 0;
} else {
fprintf(stderr, "Bad encoding: %s. Should be one of "
"{canonical, valid, invalid}\n", encoding);
return 0;
}
t->data = edata;
return 1;
}
|
['static int encode_test_init(struct evp_test *t, const char *encoding)\n{\n struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));\n if (strcmp(encoding, "canonical") == 0) {\n edata->encoding = BASE64_CANONICAL_ENCODING;\n } else if (strcmp(encoding, "valid") == 0) {\n edata->encoding = BASE64_VALID_ENCODING;\n } else if (strcmp(encoding, "invalid") == 0) {\n edata->encoding = BASE64_INVALID_ENCODING;\n t->expected_err = OPENSSL_strdup("DECODE_ERROR");\n if (t->expected_err == NULL)\n return 0;\n } else {\n fprintf(stderr, "Bad encoding: %s. Should be one of "\n "{canonical, valid, invalid}\\n", encoding);\n return 0;\n }\n t->data = edata;\n return 1;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,828
| 0
|
https://github.com/openssl/openssl/blob/516decaef31a13e5bf1b6f855dc0fefe23d7eed9/test/sha512t.c/#L128
|
static int test_sha512_multi(void)
{
unsigned char md[SHA512_DIGEST_LENGTH];
int i, testresult = 0;
EVP_MD_CTX *evp;
static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
evp = EVP_MD_CTX_new();
if (!TEST_ptr(evp))
return 0;
if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha512(), NULL)))
goto end;
for (i = 0; i < 1000000; i += 288)
if (!TEST_true(EVP_DigestUpdate(evp, updstr,
(1000000 - i) < 288 ? 1000000 - i
: 288)))
goto end;
if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
|| !TEST_mem_eq(md, sizeof(md), app_c3, sizeof(app_c3)))
goto end;
testresult = 1;
end:
EVP_MD_CTX_free(evp);
return testresult;
}
|
['static int test_sha512_multi(void)\n{\n unsigned char md[SHA512_DIGEST_LENGTH];\n int i, testresult = 0;\n EVP_MD_CTX *evp;\n static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";\n evp = EVP_MD_CTX_new();\n if (!TEST_ptr(evp))\n return 0;\n if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha512(), NULL)))\n goto end;\n for (i = 0; i < 1000000; i += 288)\n if (!TEST_true(EVP_DigestUpdate(evp, updstr,\n (1000000 - i) < 288 ? 1000000 - i\n : 288)))\n goto end;\n if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))\n || !TEST_mem_eq(md, sizeof(md), app_c3, sizeof(app_c3)))\n goto end;\n testresult = 1;\n end:\n EVP_MD_CTX_free(evp);\n return testresult;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}', 'const EVP_MD *EVP_sha512(void)\n{\n return (&sha512_md);\n}', 'int test_true(const char *file, int line, const char *s, int b)\n{\n if (b)\n return 1;\n test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");\n return 0;\n}', 'static void test_fail_message(const char *prefix, const char *file,\n int line, const char *type,\n const char *left, const char *right,\n const char *op, const char *fmt, ...)\n{\n va_list ap;\n va_start(ap, fmt);\n test_fail_message_va(prefix, file, line, type, left, right, op, fmt, ap);\n va_end(ap);\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,829
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b,BN_FLG_SECURE))
a = A = OPENSSL_secure_malloc(words * sizeof(*a));
else
a = A = OPENSSL_malloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#ifdef PURIFY
memset(a, 0, sizeof(*a) * words);
#endif
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
}
|
['static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y,\n BIGNUM *x1, BIGNUM *z1, BIGNUM *x2, BIGNUM *z2,\n BN_CTX *ctx)\n{\n BIGNUM *t3, *t4, *t5;\n int ret = 0;\n if (BN_is_zero(z1)) {\n BN_zero(x2);\n BN_zero(z2);\n return 1;\n }\n if (BN_is_zero(z2)) {\n if (!BN_copy(x2, x))\n return 0;\n if (!BN_GF2m_add(z2, x, y))\n return 0;\n return 2;\n }\n BN_CTX_start(ctx);\n t3 = BN_CTX_get(ctx);\n t4 = BN_CTX_get(ctx);\n t5 = BN_CTX_get(ctx);\n if (t5 == NULL)\n goto err;\n if (!BN_one(t5))\n goto err;\n if (!group->meth->field_mul(group, t3, z1, z2, ctx))\n goto err;\n if (!group->meth->field_mul(group, z1, z1, x, ctx))\n goto err;\n if (!BN_GF2m_add(z1, z1, x1))\n goto err;\n if (!group->meth->field_mul(group, z2, z2, x, ctx))\n goto err;\n if (!group->meth->field_mul(group, x1, z2, x1, ctx))\n goto err;\n if (!BN_GF2m_add(z2, z2, x2))\n goto err;\n if (!group->meth->field_mul(group, z2, z2, z1, ctx))\n goto err;\n if (!group->meth->field_sqr(group, t4, x, ctx))\n goto err;\n if (!BN_GF2m_add(t4, t4, y))\n goto err;\n if (!group->meth->field_mul(group, t4, t4, t3, ctx))\n goto err;\n if (!BN_GF2m_add(t4, t4, z2))\n goto err;\n if (!group->meth->field_mul(group, t3, t3, x, ctx))\n goto err;\n if (!group->meth->field_div(group, t3, t5, t3, ctx))\n goto err;\n if (!group->meth->field_mul(group, t4, t3, t4, ctx))\n goto err;\n if (!group->meth->field_mul(group, x2, x1, t3, ctx))\n goto err;\n if (!BN_GF2m_add(z2, x2, x))\n goto err;\n if (!group->meth->field_mul(group, z2, z2, t4, ctx))\n goto err;\n if (!BN_GF2m_add(z2, z2, y))\n goto err;\n ret = 2;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n const BIGNUM *at, *bt;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n at = b;\n bt = a;\n } else {\n at = a;\n bt = b;\n }\n if (bn_wexpand(r, at->top) == NULL)\n return 0;\n for (i = 0; i < bt->top; i++) {\n r->d[i] = at->d[i] ^ bt->d[i];\n }\n for (; i < at->top; i++) {\n r->d[i] = at->d[i];\n }\n r->top = at->top;\n bn_correct_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
|
2,830
| 0
|
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L237
|
static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
}
|
['static int flashsv_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame, AVPacket *avpkt)\n{\n int buf_size = avpkt->size;\n FlashSVContext *s = avctx->priv_data;\n int h_blocks, v_blocks, h_part, v_part, i, j, ret;\n BitstreamContext bc;\n if (buf_size == 0)\n return 0;\n if (buf_size < 4)\n return -1;\n bitstream_init(&bc, avpkt->data, buf_size * 8);\n s->block_width = 16 * (bitstream_read(&bc, 4) + 1);\n s->image_width = bitstream_read(&bc, 12);\n s->block_height = 16 * (bitstream_read(&bc, 4) + 1);\n s->image_height = bitstream_read(&bc, 12);\n if (s->ver == 2) {\n bitstream_skip(&bc, 6);\n if (bitstream_read_bit(&bc)) {\n avpriv_request_sample(avctx, "iframe");\n return AVERROR_PATCHWELCOME;\n }\n if (bitstream_read_bit(&bc)) {\n avpriv_request_sample(avctx, "Custom palette");\n return AVERROR_PATCHWELCOME;\n }\n }\n h_blocks = s->image_width / s->block_width;\n h_part = s->image_width % s->block_width;\n v_blocks = s->image_height / s->block_height;\n v_part = s->image_height % s->block_height;\n if (s->block_size < s->block_width * s->block_height) {\n int tmpblock_size = 3 * s->block_width * s->block_height, err;\n if ((err = av_reallocp(&s->tmpblock, tmpblock_size)) < 0) {\n s->block_size = 0;\n av_log(avctx, AV_LOG_ERROR,\n "Cannot allocate decompression buffer.\\n");\n return err;\n }\n if (s->ver == 2) {\n s->deflate_block_size = calc_deflate_block_size(tmpblock_size);\n if (s->deflate_block_size <= 0) {\n av_log(avctx, AV_LOG_ERROR,\n "Cannot determine deflate buffer size.\\n");\n return -1;\n }\n if ((err = av_reallocp(&s->deflate_block, s->deflate_block_size)) < 0) {\n s->block_size = 0;\n av_log(avctx, AV_LOG_ERROR, "Cannot allocate deflate buffer.\\n");\n return err;\n }\n }\n }\n s->block_size = s->block_width * s->block_height;\n if (avctx->width == 0 && avctx->height == 0) {\n avctx->width = s->image_width;\n avctx->height = s->image_height;\n }\n if (avctx->width != s->image_width || avctx->height != s->image_height) {\n av_log(avctx, AV_LOG_ERROR,\n "Frame width or height differs from first frame!\\n");\n av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\\n",\n avctx->height, avctx->width, s->image_height, s->image_width);\n return AVERROR_INVALIDDATA;\n }\n s->is_keyframe = (avpkt->flags & AV_PKT_FLAG_KEY) && (s->ver == 2);\n if (s->is_keyframe) {\n int err;\n int nb_blocks = (v_blocks + !!v_part) *\n (h_blocks + !!h_part) * sizeof(s->blocks[0]);\n if ((err = av_reallocp(&s->keyframedata, avpkt->size)) < 0)\n return err;\n memcpy(s->keyframedata, avpkt->data, avpkt->size);\n if ((err = av_reallocp(&s->blocks, nb_blocks)) < 0)\n return err;\n memset(s->blocks, 0, nb_blocks);\n }\n ff_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\\n",\n s->image_width, s->image_height, s->block_width, s->block_height,\n h_blocks, v_blocks, h_part, v_part);\n if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\\n");\n return ret;\n }\n for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {\n int y_pos = j * s->block_height;\n int cur_blk_height = (j < v_blocks) ? s->block_height : v_part;\n for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {\n int x_pos = i * s->block_width;\n int cur_blk_width = (i < h_blocks) ? s->block_width : h_part;\n int has_diff = 0;\n int size = bitstream_read(&bc, 16);\n s->color_depth = 0;\n s->zlibprime_curr = 0;\n s->zlibprime_prev = 0;\n s->diff_start = 0;\n s->diff_height = cur_blk_height;\n if (8 * size > bitstream_bits_left(&bc)) {\n av_frame_unref(s->frame);\n return AVERROR_INVALIDDATA;\n }\n if (s->ver == 2 && size) {\n bitstream_skip(&bc, 3);\n s->color_depth = bitstream_read(&bc, 2);\n has_diff = bitstream_read_bit(&bc);\n s->zlibprime_curr = bitstream_read_bit(&bc);\n s->zlibprime_prev = bitstream_read_bit(&bc);\n if (s->color_depth != 0 && s->color_depth != 2) {\n av_log(avctx, AV_LOG_ERROR,\n "%dx%d invalid color depth %d\\n",\n i, j, s->color_depth);\n return AVERROR_INVALIDDATA;\n }\n if (has_diff) {\n if (!s->keyframe) {\n av_log(avctx, AV_LOG_ERROR,\n "Inter frame without keyframe\\n");\n return AVERROR_INVALIDDATA;\n }\n s->diff_start = bitstream_read(&bc, 8);\n s->diff_height = bitstream_read(&bc, 8);\n if (s->diff_start + s->diff_height > cur_blk_height) {\n av_log(avctx, AV_LOG_ERROR,\n "Block parameters invalid: %d + %d > %d\\n",\n s->diff_start, s->diff_height, cur_blk_height);\n return AVERROR_INVALIDDATA;\n }\n av_log(avctx, AV_LOG_DEBUG,\n "%dx%d diff start %d height %d\\n",\n i, j, s->diff_start, s->diff_height);\n size -= 2;\n }\n if (s->zlibprime_prev)\n av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_prev\\n", i, j);\n if (s->zlibprime_curr) {\n int col = bitstream_read(&bc, 8);\n int row = bitstream_read(&bc, 8);\n av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_curr %dx%d\\n",\n i, j, col, row);\n size -= 2;\n avpriv_request_sample(avctx, "zlibprime_curr");\n return AVERROR_PATCHWELCOME;\n }\n if (!s->blocks && (s->zlibprime_curr || s->zlibprime_prev)) {\n av_log(avctx, AV_LOG_ERROR,\n "no data available for zlib priming\\n");\n return AVERROR_INVALIDDATA;\n }\n size--;\n }\n if (has_diff) {\n int k;\n int off = (s->image_height - y_pos - 1) * s->frame->linesize[0];\n for (k = 0; k < cur_blk_height; k++) {\n int x = off - k * s->frame->linesize[0] + x_pos * 3;\n memcpy(s->frame->data[0] + x, s->keyframe + x,\n cur_blk_width * 3);\n }\n }\n if (size) {\n if (flashsv_decode_block(avctx, avpkt, &bc, size,\n cur_blk_width, cur_blk_height,\n x_pos, y_pos,\n i + j * (h_blocks + !!h_part)))\n av_log(avctx, AV_LOG_ERROR,\n "error in decompression of block %dx%d\\n", i, j);\n }\n }\n }\n if (s->is_keyframe && s->ver == 2) {\n if (!s->keyframe) {\n s->keyframe = av_malloc(s->frame->linesize[0] * avctx->height);\n if (!s->keyframe) {\n av_log(avctx, AV_LOG_ERROR, "Cannot allocate image data\\n");\n return AVERROR(ENOMEM);\n }\n }\n memcpy(s->keyframe, s->frame->data[0],\n s->frame->linesize[0] * avctx->height);\n }\n if ((ret = av_frame_ref(data, s->frame)) < 0)\n return ret;\n *got_frame = 1;\n if ((bitstream_tell(&bc) / 8) != buf_size)\n av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\\n",\n buf_size, (bitstream_tell(&bc) / 8));\n return buf_size;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n <= bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n skip_remaining(bc, bc->bits_left);\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
|
2,831
| 0
|
https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/core/ngx_radix_tree.c/#L287
|
static void *
ngx_radix_alloc(ngx_radix_tree_t *tree)
{
char *p;
if (tree->free) {
p = (char *) tree->free;
tree->free = tree->free->right;
return p;
}
if (tree->size < sizeof(ngx_radix_node_t)) {
tree->start = ngx_pmemalign(tree->pool, ngx_pagesize, ngx_pagesize);
if (tree->start == NULL) {
return NULL;
}
tree->size = ngx_pagesize;
}
p = tree->start;
tree->start += sizeof(ngx_radix_node_t);
tree->size -= sizeof(ngx_radix_node_t);
return p;
}
|
['ngx_int_t\nngx_radix32tree_insert(ngx_radix_tree_t *tree, uint32_t key, uint32_t mask,\n uintptr_t value)\n{\n uint32_t bit;\n ngx_radix_node_t *node, *next;\n bit = 0x80000000;\n node = tree->root;\n next = tree->root;\n while (bit & mask) {\n if (key & bit) {\n next = node->right;\n } else {\n next = node->left;\n }\n if (next == NULL) {\n break;\n }\n bit >>= 1;\n node = next;\n }\n if (next) {\n if (node->value != NGX_RADIX_NO_VALUE) {\n return NGX_BUSY;\n }\n node->value = value;\n return NGX_OK;\n }\n while (bit & mask) {\n next = ngx_radix_alloc(tree);\n if (next == NULL) {\n return NGX_ERROR;\n }\n next->right = NULL;\n next->left = NULL;\n next->parent = node;\n next->value = NGX_RADIX_NO_VALUE;\n if (key & bit) {\n node->right = next;\n } else {\n node->left = next;\n }\n bit >>= 1;\n node = next;\n }\n node->value = value;\n return NGX_OK;\n}', 'static void *\nngx_radix_alloc(ngx_radix_tree_t *tree)\n{\n char *p;\n if (tree->free) {\n p = (char *) tree->free;\n tree->free = tree->free->right;\n return p;\n }\n if (tree->size < sizeof(ngx_radix_node_t)) {\n tree->start = ngx_pmemalign(tree->pool, ngx_pagesize, ngx_pagesize);\n if (tree->start == NULL) {\n return NULL;\n }\n tree->size = ngx_pagesize;\n }\n p = tree->start;\n tree->start += sizeof(ngx_radix_node_t);\n tree->size -= sizeof(ngx_radix_node_t);\n return p;\n}']
|
2,832
| 0
|
https://github.com/libav/libav/blob/d6e49096c0c3c10ffb176761b0da150c93bedbf6/libavformat/smush.c/#L185
|
static int smush_read_header(AVFormatContext *ctx)
{
SMUSHContext *smush = ctx->priv_data;
AVIOContext *pb = ctx->pb;
AVStream *vst, *ast;
uint32_t magic, nframes, size, subversion, i;
uint32_t width = 0, height = 0, got_audio = 0, read = 0;
uint32_t sample_rate, channels, palette[256];
magic = avio_rb32(pb);
avio_skip(pb, 4);
if (magic == MKBETAG('A', 'N', 'I', 'M')) {
if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
return AVERROR_INVALIDDATA;
size = avio_rb32(pb);
if (size < 3 * 256 + 6)
return AVERROR_INVALIDDATA;
smush->version = 0;
subversion = avio_rl16(pb);
nframes = avio_rl16(pb);
if (!nframes)
return AVERROR_INVALIDDATA;
avio_skip(pb, 2);
for (i = 0; i < 256; i++)
palette[i] = avio_rb24(pb);
avio_skip(pb, size - (3 * 256 + 6));
} else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
return AVERROR_INVALIDDATA;
size = avio_rb32(pb);
if (size < 14)
return AVERROR_INVALIDDATA;
smush->version = 1;
subversion = avio_rl16(pb);
nframes = avio_rl32(pb);
if (!nframes)
return AVERROR_INVALIDDATA;
avio_skip(pb, 2);
width = avio_rl16(pb);
height = avio_rl16(pb);
avio_skip(pb, 2);
avio_skip(pb, size - 14);
if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
return AVERROR_INVALIDDATA;
size = avio_rb32(pb);
while (!got_audio && ((read + 8) < size)) {
uint32_t sig, chunk_size;
if (pb->eof_reached)
return AVERROR_EOF;
sig = avio_rb32(pb);
chunk_size = avio_rb32(pb);
read += 8;
switch (sig) {
case MKBETAG('W', 'a', 'v', 'e'):
got_audio = 1;
sample_rate = avio_rl32(pb);
if (!sample_rate)
return AVERROR_INVALIDDATA;
channels = avio_rl32(pb);
if (!channels)
return AVERROR_INVALIDDATA;
avio_skip(pb, chunk_size - 8);
read += chunk_size;
break;
case MKBETAG('B', 'l', '1', '6'):
case MKBETAG('A', 'N', 'N', 'O'):
avio_skip(pb, chunk_size);
read += chunk_size;
break;
default:
return AVERROR_INVALIDDATA;
break;
}
}
avio_skip(pb, size - read);
} else {
av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
return AVERROR_INVALIDDATA;
}
vst = avformat_new_stream(ctx, 0);
if (!vst)
return AVERROR(ENOMEM);
smush->video_stream_index = vst->index;
avpriv_set_pts_info(vst, 64, 1, 15);
vst->start_time = 0;
vst->duration =
vst->nb_frames = nframes;
vst->avg_frame_rate = av_inv_q(vst->time_base);
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
vst->codecpar->codec_id = AV_CODEC_ID_SANM;
vst->codecpar->codec_tag = 0;
vst->codecpar->width = width;
vst->codecpar->height = height;
if (!smush->version) {
av_free(vst->codecpar->extradata);
vst->codecpar->extradata_size = 1024 + 2;
vst->codecpar->extradata = av_malloc(vst->codecpar->extradata_size +
AV_INPUT_BUFFER_PADDING_SIZE);
if (!vst->codecpar->extradata)
return AVERROR(ENOMEM);
AV_WL16(vst->codecpar->extradata, subversion);
for (i = 0; i < 256; i++)
AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
}
if (got_audio) {
ast = avformat_new_stream(ctx, 0);
if (!ast)
return AVERROR(ENOMEM);
smush->audio_stream_index = ast->index;
ast->start_time = 0;
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_VIMA;
ast->codecpar->codec_tag = 0;
ast->codecpar->sample_rate = sample_rate;
ast->codecpar->channels = channels;
avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
}
return 0;
}
|
['static int smush_read_header(AVFormatContext *ctx)\n{\n SMUSHContext *smush = ctx->priv_data;\n AVIOContext *pb = ctx->pb;\n AVStream *vst, *ast;\n uint32_t magic, nframes, size, subversion, i;\n uint32_t width = 0, height = 0, got_audio = 0, read = 0;\n uint32_t sample_rate, channels, palette[256];\n magic = avio_rb32(pb);\n avio_skip(pb, 4);\n if (magic == MKBETAG(\'A\', \'N\', \'I\', \'M\')) {\n if (avio_rb32(pb) != MKBETAG(\'A\', \'H\', \'D\', \'R\'))\n return AVERROR_INVALIDDATA;\n size = avio_rb32(pb);\n if (size < 3 * 256 + 6)\n return AVERROR_INVALIDDATA;\n smush->version = 0;\n subversion = avio_rl16(pb);\n nframes = avio_rl16(pb);\n if (!nframes)\n return AVERROR_INVALIDDATA;\n avio_skip(pb, 2);\n for (i = 0; i < 256; i++)\n palette[i] = avio_rb24(pb);\n avio_skip(pb, size - (3 * 256 + 6));\n } else if (magic == MKBETAG(\'S\', \'A\', \'N\', \'M\')) {\n if (avio_rb32(pb) != MKBETAG(\'S\', \'H\', \'D\', \'R\'))\n return AVERROR_INVALIDDATA;\n size = avio_rb32(pb);\n if (size < 14)\n return AVERROR_INVALIDDATA;\n smush->version = 1;\n subversion = avio_rl16(pb);\n nframes = avio_rl32(pb);\n if (!nframes)\n return AVERROR_INVALIDDATA;\n avio_skip(pb, 2);\n width = avio_rl16(pb);\n height = avio_rl16(pb);\n avio_skip(pb, 2);\n avio_skip(pb, size - 14);\n if (avio_rb32(pb) != MKBETAG(\'F\', \'L\', \'H\', \'D\'))\n return AVERROR_INVALIDDATA;\n size = avio_rb32(pb);\n while (!got_audio && ((read + 8) < size)) {\n uint32_t sig, chunk_size;\n if (pb->eof_reached)\n return AVERROR_EOF;\n sig = avio_rb32(pb);\n chunk_size = avio_rb32(pb);\n read += 8;\n switch (sig) {\n case MKBETAG(\'W\', \'a\', \'v\', \'e\'):\n got_audio = 1;\n sample_rate = avio_rl32(pb);\n if (!sample_rate)\n return AVERROR_INVALIDDATA;\n channels = avio_rl32(pb);\n if (!channels)\n return AVERROR_INVALIDDATA;\n avio_skip(pb, chunk_size - 8);\n read += chunk_size;\n break;\n case MKBETAG(\'B\', \'l\', \'1\', \'6\'):\n case MKBETAG(\'A\', \'N\', \'N\', \'O\'):\n avio_skip(pb, chunk_size);\n read += chunk_size;\n break;\n default:\n return AVERROR_INVALIDDATA;\n break;\n }\n }\n avio_skip(pb, size - read);\n } else {\n av_log(ctx, AV_LOG_ERROR, "Wrong magic\\n");\n return AVERROR_INVALIDDATA;\n }\n vst = avformat_new_stream(ctx, 0);\n if (!vst)\n return AVERROR(ENOMEM);\n smush->video_stream_index = vst->index;\n avpriv_set_pts_info(vst, 64, 1, 15);\n vst->start_time = 0;\n vst->duration =\n vst->nb_frames = nframes;\n vst->avg_frame_rate = av_inv_q(vst->time_base);\n vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;\n vst->codecpar->codec_id = AV_CODEC_ID_SANM;\n vst->codecpar->codec_tag = 0;\n vst->codecpar->width = width;\n vst->codecpar->height = height;\n if (!smush->version) {\n av_free(vst->codecpar->extradata);\n vst->codecpar->extradata_size = 1024 + 2;\n vst->codecpar->extradata = av_malloc(vst->codecpar->extradata_size +\n AV_INPUT_BUFFER_PADDING_SIZE);\n if (!vst->codecpar->extradata)\n return AVERROR(ENOMEM);\n AV_WL16(vst->codecpar->extradata, subversion);\n for (i = 0; i < 256; i++)\n AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);\n }\n if (got_audio) {\n ast = avformat_new_stream(ctx, 0);\n if (!ast)\n return AVERROR(ENOMEM);\n smush->audio_stream_index = ast->index;\n ast->start_time = 0;\n ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;\n ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_VIMA;\n ast->codecpar->codec_tag = 0;\n ast->codecpar->sample_rate = sample_rate;\n ast->codecpar->channels = channels;\n avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);\n }\n return 0;\n}']
|
2,833
| 0
|
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_shift.c/#L150
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i,nw,lb,rb;
BN_ULONG *t,*f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
r->neg=a->neg;
nw=n/BN_BITS2;
if (bn_wexpand(r,a->top+nw+1) == NULL) return(0);
lb=n%BN_BITS2;
rb=BN_BITS2-lb;
f=a->d;
t=r->d;
t[a->top+nw]=0;
if (lb == 0)
for (i=a->top-1; i>=0; i--)
t[nw+i]=f[i];
else
for (i=a->top-1; i>=0; i--)
{
l=f[i];
t[nw+i+1]|=(l>>rb)&BN_MASK2;
t[nw+i]=(l<<lb)&BN_MASK2;
}
memset(t,0,nw*sizeof(t[0]));
r->top=a->top+nw+1;
bn_correct_top(r);
bn_check_top(r);
return(1);
}
|
['static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)\n\t{\n\tstruct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);\n\tDSA *dsa=NULL;\n\tif (data->sign_param_nid == NID_undef)\n\t\t{\n\t\t\tGOSTerr(GOST_F_PKEY_GOST94CP_KEYGEN,\n\t\t\t\tGOST_R_NO_PARAMETERS_SET);\n\t\t\treturn 0;\n\t\t}\n\tdsa = DSA_new();\n\tif (!fill_GOST94_params(dsa,data->sign_param_nid))\n\t\t{\n\t\tDSA_free(dsa);\n\t\treturn 0;\n\t\t}\n\tgost_sign_keygen(dsa);\n\tEVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa);\n\treturn 1;\n\t}', 'int fill_GOST94_params(DSA *dsa,int nid)\n\t{\n\tR3410_params *params=R3410_paramset;\n\twhile (params->nid!=NID_undef && params->nid !=nid) params++;\n\tif (params->nid == NID_undef)\n\t\t{\n\t\tGOSTerr(GOST_F_FILL_GOST94_PARAMS,GOST_R_UNSUPPORTED_PARAMETER_SET);\n\t\treturn 0;\n\t\t}\n#define dump_signature(a,b,c)\n\tif (dsa->p) { BN_free(dsa->p); }\n\tdsa->p=NULL;\n\tBN_dec2bn(&(dsa->p),params->p);\n\tif (dsa->q) { BN_free(dsa->q); }\n\tdsa->q=NULL;\n\tBN_dec2bn(&(dsa->q),params->q);\n\tif (dsa->g) { BN_free(dsa->g); }\n\tdsa->g=NULL;\n\tBN_dec2bn(&(dsa->g),params->a);\n\treturn 1;\n\t}', "int BN_dec2bn(BIGNUM **bn, const char *a)\n\t{\n\tBIGNUM *ret=NULL;\n\tBN_ULONG l=0;\n\tint neg=0,i,j;\n\tint num;\n\tif ((a == NULL) || (*a == '\\0')) return(0);\n\tif (*a == '-') { neg=1; a++; }\n\tfor (i=0; isdigit((unsigned char) a[i]); i++)\n\t\t;\n\tnum=i+neg;\n\tif (bn == NULL) return(num);\n\tif (*bn == NULL)\n\t\t{\n\t\tif ((ret=BN_new()) == NULL) return(0);\n\t\t}\n\telse\n\t\t{\n\t\tret= *bn;\n\t\tBN_zero(ret);\n\t\t}\n\tif (bn_expand(ret,i*4) == NULL) goto err;\n\tj=BN_DEC_NUM-(i%BN_DEC_NUM);\n\tif (j == BN_DEC_NUM) j=0;\n\tl=0;\n\twhile (*a)\n\t\t{\n\t\tl*=10;\n\t\tl+= *a-'0';\n\t\ta++;\n\t\tif (++j == BN_DEC_NUM)\n\t\t\t{\n\t\t\tBN_mul_word(ret,BN_DEC_CONV);\n\t\t\tBN_add_word(ret,l);\n\t\t\tl=0;\n\t\t\tj=0;\n\t\t\t}\n\t\t}\n\tret->neg=neg;\n\tbn_correct_top(ret);\n\t*bn=ret;\n\tbn_check_top(ret);\n\treturn(num);\nerr:\n\tif (*bn == NULL) BN_free(ret);\n\treturn(0);\n\t}", 'int gost_sign_keygen(DSA *dsa)\n\t{\n\tdsa->priv_key = BN_new();\n\tBN_rand_range(dsa->priv_key,dsa->q);\n\treturn gost94_compute_public( dsa);\n\t}', 'int gost94_compute_public(DSA *dsa)\n\t{\n\tBN_CTX *ctx = BN_CTX_new();\n\tif (!dsa->g)\n\t\t{\n\t\tGOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC,GOST_R_KEY_IS_NOT_INITALIZED);\n\t\treturn 0;\n\t\t}\n\tdsa->pub_key=BN_new();\n\tBN_mod_exp(dsa->pub_key, dsa->g,dsa->priv_key,dsa->p,ctx);\n\tBN_CTX_free(ctx);\n\treturn 1;\n\t}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n\t BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{\n# ifdef MONT_EXP_WORD\n\t\tif (a->top == 1 && !a->neg && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0))\n\t\t\t{\n\t\t\tBN_ULONG A = a->d[0];\n\t\t\tret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);\n\t\t\t}\n\t\telse\n# endif\n\t\t\tret=BN_mod_exp_mont(r,a,p,m,ctx,NULL);\n\t\t}\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\tbn_check_top(r);\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1;\n\tBIGNUM *d,*r;\n\tconst BIGNUM *aa;\n\tBIGNUM *val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tif (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)\n\t\t{\n\t\treturn BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!BN_is_odd(m))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\td = BN_CTX_get(ctx);\n\tr = BN_CTX_get(ctx);\n\tval[0] = BN_CTX_get(ctx);\n\tif (!d || !r || !val[0]) goto err;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_nnmod(val[0],a,m,ctx))\n\t\t\tgoto err;\n\t\taa= val[0];\n\t\t}\n\telse\n\t\taa=a;\n\tif (BN_is_zero(aa))\n\t\t{\n\t\tBN_zero(rr);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\tif (!BN_to_montgomery(val[0],aa,mont,ctx)) goto err;\n\twindow = BN_window_bits_for_exponent_size(bits);\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(d,val[0],val[0],mont,ctx)) goto err;\n\t\tj=1<<(window-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_montgomery(val[i],val[i-1],\n\t\t\t\t\t\td,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n\tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_montgomery(r,r,val[wvalue>>1],mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tif (!BN_from_montgomery(rr,r,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tBN_CTX_end(ctx);\n\tbn_check_top(rr);\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,bits,ret=0,idx,window,wvalue;\n\tsize_t top;\n \tBIGNUM *r;\n\tconst BIGNUM *aa;\n\tBN_MONT_CTX *mont=NULL;\n\tint numPowers;\n\tunsigned char *powerbufFree=NULL;\n\tsize_t powerbufLen = 0;\n\tunsigned char *powerbuf=NULL;\n\tBIGNUM *computeTemp=NULL, *am=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\ttop = m->top;\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\tr = BN_CTX_get(ctx);\n\tif (r == NULL) goto err;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\twindow = BN_window_bits_for_ctime_exponent_size(bits);\n\tnumPowers = 1 << window;\n\tpowerbufLen = sizeof(m->d[0])*top*numPowers;\n\tif ((powerbufFree=OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)\n\t\tgoto err;\n\tpowerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n\tmemset(powerbuf, 0, powerbufLen);\n \tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers)) goto err;\n\tcomputeTemp = BN_CTX_get(ctx);\n\tam = BN_CTX_get(ctx);\n\tif (computeTemp==NULL || am==NULL) goto err;\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(am,a,m,ctx))\n\t\t\tgoto err;\n\t\taa= am;\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(am,aa,mont,ctx)) goto err;\n\tif (!BN_copy(computeTemp, am)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers)) goto err;\n\tif (window > 1)\n\t\t{\n\t\tfor (i=2; i<numPowers; i++)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(computeTemp,am,computeTemp,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(computeTemp, top, powerbuf, i, numPowers)) goto err;\n\t\t\t}\n\t\t}\n \tbits = ((bits+window-1)/window)*window;\n \tidx=bits-1;\n \twhile (idx >= 0)\n \t\t{\n \t\twvalue=0;\n \t\tfor (i=0; i<window; i++,idx--)\n \t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\tgoto err;\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,idx);\n \t\t\t}\n\t\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(computeTemp, top, powerbuf, wvalue, numPowers)) goto err;\n \t\tif (!BN_mod_mul_montgomery(r,r,computeTemp,mont,ctx)) goto err;\n \t\t}\n\tif (!BN_from_montgomery(rr,r,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tif (powerbuf!=NULL)\n\t\t{\n\t\tOPENSSL_cleanse(powerbuf,powerbufLen);\n\t\tOPENSSL_free(powerbufFree);\n\t\t}\n \tif (am!=NULL) BN_clear(am);\n \tif (computeTemp!=NULL) BN_clear(computeTemp);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i;\n\tsize_t loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tsize_t num_n,div_n;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp, div_n+1)) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tsize_t num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,loop+1U)) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,div_n+1U)) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n\t{\n\tint i,nw,lb,rb;\n\tBN_ULONG *t,*f;\n\tBN_ULONG l;\n\tbn_check_top(r);\n\tbn_check_top(a);\n\tr->neg=a->neg;\n\tnw=n/BN_BITS2;\n\tif (bn_wexpand(r,a->top+nw+1) == NULL) return(0);\n\tlb=n%BN_BITS2;\n\trb=BN_BITS2-lb;\n\tf=a->d;\n\tt=r->d;\n\tt[a->top+nw]=0;\n\tif (lb == 0)\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\tt[nw+i]=f[i];\n\telse\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\t{\n\t\t\tl=f[i];\n\t\t\tt[nw+i+1]|=(l>>rb)&BN_MASK2;\n\t\t\tt[nw+i]=(l<<lb)&BN_MASK2;\n\t\t\t}\n\tmemset(t,0,nw*sizeof(t[0]));\n\tr->top=a->top+nw+1;\n\tbn_correct_top(r);\n\tbn_check_top(r);\n\treturn(1);\n\t}']
|
2,834
| 0
|
https://github.com/openssl/openssl/blob/01b7851aa27aa144372f5484da916be042d9aa4f/crypto/ts/ts_rsp_verify.c/#L423
|
static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,
PKCS7 *token, TS_TST_INFO *tst_info)
{
X509 *signer = NULL;
GENERAL_NAME *tsa_name = tst_info->tsa;
X509_ALGOR *md_alg = NULL;
unsigned char *imprint = NULL;
unsigned imprint_len = 0;
int ret = 0;
if ((ctx->flags & TS_VFY_SIGNATURE)
&& !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))
goto err;
if ((ctx->flags & TS_VFY_VERSION)
&& TS_TST_INFO_get_version(tst_info) != 1) {
TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION);
goto err;
}
if ((ctx->flags & TS_VFY_POLICY)
&& !ts_check_policy(ctx->policy, tst_info))
goto err;
if ((ctx->flags & TS_VFY_IMPRINT)
&& !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,
tst_info))
goto err;
if ((ctx->flags & TS_VFY_DATA)
&& (!ts_compute_imprint(ctx->data, tst_info,
&md_alg, &imprint, &imprint_len)
|| !ts_check_imprints(md_alg, imprint, imprint_len, tst_info)))
goto err;
if ((ctx->flags & TS_VFY_NONCE)
&& !ts_check_nonces(ctx->nonce, tst_info))
goto err;
if ((ctx->flags & TS_VFY_SIGNER)
&& tsa_name && !ts_check_signer_name(tsa_name, signer)) {
TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH);
goto err;
}
if ((ctx->flags & TS_VFY_TSA_NAME)
&& !ts_check_signer_name(ctx->tsa_name, signer)) {
TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED);
goto err;
}
ret = 1;
err:
X509_free(signer);
X509_ALGOR_free(md_alg);
OPENSSL_free(imprint);
return ret;
}
|
['static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,\n PKCS7 *token, TS_TST_INFO *tst_info)\n{\n X509 *signer = NULL;\n GENERAL_NAME *tsa_name = tst_info->tsa;\n X509_ALGOR *md_alg = NULL;\n unsigned char *imprint = NULL;\n unsigned imprint_len = 0;\n int ret = 0;\n if ((ctx->flags & TS_VFY_SIGNATURE)\n && !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))\n goto err;\n if ((ctx->flags & TS_VFY_VERSION)\n && TS_TST_INFO_get_version(tst_info) != 1) {\n TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION);\n goto err;\n }\n if ((ctx->flags & TS_VFY_POLICY)\n && !ts_check_policy(ctx->policy, tst_info))\n goto err;\n if ((ctx->flags & TS_VFY_IMPRINT)\n && !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,\n tst_info))\n goto err;\n if ((ctx->flags & TS_VFY_DATA)\n && (!ts_compute_imprint(ctx->data, tst_info,\n &md_alg, &imprint, &imprint_len)\n || !ts_check_imprints(md_alg, imprint, imprint_len, tst_info)))\n goto err;\n if ((ctx->flags & TS_VFY_NONCE)\n && !ts_check_nonces(ctx->nonce, tst_info))\n goto err;\n if ((ctx->flags & TS_VFY_SIGNER)\n && tsa_name && !ts_check_signer_name(tsa_name, signer)) {\n TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH);\n goto err;\n }\n if ((ctx->flags & TS_VFY_TSA_NAME)\n && !ts_check_signer_name(ctx->tsa_name, signer)) {\n TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED);\n goto err;\n }\n ret = 1;\n err:\n X509_free(signer);\n X509_ALGOR_free(md_alg);\n OPENSSL_free(imprint);\n return ret;\n}', 'static int ts_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info)\n{\n ASN1_OBJECT *resp_oid = tst_info->policy_id;\n if (OBJ_cmp(req_oid, resp_oid) != 0) {\n TSerr(TS_F_TS_CHECK_POLICY, TS_R_POLICY_MISMATCH);\n return 0;\n }\n return 1;\n}', 'int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b)\n{\n int ret;\n ret = (a->length - b->length);\n if (ret)\n return (ret);\n return (memcmp(a->data, b->data, a->length));\n}']
|
2,835
| 0
|
https://github.com/libav/libav/blob/10d39405fa82473367e1ba1ed3c4ecbeca2a7986/ffmpeg.c/#L3652
|
static int opt_streamid(const char *opt, const char *arg)
{
int idx;
char *p;
char idx_str[16];
strncpy(idx_str, arg, sizeof(idx_str));
idx_str[sizeof(idx_str)-1] = '\0';
p = strchr(idx_str, ':');
if (!p) {
fprintf(stderr,
"Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
arg, opt);
ffmpeg_exit(1);
}
*p++ = '\0';
idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);
streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1);
streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
return 0;
}
|
['static int opt_streamid(const char *opt, const char *arg)\n{\n int idx;\n char *p;\n char idx_str[16];\n strncpy(idx_str, arg, sizeof(idx_str));\n idx_str[sizeof(idx_str)-1] = \'\\0\';\n p = strchr(idx_str, \':\');\n if (!p) {\n fprintf(stderr,\n "Invalid value \'%s\' for option \'%s\', required syntax is \'index:value\'\\n",\n arg, opt);\n ffmpeg_exit(1);\n }\n *p++ = \'\\0\';\n idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);\n streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1);\n streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);\n return 0;\n}', 'static void *grow_array(void *array, int elem_size, int *size, int new_size)\n{\n if (new_size >= INT_MAX / elem_size) {\n fprintf(stderr, "Array too big.\\n");\n ffmpeg_exit(1);\n }\n if (*size < new_size) {\n uint8_t *tmp = av_realloc(array, new_size*elem_size);\n if (!tmp) {\n fprintf(stderr, "Could not alloc buffer.\\n");\n ffmpeg_exit(1);\n }\n memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);\n *size = new_size;\n return tmp;\n }\n return array;\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if(!ptr) return av_malloc(size);\n diff= ((char*)ptr)[-1];\n return (char*)realloc((char*)ptr - diff, size + diff) + diff;\n#else\n return realloc(ptr, size);\n#endif\n}']
|
2,836
| 0
|
https://github.com/libav/libav/blob/77b0443544fd5f5c3f974b7a4fa4f2f18f7ba8df/ffmpeg.c/#L3600
|
static void opt_target(const char *arg)
{
int norm = -1;
static const char *const frame_rates[] = {"25", "30000/1001", "24000/1001"};
if(!strncmp(arg, "pal-", 4)) {
norm = 0;
arg += 4;
} else if(!strncmp(arg, "ntsc-", 5)) {
norm = 1;
arg += 5;
} else if(!strncmp(arg, "film-", 5)) {
norm = 2;
arg += 5;
} else {
int fr;
fr = (int)(frame_rate.num * 1000.0 / frame_rate.den);
if(fr == 25000) {
norm = 0;
} else if((fr == 29970) || (fr == 23976)) {
norm = 1;
} else {
if(nb_input_files) {
int i, j;
for(j = 0; j < nb_input_files; j++) {
for(i = 0; i < input_files[j]->nb_streams; i++) {
AVCodecContext *c = input_files[j]->streams[i]->codec;
if(c->codec_type != CODEC_TYPE_VIDEO)
continue;
fr = c->time_base.den * 1000 / c->time_base.num;
if(fr == 25000) {
norm = 0;
break;
} else if((fr == 29970) || (fr == 23976)) {
norm = 1;
break;
}
}
if(norm >= 0)
break;
}
}
}
if(verbose && norm >= 0)
fprintf(stderr, "Assuming %s for target.\n", norm ? "NTSC" : "PAL");
}
if(norm < 0) {
fprintf(stderr, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
fprintf(stderr, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
fprintf(stderr, "or set a framerate with \"-r xxx\".\n");
av_exit(1);
}
if(!strcmp(arg, "vcd")) {
opt_video_codec("mpeg1video");
opt_audio_codec("mp2");
opt_format("vcd");
opt_frame_size(norm ? "352x240" : "352x288");
opt_frame_rate(NULL, frame_rates[norm]);
opt_default("gop", norm ? "18" : "15");
opt_default("b", "1150000");
opt_default("maxrate", "1150000");
opt_default("minrate", "1150000");
opt_default("bufsize", "327680");
opt_default("ab", "224000");
audio_sample_rate = 44100;
audio_channels = 2;
opt_default("packetsize", "2324");
opt_default("muxrate", "1411200");
mux_preload= (36000+3*1200) / 90000.0;
} else if(!strcmp(arg, "svcd")) {
opt_video_codec("mpeg2video");
opt_audio_codec("mp2");
opt_format("svcd");
opt_frame_size(norm ? "480x480" : "480x576");
opt_frame_rate(NULL, frame_rates[norm]);
opt_default("gop", norm ? "18" : "15");
opt_default("b", "2040000");
opt_default("maxrate", "2516000");
opt_default("minrate", "0");
opt_default("bufsize", "1835008");
opt_default("flags", "+SCAN_OFFSET");
opt_default("ab", "224000");
audio_sample_rate = 44100;
opt_default("packetsize", "2324");
} else if(!strcmp(arg, "dvd")) {
opt_video_codec("mpeg2video");
opt_audio_codec("ac3");
opt_format("dvd");
opt_frame_size(norm ? "720x480" : "720x576");
opt_frame_rate(NULL, frame_rates[norm]);
opt_default("gop", norm ? "18" : "15");
opt_default("b", "6000000");
opt_default("maxrate", "9000000");
opt_default("minrate", "0");
opt_default("bufsize", "1835008");
opt_default("packetsize", "2048");
opt_default("muxrate", "10080000");
opt_default("ab", "448000");
audio_sample_rate = 48000;
} else if(!strncmp(arg, "dv", 2)) {
opt_format("dv");
opt_frame_size(norm ? "720x480" : "720x576");
opt_frame_pix_fmt(!strncmp(arg, "dv50", 4) ? "yuv422p" :
(norm ? "yuv411p" : "yuv420p"));
opt_frame_rate(NULL, frame_rates[norm]);
audio_sample_rate = 48000;
audio_channels = 2;
} else {
fprintf(stderr, "Unknown target: %s\n", arg);
av_exit(1);
}
}
|
['static void opt_target(const char *arg)\n{\n int norm = -1;\n static const char *const frame_rates[] = {"25", "30000/1001", "24000/1001"};\n if(!strncmp(arg, "pal-", 4)) {\n norm = 0;\n arg += 4;\n } else if(!strncmp(arg, "ntsc-", 5)) {\n norm = 1;\n arg += 5;\n } else if(!strncmp(arg, "film-", 5)) {\n norm = 2;\n arg += 5;\n } else {\n int fr;\n fr = (int)(frame_rate.num * 1000.0 / frame_rate.den);\n if(fr == 25000) {\n norm = 0;\n } else if((fr == 29970) || (fr == 23976)) {\n norm = 1;\n } else {\n if(nb_input_files) {\n int i, j;\n for(j = 0; j < nb_input_files; j++) {\n for(i = 0; i < input_files[j]->nb_streams; i++) {\n AVCodecContext *c = input_files[j]->streams[i]->codec;\n if(c->codec_type != CODEC_TYPE_VIDEO)\n continue;\n fr = c->time_base.den * 1000 / c->time_base.num;\n if(fr == 25000) {\n norm = 0;\n break;\n } else if((fr == 29970) || (fr == 23976)) {\n norm = 1;\n break;\n }\n }\n if(norm >= 0)\n break;\n }\n }\n }\n if(verbose && norm >= 0)\n fprintf(stderr, "Assuming %s for target.\\n", norm ? "NTSC" : "PAL");\n }\n if(norm < 0) {\n fprintf(stderr, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\\n");\n fprintf(stderr, "Please prefix target with \\"pal-\\", \\"ntsc-\\" or \\"film-\\",\\n");\n fprintf(stderr, "or set a framerate with \\"-r xxx\\".\\n");\n av_exit(1);\n }\n if(!strcmp(arg, "vcd")) {\n opt_video_codec("mpeg1video");\n opt_audio_codec("mp2");\n opt_format("vcd");\n opt_frame_size(norm ? "352x240" : "352x288");\n opt_frame_rate(NULL, frame_rates[norm]);\n opt_default("gop", norm ? "18" : "15");\n opt_default("b", "1150000");\n opt_default("maxrate", "1150000");\n opt_default("minrate", "1150000");\n opt_default("bufsize", "327680");\n opt_default("ab", "224000");\n audio_sample_rate = 44100;\n audio_channels = 2;\n opt_default("packetsize", "2324");\n opt_default("muxrate", "1411200");\n mux_preload= (36000+3*1200) / 90000.0;\n } else if(!strcmp(arg, "svcd")) {\n opt_video_codec("mpeg2video");\n opt_audio_codec("mp2");\n opt_format("svcd");\n opt_frame_size(norm ? "480x480" : "480x576");\n opt_frame_rate(NULL, frame_rates[norm]);\n opt_default("gop", norm ? "18" : "15");\n opt_default("b", "2040000");\n opt_default("maxrate", "2516000");\n opt_default("minrate", "0");\n opt_default("bufsize", "1835008");\n opt_default("flags", "+SCAN_OFFSET");\n opt_default("ab", "224000");\n audio_sample_rate = 44100;\n opt_default("packetsize", "2324");\n } else if(!strcmp(arg, "dvd")) {\n opt_video_codec("mpeg2video");\n opt_audio_codec("ac3");\n opt_format("dvd");\n opt_frame_size(norm ? "720x480" : "720x576");\n opt_frame_rate(NULL, frame_rates[norm]);\n opt_default("gop", norm ? "18" : "15");\n opt_default("b", "6000000");\n opt_default("maxrate", "9000000");\n opt_default("minrate", "0");\n opt_default("bufsize", "1835008");\n opt_default("packetsize", "2048");\n opt_default("muxrate", "10080000");\n opt_default("ab", "448000");\n audio_sample_rate = 48000;\n } else if(!strncmp(arg, "dv", 2)) {\n opt_format("dv");\n opt_frame_size(norm ? "720x480" : "720x576");\n opt_frame_pix_fmt(!strncmp(arg, "dv50", 4) ? "yuv422p" :\n (norm ? "yuv411p" : "yuv420p"));\n opt_frame_rate(NULL, frame_rates[norm]);\n audio_sample_rate = 48000;\n audio_channels = 2;\n } else {\n fprintf(stderr, "Unknown target: %s\\n", arg);\n av_exit(1);\n }\n}']
|
2,837
| 0
|
https://github.com/openssl/openssl/blob/3ec9e4ec46eb4356bc106db5e0e33148c693c8f0/crypto/lhash/lhash.c/#L139
|
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
{
unsigned long hash;
OPENSSL_LH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return NULL;
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return ret;
}
|
['SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return NULL;\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return NULL;\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n if (RAND_get_rand_method() == RAND_OpenSSL()) {\n s->drbg =\n RAND_DRBG_new(0, 0, RAND_DRBG_get0_public());\n if (s->drbg == NULL\n || RAND_DRBG_instantiate(s->drbg,\n (const unsigned char *) SSL_version_str,\n sizeof(SSL_version_str) - 1) == 0)\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->max_early_data = ctx->max_early_data;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n RAND_DRBG_free(s->drbg);\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return 1;\n } else\n return 0;\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, r);\n SSL_SESSION_list_remove(ctx, r);\n }\n c->not_resumable = 1;\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret)\n SSL_SESSION_free(r);\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, c);\n } else\n ret = 0;\n return ret;\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return NULL;\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return ret;\n}']
|
2,838
| 0
|
https://github.com/openssl/openssl/blob/b2aea0e3d9a15e30ebce8b6da213df4a3f346155/test/handshake_helper.c/#L293
|
static int server_ocsp_cb(SSL *s, void *arg)
{
unsigned char *resp;
resp = OPENSSL_malloc(1);
if (resp == NULL)
return SSL_TLSEXT_ERR_ALERT_FATAL;
*resp = *(unsigned char *)arg;
if (!SSL_set_tlsext_status_ocsp_resp(s, resp, 1))
return SSL_TLSEXT_ERR_ALERT_FATAL;
return SSL_TLSEXT_ERR_OK;
}
|
['static int server_ocsp_cb(SSL *s, void *arg)\n{\n unsigned char *resp;\n resp = OPENSSL_malloc(1);\n if (resp == NULL)\n return SSL_TLSEXT_ERR_ALERT_FATAL;\n *resp = *(unsigned char *)arg;\n if (!SSL_set_tlsext_status_ocsp_resp(s, resp, 1))\n return SSL_TLSEXT_ERR_ALERT_FATAL;\n return SSL_TLSEXT_ERR_OK;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,839
| 0
|
https://github.com/openssl/openssl/blob/e7d961e994620dd5dee6d80794a07fb9de1bab66/ssl/packet.c/#L48
|
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
if (!ossl_assert(pkt->subs != NULL && len != 0))
return 0;
if (pkt->maxsize - pkt->written < len)
return 0;
if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
size_t newlen;
size_t reflen;
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
newlen = reflen * 2;
if (newlen < DEFAULT_BUF_SIZE)
newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
if (allocbytes != NULL)
*allocbytes = WPACKET_get_curr(pkt);
return 1;
}
|
['int tls_construct_certificate_request(SSL *s, WPACKET *pkt)\n{\n if (SSL_IS_TLS13(s)) {\n if (!WPACKET_put_bytes_u8(pkt, 0)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!tls_construct_extensions(s, pkt,\n SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,\n 0)) {\n return 0;\n }\n goto done;\n }\n if (!WPACKET_start_sub_packet_u8(pkt)\n || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (SSL_USE_SIGALGS(s)) {\n const uint16_t *psigs;\n size_t nl = tls12_get_psigalgs(s, 1, &psigs);\n if (!WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)\n || !tls12_copy_sigalgs(s, pkt, psigs, nl)\n || !WPACKET_close(pkt)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (!construct_ca_names(s, pkt)) {\n return 0;\n }\n done:\n s->s3->tmp.cert_request = 1;\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n if (!ossl_assert(pkt->subs != NULL))\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int ssl3_get_req_cert_type(SSL *s, WPACKET *pkt)\n{\n uint32_t alg_k, alg_a = 0;\n if (s->cert->ctype)\n return WPACKET_memcpy(pkt, s->cert->ctype, s->cert->ctype_len);\n ssl_set_sig_mask(&alg_a, s, SSL_SECOP_SIGALG_MASK);\n alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n#ifndef OPENSSL_NO_GOST\n if (s->version >= TLS1_VERSION && (alg_k & SSL_kGOST))\n return WPACKET_put_bytes_u8(pkt, TLS_CT_GOST01_SIGN)\n && WPACKET_put_bytes_u8(pkt, TLS_CT_GOST12_SIGN)\n && WPACKET_put_bytes_u8(pkt, TLS_CT_GOST12_512_SIGN);\n#endif\n if ((s->version == SSL3_VERSION) && (alg_k & SSL_kDHE)) {\n#ifndef OPENSSL_NO_DH\n# ifndef OPENSSL_NO_RSA\n if (!WPACKET_put_bytes_u8(pkt, SSL3_CT_RSA_EPHEMERAL_DH))\n return 0;\n# endif\n# ifndef OPENSSL_NO_DSA\n if (!WPACKET_put_bytes_u8(pkt, SSL3_CT_DSS_EPHEMERAL_DH))\n return 0;\n# endif\n#endif\n }\n#ifndef OPENSSL_NO_RSA\n if (!(alg_a & SSL_aRSA) && !WPACKET_put_bytes_u8(pkt, SSL3_CT_RSA_SIGN))\n return 0;\n#endif\n#ifndef OPENSSL_NO_DSA\n if (!(alg_a & SSL_aDSS) && !WPACKET_put_bytes_u8(pkt, SSL3_CT_DSS_SIGN))\n return 0;\n#endif\n#ifndef OPENSSL_NO_EC\n if (s->version >= TLS1_VERSION\n && !(alg_a & SSL_aECDSA)\n && !WPACKET_put_bytes_u8(pkt, TLS_CT_ECDSA_SIGN))\n return 0;\n#endif\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
|
2,840
| 0
|
https://github.com/libav/libav/blob/7ff018c1cb43a5fe5ee2049d325cdd785852067a/libavcodec/bitstream.h/#L237
|
static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
}
|
['static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)\n{\n BitstreamContext bc;\n PutBitContext pbc;\n uint8_t buf[3];\n int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;\n if (track->vos_len < 7)\n return -1;\n avio_wb32(pb, 11);\n ffio_wfourcc(pb, "dac3");\n bitstream_init(&bc, track->vos_data + 4, (track->vos_len - 4) * 8);\n fscod = bitstream_read(&bc, 2);\n frmsizecod = bitstream_read(&bc, 6);\n bsid = bitstream_read(&bc, 5);\n bsmod = bitstream_read(&bc, 3);\n acmod = bitstream_read(&bc, 3);\n if (acmod == 2) {\n bitstream_skip(&bc, 2);\n } else {\n if ((acmod & 1) && acmod != 1)\n bitstream_skip(&bc, 2);\n if (acmod & 4)\n bitstream_skip(&bc, 2);\n }\n lfeon = bitstream_read_bit(&bc);\n init_put_bits(&pbc, buf, sizeof(buf));\n put_bits(&pbc, 2, fscod);\n put_bits(&pbc, 5, bsid);\n put_bits(&pbc, 3, bsmod);\n put_bits(&pbc, 3, acmod);\n put_bits(&pbc, 1, lfeon);\n put_bits(&pbc, 5, frmsizecod >> 1);\n put_bits(&pbc, 5, 0);\n flush_put_bits(&pbc);\n avio_write(pb, buf, sizeof(buf));\n return 11;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n <= bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n skip_remaining(bc, bc->bits_left);\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
|
2,841
| 0
|
https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/crypto/bn/bn_shift.c/#L112
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
}
|
['static int test_exp_mod_zero()\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL;\n BIGNUM *r = NULL;\n BN_ULONG one_word = 1;\n BN_CTX *ctx = BN_CTX_new();\n int ret = 1, failed = 0;\n m = BN_new();\n if (!m)\n goto err;\n BN_one(m);\n a = BN_new();\n if (!a)\n goto err;\n BN_one(a);\n p = BN_new();\n if (!p)\n goto err;\n BN_zero(p);\n r = BN_new();\n if (!r)\n goto err;\n if (!BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto err;\n if (!BN_mod_exp(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp", r, a))\n failed = 1;\n if (!BN_mod_exp_recp(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a))\n failed = 1;\n if (!BN_mod_exp_simple(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a))\n failed = 1;\n if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a))\n failed = 1;\n if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) {\n goto err;\n }\n if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))\n failed = 1;\n if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))\n goto err;\n if (!BN_is_zero(r)) {\n fprintf(stderr, "BN_mod_exp_mont_word failed:\\n");\n fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\\n");\n fprintf(stderr, "r = ");\n BN_print_fp(stderr, r);\n fprintf(stderr, "\\n");\n return 0;\n }\n ret = failed;\n err:\n BN_free(r);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (!d || !r || !val[0])\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
2,842
| 0
|
https://github.com/openssl/openssl/blob/1145e03870dd82eae00bb45e0b2162494b9b2f38/ssl/s3_clnt.c/#L2306
|
static int ssl3_check_cert_and_algorithm(SSL *s)
{
int i,idx;
long algs;
EVP_PKEY *pkey=NULL;
SESS_CERT *sc;
#ifndef OPENSSL_NO_RSA
RSA *rsa;
#endif
#ifndef OPENSSL_NO_DH
DH *dh;
#endif
sc=s->session->sess_cert;
if (sc == NULL)
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
goto err;
}
algs=s->s3->tmp.new_cipher->algorithms;
if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
return(1);
#ifndef OPENSSL_NO_RSA
rsa=s->session->sess_cert->peer_rsa_tmp;
#endif
#ifndef OPENSSL_NO_DH
dh=s->session->sess_cert->peer_dh_tmp;
#endif
idx=sc->peer_cert_type;
#ifndef OPENSSL_NO_ECDH
if (idx == SSL_PKEY_ECC)
{
if (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
s->s3->tmp.new_cipher) == 0)
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
goto f_err;
}
else
{
return 1;
}
}
#endif
pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
EVP_PKEY_free(pkey);
if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
goto f_err;
}
#ifndef OPENSSL_NO_DSA
else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
goto f_err;
}
#endif
#ifndef OPENSSL_NO_RSA
if ((algs & SSL_kRSA) &&
!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
goto f_err;
}
#endif
#ifndef OPENSSL_NO_DH
if ((algs & SSL_kEDH) &&
!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
goto f_err;
}
else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
goto f_err;
}
#ifndef OPENSSL_NO_DSA
else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
goto f_err;
}
#endif
#endif
if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
{
#ifndef OPENSSL_NO_RSA
if (algs & SSL_kRSA)
{
if (rsa == NULL
|| RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
goto f_err;
}
}
else
#endif
#ifndef OPENSSL_NO_DH
if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
{
if (dh == NULL
|| DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
goto f_err;
}
}
else
#endif
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
goto f_err;
}
}
return(1);
f_err:
ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
err:
return(0);
}
|
['static int ssl3_check_cert_and_algorithm(SSL *s)\n\t{\n\tint i,idx;\n\tlong algs;\n\tEVP_PKEY *pkey=NULL;\n\tSESS_CERT *sc;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa;\n#endif\n#ifndef OPENSSL_NO_DH\n\tDH *dh;\n#endif\n\tsc=s->session->sess_cert;\n\tif (sc == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);\n\t\tgoto err;\n\t\t}\n\talgs=s->s3->tmp.new_cipher->algorithms;\n\tif (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))\n\t\treturn(1);\n#ifndef OPENSSL_NO_RSA\n\trsa=s->session->sess_cert->peer_rsa_tmp;\n#endif\n#ifndef OPENSSL_NO_DH\n\tdh=s->session->sess_cert->peer_dh_tmp;\n#endif\n\tidx=sc->peer_cert_type;\n#ifndef OPENSSL_NO_ECDH\n\tif (idx == SSL_PKEY_ECC)\n\t\t{\n\t\tif (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,\n\t\t s->s3->tmp.new_cipher) == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\treturn 1;\n\t\t\t}\n\t\t}\n#endif\n\tpkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);\n\ti=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);\n\tEVP_PKEY_free(pkey);\n\tif ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef OPENSSL_NO_DSA\n\telse if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RSA\n\tif ((algs & SSL_kRSA) &&\n\t\t!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_DH\n\tif ((algs & SSL_kEDH) &&\n\t\t!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);\n\t\tgoto f_err;\n\t\t}\n\telse if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef OPENSSL_NO_DSA\n\telse if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#endif\n\tif (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))\n\t\t{\n#ifndef OPENSSL_NO_RSA\n\t\tif (algs & SSL_kRSA)\n\t\t\t{\n\t\t\tif (rsa == NULL\n\t\t\t || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DH\n\t\t\tif (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t\t {\n\t\t\t if (dh == NULL\n\t\t\t\t|| DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);\nerr:\n\treturn(0);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tlong j;\n\tint type;\n\tunsigned char *p;\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\tconst unsigned char *cp;\n\tX509_ALGOR *a;\n#endif\n\tif (key == NULL) goto err;\n\tif (key->pkey != NULL)\n\t\t{\n\t\tCRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\t\treturn(key->pkey);\n\t\t}\n\tif (key->public_key == NULL) goto err;\n\ttype=OBJ_obj2nid(key->algor->algorithm);\n\tif ((ret = EVP_PKEY_new()) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tret->type = EVP_PKEY_type(type);\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\ta=key->algor;\n#endif\n\tif (0)\n\t\t;\n#ifndef OPENSSL_NO_DSA\n\telse if (ret->type == EVP_PKEY_DSA)\n\t\t{\n\t\tif (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))\n\t\t\t{\n\t\t\tif ((ret->pkey.dsa = DSA_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tret->pkey.dsa->write_params=0;\n\t\t\tcp=p=a->parameter->value.sequence->data;\n\t\t\tj=a->parameter->value.sequence->length;\n\t\t\tif (!d2i_DSAparams(&ret->pkey.dsa, &cp, (long)j))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tret->save_parameters=1;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_EC\n\telse if (ret->type == EVP_PKEY_EC)\n\t\t{\n\t\tif (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))\n\t\t\t{\n\t\t\tif ((ret->pkey.eckey= EC_KEY_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET,\n\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcp = p = a->parameter->value.sequence->data;\n\t\t\tj = a->parameter->value.sequence->length;\n\t\t\tif (!d2i_ECParameters(&ret->pkey.eckey, &cp, (long)j))\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (a->parameter && (a->parameter->type == V_ASN1_OBJECT))\n\t\t\t{\n\t\t\tEC_KEY *eckey;\n\t\t\tif (ret->pkey.eckey == NULL)\n\t\t\t\tret->pkey.eckey = EC_KEY_new();\n\t\t\teckey = ret->pkey.eckey;\n\t\t\tif (eckey->group)\n\t\t\t\tEC_GROUP_free(eckey->group);\n\t\t\tif ((eckey->group = EC_GROUP_new_by_nid(\n OBJ_obj2nid(a->parameter->value.object))) == NULL)\n\t\t\t\tgoto err;\n\t\t\tEC_GROUP_set_asn1_flag(eckey->group,\n\t\t\t\t\t\tOPENSSL_EC_NAMED_CURVE);\n\t\t\t}\n\t\tret->save_parameters = 1;\n\t\t}\n#endif\n\tp=key->public_key->data;\n j=key->public_key->length;\n if (!d2i_PublicKey(type, &ret, &p, (long)j))\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, X509_R_ERR_ASN1_LIB);\n\t\tgoto err;\n\t\t}\n\tkey->pkey = ret;\n\tCRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\treturn(ret);\nerr:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n\t{\n\tASN1_OBJECT **op;\n\tADDED_OBJ ad,*adp;\n\tif (a == NULL)\n\t\treturn(NID_undef);\n\tif (a->nid != 0)\n\t\treturn(a->nid);\n\tif (added != NULL)\n\t\t{\n\t\tad.type=ADDED_DATA;\n\t\tad.obj=(ASN1_OBJECT *)a;\n\t\tadp=(ADDED_OBJ *)lh_retrieve(added,&ad);\n\t\tif (adp != NULL) return (adp->obj->nid);\n\t\t}\n\top=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,\n\t\tsizeof(ASN1_OBJECT *),obj_cmp);\n\tif (op == NULL)\n\t\treturn(NID_undef);\n\treturn((*op)->nid);\n\t}', 'EVP_PKEY *EVP_PKEY_new(void)\n\t{\n\tEVP_PKEY *ret;\n\tret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));\n\tif (ret == NULL)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->type=EVP_PKEY_NONE;\n\tret->references=1;\n\tret->pkey.ptr=NULL;\n\tret->attributes=NULL;\n\tret->save_parameters=1;\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
|
2,843
| 0
|
https://github.com/openssl/openssl/blob/207c7df746ca5c3cad6ce71e6cf2263d4183d8be/crypto/bn/bn_add.c/#L202
|
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
{
int max,min;
register BN_ULONG t1,t2,*ap,*bp,*rp;
int i,carry;
#if defined(IRIX_CC_BUG) && !defined(LINT)
int dummy;
#endif
bn_check_top(a);
bn_check_top(b);
if (a->top < b->top)
{
BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);
return(0);
}
max=a->top;
min=b->top;
if (bn_wexpand(r,max) == NULL) return(0);
ap=a->d;
bp=b->d;
rp=r->d;
#if 1
carry=0;
for (i=0; i<min; i++)
{
t1= *(ap++);
t2= *(bp++);
if (carry)
{
carry=(t1 <= t2);
t1=(t1-t2-1)&BN_MASK2;
}
else
{
carry=(t1 < t2);
t1=(t1-t2)&BN_MASK2;
}
#if defined(IRIX_CC_BUG) && !defined(LINT)
dummy=t1;
#endif
*(rp++)=t1&BN_MASK2;
}
#else
carry=bn_sub_words(rp,ap,bp,min);
ap+=min;
bp+=min;
rp+=min;
i=min;
#endif
if (carry)
{
while (i < max)
{
i++;
t1= *(ap++);
t2=(t1-1)&BN_MASK2;
*(rp++)=t2;
if (t1 > t2) break;
}
}
#if 0
memcpy(rp,ap,sizeof(*rp)*(max-i));
#else
if (rp != ap)
{
for (;;)
{
if (i++ >= max) break;
rp[0]=ap[0];
if (i++ >= max) break;
rp[1]=ap[1];
if (i++ >= max) break;
rp[2]=ap[2];
if (i++ >= max) break;
rp[3]=ap[3];
rp+=4;
ap+=4;
}
}
#endif
r->top=max;
bn_fix_top(r);
return(1);
}
|
['int BN_from_montgomery(BIGNUM *ret, BIGNUM *a, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tint retn=0;\n#ifdef MONT_WORD\n\tBIGNUM *n,*r;\n\tBN_ULONG *ap,*np,*rp,n0,v,*nrp;\n\tint al,nl,max,i,x,ri;\n\tBN_CTX_start(ctx);\n\tif ((r = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (!BN_copy(r,a)) goto err;\n\tn= &(mont->N);\n\tap=a->d;\n\tal=ri=mont->ri/BN_BITS2;\n\tnl=n->top;\n\tif ((al == 0) || (nl == 0)) { r->top=0; return(1); }\n\tmax=(nl+al+1);\n\tif (bn_wexpand(r,max) == NULL) goto err;\n\tif (bn_wexpand(ret,max) == NULL) goto err;\n\tr->neg=a->neg^n->neg;\n\tnp=n->d;\n\trp=r->d;\n\tnrp= &(r->d[nl]);\n#if 1\n\tfor (i=r->top; i<max; i++)\n\t\tr->d[i]=0;\n#else\n\tmemset(&(r->d[r->top]),0,(max-r->top)*sizeof(BN_ULONG));\n#endif\n\tr->top=max;\n\tn0=mont->n0;\n#ifdef BN_COUNT\n\tprintf("word BN_from_montgomery %d * %d\\n",nl,nl);\n#endif\n\tfor (i=0; i<nl; i++)\n\t\t{\n\t\tv=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);\n\t\tnrp++;\n\t\trp++;\n\t\tif (((nrp[-1]+=v)&BN_MASK2) >= v)\n\t\t\tcontinue;\n\t\telse\n\t\t\t{\n\t\t\tif (((++nrp[0])&BN_MASK2) != 0) continue;\n\t\t\tif (((++nrp[1])&BN_MASK2) != 0) continue;\n\t\t\tfor (x=2; (((++nrp[x])&BN_MASK2) == 0); x++) ;\n\t\t\t}\n\t\t}\n\tbn_fix_top(r);\n#if 0\n\tBN_rshift(ret,r,mont->ri);\n#else\n\tx=ri;\n\trp=ret->d;\n\tap= &(r->d[x]);\n\tif (r->top < x)\n\t\tal=0;\n\telse\n\t\tal=r->top-x;\n\tret->top=al;\n\tal-=4;\n\tfor (i=0; i<al; i+=4)\n\t\t{\n\t\tBN_ULONG t1,t2,t3,t4;\n\t\tt1=ap[i+0];\n\t\tt2=ap[i+1];\n\t\tt3=ap[i+2];\n\t\tt4=ap[i+3];\n\t\trp[i+0]=t1;\n\t\trp[i+1]=t2;\n\t\trp[i+2]=t3;\n\t\trp[i+3]=t4;\n\t\t}\n\tal+=4;\n\tfor (; i<al; i++)\n\t\trp[i]=ap[i];\n#endif\n#else\n\tBIGNUM *t1,*t2;\n\tBN_CTX_start(ctx);\n\tt1 = BN_CTX_get(ctx);\n\tt2 = BN_CTX_get(ctx);\n\tif (t1 == NULL || t2 == NULL) goto err;\n\tif (!BN_copy(t1,a)) goto err;\n\tBN_mask_bits(t1,mont->ri);\n\tif (!BN_mul(t2,t1,&mont->Ni,ctx)) goto err;\n\tBN_mask_bits(t2,mont->ri);\n\tif (!BN_mul(t1,t2,&mont->N,ctx)) goto err;\n\tif (!BN_add(t2,a,t1)) goto err;\n\tBN_rshift(ret,t2,mont->ri);\n#endif\n\tif (BN_ucmp(ret, &(mont->N)) >= 0)\n\t\t{\n\t\tBN_usub(ret,ret,&(mont->N));\n\t\t}\n\tretn=1;\n err:\n\tBN_CTX_end(ctx);\n\treturn(retn);\n\t}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint max,min;\n\tregister BN_ULONG t1,t2,*ap,*bp,*rp;\n\tint i,carry;\n#if defined(IRIX_CC_BUG) && !defined(LINT)\n\tint dummy;\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tif (a->top < b->top)\n\t\t{\n\t\tBNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);\n\t\treturn(0);\n\t\t}\n\tmax=a->top;\n\tmin=b->top;\n\tif (bn_wexpand(r,max) == NULL) return(0);\n\tap=a->d;\n\tbp=b->d;\n\trp=r->d;\n#if 1\n\tcarry=0;\n\tfor (i=0; i<min; i++)\n\t\t{\n\t\tt1= *(ap++);\n\t\tt2= *(bp++);\n\t\tif (carry)\n\t\t\t{\n\t\t\tcarry=(t1 <= t2);\n\t\t\tt1=(t1-t2-1)&BN_MASK2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tcarry=(t1 < t2);\n\t\t\tt1=(t1-t2)&BN_MASK2;\n\t\t\t}\n#if defined(IRIX_CC_BUG) && !defined(LINT)\n\t\tdummy=t1;\n#endif\n\t\t*(rp++)=t1&BN_MASK2;\n\t\t}\n#else\n\tcarry=bn_sub_words(rp,ap,bp,min);\n\tap+=min;\n\tbp+=min;\n\trp+=min;\n\ti=min;\n#endif\n\tif (carry)\n\t\t{\n\t\twhile (i < max)\n\t\t\t{\n\t\t\ti++;\n\t\t\tt1= *(ap++);\n\t\t\tt2=(t1-1)&BN_MASK2;\n\t\t\t*(rp++)=t2;\n\t\t\tif (t1 > t2) break;\n\t\t\t}\n\t\t}\n#if 0\n\tmemcpy(rp,ap,sizeof(*rp)*(max-i));\n#else\n\tif (rp != ap)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (i++ >= max) break;\n\t\t\trp[0]=ap[0];\n\t\t\tif (i++ >= max) break;\n\t\t\trp[1]=ap[1];\n\t\t\tif (i++ >= max) break;\n\t\t\trp[2]=ap[2];\n\t\t\tif (i++ >= max) break;\n\t\t\trp[3]=ap[3];\n\t\t\trp+=4;\n\t\t\tap+=4;\n\t\t\t}\n\t\t}\n#endif\n\tr->top=max;\n\tbn_fix_top(r);\n\treturn(1);\n\t}']
|
2,844
| 0
|
https://github.com/openssl/openssl/blob/47bbaa5b607f592009ed40f5678fde21c10a873c/crypto/x509/x509_lu.c/#L298
|
int X509_STORE_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,
X509_NAME *name, X509_OBJECT *ret)
{
X509_STORE *ctx = vs->ctx;
X509_LOOKUP *lu;
X509_OBJECT stmp, *tmp;
int i, j;
CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);
CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
if (tmp == NULL || type == X509_LU_CRL) {
for (i = vs->current_method;
i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {
lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);
j = X509_LOOKUP_by_subject(lu, type, name, &stmp);
if (j < 0) {
vs->current_method = j;
return j;
} else if (j) {
tmp = &stmp;
break;
}
}
vs->current_method = 0;
if (tmp == NULL)
return 0;
}
ret->type = tmp->type;
ret->data.ptr = tmp->data.ptr;
X509_OBJECT_up_ref_count(ret);
return 1;
}
|
['int X509_STORE_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,\n X509_NAME *name, X509_OBJECT *ret)\n{\n X509_STORE *ctx = vs->ctx;\n X509_LOOKUP *lu;\n X509_OBJECT stmp, *tmp;\n int i, j;\n CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);\n tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);\n CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n if (tmp == NULL || type == X509_LU_CRL) {\n for (i = vs->current_method;\n i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {\n lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);\n j = X509_LOOKUP_by_subject(lu, type, name, &stmp);\n if (j < 0) {\n vs->current_method = j;\n return j;\n } else if (j) {\n tmp = &stmp;\n break;\n }\n }\n vs->current_method = 0;\n if (tmp == NULL)\n return 0;\n }\n ret->type = tmp->type;\n ret->data.ptr = tmp->data.ptr;\n X509_OBJECT_up_ref_count(ret);\n return 1;\n}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n{\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n char *rw_text, *operation_text;\n if (mode & CRYPTO_LOCK)\n operation_text = "lock ";\n else if (mode & CRYPTO_UNLOCK)\n operation_text = "unlock";\n else\n operation_text = "ERROR ";\n if (mode & CRYPTO_READ)\n rw_text = "r";\n else if (mode & CRYPTO_WRITE)\n rw_text = "w";\n else\n rw_text = "ERROR";\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), rw_text, operation_text,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n if (type < 0) {\n if (dynlock_lock_callback != NULL) {\n struct CRYPTO_dynlock_value *pointer\n = CRYPTO_get_dynlock_value(type);\n OPENSSL_assert(pointer != NULL);\n dynlock_lock_callback(mode, pointer, file, line);\n CRYPTO_destroy_dynlockid(type);\n }\n } else if (locking_callback != NULL)\n locking_callback(mode, type, file, line);\n}', 'X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,\n int type, X509_NAME *name)\n{\n int idx;\n idx = X509_OBJECT_idx_by_subject(h, type, name);\n if (idx == -1)\n return NULL;\n return sk_X509_OBJECT_value(h, idx);\n}', 'int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,\n X509_NAME *name)\n{\n return x509_object_idx_cnt(h, type, name, NULL);\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}', 'int sk_num(const _STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,\n X509_OBJECT *ret)\n{\n if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))\n return X509_LU_FAIL;\n if (ctx->skip)\n return 0;\n return ctx->method->get_by_subject(ctx, type, name, ret);\n}']
|
2,845
| 0
|
https://github.com/openssl/openssl/blob/6fc1748ec65c94c195d02b59556434e36a5f7651/test/handshake_helper.c/#L74
|
static void info_cb(const SSL *s, int where, int ret)
{
if (where & SSL_CB_ALERT) {
HANDSHAKE_EX_DATA *ex_data =
(HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
if (where & SSL_CB_WRITE) {
ex_data->alert_sent = ret;
if (strcmp(SSL_alert_type_string(ret), "F") == 0
|| strcmp(SSL_alert_desc_string(ret), "CN") == 0)
ex_data->num_fatal_alerts_sent++;
} else {
ex_data->alert_received = ret;
}
}
}
|
['static void info_cb(const SSL *s, int where, int ret)\n{\n if (where & SSL_CB_ALERT) {\n HANDSHAKE_EX_DATA *ex_data =\n (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));\n if (where & SSL_CB_WRITE) {\n ex_data->alert_sent = ret;\n if (strcmp(SSL_alert_type_string(ret), "F") == 0\n || strcmp(SSL_alert_desc_string(ret), "CN") == 0)\n ex_data->num_fatal_alerts_sent++;\n } else {\n ex_data->alert_received = ret;\n }\n }\n}', 'void *SSL_get_ex_data(const SSL *s, int idx)\n{\n return (CRYPTO_get_ex_data(&s->ex_data, idx));\n}', 'void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)\n{\n if (ad->sk == NULL || idx >= sk_void_num(ad->sk))\n return NULL;\n return sk_void_value(ad->sk, idx);\n}']
|
2,846
| 0
|
https://github.com/libav/libav/blob/8a49d2bcbe7573bb4b765728b2578fac0d19763f/libavfilter/vf_pad.c/#L306
|
static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
{
int planes[4] = { -1, -1, -1, -1}, *p = planes;
int i, j;
for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
if (av_frame_get_plane_buffer(frame, i) == buf)
*p++ = i;
}
for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;
int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;
uint8_t *start = frame->data[planes[i]];
uint8_t *end = start + (frame->height >> hsub) *
frame->linesize[planes[i]];
ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +
(s->y >> vsub) * frame->linesize[planes[i]];
ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
s->line_step[planes[i]] +
(s->y >> vsub) * frame->linesize[planes[i]];
if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])
return 1;
if (start - buf->data < req_start ||
(buf->data + buf->size) - end < req_end)
return 1;
#define SIGN(x) ((x) > 0 ? 1 : -1)
for (j = 0; j < FF_ARRAY_ELEMS(planes) & planes[j] >= 0; j++) {
int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;
uint8_t *start1 = frame->data[planes[j]];
uint8_t *end1 = start1 + (frame->height >> hsub1) *
frame->linesize[planes[j]];
if (i == j)
continue;
if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
SIGN(end - start1) != SIGN(end - start1 + req_end))
return 1;
}
}
return 0;
}
|
['static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)\n{\n int planes[4] = { -1, -1, -1, -1}, *p = planes;\n int i, j;\n for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {\n if (av_frame_get_plane_buffer(frame, i) == buf)\n *p++ = i;\n }\n for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {\n int hsub = (planes[i] == 1 || planes[i] == 2) ? s->hsub : 0;\n int vsub = (planes[i] == 1 || planes[i] == 2) ? s->vsub : 0;\n uint8_t *start = frame->data[planes[i]];\n uint8_t *end = start + (frame->height >> hsub) *\n frame->linesize[planes[i]];\n ptrdiff_t req_start = (s->x >> hsub) * s->line_step[planes[i]] +\n (s->y >> vsub) * frame->linesize[planes[i]];\n ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *\n s->line_step[planes[i]] +\n (s->y >> vsub) * frame->linesize[planes[i]];\n if (frame->linesize[planes[i]] < (s->w >> hsub) * s->line_step[planes[i]])\n return 1;\n if (start - buf->data < req_start ||\n (buf->data + buf->size) - end < req_end)\n return 1;\n#define SIGN(x) ((x) > 0 ? 1 : -1)\n for (j = 0; j < FF_ARRAY_ELEMS(planes) & planes[j] >= 0; j++) {\n int hsub1 = (planes[j] == 1 || planes[j] == 2) ? s->hsub : 0;\n uint8_t *start1 = frame->data[planes[j]];\n uint8_t *end1 = start1 + (frame->height >> hsub1) *\n frame->linesize[planes[j]];\n if (i == j)\n continue;\n if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||\n SIGN(end - start1) != SIGN(end - start1 + req_end))\n return 1;\n }\n }\n return 0;\n}']
|
2,847
| 0
|
https://github.com/openssl/openssl/blob/a4af39ac4482355ffdd61fb61231a0c79b96997b/apps/x509.c/#L889
|
static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,
int days, LHASH *conf, char *section)
{
int ret=0;
BIO *io=NULL;
MS_STATIC char buf2[1024];
char *buf=NULL,*p;
BIGNUM *serial=NULL;
ASN1_INTEGER *bs=NULL,bs2;
X509_STORE_CTX xsc;
EVP_PKEY *upkey;
upkey = X509_get_pubkey(xca);
EVP_PKEY_copy_parameters(upkey,pkey);
EVP_PKEY_free(upkey);
X509_STORE_CTX_init(&xsc,ctx,x,NULL);
buf=(char *)Malloc(EVP_PKEY_size(pkey)*2+
((serialfile == NULL)
?(strlen(CAfile)+strlen(POSTFIX)+1)
:(strlen(serialfile)))+1);
if (buf == NULL) { BIO_printf(bio_err,"out of mem\n"); goto end; }
if (serialfile == NULL)
{
strcpy(buf,CAfile);
for (p=buf; *p; p++)
if (*p == '.')
{
*p='\0';
break;
}
strcat(buf,POSTFIX);
}
else
strcpy(buf,serialfile);
serial=BN_new();
bs=ASN1_INTEGER_new();
if ((serial == NULL) || (bs == NULL))
{
ERR_print_errors(bio_err);
goto end;
}
io=BIO_new(BIO_s_file());
if (io == NULL)
{
ERR_print_errors(bio_err);
goto end;
}
if (BIO_read_filename(io,buf) <= 0)
{
if (!create)
{
perror(buf);
goto end;
}
else
{
ASN1_INTEGER_set(bs,0);
BN_zero(serial);
}
}
else
{
if (!a2i_ASN1_INTEGER(io,bs,buf2,1024))
{
BIO_printf(bio_err,"unable to load serial number from %s\n",buf);
ERR_print_errors(bio_err);
goto end;
}
else
{
serial=BN_bin2bn(bs->data,bs->length,serial);
if (serial == NULL)
{
BIO_printf(bio_err,"error converting bin 2 bn");
goto end;
}
}
}
if (!BN_add_word(serial,1))
{ BIO_printf(bio_err,"add_word failure\n"); goto end; }
bs2.data=(unsigned char *)buf2;
bs2.length=BN_bn2bin(serial,bs2.data);
if (BIO_write_filename(io,buf) <= 0)
{
BIO_printf(bio_err,"error attempting to write serial number file\n");
perror(buf);
goto end;
}
i2a_ASN1_INTEGER(io,&bs2);
BIO_puts(io,"\n");
BIO_free(io);
io=NULL;
if (!X509_STORE_add_cert(ctx,x)) goto end;
X509_STORE_CTX_set_cert(&xsc,x);
if (!reqfile && !X509_verify_cert(&xsc))
goto end;
if (!X509_check_private_key(xca,pkey))
{
BIO_printf(bio_err,"CA certificate and CA private key do not match\n");
goto end;
}
if (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;
if (!X509_set_serialNumber(x,bs)) goto end;
if (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)
goto end;
if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
goto end;
if(conf) {
X509V3_CTX ctx2;
X509_set_version(x,2);
X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
X509V3_set_conf_lhash(&ctx2, conf);
if(!X509V3_EXT_add_conf(conf, &ctx2, section, x)) goto end;
}
if (!X509_sign(x,pkey,digest)) goto end;
ret=1;
end:
X509_STORE_CTX_cleanup(&xsc);
if (!ret)
ERR_print_errors(bio_err);
if (buf != NULL) Free(buf);
if (bs != NULL) ASN1_INTEGER_free(bs);
if (io != NULL) BIO_free(io);
if (serial != NULL) BN_free(serial);
return(ret);
}
|
['static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,\n\t X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,\n\t int days, LHASH *conf, char *section)\n\t{\n\tint ret=0;\n\tBIO *io=NULL;\n\tMS_STATIC char buf2[1024];\n\tchar *buf=NULL,*p;\n\tBIGNUM *serial=NULL;\n\tASN1_INTEGER *bs=NULL,bs2;\n\tX509_STORE_CTX xsc;\n\tEVP_PKEY *upkey;\n\tupkey = X509_get_pubkey(xca);\n\tEVP_PKEY_copy_parameters(upkey,pkey);\n\tEVP_PKEY_free(upkey);\n\tX509_STORE_CTX_init(&xsc,ctx,x,NULL);\n\tbuf=(char *)Malloc(EVP_PKEY_size(pkey)*2+\n\t\t((serialfile == NULL)\n\t\t\t?(strlen(CAfile)+strlen(POSTFIX)+1)\n\t\t\t:(strlen(serialfile)))+1);\n\tif (buf == NULL) { BIO_printf(bio_err,"out of mem\\n"); goto end; }\n\tif (serialfile == NULL)\n\t\t{\n\t\tstrcpy(buf,CAfile);\n\t\tfor (p=buf; *p; p++)\n\t\t\tif (*p == \'.\')\n\t\t\t\t{\n\t\t\t\t*p=\'\\0\';\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\tstrcat(buf,POSTFIX);\n\t\t}\n\telse\n\t\tstrcpy(buf,serialfile);\n\tserial=BN_new();\n\tbs=ASN1_INTEGER_new();\n\tif ((serial == NULL) || (bs == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tio=BIO_new(BIO_s_file());\n\tif (io == NULL)\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (BIO_read_filename(io,buf) <= 0)\n\t\t{\n\t\tif (!create)\n\t\t\t{\n\t\t\tperror(buf);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tASN1_INTEGER_set(bs,0);\n\t\t\tBN_zero(serial);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (!a2i_ASN1_INTEGER(io,bs,buf2,1024))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load serial number from %s\\n",buf);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tserial=BN_bin2bn(bs->data,bs->length,serial);\n\t\t\tif (serial == NULL)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"error converting bin 2 bn");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (!BN_add_word(serial,1))\n\t\t{ BIO_printf(bio_err,"add_word failure\\n"); goto end; }\n\tbs2.data=(unsigned char *)buf2;\n\tbs2.length=BN_bn2bin(serial,bs2.data);\n\tif (BIO_write_filename(io,buf) <= 0)\n\t\t{\n\t\tBIO_printf(bio_err,"error attempting to write serial number file\\n");\n\t\tperror(buf);\n\t\tgoto end;\n\t\t}\n\ti2a_ASN1_INTEGER(io,&bs2);\n\tBIO_puts(io,"\\n");\n\tBIO_free(io);\n\tio=NULL;\n\tif (!X509_STORE_add_cert(ctx,x)) goto end;\n\tX509_STORE_CTX_set_cert(&xsc,x);\n\tif (!reqfile && !X509_verify_cert(&xsc))\n\t\tgoto end;\n\tif (!X509_check_private_key(xca,pkey))\n\t\t{\n\t\tBIO_printf(bio_err,"CA certificate and CA private key do not match\\n");\n\t\tgoto end;\n\t\t}\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;\n\tif (!X509_set_serialNumber(x,bs)) goto end;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)\n\t\tgoto end;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto end;\n\tif(conf) {\n\t\tX509V3_CTX ctx2;\n\t\tX509_set_version(x,2);\n X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);\n X509V3_set_conf_lhash(&ctx2, conf);\n if(!X509V3_EXT_add_conf(conf, &ctx2, section, x)) goto end;\n\t}\n\tif (!X509_sign(x,pkey,digest)) goto end;\n\tret=1;\nend:\n\tX509_STORE_CTX_cleanup(&xsc);\n\tif (!ret)\n\t\tERR_print_errors(bio_err);\n\tif (buf != NULL) Free(buf);\n\tif (bs != NULL) ASN1_INTEGER_free(bs);\n\tif (io != NULL)\tBIO_free(io);\n\tif (serial != NULL) BN_free(serial);\n\treturn(ret);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}']
|
2,848
| 0
|
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/crypto/ui/ui_lib.c/#L359
|
int UI_dup_info_string(UI *ui, const char *text)
{
char *text_copy = NULL;
if (text) {
text_copy = OPENSSL_strdup(text);
if (text_copy == NULL) {
UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
return -1;
}
}
return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
0, 0, NULL);
}
|
['int UI_dup_info_string(UI *ui, const char *text)\n{\n char *text_copy = NULL;\n if (text) {\n text_copy = OPENSSL_strdup(text);\n if (text_copy == NULL) {\n UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n }\n return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,\n 0, 0, NULL);\n}', 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n size_t size;\n if (str == NULL)\n return NULL;\n size = strlen(str) + 1;\n ret = CRYPTO_malloc(size, file, line);\n if (ret != NULL)\n memcpy(ret, str, size);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}']
|
2,849
| 0
|
https://github.com/openssl/openssl/blob/00701e5ea84861b74d9d624f21a6b3fcb12e8acd/ssl/ssl_lib.c/#L4412
|
EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
{
ssl_clear_hash_ctx(hash);
*hash = EVP_MD_CTX_new();
if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
EVP_MD_CTX_free(*hash);
*hash = NULL;
return NULL;
}
return *hash;
}
|
['EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)\n{\n ssl_clear_hash_ctx(hash);\n *hash = EVP_MD_CTX_new();\n if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {\n EVP_MD_CTX_free(*hash);\n *hash = NULL;\n return NULL;\n }\n return *hash;\n}', 'void ssl_clear_hash_ctx(EVP_MD_CTX **hash)\n{\n EVP_MD_CTX_free(*hash);\n *hash = NULL;\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,850
| 0
|
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/crypto/mem.c/#L244
|
void CRYPTO_free(void *str)
{
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
if (call_malloc_debug) {
CRYPTO_mem_debug_free(str, 0);
free(str);
CRYPTO_mem_debug_free(str, 1);
} else {
free(str);
}
#else
free(str);
#endif
}
|
['int engine_main(int argc, char **argv)\n{\n int ret = 1, i;\n int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;\n ENGINE *e;\n STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();\n STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();\n STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();\n BIO *out;\n const char *indent = " ";\n OPTION_CHOICE o;\n char *prog;\n char *argv1;\n out = dup_bio_out(FORMAT_TEXT);\n if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)\n goto end;\n prog = argv[0];\n while ((argv1 = argv[1]) != NULL && *argv1 != \'-\') {\n sk_OPENSSL_STRING_push(engines, argv1);\n argc--;\n argv++;\n }\n argv[0] = prog;\n opt_init(argc, argv, engine_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n opt_help(engine_options);\n ret = 0;\n goto end;\n case OPT_VVVV:\n case OPT_VVV:\n case OPT_VV:\n case OPT_V:\n i = (int)(o - OPT_V) + 1;\n if (verbose < i)\n verbose = i;\n break;\n case OPT_C:\n list_cap = 1;\n break;\n case OPT_TT:\n test_avail_noise++;\n case OPT_T:\n test_avail++;\n break;\n case OPT_PRE:\n sk_OPENSSL_STRING_push(pre_cmds, opt_arg());\n break;\n case OPT_POST:\n sk_OPENSSL_STRING_push(post_cmds, opt_arg());\n break;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n for ( ; *argv; argv++) {\n if (**argv == \'-\') {\n BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\\n",\n prog);\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n }\n sk_OPENSSL_STRING_push(engines, *argv);\n }\n if (sk_OPENSSL_STRING_num(engines) == 0) {\n for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {\n sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));\n }\n }\n for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {\n const char *id = sk_OPENSSL_STRING_value(engines, i);\n if ((e = ENGINE_by_id(id)) != NULL) {\n const char *name = ENGINE_get_name(e);\n BIO_printf(out, "(%s) %s\\n", id, name);\n util_do_cmds(e, pre_cmds, out, indent);\n if (strcmp(ENGINE_get_id(e), id) != 0) {\n BIO_printf(out, "Loaded: (%s) %s\\n",\n ENGINE_get_id(e), ENGINE_get_name(e));\n }\n if (list_cap) {\n int cap_size = 256;\n char *cap_buf = NULL;\n int k, n;\n const int *nids;\n ENGINE_CIPHERS_PTR fn_c;\n ENGINE_DIGESTS_PTR fn_d;\n ENGINE_PKEY_METHS_PTR fn_pk;\n if (ENGINE_get_RSA(e) != NULL\n && !append_buf(&cap_buf, &cap_size, "RSA"))\n goto end;\n if (ENGINE_get_DSA(e) != NULL\n && !append_buf(&cap_buf, &cap_size, "DSA"))\n goto end;\n if (ENGINE_get_DH(e) != NULL\n && !append_buf(&cap_buf, &cap_size, "DH"))\n goto end;\n if (ENGINE_get_RAND(e) != NULL\n && !append_buf(&cap_buf, &cap_size, "RAND"))\n goto end;\n fn_c = ENGINE_get_ciphers(e);\n if (!fn_c)\n goto skip_ciphers;\n n = fn_c(e, NULL, &nids, 0);\n for (k = 0; k < n; ++k)\n if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))\n goto end;\n skip_ciphers:\n fn_d = ENGINE_get_digests(e);\n if (!fn_d)\n goto skip_digests;\n n = fn_d(e, NULL, &nids, 0);\n for (k = 0; k < n; ++k)\n if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))\n goto end;\n skip_digests:\n fn_pk = ENGINE_get_pkey_meths(e);\n if (!fn_pk)\n goto skip_pmeths;\n n = fn_pk(e, NULL, &nids, 0);\n for (k = 0; k < n; ++k)\n if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))\n goto end;\n skip_pmeths:\n if (cap_buf && (*cap_buf != \'\\0\'))\n BIO_printf(out, " [%s]\\n", cap_buf);\n OPENSSL_free(cap_buf);\n }\n if (test_avail) {\n BIO_printf(out, "%s", indent);\n if (ENGINE_init(e)) {\n BIO_printf(out, "[ available ]\\n");\n util_do_cmds(e, post_cmds, out, indent);\n ENGINE_finish(e);\n } else {\n BIO_printf(out, "[ unavailable ]\\n");\n if (test_avail_noise)\n ERR_print_errors_fp(stdout);\n ERR_clear_error();\n }\n }\n if ((verbose > 0) && !util_verbose(e, verbose, out, indent))\n goto end;\n ENGINE_free(e);\n } else\n ERR_print_errors(bio_err);\n }\n ret = 0;\n end:\n ERR_print_errors(bio_err);\n sk_OPENSSL_STRING_pop_free(engines, identity);\n sk_OPENSSL_STRING_pop_free(pre_cmds, identity);\n sk_OPENSSL_STRING_pop_free(post_cmds, identity);\n BIO_free_all(out);\n return (ret);\n}', 'DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)', 'int sk_push(_STACK *st, void *data)\n{\n return (sk_insert(st, data, st->num));\n}', 'int sk_insert(_STACK *st, void *data, int loc)\n{\n char **s;\n if (st == NULL)\n return 0;\n if (st->num_alloc <= st->num + 1) {\n s = OPENSSL_realloc((char *)st->data,\n (unsigned int)sizeof(char *) * st->num_alloc * 2);\n if (s == NULL)\n return (0);\n st->data = s;\n st->num_alloc *= 2;\n }\n if ((loc >= (int)st->num) || (loc < 0))\n st->data[st->num] = data;\n else {\n memmove(&(st->data[loc + 1]),\n &(st->data[loc]), sizeof(char *) * (st->num - loc));\n st->data[loc] = data;\n }\n st->num++;\n st->sorted = 0;\n return (st->num);\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)file;\n (void)line;\n#endif\n return realloc(str, num);\n}', 'void CRYPTO_free(void *str)\n{\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,851
| 0
|
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_shift.c/#L158
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
r->neg = a->neg;
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
}
|
["int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)\n{\n int i, first, len = 0, c, use_bn;\n char ftmp[24], *tmp = ftmp;\n int tmpsize = sizeof ftmp;\n const char *p;\n unsigned long l;\n BIGNUM *bl = NULL;\n if (num == 0)\n return (0);\n else if (num == -1)\n num = strlen(buf);\n p = buf;\n c = *(p++);\n num--;\n if ((c >= '0') && (c <= '2')) {\n first = c - '0';\n } else {\n ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);\n goto err;\n }\n if (num <= 0) {\n ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);\n goto err;\n }\n c = *(p++);\n num--;\n for (;;) {\n if (num <= 0)\n break;\n if ((c != '.') && (c != ' ')) {\n ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);\n goto err;\n }\n l = 0;\n use_bn = 0;\n for (;;) {\n if (num <= 0)\n break;\n num--;\n c = *(p++);\n if ((c == ' ') || (c == '.'))\n break;\n if ((c < '0') || (c > '9')) {\n ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);\n goto err;\n }\n if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {\n use_bn = 1;\n if (bl == NULL)\n bl = BN_new();\n if (bl == NULL || !BN_set_word(bl, l))\n goto err;\n }\n if (use_bn) {\n if (!BN_mul_word(bl, 10L)\n || !BN_add_word(bl, c - '0'))\n goto err;\n } else\n l = l * 10L + (long)(c - '0');\n }\n if (len == 0) {\n if ((first < 2) && (l >= 40)) {\n ASN1err(ASN1_F_A2D_ASN1_OBJECT,\n ASN1_R_SECOND_NUMBER_TOO_LARGE);\n goto err;\n }\n if (use_bn) {\n if (!BN_add_word(bl, first * 40))\n goto err;\n } else\n l += (long)first *40;\n }\n i = 0;\n if (use_bn) {\n int blsize;\n blsize = BN_num_bits(bl);\n blsize = (blsize + 6) / 7;\n if (blsize > tmpsize) {\n if (tmp != ftmp)\n OPENSSL_free(tmp);\n tmpsize = blsize + 32;\n tmp = OPENSSL_malloc(tmpsize);\n if (tmp == NULL)\n goto err;\n }\n while (blsize--)\n tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);\n } else {\n for (;;) {\n tmp[i++] = (unsigned char)l & 0x7f;\n l >>= 7L;\n if (l == 0L)\n break;\n }\n }\n if (out != NULL) {\n if (len + i > olen) {\n ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);\n goto err;\n }\n while (--i > 0)\n out[len++] = tmp[i] | 0x80;\n out[len++] = tmp[0];\n } else\n len += i;\n }\n if (tmp != ftmp)\n OPENSSL_free(tmp);\n BN_free(bl);\n return (len);\n err:\n if (tmp != ftmp)\n OPENSSL_free(tmp);\n BN_free(bl);\n return (0);\n}", 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)\n{\n BN_ULONG ret = 0;\n int i, j;\n bn_check_top(a);\n w &= BN_MASK2;\n if (!w)\n return (BN_ULONG)-1;\n if (a->top == 0)\n return 0;\n j = BN_BITS2 - BN_num_bits_word(w);\n w <<= j;\n if (!BN_lshift(a, a, j))\n return (BN_ULONG)-1;\n for (i = a->top - 1; i >= 0; i--) {\n BN_ULONG l, d;\n l = a->d[i];\n d = bn_div_words(ret, l, w);\n ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;\n a->d[i] = d;\n }\n if ((a->top > 0) && (a->d[a->top - 1] == 0))\n a->top--;\n ret >>= j;\n bn_check_top(a);\n return (ret);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
2,852
| 0
|
https://github.com/libav/libav/blob/5228bcd8705523cee43e351e1a113e12aefcf837/libavcodec/vp8dsp.c/#L150
|
static av_always_inline int simple_limit(uint8_t *p, int stride, int flim)
{
LOAD_PIXELS
return 2*FFABS(p0-q0) + (FFABS(p1-q1) >> 1) <= flim;
}
|
['static av_always_inline int simple_limit(uint8_t *p, int stride, int flim)\n{\n LOAD_PIXELS\n return 2*FFABS(p0-q0) + (FFABS(p1-q1) >> 1) <= flim;\n}']
|
2,853
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/bitstream_filter.c/#L41
|
AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
AVBitStreamFilter *bsf= first_bitstream_filter;
while(bsf){
if(!strcmp(name, bsf->name)){
AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
bsfc->filter= bsf;
bsfc->priv_data= av_mallocz(bsf->priv_data_size);
return bsfc;
}
bsf= bsf->next;
}
return NULL;
}
|
['AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){\n AVBitStreamFilter *bsf= first_bitstream_filter;\n while(bsf){\n if(!strcmp(name, bsf->name)){\n AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));\n bsfc->filter= bsf;\n bsfc->priv_data= av_mallocz(bsf->priv_data_size);\n return bsfc;\n }\n bsf= bsf->next;\n }\n return NULL;\n}', 'void *av_mallocz(unsigned int size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
2,854
| 0
|
https://github.com/libav/libav/blob/cf6bae6883607f83f3b042b7b9d711197f736e2a/libavcodec/smacker.c/#L587
|
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
GetBitContext gb;
HuffContext h[4];
VLC vlc[4];
int16_t *samples = data;
int8_t *samples8 = data;
int val;
int i, res;
int unp_size;
int bits, stereo;
int pred[2] = {0, 0};
unp_size = AV_RL32(buf);
init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
if(!get_bits1(&gb)){
av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
*data_size = 0;
return 1;
}
stereo = get_bits1(&gb);
bits = get_bits1(&gb);
if (unp_size & 0xC0000000 || unp_size > *data_size) {
av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
return -1;
}
memset(vlc, 0, sizeof(VLC) * 4);
memset(h, 0, sizeof(HuffContext) * 4);
for(i = 0; i < (1 << (bits + stereo)); i++) {
h[i].length = 256;
h[i].maxlength = 0;
h[i].current = 0;
h[i].bits = av_mallocz(256 * 4);
h[i].lengths = av_mallocz(256 * sizeof(int));
h[i].values = av_mallocz(256 * sizeof(int));
skip_bits1(&gb);
smacker_decode_tree(&gb, &h[i], 0, 0);
skip_bits1(&gb);
if(h[i].current > 1) {
res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
h[i].lengths, sizeof(int), sizeof(int),
h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
if(res < 0) {
av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
return -1;
}
}
}
if(bits) {
for(i = stereo; i >= 0; i--)
pred[i] = bswap_16(get_bits(&gb, 16));
for(i = 0; i < stereo; i++)
*samples++ = pred[i];
for(i = 0; i < unp_size / 2; i++) {
if(i & stereo) {
if(vlc[2].table)
res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
else
res = 0;
val = h[2].values[res];
if(vlc[3].table)
res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
else
res = 0;
val |= h[3].values[res] << 8;
pred[1] += (int16_t)val;
*samples++ = pred[1];
} else {
if(vlc[0].table)
res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
else
res = 0;
val = h[0].values[res];
if(vlc[1].table)
res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
else
res = 0;
val |= h[1].values[res] << 8;
pred[0] += val;
*samples++ = pred[0];
}
}
} else {
for(i = stereo; i >= 0; i--)
pred[i] = get_bits(&gb, 8);
for(i = 0; i < stereo; i++)
*samples8++ = pred[i];
for(i = 0; i < unp_size; i++) {
if(i & stereo){
if(vlc[1].table)
res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
else
res = 0;
pred[1] += (int8_t)h[1].values[res];
*samples8++ = pred[1];
} else {
if(vlc[0].table)
res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
else
res = 0;
pred[0] += (int8_t)h[0].values[res];
*samples8++ = pred[0];
}
}
}
for(i = 0; i < 4; i++) {
if(vlc[i].table)
free_vlc(&vlc[i]);
if(h[i].bits)
av_free(h[i].bits);
if(h[i].lengths)
av_free(h[i].lengths);
if(h[i].values)
av_free(h[i].values);
}
*data_size = unp_size;
return buf_size;
}
|
['static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n GetBitContext gb;\n HuffContext h[4];\n VLC vlc[4];\n int16_t *samples = data;\n int8_t *samples8 = data;\n int val;\n int i, res;\n int unp_size;\n int bits, stereo;\n int pred[2] = {0, 0};\n unp_size = AV_RL32(buf);\n init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);\n if(!get_bits1(&gb)){\n av_log(avctx, AV_LOG_INFO, "Sound: no data\\n");\n *data_size = 0;\n return 1;\n }\n stereo = get_bits1(&gb);\n bits = get_bits1(&gb);\n if (unp_size & 0xC0000000 || unp_size > *data_size) {\n av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\\n");\n return -1;\n }\n memset(vlc, 0, sizeof(VLC) * 4);\n memset(h, 0, sizeof(HuffContext) * 4);\n for(i = 0; i < (1 << (bits + stereo)); i++) {\n h[i].length = 256;\n h[i].maxlength = 0;\n h[i].current = 0;\n h[i].bits = av_mallocz(256 * 4);\n h[i].lengths = av_mallocz(256 * sizeof(int));\n h[i].values = av_mallocz(256 * sizeof(int));\n skip_bits1(&gb);\n smacker_decode_tree(&gb, &h[i], 0, 0);\n skip_bits1(&gb);\n if(h[i].current > 1) {\n res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,\n h[i].lengths, sizeof(int), sizeof(int),\n h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);\n if(res < 0) {\n av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\\n");\n return -1;\n }\n }\n }\n if(bits) {\n for(i = stereo; i >= 0; i--)\n pred[i] = bswap_16(get_bits(&gb, 16));\n for(i = 0; i < stereo; i++)\n *samples++ = pred[i];\n for(i = 0; i < unp_size / 2; i++) {\n if(i & stereo) {\n if(vlc[2].table)\n res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[2].values[res];\n if(vlc[3].table)\n res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[3].values[res] << 8;\n pred[1] += (int16_t)val;\n *samples++ = pred[1];\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[0].values[res];\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[1].values[res] << 8;\n pred[0] += val;\n *samples++ = pred[0];\n }\n }\n } else {\n for(i = stereo; i >= 0; i--)\n pred[i] = get_bits(&gb, 8);\n for(i = 0; i < stereo; i++)\n *samples8++ = pred[i];\n for(i = 0; i < unp_size; i++) {\n if(i & stereo){\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[1] += (int8_t)h[1].values[res];\n *samples8++ = pred[1];\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[0] += (int8_t)h[0].values[res];\n *samples8++ = pred[0];\n }\n }\n }\n for(i = 0; i < 4; i++) {\n if(vlc[i].table)\n free_vlc(&vlc[i]);\n if(h[i].bits)\n av_free(h[i].bits);\n if(h[i].lengths)\n av_free(h[i].lengths);\n if(h[i].values)\n av_free(h[i].values);\n }\n *data_size = unp_size;\n return buf_size;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n int index= s->index;\n uint8_t result= s->buffer[ index>>3 ];\n#ifdef ALT_BITSTREAM_READER_LE\n result>>= (index&0x07);\n result&= 1;\n#else\n result<<= (index&0x07);\n result>>= 8 - 1;\n#endif\n index++;\n s->index= index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}']
|
2,855
| 0
|
https://github.com/libav/libav/blob/3b2fbe67bd63b00331db2a9b213f6d420418a312/libavcodec/opus.c/#L362
|
int ff_opus_parse_extradata(AVCodecContext *avctx,
OpusContext *s)
{
static const uint8_t default_channel_map[2] = { 0, 1 };
uint8_t default_extradata[19] = {
'O', 'p', 'u', 's', 'H', 'e', 'a', 'd',
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
int (*channel_reorder)(int, int) = channel_reorder_unknown;
const uint8_t *extradata, *channel_map;
int extradata_size;
int version, channels, map_type, streams, stereo_streams, i, j;
uint64_t layout;
if (!avctx->extradata) {
if (avctx->channels > 2) {
av_log(avctx, AV_LOG_ERROR,
"Multichannel configuration without extradata.\n");
return AVERROR(EINVAL);
}
default_extradata[9] = (avctx->channels == 1) ? 1 : 2;
extradata = default_extradata;
extradata_size = sizeof(default_extradata);
} else {
extradata = avctx->extradata;
extradata_size = avctx->extradata_size;
}
if (extradata_size < 19) {
av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
extradata_size);
return AVERROR_INVALIDDATA;
}
version = extradata[8];
if (version > 15) {
avpriv_request_sample(avctx, "Extradata version %d", version);
return AVERROR_PATCHWELCOME;
}
avctx->delay = AV_RL16(extradata + 10);
channels = extradata[9];
if (!channels) {
av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\n");
return AVERROR_INVALIDDATA;
}
s->gain_i = AV_RL16(extradata + 16);
if (s->gain_i)
s->gain = pow(10, s->gain_i / (20.0 * 256));
map_type = extradata[18];
if (!map_type) {
if (channels > 2) {
av_log(avctx, AV_LOG_ERROR,
"Channel mapping 0 is only specified for up to 2 channels\n");
return AVERROR_INVALIDDATA;
}
layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
streams = 1;
stereo_streams = channels - 1;
channel_map = default_channel_map;
} else if (map_type == 1 || map_type == 255) {
if (extradata_size < 21 + channels) {
av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
extradata_size);
return AVERROR_INVALIDDATA;
}
streams = extradata[19];
stereo_streams = extradata[20];
if (!streams || stereo_streams > streams ||
streams + stereo_streams > 255) {
av_log(avctx, AV_LOG_ERROR,
"Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
return AVERROR_INVALIDDATA;
}
if (map_type == 1) {
if (channels > 8) {
av_log(avctx, AV_LOG_ERROR,
"Channel mapping 1 is only specified for up to 8 channels\n");
return AVERROR_INVALIDDATA;
}
layout = ff_vorbis_channel_layouts[channels - 1];
channel_reorder = channel_reorder_vorbis;
} else
layout = 0;
channel_map = extradata + 21;
} else {
avpriv_request_sample(avctx, "Mapping type %d", map_type);
return AVERROR_PATCHWELCOME;
}
s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));
if (!s->channel_maps)
return AVERROR(ENOMEM);
for (i = 0; i < channels; i++) {
ChannelMap *map = &s->channel_maps[i];
uint8_t idx = channel_map[channel_reorder(channels, i)];
if (idx == 255) {
map->silence = 1;
continue;
} else if (idx >= streams + stereo_streams) {
av_log(avctx, AV_LOG_ERROR,
"Invalid channel map for output channel %d: %d\n", i, idx);
return AVERROR_INVALIDDATA;
}
map->copy = 0;
for (j = 0; j < i; j++)
if (channel_map[channel_reorder(channels, j)] == idx) {
map->copy = 1;
map->copy_idx = j;
break;
}
if (idx < 2 * stereo_streams) {
map->stream_idx = idx / 2;
map->channel_idx = idx & 1;
} else {
map->stream_idx = idx - stereo_streams;
map->channel_idx = 0;
}
}
avctx->channels = channels;
avctx->channel_layout = layout;
s->nb_streams = streams;
s->nb_stereo_streams = stereo_streams;
return 0;
}
|
['int ff_opus_parse_extradata(AVCodecContext *avctx,\n OpusContext *s)\n{\n static const uint8_t default_channel_map[2] = { 0, 1 };\n uint8_t default_extradata[19] = {\n \'O\', \'p\', \'u\', \'s\', \'H\', \'e\', \'a\', \'d\',\n 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n };\n int (*channel_reorder)(int, int) = channel_reorder_unknown;\n const uint8_t *extradata, *channel_map;\n int extradata_size;\n int version, channels, map_type, streams, stereo_streams, i, j;\n uint64_t layout;\n if (!avctx->extradata) {\n if (avctx->channels > 2) {\n av_log(avctx, AV_LOG_ERROR,\n "Multichannel configuration without extradata.\\n");\n return AVERROR(EINVAL);\n }\n default_extradata[9] = (avctx->channels == 1) ? 1 : 2;\n extradata = default_extradata;\n extradata_size = sizeof(default_extradata);\n } else {\n extradata = avctx->extradata;\n extradata_size = avctx->extradata_size;\n }\n if (extradata_size < 19) {\n av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\\n",\n extradata_size);\n return AVERROR_INVALIDDATA;\n }\n version = extradata[8];\n if (version > 15) {\n avpriv_request_sample(avctx, "Extradata version %d", version);\n return AVERROR_PATCHWELCOME;\n }\n avctx->delay = AV_RL16(extradata + 10);\n channels = extradata[9];\n if (!channels) {\n av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\\n");\n return AVERROR_INVALIDDATA;\n }\n s->gain_i = AV_RL16(extradata + 16);\n if (s->gain_i)\n s->gain = pow(10, s->gain_i / (20.0 * 256));\n map_type = extradata[18];\n if (!map_type) {\n if (channels > 2) {\n av_log(avctx, AV_LOG_ERROR,\n "Channel mapping 0 is only specified for up to 2 channels\\n");\n return AVERROR_INVALIDDATA;\n }\n layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;\n streams = 1;\n stereo_streams = channels - 1;\n channel_map = default_channel_map;\n } else if (map_type == 1 || map_type == 255) {\n if (extradata_size < 21 + channels) {\n av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\\n",\n extradata_size);\n return AVERROR_INVALIDDATA;\n }\n streams = extradata[19];\n stereo_streams = extradata[20];\n if (!streams || stereo_streams > streams ||\n streams + stereo_streams > 255) {\n av_log(avctx, AV_LOG_ERROR,\n "Invalid stream/stereo stream count: %d/%d\\n", streams, stereo_streams);\n return AVERROR_INVALIDDATA;\n }\n if (map_type == 1) {\n if (channels > 8) {\n av_log(avctx, AV_LOG_ERROR,\n "Channel mapping 1 is only specified for up to 8 channels\\n");\n return AVERROR_INVALIDDATA;\n }\n layout = ff_vorbis_channel_layouts[channels - 1];\n channel_reorder = channel_reorder_vorbis;\n } else\n layout = 0;\n channel_map = extradata + 21;\n } else {\n avpriv_request_sample(avctx, "Mapping type %d", map_type);\n return AVERROR_PATCHWELCOME;\n }\n s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));\n if (!s->channel_maps)\n return AVERROR(ENOMEM);\n for (i = 0; i < channels; i++) {\n ChannelMap *map = &s->channel_maps[i];\n uint8_t idx = channel_map[channel_reorder(channels, i)];\n if (idx == 255) {\n map->silence = 1;\n continue;\n } else if (idx >= streams + stereo_streams) {\n av_log(avctx, AV_LOG_ERROR,\n "Invalid channel map for output channel %d: %d\\n", i, idx);\n return AVERROR_INVALIDDATA;\n }\n map->copy = 0;\n for (j = 0; j < i; j++)\n if (channel_map[channel_reorder(channels, j)] == idx) {\n map->copy = 1;\n map->copy_idx = j;\n break;\n }\n if (idx < 2 * stereo_streams) {\n map->stream_idx = idx / 2;\n map->channel_idx = idx & 1;\n } else {\n map->stream_idx = idx - stereo_streams;\n map->channel_idx = 0;\n }\n }\n avctx->channels = channels;\n avctx->channel_layout = layout;\n s->nb_streams = streams;\n s->nb_stereo_streams = stereo_streams;\n return 0;\n}']
|
2,856
| 0
|
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/mem.c/#L312
|
void CRYPTO_free(void *str, const char *file, int line)
{
INCREMENT(free_count);
if (free_impl != NULL && free_impl != &CRYPTO_free) {
free_impl(str, file, line);
return;
}
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
if (call_malloc_debug) {
CRYPTO_mem_debug_free(str, 0, file, line);
free(str);
CRYPTO_mem_debug_free(str, 1, file, line);
} else {
free(str);
}
#else
free(str);
#endif
}
|
['static int tlsa_import_rrset(SSL *con, STACK_OF(OPENSSL_STRING) *rrset)\n{\n int num = sk_OPENSSL_STRING_num(rrset);\n int count = 0;\n int i;\n for (i = 0; i < num; ++i) {\n char *rrdata = sk_OPENSSL_STRING_value(rrset, i);\n if (tlsa_import_rr(con, rrdata) > 0)\n ++count;\n }\n return count > 0;\n}', 'static int tlsa_import_rr(SSL *con, const char *rrdata)\n{\n static uint8_t usage;\n static uint8_t selector;\n static uint8_t mtype;\n static unsigned char *data;\n static struct tlsa_field tlsa_fields[] = {\n { &usage, "usage", checked_uint8 },\n { &selector, "selector", checked_uint8 },\n { &mtype, "mtype", checked_uint8 },\n { &data, "data", hexdecode },\n { NULL, }\n };\n struct tlsa_field *f;\n int ret;\n const char *cp = rrdata;\n ossl_ssize_t len = 0;\n for (f = tlsa_fields; f->var; ++f) {\n if ((len = f->parser(&cp, f->var)) <= 0) {\n BIO_printf(bio_err, "%s: warning: bad TLSA %s field in: %s\\n",\n prog, f->name, rrdata);\n return 0;\n }\n }\n ret = SSL_dane_tlsa_add(con, usage, selector, mtype, data, len);\n OPENSSL_free(data);\n if (ret == 0) {\n ERR_print_errors(bio_err);\n BIO_printf(bio_err, "%s: warning: unusable TLSA rrdata: %s\\n",\n prog, rrdata);\n return 0;\n }\n if (ret < 0) {\n ERR_print_errors(bio_err);\n BIO_printf(bio_err, "%s: warning: error loading TLSA rrdata: %s\\n",\n prog, rrdata);\n return 0;\n }\n return ret;\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,857
| 0
|
https://github.com/openssl/openssl/blob/816d78572188d76dc9f08fa00a93d0c65f64e02a/crypto/ui/ui_lib.c/#L624
|
UI_METHOD *UI_create_method(char *name)
{
UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
if (ui_method)
memset(ui_method, 0, sizeof(*ui_method));
ui_method->name = BUF_strdup(name);
return ui_method;
}
|
['UI_METHOD *UI_create_method(char *name)\n\t{\n\tUI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));\n\tif (ui_method)\n\t\tmemset(ui_method, 0, sizeof(*ui_method));\n\tui_method->name = BUF_strdup(name);\n\treturn ui_method;\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num < 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tif (str == NULL) return(NULL);\n\treturn BUF_strndup(str, strlen(str));\n\t}', 'char *BUF_strndup(const char *str, size_t siz)\n\t{\n\tchar *ret;\n\tif (str == NULL) return(NULL);\n\tret=OPENSSL_malloc(siz+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBUF_strlcpy(ret,str,siz+1);\n\treturn(ret);\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
|
2,858
| 0
|
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/x509/x509_obj.c/#L96
|
char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)
{
X509_NAME_ENTRY *ne;
int i;
int n, lold, l, l1, l2, num, j, type;
const char *s;
char *p;
unsigned char *q;
BUF_MEM *b = NULL;
static const char hex[17] = "0123456789ABCDEF";
int gs_doit[4];
char tmp_buf[80];
#ifdef CHARSET_EBCDIC
char ebcdic_buf[1024];
#endif
if (buf == NULL) {
if ((b = BUF_MEM_new()) == NULL)
goto err;
if (!BUF_MEM_grow(b, 200))
goto err;
b->data[0] = '\0';
len = 200;
}
if (a == NULL) {
if (b) {
buf = b->data;
OPENSSL_free(b);
}
strncpy(buf, "NO X509_NAME", len);
buf[len - 1] = '\0';
return buf;
}
len--;
l = 0;
for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
ne = sk_X509_NAME_ENTRY_value(a->entries, i);
n = OBJ_obj2nid(ne->object);
if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
s = tmp_buf;
}
l1 = strlen(s);
type = ne->value->type;
num = ne->value->length;
q = ne->value->data;
#ifdef CHARSET_EBCDIC
if (type == V_ASN1_GENERALSTRING ||
type == V_ASN1_VISIBLESTRING ||
type == V_ASN1_PRINTABLESTRING ||
type == V_ASN1_TELETEXSTRING ||
type == V_ASN1_VISIBLESTRING || type == V_ASN1_IA5STRING) {
ascii2ebcdic(ebcdic_buf, q, (num > sizeof ebcdic_buf)
? sizeof ebcdic_buf : num);
q = ebcdic_buf;
}
#endif
if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
for (j = 0; j < num; j++)
if (q[j] != 0)
gs_doit[j & 3] = 1;
if (gs_doit[0] | gs_doit[1] | gs_doit[2])
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
else {
gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
gs_doit[3] = 1;
}
} else
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
for (l2 = j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
l2++;
#ifndef CHARSET_EBCDIC
if ((q[j] < ' ') || (q[j] > '~'))
l2 += 3;
#else
if ((os_toascii[q[j]] < os_toascii[' ']) ||
(os_toascii[q[j]] > os_toascii['~']))
l2 += 3;
#endif
}
lold = l;
l += 1 + l1 + 1 + l2;
if (b != NULL) {
if (!BUF_MEM_grow(b, l + 1))
goto err;
p = &(b->data[lold]);
} else if (l > len) {
break;
} else
p = &(buf[lold]);
*(p++) = '/';
memcpy(p, s, (unsigned int)l1);
p += l1;
*(p++) = '=';
#ifndef CHARSET_EBCDIC
q = ne->value->data;
#endif
for (j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
#ifndef CHARSET_EBCDIC
n = q[j];
if ((n < ' ') || (n > '~')) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = n;
#else
n = os_toascii[q[j]];
if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = q[j];
#endif
}
*p = '\0';
}
if (b != NULL) {
p = b->data;
OPENSSL_free(b);
} else
p = buf;
if (i == 0)
*p = '\0';
return (p);
err:
X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);
BUF_MEM_free(b);
return (NULL);
}
|
['int x509_main(int argc, char **argv)\n{\n ASN1_INTEGER *sno = NULL;\n ASN1_OBJECT *objtmp;\n BIO *out = NULL;\n CONF *extconf = NULL;\n EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL;\n STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;\n STACK_OF(OPENSSL_STRING) *sigopts = NULL;\n X509 *x = NULL, *xca = NULL;\n X509_REQ *req = NULL, *rq = NULL;\n X509_STORE *ctx = NULL;\n const EVP_MD *digest = NULL;\n char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL;\n char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;\n char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;\n char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;\n char buf[256], *prog;\n int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0;\n int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM;\n int fingerprint = 0, reqfile = 0, need_rand = 0, checkend = 0;\n int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;\n int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;\n int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;\n int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;\n int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0;\n int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0;\n int checkoffset = 0, enddate = 0;\n unsigned long nmflag = 0, certflag = 0;\n char nmflag_set = 0;\n OPTION_CHOICE o;\n ENGINE *e = NULL;\n#ifndef OPENSSL_NO_MD5\n int subject_hash_old = 0, issuer_hash_old = 0;\n#endif\n ctx = X509_STORE_new();\n if (ctx == NULL)\n goto end;\n X509_STORE_set_verify_cb(ctx, callb);\n prog = opt_init(argc, argv, x509_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n opthelp:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n opt_help(x509_options);\n ret = 0;\n goto end;\n case OPT_INFORM:\n if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))\n goto opthelp;\n break;\n case OPT_IN:\n infile = opt_arg();\n break;\n case OPT_OUTFORM:\n if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))\n goto opthelp;\n break;\n case OPT_KEYFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))\n goto opthelp;\n break;\n case OPT_CAFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))\n goto opthelp;\n break;\n case OPT_CAKEYFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAkeyformat))\n goto opthelp;\n break;\n case OPT_OUT:\n outfile = opt_arg();\n break;\n case OPT_REQ:\n reqfile = need_rand = 1;\n break;\n case OPT_SIGOPT:\n if (!sigopts)\n sigopts = sk_OPENSSL_STRING_new_null();\n if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))\n goto opthelp;\n break;\n case OPT_FORCE_VERSION:\n#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL\n force_version = atoi(opt_arg()) - 1;\n#endif\n break;\n case OPT_DAYS:\n days = atoi(opt_arg());\n break;\n case OPT_PASSIN:\n passinarg = opt_arg();\n break;\n case OPT_EXTFILE:\n extfile = opt_arg();\n break;\n case OPT_EXTENSIONS:\n extsect = opt_arg();\n break;\n case OPT_SIGNKEY:\n keyfile = opt_arg();\n sign_flag = ++num;\n need_rand = 1;\n break;\n case OPT_CA:\n CAfile = opt_arg();\n CA_flag = ++num;\n need_rand = 1;\n break;\n case OPT_CAKEY:\n CAkeyfile = opt_arg();\n break;\n case OPT_CASERIAL:\n CAserial = opt_arg();\n break;\n case OPT_SET_SERIAL:\n if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)\n goto opthelp;\n break;\n case OPT_FORCE_PUBKEY:\n fkeyfile = opt_arg();\n break;\n case OPT_ADDTRUST:\n if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {\n BIO_printf(bio_err,\n "%s: Invalid trust object value %s\\n",\n prog, opt_arg());\n goto opthelp;\n }\n if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)\n goto end;\n sk_ASN1_OBJECT_push(trust, objtmp);\n trustout = 1;\n break;\n case OPT_ADDREJECT:\n if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {\n BIO_printf(bio_err,\n "%s: Invalid reject object value %s\\n",\n prog, opt_arg());\n goto opthelp;\n }\n if (reject == NULL\n && (reject = sk_ASN1_OBJECT_new_null()) == NULL)\n goto end;\n sk_ASN1_OBJECT_push(reject, objtmp);\n trustout = 1;\n break;\n case OPT_SETALIAS:\n alias = opt_arg();\n trustout = 1;\n break;\n case OPT_CERTOPT:\n if (!set_cert_ex(&certflag, opt_arg()))\n goto opthelp;\n break;\n case OPT_NAMEOPT:\n nmflag_set = 1;\n if (!set_name_ex(&nmflag, opt_arg()))\n goto opthelp;\n break;\n case OPT_ENGINE:\n e = setup_engine(opt_arg(), 0);\n break;\n case OPT_C:\n C = ++num;\n break;\n case OPT_EMAIL:\n email = ++num;\n break;\n case OPT_OCSP_URI:\n ocsp_uri = ++num;\n break;\n case OPT_SERIAL:\n serial = ++num;\n break;\n case OPT_NEXT_SERIAL:\n next_serial = ++num;\n break;\n case OPT_MODULUS:\n modulus = ++num;\n break;\n case OPT_PUBKEY:\n pubkey = ++num;\n break;\n case OPT_X509TOREQ:\n x509req = ++num;\n break;\n case OPT_TEXT:\n text = ++num;\n break;\n case OPT_SUBJECT:\n subject = ++num;\n break;\n case OPT_ISSUER:\n issuer = ++num;\n break;\n case OPT_FINGERPRINT:\n fingerprint = ++num;\n break;\n case OPT_HASH:\n subject_hash = ++num;\n break;\n case OPT_ISSUER_HASH:\n issuer_hash = ++num;\n break;\n case OPT_PURPOSE:\n pprint = ++num;\n break;\n case OPT_STARTDATE:\n startdate = ++num;\n break;\n case OPT_ENDDATE:\n enddate = ++num;\n break;\n case OPT_NOOUT:\n noout = ++num;\n break;\n case OPT_NOCERT:\n nocert = 1;\n break;\n case OPT_TRUSTOUT:\n trustout = 1;\n break;\n case OPT_CLRTRUST:\n clrtrust = ++num;\n break;\n case OPT_CLRREJECT:\n clrreject = ++num;\n break;\n case OPT_ALIAS:\n aliasout = ++num;\n break;\n case OPT_CACREATESERIAL:\n CA_createserial = ++num;\n break;\n case OPT_CLREXT:\n clrext = 1;\n break;\n case OPT_OCSPID:\n ocspid = ++num;\n break;\n case OPT_BADSIG:\n badsig = 1;\n break;\n#ifndef OPENSSL_NO_MD5\n case OPT_SUBJECT_HASH_OLD:\n subject_hash_old = ++num;\n break;\n case OPT_ISSUER_HASH_OLD:\n issuer_hash_old = ++num;\n break;\n#else\n case OPT_SUBJECT_HASH_OLD:\n case OPT_ISSUER_HASH_OLD:\n break;\n#endif\n case OPT_DATES:\n startdate = ++num;\n enddate = ++num;\n break;\n case OPT_CHECKEND:\n checkoffset = atoi(opt_arg());\n checkend = 1;\n break;\n case OPT_CHECKHOST:\n checkhost = opt_arg();\n break;\n case OPT_CHECKEMAIL:\n checkemail = opt_arg();\n break;\n case OPT_CHECKIP:\n checkip = opt_arg();\n break;\n case OPT_MD:\n if (!opt_md(opt_unknown(), &digest))\n goto opthelp;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n if (argc != 0) {\n BIO_printf(bio_err, "%s: Unknown parameter %s\\n", prog, argv[0]);\n goto opthelp;\n }\n if (!nmflag_set)\n nmflag = XN_FLAG_ONELINE;\n out = bio_open_default(outfile, \'w\', outformat);\n if (out == NULL)\n goto end;\n if (need_rand)\n app_RAND_load_file(NULL, 0);\n if (!app_passwd(passinarg, NULL, &passin, NULL)) {\n BIO_printf(bio_err, "Error getting password\\n");\n goto end;\n }\n if (!X509_STORE_set_default_paths(ctx)) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (fkeyfile) {\n fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");\n if (fkey == NULL)\n goto end;\n }\n if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {\n CAkeyfile = CAfile;\n } else if ((CA_flag) && (CAkeyfile == NULL)) {\n BIO_printf(bio_err,\n "need to specify a CAkey if using the CA command\\n");\n goto end;\n }\n if (extfile) {\n X509V3_CTX ctx2;\n if ((extconf = app_load_config(extfile)) == NULL)\n goto end;\n if (!extsect) {\n extsect = NCONF_get_string(extconf, "default", "extensions");\n if (!extsect) {\n ERR_clear_error();\n extsect = "default";\n }\n }\n X509V3_set_ctx_test(&ctx2);\n X509V3_set_nconf(&ctx2, extconf);\n if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {\n BIO_printf(bio_err,\n "Error Loading extension section %s\\n", extsect);\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (reqfile) {\n EVP_PKEY *pkey;\n BIO *in;\n if (!sign_flag && !CA_flag) {\n BIO_printf(bio_err, "We need a private key to sign with\\n");\n goto end;\n }\n in = bio_open_default(infile, \'r\', informat);\n if (in == NULL)\n goto end;\n req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);\n BIO_free(in);\n if (req == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {\n BIO_printf(bio_err, "error unpacking public key\\n");\n goto end;\n }\n i = X509_REQ_verify(req, pkey);\n EVP_PKEY_free(pkey);\n if (i < 0) {\n BIO_printf(bio_err, "Signature verification error\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n if (i == 0) {\n BIO_printf(bio_err,\n "Signature did not match the certificate request\\n");\n goto end;\n } else\n BIO_printf(bio_err, "Signature ok\\n");\n print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),\n nmflag);\n if ((x = X509_new()) == NULL)\n goto end;\n if (sno == NULL) {\n sno = ASN1_INTEGER_new();\n if (sno == NULL || !rand_serial(NULL, sno))\n goto end;\n if (!X509_set_serialNumber(x, sno))\n goto end;\n ASN1_INTEGER_free(sno);\n sno = NULL;\n } else if (!X509_set_serialNumber(x, sno))\n goto end;\n if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req)))\n goto end;\n if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req)))\n goto end;\n X509_gmtime_adj(X509_get_notBefore(x), 0);\n X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);\n if (fkey)\n X509_set_pubkey(x, fkey);\n else {\n pkey = X509_REQ_get_pubkey(req);\n X509_set_pubkey(x, pkey);\n EVP_PKEY_free(pkey);\n }\n } else\n x = load_cert(infile, informat, NULL, e, "Certificate");\n if (x == NULL)\n goto end;\n if (CA_flag) {\n xca = load_cert(CAfile, CAformat, NULL, e, "CA Certificate");\n if (xca == NULL)\n goto end;\n }\n if (!noout || text || next_serial) {\n OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");\n }\n if (alias)\n X509_alias_set1(x, (unsigned char *)alias, -1);\n if (clrtrust)\n X509_trust_clear(x);\n if (clrreject)\n X509_reject_clear(x);\n if (trust) {\n for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {\n objtmp = sk_ASN1_OBJECT_value(trust, i);\n X509_add1_trust_object(x, objtmp);\n }\n }\n if (reject) {\n for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {\n objtmp = sk_ASN1_OBJECT_value(reject, i);\n X509_add1_reject_object(x, objtmp);\n }\n }\n if (num) {\n for (i = 1; i <= num; i++) {\n if (issuer == i) {\n print_name(out, "issuer= ", X509_get_issuer_name(x), nmflag);\n } else if (subject == i) {\n print_name(out, "subject= ",\n X509_get_subject_name(x), nmflag);\n } else if (serial == i) {\n BIO_printf(out, "serial=");\n i2a_ASN1_INTEGER(out, X509_get_serialNumber(x));\n BIO_printf(out, "\\n");\n } else if (next_serial == i) {\n BIGNUM *bnser;\n ASN1_INTEGER *ser;\n ser = X509_get_serialNumber(x);\n bnser = ASN1_INTEGER_to_BN(ser, NULL);\n if (!bnser)\n goto end;\n if (!BN_add_word(bnser, 1))\n goto end;\n ser = BN_to_ASN1_INTEGER(bnser, NULL);\n if (!ser)\n goto end;\n BN_free(bnser);\n i2a_ASN1_INTEGER(out, ser);\n ASN1_INTEGER_free(ser);\n BIO_puts(out, "\\n");\n } else if ((email == i) || (ocsp_uri == i)) {\n int j;\n STACK_OF(OPENSSL_STRING) *emlst;\n if (email == i)\n emlst = X509_get1_email(x);\n else\n emlst = X509_get1_ocsp(x);\n for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)\n BIO_printf(out, "%s\\n",\n sk_OPENSSL_STRING_value(emlst, j));\n X509_email_free(emlst);\n } else if (aliasout == i) {\n unsigned char *alstr;\n alstr = X509_alias_get0(x, NULL);\n if (alstr)\n BIO_printf(out, "%s\\n", alstr);\n else\n BIO_puts(out, "<No Alias>\\n");\n } else if (subject_hash == i) {\n BIO_printf(out, "%08lx\\n", X509_subject_name_hash(x));\n }\n#ifndef OPENSSL_NO_MD5\n else if (subject_hash_old == i) {\n BIO_printf(out, "%08lx\\n", X509_subject_name_hash_old(x));\n }\n#endif\n else if (issuer_hash == i) {\n BIO_printf(out, "%08lx\\n", X509_issuer_name_hash(x));\n }\n#ifndef OPENSSL_NO_MD5\n else if (issuer_hash_old == i) {\n BIO_printf(out, "%08lx\\n", X509_issuer_name_hash_old(x));\n }\n#endif\n else if (pprint == i) {\n X509_PURPOSE *ptmp;\n int j;\n BIO_printf(out, "Certificate purposes:\\n");\n for (j = 0; j < X509_PURPOSE_get_count(); j++) {\n ptmp = X509_PURPOSE_get0(j);\n purpose_print(out, x, ptmp);\n }\n } else if (modulus == i) {\n EVP_PKEY *pkey;\n pkey = X509_get0_pubkey(x);\n if (pkey == NULL) {\n BIO_printf(bio_err, "Modulus=unavailable\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n BIO_printf(out, "Modulus=");\n#ifndef OPENSSL_NO_RSA\n if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)\n BN_print(out, EVP_PKEY_get0_RSA(pkey)->n);\n else\n#endif\n#ifndef OPENSSL_NO_DSA\n if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA)\n BN_print(out, EVP_PKEY_get0_DSA(pkey)->pub_key);\n else\n#endif\n BIO_printf(out, "Wrong Algorithm type");\n BIO_printf(out, "\\n");\n } else if (pubkey == i) {\n EVP_PKEY *pkey;\n pkey = X509_get0_pubkey(x);\n if (pkey == NULL) {\n BIO_printf(bio_err, "Error getting public key\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n PEM_write_bio_PUBKEY(out, pkey);\n } else if (C == i) {\n unsigned char *d;\n char *m;\n int len;\n X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf);\n BIO_printf(out, "/*\\n"\n " * Subject: %s\\n", buf);\n m = X509_NAME_oneline(X509_get_issuer_name(x), buf, sizeof buf);\n BIO_printf(out, " * Issuer: %s\\n"\n " */\\n", buf);\n len = i2d_X509(x, NULL);\n m = app_malloc(len, "x509 name buffer");\n d = (unsigned char *)m;\n len = i2d_X509_NAME(X509_get_subject_name(x), &d);\n print_array(out, "the_subject_name", len, (unsigned char *)m);\n d = (unsigned char *)m;\n len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);\n print_array(out, "the_public_key", len, (unsigned char *)m);\n d = (unsigned char *)m;\n len = i2d_X509(x, &d);\n print_array(out, "the_certificate", len, (unsigned char *)m);\n OPENSSL_free(m);\n } else if (text == i) {\n X509_print_ex(out, x, nmflag, certflag);\n } else if (startdate == i) {\n BIO_puts(out, "notBefore=");\n ASN1_TIME_print(out, X509_get_notBefore(x));\n BIO_puts(out, "\\n");\n } else if (enddate == i) {\n BIO_puts(out, "notAfter=");\n ASN1_TIME_print(out, X509_get_notAfter(x));\n BIO_puts(out, "\\n");\n } else if (fingerprint == i) {\n int j;\n unsigned int n;\n unsigned char md[EVP_MAX_MD_SIZE];\n const EVP_MD *fdig = digest;\n if (!fdig)\n fdig = EVP_sha1();\n if (!X509_digest(x, fdig, md, &n)) {\n BIO_printf(bio_err, "out of memory\\n");\n goto end;\n }\n BIO_printf(out, "%s Fingerprint=",\n OBJ_nid2sn(EVP_MD_type(fdig)));\n for (j = 0; j < (int)n; j++) {\n BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)\n ? \'\\n\' : \':\');\n }\n }\n else if ((sign_flag == i) && (x509req == 0)) {\n BIO_printf(bio_err, "Getting Private key\\n");\n if (Upkey == NULL) {\n Upkey = load_key(keyfile, keyformat, 0,\n passin, e, "Private key");\n if (Upkey == NULL)\n goto end;\n }\n assert(need_rand);\n if (!sign(x, Upkey, days, clrext, digest, extconf, extsect))\n goto end;\n } else if (CA_flag == i) {\n BIO_printf(bio_err, "Getting CA Private Key\\n");\n if (CAkeyfile != NULL) {\n CApkey = load_key(CAkeyfile, CAkeyformat,\n 0, passin, e, "CA Private Key");\n if (CApkey == NULL)\n goto end;\n }\n assert(need_rand);\n if (!x509_certify(ctx, CAfile, digest, x, xca,\n CApkey, sigopts,\n CAserial, CA_createserial, days, clrext,\n extconf, extsect, sno, reqfile))\n goto end;\n } else if (x509req == i) {\n EVP_PKEY *pk;\n BIO_printf(bio_err, "Getting request Private Key\\n");\n if (keyfile == NULL) {\n BIO_printf(bio_err, "no request key file specified\\n");\n goto end;\n } else {\n pk = load_key(keyfile, keyformat, 0,\n passin, e, "request key");\n if (pk == NULL)\n goto end;\n }\n BIO_printf(bio_err, "Generating certificate request\\n");\n rq = X509_to_X509_REQ(x, pk, digest);\n EVP_PKEY_free(pk);\n if (rq == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (!noout) {\n X509_REQ_print(out, rq);\n PEM_write_bio_X509_REQ(out, rq);\n }\n noout = 1;\n } else if (ocspid == i) {\n X509_ocspid_print(out, x);\n }\n }\n }\n if (checkend) {\n time_t tcheck = time(NULL) + checkoffset;\n if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {\n BIO_printf(out, "Certificate will expire\\n");\n ret = 1;\n } else {\n BIO_printf(out, "Certificate will not expire\\n");\n ret = 0;\n }\n goto end;\n }\n print_cert_checks(out, x, checkhost, checkemail, checkip);\n if (noout || nocert) {\n ret = 0;\n goto end;\n }\n if (badsig) {\n ASN1_BIT_STRING *signature;\n unsigned char *s;\n X509_get0_signature(&signature, NULL, x);\n s = ASN1_STRING_data(signature);\n s[ASN1_STRING_length(signature) - 1] ^= 0x1;\n }\n if (outformat == FORMAT_ASN1)\n i = i2d_X509_bio(out, x);\n else if (outformat == FORMAT_PEM) {\n if (trustout)\n i = PEM_write_bio_X509_AUX(out, x);\n else\n i = PEM_write_bio_X509(out, x);\n } else {\n BIO_printf(bio_err, "bad output format specified for outfile\\n");\n goto end;\n }\n if (!i) {\n BIO_printf(bio_err, "unable to write certificate\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n ret = 0;\n end:\n if (need_rand)\n app_RAND_write_file(NULL);\n OBJ_cleanup();\n NCONF_free(extconf);\n BIO_free_all(out);\n X509_STORE_free(ctx);\n X509_REQ_free(req);\n X509_free(x);\n X509_free(xca);\n EVP_PKEY_free(Upkey);\n EVP_PKEY_free(CApkey);\n EVP_PKEY_free(fkey);\n sk_OPENSSL_STRING_free(sigopts);\n X509_REQ_free(rq);\n ASN1_INTEGER_free(sno);\n sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);\n sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);\n OPENSSL_free(passin);\n return (ret);\n}', 'char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)\n{\n X509_NAME_ENTRY *ne;\n int i;\n int n, lold, l, l1, l2, num, j, type;\n const char *s;\n char *p;\n unsigned char *q;\n BUF_MEM *b = NULL;\n static const char hex[17] = "0123456789ABCDEF";\n int gs_doit[4];\n char tmp_buf[80];\n#ifdef CHARSET_EBCDIC\n char ebcdic_buf[1024];\n#endif\n if (buf == NULL) {\n if ((b = BUF_MEM_new()) == NULL)\n goto err;\n if (!BUF_MEM_grow(b, 200))\n goto err;\n b->data[0] = \'\\0\';\n len = 200;\n }\n if (a == NULL) {\n if (b) {\n buf = b->data;\n OPENSSL_free(b);\n }\n strncpy(buf, "NO X509_NAME", len);\n buf[len - 1] = \'\\0\';\n return buf;\n }\n len--;\n l = 0;\n for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {\n ne = sk_X509_NAME_ENTRY_value(a->entries, i);\n n = OBJ_obj2nid(ne->object);\n if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {\n i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);\n s = tmp_buf;\n }\n l1 = strlen(s);\n type = ne->value->type;\n num = ne->value->length;\n q = ne->value->data;\n#ifdef CHARSET_EBCDIC\n if (type == V_ASN1_GENERALSTRING ||\n type == V_ASN1_VISIBLESTRING ||\n type == V_ASN1_PRINTABLESTRING ||\n type == V_ASN1_TELETEXSTRING ||\n type == V_ASN1_VISIBLESTRING || type == V_ASN1_IA5STRING) {\n ascii2ebcdic(ebcdic_buf, q, (num > sizeof ebcdic_buf)\n ? sizeof ebcdic_buf : num);\n q = ebcdic_buf;\n }\n#endif\n if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;\n for (j = 0; j < num; j++)\n if (q[j] != 0)\n gs_doit[j & 3] = 1;\n if (gs_doit[0] | gs_doit[1] | gs_doit[2])\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n else {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;\n gs_doit[3] = 1;\n }\n } else\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n for (l2 = j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n l2++;\n#ifndef CHARSET_EBCDIC\n if ((q[j] < \' \') || (q[j] > \'~\'))\n l2 += 3;\n#else\n if ((os_toascii[q[j]] < os_toascii[\' \']) ||\n (os_toascii[q[j]] > os_toascii[\'~\']))\n l2 += 3;\n#endif\n }\n lold = l;\n l += 1 + l1 + 1 + l2;\n if (b != NULL) {\n if (!BUF_MEM_grow(b, l + 1))\n goto err;\n p = &(b->data[lold]);\n } else if (l > len) {\n break;\n } else\n p = &(buf[lold]);\n *(p++) = \'/\';\n memcpy(p, s, (unsigned int)l1);\n p += l1;\n *(p++) = \'=\';\n#ifndef CHARSET_EBCDIC\n q = ne->value->data;\n#endif\n for (j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n#ifndef CHARSET_EBCDIC\n n = q[j];\n if ((n < \' \') || (n > \'~\')) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = n;\n#else\n n = os_toascii[q[j]];\n if ((n < os_toascii[\' \']) || (n > os_toascii[\'~\'])) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = q[j];\n#endif\n }\n *p = \'\\0\';\n }\n if (b != NULL) {\n p = b->data;\n OPENSSL_free(b);\n } else\n p = buf;\n if (i == 0)\n *p = \'\\0\';\n return (p);\n err:\n X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);\n BUF_MEM_free(b);\n return (NULL);\n}']
|
2,859
| 0
|
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L232
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
|
2,860
| 0
|
https://github.com/openssl/openssl/blob/ddc6a5c8f5900959bdbdfee79e1625a3f7808acd/crypto/rand/drbg_rand.c/#L220
|
static void ctr_update(RAND_DRBG *drbg,
const unsigned char *in1, size_t in1len,
const unsigned char *in2, size_t in2len,
const unsigned char *nonce, size_t noncelen)
{
RAND_DRBG_CTR *ctr = &drbg->ctr;
inc_128(ctr);
AES_encrypt(ctr->V, ctr->K, &ctr->ks);
if (ctr->keylen != 16) {
inc_128(ctr);
AES_encrypt(ctr->V, ctr->K + 16, &ctr->ks);
}
inc_128(ctr);
AES_encrypt(ctr->V, ctr->V, &ctr->ks);
if (ctr->keylen == 24) {
memcpy(ctr->V + 8, ctr->V, 8);
memcpy(ctr->V, ctr->K + 24, 8);
}
if (drbg->flags & RAND_DRBG_FLAG_CTR_USE_DF) {
if (in1 != NULL || nonce != NULL || in2 != NULL)
ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len);
if (in1len)
ctr_XOR(ctr, ctr->KX, drbg->seedlen);
} else {
ctr_XOR(ctr, in1, in1len);
ctr_XOR(ctr, in2, in2len);
}
AES_set_encrypt_key(ctr->K, drbg->strength, &ctr->ks);
}
|
['static void ctr_update(RAND_DRBG *drbg,\n const unsigned char *in1, size_t in1len,\n const unsigned char *in2, size_t in2len,\n const unsigned char *nonce, size_t noncelen)\n{\n RAND_DRBG_CTR *ctr = &drbg->ctr;\n inc_128(ctr);\n AES_encrypt(ctr->V, ctr->K, &ctr->ks);\n if (ctr->keylen != 16) {\n inc_128(ctr);\n AES_encrypt(ctr->V, ctr->K + 16, &ctr->ks);\n }\n inc_128(ctr);\n AES_encrypt(ctr->V, ctr->V, &ctr->ks);\n if (ctr->keylen == 24) {\n memcpy(ctr->V + 8, ctr->V, 8);\n memcpy(ctr->V, ctr->K + 24, 8);\n }\n if (drbg->flags & RAND_DRBG_FLAG_CTR_USE_DF) {\n if (in1 != NULL || nonce != NULL || in2 != NULL)\n ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len);\n if (in1len)\n ctr_XOR(ctr, ctr->KX, drbg->seedlen);\n } else {\n ctr_XOR(ctr, in1, in1len);\n ctr_XOR(ctr, in2, in2len);\n }\n AES_set_encrypt_key(ctr->K, drbg->strength, &ctr->ks);\n}']
|
2,861
| 0
|
https://github.com/nginx/nginx/blob/ea327f13f130be87cf3023e215d1c97b5465343f/src/http/modules/ngx_http_geo_module.c/#L816
|
static char *
ngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
in_addr_t start, in_addr_t end)
{
in_addr_t n;
ngx_uint_t h, i, s, e;
ngx_array_t *a;
ngx_http_geo_range_t *range;
for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {
h = n >> 16;
if (n == start) {
s = n & 0xffff;
} else {
s = 0;
}
if ((n | 0xffff) > end) {
e = end & 0xffff;
} else {
e = 0xffff;
}
a = (ngx_array_t *) ctx->high.low[h];
if (a == NULL) {
a = ngx_array_create(ctx->temp_pool, 64,
sizeof(ngx_http_geo_range_t));
if (a == NULL) {
return NGX_CONF_ERROR;
}
ctx->high.low[h] = (ngx_http_geo_range_t *) a;
}
i = a->nelts;
range = a->elts;
while (i) {
i--;
if (e < (ngx_uint_t) range[i].start) {
continue;
}
if (s > (ngx_uint_t) range[i].end) {
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 2], &range[i + 1],
(a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
range[i + 1].start = (u_short) s;
range[i + 1].end = (u_short) e;
range[i + 1].value = ctx->value;
goto next;
}
if (s == (ngx_uint_t) range[i].start
&& e == (ngx_uint_t) range[i].end)
{
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
ctx->net, ctx->value, range[i].value);
range[i].value = ctx->value;
goto next;
}
if (s > (ngx_uint_t) range[i].start
&& e < (ngx_uint_t) range[i].end)
{
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 3], &range[i + 1],
(a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t));
range[i + 2].start = (u_short) (e + 1);
range[i + 2].end = range[i].end;
range[i + 2].value = range[i].value;
range[i + 1].start = (u_short) s;
range[i + 1].end = (u_short) e;
range[i + 1].value = ctx->value;
range[i].end = (u_short) (s - 1);
goto next;
}
if (s == (ngx_uint_t) range[i].start
&& e < (ngx_uint_t) range[i].end)
{
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 1], &range[i],
(a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));
range[i + 1].start = (u_short) (e + 1);
range[i].start = (u_short) s;
range[i].end = (u_short) e;
range[i].value = ctx->value;
goto next;
}
if (s > (ngx_uint_t) range[i].start
&& e == (ngx_uint_t) range[i].end)
{
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 2], &range[i + 1],
(a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
range[i + 1].start = (u_short) s;
range[i + 1].end = (u_short) e;
range[i + 1].value = ctx->value;
range[i].end = (u_short) (s - 1);
goto next;
}
s = (ngx_uint_t) range[i].start;
e = (ngx_uint_t) range[i].end;
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"",
ctx->net,
h >> 8, h & 0xff, s >> 8, s & 0xff,
h >> 8, h & 0xff, e >> 8, e & 0xff);
return NGX_CONF_ERROR;
}
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range->start = (u_short) s;
range->end = (u_short) e;
range->value = ctx->value;
next:
continue;
}
return NGX_CONF_OK;
}
|
['static char *\nngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,\n in_addr_t start, in_addr_t end)\n{\n in_addr_t n;\n ngx_uint_t h, i, s, e;\n ngx_array_t *a;\n ngx_http_geo_range_t *range;\n for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {\n h = n >> 16;\n if (n == start) {\n s = n & 0xffff;\n } else {\n s = 0;\n }\n if ((n | 0xffff) > end) {\n e = end & 0xffff;\n } else {\n e = 0xffff;\n }\n a = (ngx_array_t *) ctx->high.low[h];\n if (a == NULL) {\n a = ngx_array_create(ctx->temp_pool, 64,\n sizeof(ngx_http_geo_range_t));\n if (a == NULL) {\n return NGX_CONF_ERROR;\n }\n ctx->high.low[h] = (ngx_http_geo_range_t *) a;\n }\n i = a->nelts;\n range = a->elts;\n while (i) {\n i--;\n if (e < (ngx_uint_t) range[i].start) {\n continue;\n }\n if (s > (ngx_uint_t) range[i].end) {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 2], &range[i + 1],\n (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 1].start = (u_short) s;\n range[i + 1].end = (u_short) e;\n range[i + 1].value = ctx->value;\n goto next;\n }\n if (s == (ngx_uint_t) range[i].start\n && e == (ngx_uint_t) range[i].end)\n {\n ngx_conf_log_error(NGX_LOG_WARN, cf, 0,\n "duplicate range \\"%V\\", value: \\"%v\\", old value: \\"%v\\"",\n ctx->net, ctx->value, range[i].value);\n range[i].value = ctx->value;\n goto next;\n }\n if (s > (ngx_uint_t) range[i].start\n && e < (ngx_uint_t) range[i].end)\n {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 3], &range[i + 1],\n (a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 2].start = (u_short) (e + 1);\n range[i + 2].end = range[i].end;\n range[i + 2].value = range[i].value;\n range[i + 1].start = (u_short) s;\n range[i + 1].end = (u_short) e;\n range[i + 1].value = ctx->value;\n range[i].end = (u_short) (s - 1);\n goto next;\n }\n if (s == (ngx_uint_t) range[i].start\n && e < (ngx_uint_t) range[i].end)\n {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 1], &range[i],\n (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 1].start = (u_short) (e + 1);\n range[i].start = (u_short) s;\n range[i].end = (u_short) e;\n range[i].value = ctx->value;\n goto next;\n }\n if (s > (ngx_uint_t) range[i].start\n && e == (ngx_uint_t) range[i].end)\n {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 2], &range[i + 1],\n (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 1].start = (u_short) s;\n range[i + 1].end = (u_short) e;\n range[i + 1].value = ctx->value;\n range[i].end = (u_short) (s - 1);\n goto next;\n }\n s = (ngx_uint_t) range[i].start;\n e = (ngx_uint_t) range[i].end;\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "range \\"%V\\" overlaps \\"%d.%d.%d.%d-%d.%d.%d.%d\\"",\n ctx->net,\n h >> 8, h & 0xff, s >> 8, s & 0xff,\n h >> 8, h & 0xff, e >> 8, e & 0xff);\n return NGX_CONF_ERROR;\n }\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range->start = (u_short) s;\n range->end = (u_short) e;\n range->value = ctx->value;\n next:\n continue;\n }\n return NGX_CONF_OK;\n}', 'ngx_array_t *\nngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)\n{\n ngx_array_t *a;\n a = ngx_palloc(p, sizeof(ngx_array_t));\n if (a == NULL) {\n return NULL;\n }\n if (ngx_array_init(a, p, n, size) != NGX_OK) {\n return NULL;\n }\n return a;\n}', 'static ngx_inline ngx_int_t\nngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)\n{\n array->nelts = 0;\n array->size = size;\n array->nalloc = n;\n array->pool = pool;\n array->elts = ngx_palloc(pool, n * size);\n if (array->elts == NULL) {\n return NGX_ERROR;\n }\n return NGX_OK;\n}']
|
2,862
| 0
|
https://github.com/openssl/openssl/blob/6438632420cee9821409221ef6717edc5ee408c1/ssl/s3_enc.c/#L430
|
size_t ssl3_final_finish_mac(SSL *s, const char *sender, size_t len,
unsigned char *p)
{
int ret;
EVP_MD_CTX *ctx = NULL;
if (!ssl3_digest_cached_records(s, 0))
return 0;
if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) {
SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, SSL_R_NO_REQUIRED_DIGEST);
return 0;
}
ctx = EVP_MD_CTX_new();
if (ctx == NULL) {
SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst)) {
SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);
return 0;
}
ret = EVP_MD_CTX_size(ctx);
if (ret < 0) {
EVP_MD_CTX_reset(ctx);
return 0;
}
if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0)
|| EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET,
(int)s->session->master_key_length,
s->session->master_key) <= 0
|| EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {
SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);
ret = 0;
}
EVP_MD_CTX_free(ctx);
return ret;
}
|
['size_t ssl3_final_finish_mac(SSL *s, const char *sender, size_t len,\n unsigned char *p)\n{\n int ret;\n EVP_MD_CTX *ctx = NULL;\n if (!ssl3_digest_cached_records(s, 0))\n return 0;\n if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, SSL_R_NO_REQUIRED_DIGEST);\n return 0;\n }\n ctx = EVP_MD_CTX_new();\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (!EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst)) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n ret = EVP_MD_CTX_size(ctx);\n if (ret < 0) {\n EVP_MD_CTX_reset(ctx);\n return 0;\n }\n if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0)\n || EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET,\n (int)s->session->master_key_length,\n s->session->master_key) <= 0\n || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);\n ret = 0;\n }\n EVP_MD_CTX_free(ctx);\n return ret;\n}', 'int ssl3_digest_cached_records(SSL *s, int keep)\n{\n const EVP_MD *md;\n long hdatalen;\n void *hdata;\n if (s->s3->handshake_dgst == NULL) {\n hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);\n if (hdatalen <= 0) {\n SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS,\n SSL_R_BAD_HANDSHAKE_LENGTH);\n return 0;\n }\n s->s3->handshake_dgst = EVP_MD_CTX_new();\n if (s->s3->handshake_dgst == NULL) {\n SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n md = ssl_handshake_md(s);\n if (md == NULL || !EVP_DigestInit_ex(s->s3->handshake_dgst, md, NULL)\n || !EVP_DigestUpdate(s->s3->handshake_dgst, hdata, hdatalen)) {\n SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (keep == 0) {\n BIO_free(s->s3->handshake_buffer);\n s->s3->handshake_buffer = NULL;\n }\n return 1;\n}', 'const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)\n{\n if (!ctx)\n return NULL;\n return ctx->digest;\n}', 'int EVP_MD_type(const EVP_MD *md)\n{\n return md->type;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)\n{\n unsigned char *tmp_buf;\n if ((in == NULL) || (in->digest == NULL)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);\n return 0;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (in->engine && !ENGINE_init(in->engine)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);\n return 0;\n }\n#endif\n if (out->digest == in->digest) {\n tmp_buf = out->md_data;\n EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);\n } else\n tmp_buf = NULL;\n EVP_MD_CTX_reset(out);\n memcpy(out, in, sizeof(*out));\n out->md_data = NULL;\n out->pctx = NULL;\n if (in->md_data && out->digest->ctx_size) {\n if (tmp_buf)\n out->md_data = tmp_buf;\n else {\n out->md_data = OPENSSL_malloc(out->digest->ctx_size);\n if (out->md_data == NULL) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n memcpy(out->md_data, in->md_data, out->digest->ctx_size);\n }\n out->update = in->update;\n if (in->pctx) {\n out->pctx = EVP_PKEY_CTX_dup(in->pctx);\n if (!out->pctx) {\n EVP_MD_CTX_reset(out);\n return 0;\n }\n }\n if (out->digest->copy)\n return out->digest->copy(out, in);\n return 1;\n}', 'int ENGINE_init(ENGINE *e)\n{\n int ret;\n if (e == NULL) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n CRYPTO_THREAD_write_lock(global_engine_lock);\n ret = engine_unlocked_init(e);\n CRYPTO_THREAD_unlock(global_engine_lock);\n return ret;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}']
|
2,863
| 0
|
https://github.com/nginx/nginx/blob/58e26b88b774bbffbcd54bfc2de57a1b902ed4dd/src/http/ngx_http_core_module.c/#L1804
|
void
ngx_http_set_exten(ngx_http_request_t *r)
{
ngx_int_t i;
ngx_str_null(&r->exten);
for (i = r->uri.len - 1; i > 1; i--) {
if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
r->exten.len = r->uri.len - i - 1;
r->exten.data = &r->uri.data[i + 1];
return;
} else if (r->uri.data[i] == '/') {
return;
}
}
return;
}
|
['static void\nngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ssize_t n;\n ngx_int_t rc;\n ngx_connection_t *c;\n c = u->peer.connection;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream process header");\n c->log->action = "reading response header from upstream";\n if (c->read->timedout) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);\n return;\n }\n if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n if (u->buffer.start == NULL) {\n u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size);\n if (u->buffer.start == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->buffer.pos = u->buffer.start;\n u->buffer.last = u->buffer.start;\n u->buffer.end = u->buffer.start + u->conf->buffer_size;\n u->buffer.temporary = 1;\n u->buffer.tag = u->output.tag;\n if (ngx_list_init(&u->headers_in.headers, r->pool, 8,\n sizeof(ngx_table_elt_t))\n != NGX_OK)\n {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache) {\n u->buffer.pos += r->cache->header_start;\n u->buffer.last = u->buffer.pos;\n }\n#endif\n }\n for ( ;; ) {\n n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);\n if (n == NGX_AGAIN) {\n#if 0\n ngx_add_timer(rev, u->read_timeout);\n#endif\n if (ngx_handle_read_event(c->read, 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n return;\n }\n if (n == 0) {\n ngx_log_error(NGX_LOG_ERR, c->log, 0,\n "upstream prematurely closed connection");\n }\n if (n == NGX_ERROR || n == 0) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n u->buffer.last += n;\n#if 0\n u->valid_header_in = 0;\n u->peer.cached = 0;\n#endif\n rc = u->process_header(r);\n if (rc == NGX_AGAIN) {\n if (u->buffer.last == u->buffer.end) {\n ngx_log_error(NGX_LOG_ERR, c->log, 0,\n "upstream sent too big header");\n ngx_http_upstream_next(r, u,\n NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);\n return;\n }\n continue;\n }\n break;\n }\n if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);\n return;\n }\n if (rc == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n if (u->headers_in.status_n >= NGX_HTTP_SPECIAL_RESPONSE) {\n if (ngx_http_upstream_test_next(r, u) == NGX_OK) {\n return;\n }\n if (ngx_http_upstream_intercept_errors(r, u) == NGX_OK) {\n return;\n }\n }\n if (ngx_http_upstream_process_headers(r, u) != NGX_OK) {\n return;\n }\n if (!r->subrequest_in_memory) {\n ngx_http_upstream_send_response(r, u);\n return;\n }\n if (u->input_filter == NULL) {\n u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;\n u->input_filter = ngx_http_upstream_non_buffered_filter;\n u->input_filter_ctx = r;\n }\n if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, NGX_ERROR);\n return;\n }\n n = u->buffer.last - u->buffer.pos;\n if (n) {\n u->buffer.last = u->buffer.pos;\n u->state->response_length += n;\n if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, NGX_ERROR);\n return;\n }\n }\n if (u->length == 0) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n u->read_event_handler = ngx_http_upstream_process_body_in_memory;\n ngx_http_upstream_process_body_in_memory(r, u);\n}', 'static ngx_int_t\nngx_http_upstream_process_headers(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ngx_str_t uri, args;\n ngx_uint_t i, flags;\n ngx_list_part_t *part;\n ngx_table_elt_t *h;\n ngx_http_upstream_header_t *hh;\n ngx_http_upstream_main_conf_t *umcf;\n umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);\n if (u->headers_in.x_accel_redirect\n && !(u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT))\n {\n ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);\n part = &u->headers_in.headers.part;\n h = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n h = part->elts;\n i = 0;\n }\n hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,\n h[i].lowcase_key, h[i].key.len);\n if (hh && hh->redirect) {\n if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {\n ngx_http_finalize_request(r,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n }\n }\n uri = u->headers_in.x_accel_redirect->value;\n ngx_str_null(&args);\n flags = NGX_HTTP_LOG_UNSAFE;\n if (ngx_http_parse_unsafe_uri(r, &uri, &args, &flags) != NGX_OK) {\n ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);\n return NGX_DONE;\n }\n if (r->method != NGX_HTTP_HEAD) {\n r->method = NGX_HTTP_GET;\n }\n ngx_http_internal_redirect(r, &uri, &args);\n ngx_http_finalize_request(r, NGX_DONE);\n return NGX_DONE;\n }\n part = &u->headers_in.headers.part;\n h = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n h = part->elts;\n i = 0;\n }\n if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,\n h[i].lowcase_key, h[i].key.len))\n {\n continue;\n }\n hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,\n h[i].lowcase_key, h[i].key.len);\n if (hh) {\n if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n continue;\n }\n if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n }\n if (r->headers_out.server && r->headers_out.server->value.data == NULL) {\n r->headers_out.server->hash = 0;\n }\n if (r->headers_out.date && r->headers_out.date->value.data == NULL) {\n r->headers_out.date->hash = 0;\n }\n r->headers_out.status = u->headers_in.status_n;\n r->headers_out.status_line = u->headers_in.status_line;\n r->headers_out.content_length_n = u->headers_in.content_length_n;\n u->length = -1;\n return NGX_OK;\n}', 'static void\nngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,\n ngx_http_upstream_t *u)\n{\n size_t size;\n ssize_t n;\n ngx_buf_t *b;\n ngx_event_t *rev;\n ngx_connection_t *c;\n c = u->peer.connection;\n rev = c->read;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream process body on memory");\n if (rev->timedout) {\n ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");\n ngx_http_upstream_finalize_request(r, u, NGX_HTTP_GATEWAY_TIME_OUT);\n return;\n }\n b = &u->buffer;\n for ( ;; ) {\n size = b->end - b->last;\n if (size == 0) {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "upstream buffer is too small to read response");\n ngx_http_upstream_finalize_request(r, u, NGX_ERROR);\n return;\n }\n n = c->recv(c, b->last, size);\n if (n == NGX_AGAIN) {\n break;\n }\n if (n == 0 || n == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, n);\n return;\n }\n u->state->response_length += n;\n if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, NGX_ERROR);\n return;\n }\n if (!rev->ready) {\n break;\n }\n }\n if (u->length == 0) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n if (ngx_handle_read_event(rev, 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u, NGX_ERROR);\n return;\n }\n if (rev->active) {\n ngx_add_timer(rev, u->conf->read_timeout);\n } else if (rev->timer_set) {\n ngx_del_timer(rev);\n }\n}', 'static void\nngx_http_upstream_finalize_request(ngx_http_request_t *r,\n ngx_http_upstream_t *u, ngx_int_t rc)\n{\n ngx_uint_t flush;\n ngx_time_t *tp;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "finalize http upstream request: %i", rc);\n if (u->cleanup) {\n *u->cleanup = NULL;\n u->cleanup = NULL;\n }\n if (u->resolved && u->resolved->ctx) {\n ngx_resolve_name_done(u->resolved->ctx);\n u->resolved->ctx = NULL;\n }\n if (u->state && u->state->response_sec) {\n tp = ngx_timeofday();\n u->state->response_sec = tp->sec - u->state->response_sec;\n u->state->response_msec = tp->msec - u->state->response_msec;\n if (u->pipe && u->pipe->read_length) {\n u->state->response_length = u->pipe->read_length;\n }\n }\n u->finalize_request(r, rc);\n if (u->peer.free && u->peer.sockaddr) {\n u->peer.free(&u->peer, u->peer.data, 0);\n u->peer.sockaddr = NULL;\n }\n if (u->peer.connection) {\n#if (NGX_HTTP_SSL)\n if (u->peer.connection->ssl) {\n u->peer.connection->ssl->no_wait_shutdown = 1;\n (void) ngx_ssl_shutdown(u->peer.connection);\n }\n#endif\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "close http upstream connection: %d",\n u->peer.connection->fd);\n if (u->peer.connection->pool) {\n ngx_destroy_pool(u->peer.connection->pool);\n }\n ngx_close_connection(u->peer.connection);\n }\n u->peer.connection = NULL;\n if (u->pipe && u->pipe->temp_file) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream temp fd: %d",\n u->pipe->temp_file->file.fd);\n }\n if (u->store && u->pipe && u->pipe->temp_file\n && u->pipe->temp_file->file.fd != NGX_INVALID_FILE)\n {\n if (ngx_delete_file(u->pipe->temp_file->file.name.data)\n == NGX_FILE_ERROR)\n {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,\n ngx_delete_file_n " \\"%s\\" failed",\n u->pipe->temp_file->file.name.data);\n }\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache) {\n if (u->cacheable) {\n if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) {\n time_t valid;\n valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc);\n if (valid) {\n r->cache->valid_sec = ngx_time() + valid;\n r->cache->error = rc;\n }\n }\n }\n ngx_http_file_cache_free(r->cache, u->pipe->temp_file);\n }\n#endif\n if (r->subrequest_in_memory\n && u->headers_in.status_n >= NGX_HTTP_SPECIAL_RESPONSE)\n {\n u->buffer.last = u->buffer.pos;\n }\n if (rc == NGX_DECLINED) {\n return;\n }\n r->connection->log->action = "sending to client";\n if (!u->header_sent\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST)\n {\n ngx_http_finalize_request(r, rc);\n return;\n }\n flush = 0;\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {\n rc = NGX_ERROR;\n flush = 1;\n }\n if (r->header_only) {\n ngx_http_finalize_request(r, rc);\n return;\n }\n if (rc == 0) {\n rc = ngx_http_send_special(r, NGX_HTTP_LAST);\n } else if (flush) {\n r->keepalive = 0;\n rc = ngx_http_send_special(r, NGX_HTTP_FLUSH);\n }\n ngx_http_finalize_request(r, rc);\n}', 'void\nngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)\n{\n ngx_connection_t *c;\n ngx_http_request_t *pr;\n ngx_http_core_loc_conf_t *clcf;\n c = r->connection;\n ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize request: %d, \\"%V?%V\\" a:%d, c:%d",\n rc, &r->uri, &r->args, r == c->data, r->main->count);\n if (rc == NGX_DONE) {\n ngx_http_finalize_connection(r);\n return;\n }\n if (rc == NGX_OK && r->filter_finalize) {\n c->error = 1;\n }\n if (rc == NGX_DECLINED) {\n r->content_handler = NULL;\n r->write_event_handler = ngx_http_core_run_phases;\n ngx_http_core_run_phases(r);\n return;\n }\n if (r != r->main && r->post_subrequest) {\n rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);\n }\n if (rc == NGX_ERROR\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST\n || c->error)\n {\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (r->main->blocked) {\n r->write_event_handler = ngx_http_request_finalizer;\n }\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE\n || rc == NGX_HTTP_CREATED\n || rc == NGX_HTTP_NO_CONTENT)\n {\n if (rc == NGX_HTTP_CLOSE) {\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (r == r->main) {\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n }\n c->read->handler = ngx_http_request_handler;\n c->write->handler = ngx_http_request_handler;\n ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));\n return;\n }\n if (r != r->main) {\n if (r->buffered || r->postponed) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n pr = r->parent;\n if (r == c->data) {\n r->main->count--;\n r->main->subrequests++;\n if (!r->logged) {\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->log_subrequest) {\n ngx_http_log_request(r);\n }\n r->logged = 1;\n } else {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "subrequest: \\"%V?%V\\" logged again",\n &r->uri, &r->args);\n }\n r->done = 1;\n if (pr->postponed && pr->postponed->request == r) {\n pr->postponed = pr->postponed->next;\n }\n c->data = pr;\n } else {\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n r->write_event_handler = ngx_http_request_finalizer;\n if (r->waited) {\n r->done = 1;\n }\n }\n if (ngx_http_post_request(pr, NULL) != NGX_OK) {\n r->main->count++;\n ngx_http_terminate_request(r, 0);\n return;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http wake parent request: \\"%V?%V\\"",\n &pr->uri, &pr->args);\n return;\n }\n if (r->buffered || c->buffered || r->postponed || r->blocked) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n if (r != c->data) {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n return;\n }\n r->done = 1;\n r->write_event_handler = ngx_http_request_empty_handler;\n if (!r->post_action) {\n r->request_complete = 1;\n }\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n c->write->delayed = 0;\n ngx_del_timer(c->write);\n }\n if (c->read->eof) {\n ngx_http_close_request(r, 0);\n return;\n }\n ngx_http_finalize_connection(r);\n}', 'ngx_int_t\nngx_http_special_response_handler(ngx_http_request_t *r, ngx_int_t error)\n{\n ngx_uint_t i, err;\n ngx_http_err_page_t *err_page;\n ngx_http_core_loc_conf_t *clcf;\n ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http special response: %i, \\"%V?%V\\"",\n error, &r->uri, &r->args);\n r->err_status = error;\n if (r->keepalive) {\n switch (error) {\n case NGX_HTTP_BAD_REQUEST:\n case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE:\n case NGX_HTTP_REQUEST_URI_TOO_LARGE:\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n case NGX_HTTP_INTERNAL_SERVER_ERROR:\n case NGX_HTTP_NOT_IMPLEMENTED:\n r->keepalive = 0;\n }\n }\n if (r->lingering_close) {\n switch (error) {\n case NGX_HTTP_BAD_REQUEST:\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n r->lingering_close = 0;\n }\n }\n r->headers_out.content_type.len = 0;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (!r->error_page && clcf->error_pages && r->uri_changes != 0) {\n if (clcf->recursive_error_pages == 0) {\n r->error_page = 1;\n }\n err_page = clcf->error_pages->elts;\n for (i = 0; i < clcf->error_pages->nelts; i++) {\n if (err_page[i].status == error) {\n return ngx_http_send_error_page(r, &err_page[i]);\n }\n }\n }\n r->expect_tested = 1;\n if (ngx_http_discard_request_body(r) != NGX_OK) {\n r->keepalive = 0;\n }\n if (clcf->msie_refresh\n && r->headers_in.msie\n && (error == NGX_HTTP_MOVED_PERMANENTLY\n || error == NGX_HTTP_MOVED_TEMPORARILY))\n {\n return ngx_http_send_refresh(r);\n }\n if (error == NGX_HTTP_CREATED) {\n err = 0;\n } else if (error == NGX_HTTP_NO_CONTENT) {\n err = 0;\n } else if (error >= NGX_HTTP_MOVED_PERMANENTLY\n && error < NGX_HTTP_LAST_3XX)\n {\n err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX;\n } else if (error >= NGX_HTTP_BAD_REQUEST\n && error < NGX_HTTP_LAST_4XX)\n {\n err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_OFF_4XX;\n } else if (error >= NGX_HTTP_NGINX_CODES\n && error < NGX_HTTP_LAST_5XX)\n {\n err = error - NGX_HTTP_NGINX_CODES + NGX_HTTP_OFF_5XX;\n switch (error) {\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n case NGX_HTTP_REQUEST_HEADER_TOO_LARGE:\n r->err_status = NGX_HTTP_BAD_REQUEST;\n break;\n }\n } else {\n err = 0;\n }\n return ngx_http_send_special_response(r, clcf, err);\n}', 'static ngx_int_t\nngx_http_send_error_page(ngx_http_request_t *r, ngx_http_err_page_t *err_page)\n{\n ngx_int_t overwrite;\n ngx_str_t uri, args;\n ngx_table_elt_t *location;\n ngx_http_core_loc_conf_t *clcf;\n overwrite = err_page->overwrite;\n if (overwrite && overwrite != NGX_HTTP_OK) {\n r->expect_tested = 1;\n }\n if (overwrite >= 0) {\n r->err_status = overwrite;\n }\n if (ngx_http_complex_value(r, &err_page->value, &uri) != NGX_OK) {\n return NGX_ERROR;\n }\n if (uri.data[0] == \'/\') {\n if (err_page->value.lengths) {\n ngx_http_split_args(r, &uri, &args);\n } else {\n args = err_page->args;\n }\n if (r->method != NGX_HTTP_HEAD) {\n r->method = NGX_HTTP_GET;\n r->method_name = ngx_http_get_name;\n }\n return ngx_http_internal_redirect(r, &uri, &args);\n }\n if (uri.data[0] == \'@\') {\n return ngx_http_named_location(r, &uri);\n }\n location = ngx_list_push(&r->headers_out.headers);\n if (location == NULL) {\n return NGX_ERROR;\n }\n if (overwrite != NGX_HTTP_MOVED_PERMANENTLY\n && overwrite != NGX_HTTP_MOVED_TEMPORARILY\n && overwrite != NGX_HTTP_SEE_OTHER\n && overwrite != NGX_HTTP_TEMPORARY_REDIRECT)\n {\n r->err_status = NGX_HTTP_MOVED_TEMPORARILY;\n }\n location->hash = 1;\n ngx_str_set(&location->key, "Location");\n location->value = uri;\n ngx_http_clear_location(r);\n r->headers_out.location = location;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->msie_refresh && r->headers_in.msie) {\n return ngx_http_send_refresh(r);\n }\n return ngx_http_send_special_response(r, clcf, r->err_status\n - NGX_HTTP_MOVED_PERMANENTLY\n + NGX_HTTP_OFF_3XX);\n}', 'ngx_int_t\nngx_http_internal_redirect(ngx_http_request_t *r,\n ngx_str_t *uri, ngx_str_t *args)\n{\n ngx_http_core_srv_conf_t *cscf;\n r->uri_changes--;\n if (r->uri_changes == 0) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "rewrite or internal redirection cycle "\n "while internally redirecting to \\"%V\\"", uri);\n r->main->count++;\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n r->uri = *uri;\n if (args) {\n r->args = *args;\n } else {\n ngx_str_null(&r->args);\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "internal redirect: \\"%V?%V\\"", uri, &r->args);\n ngx_http_set_exten(r);\n ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);\n cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);\n r->loc_conf = cscf->ctx->loc_conf;\n ngx_http_update_location_config(r);\n#if (NGX_HTTP_CACHE)\n r->cache = NULL;\n#endif\n r->internal = 1;\n r->valid_unparsed_uri = 0;\n r->add_uri_to_alias = 0;\n r->main->count++;\n ngx_http_handler(r);\n return NGX_DONE;\n}', "void\nngx_http_set_exten(ngx_http_request_t *r)\n{\n ngx_int_t i;\n ngx_str_null(&r->exten);\n for (i = r->uri.len - 1; i > 1; i--) {\n if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {\n r->exten.len = r->uri.len - i - 1;\n r->exten.data = &r->uri.data[i + 1];\n return;\n } else if (r->uri.data[i] == '/') {\n return;\n }\n }\n return;\n}"]
|
2,864
| 0
|
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L861
|
int BN_is_odd(const BIGNUM *a)
{
return (a->top > 0) && (a->d[0] & 1);
}
|
['int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len, BN_CTX *ctx)\n{\n point_conversion_form_t form;\n int y_bit;\n BN_CTX *new_ctx = NULL;\n BIGNUM *x, *y;\n size_t field_len, enc_len;\n int ret = 0;\n if (len == 0) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n form = buf[0];\n y_bit = form & 1;\n form = form & ~1U;\n if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (form == 0) {\n if (len != 1) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n return EC_POINT_set_to_infinity(group, point);\n }\n field_len = BN_num_bytes(group->field);\n enc_len =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (len != enc_len) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_bin2bn(buf + 1, field_len, x))\n goto err;\n if (BN_ucmp(x, group->field) >= 0) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_COMPRESSED) {\n if (!EC_POINT_set_compressed_coordinates_GFp\n (group, point, x, y_bit, ctx))\n goto err;\n } else {\n if (!BN_bin2bn(buf + 1 + field_len, field_len, y))\n goto err;\n if (BN_ucmp(y, group->field) >= 0) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_HYBRID) {\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}']
|
2,865
| 0
|
https://github.com/libav/libav/blob/1f0920dbcaf7a388dcbb54fae93f38c41ad46fc7/libavcodec/h264.c/#L1247
|
static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
MpegEncContext * const s = &h->s;
const int mb_x= s->mb_x;
const int mb_y= s->mb_y;
const int mb_xy= h->mb_xy;
const int mb_type= s->current_picture.mb_type[mb_xy];
uint8_t *dest_y, *dest_cb, *dest_cr;
int linesize, uvlinesize ;
int i;
int *block_offset = &h->block_offset[0];
const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
h->list_counts[mb_xy]= h->list_count;
if (!simple && MB_FIELD) {
linesize = h->mb_linesize = s->linesize * 2;
uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
block_offset = &h->block_offset[24];
if(mb_y&1){
dest_y -= s->linesize*15;
dest_cb-= s->uvlinesize*7;
dest_cr-= s->uvlinesize*7;
}
if(FRAME_MBAFF) {
int list;
for(list=0; list<h->list_count; list++){
if(!USES_LIST(mb_type, list))
continue;
if(IS_16X16(mb_type)){
int8_t *ref = &h->ref_cache[list][scan8[0]];
fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
}else{
for(i=0; i<16; i+=4){
int ref = h->ref_cache[list][scan8[i]];
if(ref >= 0)
fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
}
}
}
}
} else {
linesize = h->mb_linesize = s->linesize;
uvlinesize = h->mb_uvlinesize = s->uvlinesize;
}
if (!simple && IS_INTRA_PCM(mb_type)) {
for (i=0; i<16; i++) {
memcpy(dest_y + i* linesize, h->mb + i*8, 16);
}
for (i=0; i<8; i++) {
memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
}
} else {
if(IS_INTRA(mb_type)){
if(h->deblocking_filter)
xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
}
if(IS_INTRA4x4(mb_type)){
if(simple || !s->encoding){
if(IS_8x8DCT(mb_type)){
if(transform_bypass){
idct_dc_add =
idct_add = s->dsp.add_pixels8;
}else{
idct_dc_add = h->h264dsp.h264_idct8_dc_add;
idct_add = h->h264dsp.h264_idct8_add;
}
for(i=0; i<16; i+=4){
uint8_t * const ptr= dest_y + block_offset[i];
const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
}else{
const int nnz = h->non_zero_count_cache[ scan8[i] ];
h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
(h->topright_samples_available<<i)&0x4000, linesize);
if(nnz){
if(nnz == 1 && h->mb[i*16])
idct_dc_add(ptr, h->mb + i*16, linesize);
else
idct_add (ptr, h->mb + i*16, linesize);
}
}
}
}else{
if(transform_bypass){
idct_dc_add =
idct_add = s->dsp.add_pixels4;
}else{
idct_dc_add = h->h264dsp.h264_idct_dc_add;
idct_add = h->h264dsp.h264_idct_add;
}
for(i=0; i<16; i++){
uint8_t * const ptr= dest_y + block_offset[i];
const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
}else{
uint8_t *topright;
int nnz, tr;
if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
const int topright_avail= (h->topright_samples_available<<i)&0x8000;
assert(mb_y || linesize <= block_offset[i]);
if(!topright_avail){
tr= ptr[3 - linesize]*0x01010101;
topright= (uint8_t*) &tr;
}else
topright= ptr + 4 - linesize;
}else
topright= NULL;
h->hpc.pred4x4[ dir ](ptr, topright, linesize);
nnz = h->non_zero_count_cache[ scan8[i] ];
if(nnz){
if(is_h264){
if(nnz == 1 && h->mb[i*16])
idct_dc_add(ptr, h->mb + i*16, linesize);
else
idct_add (ptr, h->mb + i*16, linesize);
}else
ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
}
}
}
}
}
}else{
h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
if(is_h264){
if(!transform_bypass)
h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
}else
ff_svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
}
if(h->deblocking_filter)
xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
}else if(is_h264){
hl_motion(h, dest_y, dest_cb, dest_cr,
s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
}
if(!IS_INTRA4x4(mb_type)){
if(is_h264){
if(IS_INTRA16x16(mb_type)){
if(transform_bypass){
if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
}else{
for(i=0; i<16; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
}
}
}else{
h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
}
}else if(h->cbp&15){
if(transform_bypass){
const int di = IS_8x8DCT(mb_type) ? 4 : 1;
idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
for(i=0; i<16; i+=di){
if(h->non_zero_count_cache[ scan8[i] ]){
idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
}
}
}else{
if(IS_8x8DCT(mb_type)){
h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
}else{
h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
}
}
}
}else{
for(i=0; i<16; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
uint8_t * const ptr= dest_y + block_offset[i];
ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
}
}
}
}
if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
uint8_t *dest[2] = {dest_cb, dest_cr};
if(transform_bypass){
if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
}else{
idct_add = s->dsp.add_pixels4;
for(i=16; i<16+8; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
}
}
}else{
chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
if(is_h264){
h->h264dsp.h264_idct_add8(dest, block_offset,
h->mb, uvlinesize,
h->non_zero_count_cache);
}else{
for(i=16; i<16+8; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
}
}
}
}
}
}
if(h->cbp || IS_INTRA(mb_type))
s->dsp.clear_blocks(h->mb);
}
|
['static int svq3_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n MpegEncContext *const s = avctx->priv_data;\n H264Context *const h = avctx->priv_data;\n int m, mb_type;\n if (buf_size == 0) {\n if (s->next_picture_ptr && !s->low_delay) {\n *(AVFrame *) data = *(AVFrame *) &s->next_picture;\n s->next_picture_ptr = NULL;\n *data_size = sizeof(AVFrame);\n }\n return 0;\n }\n init_get_bits (&s->gb, buf, 8*buf_size);\n s->mb_x = s->mb_y = h->mb_xy = 0;\n if (svq3_decode_slice_header(h))\n return -1;\n s->pict_type = h->slice_type;\n s->picture_number = h->slice_num;\n if (avctx->debug&FF_DEBUG_PICT_INFO){\n av_log(h->s.avctx, AV_LOG_DEBUG, "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\\n",\n av_get_pict_type_char(s->pict_type), h->halfpel_flag, h->thirdpel_flag,\n s->adaptive_quant, s->qscale, h->slice_num);\n }\n s->current_picture.pict_type = s->pict_type;\n s->current_picture.key_frame = (s->pict_type == FF_I_TYPE);\n if (s->last_picture_ptr == NULL && s->pict_type == FF_B_TYPE)\n return 0;\n if (avctx->hurry_up && s->pict_type == FF_B_TYPE)\n return 0;\n if (avctx->hurry_up >= 5)\n return 0;\n if ( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == FF_B_TYPE)\n ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != FF_I_TYPE)\n || avctx->skip_frame >= AVDISCARD_ALL)\n return 0;\n if (s->next_p_frame_damaged) {\n if (s->pict_type == FF_B_TYPE)\n return 0;\n else\n s->next_p_frame_damaged = 0;\n }\n if (ff_h264_frame_start(h) < 0)\n return -1;\n if (s->pict_type == FF_B_TYPE) {\n h->frame_num_offset = (h->slice_num - h->prev_frame_num);\n if (h->frame_num_offset < 0) {\n h->frame_num_offset += 256;\n }\n if (h->frame_num_offset == 0 || h->frame_num_offset >= h->prev_frame_num_offset) {\n av_log(h->s.avctx, AV_LOG_ERROR, "error in B-frame picture id\\n");\n return -1;\n }\n } else {\n h->prev_frame_num = h->frame_num;\n h->frame_num = h->slice_num;\n h->prev_frame_num_offset = (h->frame_num - h->prev_frame_num);\n if (h->prev_frame_num_offset < 0) {\n h->prev_frame_num_offset += 256;\n }\n }\n for (m = 0; m < 2; m++){\n int i;\n for (i = 0; i < 4; i++){\n int j;\n for (j = -1; j < 4; j++)\n h->ref_cache[m][scan8[0] + 8*i + j]= 1;\n if (i < 3)\n h->ref_cache[m][scan8[0] + 8*i + j]= PART_NOT_AVAILABLE;\n }\n }\n for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {\n for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {\n h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;\n if ( (get_bits_count(&s->gb) + 7) >= s->gb.size_in_bits &&\n ((get_bits_count(&s->gb) & 7) == 0 || show_bits(&s->gb, (-get_bits_count(&s->gb) & 7)) == 0)) {\n skip_bits(&s->gb, h->next_slice_index - get_bits_count(&s->gb));\n s->gb.size_in_bits = 8*buf_size;\n if (svq3_decode_slice_header(h))\n return -1;\n }\n mb_type = svq3_get_ue_golomb(&s->gb);\n if (s->pict_type == FF_I_TYPE) {\n mb_type += 8;\n } else if (s->pict_type == FF_B_TYPE && mb_type >= 4) {\n mb_type += 4;\n }\n if (mb_type > 33 || svq3_decode_mb(h, mb_type)) {\n av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\\n", s->mb_x, s->mb_y);\n return -1;\n }\n if (mb_type != 0) {\n ff_h264_hl_decode_mb (h);\n }\n if (s->pict_type != FF_B_TYPE && !s->low_delay) {\n s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride] =\n (s->pict_type == FF_P_TYPE && mb_type < 8) ? (mb_type - 1) : -1;\n }\n }\n ff_draw_horiz_band(s, 16*s->mb_y, 16);\n }\n MPV_frame_end(s);\n if (s->pict_type == FF_B_TYPE || s->low_delay) {\n *(AVFrame *) data = *(AVFrame *) &s->current_picture;\n } else {\n *(AVFrame *) data = *(AVFrame *) &s->last_picture;\n }\n if (s->last_picture_ptr || s->low_delay) {\n *data_size = sizeof(AVFrame);\n }\n return buf_size;\n}', 'static int svq3_decode_mb(H264Context *h, unsigned int mb_type)\n{\n int i, j, k, m, dir, mode;\n int cbp = 0;\n uint32_t vlc;\n int8_t *top, *left;\n MpegEncContext *const s = (MpegEncContext *) h;\n const int mb_xy = h->mb_xy;\n const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;\n h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;\n h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;\n h->topright_samples_available = 0xFFFF;\n if (mb_type == 0) {\n if (s->pict_type == FF_P_TYPE || s->next_picture.mb_type[mb_xy] == -1) {\n svq3_mc_dir_part(s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 0, 0);\n if (s->pict_type == FF_B_TYPE) {\n svq3_mc_dir_part(s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 1, 1);\n }\n mb_type = MB_TYPE_SKIP;\n } else {\n mb_type = FFMIN(s->next_picture.mb_type[mb_xy], 6);\n if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 0, 0) < 0)\n return -1;\n if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 1, 1) < 0)\n return -1;\n mb_type = MB_TYPE_16x16;\n }\n } else if (mb_type < 8) {\n if (h->thirdpel_flag && h->halfpel_flag == !get_bits1 (&s->gb)) {\n mode = THIRDPEL_MODE;\n } else if (h->halfpel_flag && h->thirdpel_flag == !get_bits1 (&s->gb)) {\n mode = HALFPEL_MODE;\n } else {\n mode = FULLPEL_MODE;\n }\n for (m = 0; m < 2; m++) {\n if (s->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1]+6] != -1) {\n for (i = 0; i < 4; i++) {\n *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - 1 + i*h->b_stride];\n }\n } else {\n for (i = 0; i < 4; i++) {\n *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = 0;\n }\n }\n if (s->mb_y > 0) {\n memcpy(h->mv_cache[m][scan8[0] - 1*8], s->current_picture.motion_val[m][b_xy - h->b_stride], 4*2*sizeof(int16_t));\n memset(&h->ref_cache[m][scan8[0] - 1*8], (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);\n if (s->mb_x < (s->mb_width - 1)) {\n *(uint32_t *) h->mv_cache[m][scan8[0] + 4 - 1*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - h->b_stride + 4];\n h->ref_cache[m][scan8[0] + 4 - 1*8] =\n (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride + 1]+6] == -1 ||\n h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride ] ] == -1) ? PART_NOT_AVAILABLE : 1;\n }else\n h->ref_cache[m][scan8[0] + 4 - 1*8] = PART_NOT_AVAILABLE;\n if (s->mb_x > 0) {\n *(uint32_t *) h->mv_cache[m][scan8[0] - 1 - 1*8] = *(uint32_t *) s->current_picture.motion_val[m][b_xy - h->b_stride - 1];\n h->ref_cache[m][scan8[0] - 1 - 1*8] = (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1]+3] == -1) ? PART_NOT_AVAILABLE : 1;\n }else\n h->ref_cache[m][scan8[0] - 1 - 1*8] = PART_NOT_AVAILABLE;\n }else\n memset(&h->ref_cache[m][scan8[0] - 1*8 - 1], PART_NOT_AVAILABLE, 8);\n if (s->pict_type != FF_B_TYPE)\n break;\n }\n if (s->pict_type == FF_P_TYPE) {\n if (svq3_mc_dir(h, (mb_type - 1), mode, 0, 0) < 0)\n return -1;\n } else {\n if (mb_type != 2) {\n if (svq3_mc_dir(h, 0, mode, 0, 0) < 0)\n return -1;\n } else {\n for (i = 0; i < 4; i++) {\n memset(s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));\n }\n }\n if (mb_type != 1) {\n if (svq3_mc_dir(h, 0, mode, 1, (mb_type == 3)) < 0)\n return -1;\n } else {\n for (i = 0; i < 4; i++) {\n memset(s->current_picture.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));\n }\n }\n }\n mb_type = MB_TYPE_16x16;\n } else if (mb_type == 8 || mb_type == 33) {\n memset(h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));\n if (mb_type == 8) {\n if (s->mb_x > 0) {\n for (i = 0; i < 4; i++) {\n h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1]+6-i];\n }\n if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1) {\n h->left_samples_available = 0x5F5F;\n }\n }\n if (s->mb_y > 0) {\n h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+0];\n h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+1];\n h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+2];\n h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+3];\n if (h->intra4x4_pred_mode_cache[4+8*0] == -1) {\n h->top_samples_available = 0x33FF;\n }\n }\n for (i = 0; i < 16; i+=2) {\n vlc = svq3_get_ue_golomb(&s->gb);\n if (vlc >= 25){\n av_log(h->s.avctx, AV_LOG_ERROR, "luma prediction:%d\\n", vlc);\n return -1;\n }\n left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];\n top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];\n left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];\n left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];\n if (left[1] == -1 || left[2] == -1){\n av_log(h->s.avctx, AV_LOG_ERROR, "weird prediction\\n");\n return -1;\n }\n }\n } else {\n for (i = 0; i < 4; i++) {\n memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_PRED, 4);\n }\n }\n ff_h264_write_back_intra_pred_mode(h);\n if (mb_type == 8) {\n ff_h264_check_intra4x4_pred_mode(h);\n h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;\n h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;\n } else {\n for (i = 0; i < 4; i++) {\n memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_128_PRED, 4);\n }\n h->top_samples_available = 0x33FF;\n h->left_samples_available = 0x5F5F;\n }\n mb_type = MB_TYPE_INTRA4x4;\n } else {\n dir = i_mb_type_info[mb_type - 8].pred_mode;\n dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;\n if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir)) == -1){\n av_log(h->s.avctx, AV_LOG_ERROR, "check_intra_pred_mode = -1\\n");\n return -1;\n }\n cbp = i_mb_type_info[mb_type - 8].cbp;\n mb_type = MB_TYPE_INTRA16x16;\n }\n if (!IS_INTER(mb_type) && s->pict_type != FF_I_TYPE) {\n for (i = 0; i < 4; i++) {\n memset(s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));\n }\n if (s->pict_type == FF_B_TYPE) {\n for (i = 0; i < 4; i++) {\n memset(s->current_picture.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));\n }\n }\n }\n if (!IS_INTRA4x4(mb_type)) {\n memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy], DC_PRED, 8);\n }\n if (!IS_SKIP(mb_type) || s->pict_type == FF_B_TYPE) {\n memset(h->non_zero_count_cache + 8, 0, 4*9*sizeof(uint8_t));\n s->dsp.clear_blocks(h->mb);\n }\n if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == FF_B_TYPE)) {\n if ((vlc = svq3_get_ue_golomb(&s->gb)) >= 48){\n av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\\n", vlc);\n return -1;\n }\n cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];\n }\n if (IS_INTRA16x16(mb_type) || (s->pict_type != FF_I_TYPE && s->adaptive_quant && cbp)) {\n s->qscale += svq3_get_se_golomb(&s->gb);\n if (s->qscale > 31){\n av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\\n", s->qscale);\n return -1;\n }\n }\n if (IS_INTRA16x16(mb_type)) {\n if (svq3_decode_block(&s->gb, h->mb, 0, 0)){\n av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\\n");\n return -1;\n }\n }\n if (cbp) {\n const int index = IS_INTRA16x16(mb_type) ? 1 : 0;\n const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);\n for (i = 0; i < 4; i++) {\n if ((cbp & (1 << i))) {\n for (j = 0; j < 4; j++) {\n k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);\n h->non_zero_count_cache[ scan8[k] ] = 1;\n if (svq3_decode_block(&s->gb, &h->mb[16*k], index, type)){\n av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\\n");\n return -1;\n }\n }\n }\n }\n if ((cbp & 0x30)) {\n for (i = 0; i < 2; ++i) {\n if (svq3_decode_block(&s->gb, &h->mb[16*(16 + 4*i)], 0, 3)){\n av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\\n");\n return -1;\n }\n }\n if ((cbp & 0x20)) {\n for (i = 0; i < 8; i++) {\n h->non_zero_count_cache[ scan8[16+i] ] = 1;\n if (svq3_decode_block(&s->gb, &h->mb[16*(16 + i)], 1, 1)){\n av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\\n");\n return -1;\n }\n }\n }\n }\n }\n h->cbp= cbp;\n s->current_picture.mb_type[mb_xy] = mb_type;\n if (IS_INTRA(mb_type)) {\n h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8);\n }\n return 0;\n}', 'void ff_h264_hl_decode_mb(H264Context *h){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;\n if (is_complex)\n hl_decode_mb_complex(h);\n else hl_decode_mb_simple(h);\n}', 'static void av_noinline hl_decode_mb_complex(H264Context *h){\n hl_decode_mb_internal(h, 0);\n}', 'static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){\n MpegEncContext * const s = &h->s;\n const int mb_x= s->mb_x;\n const int mb_y= s->mb_y;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n uint8_t *dest_y, *dest_cb, *dest_cr;\n int linesize, uvlinesize ;\n int i;\n int *block_offset = &h->block_offset[0];\n const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);\n const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;\n void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);\n void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);\n dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;\n dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;\n dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;\n s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);\n s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);\n h->list_counts[mb_xy]= h->list_count;\n if (!simple && MB_FIELD) {\n linesize = h->mb_linesize = s->linesize * 2;\n uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;\n block_offset = &h->block_offset[24];\n if(mb_y&1){\n dest_y -= s->linesize*15;\n dest_cb-= s->uvlinesize*7;\n dest_cr-= s->uvlinesize*7;\n }\n if(FRAME_MBAFF) {\n int list;\n for(list=0; list<h->list_count; list++){\n if(!USES_LIST(mb_type, list))\n continue;\n if(IS_16X16(mb_type)){\n int8_t *ref = &h->ref_cache[list][scan8[0]];\n fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);\n }else{\n for(i=0; i<16; i+=4){\n int ref = h->ref_cache[list][scan8[i]];\n if(ref >= 0)\n fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);\n }\n }\n }\n }\n } else {\n linesize = h->mb_linesize = s->linesize;\n uvlinesize = h->mb_uvlinesize = s->uvlinesize;\n }\n if (!simple && IS_INTRA_PCM(mb_type)) {\n for (i=0; i<16; i++) {\n memcpy(dest_y + i* linesize, h->mb + i*8, 16);\n }\n for (i=0; i<8; i++) {\n memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);\n memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);\n }\n } else {\n if(IS_INTRA(mb_type)){\n if(h->deblocking_filter)\n xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);\n if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){\n h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);\n h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);\n }\n if(IS_INTRA4x4(mb_type)){\n if(simple || !s->encoding){\n if(IS_8x8DCT(mb_type)){\n if(transform_bypass){\n idct_dc_add =\n idct_add = s->dsp.add_pixels8;\n }else{\n idct_dc_add = h->h264dsp.h264_idct8_dc_add;\n idct_add = h->h264dsp.h264_idct8_add;\n }\n for(i=0; i<16; i+=4){\n uint8_t * const ptr= dest_y + block_offset[i];\n const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];\n if(transform_bypass && h->sps.profile_idc==244 && dir<=1){\n h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);\n }else{\n const int nnz = h->non_zero_count_cache[ scan8[i] ];\n h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,\n (h->topright_samples_available<<i)&0x4000, linesize);\n if(nnz){\n if(nnz == 1 && h->mb[i*16])\n idct_dc_add(ptr, h->mb + i*16, linesize);\n else\n idct_add (ptr, h->mb + i*16, linesize);\n }\n }\n }\n }else{\n if(transform_bypass){\n idct_dc_add =\n idct_add = s->dsp.add_pixels4;\n }else{\n idct_dc_add = h->h264dsp.h264_idct_dc_add;\n idct_add = h->h264dsp.h264_idct_add;\n }\n for(i=0; i<16; i++){\n uint8_t * const ptr= dest_y + block_offset[i];\n const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];\n if(transform_bypass && h->sps.profile_idc==244 && dir<=1){\n h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);\n }else{\n uint8_t *topright;\n int nnz, tr;\n if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){\n const int topright_avail= (h->topright_samples_available<<i)&0x8000;\n assert(mb_y || linesize <= block_offset[i]);\n if(!topright_avail){\n tr= ptr[3 - linesize]*0x01010101;\n topright= (uint8_t*) &tr;\n }else\n topright= ptr + 4 - linesize;\n }else\n topright= NULL;\n h->hpc.pred4x4[ dir ](ptr, topright, linesize);\n nnz = h->non_zero_count_cache[ scan8[i] ];\n if(nnz){\n if(is_h264){\n if(nnz == 1 && h->mb[i*16])\n idct_dc_add(ptr, h->mb + i*16, linesize);\n else\n idct_add (ptr, h->mb + i*16, linesize);\n }else\n ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);\n }\n }\n }\n }\n }\n }else{\n h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);\n if(is_h264){\n if(!transform_bypass)\n h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);\n }else\n ff_svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);\n }\n if(h->deblocking_filter)\n xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);\n }else if(is_h264){\n hl_motion(h, dest_y, dest_cb, dest_cr,\n s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,\n s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,\n h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);\n }\n if(!IS_INTRA4x4(mb_type)){\n if(is_h264){\n if(IS_INTRA16x16(mb_type)){\n if(transform_bypass){\n if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){\n h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);\n }else{\n for(i=0; i<16; i++){\n if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])\n s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);\n }\n }\n }else{\n h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);\n }\n }else if(h->cbp&15){\n if(transform_bypass){\n const int di = IS_8x8DCT(mb_type) ? 4 : 1;\n idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;\n for(i=0; i<16; i+=di){\n if(h->non_zero_count_cache[ scan8[i] ]){\n idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);\n }\n }\n }else{\n if(IS_8x8DCT(mb_type)){\n h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);\n }else{\n h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);\n }\n }\n }\n }else{\n for(i=0; i<16; i++){\n if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){\n uint8_t * const ptr= dest_y + block_offset[i];\n ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);\n }\n }\n }\n }\n if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){\n uint8_t *dest[2] = {dest_cb, dest_cr};\n if(transform_bypass){\n if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){\n h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);\n h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);\n }else{\n idct_add = s->dsp.add_pixels4;\n for(i=16; i<16+8; i++){\n if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])\n idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);\n }\n }\n }else{\n chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);\n chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);\n if(is_h264){\n h->h264dsp.h264_idct_add8(dest, block_offset,\n h->mb, uvlinesize,\n h->non_zero_count_cache);\n }else{\n for(i=16; i<16+8; i++){\n if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){\n uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];\n ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);\n }\n }\n }\n }\n }\n }\n if(h->cbp || IS_INTRA(mb_type))\n s->dsp.clear_blocks(h->mb);\n}']
|
2,866
| 0
|
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_sqr.c/#L161
|
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
}
|
['int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a;\n const BIGNUM *ca;\n BN_CTX_start(ctx);\n if ((a = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (y != NULL) {\n if (x == y) {\n if (!BN_sqr(a, x, ctx))\n goto err;\n } else {\n if (!BN_mul(a, x, y, ctx))\n goto err;\n }\n ca = a;\n } else\n ca = x;\n ret = BN_div_recp(NULL, r, ca, recp, ctx);\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !tmp)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
|
2,867
| 0
|
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L270
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int file_modmul(STANZA *s)\n{\n BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;\n int st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(b = getBN(s, "B"))\n || !TEST_ptr(m = getBN(s, "M"))\n || !TEST_ptr(mod_mul = getBN(s, "ModMul"))\n || !TEST_ptr(ret = BN_new()))\n goto err;\n if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))\n || !equalBN("A * B (mod M)", mod_mul, ret))\n goto err;\n if (BN_is_odd(m)) {\n BN_MONT_CTX *mont = BN_MONT_CTX_new();\n BIGNUM *a_tmp = BN_new();\n BIGNUM *b_tmp = BN_new();\n if (mont == NULL || a_tmp == NULL || b_tmp == NULL\n || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))\n || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))\n || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))\n || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))\n || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))\n || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,\n mont, ctx))\n || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))\n || !equalBN("A * B (mod M) (mont)", mod_mul, ret))\n st = 0;\n else\n st = 1;\n BN_MONT_CTX_free(mont);\n BN_free(a_tmp);\n BN_free(b_tmp);\n if (st == 0)\n goto err;\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(m);\n BN_free(mod_mul);\n BN_free(ret);\n return st;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,868
| 0
|
https://github.com/openssl/openssl/blob/47bbaa5b607f592009ed40f5678fde21c10a873c/crypto/x509/x509_vfy.c/#L641
|
static int check_chain_extensions(X509_STORE_CTX *ctx)
{
int i, ok = 0, must_be_ca, plen = 0;
X509 *x;
int (*cb) (int xok, X509_STORE_CTX *xctx);
int proxy_path_length = 0;
int purpose;
int allow_proxy_certs;
cb = ctx->verify_cb;
must_be_ca = -1;
if (ctx->parent) {
allow_proxy_certs = 0;
purpose = X509_PURPOSE_CRL_SIGN;
} else {
allow_proxy_certs =
! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
allow_proxy_certs = 1;
purpose = ctx->param->purpose;
}
for (i = 0; i < ctx->last_untrusted; i++) {
int ret;
x = sk_X509_value(ctx->chain, i);
if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
&& (x->ex_flags & EXFLAG_CRITICAL)) {
ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
ctx->error_depth = i;
ctx->current_cert = x;
ok = cb(0, ctx);
if (!ok)
goto end;
}
if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
ctx->error_depth = i;
ctx->current_cert = x;
ok = cb(0, ctx);
if (!ok)
goto end;
}
ret = X509_check_ca(x);
switch (must_be_ca) {
case -1:
if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
&& (ret != 1) && (ret != 0)) {
ret = 0;
ctx->error = X509_V_ERR_INVALID_CA;
} else
ret = 1;
break;
case 0:
if (ret != 0) {
ret = 0;
ctx->error = X509_V_ERR_INVALID_NON_CA;
} else
ret = 1;
break;
default:
if ((ret == 0)
|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
&& (ret != 1))) {
ret = 0;
ctx->error = X509_V_ERR_INVALID_CA;
} else
ret = 1;
break;
}
if (ret == 0) {
ctx->error_depth = i;
ctx->current_cert = x;
ok = cb(0, ctx);
if (!ok)
goto end;
}
if (ctx->param->purpose > 0) {
ret = X509_check_purpose(x, purpose, must_be_ca > 0);
if ((ret == 0)
|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
&& (ret != 1))) {
ctx->error = X509_V_ERR_INVALID_PURPOSE;
ctx->error_depth = i;
ctx->current_cert = x;
ok = cb(0, ctx);
if (!ok)
goto end;
}
}
if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
&& (x->ex_pathlen != -1)
&& (plen > (x->ex_pathlen + proxy_path_length + 1))) {
ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
ctx->error_depth = i;
ctx->current_cert = x;
ok = cb(0, ctx);
if (!ok)
goto end;
}
if (!(x->ex_flags & EXFLAG_SI))
plen++;
if (x->ex_flags & EXFLAG_PROXY) {
if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
ctx->error_depth = i;
ctx->current_cert = x;
ok = cb(0, ctx);
if (!ok)
goto end;
}
proxy_path_length++;
must_be_ca = 0;
} else
must_be_ca = 1;
}
ok = 1;
end:
return ok;
}
|
['static int check_chain_extensions(X509_STORE_CTX *ctx)\n{\n int i, ok = 0, must_be_ca, plen = 0;\n X509 *x;\n int (*cb) (int xok, X509_STORE_CTX *xctx);\n int proxy_path_length = 0;\n int purpose;\n int allow_proxy_certs;\n cb = ctx->verify_cb;\n must_be_ca = -1;\n if (ctx->parent) {\n allow_proxy_certs = 0;\n purpose = X509_PURPOSE_CRL_SIGN;\n } else {\n allow_proxy_certs =\n ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);\n if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))\n allow_proxy_certs = 1;\n purpose = ctx->param->purpose;\n }\n for (i = 0; i < ctx->last_untrusted; i++) {\n int ret;\n x = sk_X509_value(ctx->chain, i);\n if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)\n && (x->ex_flags & EXFLAG_CRITICAL)) {\n ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {\n ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n ret = X509_check_ca(x);\n switch (must_be_ca) {\n case -1:\n if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n && (ret != 1) && (ret != 0)) {\n ret = 0;\n ctx->error = X509_V_ERR_INVALID_CA;\n } else\n ret = 1;\n break;\n case 0:\n if (ret != 0) {\n ret = 0;\n ctx->error = X509_V_ERR_INVALID_NON_CA;\n } else\n ret = 1;\n break;\n default:\n if ((ret == 0)\n || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n && (ret != 1))) {\n ret = 0;\n ctx->error = X509_V_ERR_INVALID_CA;\n } else\n ret = 1;\n break;\n }\n if (ret == 0) {\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n if (ctx->param->purpose > 0) {\n ret = X509_check_purpose(x, purpose, must_be_ca > 0);\n if ((ret == 0)\n || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n && (ret != 1))) {\n ctx->error = X509_V_ERR_INVALID_PURPOSE;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n }\n if ((i > 1) && !(x->ex_flags & EXFLAG_SI)\n && (x->ex_pathlen != -1)\n && (plen > (x->ex_pathlen + proxy_path_length + 1))) {\n ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n if (!(x->ex_flags & EXFLAG_SI))\n plen++;\n if (x->ex_flags & EXFLAG_PROXY) {\n if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {\n ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;\n ctx->error_depth = i;\n ctx->current_cert = x;\n ok = cb(0, ctx);\n if (!ok)\n goto end;\n }\n proxy_path_length++;\n must_be_ca = 0;\n } else\n must_be_ca = 1;\n }\n ok = 1;\n end:\n return ok;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}', 'int X509_check_ca(X509 *x)\n{\n if (!(x->ex_flags & EXFLAG_SET)) {\n CRYPTO_w_lock(CRYPTO_LOCK_X509);\n x509v3_cache_extensions(x);\n CRYPTO_w_unlock(CRYPTO_LOCK_X509);\n }\n return check_ca(x);\n}']
|
2,869
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L192
|
static void pred4x4_down_left_rv40_c(uint8_t *src, uint8_t *topright, int stride){
LOAD_TOP_EDGE
LOAD_TOP_RIGHT_EDGE
LOAD_LEFT_EDGE
LOAD_DOWN_LEFT_EDGE
src[0+0*stride]=(t0 + t2 + 2*t1 + 2 + l0 + l2 + 2*l1 + 2)>>3;
src[1+0*stride]=
src[0+1*stride]=(t1 + t3 + 2*t2 + 2 + l1 + l3 + 2*l2 + 2)>>3;
src[2+0*stride]=
src[1+1*stride]=
src[0+2*stride]=(t2 + t4 + 2*t3 + 2 + l2 + l4 + 2*l3 + 2)>>3;
src[3+0*stride]=
src[2+1*stride]=
src[1+2*stride]=
src[0+3*stride]=(t3 + t5 + 2*t4 + 2 + l3 + l5 + 2*l4 + 2)>>3;
src[3+1*stride]=
src[2+2*stride]=
src[1+3*stride]=(t4 + t6 + 2*t5 + 2 + l4 + l6 + 2*l5 + 2)>>3;
src[3+2*stride]=
src[2+3*stride]=(t5 + t7 + 2*t6 + 2 + l5 + l7 + 2*l6 + 2)>>3;
src[3+3*stride]=(t6 + t7 + 1 + l6 + l7 + 1)>>2;
}
|
['static void pred4x4_down_left_rv40_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n src[0+0*stride]=(t0 + t2 + 2*t1 + 2 + l0 + l2 + 2*l1 + 2)>>3;\n src[1+0*stride]=\n src[0+1*stride]=(t1 + t3 + 2*t2 + 2 + l1 + l3 + 2*l2 + 2)>>3;\n src[2+0*stride]=\n src[1+1*stride]=\n src[0+2*stride]=(t2 + t4 + 2*t3 + 2 + l2 + l4 + 2*l3 + 2)>>3;\n src[3+0*stride]=\n src[2+1*stride]=\n src[1+2*stride]=\n src[0+3*stride]=(t3 + t5 + 2*t4 + 2 + l3 + l5 + 2*l4 + 2)>>3;\n src[3+1*stride]=\n src[2+2*stride]=\n src[1+3*stride]=(t4 + t6 + 2*t5 + 2 + l4 + l6 + 2*l5 + 2)>>3;\n src[3+2*stride]=\n src[2+3*stride]=(t5 + t7 + 2*t6 + 2 + l5 + l7 + 2*l6 + 2)>>3;\n src[3+3*stride]=(t6 + t7 + 1 + l6 + l7 + 1)>>2;\n}']
|
2,870
| 1
|
https://github.com/openssl/openssl/blob/83e034379fa3f6f0d308ec75fbcb137e26154aec/crypto/bn/bn_rand.c/#L83
|
static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)
{
unsigned char *buf = NULL;
int b, ret = 0, bit, bytes, mask;
if (bits == 0) {
if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
goto toosmall;
BN_zero(rnd);
return 1;
}
if (bits < 0 || (bits == 1 && top > 0))
goto toosmall;
bytes = (bits + 7) / 8;
bit = (bits - 1) % 8;
mask = 0xff << (bit + 1);
buf = OPENSSL_malloc(bytes);
if (buf == NULL) {
BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
goto err;
}
b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);
if (b <= 0)
goto err;
if (flag == TESTING) {
int i;
unsigned char c;
for (i = 0; i < bytes; i++) {
if (RAND_bytes(&c, 1) <= 0)
goto err;
if (c >= 128 && i > 0)
buf[i] = buf[i - 1];
else if (c < 42)
buf[i] = 0;
else if (c < 84)
buf[i] = 255;
}
}
if (top >= 0) {
if (top) {
if (bit == 0) {
buf[0] = 1;
buf[1] |= 0x80;
} else {
buf[0] |= (3 << (bit - 1));
}
} else {
buf[0] |= (1 << bit);
}
}
buf[0] &= ~mask;
if (bottom)
buf[bytes - 1] |= 1;
if (!BN_bin2bn(buf, bytes, rnd))
goto err;
ret = 1;
err:
OPENSSL_clear_free(buf, bytes);
bn_check_top(rnd);
return ret;
toosmall:
BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
return 0;
}
|
['ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,\n const BIGNUM *in_kinv, const BIGNUM *in_r,\n EC_KEY *eckey)\n{\n int ok = 0, i;\n BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *blind = NULL;\n BIGNUM *blindm = NULL;\n const BIGNUM *order, *ckinv;\n BN_CTX *ctx = NULL;\n const EC_GROUP *group;\n ECDSA_SIG *ret;\n const BIGNUM *priv_key;\n group = EC_KEY_get0_group(eckey);\n priv_key = EC_KEY_get0_private_key(eckey);\n if (group == NULL || priv_key == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);\n return NULL;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return NULL;\n }\n ret = ECDSA_SIG_new();\n if (ret == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n ret->r = BN_new();\n ret->s = BN_new();\n if (ret->r == NULL || ret->s == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n s = ret->s;\n ctx = BN_CTX_secure_new();\n if (ctx == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n blind = BN_CTX_get(ctx);\n blindm = BN_CTX_get(ctx);\n if (blindm == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);\n goto err;\n }\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n do {\n if (in_kinv == NULL || in_r == NULL) {\n if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);\n goto err;\n }\n ckinv = kinv;\n } else {\n ckinv = in_kinv;\n if (BN_copy(ret->r, in_r) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n do {\n if (!BN_priv_rand(blind, BN_num_bits(order) - 1,\n BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n goto err;\n } while (BN_is_zero(blind));\n BN_set_flags(blind, BN_FLG_CONSTTIME);\n BN_set_flags(blindm, BN_FLG_CONSTTIME);\n BN_set_flags(tmp, BN_FLG_CONSTTIME);\n if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(blindm, blind, m, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_add_quick(s, tmp, blindm, order)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(s, s, ckinv, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (BN_mod_inverse(blind, blind, order, ctx) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(s, s, blind, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (BN_is_zero(s)) {\n if (in_kinv != NULL && in_r != NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);\n goto err;\n }\n } else\n break;\n }\n while (1);\n ok = 1;\n err:\n if (!ok) {\n ECDSA_SIG_free(ret);\n ret = NULL;\n }\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_clear_free(kinv);\n return ret;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(PRIVATE, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return ret;\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,871
| 0
|
https://github.com/openssl/openssl/blob/8ccc237720d59cdf249c2c901d19f1fec739e66e/test/sha256t.c/#L155
|
static int test_sha224_multi(void)
{
unsigned char md[SHA224_DIGEST_LENGTH];
int i, testresult = 0;
EVP_MD_CTX *evp;
static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
"aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
evp = EVP_MD_CTX_new();
if (!TEST_ptr(evp))
return 0;
if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha224(), NULL)))
goto end;
for (i = 0; i < 1000000; i += 64)
if (!TEST_true(EVP_DigestUpdate(evp, updstr,
(1000000 - i) < 64 ? 1000000 - i : 64)))
goto end;
if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
|| !TEST_mem_eq(md, sizeof(md), addenum_3, sizeof(addenum_3)))
goto end;
testresult = 1;
end:
EVP_MD_CTX_free(evp);
return testresult;
}
|
['static int test_sha224_multi(void)\n{\n unsigned char md[SHA224_DIGEST_LENGTH];\n int i, testresult = 0;\n EVP_MD_CTX *evp;\n static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"\n "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";\n evp = EVP_MD_CTX_new();\n if (!TEST_ptr(evp))\n return 0;\n if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha224(), NULL)))\n goto end;\n for (i = 0; i < 1000000; i += 64)\n if (!TEST_true(EVP_DigestUpdate(evp, updstr,\n (1000000 - i) < 64 ? 1000000 - i : 64)))\n goto end;\n if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))\n || !TEST_mem_eq(md, sizeof(md), addenum_3, sizeof(addenum_3)))\n goto end;\n testresult = 1;\n end:\n EVP_MD_CTX_free(evp);\n return testresult;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", "%s [%p] != NULL", s, p);\n return 0;\n}', 'const EVP_MD *EVP_sha224(void)\n{\n return (&sha224_md);\n}', 'int test_true(const char *file, int line, const char *s, int b)\n{\n if (b)\n return 1;\n test_fail_message(NULL, file, line, "bool", "%s [false] == true", s);\n return 0;\n}', 'static void test_fail_message(const char *prefix, const char *file, int line,\n const char *type, const char *fmt, ...)\n{\n va_list ap;\n va_start(ap, fmt);\n test_fail_message_va(prefix, file, line, type, fmt, ap);\n va_end(ap);\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,872
| 0
|
https://github.com/openssl/openssl/blob/b8f1c116a357285ccb4905cd88c83f5076bafb52/ssl/ssl_cert.c/#L843
|
static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
{
int n;
unsigned char *p;
n = i2d_X509(x, NULL);
if (n < 0 || !BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
return 0;
}
p = (unsigned char *)&(buf->data[*l]);
l2n3(n, p);
n = i2d_X509(x, &p);
if (n < 0) {
SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
return 0;
}
*l += n + 3;
return 1;
}
|
['unsigned long ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk)\n{\n unsigned char *p;\n unsigned long l = 3 + SSL_HM_HEADER_LENGTH(s);\n if (!ssl_add_cert_chain(s, cpk, &l))\n return 0;\n l -= 3 + SSL_HM_HEADER_LENGTH(s);\n p = ssl_handshake_start(s);\n l2n3(l, p);\n l += 3;\n if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, l)) {\n SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return l + SSL_HM_HEADER_LENGTH(s);\n}', 'int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)\n{\n BUF_MEM *buf = s->init_buf;\n int i, chain_count;\n X509 *x;\n STACK_OF(X509) *extra_certs;\n STACK_OF(X509) *chain = NULL;\n X509_STORE *chain_store;\n if (!BUF_MEM_grow_clean(buf, 10)) {\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);\n return 0;\n }\n if (!cpk || !cpk->x509)\n return 1;\n x = cpk->x509;\n if (cpk->chain)\n extra_certs = cpk->chain;\n else\n extra_certs = s->ctx->extra_certs;\n if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)\n chain_store = NULL;\n else if (s->cert->chain_store)\n chain_store = s->cert->chain_store;\n else\n chain_store = s->ctx->cert_store;\n if (chain_store) {\n X509_STORE_CTX* xs_ctx = X509_STORE_CTX_new();\n if (xs_ctx == NULL) {\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);\n return (0);\n }\n if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {\n X509_STORE_CTX_free(xs_ctx);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);\n return (0);\n }\n (void) X509_verify_cert(xs_ctx);\n ERR_clear_error();\n chain = X509_STORE_CTX_get0_chain(xs_ctx);\n i = ssl_security_cert_chain(s, chain, NULL, 0);\n if (i != 1) {\n#if 0\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);\n#endif\n X509_STORE_CTX_free(xs_ctx);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n return 0;\n }\n chain_count = sk_X509_num(chain);\n for (i = 0; i < chain_count; i++) {\n x = sk_X509_value(chain, i);\n if (!ssl_add_cert_to_buf(buf, l, x)) {\n X509_STORE_CTX_free(xs_ctx);\n return 0;\n }\n }\n X509_STORE_CTX_free(xs_ctx);\n } else {\n i = ssl_security_cert_chain(s, extra_certs, x, 0);\n if (i != 1) {\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n return 0;\n }\n if (!ssl_add_cert_to_buf(buf, l, x))\n return 0;\n for (i = 0; i < sk_X509_num(extra_certs); i++) {\n x = sk_X509_value(extra_certs, i);\n if (!ssl_add_cert_to_buf(buf, l, x))\n return 0;\n }\n }\n return 1;\n}', 'static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)\n{\n int n;\n unsigned char *p;\n n = i2d_X509(x, NULL);\n if (n < 0 || !BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {\n SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);\n return 0;\n }\n p = (unsigned char *)&(buf->data[*l]);\n l2n3(n, p);\n n = i2d_X509(x, &p);\n if (n < 0) {\n SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);\n return 0;\n }\n *l += n + 3;\n return 1;\n}', 'size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)\n{\n char *ret;\n size_t n;\n if (str->length >= len) {\n memset(&str->data[len], 0, str->length - len);\n str->length = len;\n return (len);\n }\n if (str->max >= len) {\n memset(&str->data[str->length], 0, len - str->length);\n str->length = len;\n return (len);\n }\n if (len > LIMIT_BEFORE_EXPANSION) {\n BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n n = (len + 3) / 3 * 4;\n if ((str->flags & BUF_MEM_FLAG_SECURE))\n ret = sec_alloc_realloc(str, n);\n else\n ret = OPENSSL_clear_realloc(str->data, str->max, n);\n if (ret == NULL) {\n BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);\n len = 0;\n } else {\n str->data = ret;\n str->max = n;\n memset(&str->data[str->length], 0, len - str->length);\n str->length = len;\n }\n return (len);\n}']
|
2,873
| 0
|
https://github.com/openssl/openssl/blob/5c98b2caf5ce545fbf77611431c7084979da8177/crypto/bn/bn_ctx.c/#L440
|
static void BN_POOL_release(BN_POOL *p, unsigned int num)
{
unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
p->used -= num;
while(num--)
{
bn_check_top(p->current->vals + offset);
if(!offset)
{
offset = BN_CTX_POOL_SIZE - 1;
p->current = p->current->prev;
}
else
offset--;
}
}
|
['int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tif ((b->A == NULL) || (b->Ai == NULL))\n\t\t{\n\t\tBNerr(BN_F_BN_BLINDING_UPDATE,BN_R_NOT_INITIALIZED);\n\t\tgoto err;\n\t\t}\n\tif (!BN_mod_mul(b->A,b->A,b->A,b->mod,ctx)) goto err;\n\tif (!BN_mod_mul(b->Ai,b->Ai,b->Ai,b->mod,ctx)) goto err;\n\tret=1;\nerr:\n\treturn(ret);\n\t}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n\tBN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint ret=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tBN_CTX_start(ctx);\n\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (a == b)\n\t\t{ if (!BN_sqr(t,a,ctx)) goto err; }\n\telse\n\t\t{ if (!BN_mul(t,a,b,ctx)) goto err; }\n\tif (!BN_nnmod(r,t,m,ctx)) goto err;\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tBIGNUM *ret;\n\tCTXDBG_ENTRY("BN_CTX_get", ctx);\n\tif(ctx->err_stack || ctx->too_many) return NULL;\n\tif((ret = BN_POOL_get(&ctx->pool)) == NULL)\n\t\t{\n\t\tctx->too_many = 1;\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\treturn NULL;\n\t\t}\n\tBN_zero(ret);\n\tctx->used++;\n\tCTXDBG_RET(ctx, ret);\n\treturn ret;\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tint sav_j =0;\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tsav_j = j;\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\n\t\t\t}\n\t\telse if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n\t{\n\tunsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n\tp->used -= num;\n\twhile(num--)\n\t\t{\n\t\tbn_check_top(p->current->vals + offset);\n\t\tif(!offset)\n\t\t\t{\n\t\t\toffset = BN_CTX_POOL_SIZE - 1;\n\t\t\tp->current = p->current->prev;\n\t\t\t}\n\t\telse\n\t\t\toffset--;\n\t\t}\n\t}']
|
2,874
| 0
|
https://github.com/openssl/openssl/blob/5ea564f154ebe8bda2a0e091a312e2058edf437f/crypto/bn/bn_lib.c/#L399
|
int BN_set_word(BIGNUM *a, BN_ULONG w)
{
bn_check_top(a);
if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
return (0);
a->neg = 0;
a->d[0] = w;
a->top = (w ? 1 : 0);
bn_check_top(a);
return (1);
}
|
['int ec_GFp_simple_group_set_curve(EC_GROUP *group,\n const BIGNUM *p, const BIGNUM *a,\n const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp_a;\n if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {\n ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n tmp_a = BN_CTX_get(ctx);\n if (tmp_a == NULL)\n goto err;\n if (!BN_copy(group->field, p))\n goto err;\n BN_set_negative(group->field, 0);\n if (!BN_nnmod(tmp_a, a, p, ctx))\n goto err;\n if (group->meth->field_encode) {\n if (!group->meth->field_encode(group, group->a, tmp_a, ctx))\n goto err;\n } else if (!BN_copy(group->a, tmp_a))\n goto err;\n if (!BN_nnmod(group->b, b, p, ctx))\n goto err;\n if (group->meth->field_encode)\n if (!group->meth->field_encode(group, group->b, group->b, ctx))\n goto err;\n if (!BN_add_word(tmp_a, 3))\n goto err;\n group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field));\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return (1);\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return (0);\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return (1);\n}']
|
2,875
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *abs_m = NULL;\n int ret;\n if (!BN_nnmod(r, a, m, ctx))\n return 0;\n if (m->neg) {\n abs_m = BN_dup(m);\n if (abs_m == NULL)\n return 0;\n abs_m->neg = 0;\n }\n ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));\n bn_check_top(r);\n BN_free(abs_m);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)\n{\n if (r != a) {\n if (BN_copy(r, a) == NULL)\n return 0;\n }\n while (n > 0) {\n int max_shift;\n max_shift = BN_num_bits(m) - BN_num_bits(r);\n if (max_shift < 0) {\n BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);\n return 0;\n }\n if (max_shift > n)\n max_shift = n;\n if (max_shift) {\n if (!BN_lshift(r, r, max_shift))\n return 0;\n n -= max_shift;\n } else {\n if (!BN_lshift1(r, r))\n return 0;\n --n;\n }\n if (BN_cmp(r, m) >= 0) {\n if (!BN_sub(r, r, m))\n return 0;\n }\n }\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,876
| 0
|
https://github.com/openssl/openssl/blob/7d461736f7bd3af3c2f266f8541034ecf6f41ed9/crypto/bn/bn_lib.c/#L322
|
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return a;
}
|
['int srp_generate_client_master_secret(SSL *s)\n{\n BIGNUM *x = NULL, *u = NULL, *K = NULL;\n int ret = -1, tmp_len = 0;\n char *passwd = NULL;\n unsigned char *tmp = NULL;\n if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0\n || (u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N))\n == NULL\n || s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((passwd = s->srp_ctx.SRP_give_srp_client_pwd_callback(s,\n s->srp_ctx.SRP_cb_arg))\n == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET,\n SSL_R_CALLBACK_FAILED);\n goto err;\n }\n if ((x = SRP_Calc_x(s->srp_ctx.s, s->srp_ctx.login, passwd)) == NULL\n || (K = SRP_Calc_client_key(s->srp_ctx.N, s->srp_ctx.B,\n s->srp_ctx.g, x,\n s->srp_ctx.a, u)) == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n tmp_len = BN_num_bytes(K);\n if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n BN_bn2bin(K, tmp);\n ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);\n err:\n BN_clear_free(K);\n BN_clear_free(x);\n if (passwd != NULL)\n OPENSSL_clear_free(passwd, strlen(passwd));\n BN_clear_free(u);\n return ret;\n}', 'int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)\n{\n BIGNUM *r;\n BN_CTX *bn_ctx;\n int ret = 0;\n if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return 0;\n if ((r = BN_new()) == NULL)\n goto err;\n if (!BN_nnmod(r, B, N, bn_ctx))\n goto err;\n ret = !BN_is_zero(r);\n err:\n BN_CTX_free(bn_ctx);\n BN_free(r);\n return ret;\n}', 'BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)\n{\n return srp_Calc_xy(A, B, N);\n}', 'static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)\n{\n unsigned char digest[SHA_DIGEST_LENGTH];\n unsigned char *tmp = NULL;\n int numN = BN_num_bytes(N);\n BIGNUM *res = NULL;\n if (x != N && BN_ucmp(x, N) >= 0)\n return NULL;\n if (y != N && BN_ucmp(y, N) >= 0)\n return NULL;\n if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)\n goto err;\n if (BN_bn2binpad(x, tmp, numN) < 0\n || BN_bn2binpad(y, tmp + numN, numN) < 0\n || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))\n goto err;\n res = BN_bin2bn(digest, sizeof(digest), NULL);\n err:\n OPENSSL_free(tmp);\n return res;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,\n const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)\n{\n BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL\n || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((tmp = BN_new()) == NULL ||\n (tmp2 = BN_new()) == NULL ||\n (tmp3 = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, g, x, N, bn_ctx))\n goto err;\n if ((k = srp_Calc_k(N, g)) == NULL)\n goto err;\n if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))\n goto err;\n if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))\n goto err;\n if (!BN_mul(tmp3, u, x, bn_ctx))\n goto err;\n if (!BN_add(tmp2, a, tmp3))\n goto err;\n K = BN_new();\n if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {\n BN_free(K);\n K = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n BN_clear_free(tmp2);\n BN_clear_free(tmp3);\n BN_free(k);\n return K;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
2,877
| 0
|
https://github.com/openssl/openssl/blob/56c7754cab3da9745e52e36b0bf998f8356fd6d5/engines/ccgost/gost_pmeth.c/#L85
|
static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
{
struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
switch (type)
{
case EVP_PKEY_CTRL_MD:
{
if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)
{
GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
return 0;
}
pctx->md = (EVP_MD *)p2;
return 1;
}
break;
case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
case EVP_PKEY_CTRL_PKCS7_DECRYPT:
case EVP_PKEY_CTRL_PKCS7_SIGN:
return 1;
case EVP_PKEY_CTRL_GOST_PARAMSET:
pctx->sign_param_nid = (int)p1;
return 1;
case EVP_PKEY_CTRL_SET_IV:
pctx->shared_ukm=OPENSSL_malloc((int)p1);
memcpy(pctx->shared_ukm,p2,(int) p1);
return 1;
}
return -2;
}
|
['static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)\n\t{\n\tstruct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);\n\tswitch (type)\n\t\t{\n\t\tcase EVP_PKEY_CTRL_MD:\n\t\t{\n\t\tif (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)\n\t\t\t{\n\t\t\tGOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);\n\t\t\treturn 0;\n\t\t\t}\n\t\tpctx->md = (EVP_MD *)p2;\n\t\treturn 1;\n\t\t}\n\t\tbreak;\n\t\tcase EVP_PKEY_CTRL_PKCS7_ENCRYPT:\n\t\tcase EVP_PKEY_CTRL_PKCS7_DECRYPT:\n\t\tcase EVP_PKEY_CTRL_PKCS7_SIGN:\n\t\t\treturn 1;\n\t\tcase EVP_PKEY_CTRL_GOST_PARAMSET:\n\t\t\tpctx->sign_param_nid = (int)p1;\n\t\t\treturn 1;\n\t\tcase EVP_PKEY_CTRL_SET_IV:\n\t\t\tpctx->shared_ukm=OPENSSL_malloc((int)p1);\n\t\t\tmemcpy(pctx->shared_ukm,p2,(int) p1);\n\t\t\treturn 1;\n\t\t}\n\treturn -2;\n\t}', 'void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)\n\t{\n\treturn ctx->data;\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}']
|
2,878
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/x509v3/v3_utl.c/#L203
|
ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
{
BIGNUM *bn = NULL;
ASN1_INTEGER *aint;
int isneg, ishex;
int ret;
if (value == NULL) {
X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
return NULL;
}
bn = BN_new();
if (bn == NULL) {
X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
return NULL;
}
if (value[0] == '-') {
value++;
isneg = 1;
} else
isneg = 0;
if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
value += 2;
ishex = 1;
} else
ishex = 0;
if (ishex)
ret = BN_hex2bn(&bn, value);
else
ret = BN_dec2bn(&bn, value);
if (!ret || value[ret]) {
BN_free(bn);
X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
return NULL;
}
if (isneg && BN_is_zero(bn))
isneg = 0;
aint = BN_to_ASN1_INTEGER(bn, NULL);
BN_free(bn);
if (!aint) {
X509V3err(X509V3_F_S2I_ASN1_INTEGER,
X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
return NULL;
}
if (isneg)
aint->type |= V_ASN1_NEG;
return aint;
}
|
["ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)\n{\n BIGNUM *bn = NULL;\n ASN1_INTEGER *aint;\n int isneg, ishex;\n int ret;\n if (value == NULL) {\n X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);\n return NULL;\n }\n bn = BN_new();\n if (bn == NULL) {\n X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n if (value[0] == '-') {\n value++;\n isneg = 1;\n } else\n isneg = 0;\n if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {\n value += 2;\n ishex = 1;\n } else\n ishex = 0;\n if (ishex)\n ret = BN_hex2bn(&bn, value);\n else\n ret = BN_dec2bn(&bn, value);\n if (!ret || value[ret]) {\n BN_free(bn);\n X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);\n return NULL;\n }\n if (isneg && BN_is_zero(bn))\n isneg = 0;\n aint = BN_to_ASN1_INTEGER(bn, NULL);\n BN_free(bn);\n if (!aint) {\n X509V3err(X509V3_F_S2I_ASN1_INTEGER,\n X509V3_R_BN_TO_ASN1_INTEGER_ERROR);\n return NULL;\n }\n if (isneg)\n aint->type |= V_ASN1_NEG;\n return aint;\n}", 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}']
|
2,879
| 0
|
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_sqr.c/#L114
|
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
}
|
['static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)\n{\n const BIGNUM *dA = EC_KEY_get0_private_key(key);\n const EC_GROUP *group = EC_KEY_get0_group(key);\n const BIGNUM *order = EC_GROUP_get0_order(group);\n ECDSA_SIG *sig = NULL;\n EC_POINT *kG = NULL;\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL;\n BIGNUM *rk = NULL;\n BIGNUM *r = NULL;\n BIGNUM *s = NULL;\n BIGNUM *x1 = NULL;\n BIGNUM *tmp = NULL;\n kG = EC_POINT_new(group);\n ctx = BN_CTX_new();\n if (kG == NULL || ctx == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n BN_CTX_start(ctx);\n k = BN_CTX_get(ctx);\n rk = BN_CTX_get(ctx);\n x1 = BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n r = BN_new();\n s = BN_new();\n if (r == NULL || s == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n for (;;) {\n if (!BN_priv_rand_range(k, order)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)\n || !EC_POINT_get_affine_coordinates_GFp(group, kG, x1, NULL,\n ctx)\n || !BN_mod_add(r, e, x1, order, ctx)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n if (BN_is_zero(r))\n continue;\n if (!BN_add(rk, r, k)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n if (BN_cmp(rk, order) == 0)\n continue;\n if (!BN_add(s, dA, BN_value_one())\n || !ec_group_do_inverse_ord(group, s, s, ctx)\n || !BN_mod_mul(tmp, dA, r, order, ctx)\n || !BN_sub(tmp, k, tmp)\n || !BN_mod_mul(s, s, tmp, order, ctx)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);\n goto done;\n }\n sig = ECDSA_SIG_new();\n if (sig == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n ECDSA_SIG_set0(sig, r, s);\n break;\n }\n done:\n if (sig == NULL) {\n BN_free(r);\n BN_free(s);\n }\n BN_CTX_free(ctx);\n EC_POINT_free(kG);\n return sig;\n}', 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg == b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,\n const BIGNUM *x, BN_CTX *ctx)\n{\n if (group->meth->field_inverse_mod_ord != NULL)\n return group->meth->field_inverse_mod_ord(group, res, x, ctx);\n else\n return ec_field_inverse_mod_ord(group, res, x, ctx);\n}', 'static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,\n const BIGNUM *x, BN_CTX *ctx)\n{\n BIGNUM *e = NULL;\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n if (group->mont_data == NULL)\n return 0;\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)\n return 0;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_word(e, 2))\n goto err;\n if (!BN_sub(e, group->order, e))\n goto err;\n if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))\n goto err;\n ret = 1;\n err:\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if (!a->neg) {\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
|
2,880
| 0
|
https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/ssl/record/ssl3_record.c/#L1180
|
int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
unsigned block_size, unsigned mac_size)
{
unsigned padding_length, good;
const unsigned overhead = 1 + mac_size;
if (overhead > rec->length)
return 0;
padding_length = rec->data[rec->length - 1];
good = constant_time_ge(rec->length, padding_length + overhead);
good &= constant_time_ge(block_size, padding_length + 1);
rec->length -= good & (padding_length + 1);
return constant_time_select_int(good, 1, -1);
}
|
['int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send)\n{\n SSL3_RECORD *rec;\n EVP_CIPHER_CTX *ds;\n unsigned long l;\n int bs, i, mac_size = 0;\n const EVP_CIPHER *enc;\n rec = inrecs;\n if (n_recs != 1)\n return 0;\n if (send) {\n ds = s->enc_write_ctx;\n if (s->enc_write_ctx == NULL)\n enc = NULL;\n else\n enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);\n } else {\n ds = s->enc_read_ctx;\n if (s->enc_read_ctx == NULL)\n enc = NULL;\n else\n enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);\n }\n if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {\n memmove(rec->data, rec->input, rec->length);\n rec->input = rec->data;\n } else {\n l = rec->length;\n bs = EVP_CIPHER_CTX_block_size(ds);\n if ((bs != 1) && send) {\n i = bs - ((int)l % bs);\n l += i;\n memset(&rec->input[rec->length], 0, i);\n rec->length += i;\n rec->input[l - 1] = (i - 1);\n }\n if (!send) {\n if (l == 0 || l % bs != 0)\n return 0;\n }\n if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)\n return -1;\n if (EVP_MD_CTX_md(s->read_hash) != NULL)\n mac_size = EVP_MD_CTX_size(s->read_hash);\n if ((bs != 1) && !send)\n return ssl3_cbc_remove_padding(rec, bs, mac_size);\n }\n return (1);\n}', 'int ssl3_cbc_remove_padding(SSL3_RECORD *rec,\n unsigned block_size, unsigned mac_size)\n{\n unsigned padding_length, good;\n const unsigned overhead = 1 + mac_size;\n if (overhead > rec->length)\n return 0;\n padding_length = rec->data[rec->length - 1];\n good = constant_time_ge(rec->length, padding_length + overhead);\n good &= constant_time_ge(block_size, padding_length + 1);\n rec->length -= good & (padding_length + 1);\n return constant_time_select_int(good, 1, -1);\n}']
|
2,881
| 0
|
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139
|
static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
bc->bits >>= n;
#else
uint64_t ret = bc->bits >> (64 - n);
bc->bits <<= n;
#endif
bc->bits_left -= n;
return ret;
}
|
['static int hq_decode_block(HQContext *c, BitstreamContext *bc, int16_t block[64],\n int qsel, int is_chroma, int is_hqa)\n{\n const int32_t *q;\n int val, pos = 1;\n memset(block, 0, 64 * sizeof(*block));\n if (!is_hqa) {\n block[0] = bitstream_read_signed(bc, 9) << 6;\n q = ff_hq_quants[qsel][is_chroma][bitstream_read(bc, 2)];\n } else {\n q = ff_hq_quants[qsel][is_chroma][bitstream_read(bc, 2)];\n block[0] = bitstream_read_signed(bc, 9) << 6;\n }\n for (;;) {\n val = bitstream_read_vlc(bc, c->hq_ac_vlc.table, 9, 2);\n if (val < 0)\n return AVERROR_INVALIDDATA;\n pos += ff_hq_ac_skips[val];\n if (pos >= 64)\n break;\n block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;\n pos++;\n }\n return 0;\n}', 'static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n)\n{\n return sign_extend(bitstream_read(bc, n), n);\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
|
2,882
| 0
|
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/bn/bn_ctx.c/#L268
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *A3, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_is_word(a, 2) || BN_is_word(a, 3))\n return 1;\n if (!BN_is_odd(a) || BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A3 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a) || !BN_sub_word(A1, 1))\n goto err;\n if (!BN_copy(A3, a) || !BN_sub_word(A3, 3))\n goto err;\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A3) || !BN_add_word(check, 2))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,883
| 0
|
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/lhash/lhash.c/#L233
|
void *lh_delete(_LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
}
|
['static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)\n{\n SSL **sslp, *ssl;\n BIO_SSL *bs;\n BIO *dbio, *bio;\n long ret = 1;\n bs = (BIO_SSL *)b->ptr;\n ssl = bs->ssl;\n if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))\n return (0);\n switch (cmd) {\n case BIO_CTRL_RESET:\n SSL_shutdown(ssl);\n if (ssl->handshake_func == ssl->method->ssl_connect)\n SSL_set_connect_state(ssl);\n else if (ssl->handshake_func == ssl->method->ssl_accept)\n SSL_set_accept_state(ssl);\n SSL_clear(ssl);\n if (b->next_bio != NULL)\n ret = BIO_ctrl(b->next_bio, cmd, num, ptr);\n else if (ssl->rbio != NULL)\n ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);\n else\n ret = 1;\n break;\n case BIO_CTRL_INFO:\n ret = 0;\n break;\n case BIO_C_SSL_MODE:\n if (num)\n SSL_set_connect_state(ssl);\n else\n SSL_set_accept_state(ssl);\n break;\n case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:\n ret = bs->renegotiate_timeout;\n if (num < 60)\n num = 5;\n bs->renegotiate_timeout = (unsigned long)num;\n bs->last_time = (unsigned long)time(NULL);\n break;\n case BIO_C_SET_SSL_RENEGOTIATE_BYTES:\n ret = bs->renegotiate_count;\n if ((long)num >= 512)\n bs->renegotiate_count = (unsigned long)num;\n break;\n case BIO_C_GET_SSL_NUM_RENEGOTIATES:\n ret = bs->num_renegotiates;\n break;\n case BIO_C_SET_SSL:\n if (ssl != NULL) {\n ssl_free(b);\n if (!ssl_new(b))\n return 0;\n }\n b->shutdown = (int)num;\n ssl = (SSL *)ptr;\n ((BIO_SSL *)b->ptr)->ssl = ssl;\n bio = SSL_get_rbio(ssl);\n if (bio != NULL) {\n if (b->next_bio != NULL)\n BIO_push(bio, b->next_bio);\n b->next_bio = bio;\n CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO);\n }\n b->init = 1;\n break;\n case BIO_C_GET_SSL:\n if (ptr != NULL) {\n sslp = (SSL **)ptr;\n *sslp = ssl;\n } else\n ret = 0;\n break;\n case BIO_CTRL_GET_CLOSE:\n ret = b->shutdown;\n break;\n case BIO_CTRL_SET_CLOSE:\n b->shutdown = (int)num;\n break;\n case BIO_CTRL_WPENDING:\n ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);\n break;\n case BIO_CTRL_PENDING:\n ret = SSL_pending(ssl);\n if (ret == 0)\n ret = BIO_pending(ssl->rbio);\n break;\n case BIO_CTRL_FLUSH:\n BIO_clear_retry_flags(b);\n ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);\n BIO_copy_next_retry(b);\n break;\n case BIO_CTRL_PUSH:\n if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) {\n SSL_set_bio(ssl, b->next_bio, b->next_bio);\n CRYPTO_add(&b->next_bio->references, 1, CRYPTO_LOCK_BIO);\n }\n break;\n case BIO_CTRL_POP:\n if (b == ptr) {\n if (ssl->rbio != ssl->wbio)\n BIO_free_all(ssl->wbio);\n if (b->next_bio != NULL)\n CRYPTO_add(&b->next_bio->references, -1, CRYPTO_LOCK_BIO);\n ssl->wbio = NULL;\n ssl->rbio = NULL;\n }\n break;\n case BIO_C_DO_STATE_MACHINE:\n BIO_clear_retry_flags(b);\n b->retry_reason = 0;\n ret = (int)SSL_do_handshake(ssl);\n switch (SSL_get_error(ssl, (int)ret)) {\n case SSL_ERROR_WANT_READ:\n BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);\n break;\n case SSL_ERROR_WANT_WRITE:\n BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);\n break;\n case SSL_ERROR_WANT_CONNECT:\n BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);\n b->retry_reason = b->next_bio->retry_reason;\n break;\n default:\n break;\n }\n break;\n case BIO_CTRL_DUP:\n dbio = (BIO *)ptr;\n if (((BIO_SSL *)dbio->ptr)->ssl != NULL)\n SSL_free(((BIO_SSL *)dbio->ptr)->ssl);\n ((BIO_SSL *)dbio->ptr)->ssl = SSL_dup(ssl);\n ((BIO_SSL *)dbio->ptr)->renegotiate_count =\n ((BIO_SSL *)b->ptr)->renegotiate_count;\n ((BIO_SSL *)dbio->ptr)->byte_count = ((BIO_SSL *)b->ptr)->byte_count;\n ((BIO_SSL *)dbio->ptr)->renegotiate_timeout =\n ((BIO_SSL *)b->ptr)->renegotiate_timeout;\n ((BIO_SSL *)dbio->ptr)->last_time = ((BIO_SSL *)b->ptr)->last_time;\n ret = (((BIO_SSL *)dbio->ptr)->ssl != NULL);\n break;\n case BIO_C_GET_FD:\n ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);\n break;\n case BIO_CTRL_SET_CALLBACK:\n {\n#if 0\n SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n ret = -1;\n#else\n ret = 0;\n#endif\n }\n break;\n case BIO_CTRL_GET_CALLBACK:\n {\n void (**fptr) (const SSL *xssl, int type, int val);\n fptr = (void (**)(const SSL *xssl, int type, int val))ptr;\n *fptr = SSL_get_info_callback(ssl);\n }\n break;\n default:\n ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);\n break;\n }\n return (ret);\n}', 'int SSL_clear(SSL *s)\n{\n if (s->method == NULL) {\n SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);\n return (0);\n }\n if (ssl_clear_bad_session(s)) {\n SSL_SESSION_free(s->session);\n s->session = NULL;\n }\n s->error = 0;\n s->hit = 0;\n s->shutdown = 0;\n#if 0\n if (s->renegotiate)\n return (1);\n#else\n if (s->renegotiate) {\n SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n#endif\n s->type = 0;\n s->state = SSL_ST_BEFORE | ((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT);\n s->version = s->method->version;\n s->client_version = s->version;\n s->rwstate = SSL_NOTHING;\n s->rstate = SSL_ST_READ_HEADER;\n#if 0\n s->read_ahead = s->ctx->read_ahead;\n#endif\n if (s->init_buf != NULL) {\n BUF_MEM_free(s->init_buf);\n s->init_buf = NULL;\n }\n ssl_clear_cipher_ctx(s);\n ssl_clear_hash_ctx(&s->read_hash);\n ssl_clear_hash_ctx(&s->write_hash);\n s->first_packet = 0;\n#if 1\n if (!s->in_handshake && (s->session == NULL)\n && (s->method != s->ctx->method)) {\n s->method->ssl_free(s);\n s->method = s->ctx->method;\n if (!s->method->ssl_new(s))\n return (0);\n } else\n#endif\n s->method->ssl_clear(s);\n return (1);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->ctx, s->session);\n return (1);\n } else\n return (0);\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n if (lck)\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'void *lh_delete(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
|
2,884
| 0
|
https://github.com/nginx/nginx/blob/030a1f959c9c673258fe53f968fab04fc9214b86/src/core/ngx_hash.c/#L391
|
ngx_int_t
ngx_hash_init(ngx_hash_init_t *hinit, ngx_hash_key_t *names, ngx_uint_t nelts)
{
u_char *elts;
size_t len;
u_short *test;
ngx_uint_t i, n, key, size, start, bucket_size;
ngx_hash_elt_t *elt, **buckets;
if (hinit->max_size == 0) {
ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,
"could not build %s, you should "
"increase %s_max_size: %i",
hinit->name, hinit->name, hinit->max_size);
return NGX_ERROR;
}
for (n = 0; n < nelts; n++) {
if (hinit->bucket_size < NGX_HASH_ELT_SIZE(&names[n]) + sizeof(void *))
{
ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,
"could not build %s, you should "
"increase %s_bucket_size: %i",
hinit->name, hinit->name, hinit->bucket_size);
return NGX_ERROR;
}
}
test = ngx_alloc(hinit->max_size * sizeof(u_short), hinit->pool->log);
if (test == NULL) {
return NGX_ERROR;
}
bucket_size = hinit->bucket_size - sizeof(void *);
start = nelts / (bucket_size / (2 * sizeof(void *)));
start = start ? start : 1;
if (hinit->max_size > 10000 && nelts && hinit->max_size / nelts < 100) {
start = hinit->max_size - 1000;
}
for (size = start; size <= hinit->max_size; size++) {
ngx_memzero(test, size * sizeof(u_short));
for (n = 0; n < nelts; n++) {
if (names[n].key.data == NULL) {
continue;
}
key = names[n].key_hash % size;
test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));
#if 0
ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,
"%ui: %ui %ui \"%V\"",
size, key, test[key], &names[n].key);
#endif
if (test[key] > (u_short) bucket_size) {
goto next;
}
}
goto found;
next:
continue;
}
size = hinit->max_size;
ngx_log_error(NGX_LOG_WARN, hinit->pool->log, 0,
"could not build optimal %s, you should increase "
"either %s_max_size: %i or %s_bucket_size: %i; "
"ignoring %s_bucket_size",
hinit->name, hinit->name, hinit->max_size,
hinit->name, hinit->bucket_size, hinit->name);
found:
for (i = 0; i < size; i++) {
test[i] = sizeof(void *);
}
for (n = 0; n < nelts; n++) {
if (names[n].key.data == NULL) {
continue;
}
key = names[n].key_hash % size;
test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));
}
len = 0;
for (i = 0; i < size; i++) {
if (test[i] == sizeof(void *)) {
continue;
}
test[i] = (u_short) (ngx_align(test[i], ngx_cacheline_size));
len += test[i];
}
if (hinit->hash == NULL) {
hinit->hash = ngx_pcalloc(hinit->pool, sizeof(ngx_hash_wildcard_t)
+ size * sizeof(ngx_hash_elt_t *));
if (hinit->hash == NULL) {
ngx_free(test);
return NGX_ERROR;
}
buckets = (ngx_hash_elt_t **)
((u_char *) hinit->hash + sizeof(ngx_hash_wildcard_t));
} else {
buckets = ngx_pcalloc(hinit->pool, size * sizeof(ngx_hash_elt_t *));
if (buckets == NULL) {
ngx_free(test);
return NGX_ERROR;
}
}
elts = ngx_palloc(hinit->pool, len + ngx_cacheline_size);
if (elts == NULL) {
ngx_free(test);
return NGX_ERROR;
}
elts = ngx_align_ptr(elts, ngx_cacheline_size);
for (i = 0; i < size; i++) {
if (test[i] == sizeof(void *)) {
continue;
}
buckets[i] = (ngx_hash_elt_t *) elts;
elts += test[i];
}
for (i = 0; i < size; i++) {
test[i] = 0;
}
for (n = 0; n < nelts; n++) {
if (names[n].key.data == NULL) {
continue;
}
key = names[n].key_hash % size;
elt = (ngx_hash_elt_t *) ((u_char *) buckets[key] + test[key]);
elt->value = names[n].value;
elt->len = (u_short) names[n].key.len;
ngx_strlow(elt->name, names[n].key.data, names[n].key.len);
test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));
}
for (i = 0; i < size; i++) {
if (buckets[i] == NULL) {
continue;
}
elt = (ngx_hash_elt_t *) ((u_char *) buckets[i] + test[i]);
elt->value = NULL;
}
ngx_free(test);
hinit->hash->buckets = buckets;
hinit->hash->size = size;
#if 0
for (i = 0; i < size; i++) {
ngx_str_t val;
ngx_uint_t key;
elt = buckets[i];
if (elt == NULL) {
ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,
"%ui: NULL", i);
continue;
}
while (elt->value) {
val.len = elt->len;
val.data = &elt->name[0];
key = hinit->key(val.data, val.len);
ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,
"%ui: %p \"%V\" %ui", i, elt, &val, key);
elt = (ngx_hash_elt_t *) ngx_align_ptr(&elt->name[0] + elt->len,
sizeof(void *));
}
}
#endif
return NGX_OK;
}
|
['static char *\nngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)\n{\n ngx_http_scgi_loc_conf_t *prev = parent;\n ngx_http_scgi_loc_conf_t *conf = child;\n size_t size;\n ngx_int_t rc;\n ngx_hash_init_t hash;\n ngx_http_core_loc_conf_t *clcf;\n#if (NGX_HTTP_CACHE)\n if (conf->upstream.store > 0) {\n conf->upstream.cache = 0;\n }\n if (conf->upstream.cache > 0) {\n conf->upstream.store = 0;\n }\n#endif\n if (conf->upstream.store == NGX_CONF_UNSET) {\n ngx_conf_merge_value(conf->upstream.store, prev->upstream.store, 0);\n conf->upstream.store_lengths = prev->upstream.store_lengths;\n conf->upstream.store_values = prev->upstream.store_values;\n }\n ngx_conf_merge_uint_value(conf->upstream.store_access,\n prev->upstream.store_access, 0600);\n ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,\n prev->upstream.next_upstream_tries, 0);\n ngx_conf_merge_value(conf->upstream.buffering,\n prev->upstream.buffering, 1);\n ngx_conf_merge_value(conf->upstream.request_buffering,\n prev->upstream.request_buffering, 1);\n ngx_conf_merge_value(conf->upstream.ignore_client_abort,\n prev->upstream.ignore_client_abort, 0);\n ngx_conf_merge_value(conf->upstream.force_ranges,\n prev->upstream.force_ranges, 0);\n ngx_conf_merge_ptr_value(conf->upstream.local,\n prev->upstream.local, NULL);\n ngx_conf_merge_msec_value(conf->upstream.connect_timeout,\n prev->upstream.connect_timeout, 60000);\n ngx_conf_merge_msec_value(conf->upstream.send_timeout,\n prev->upstream.send_timeout, 60000);\n ngx_conf_merge_msec_value(conf->upstream.read_timeout,\n prev->upstream.read_timeout, 60000);\n ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,\n prev->upstream.next_upstream_timeout, 0);\n ngx_conf_merge_size_value(conf->upstream.send_lowat,\n prev->upstream.send_lowat, 0);\n ngx_conf_merge_size_value(conf->upstream.buffer_size,\n prev->upstream.buffer_size,\n (size_t) ngx_pagesize);\n ngx_conf_merge_size_value(conf->upstream.limit_rate,\n prev->upstream.limit_rate, 0);\n ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,\n 8, ngx_pagesize);\n if (conf->upstream.bufs.num < 2) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "there must be at least 2 \\"scgi_buffers\\"");\n return NGX_CONF_ERROR;\n }\n size = conf->upstream.buffer_size;\n if (size < conf->upstream.bufs.size) {\n size = conf->upstream.bufs.size;\n }\n ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,\n prev->upstream.busy_buffers_size_conf,\n NGX_CONF_UNSET_SIZE);\n if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {\n conf->upstream.busy_buffers_size = 2 * size;\n } else {\n conf->upstream.busy_buffers_size =\n conf->upstream.busy_buffers_size_conf;\n }\n if (conf->upstream.busy_buffers_size < size) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "\\"scgi_busy_buffers_size\\" must be equal to or greater "\n "than the maximum of the value of \\"scgi_buffer_size\\" and "\n "one of the \\"scgi_buffers\\"");\n return NGX_CONF_ERROR;\n }\n if (conf->upstream.busy_buffers_size\n > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)\n {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "\\"scgi_busy_buffers_size\\" must be less than "\n "the size of all \\"scgi_buffers\\" minus one buffer");\n return NGX_CONF_ERROR;\n }\n ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,\n prev->upstream.temp_file_write_size_conf,\n NGX_CONF_UNSET_SIZE);\n if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {\n conf->upstream.temp_file_write_size = 2 * size;\n } else {\n conf->upstream.temp_file_write_size =\n conf->upstream.temp_file_write_size_conf;\n }\n if (conf->upstream.temp_file_write_size < size) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "\\"scgi_temp_file_write_size\\" must be equal to or greater than "\n "the maximum of the value of \\"scgi_buffer_size\\" and "\n "one of the \\"scgi_buffers\\"");\n return NGX_CONF_ERROR;\n }\n ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,\n prev->upstream.max_temp_file_size_conf,\n NGX_CONF_UNSET_SIZE);\n if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {\n conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;\n } else {\n conf->upstream.max_temp_file_size =\n conf->upstream.max_temp_file_size_conf;\n }\n if (conf->upstream.max_temp_file_size != 0\n && conf->upstream.max_temp_file_size < size)\n {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "\\"scgi_max_temp_file_size\\" must be equal to zero to disable "\n "temporary files usage or must be equal to or greater than "\n "the maximum of the value of \\"scgi_buffer_size\\" and "\n "one of the \\"scgi_buffers\\"");\n return NGX_CONF_ERROR;\n }\n ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,\n prev->upstream.ignore_headers,\n NGX_CONF_BITMASK_SET);\n ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,\n prev->upstream.next_upstream,\n (NGX_CONF_BITMASK_SET\n |NGX_HTTP_UPSTREAM_FT_ERROR\n |NGX_HTTP_UPSTREAM_FT_TIMEOUT));\n if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {\n conf->upstream.next_upstream = NGX_CONF_BITMASK_SET\n |NGX_HTTP_UPSTREAM_FT_OFF;\n }\n if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,\n prev->upstream.temp_path,\n &ngx_http_scgi_temp_path)\n != NGX_OK)\n {\n return NGX_CONF_ERROR;\n }\n#if (NGX_HTTP_CACHE)\n if (conf->upstream.cache == NGX_CONF_UNSET) {\n ngx_conf_merge_value(conf->upstream.cache,\n prev->upstream.cache, 0);\n conf->upstream.cache_zone = prev->upstream.cache_zone;\n conf->upstream.cache_value = prev->upstream.cache_value;\n }\n if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) {\n ngx_shm_zone_t *shm_zone;\n shm_zone = conf->upstream.cache_zone;\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "\\"scgi_cache\\" zone \\"%V\\" is unknown",\n &shm_zone->shm.name);\n return NGX_CONF_ERROR;\n }\n ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,\n prev->upstream.cache_min_uses, 1);\n ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,\n prev->upstream.cache_use_stale,\n (NGX_CONF_BITMASK_SET\n |NGX_HTTP_UPSTREAM_FT_OFF));\n if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {\n conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET\n |NGX_HTTP_UPSTREAM_FT_OFF;\n }\n if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {\n conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;\n }\n if (conf->upstream.cache_methods == 0) {\n conf->upstream.cache_methods = prev->upstream.cache_methods;\n }\n conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;\n ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,\n prev->upstream.cache_bypass, NULL);\n ngx_conf_merge_ptr_value(conf->upstream.no_cache,\n prev->upstream.no_cache, NULL);\n ngx_conf_merge_ptr_value(conf->upstream.cache_valid,\n prev->upstream.cache_valid, NULL);\n if (conf->cache_key.value.data == NULL) {\n conf->cache_key = prev->cache_key;\n }\n if (conf->upstream.cache && conf->cache_key.value.data == NULL) {\n ngx_conf_log_error(NGX_LOG_WARN, cf, 0,\n "no \\"scgi_cache_key\\" for \\"scgi_cache\\"");\n }\n ngx_conf_merge_value(conf->upstream.cache_lock,\n prev->upstream.cache_lock, 0);\n ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,\n prev->upstream.cache_lock_timeout, 5000);\n ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,\n prev->upstream.cache_lock_age, 5000);\n ngx_conf_merge_value(conf->upstream.cache_revalidate,\n prev->upstream.cache_revalidate, 0);\n#endif\n ngx_conf_merge_value(conf->upstream.pass_request_headers,\n prev->upstream.pass_request_headers, 1);\n ngx_conf_merge_value(conf->upstream.pass_request_body,\n prev->upstream.pass_request_body, 1);\n ngx_conf_merge_value(conf->upstream.intercept_errors,\n prev->upstream.intercept_errors, 0);\n hash.max_size = 512;\n hash.bucket_size = ngx_align(64, ngx_cacheline_size);\n hash.name = "scgi_hide_headers_hash";\n if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,\n &prev->upstream, ngx_http_scgi_hide_headers, &hash)\n != NGX_OK)\n {\n return NGX_CONF_ERROR;\n }\n clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);\n if (clcf->noname\n && conf->upstream.upstream == NULL && conf->scgi_lengths == NULL)\n {\n conf->upstream.upstream = prev->upstream.upstream;\n conf->scgi_lengths = prev->scgi_lengths;\n conf->scgi_values = prev->scgi_values;\n }\n if (clcf->lmt_excpt && clcf->handler == NULL\n && (conf->upstream.upstream || conf->scgi_lengths))\n {\n clcf->handler = ngx_http_scgi_handler;\n }\n if (conf->params_source == NULL) {\n conf->params = prev->params;\n#if (NGX_HTTP_CACHE)\n conf->params_cache = prev->params_cache;\n#endif\n conf->params_source = prev->params_source;\n }\n rc = ngx_http_scgi_init_params(cf, conf, &conf->params, NULL);\n if (rc != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n#if (NGX_HTTP_CACHE)\n if (conf->upstream.cache) {\n rc = ngx_http_scgi_init_params(cf, conf, &conf->params_cache,\n ngx_http_scgi_cache_headers);\n if (rc != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n }\n#endif\n return NGX_CONF_OK;\n}', 'ngx_int_t\nngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,\n ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,\n ngx_str_t *default_hide_headers, ngx_hash_init_t *hash)\n{\n ngx_str_t *h;\n ngx_uint_t i, j;\n ngx_array_t hide_headers;\n ngx_hash_key_t *hk;\n if (conf->hide_headers == NGX_CONF_UNSET_PTR\n && conf->pass_headers == NGX_CONF_UNSET_PTR)\n {\n conf->hide_headers = prev->hide_headers;\n conf->pass_headers = prev->pass_headers;\n conf->hide_headers_hash = prev->hide_headers_hash;\n if (conf->hide_headers_hash.buckets\n#if (NGX_HTTP_CACHE)\n && ((conf->cache == 0) == (prev->cache == 0))\n#endif\n )\n {\n return NGX_OK;\n }\n } else {\n if (conf->hide_headers == NGX_CONF_UNSET_PTR) {\n conf->hide_headers = prev->hide_headers;\n }\n if (conf->pass_headers == NGX_CONF_UNSET_PTR) {\n conf->pass_headers = prev->pass_headers;\n }\n }\n if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n for (h = default_hide_headers; h->len; h++) {\n hk = ngx_array_push(&hide_headers);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key = *h;\n hk->key_hash = ngx_hash_key_lc(h->data, h->len);\n hk->value = (void *) 1;\n }\n if (conf->hide_headers != NGX_CONF_UNSET_PTR) {\n h = conf->hide_headers->elts;\n for (i = 0; i < conf->hide_headers->nelts; i++) {\n hk = hide_headers.elts;\n for (j = 0; j < hide_headers.nelts; j++) {\n if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {\n goto exist;\n }\n }\n hk = ngx_array_push(&hide_headers);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key = h[i];\n hk->key_hash = ngx_hash_key_lc(h[i].data, h[i].len);\n hk->value = (void *) 1;\n exist:\n continue;\n }\n }\n if (conf->pass_headers != NGX_CONF_UNSET_PTR) {\n h = conf->pass_headers->elts;\n hk = hide_headers.elts;\n for (i = 0; i < conf->pass_headers->nelts; i++) {\n for (j = 0; j < hide_headers.nelts; j++) {\n if (hk[j].key.data == NULL) {\n continue;\n }\n if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {\n hk[j].key.data = NULL;\n break;\n }\n }\n }\n }\n hash->hash = &conf->hide_headers_hash;\n hash->key = ngx_hash_key_lc;\n hash->pool = cf->pool;\n hash->temp_pool = NULL;\n return ngx_hash_init(hash, hide_headers.elts, hide_headers.nelts);\n}', 'ngx_int_t\nngx_hash_init(ngx_hash_init_t *hinit, ngx_hash_key_t *names, ngx_uint_t nelts)\n{\n u_char *elts;\n size_t len;\n u_short *test;\n ngx_uint_t i, n, key, size, start, bucket_size;\n ngx_hash_elt_t *elt, **buckets;\n if (hinit->max_size == 0) {\n ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,\n "could not build %s, you should "\n "increase %s_max_size: %i",\n hinit->name, hinit->name, hinit->max_size);\n return NGX_ERROR;\n }\n for (n = 0; n < nelts; n++) {\n if (hinit->bucket_size < NGX_HASH_ELT_SIZE(&names[n]) + sizeof(void *))\n {\n ngx_log_error(NGX_LOG_EMERG, hinit->pool->log, 0,\n "could not build %s, you should "\n "increase %s_bucket_size: %i",\n hinit->name, hinit->name, hinit->bucket_size);\n return NGX_ERROR;\n }\n }\n test = ngx_alloc(hinit->max_size * sizeof(u_short), hinit->pool->log);\n if (test == NULL) {\n return NGX_ERROR;\n }\n bucket_size = hinit->bucket_size - sizeof(void *);\n start = nelts / (bucket_size / (2 * sizeof(void *)));\n start = start ? start : 1;\n if (hinit->max_size > 10000 && nelts && hinit->max_size / nelts < 100) {\n start = hinit->max_size - 1000;\n }\n for (size = start; size <= hinit->max_size; size++) {\n ngx_memzero(test, size * sizeof(u_short));\n for (n = 0; n < nelts; n++) {\n if (names[n].key.data == NULL) {\n continue;\n }\n key = names[n].key_hash % size;\n test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));\n#if 0\n ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,\n "%ui: %ui %ui \\"%V\\"",\n size, key, test[key], &names[n].key);\n#endif\n if (test[key] > (u_short) bucket_size) {\n goto next;\n }\n }\n goto found;\n next:\n continue;\n }\n size = hinit->max_size;\n ngx_log_error(NGX_LOG_WARN, hinit->pool->log, 0,\n "could not build optimal %s, you should increase "\n "either %s_max_size: %i or %s_bucket_size: %i; "\n "ignoring %s_bucket_size",\n hinit->name, hinit->name, hinit->max_size,\n hinit->name, hinit->bucket_size, hinit->name);\nfound:\n for (i = 0; i < size; i++) {\n test[i] = sizeof(void *);\n }\n for (n = 0; n < nelts; n++) {\n if (names[n].key.data == NULL) {\n continue;\n }\n key = names[n].key_hash % size;\n test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));\n }\n len = 0;\n for (i = 0; i < size; i++) {\n if (test[i] == sizeof(void *)) {\n continue;\n }\n test[i] = (u_short) (ngx_align(test[i], ngx_cacheline_size));\n len += test[i];\n }\n if (hinit->hash == NULL) {\n hinit->hash = ngx_pcalloc(hinit->pool, sizeof(ngx_hash_wildcard_t)\n + size * sizeof(ngx_hash_elt_t *));\n if (hinit->hash == NULL) {\n ngx_free(test);\n return NGX_ERROR;\n }\n buckets = (ngx_hash_elt_t **)\n ((u_char *) hinit->hash + sizeof(ngx_hash_wildcard_t));\n } else {\n buckets = ngx_pcalloc(hinit->pool, size * sizeof(ngx_hash_elt_t *));\n if (buckets == NULL) {\n ngx_free(test);\n return NGX_ERROR;\n }\n }\n elts = ngx_palloc(hinit->pool, len + ngx_cacheline_size);\n if (elts == NULL) {\n ngx_free(test);\n return NGX_ERROR;\n }\n elts = ngx_align_ptr(elts, ngx_cacheline_size);\n for (i = 0; i < size; i++) {\n if (test[i] == sizeof(void *)) {\n continue;\n }\n buckets[i] = (ngx_hash_elt_t *) elts;\n elts += test[i];\n }\n for (i = 0; i < size; i++) {\n test[i] = 0;\n }\n for (n = 0; n < nelts; n++) {\n if (names[n].key.data == NULL) {\n continue;\n }\n key = names[n].key_hash % size;\n elt = (ngx_hash_elt_t *) ((u_char *) buckets[key] + test[key]);\n elt->value = names[n].value;\n elt->len = (u_short) names[n].key.len;\n ngx_strlow(elt->name, names[n].key.data, names[n].key.len);\n test[key] = (u_short) (test[key] + NGX_HASH_ELT_SIZE(&names[n]));\n }\n for (i = 0; i < size; i++) {\n if (buckets[i] == NULL) {\n continue;\n }\n elt = (ngx_hash_elt_t *) ((u_char *) buckets[i] + test[i]);\n elt->value = NULL;\n }\n ngx_free(test);\n hinit->hash->buckets = buckets;\n hinit->hash->size = size;\n#if 0\n for (i = 0; i < size; i++) {\n ngx_str_t val;\n ngx_uint_t key;\n elt = buckets[i];\n if (elt == NULL) {\n ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,\n "%ui: NULL", i);\n continue;\n }\n while (elt->value) {\n val.len = elt->len;\n val.data = &elt->name[0];\n key = hinit->key(val.data, val.len);\n ngx_log_error(NGX_LOG_ALERT, hinit->pool->log, 0,\n "%ui: %p \\"%V\\" %ui", i, elt, &val, key);\n elt = (ngx_hash_elt_t *) ngx_align_ptr(&elt->name[0] + elt->len,\n sizeof(void *));\n }\n }\n#endif\n return NGX_OK;\n}', 'void *\nngx_pcalloc(ngx_pool_t *pool, size_t size)\n{\n void *p;\n p = ngx_palloc(pool, size);\n if (p) {\n ngx_memzero(p, size);\n }\n return p;\n}', 'void *\nngx_palloc(ngx_pool_t *pool, size_t size)\n{\n#if !(NGX_DEBUG_PALLOC)\n if (size <= pool->max) {\n return ngx_palloc_small(pool, size, 1);\n }\n#endif\n return ngx_palloc_large(pool, size);\n}', 'static ngx_inline void *\nngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)\n{\n u_char *m;\n ngx_pool_t *p;\n p = pool->current;\n do {\n m = p->d.last;\n if (align) {\n m = ngx_align_ptr(m, NGX_ALIGNMENT);\n }\n if ((size_t) (p->d.end - m) >= size) {\n p->d.last = m + size;\n return m;\n }\n p = p->d.next;\n } while (p);\n return ngx_palloc_block(pool, size);\n}']
|
2,885
| 0
|
https://github.com/openssl/openssl/blob/d858c87653257185ead1c5baf3d84cd7276dd912/ssl/packet_locl.h/#L84
|
static ossl_inline void packet_forward(PACKET *pkt, size_t len)
{
pkt->curr += len;
pkt->remaining -= len;
}
|
['static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])\n{\n const unsigned char *bytes;\n PACKET pkt;\n if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)\n || !PACKET_get_bytes(&pkt, &bytes, 4)\n || bytes[0] != 2 || bytes[1] != 4\n || bytes[2] != 6 || bytes[3] != 8\n || PACKET_remaining(&pkt) != BUF_LEN -4\n || !PACKET_forward(&pkt, BUF_LEN - 8)\n || !PACKET_get_bytes(&pkt, &bytes, 4)\n || bytes[0] != 0xf8 || bytes[1] != 0xfa\n || bytes[2] != 0xfc || bytes[3] != 0xfe\n || PACKET_remaining(&pkt)) {\n fprintf(stderr, "test_PACKET_get_bytes() failed\\n");\n return 0;\n }\n return 1;\n}', 'static ossl_inline void packet_forward(PACKET *pkt, size_t len)\n{\n pkt->curr += len;\n pkt->remaining -= len;\n}']
|
2,886
| 0
|
https://github.com/openssl/openssl/blob/a68d8c7b77a3d46d591b89cfd0ecd2a2242e4613/crypto/ec/ec_mult.c/#L339
|
int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
BN_CTX *ctx)
{
BN_CTX *new_ctx = NULL;
const EC_POINT *generator = NULL;
EC_POINT *tmp = NULL;
size_t totalnum;
size_t blocksize = 0, numblocks = 0;
size_t pre_points_per_block = 0;
size_t i, j;
int k;
int r_is_inverted = 0;
int r_is_at_infinity = 1;
size_t *wsize = NULL;
signed char **wNAF = NULL;
size_t *wNAF_len = NULL;
size_t max_len = 0;
size_t num_val;
EC_POINT **val = NULL;
EC_POINT **v;
EC_POINT ***val_sub = NULL;
const EC_PRE_COMP *pre_comp = NULL;
int num_scalar = 0;
int ret = 0;
if (group->meth != r->meth) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
return 0;
}
if ((scalar == NULL) && (num == 0)) {
return EC_POINT_set_to_infinity(group, r);
}
for (i = 0; i < num; i++) {
if (group->meth != points[i]->meth) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
return 0;
}
}
if (ctx == NULL) {
ctx = new_ctx = BN_CTX_new();
if (ctx == NULL)
goto err;
}
if (scalar != NULL) {
generator = EC_GROUP_get0_generator(group);
if (generator == NULL) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
goto err;
}
pre_comp = group->pre_comp.ec;
if (pre_comp && pre_comp->numblocks
&& (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
0)) {
blocksize = pre_comp->blocksize;
numblocks = (BN_num_bits(scalar) / blocksize) + 1;
if (numblocks > pre_comp->numblocks)
numblocks = pre_comp->numblocks;
pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
} else {
pre_comp = NULL;
numblocks = 1;
num_scalar = 1;
}
}
totalnum = num + numblocks;
wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]);
val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
if (wNAF != NULL)
wNAF[0] = NULL;
if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
goto err;
}
num_val = 0;
for (i = 0; i < num + num_scalar; i++) {
size_t bits;
bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
wsize[i] = EC_window_bits_for_scalar_size(bits);
num_val += (size_t)1 << (wsize[i] - 1);
wNAF[i + 1] = NULL;
wNAF[i] =
bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
&wNAF_len[i]);
if (wNAF[i] == NULL)
goto err;
if (wNAF_len[i] > max_len)
max_len = wNAF_len[i];
}
if (numblocks) {
if (pre_comp == NULL) {
if (num_scalar != 1) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
} else {
signed char *tmp_wNAF = NULL;
size_t tmp_len = 0;
if (num_scalar != 0) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
wsize[num] = pre_comp->w;
tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
if (!tmp_wNAF)
goto err;
if (tmp_len <= max_len) {
numblocks = 1;
totalnum = num + 1;
wNAF[num] = tmp_wNAF;
wNAF[num + 1] = NULL;
wNAF_len[num] = tmp_len;
val_sub[num] = pre_comp->points;
} else {
signed char *pp;
EC_POINT **tmp_points;
if (tmp_len < numblocks * blocksize) {
numblocks = (tmp_len + blocksize - 1) / blocksize;
if (numblocks > pre_comp->numblocks) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
OPENSSL_free(tmp_wNAF);
goto err;
}
totalnum = num + numblocks;
}
pp = tmp_wNAF;
tmp_points = pre_comp->points;
for (i = num; i < totalnum; i++) {
if (i < totalnum - 1) {
wNAF_len[i] = blocksize;
if (tmp_len < blocksize) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
OPENSSL_free(tmp_wNAF);
goto err;
}
tmp_len -= blocksize;
} else
wNAF_len[i] = tmp_len;
wNAF[i + 1] = NULL;
wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
if (wNAF[i] == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
OPENSSL_free(tmp_wNAF);
goto err;
}
memcpy(wNAF[i], pp, wNAF_len[i]);
if (wNAF_len[i] > max_len)
max_len = wNAF_len[i];
if (*tmp_points == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
OPENSSL_free(tmp_wNAF);
goto err;
}
val_sub[i] = tmp_points;
tmp_points += pre_points_per_block;
pp += blocksize;
}
OPENSSL_free(tmp_wNAF);
}
}
}
val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
if (val == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
goto err;
}
val[num_val] = NULL;
v = val;
for (i = 0; i < num + num_scalar; i++) {
val_sub[i] = v;
for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
*v = EC_POINT_new(group);
if (*v == NULL)
goto err;
v++;
}
}
if (!(v == val + num_val)) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
if ((tmp = EC_POINT_new(group)) == NULL)
goto err;
for (i = 0; i < num + num_scalar; i++) {
if (i < num) {
if (!EC_POINT_copy(val_sub[i][0], points[i]))
goto err;
} else {
if (!EC_POINT_copy(val_sub[i][0], generator))
goto err;
}
if (wsize[i] > 1) {
if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
goto err;
for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
if (!EC_POINT_add
(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
goto err;
}
}
}
if (!EC_POINTs_make_affine(group, num_val, val, ctx))
goto err;
r_is_at_infinity = 1;
for (k = max_len - 1; k >= 0; k--) {
if (!r_is_at_infinity) {
if (!EC_POINT_dbl(group, r, r, ctx))
goto err;
}
for (i = 0; i < totalnum; i++) {
if (wNAF_len[i] > (size_t)k) {
int digit = wNAF[i][k];
int is_neg;
if (digit) {
is_neg = digit < 0;
if (is_neg)
digit = -digit;
if (is_neg != r_is_inverted) {
if (!r_is_at_infinity) {
if (!EC_POINT_invert(group, r, ctx))
goto err;
}
r_is_inverted = !r_is_inverted;
}
if (r_is_at_infinity) {
if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
goto err;
r_is_at_infinity = 0;
} else {
if (!EC_POINT_add
(group, r, r, val_sub[i][digit >> 1], ctx))
goto err;
}
}
}
}
}
if (r_is_at_infinity) {
if (!EC_POINT_set_to_infinity(group, r))
goto err;
} else {
if (r_is_inverted)
if (!EC_POINT_invert(group, r, ctx))
goto err;
}
ret = 1;
err:
BN_CTX_free(new_ctx);
EC_POINT_free(tmp);
OPENSSL_free(wsize);
OPENSSL_free(wNAF_len);
if (wNAF != NULL) {
signed char **w;
for (w = wNAF; *w != NULL; w++)
OPENSSL_free(*w);
OPENSSL_free(wNAF);
}
if (val != NULL) {
for (v = val; *v != NULL; v++)
EC_POINT_clear_free(*v);
OPENSSL_free(val);
}
OPENSSL_free(val_sub);
return ret;
}
|
['int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (group->meth != r->meth) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n for (i = 0; i < num; i++) {\n if (group->meth != points[i]->meth) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.ec;\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);\n wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]);\n val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_free(new_ctx);\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,887
| 0
|
https://github.com/libav/libav/blob/d9cf5f516974c64e01846ca685301014b38cf224/libavcodec/smacker.c/#L608
|
static int smka_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *avpkt)
{
SmackerAudioContext *s = avctx->priv_data;
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
GetBitContext gb;
HuffContext h[4] = { { 0 } };
VLC vlc[4] = { { 0 } };
int16_t *samples;
uint8_t *samples8;
int val;
int i, res, ret;
int unp_size;
int bits, stereo;
int pred[2] = {0, 0};
if (buf_size <= 4) {
av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
return AVERROR(EINVAL);
}
unp_size = AV_RL32(buf);
init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
if(!get_bits1(&gb)){
av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
*got_frame_ptr = 0;
return 1;
}
stereo = get_bits1(&gb);
bits = get_bits1(&gb);
if (stereo ^ (avctx->channels != 1)) {
av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
return AVERROR(EINVAL);
}
if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
return AVERROR(EINVAL);
}
s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
samples = (int16_t *)s->frame.data[0];
samples8 = s->frame.data[0];
for(i = 0; i < (1 << (bits + stereo)); i++) {
h[i].length = 256;
h[i].maxlength = 0;
h[i].current = 0;
h[i].bits = av_mallocz(256 * 4);
h[i].lengths = av_mallocz(256 * sizeof(int));
h[i].values = av_mallocz(256 * sizeof(int));
skip_bits1(&gb);
smacker_decode_tree(&gb, &h[i], 0, 0);
skip_bits1(&gb);
if(h[i].current > 1) {
res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
h[i].lengths, sizeof(int), sizeof(int),
h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
if(res < 0) {
av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
return -1;
}
}
}
if(bits) {
for(i = stereo; i >= 0; i--)
pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
for(i = 0; i <= stereo; i++)
*samples++ = pred[i];
for(; i < unp_size / 2; i++) {
if(i & stereo) {
if(vlc[2].table)
res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
else
res = 0;
val = h[2].values[res];
if(vlc[3].table)
res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
else
res = 0;
val |= h[3].values[res] << 8;
pred[1] += sign_extend(val, 16);
*samples++ = av_clip_int16(pred[1]);
} else {
if(vlc[0].table)
res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
else
res = 0;
val = h[0].values[res];
if(vlc[1].table)
res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
else
res = 0;
val |= h[1].values[res] << 8;
pred[0] += sign_extend(val, 16);
*samples++ = av_clip_int16(pred[0]);
}
}
} else {
for(i = stereo; i >= 0; i--)
pred[i] = get_bits(&gb, 8);
for(i = 0; i <= stereo; i++)
*samples8++ = pred[i];
for(; i < unp_size; i++) {
if(i & stereo){
if(vlc[1].table)
res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
else
res = 0;
pred[1] += sign_extend(h[1].values[res], 8);
*samples8++ = av_clip_uint8(pred[1]);
} else {
if(vlc[0].table)
res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
else
res = 0;
pred[0] += sign_extend(h[0].values[res], 8);
*samples8++ = av_clip_uint8(pred[0]);
}
}
}
for(i = 0; i < 4; i++) {
if(vlc[i].table)
ff_free_vlc(&vlc[i]);
av_free(h[i].bits);
av_free(h[i].lengths);
av_free(h[i].values);
}
*got_frame_ptr = 1;
*(AVFrame *)data = s->frame;
return buf_size;
}
|
['static int smka_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n SmackerAudioContext *s = avctx->priv_data;\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n GetBitContext gb;\n HuffContext h[4] = { { 0 } };\n VLC vlc[4] = { { 0 } };\n int16_t *samples;\n uint8_t *samples8;\n int val;\n int i, res, ret;\n int unp_size;\n int bits, stereo;\n int pred[2] = {0, 0};\n if (buf_size <= 4) {\n av_log(avctx, AV_LOG_ERROR, "packet is too small\\n");\n return AVERROR(EINVAL);\n }\n unp_size = AV_RL32(buf);\n init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);\n if(!get_bits1(&gb)){\n av_log(avctx, AV_LOG_INFO, "Sound: no data\\n");\n *got_frame_ptr = 0;\n return 1;\n }\n stereo = get_bits1(&gb);\n bits = get_bits1(&gb);\n if (stereo ^ (avctx->channels != 1)) {\n av_log(avctx, AV_LOG_ERROR, "channels mismatch\\n");\n return AVERROR(EINVAL);\n }\n if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {\n av_log(avctx, AV_LOG_ERROR, "sample format mismatch\\n");\n return AVERROR(EINVAL);\n }\n s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));\n if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (int16_t *)s->frame.data[0];\n samples8 = s->frame.data[0];\n for(i = 0; i < (1 << (bits + stereo)); i++) {\n h[i].length = 256;\n h[i].maxlength = 0;\n h[i].current = 0;\n h[i].bits = av_mallocz(256 * 4);\n h[i].lengths = av_mallocz(256 * sizeof(int));\n h[i].values = av_mallocz(256 * sizeof(int));\n skip_bits1(&gb);\n smacker_decode_tree(&gb, &h[i], 0, 0);\n skip_bits1(&gb);\n if(h[i].current > 1) {\n res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,\n h[i].lengths, sizeof(int), sizeof(int),\n h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);\n if(res < 0) {\n av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\\n");\n return -1;\n }\n }\n }\n if(bits) {\n for(i = stereo; i >= 0; i--)\n pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);\n for(i = 0; i <= stereo; i++)\n *samples++ = pred[i];\n for(; i < unp_size / 2; i++) {\n if(i & stereo) {\n if(vlc[2].table)\n res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[2].values[res];\n if(vlc[3].table)\n res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[3].values[res] << 8;\n pred[1] += sign_extend(val, 16);\n *samples++ = av_clip_int16(pred[1]);\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[0].values[res];\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[1].values[res] << 8;\n pred[0] += sign_extend(val, 16);\n *samples++ = av_clip_int16(pred[0]);\n }\n }\n } else {\n for(i = stereo; i >= 0; i--)\n pred[i] = get_bits(&gb, 8);\n for(i = 0; i <= stereo; i++)\n *samples8++ = pred[i];\n for(; i < unp_size; i++) {\n if(i & stereo){\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[1] += sign_extend(h[1].values[res], 8);\n *samples8++ = av_clip_uint8(pred[1]);\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[0] += sign_extend(h[0].values[res], 8);\n *samples8++ = av_clip_uint8(pred[0]);\n }\n }\n }\n for(i = 0; i < 4; i++) {\n if(vlc[i].table)\n ff_free_vlc(&vlc[i]);\n av_free(h[i].bits);\n av_free(h[i].lengths);\n av_free(h[i].values);\n }\n *got_frame_ptr = 1;\n *(AVFrame *)data = s->frame;\n return buf_size;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size <= 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
|
2,888
| 0
|
https://github.com/libav/libav/blob/f653095bdd5f6c25960f75b81b138dd78f73ca37/ffmpeg.c/#L2950
|
static void new_video_stream(AVFormatContext *oc)
{
AVStream *st;
AVCodecContext *video_enc;
int codec_id;
st = av_new_stream(oc, oc->nb_streams);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
av_exit(1);
}
avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);
bitstream_filters[nb_output_files][oc->nb_streams - 1]= video_bitstream_filters;
video_bitstream_filters= NULL;
if(thread_count>1)
avcodec_thread_init(st->codec, thread_count);
video_enc = st->codec;
if(video_codec_tag)
video_enc->codec_tag= video_codec_tag;
if( (video_global_header&1)
|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){
video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
avctx_opts[CODEC_TYPE_VIDEO]->flags|= CODEC_FLAG_GLOBAL_HEADER;
}
if(video_global_header&2){
video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
avctx_opts[CODEC_TYPE_VIDEO]->flags2|= CODEC_FLAG2_LOCAL_HEADER;
}
if (video_stream_copy) {
st->stream_copy = 1;
video_enc->codec_type = CODEC_TYPE_VIDEO;
video_enc->sample_aspect_ratio =
st->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
} else {
const char *p;
int i;
AVCodec *codec;
AVRational fps= frame_rate.num ? frame_rate : (AVRational){25,1};
if (video_codec_name) {
codec_id = find_codec_or_die(video_codec_name, CODEC_TYPE_VIDEO, 1);
codec = avcodec_find_encoder_by_name(video_codec_name);
output_codecs[nb_ocodecs] = codec;
} else {
codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);
codec = avcodec_find_encoder(codec_id);
}
video_enc->codec_id = codec_id;
set_context_opts(video_enc, avctx_opts[CODEC_TYPE_VIDEO], AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM);
if (codec && codec->supported_framerates && !force_fps)
fps = codec->supported_framerates[av_find_nearest_q_idx(fps, codec->supported_framerates)];
video_enc->time_base.den = fps.num;
video_enc->time_base.num = fps.den;
video_enc->width = frame_width + frame_padright + frame_padleft;
video_enc->height = frame_height + frame_padtop + frame_padbottom;
video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*video_enc->height/video_enc->width, 255);
video_enc->pix_fmt = frame_pix_fmt;
st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
if(codec && codec->pix_fmts){
const enum PixelFormat *p= codec->pix_fmts;
for(; *p!=-1; p++){
if(*p == video_enc->pix_fmt)
break;
}
if(*p == -1)
video_enc->pix_fmt = codec->pix_fmts[0];
}
if (intra_only)
video_enc->gop_size = 0;
if (video_qscale || same_quality) {
video_enc->flags |= CODEC_FLAG_QSCALE;
video_enc->global_quality=
st->quality = FF_QP2LAMBDA * video_qscale;
}
if(intra_matrix)
video_enc->intra_matrix = intra_matrix;
if(inter_matrix)
video_enc->inter_matrix = inter_matrix;
video_enc->thread_count = thread_count;
p= video_rc_override_string;
for(i=0; p; i++){
int start, end, q;
int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
if(e!=3){
fprintf(stderr, "error parsing rc_override\n");
av_exit(1);
}
video_enc->rc_override=
av_realloc(video_enc->rc_override,
sizeof(RcOverride)*(i+1));
video_enc->rc_override[i].start_frame= start;
video_enc->rc_override[i].end_frame = end;
if(q>0){
video_enc->rc_override[i].qscale= q;
video_enc->rc_override[i].quality_factor= 1.0;
}
else{
video_enc->rc_override[i].qscale= 0;
video_enc->rc_override[i].quality_factor= -q/100.0;
}
p= strchr(p, '/');
if(p) p++;
}
video_enc->rc_override_count=i;
if (!video_enc->rc_initial_buffer_occupancy)
video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4;
video_enc->me_threshold= me_threshold;
video_enc->intra_dc_precision= intra_dc_precision - 8;
if (do_psnr)
video_enc->flags|= CODEC_FLAG_PSNR;
if (do_pass) {
if (do_pass == 1) {
video_enc->flags |= CODEC_FLAG_PASS1;
} else {
video_enc->flags |= CODEC_FLAG_PASS2;
}
}
}
nb_ocodecs++;
video_disable = 0;
av_freep(&video_codec_name);
video_stream_copy = 0;
}
|
['static void new_video_stream(AVFormatContext *oc)\n{\n AVStream *st;\n AVCodecContext *video_enc;\n int codec_id;\n st = av_new_stream(oc, oc->nb_streams);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n av_exit(1);\n }\n avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);\n bitstream_filters[nb_output_files][oc->nb_streams - 1]= video_bitstream_filters;\n video_bitstream_filters= NULL;\n if(thread_count>1)\n avcodec_thread_init(st->codec, thread_count);\n video_enc = st->codec;\n if(video_codec_tag)\n video_enc->codec_tag= video_codec_tag;\n if( (video_global_header&1)\n || (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){\n video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;\n avctx_opts[CODEC_TYPE_VIDEO]->flags|= CODEC_FLAG_GLOBAL_HEADER;\n }\n if(video_global_header&2){\n video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;\n avctx_opts[CODEC_TYPE_VIDEO]->flags2|= CODEC_FLAG2_LOCAL_HEADER;\n }\n if (video_stream_copy) {\n st->stream_copy = 1;\n video_enc->codec_type = CODEC_TYPE_VIDEO;\n video_enc->sample_aspect_ratio =\n st->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);\n } else {\n const char *p;\n int i;\n AVCodec *codec;\n AVRational fps= frame_rate.num ? frame_rate : (AVRational){25,1};\n if (video_codec_name) {\n codec_id = find_codec_or_die(video_codec_name, CODEC_TYPE_VIDEO, 1);\n codec = avcodec_find_encoder_by_name(video_codec_name);\n output_codecs[nb_ocodecs] = codec;\n } else {\n codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);\n codec = avcodec_find_encoder(codec_id);\n }\n video_enc->codec_id = codec_id;\n set_context_opts(video_enc, avctx_opts[CODEC_TYPE_VIDEO], AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM);\n if (codec && codec->supported_framerates && !force_fps)\n fps = codec->supported_framerates[av_find_nearest_q_idx(fps, codec->supported_framerates)];\n video_enc->time_base.den = fps.num;\n video_enc->time_base.num = fps.den;\n video_enc->width = frame_width + frame_padright + frame_padleft;\n video_enc->height = frame_height + frame_padtop + frame_padbottom;\n video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*video_enc->height/video_enc->width, 255);\n video_enc->pix_fmt = frame_pix_fmt;\n st->sample_aspect_ratio = video_enc->sample_aspect_ratio;\n if(codec && codec->pix_fmts){\n const enum PixelFormat *p= codec->pix_fmts;\n for(; *p!=-1; p++){\n if(*p == video_enc->pix_fmt)\n break;\n }\n if(*p == -1)\n video_enc->pix_fmt = codec->pix_fmts[0];\n }\n if (intra_only)\n video_enc->gop_size = 0;\n if (video_qscale || same_quality) {\n video_enc->flags |= CODEC_FLAG_QSCALE;\n video_enc->global_quality=\n st->quality = FF_QP2LAMBDA * video_qscale;\n }\n if(intra_matrix)\n video_enc->intra_matrix = intra_matrix;\n if(inter_matrix)\n video_enc->inter_matrix = inter_matrix;\n video_enc->thread_count = thread_count;\n p= video_rc_override_string;\n for(i=0; p; i++){\n int start, end, q;\n int e=sscanf(p, "%d,%d,%d", &start, &end, &q);\n if(e!=3){\n fprintf(stderr, "error parsing rc_override\\n");\n av_exit(1);\n }\n video_enc->rc_override=\n av_realloc(video_enc->rc_override,\n sizeof(RcOverride)*(i+1));\n video_enc->rc_override[i].start_frame= start;\n video_enc->rc_override[i].end_frame = end;\n if(q>0){\n video_enc->rc_override[i].qscale= q;\n video_enc->rc_override[i].quality_factor= 1.0;\n }\n else{\n video_enc->rc_override[i].qscale= 0;\n video_enc->rc_override[i].quality_factor= -q/100.0;\n }\n p= strchr(p, \'/\');\n if(p) p++;\n }\n video_enc->rc_override_count=i;\n if (!video_enc->rc_initial_buffer_occupancy)\n video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4;\n video_enc->me_threshold= me_threshold;\n video_enc->intra_dc_precision= intra_dc_precision - 8;\n if (do_psnr)\n video_enc->flags|= CODEC_FLAG_PSNR;\n if (do_pass) {\n if (do_pass == 1) {\n video_enc->flags |= CODEC_FLAG_PASS1;\n } else {\n video_enc->flags |= CODEC_FLAG_PASS2;\n }\n }\n }\n nb_ocodecs++;\n video_disable = 0;\n av_freep(&video_codec_name);\n video_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n if (s->nb_streams >= MAX_STREAMS)\n return NULL;\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\n st->start_time = AV_NOPTS_VALUE;\n st->duration = AV_NOPTS_VALUE;\n st->cur_dts = 0;\n st->first_dts = AV_NOPTS_VALUE;\n av_set_pts_info(st, 33, 1, 90000);\n st->last_IP_pts = AV_NOPTS_VALUE;\n for(i=0; i<MAX_REORDER_DELAY+1; i++)\n st->pts_buffer[i]= AV_NOPTS_VALUE;\n st->sample_aspect_ratio = (AVRational){0,1};\n s->streams[s->nb_streams++] = st;\n return st;\n}']
|
2,889
| 0
|
https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L138
|
static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
bc->bits >>= n;
#else
uint64_t ret = bc->bits >> (64 - n);
bc->bits <<= n;
#endif
bc->bits_left -= n;
return ret;
}
|
['static int parse_source_parameters(AVDiracSeqHeader *dsh, BitstreamContext *bc,\n void *log_ctx)\n{\n AVRational frame_rate = { 0, 0 };\n unsigned luma_depth = 8, luma_offset = 16;\n int idx;\n if (bitstream_read_bit(bc)) {\n dsh->width = get_interleaved_ue_golomb(bc);\n dsh->height = get_interleaved_ue_golomb(bc);\n }\n if (bitstream_read_bit(bc))\n dsh->chroma_format = get_interleaved_ue_golomb(bc);\n if (dsh->chroma_format > 2) {\n if (log_ctx)\n av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\\n",\n dsh->chroma_format);\n return AVERROR_INVALIDDATA;\n }\n if (bitstream_read_bit(bc))\n dsh->interlaced = get_interleaved_ue_golomb(bc);\n if (dsh->interlaced > 1)\n return AVERROR_INVALIDDATA;\n if (bitstream_read_bit(bc)) {\n dsh->frame_rate_index = get_interleaved_ue_golomb(bc);\n if (dsh->frame_rate_index > 10)\n return AVERROR_INVALIDDATA;\n if (!dsh->frame_rate_index) {\n frame_rate.num = get_interleaved_ue_golomb(bc);\n frame_rate.den = get_interleaved_ue_golomb(bc);\n }\n }\n if (dsh->frame_rate_index > 0) {\n if (dsh->frame_rate_index <= 8)\n frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];\n else\n frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];\n }\n dsh->framerate = frame_rate;\n if (bitstream_read_bit(bc)) {\n dsh->aspect_ratio_index = get_interleaved_ue_golomb(bc);\n if (dsh->aspect_ratio_index > 6)\n return AVERROR_INVALIDDATA;\n if (!dsh->aspect_ratio_index) {\n dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(bc);\n dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(bc);\n }\n }\n if (dsh->aspect_ratio_index > 0)\n dsh->sample_aspect_ratio =\n dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];\n if (bitstream_read_bit(bc)) {\n dsh->clean_width = get_interleaved_ue_golomb(bc);\n dsh->clean_height = get_interleaved_ue_golomb(bc);\n dsh->clean_left_offset = get_interleaved_ue_golomb(bc);\n dsh->clean_right_offset = get_interleaved_ue_golomb(bc);\n }\n if (bitstream_read_bit(bc)) {\n dsh->pixel_range_index = get_interleaved_ue_golomb(bc);\n if (dsh->pixel_range_index > 4)\n return AVERROR_INVALIDDATA;\n if (!dsh->pixel_range_index) {\n luma_offset = get_interleaved_ue_golomb(bc);\n luma_depth = av_log2(get_interleaved_ue_golomb(bc)) + 1;\n get_interleaved_ue_golomb(bc);\n get_interleaved_ue_golomb(bc);\n dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG\n : AVCOL_RANGE_JPEG;\n }\n }\n if (dsh->pixel_range_index > 0) {\n idx = dsh->pixel_range_index - 1;\n luma_depth = pixel_range_presets[idx].bitdepth;\n dsh->color_range = pixel_range_presets[idx].color_range;\n }\n if (luma_depth > 8 && log_ctx)\n av_log(log_ctx, AV_LOG_WARNING, "Bitdepth greater than 8");\n dsh->pix_fmt = dirac_pix_fmt[!luma_offset][dsh->chroma_format];\n if (bitstream_read_bit(bc)) {\n idx = dsh->color_spec_index = get_interleaved_ue_golomb(bc);\n if (dsh->color_spec_index > 4)\n return AVERROR_INVALIDDATA;\n dsh->color_primaries = dirac_color_presets[idx].color_primaries;\n dsh->colorspace = dirac_color_presets[idx].colorspace;\n dsh->color_trc = dirac_color_presets[idx].color_trc;\n if (!dsh->color_spec_index) {\n if (bitstream_read_bit(bc)) {\n idx = get_interleaved_ue_golomb(bc);\n if (idx < 3)\n dsh->color_primaries = dirac_primaries[idx];\n }\n if (bitstream_read_bit(bc)) {\n idx = get_interleaved_ue_golomb(bc);\n if (!idx)\n dsh->colorspace = AVCOL_SPC_BT709;\n else if (idx == 1)\n dsh->colorspace = AVCOL_SPC_BT470BG;\n }\n if (bitstream_read_bit(bc) && !get_interleaved_ue_golomb(bc))\n dsh->color_trc = AVCOL_TRC_BT709;\n }\n } else {\n idx = dsh->color_spec_index;\n dsh->color_primaries = dirac_color_presets[idx].color_primaries;\n dsh->colorspace = dirac_color_presets[idx].colorspace;\n dsh->color_trc = dirac_color_presets[idx].color_trc;\n }\n return 0;\n}', 'static inline unsigned get_interleaved_ue_golomb(BitstreamContext *bc)\n{\n uint32_t buf;\n buf = bitstream_peek(bc, 32);\n if (buf & 0xAA800000) {\n buf >>= 32 - 8;\n bitstream_skip(bc, ff_interleaved_golomb_vlc_len[buf]);\n return ff_interleaved_ue_golomb_vlc_code[buf];\n } else {\n unsigned ret = 1;\n do {\n buf >>= 32 - 8;\n bitstream_skip(bc, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));\n if (ff_interleaved_golomb_vlc_len[buf] != 9) {\n ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;\n ret |= ff_interleaved_dirac_golomb_vlc_code[buf];\n break;\n }\n ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];\n buf = bitstream_peek(bc, 32);\n } while (bitstream_bits_left(bc) > 0);\n return ret - 1;\n }\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n < bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n bc->bits = 0;\n bc->bits_left = 0;\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void refill_64(BitstreamContext *bc)\n{\n if (bc->ptr >= bc->buffer_end)\n return;\n#ifdef BITSTREAM_READER_LE\n bc->bits = AV_RL64(bc->ptr);\n#else\n bc->bits = AV_RB64(bc->ptr);\n#endif\n bc->ptr += 8;\n bc->bits_left = 64;\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
|
2,890
| 0
|
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,\n const BIGNUM *in_kinv, const BIGNUM *in_r,\n EC_KEY *eckey)\n{\n int ok = 0, i;\n BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;\n const BIGNUM *order, *ckinv;\n BN_CTX *ctx = NULL;\n const EC_GROUP *group;\n ECDSA_SIG *ret;\n const BIGNUM *priv_key;\n group = EC_KEY_get0_group(eckey);\n priv_key = EC_KEY_get0_private_key(eckey);\n if (group == NULL || priv_key == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);\n return NULL;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return NULL;\n }\n ret = ECDSA_SIG_new();\n if (ret == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n ret->r = BN_new();\n ret->s = BN_new();\n if (ret->r == NULL || ret->s == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n s = ret->s;\n if ((ctx = BN_CTX_new()) == NULL ||\n (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);\n goto err;\n }\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n do {\n if (in_kinv == NULL || in_r == NULL) {\n if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);\n goto err;\n }\n ckinv = kinv;\n } else {\n ckinv = in_kinv;\n if (BN_copy(ret->r, in_r) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_add_quick(s, tmp, m, order)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(s, s, ckinv, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (BN_is_zero(s)) {\n if (in_kinv != NULL && in_r != NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);\n goto err;\n }\n } else\n break;\n }\n while (1);\n ok = 1;\n err:\n if (!ok) {\n ECDSA_SIG_free(ret);\n ret = NULL;\n }\n BN_CTX_free(ctx);\n BN_clear_free(m);\n BN_clear_free(tmp);\n BN_clear_free(kinv);\n return ret;\n}', 'static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,\n BIGNUM **kinvp, BIGNUM **rp,\n const unsigned char *dgst, int dlen)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL, *r = NULL, *X = NULL;\n const BIGNUM *order;\n EC_POINT *tmp_point = NULL;\n const EC_GROUP *group;\n int ret = 0;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return 0;\n }\n if (ctx_in == NULL) {\n if ((ctx = BN_CTX_new()) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n } else\n ctx = ctx_in;\n k = BN_new();\n r = BN_new();\n X = BN_new();\n if (k == NULL || r == NULL || X == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if ((tmp_point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n do {\n do\n if (dgst != NULL) {\n if (!BN_generate_dsa_nonce\n (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,\n ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n } else {\n if (!BN_priv_rand_range(k, order)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n }\n while (BN_is_zero(k));\n if (!BN_add(k, k, order))\n goto err;\n if (BN_num_bits(k) <= BN_num_bits(order))\n if (!BN_add(k, k, order))\n goto err;\n if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp\n (group, tmp_point, X, NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group,\n tmp_point, X, NULL,\n ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(r, X, order, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n }\n while (BN_is_zero(r));\n if (EC_GROUP_get_mont_data(group) != NULL) {\n if (!BN_set_word(X, 2)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_sub(X, order, X, order, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(X, BN_FLG_CONSTTIME);\n if (!BN_mod_exp_mont_consttime\n (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n } else {\n if (!BN_mod_inverse(k, k, order, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n }\n BN_clear_free(*rp);\n BN_clear_free(*kinvp);\n *rp = r;\n *kinvp = k;\n ret = 1;\n err:\n if (!ret) {\n BN_clear_free(k);\n BN_clear_free(r);\n }\n if (ctx != ctx_in)\n BN_CTX_free(ctx);\n EC_POINT_free(tmp_point);\n BN_clear_free(X);\n return ret;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,891
| 0
|
https://github.com/openssl/openssl/blob/393b704d282909dff28bdca80e2d8a1d404086f4/apps/speed.c/#L2558
|
static int do_multi(int multi)
{
int n;
int fd[2];
int *fds;
static char sep[]=":";
fds=malloc(multi*sizeof *fds);
for(n=0 ; n < multi ; ++n)
{
pipe(fd);
if(fork())
{
close(fd[1]);
fds[n]=fd[0];
}
else
{
close(fd[0]);
close(1);
dup(fd[1]);
close(fd[1]);
mr=1;
usertime=0;
return 0;
}
printf("Forked child %d\n",n);
}
for(n=0 ; n < multi ; ++n)
{
FILE *f;
char buf[1024];
char *p;
f=fdopen(fds[n],"r");
while(fgets(buf,sizeof buf,f))
{
p=strchr(buf,'\n');
if(p)
*p='\0';
if(buf[0] != '+')
{
fprintf(stderr,"Don't understand line '%s' from child %d\n",
buf,n);
continue;
}
printf("Got: %s from %d\n",buf,n);
if(!strncmp(buf,"+F:",3))
{
int alg;
int j;
p=buf+3;
alg=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
for(j=0 ; j < SIZE_NUM ; ++j)
results[alg][j]+=atof(sstrsep(&p,sep));
}
else if(!strncmp(buf,"+F2:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
else
rsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
else
rsa_results[k][1]=d;
}
else if(!strncmp(buf,"+F2:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
else
rsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
else
rsa_results[k][1]=d;
}
else if(!strncmp(buf,"+F3:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);
else
dsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);
else
dsa_results[k][1]=d;
}
#ifndef OPENSSL_NO_ECDSA
else if(!strncmp(buf,"+F4:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
ecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);
else
ecdsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
ecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);
else
ecdsa_results[k][1]=d;
}
#endif
#ifndef OPENSSL_NO_ECDH
else if(!strncmp(buf,"+F5:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
ecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);
else
ecdh_results[k][0]=d;
}
#endif
else if(!strncmp(buf,"+H:",3))
{
}
else
fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
}
}
return 1;
}
|
['static int do_multi(int multi)\n\t{\n\tint n;\n\tint fd[2];\n\tint *fds;\n\tstatic char sep[]=":";\n\tfds=malloc(multi*sizeof *fds);\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tpipe(fd);\n\t\tif(fork())\n\t\t\t{\n\t\t\tclose(fd[1]);\n\t\t\tfds[n]=fd[0];\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tclose(fd[0]);\n\t\t\tclose(1);\n\t\t\tdup(fd[1]);\n\t\t\tclose(fd[1]);\n\t\t\tmr=1;\n\t\t\tusertime=0;\n\t\t\treturn 0;\n\t\t\t}\n\t\tprintf("Forked child %d\\n",n);\n\t\t}\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tFILE *f;\n\t\tchar buf[1024];\n\t\tchar *p;\n\t\tf=fdopen(fds[n],"r");\n\t\twhile(fgets(buf,sizeof buf,f))\n\t\t\t{\n\t\t\tp=strchr(buf,\'\\n\');\n\t\t\tif(p)\n\t\t\t\t*p=\'\\0\';\n\t\t\tif(buf[0] != \'+\')\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"Don\'t understand line \'%s\' from child %d\\n",\n\t\t\t\t\t\tbuf,n);\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\tprintf("Got: %s from %d\\n",buf,n);\n\t\t\tif(!strncmp(buf,"+F:",3))\n\t\t\t\t{\n\t\t\t\tint alg;\n\t\t\t\tint j;\n\t\t\t\tp=buf+3;\n\t\t\t\talg=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\tfor(j=0 ; j < SIZE_NUM ; ++j)\n\t\t\t\t\tresults[alg][j]+=atof(sstrsep(&p,sep));\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F3:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][1]=d;\n\t\t\t\t}\n#ifndef OPENSSL_NO_ECDSA\n\t\t\telse if(!strncmp(buf,"+F4:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][1]=d;\n\t\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\t\telse if(!strncmp(buf,"+F5:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdh_results[k][0]=d;\n\t\t\t\t}\n#endif\n\t\t\telse if(!strncmp(buf,"+H:",3))\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tfprintf(stderr,"Unknown type \'%s\' from child %d\\n",buf,n);\n\t\t\t}\n\t\t}\n\treturn 1;\n\t}']
|
2,892
| 0
|
https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/core/ngx_hash.c/#L760
|
ngx_int_t
ngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value,
ngx_uint_t flags)
{
size_t len;
u_char *p;
ngx_str_t *name;
ngx_uint_t i, k, n, skip, last;
ngx_array_t *keys, *hwc;
ngx_hash_key_t *hk;
last = key->len;
if (flags & NGX_HASH_WILDCARD_KEY) {
n = 0;
for (i = 0; i < key->len; i++) {
if (key->data[i] == '*') {
if (++n > 1) {
return NGX_DECLINED;
}
}
if (key->data[i] == '.' && key->data[i + 1] == '.') {
return NGX_DECLINED;
}
}
if (key->len > 1 && key->data[0] == '.') {
skip = 1;
goto wildcard;
}
if (key->len > 2) {
if (key->data[0] == '*' && key->data[1] == '.') {
skip = 2;
goto wildcard;
}
if (key->data[i - 2] == '.' && key->data[i - 1] == '*') {
skip = 0;
last -= 2;
goto wildcard;
}
}
if (n) {
return NGX_DECLINED;
}
}
k = 0;
for (i = 0; i < last; i++) {
if (!(flags & NGX_HASH_READONLY_KEY)) {
key->data[i] = ngx_tolower(key->data[i]);
}
k = ngx_hash(k, key->data[i]);
}
k %= ha->hsize;
name = ha->keys_hash[k].elts;
if (name) {
for (i = 0; i < ha->keys_hash[k].nelts; i++) {
if (last != name[i].len) {
continue;
}
if (ngx_strncmp(key->data, name[i].data, last) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,
sizeof(ngx_str_t))
!= NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(&ha->keys_hash[k]);
if (name == NULL) {
return NGX_ERROR;
}
*name = *key;
hk = ngx_array_push(&ha->keys);
if (hk == NULL) {
return NGX_ERROR;
}
hk->key = *key;
hk->key_hash = ngx_hash_key(key->data, last);
hk->value = value;
return NGX_OK;
wildcard:
k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);
k %= ha->hsize;
if (skip == 1) {
name = ha->keys_hash[k].elts;
if (name) {
len = last - skip;
for (i = 0; i < ha->keys_hash[k].nelts; i++) {
if (len != name[i].len) {
continue;
}
if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,
sizeof(ngx_str_t))
!= NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(&ha->keys_hash[k]);
if (name == NULL) {
return NGX_ERROR;
}
name->len = last - 1;
name->data = ngx_pnalloc(ha->temp_pool, name->len);
if (name->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(name->data, &key->data[1], name->len);
}
if (skip) {
p = ngx_pnalloc(ha->temp_pool, last);
if (p == NULL) {
return NGX_ERROR;
}
len = 0;
n = 0;
for (i = last - 1; i; i--) {
if (key->data[i] == '.') {
ngx_memcpy(&p[n], &key->data[i + 1], len);
n += len;
p[n++] = '.';
len = 0;
continue;
}
len++;
}
if (len) {
ngx_memcpy(&p[n], &key->data[1], len);
n += len;
}
p[n] = '\0';
hwc = &ha->dns_wc_head;
keys = &ha->dns_wc_head_hash[k];
} else {
last++;
p = ngx_pnalloc(ha->temp_pool, last);
if (p == NULL) {
return NGX_ERROR;
}
ngx_cpystrn(p, key->data, last);
hwc = &ha->dns_wc_tail;
keys = &ha->dns_wc_tail_hash[k];
}
hk = ngx_array_push(hwc);
if (hk == NULL) {
return NGX_ERROR;
}
hk->key.len = last - 1;
hk->key.data = p;
hk->key_hash = 0;
hk->value = value;
name = keys->elts;
if (name) {
len = last - skip;
for (i = 0; i < keys->nelts; i++) {
if (len != name[i].len) {
continue;
}
if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(keys);
if (name == NULL) {
return NGX_ERROR;
}
name->len = last - skip;
name->data = ngx_pnalloc(ha->temp_pool, name->len);
if (name->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(name->data, key->data + skip, name->len);
return NGX_OK;
}
|
['static char *\nngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)\n{\n ngx_http_map_conf_t *mcf = conf;\n char *rv;\n ngx_str_t *value, name;\n ngx_conf_t save;\n ngx_pool_t *pool;\n ngx_hash_init_t hash;\n ngx_http_map_ctx_t *map;\n ngx_http_variable_t *var;\n ngx_http_map_conf_ctx_t ctx;\n if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) {\n mcf->hash_max_size = 2048;\n }\n if (mcf->hash_bucket_size == NGX_CONF_UNSET_UINT) {\n mcf->hash_bucket_size = ngx_cacheline_size;\n } else {\n mcf->hash_bucket_size = ngx_align(mcf->hash_bucket_size,\n ngx_cacheline_size);\n }\n map = ngx_pcalloc(cf->pool, sizeof(ngx_http_map_ctx_t));\n if (map == NULL) {\n return NGX_CONF_ERROR;\n }\n value = cf->args->elts;\n name = value[1];\n name.len--;\n name.data++;\n map->index = ngx_http_get_variable_index(cf, &name);\n if (map->index == NGX_ERROR) {\n return NGX_CONF_ERROR;\n }\n name = value[2];\n name.len--;\n name.data++;\n var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);\n if (var == NULL) {\n return NGX_CONF_ERROR;\n }\n var->get_handler = ngx_http_map_variable;\n var->data = (uintptr_t) map;\n pool = ngx_create_pool(16384, cf->log);\n if (pool == NULL) {\n return NGX_CONF_ERROR;\n }\n ctx.keys.pool = cf->pool;\n ctx.keys.temp_pool = pool;\n if (ngx_hash_keys_array_init(&ctx.keys, NGX_HASH_LARGE) != NGX_OK) {\n ngx_destroy_pool(pool);\n return NGX_CONF_ERROR;\n }\n ctx.values_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * ctx.keys.hsize);\n if (ctx.values_hash == NULL) {\n ngx_destroy_pool(pool);\n return NGX_CONF_ERROR;\n }\n ctx.default_value = NULL;\n ctx.hostnames = 0;\n save = *cf;\n cf->pool = pool;\n cf->ctx = &ctx;\n cf->handler = ngx_http_map;\n cf->handler_conf = conf;\n rv = ngx_conf_parse(cf, NULL);\n *cf = save;\n if (rv != NGX_CONF_OK) {\n ngx_destroy_pool(pool);\n return rv;\n }\n map->default_value = ctx.default_value ? ctx.default_value:\n &ngx_http_variable_null_value;\n hash.key = ngx_hash_key_lc;\n hash.max_size = mcf->hash_max_size;\n hash.bucket_size = mcf->hash_bucket_size;\n hash.name = "map_hash";\n hash.pool = cf->pool;\n if (ctx.keys.keys.nelts) {\n hash.hash = &map->hash.hash;\n hash.temp_pool = NULL;\n if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts)\n != NGX_OK)\n {\n ngx_destroy_pool(pool);\n return NGX_CONF_ERROR;\n }\n }\n if (ctx.keys.dns_wc_head.nelts) {\n ngx_qsort(ctx.keys.dns_wc_head.elts,\n (size_t) ctx.keys.dns_wc_head.nelts,\n sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards);\n hash.hash = NULL;\n hash.temp_pool = pool;\n if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_head.elts,\n ctx.keys.dns_wc_head.nelts)\n != NGX_OK)\n {\n ngx_destroy_pool(pool);\n return NGX_CONF_ERROR;\n }\n map->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash;\n }\n if (ctx.keys.dns_wc_tail.nelts) {\n ngx_qsort(ctx.keys.dns_wc_tail.elts,\n (size_t) ctx.keys.dns_wc_tail.nelts,\n sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards);\n hash.hash = NULL;\n hash.temp_pool = pool;\n if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_tail.elts,\n ctx.keys.dns_wc_tail.nelts)\n != NGX_OK)\n {\n ngx_destroy_pool(pool);\n return NGX_CONF_ERROR;\n }\n map->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash;\n }\n ngx_destroy_pool(pool);\n return rv;\n}', 'ngx_int_t\nngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name)\n{\n ngx_uint_t i;\n ngx_http_variable_t *v;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\n v = cmcf->variables.elts;\n if (v == NULL) {\n if (ngx_array_init(&cmcf->variables, cf->pool, 4,\n sizeof(ngx_http_variable_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n } else {\n for (i = 0; i < cmcf->variables.nelts; i++) {\n if (name->len != v[i].name.len\n || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)\n {\n continue;\n }\n return i;\n }\n }\n v = ngx_array_push(&cmcf->variables);\n if (v == NULL) {\n return NGX_ERROR;\n }\n v->name.len = name->len;\n v->name.data = ngx_pnalloc(cf->pool, name->len);\n if (v->name.data == NULL) {\n return NGX_ERROR;\n }\n ngx_strlow(v->name.data, name->data, name->len);\n v->set_handler = NULL;\n v->get_handler = NULL;\n v->data = 0;\n v->flags = 0;\n v->index = cmcf->variables.nelts - 1;\n return cmcf->variables.nelts - 1;\n}', 'ngx_http_variable_t *\nngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)\n{\n ngx_int_t rc;\n ngx_uint_t i;\n ngx_hash_key_t *key;\n ngx_http_variable_t *v;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\n key = cmcf->variables_keys->keys.elts;\n for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {\n if (name->len != key[i].key.len\n || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)\n {\n continue;\n }\n v = key[i].value;\n if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "the duplicate \\"%V\\" variable", name);\n return NULL;\n }\n return v;\n }\n v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));\n if (v == NULL) {\n return NULL;\n }\n v->name.len = name->len;\n v->name.data = ngx_pnalloc(cf->pool, name->len);\n if (v->name.data == NULL) {\n return NULL;\n }\n ngx_strlow(v->name.data, name->data, name->len);\n v->set_handler = NULL;\n v->get_handler = NULL;\n v->data = 0;\n v->flags = flags;\n v->index = 0;\n rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);\n if (rc == NGX_ERROR) {\n return NULL;\n }\n if (rc == NGX_BUSY) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "conflicting variable name \\"%V\\"", name);\n return NULL;\n }\n return v;\n}', "ngx_int_t\nngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value,\n ngx_uint_t flags)\n{\n size_t len;\n u_char *p;\n ngx_str_t *name;\n ngx_uint_t i, k, n, skip, last;\n ngx_array_t *keys, *hwc;\n ngx_hash_key_t *hk;\n last = key->len;\n if (flags & NGX_HASH_WILDCARD_KEY) {\n n = 0;\n for (i = 0; i < key->len; i++) {\n if (key->data[i] == '*') {\n if (++n > 1) {\n return NGX_DECLINED;\n }\n }\n if (key->data[i] == '.' && key->data[i + 1] == '.') {\n return NGX_DECLINED;\n }\n }\n if (key->len > 1 && key->data[0] == '.') {\n skip = 1;\n goto wildcard;\n }\n if (key->len > 2) {\n if (key->data[0] == '*' && key->data[1] == '.') {\n skip = 2;\n goto wildcard;\n }\n if (key->data[i - 2] == '.' && key->data[i - 1] == '*') {\n skip = 0;\n last -= 2;\n goto wildcard;\n }\n }\n if (n) {\n return NGX_DECLINED;\n }\n }\n k = 0;\n for (i = 0; i < last; i++) {\n if (!(flags & NGX_HASH_READONLY_KEY)) {\n key->data[i] = ngx_tolower(key->data[i]);\n }\n k = ngx_hash(k, key->data[i]);\n }\n k %= ha->hsize;\n name = ha->keys_hash[k].elts;\n if (name) {\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (last != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data, name[i].data, last) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n *name = *key;\n hk = ngx_array_push(&ha->keys);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key = *key;\n hk->key_hash = ngx_hash_key(key->data, last);\n hk->value = value;\n return NGX_OK;\nwildcard:\n k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);\n k %= ha->hsize;\n if (skip == 1) {\n name = ha->keys_hash[k].elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - 1;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, &key->data[1], name->len);\n }\n if (skip) {\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n len = 0;\n n = 0;\n for (i = last - 1; i; i--) {\n if (key->data[i] == '.') {\n ngx_memcpy(&p[n], &key->data[i + 1], len);\n n += len;\n p[n++] = '.';\n len = 0;\n continue;\n }\n len++;\n }\n if (len) {\n ngx_memcpy(&p[n], &key->data[1], len);\n n += len;\n }\n p[n] = '\\0';\n hwc = &ha->dns_wc_head;\n keys = &ha->dns_wc_head_hash[k];\n } else {\n last++;\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n ngx_cpystrn(p, key->data, last);\n hwc = &ha->dns_wc_tail;\n keys = &ha->dns_wc_tail_hash[k];\n }\n hk = ngx_array_push(hwc);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key.len = last - 1;\n hk->key.data = p;\n hk->key_hash = 0;\n hk->value = value;\n name = keys->elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < keys->nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(keys);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - skip;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, key->data + skip, name->len);\n return NGX_OK;\n}"]
|
2,893
| 0
|
https://github.com/libav/libav/blob/62844c3fd66940c7747e9b2bb7804e265319f43f/libavfilter/vf_crop.c/#L165
|
static int config_input(AVFilterLink *link)
{
AVFilterContext *ctx = link->dst;
CropContext *crop = ctx->priv;
const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(link->format);
int ret;
const char *expr;
double res;
crop->var_values[VAR_E] = M_E;
crop->var_values[VAR_PHI] = M_PHI;
crop->var_values[VAR_PI] = M_PI;
crop->var_values[VAR_IN_W] = crop->var_values[VAR_IW] = ctx->inputs[0]->w;
crop->var_values[VAR_IN_H] = crop->var_values[VAR_IH] = ctx->inputs[0]->h;
crop->var_values[VAR_X] = NAN;
crop->var_values[VAR_Y] = NAN;
crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;
crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;
crop->var_values[VAR_N] = 0;
crop->var_values[VAR_T] = NAN;
av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);
crop->hsub = pix_desc->log2_chroma_w;
crop->vsub = pix_desc->log2_chroma_h;
if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
var_names, crop->var_values,
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),
var_names, crop->var_values,
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;
if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
var_names, crop->var_values,
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
if (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||
normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {
av_log(ctx, AV_LOG_ERROR,
"Too big value or invalid expression for out_w/ow or out_h/oh. "
"Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
crop->ow_expr, crop->oh_expr);
return AVERROR(EINVAL);
}
crop->w &= ~((1 << crop->hsub) - 1);
crop->h &= ~((1 << crop->vsub) - 1);
if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
(ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
NULL, NULL, NULL, NULL, 0, ctx)) < 0)
return AVERROR(EINVAL);
av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
link->w, link->h, crop->w, crop->h);
if (crop->w <= 0 || crop->h <= 0 ||
crop->w > link->w || crop->h > link->h) {
av_log(ctx, AV_LOG_ERROR,
"Invalid too big or non positive size for width '%d' or height '%d'\n",
crop->w, crop->h);
return AVERROR(EINVAL);
}
crop->x = (link->w - crop->w) / 2;
crop->y = (link->h - crop->h) / 2;
crop->x &= ~((1 << crop->hsub) - 1);
crop->y &= ~((1 << crop->vsub) - 1);
return 0;
fail_expr:
av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
return ret;
}
|
['static int config_input(AVFilterLink *link)\n{\n AVFilterContext *ctx = link->dst;\n CropContext *crop = ctx->priv;\n const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(link->format);\n int ret;\n const char *expr;\n double res;\n crop->var_values[VAR_E] = M_E;\n crop->var_values[VAR_PHI] = M_PHI;\n crop->var_values[VAR_PI] = M_PI;\n crop->var_values[VAR_IN_W] = crop->var_values[VAR_IW] = ctx->inputs[0]->w;\n crop->var_values[VAR_IN_H] = crop->var_values[VAR_IH] = ctx->inputs[0]->h;\n crop->var_values[VAR_X] = NAN;\n crop->var_values[VAR_Y] = NAN;\n crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;\n crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;\n crop->var_values[VAR_N] = 0;\n crop->var_values[VAR_T] = NAN;\n av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);\n crop->hsub = pix_desc->log2_chroma_w;\n crop->vsub = pix_desc->log2_chroma_h;\n if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),\n var_names, crop->var_values,\n NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;\n crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;\n if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),\n var_names, crop->var_values,\n NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;\n crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;\n if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),\n var_names, crop->var_values,\n NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;\n crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;\n if (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||\n normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {\n av_log(ctx, AV_LOG_ERROR,\n "Too big value or invalid expression for out_w/ow or out_h/oh. "\n "Maybe the expression for out_w:\'%s\' or for out_h:\'%s\' is self-referencing.\\n",\n crop->ow_expr, crop->oh_expr);\n return AVERROR(EINVAL);\n }\n crop->w &= ~((1 << crop->hsub) - 1);\n crop->h &= ~((1 << crop->vsub) - 1);\n if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,\n NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||\n (ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,\n NULL, NULL, NULL, NULL, 0, ctx)) < 0)\n return AVERROR(EINVAL);\n av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\\n",\n link->w, link->h, crop->w, crop->h);\n if (crop->w <= 0 || crop->h <= 0 ||\n crop->w > link->w || crop->h > link->h) {\n av_log(ctx, AV_LOG_ERROR,\n "Invalid too big or non positive size for width \'%d\' or height \'%d\'\\n",\n crop->w, crop->h);\n return AVERROR(EINVAL);\n }\n crop->x = (link->w - crop->w) / 2;\n crop->y = (link->h - crop->h) / 2;\n crop->x &= ~((1 << crop->hsub) - 1);\n crop->y &= ~((1 << crop->vsub) - 1);\n return 0;\nfail_expr:\n av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression \'%s\'\\n", expr);\n return ret;\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}']
|
2,894
| 0
|
https://github.com/openssl/openssl/blob/de3955f66225e42bfae710c50b51c98aa4616ac1/crypto/bio/b_addr.c/#L690
|
int BIO_lookup_ex(const char *host, const char *service, int lookup_type,
int family, int socktype, int protocol, BIO_ADDRINFO **res)
{
int ret = 0;
switch(family) {
case AF_INET:
#ifdef AF_INET6
case AF_INET6:
#endif
#ifdef AF_UNIX
case AF_UNIX:
#endif
#ifdef AF_UNSPEC
case AF_UNSPEC:
#endif
break;
default:
BIOerr(BIO_F_BIO_LOOKUP_EX, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
return 0;
}
#ifdef AF_UNIX
if (family == AF_UNIX) {
if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
return 1;
else
BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);
return 0;
}
#endif
if (BIO_sock_init() != 1)
return 0;
if (1) {
#ifdef AI_PASSIVE
int gai_ret = 0;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = socktype;
hints.ai_protocol = protocol;
#ifdef AI_ADDRCONFIG
#ifdef AF_UNSPEC
if (family == AF_UNSPEC)
#endif
hints.ai_flags |= AI_ADDRCONFIG;
#endif
if (lookup_type == BIO_LOOKUP_SERVER)
hints.ai_flags |= AI_PASSIVE;
switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
# ifdef EAI_SYSTEM
case EAI_SYSTEM:
SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_SYS_LIB);
break;
# endif
case 0:
ret = 1;
break;
default:
BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_SYS_LIB);
ERR_add_error_data(1, gai_strerror(gai_ret));
break;
}
} else {
#endif
const struct hostent *he;
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
# pragma pointer_size save
# pragma pointer_size 32
#endif
#ifdef OPENSSL_SYS_WINDOWS
static uint32_t he_fallback_address;
static const char *he_fallback_addresses[] =
{ (char *)&he_fallback_address, NULL };
#else
static in_addr_t he_fallback_address;
static const char *he_fallback_addresses[] =
{ (char *)&he_fallback_address, NULL };
#endif
static const struct hostent he_fallback =
{ NULL, NULL, AF_INET, sizeof(he_fallback_address),
(char **)&he_fallback_addresses };
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
# pragma pointer_size restore
#endif
struct servent *se;
#ifdef _WIN64
struct servent se_fallback = { NULL, NULL, NULL, 0 };
#else
struct servent se_fallback = { NULL, NULL, 0, NULL };
#endif
if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) {
BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);
ret = 0;
goto err;
}
CRYPTO_THREAD_write_lock(bio_lookup_lock);
he_fallback_address = INADDR_ANY;
if (host == NULL) {
he = &he_fallback;
switch(lookup_type) {
case BIO_LOOKUP_CLIENT:
he_fallback_address = INADDR_LOOPBACK;
break;
case BIO_LOOKUP_SERVER:
he_fallback_address = INADDR_ANY;
break;
default:
assert("We forgot to handle a lookup type!" == NULL);
BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_INTERNAL_ERROR);
ret = 0;
goto err;
}
} else {
he = gethostbyname(host);
if (he == NULL) {
#ifndef OPENSSL_SYS_WINDOWS
# if defined(OPENSSL_SYS_VXWORKS)
SYSerr(SYS_F_GETHOSTBYNAME, 1000 );
# else
SYSerr(SYS_F_GETHOSTBYNAME, 1000 + h_errno);
# endif
#else
SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
#endif
ret = 0;
goto err;
}
}
if (service == NULL) {
se_fallback.s_port = 0;
se_fallback.s_proto = NULL;
se = &se_fallback;
} else {
char *endp = NULL;
long portnum = strtol(service, &endp, 10);
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
# pragma pointer_size save
# pragma pointer_size 32
#endif
char *proto = NULL;
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
# pragma pointer_size restore
#endif
switch (socktype) {
case SOCK_STREAM:
proto = "tcp";
break;
case SOCK_DGRAM:
proto = "udp";
break;
}
if (endp != service && *endp == '\0'
&& portnum > 0 && portnum < 65536) {
se_fallback.s_port = htons((unsigned short)portnum);
se_fallback.s_proto = proto;
se = &se_fallback;
} else if (endp == service) {
se = getservbyname(service, proto);
if (se == NULL) {
#ifndef OPENSSL_SYS_WINDOWS
SYSerr(SYS_F_GETSERVBYNAME, errno);
#else
SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
#endif
goto err;
}
} else {
BIOerr(BIO_F_BIO_LOOKUP_EX, BIO_R_MALFORMED_HOST_OR_SERVICE);
goto err;
}
}
*res = NULL;
{
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
# pragma pointer_size save
# pragma pointer_size 32
#endif
char **addrlistp;
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
# pragma pointer_size restore
#endif
size_t addresses;
BIO_ADDRINFO *tmp_bai = NULL;
for(addrlistp = he->h_addr_list; *addrlistp != NULL;
addrlistp++)
;
for(addresses = addrlistp - he->h_addr_list;
addrlistp--, addresses-- > 0; ) {
if (!addrinfo_wrap(he->h_addrtype, socktype,
*addrlistp, he->h_length,
se->s_port, &tmp_bai))
goto addrinfo_malloc_err;
tmp_bai->bai_next = *res;
*res = tmp_bai;
continue;
addrinfo_malloc_err:
BIO_ADDRINFO_free(*res);
*res = NULL;
BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);
ret = 0;
goto err;
}
ret = 1;
}
err:
CRYPTO_THREAD_unlock(bio_lookup_lock);
}
return ret;
}
|
['int BIO_lookup_ex(const char *host, const char *service, int lookup_type,\n int family, int socktype, int protocol, BIO_ADDRINFO **res)\n{\n int ret = 0;\n switch(family) {\n case AF_INET:\n#ifdef AF_INET6\n case AF_INET6:\n#endif\n#ifdef AF_UNIX\n case AF_UNIX:\n#endif\n#ifdef AF_UNSPEC\n case AF_UNSPEC:\n#endif\n break;\n default:\n BIOerr(BIO_F_BIO_LOOKUP_EX, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);\n return 0;\n }\n#ifdef AF_UNIX\n if (family == AF_UNIX) {\n if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))\n return 1;\n else\n BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n#endif\n if (BIO_sock_init() != 1)\n return 0;\n if (1) {\n#ifdef AI_PASSIVE\n int gai_ret = 0;\n struct addrinfo hints;\n memset(&hints, 0, sizeof(hints));\n hints.ai_family = family;\n hints.ai_socktype = socktype;\n hints.ai_protocol = protocol;\n#ifdef AI_ADDRCONFIG\n#ifdef AF_UNSPEC\n if (family == AF_UNSPEC)\n#endif\n hints.ai_flags |= AI_ADDRCONFIG;\n#endif\n if (lookup_type == BIO_LOOKUP_SERVER)\n hints.ai_flags |= AI_PASSIVE;\n switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {\n# ifdef EAI_SYSTEM\n case EAI_SYSTEM:\n SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());\n BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_SYS_LIB);\n break;\n# endif\n case 0:\n ret = 1;\n break;\n default:\n BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_SYS_LIB);\n ERR_add_error_data(1, gai_strerror(gai_ret));\n break;\n }\n } else {\n#endif\n const struct hostent *he;\n#if defined(OPENSSL_SYS_VMS) && defined(__DECC)\n# pragma pointer_size save\n# pragma pointer_size 32\n#endif\n#ifdef OPENSSL_SYS_WINDOWS\n static uint32_t he_fallback_address;\n static const char *he_fallback_addresses[] =\n { (char *)&he_fallback_address, NULL };\n#else\n static in_addr_t he_fallback_address;\n static const char *he_fallback_addresses[] =\n { (char *)&he_fallback_address, NULL };\n#endif\n static const struct hostent he_fallback =\n { NULL, NULL, AF_INET, sizeof(he_fallback_address),\n (char **)&he_fallback_addresses };\n#if defined(OPENSSL_SYS_VMS) && defined(__DECC)\n# pragma pointer_size restore\n#endif\n struct servent *se;\n#ifdef _WIN64\n struct servent se_fallback = { NULL, NULL, NULL, 0 };\n#else\n struct servent se_fallback = { NULL, NULL, 0, NULL };\n#endif\n if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) {\n BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);\n ret = 0;\n goto err;\n }\n CRYPTO_THREAD_write_lock(bio_lookup_lock);\n he_fallback_address = INADDR_ANY;\n if (host == NULL) {\n he = &he_fallback;\n switch(lookup_type) {\n case BIO_LOOKUP_CLIENT:\n he_fallback_address = INADDR_LOOPBACK;\n break;\n case BIO_LOOKUP_SERVER:\n he_fallback_address = INADDR_ANY;\n break;\n default:\n assert("We forgot to handle a lookup type!" == NULL);\n BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_INTERNAL_ERROR);\n ret = 0;\n goto err;\n }\n } else {\n he = gethostbyname(host);\n if (he == NULL) {\n#ifndef OPENSSL_SYS_WINDOWS\n# if defined(OPENSSL_SYS_VXWORKS)\n SYSerr(SYS_F_GETHOSTBYNAME, 1000 );\n# else\n SYSerr(SYS_F_GETHOSTBYNAME, 1000 + h_errno);\n# endif\n#else\n SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());\n#endif\n ret = 0;\n goto err;\n }\n }\n if (service == NULL) {\n se_fallback.s_port = 0;\n se_fallback.s_proto = NULL;\n se = &se_fallback;\n } else {\n char *endp = NULL;\n long portnum = strtol(service, &endp, 10);\n#if defined(OPENSSL_SYS_VMS) && defined(__DECC)\n# pragma pointer_size save\n# pragma pointer_size 32\n#endif\n char *proto = NULL;\n#if defined(OPENSSL_SYS_VMS) && defined(__DECC)\n# pragma pointer_size restore\n#endif\n switch (socktype) {\n case SOCK_STREAM:\n proto = "tcp";\n break;\n case SOCK_DGRAM:\n proto = "udp";\n break;\n }\n if (endp != service && *endp == \'\\0\'\n && portnum > 0 && portnum < 65536) {\n se_fallback.s_port = htons((unsigned short)portnum);\n se_fallback.s_proto = proto;\n se = &se_fallback;\n } else if (endp == service) {\n se = getservbyname(service, proto);\n if (se == NULL) {\n#ifndef OPENSSL_SYS_WINDOWS\n SYSerr(SYS_F_GETSERVBYNAME, errno);\n#else\n SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());\n#endif\n goto err;\n }\n } else {\n BIOerr(BIO_F_BIO_LOOKUP_EX, BIO_R_MALFORMED_HOST_OR_SERVICE);\n goto err;\n }\n }\n *res = NULL;\n {\n#if defined(OPENSSL_SYS_VMS) && defined(__DECC)\n# pragma pointer_size save\n# pragma pointer_size 32\n#endif\n char **addrlistp;\n#if defined(OPENSSL_SYS_VMS) && defined(__DECC)\n# pragma pointer_size restore\n#endif\n size_t addresses;\n BIO_ADDRINFO *tmp_bai = NULL;\n for(addrlistp = he->h_addr_list; *addrlistp != NULL;\n addrlistp++)\n ;\n for(addresses = addrlistp - he->h_addr_list;\n addrlistp--, addresses-- > 0; ) {\n if (!addrinfo_wrap(he->h_addrtype, socktype,\n *addrlistp, he->h_length,\n se->s_port, &tmp_bai))\n goto addrinfo_malloc_err;\n tmp_bai->bai_next = *res;\n *res = tmp_bai;\n continue;\n addrinfo_malloc_err:\n BIO_ADDRINFO_free(*res);\n *res = NULL;\n BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);\n ret = 0;\n goto err;\n }\n ret = 1;\n }\n err:\n CRYPTO_THREAD_unlock(bio_lookup_lock);\n }\n return ret;\n}']
|
2,895
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['static int test_gf2m_add()\n{\n BIGNUM *a, *b, *c;\n int i, st = 0;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n for (i = 0; i < NUM0; i++) {\n BN_rand(a, 512, 0, 0);\n BN_copy(b, BN_value_one());\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_GF2m_add(c, a, b);\n if ((BN_is_odd(a) && BN_is_odd(c))\n || (!BN_is_odd(a) && !BN_is_odd(c))) {\n printf("GF(2^m) addition test (a) failed!\\n");\n goto err;\n }\n BN_GF2m_add(c, c, c);\n if (!BN_is_zero(c)) {\n printf("GF(2^m) addition test (b) failed!\\n");\n goto err;\n }\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return st;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(0, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int ret = 0, bit, bytes, mask;\n time_t tim;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n if (pseudorand == 2) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,896
| 0
|
https://github.com/openssl/openssl/blob/ec04e866343d40a1e3e8e5db79557e279a2dd0d8/crypto/asn1/tasn_new.c/#L152
|
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
{
const ASN1_TEMPLATE *tt = NULL;
const ASN1_EXTERN_FUNCS *ef;
const ASN1_AUX *aux = it->funcs;
ASN1_aux_cb *asn1_cb;
ASN1_VALUE **pseqval;
int i;
if (aux && aux->asn1_cb)
asn1_cb = aux->asn1_cb;
else
asn1_cb = 0;
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");
#endif
switch (it->itype) {
case ASN1_ITYPE_EXTERN:
ef = it->funcs;
if (ef && ef->asn1_ex_new) {
if (!ef->asn1_ex_new(pval, it))
goto memerr;
}
break;
case ASN1_ITYPE_PRIMITIVE:
if (it->templates) {
if (!asn1_template_new(pval, it->templates))
goto memerr;
} else if (!asn1_primitive_new(pval, it, embed))
goto memerr;
break;
case ASN1_ITYPE_MSTRING:
if (!asn1_primitive_new(pval, it, embed))
goto memerr;
break;
case ASN1_ITYPE_CHOICE:
if (asn1_cb) {
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
if (!i)
goto auxerr;
if (i == 2) {
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
OPENSSL_mem_debug_pop();
#endif
return 1;
}
}
if (embed) {
memset(*pval, 0, it->size);
} else {
*pval = OPENSSL_zalloc(it->size);
if (*pval == NULL)
goto memerr;
}
asn1_set_choice_selector(pval, -1, it);
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
goto auxerr;
break;
case ASN1_ITYPE_NDEF_SEQUENCE:
case ASN1_ITYPE_SEQUENCE:
if (asn1_cb) {
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
if (!i)
goto auxerr;
if (i == 2) {
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
OPENSSL_mem_debug_pop();
#endif
return 1;
}
}
if (embed) {
memset(*pval, 0, it->size);
} else {
*pval = OPENSSL_zalloc(it->size);
if (*pval == NULL)
goto memerr;
}
asn1_do_lock(pval, 0, it);
asn1_enc_init(pval, it);
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
pseqval = asn1_get_field_ptr(pval, tt);
if (!asn1_template_new(pseqval, tt))
goto memerr;
}
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
goto auxerr;
break;
}
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
OPENSSL_mem_debug_pop();
#endif
return 1;
memerr:
ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
OPENSSL_mem_debug_pop();
#endif
return 0;
auxerr:
ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
ASN1_item_ex_free(pval, it);
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
OPENSSL_mem_debug_pop();
#endif
return 0;
}
|
['int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)\n{\n const ASN1_TEMPLATE *tt = NULL;\n const ASN1_EXTERN_FUNCS *ef;\n const ASN1_AUX *aux = it->funcs;\n ASN1_aux_cb *asn1_cb;\n ASN1_VALUE **pseqval;\n int i;\n if (aux && aux->asn1_cb)\n asn1_cb = aux->asn1_cb;\n else\n asn1_cb = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");\n#endif\n switch (it->itype) {\n case ASN1_ITYPE_EXTERN:\n ef = it->funcs;\n if (ef && ef->asn1_ex_new) {\n if (!ef->asn1_ex_new(pval, it))\n goto memerr;\n }\n break;\n case ASN1_ITYPE_PRIMITIVE:\n if (it->templates) {\n if (!asn1_template_new(pval, it->templates))\n goto memerr;\n } else if (!asn1_primitive_new(pval, it, embed))\n goto memerr;\n break;\n case ASN1_ITYPE_MSTRING:\n if (!asn1_primitive_new(pval, it, embed))\n goto memerr;\n break;\n case ASN1_ITYPE_CHOICE:\n if (asn1_cb) {\n i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);\n if (!i)\n goto auxerr;\n if (i == 2) {\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n OPENSSL_mem_debug_pop();\n#endif\n return 1;\n }\n }\n if (embed) {\n memset(*pval, 0, it->size);\n } else {\n *pval = OPENSSL_zalloc(it->size);\n if (*pval == NULL)\n goto memerr;\n }\n asn1_set_choice_selector(pval, -1, it);\n if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))\n goto auxerr;\n break;\n case ASN1_ITYPE_NDEF_SEQUENCE:\n case ASN1_ITYPE_SEQUENCE:\n if (asn1_cb) {\n i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);\n if (!i)\n goto auxerr;\n if (i == 2) {\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n OPENSSL_mem_debug_pop();\n#endif\n return 1;\n }\n }\n if (embed) {\n memset(*pval, 0, it->size);\n } else {\n *pval = OPENSSL_zalloc(it->size);\n if (*pval == NULL)\n goto memerr;\n }\n asn1_do_lock(pval, 0, it);\n asn1_enc_init(pval, it);\n for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {\n pseqval = asn1_get_field_ptr(pval, tt);\n if (!asn1_template_new(pseqval, tt))\n goto memerr;\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))\n goto auxerr;\n break;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n OPENSSL_mem_debug_pop();\n#endif\n return 1;\n memerr:\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n OPENSSL_mem_debug_pop();\n#endif\n return 0;\n auxerr:\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);\n ASN1_item_ex_free(pval, it);\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n OPENSSL_mem_debug_pop();\n#endif\n return 0;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'int asn1_set_choice_selector(ASN1_VALUE **pval, int value,\n const ASN1_ITEM *it)\n{\n int *sel, ret;\n sel = offset2ptr(*pval, it->utype);\n ret = *sel;\n *sel = value;\n return ret;\n}']
|
2,897
| 0
|
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/ssl/s3_cbc.c/#L706
|
void ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,
unsigned char *md_out,
size_t *md_out_size,
const unsigned char header[13],
const unsigned char *data,
size_t data_plus_mac_size,
size_t data_plus_mac_plus_padding_size,
const unsigned char *mac_secret,
unsigned mac_secret_length, char is_sslv3)
{
union {
double align;
unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
} md_state;
void (*md_final_raw) (void *ctx, unsigned char *md_out);
void (*md_transform) (void *ctx, const unsigned char *block);
unsigned md_size, md_block_size = 64;
unsigned sslv3_pad_length = 40, header_length, variance_blocks,
len, max_mac_bytes, num_blocks,
num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
unsigned int bits;
unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
unsigned char first_block[MAX_HASH_BLOCK_SIZE];
unsigned char mac_out[EVP_MAX_MD_SIZE];
unsigned i, j, md_out_size_u;
EVP_MD_CTX md_ctx;
unsigned md_length_size = 8;
char length_is_big_endian = 1;
int ret;
OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024);
switch (EVP_MD_CTX_type(ctx)) {
case NID_md5:
MD5_Init((MD5_CTX *)md_state.c);
md_final_raw = tls1_md5_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))MD5_Transform;
md_size = 16;
sslv3_pad_length = 48;
length_is_big_endian = 0;
break;
case NID_sha1:
SHA1_Init((SHA_CTX *)md_state.c);
md_final_raw = tls1_sha1_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
md_size = 20;
break;
#ifndef OPENSSL_NO_SHA256
case NID_sha224:
SHA224_Init((SHA256_CTX *)md_state.c);
md_final_raw = tls1_sha256_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
md_size = 224 / 8;
break;
case NID_sha256:
SHA256_Init((SHA256_CTX *)md_state.c);
md_final_raw = tls1_sha256_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
md_size = 32;
break;
#endif
#ifndef OPENSSL_NO_SHA512
case NID_sha384:
SHA384_Init((SHA512_CTX *)md_state.c);
md_final_raw = tls1_sha512_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
md_size = 384 / 8;
md_block_size = 128;
md_length_size = 16;
break;
case NID_sha512:
SHA512_Init((SHA512_CTX *)md_state.c);
md_final_raw = tls1_sha512_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
md_size = 64;
md_block_size = 128;
md_length_size = 16;
break;
#endif
default:
OPENSSL_assert(0);
if (md_out_size)
*md_out_size = -1;
return;
}
OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
header_length = 13;
if (is_sslv3) {
header_length = mac_secret_length + sslv3_pad_length + 8 +
1 +
2 ;
}
variance_blocks = is_sslv3 ? 2 : 6;
len = data_plus_mac_plus_padding_size + header_length;
max_mac_bytes = len - md_size - 1;
num_blocks =
(max_mac_bytes + 1 + md_length_size + md_block_size -
1) / md_block_size;
num_starting_blocks = 0;
k = 0;
mac_end_offset = data_plus_mac_size + header_length - md_size;
c = mac_end_offset % md_block_size;
index_a = mac_end_offset / md_block_size;
index_b = (mac_end_offset + md_length_size) / md_block_size;
if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
num_starting_blocks = num_blocks - variance_blocks;
k = md_block_size * num_starting_blocks;
}
bits = 8 * mac_end_offset;
if (!is_sslv3) {
bits += 8 * md_block_size;
memset(hmac_pad, 0, md_block_size);
OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));
memcpy(hmac_pad, mac_secret, mac_secret_length);
for (i = 0; i < md_block_size; i++)
hmac_pad[i] ^= 0x36;
md_transform(md_state.c, hmac_pad);
}
if (length_is_big_endian) {
memset(length_bytes, 0, md_length_size - 4);
length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
length_bytes[md_length_size - 1] = (unsigned char)bits;
} else {
memset(length_bytes, 0, md_length_size);
length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
length_bytes[md_length_size - 8] = (unsigned char)bits;
}
if (k > 0) {
if (is_sslv3) {
unsigned overhang = header_length - md_block_size;
md_transform(md_state.c, header);
memcpy(first_block, header + md_block_size, overhang);
memcpy(first_block + overhang, data, md_block_size - overhang);
md_transform(md_state.c, first_block);
for (i = 1; i < k / md_block_size - 1; i++)
md_transform(md_state.c, data + md_block_size * i - overhang);
} else {
memcpy(first_block, header, 13);
memcpy(first_block + 13, data, md_block_size - 13);
md_transform(md_state.c, first_block);
for (i = 1; i < k / md_block_size; i++)
md_transform(md_state.c, data + md_block_size * i - 13);
}
}
memset(mac_out, 0, sizeof(mac_out));
for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
i++) {
unsigned char block[MAX_HASH_BLOCK_SIZE];
unsigned char is_block_a = constant_time_eq_8(i, index_a);
unsigned char is_block_b = constant_time_eq_8(i, index_b);
for (j = 0; j < md_block_size; j++) {
unsigned char b = 0, is_past_c, is_past_cp1;
if (k < header_length)
b = header[k];
else if (k < data_plus_mac_plus_padding_size + header_length)
b = data[k - header_length];
k++;
is_past_c = is_block_a & constant_time_ge_8(j, c);
is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
b = constant_time_select_8(is_past_c, 0x80, b);
b = b & ~is_past_cp1;
b &= ~is_block_b | is_block_a;
if (j >= md_block_size - md_length_size) {
b = constant_time_select_8(is_block_b,
length_bytes[j -
(md_block_size -
md_length_size)], b);
}
block[j] = b;
}
md_transform(md_state.c, block);
md_final_raw(md_state.c, block);
for (j = 0; j < md_size; j++)
mac_out[j] |= block[j] & is_block_b;
}
EVP_MD_CTX_init(&md_ctx);
EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL );
if (is_sslv3) {
memset(hmac_pad, 0x5c, sslv3_pad_length);
EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length);
EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length);
EVP_DigestUpdate(&md_ctx, mac_out, md_size);
} else {
for (i = 0; i < md_block_size; i++)
hmac_pad[i] ^= 0x6a;
EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
EVP_DigestUpdate(&md_ctx, mac_out, md_size);
}
ret = EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
if (ret && md_out_size)
*md_out_size = md_out_size_u;
EVP_MD_CTX_cleanup(&md_ctx);
}
|
['void ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,\n unsigned char *md_out,\n size_t *md_out_size,\n const unsigned char header[13],\n const unsigned char *data,\n size_t data_plus_mac_size,\n size_t data_plus_mac_plus_padding_size,\n const unsigned char *mac_secret,\n unsigned mac_secret_length, char is_sslv3)\n{\n union {\n double align;\n unsigned char c[sizeof(LARGEST_DIGEST_CTX)];\n } md_state;\n void (*md_final_raw) (void *ctx, unsigned char *md_out);\n void (*md_transform) (void *ctx, const unsigned char *block);\n unsigned md_size, md_block_size = 64;\n unsigned sslv3_pad_length = 40, header_length, variance_blocks,\n len, max_mac_bytes, num_blocks,\n num_starting_blocks, k, mac_end_offset, c, index_a, index_b;\n unsigned int bits;\n unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];\n unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];\n unsigned char first_block[MAX_HASH_BLOCK_SIZE];\n unsigned char mac_out[EVP_MAX_MD_SIZE];\n unsigned i, j, md_out_size_u;\n EVP_MD_CTX md_ctx;\n unsigned md_length_size = 8;\n char length_is_big_endian = 1;\n int ret;\n OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024);\n switch (EVP_MD_CTX_type(ctx)) {\n case NID_md5:\n MD5_Init((MD5_CTX *)md_state.c);\n md_final_raw = tls1_md5_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))MD5_Transform;\n md_size = 16;\n sslv3_pad_length = 48;\n length_is_big_endian = 0;\n break;\n case NID_sha1:\n SHA1_Init((SHA_CTX *)md_state.c);\n md_final_raw = tls1_sha1_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;\n md_size = 20;\n break;\n#ifndef OPENSSL_NO_SHA256\n case NID_sha224:\n SHA224_Init((SHA256_CTX *)md_state.c);\n md_final_raw = tls1_sha256_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;\n md_size = 224 / 8;\n break;\n case NID_sha256:\n SHA256_Init((SHA256_CTX *)md_state.c);\n md_final_raw = tls1_sha256_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;\n md_size = 32;\n break;\n#endif\n#ifndef OPENSSL_NO_SHA512\n case NID_sha384:\n SHA384_Init((SHA512_CTX *)md_state.c);\n md_final_raw = tls1_sha512_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;\n md_size = 384 / 8;\n md_block_size = 128;\n md_length_size = 16;\n break;\n case NID_sha512:\n SHA512_Init((SHA512_CTX *)md_state.c);\n md_final_raw = tls1_sha512_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;\n md_size = 64;\n md_block_size = 128;\n md_length_size = 16;\n break;\n#endif\n default:\n OPENSSL_assert(0);\n if (md_out_size)\n *md_out_size = -1;\n return;\n }\n OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);\n OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);\n OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);\n header_length = 13;\n if (is_sslv3) {\n header_length = mac_secret_length + sslv3_pad_length + 8 +\n 1 +\n 2 ;\n }\n variance_blocks = is_sslv3 ? 2 : 6;\n len = data_plus_mac_plus_padding_size + header_length;\n max_mac_bytes = len - md_size - 1;\n num_blocks =\n (max_mac_bytes + 1 + md_length_size + md_block_size -\n 1) / md_block_size;\n num_starting_blocks = 0;\n k = 0;\n mac_end_offset = data_plus_mac_size + header_length - md_size;\n c = mac_end_offset % md_block_size;\n index_a = mac_end_offset / md_block_size;\n index_b = (mac_end_offset + md_length_size) / md_block_size;\n if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {\n num_starting_blocks = num_blocks - variance_blocks;\n k = md_block_size * num_starting_blocks;\n }\n bits = 8 * mac_end_offset;\n if (!is_sslv3) {\n bits += 8 * md_block_size;\n memset(hmac_pad, 0, md_block_size);\n OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));\n memcpy(hmac_pad, mac_secret, mac_secret_length);\n for (i = 0; i < md_block_size; i++)\n hmac_pad[i] ^= 0x36;\n md_transform(md_state.c, hmac_pad);\n }\n if (length_is_big_endian) {\n memset(length_bytes, 0, md_length_size - 4);\n length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);\n length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);\n length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);\n length_bytes[md_length_size - 1] = (unsigned char)bits;\n } else {\n memset(length_bytes, 0, md_length_size);\n length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);\n length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);\n length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);\n length_bytes[md_length_size - 8] = (unsigned char)bits;\n }\n if (k > 0) {\n if (is_sslv3) {\n unsigned overhang = header_length - md_block_size;\n md_transform(md_state.c, header);\n memcpy(first_block, header + md_block_size, overhang);\n memcpy(first_block + overhang, data, md_block_size - overhang);\n md_transform(md_state.c, first_block);\n for (i = 1; i < k / md_block_size - 1; i++)\n md_transform(md_state.c, data + md_block_size * i - overhang);\n } else {\n memcpy(first_block, header, 13);\n memcpy(first_block + 13, data, md_block_size - 13);\n md_transform(md_state.c, first_block);\n for (i = 1; i < k / md_block_size; i++)\n md_transform(md_state.c, data + md_block_size * i - 13);\n }\n }\n memset(mac_out, 0, sizeof(mac_out));\n for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;\n i++) {\n unsigned char block[MAX_HASH_BLOCK_SIZE];\n unsigned char is_block_a = constant_time_eq_8(i, index_a);\n unsigned char is_block_b = constant_time_eq_8(i, index_b);\n for (j = 0; j < md_block_size; j++) {\n unsigned char b = 0, is_past_c, is_past_cp1;\n if (k < header_length)\n b = header[k];\n else if (k < data_plus_mac_plus_padding_size + header_length)\n b = data[k - header_length];\n k++;\n is_past_c = is_block_a & constant_time_ge_8(j, c);\n is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);\n b = constant_time_select_8(is_past_c, 0x80, b);\n b = b & ~is_past_cp1;\n b &= ~is_block_b | is_block_a;\n if (j >= md_block_size - md_length_size) {\n b = constant_time_select_8(is_block_b,\n length_bytes[j -\n (md_block_size -\n md_length_size)], b);\n }\n block[j] = b;\n }\n md_transform(md_state.c, block);\n md_final_raw(md_state.c, block);\n for (j = 0; j < md_size; j++)\n mac_out[j] |= block[j] & is_block_b;\n }\n EVP_MD_CTX_init(&md_ctx);\n EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL );\n if (is_sslv3) {\n memset(hmac_pad, 0x5c, sslv3_pad_length);\n EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length);\n EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length);\n EVP_DigestUpdate(&md_ctx, mac_out, md_size);\n } else {\n for (i = 0; i < md_block_size; i++)\n hmac_pad[i] ^= 0x6a;\n EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);\n EVP_DigestUpdate(&md_ctx, mac_out, md_size);\n }\n ret = EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);\n if (ret && md_out_size)\n *md_out_size = md_out_size_u;\n EVP_MD_CTX_cleanup(&md_ctx);\n}']
|
2,898
| 0
|
https://github.com/libav/libav/blob/a0c5917f86a6ff0d91d7f9af71afca0d43c14825/libavcodec/mss2.c/#L486
|
static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
MSS2Context *ctx = avctx->priv_data;
MSS12Context *c = &ctx->c;
GetBitContext gb;
GetByteContext gB;
ArithCoder acoder;
int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);
av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=
ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
init_get_bits(&gb, buf, buf_size * 8);
if (keyframe = get_bits1(&gb))
skip_bits(&gb, 7);
has_wmv9 = get_bits1(&gb);
has_mv = keyframe ? 0 : get_bits1(&gb);
is_rle = get_bits1(&gb);
is_555 = is_rle && get_bits1(&gb);
if (c->slice_split > 0)
ctx->split_position = c->slice_split;
else if (c->slice_split < 0) {
if (get_bits1(&gb)) {
if (get_bits1(&gb)) {
if (get_bits1(&gb))
ctx->split_position = get_bits(&gb, 16);
else
ctx->split_position = get_bits(&gb, 12);
} else
ctx->split_position = get_bits(&gb, 8) << 4;
} else {
if (keyframe)
ctx->split_position = avctx->height / 2;
}
} else
ctx->split_position = avctx->height;
if (c->slice_split && (ctx->split_position < 1 - is_555 ||
ctx->split_position > avctx->height - 1))
return AVERROR_INVALIDDATA;
align_get_bits(&gb);
buf += get_bits_count(&gb) >> 3;
buf_size -= get_bits_count(&gb) >> 3;
if (buf_size < 1)
return AVERROR_INVALIDDATA;
if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
return AVERROR_INVALIDDATA;
avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)
avctx->release_buffer(avctx, &ctx->pic);
if (has_wmv9) {
bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
arith2_init(&acoder, &gB);
implicit_rect = !arith2_get_bit(&acoder);
while (arith2_get_bit(&acoder)) {
if (used_rects == MAX_WMV9_RECTANGLES)
return AVERROR_INVALIDDATA;
r = &wmv9rects[used_rects];
if (!used_rects)
r->x = arith2_get_number(&acoder, avctx->width);
else
r->x = arith2_get_number(&acoder, avctx->width -
wmv9rects[used_rects - 1].x) +
wmv9rects[used_rects - 1].x;
r->y = arith2_get_number(&acoder, avctx->height);
r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;
r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
used_rects++;
}
if (implicit_rect && used_rects) {
av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
return AVERROR_INVALIDDATA;
}
if (implicit_rect) {
wmv9rects[0].x = 0;
wmv9rects[0].y = 0;
wmv9rects[0].w = avctx->width;
wmv9rects[0].h = avctx->height;
used_rects = 1;
}
for (i = 0; i < used_rects; i++) {
if (!implicit_rect && arith2_get_bit(&acoder)) {
av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
return AVERROR_INVALIDDATA;
}
if (!i) {
wmv9_mask = arith2_get_bit(&acoder) - 1;
if (!wmv9_mask)
wmv9_mask = arith2_get_number(&acoder, 256);
}
wmv9rects[i].coded = arith2_get_number(&acoder, 2);
}
buf += arith2_get_consumed_bytes(&acoder);
buf_size -= arith2_get_consumed_bytes(&acoder);
if (buf_size < 1)
return AVERROR_INVALIDDATA;
}
c->mvX = c->mvY = 0;
if (keyframe && !is_555) {
if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
return AVERROR_INVALIDDATA;
buf += i;
buf_size -= i;
} else if (has_mv) {
buf += 4;
buf_size -= 4;
if (buf_size < 1)
return AVERROR_INVALIDDATA;
c->mvX = AV_RB16(buf - 4) - avctx->width;
c->mvY = AV_RB16(buf - 2) - avctx->height;
}
if (c->mvX < 0 || c->mvY < 0) {
FFSWAP(AVFrame, ctx->pic, ctx->last_pic);
FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
if (ctx->pic.data[0])
avctx->release_buffer(avctx, &ctx->pic);
ctx->pic.reference = 3;
ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
FF_BUFFER_HINTS_READABLE |
FF_BUFFER_HINTS_PRESERVE |
FF_BUFFER_HINTS_REUSABLE;
if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
if (ctx->last_pic.data[0]) {
av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);
c->last_rgb_pic = ctx->last_pic.data[0] +
ctx->last_pic.linesize[0] * (avctx->height - 1);
} else {
av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
return AVERROR_INVALIDDATA;
}
} else {
if (ctx->last_pic.data[0])
avctx->release_buffer(avctx, &ctx->last_pic);
ctx->pic.reference = 3;
ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
FF_BUFFER_HINTS_READABLE |
FF_BUFFER_HINTS_PRESERVE |
FF_BUFFER_HINTS_REUSABLE;
if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
return ret;
}
c->last_rgb_pic = NULL;
}
c->rgb_pic = ctx->pic.data[0] +
ctx->pic.linesize[0] * (avctx->height - 1);
c->rgb_stride = -ctx->pic.linesize[0];
ctx->pic.key_frame = keyframe;
ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
if (is_555) {
bytestream2_init(&gB, buf, buf_size);
if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
keyframe, avctx->width, avctx->height))
return AVERROR_INVALIDDATA;
buf_size -= bytestream2_tell(&gB);
} else {
if (keyframe) {
c->corrupted = 0;
ff_mss12_slicecontext_reset(&ctx->sc[0]);
if (c->slice_split)
ff_mss12_slicecontext_reset(&ctx->sc[1]);
}
if (is_rle) {
init_get_bits(&gb, buf, buf_size * 8);
if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
c->rgb_pic, c->rgb_stride, c->pal, keyframe,
ctx->split_position, 0,
avctx->width, avctx->height))
return ret;
align_get_bits(&gb);
if (c->slice_split)
if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
c->rgb_pic, c->rgb_stride, c->pal, keyframe,
ctx->split_position, 1,
avctx->width, avctx->height))
return ret;
align_get_bits(&gb);
buf += get_bits_count(&gb) >> 3;
buf_size -= get_bits_count(&gb) >> 3;
} else if (!implicit_rect || wmv9_mask != -1) {
if (c->corrupted)
return AVERROR_INVALIDDATA;
bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
arith2_init(&acoder, &gB);
c->keyframe = keyframe;
if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,
avctx->width,
ctx->split_position))
return AVERROR_INVALIDDATA;
buf += arith2_get_consumed_bytes(&acoder);
buf_size -= arith2_get_consumed_bytes(&acoder);
if (c->slice_split) {
if (buf_size < 1)
return AVERROR_INVALIDDATA;
bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
arith2_init(&acoder, &gB);
if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,
ctx->split_position,
avctx->width,
avctx->height - ctx->split_position))
return AVERROR_INVALIDDATA;
buf += arith2_get_consumed_bytes(&acoder);
buf_size -= arith2_get_consumed_bytes(&acoder);
}
} else
memset(c->pal_pic, 0, c->pal_stride * avctx->height);
}
if (has_wmv9) {
for (i = 0; i < used_rects; i++) {
int x = wmv9rects[i].x;
int y = wmv9rects[i].y;
int w = wmv9rects[i].w;
int h = wmv9rects[i].h;
if (wmv9rects[i].coded) {
int WMV9codedFrameSize;
if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
return AVERROR_INVALIDDATA;
if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
x, y, w, h, wmv9_mask))
return ret;
buf += WMV9codedFrameSize + 3;
buf_size -= WMV9codedFrameSize + 3;
} else {
uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
if (wmv9_mask != -1) {
ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
wmv9_mask,
c->pal_pic + y * c->pal_stride + x,
c->pal_stride,
w, h);
} else {
do {
memset(dst, 0x80, w * 3);
dst += c->rgb_stride;
} while (--h);
}
}
}
}
if (buf_size)
av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
*got_frame = 1;
*(AVFrame *)data = ctx->pic;
return avpkt->size;
}
|
['static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n MSS2Context *ctx = avctx->priv_data;\n MSS12Context *c = &ctx->c;\n GetBitContext gb;\n GetByteContext gB;\n ArithCoder acoder;\n int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;\n Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;\n int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);\n av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=\n ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);\n init_get_bits(&gb, buf, buf_size * 8);\n if (keyframe = get_bits1(&gb))\n skip_bits(&gb, 7);\n has_wmv9 = get_bits1(&gb);\n has_mv = keyframe ? 0 : get_bits1(&gb);\n is_rle = get_bits1(&gb);\n is_555 = is_rle && get_bits1(&gb);\n if (c->slice_split > 0)\n ctx->split_position = c->slice_split;\n else if (c->slice_split < 0) {\n if (get_bits1(&gb)) {\n if (get_bits1(&gb)) {\n if (get_bits1(&gb))\n ctx->split_position = get_bits(&gb, 16);\n else\n ctx->split_position = get_bits(&gb, 12);\n } else\n ctx->split_position = get_bits(&gb, 8) << 4;\n } else {\n if (keyframe)\n ctx->split_position = avctx->height / 2;\n }\n } else\n ctx->split_position = avctx->height;\n if (c->slice_split && (ctx->split_position < 1 - is_555 ||\n ctx->split_position > avctx->height - 1))\n return AVERROR_INVALIDDATA;\n align_get_bits(&gb);\n buf += get_bits_count(&gb) >> 3;\n buf_size -= get_bits_count(&gb) >> 3;\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))\n return AVERROR_INVALIDDATA;\n avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;\n if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)\n avctx->release_buffer(avctx, &ctx->pic);\n if (has_wmv9) {\n bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);\n arith2_init(&acoder, &gB);\n implicit_rect = !arith2_get_bit(&acoder);\n while (arith2_get_bit(&acoder)) {\n if (used_rects == MAX_WMV9_RECTANGLES)\n return AVERROR_INVALIDDATA;\n r = &wmv9rects[used_rects];\n if (!used_rects)\n r->x = arith2_get_number(&acoder, avctx->width);\n else\n r->x = arith2_get_number(&acoder, avctx->width -\n wmv9rects[used_rects - 1].x) +\n wmv9rects[used_rects - 1].x;\n r->y = arith2_get_number(&acoder, avctx->height);\n r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;\n r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;\n used_rects++;\n }\n if (implicit_rect && used_rects) {\n av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\\n");\n return AVERROR_INVALIDDATA;\n }\n if (implicit_rect) {\n wmv9rects[0].x = 0;\n wmv9rects[0].y = 0;\n wmv9rects[0].w = avctx->width;\n wmv9rects[0].h = avctx->height;\n used_rects = 1;\n }\n for (i = 0; i < used_rects; i++) {\n if (!implicit_rect && arith2_get_bit(&acoder)) {\n av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!i) {\n wmv9_mask = arith2_get_bit(&acoder) - 1;\n if (!wmv9_mask)\n wmv9_mask = arith2_get_number(&acoder, 256);\n }\n wmv9rects[i].coded = arith2_get_number(&acoder, 2);\n }\n buf += arith2_get_consumed_bytes(&acoder);\n buf_size -= arith2_get_consumed_bytes(&acoder);\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n }\n c->mvX = c->mvY = 0;\n if (keyframe && !is_555) {\n if ((i = decode_pal_v2(c, buf, buf_size)) < 0)\n return AVERROR_INVALIDDATA;\n buf += i;\n buf_size -= i;\n } else if (has_mv) {\n buf += 4;\n buf_size -= 4;\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n c->mvX = AV_RB16(buf - 4) - avctx->width;\n c->mvY = AV_RB16(buf - 2) - avctx->height;\n }\n if (c->mvX < 0 || c->mvY < 0) {\n FFSWAP(AVFrame, ctx->pic, ctx->last_pic);\n FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);\n if (ctx->pic.data[0])\n avctx->release_buffer(avctx, &ctx->pic);\n ctx->pic.reference = 3;\n ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |\n FF_BUFFER_HINTS_READABLE |\n FF_BUFFER_HINTS_PRESERVE |\n FF_BUFFER_HINTS_REUSABLE;\n if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n if (ctx->last_pic.data[0]) {\n av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);\n c->last_rgb_pic = ctx->last_pic.data[0] +\n ctx->last_pic.linesize[0] * (avctx->height - 1);\n } else {\n av_log(avctx, AV_LOG_ERROR, "Missing keyframe\\n");\n return AVERROR_INVALIDDATA;\n }\n } else {\n if (ctx->last_pic.data[0])\n avctx->release_buffer(avctx, &ctx->last_pic);\n ctx->pic.reference = 3;\n ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |\n FF_BUFFER_HINTS_READABLE |\n FF_BUFFER_HINTS_PRESERVE |\n FF_BUFFER_HINTS_REUSABLE;\n if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\\n");\n return ret;\n }\n c->last_rgb_pic = NULL;\n }\n c->rgb_pic = ctx->pic.data[0] +\n ctx->pic.linesize[0] * (avctx->height - 1);\n c->rgb_stride = -ctx->pic.linesize[0];\n ctx->pic.key_frame = keyframe;\n ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;\n if (is_555) {\n bytestream2_init(&gB, buf, buf_size);\n if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,\n keyframe, avctx->width, avctx->height))\n return AVERROR_INVALIDDATA;\n buf_size -= bytestream2_tell(&gB);\n } else {\n if (keyframe) {\n c->corrupted = 0;\n ff_mss12_slicecontext_reset(&ctx->sc[0]);\n if (c->slice_split)\n ff_mss12_slicecontext_reset(&ctx->sc[1]);\n }\n if (is_rle) {\n init_get_bits(&gb, buf, buf_size * 8);\n if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,\n c->rgb_pic, c->rgb_stride, c->pal, keyframe,\n ctx->split_position, 0,\n avctx->width, avctx->height))\n return ret;\n align_get_bits(&gb);\n if (c->slice_split)\n if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,\n c->rgb_pic, c->rgb_stride, c->pal, keyframe,\n ctx->split_position, 1,\n avctx->width, avctx->height))\n return ret;\n align_get_bits(&gb);\n buf += get_bits_count(&gb) >> 3;\n buf_size -= get_bits_count(&gb) >> 3;\n } else if (!implicit_rect || wmv9_mask != -1) {\n if (c->corrupted)\n return AVERROR_INVALIDDATA;\n bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);\n arith2_init(&acoder, &gB);\n c->keyframe = keyframe;\n if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,\n avctx->width,\n ctx->split_position))\n return AVERROR_INVALIDDATA;\n buf += arith2_get_consumed_bytes(&acoder);\n buf_size -= arith2_get_consumed_bytes(&acoder);\n if (c->slice_split) {\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);\n arith2_init(&acoder, &gB);\n if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,\n ctx->split_position,\n avctx->width,\n avctx->height - ctx->split_position))\n return AVERROR_INVALIDDATA;\n buf += arith2_get_consumed_bytes(&acoder);\n buf_size -= arith2_get_consumed_bytes(&acoder);\n }\n } else\n memset(c->pal_pic, 0, c->pal_stride * avctx->height);\n }\n if (has_wmv9) {\n for (i = 0; i < used_rects; i++) {\n int x = wmv9rects[i].x;\n int y = wmv9rects[i].y;\n int w = wmv9rects[i].w;\n int h = wmv9rects[i].h;\n if (wmv9rects[i].coded) {\n int WMV9codedFrameSize;\n if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))\n return AVERROR_INVALIDDATA;\n if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,\n x, y, w, h, wmv9_mask))\n return ret;\n buf += WMV9codedFrameSize + 3;\n buf_size -= WMV9codedFrameSize + 3;\n } else {\n uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;\n if (wmv9_mask != -1) {\n ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,\n wmv9_mask,\n c->pal_pic + y * c->pal_stride + x,\n c->pal_stride,\n w, h);\n } else {\n do {\n memset(dst, 0x80, w * 3);\n dst += c->rgb_stride;\n } while (--h);\n }\n }\n }\n }\n if (buf_size)\n av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\\n");\n *got_frame = 1;\n *(AVFrame *)data = ctx->pic;\n return avpkt->size;\n}', 'static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
|
2,899
| 0
|
https://gitlab.com/libtiff/libtiff/blob/6dac309a9701d15ac52d895d566ddae2ed49db9b/libtiff/tif_swab.c/#L113
|
void
TIFFSwabArrayOfLong(register uint32* lp, tmsize_t n)
{
register unsigned char *cp;
register unsigned char t;
assert(sizeof(uint32)==4);
while (n-- > 0) {
cp = (unsigned char *)lp;
t = cp[3]; cp[3] = cp[0]; cp[0] = t;
t = cp[2]; cp[2] = cp[1]; cp[1] = t;
lp++;
}
}
|
['TIFF*\nTIFFClientOpen(\n\tconst char* name, const char* mode,\n\tthandle_t clientdata,\n\tTIFFReadWriteProc readproc,\n\tTIFFReadWriteProc writeproc,\n\tTIFFSeekProc seekproc,\n\tTIFFCloseProc closeproc,\n\tTIFFSizeProc sizeproc,\n\tTIFFMapFileProc mapproc,\n\tTIFFUnmapFileProc unmapproc\n)\n{\n\tstatic const char module[] = "TIFFClientOpen";\n\tTIFF *tif;\n\tint m;\n\tconst char* cp;\n\tassert(sizeof(uint8)==1);\n\tassert(sizeof(int8)==1);\n\tassert(sizeof(uint16)==2);\n\tassert(sizeof(int16)==2);\n\tassert(sizeof(uint32)==4);\n\tassert(sizeof(int32)==4);\n\tassert(sizeof(uint64)==8);\n\tassert(sizeof(int64)==8);\n\tassert(sizeof(tmsize_t)==sizeof(void*));\n\t{\n\t\tunion{\n\t\t\tuint8 a8[2];\n\t\t\tuint16 a16;\n\t\t} n;\n\t\tn.a8[0]=1;\n\t\tn.a8[1]=0;\n\t\t#ifdef WORDS_BIGENDIAN\n\t\tassert(n.a16==256);\n\t\t#else\n\t\tassert(n.a16==1);\n\t\t#endif\n\t}\n\tm = _TIFFgetMode(mode, module);\n\tif (m == -1)\n\t\tgoto bad2;\n\ttif = (TIFF *)_TIFFmalloc((tmsize_t)(sizeof (TIFF) + strlen(name) + 1));\n\tif (tif == NULL) {\n\t\tTIFFErrorExt(clientdata, module, "%s: Out of memory (TIFF structure)", name);\n\t\tgoto bad2;\n\t}\n\t_TIFFmemset(tif, 0, sizeof (*tif));\n\ttif->tif_name = (char *)tif + sizeof (TIFF);\n\tstrcpy(tif->tif_name, name);\n\ttif->tif_mode = m &~ (O_CREAT|O_TRUNC);\n\ttif->tif_curdir = (uint16) -1;\n\ttif->tif_curoff = 0;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_clientdata = clientdata;\n\tif (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) {\n\t\tTIFFErrorExt(clientdata, module,\n\t\t "One of the client procedures is NULL pointer.");\n\t\tgoto bad2;\n\t}\n\ttif->tif_readproc = readproc;\n\ttif->tif_writeproc = writeproc;\n\ttif->tif_seekproc = seekproc;\n\ttif->tif_closeproc = closeproc;\n\ttif->tif_sizeproc = sizeproc;\n\tif (mapproc)\n\t\ttif->tif_mapproc = mapproc;\n\telse\n\t\ttif->tif_mapproc = _tiffDummyMapProc;\n\tif (unmapproc)\n\t\ttif->tif_unmapproc = unmapproc;\n\telse\n\t\ttif->tif_unmapproc = _tiffDummyUnmapProc;\n\t_TIFFSetDefaultCompressionState(tif);\n\ttif->tif_flags = FILLORDER_MSB2LSB;\n\tif (m == O_RDONLY )\n\t\ttif->tif_flags |= TIFF_MAPPED;\n\t#ifdef STRIPCHOP_DEFAULT\n\tif (m == O_RDONLY || m == O_RDWR)\n\t\ttif->tif_flags |= STRIPCHOP_DEFAULT;\n\t#endif\n\tfor (cp = mode; *cp; cp++)\n\t\tswitch (*cp) {\n\t\t\tcase \'b\':\n\t\t\t\t#ifndef WORDS_BIGENDIAN\n\t\t\t\tif (m&O_CREAT)\n\t\t\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t\t#endif\n\t\t\t\tbreak;\n\t\t\tcase \'l\':\n\t\t\t\t#ifdef WORDS_BIGENDIAN\n\t\t\t\tif ((m&O_CREAT))\n\t\t\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t\t#endif\n\t\t\t\tbreak;\n\t\t\tcase \'B\':\n\t\t\t\ttif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |\n\t\t\t\t FILLORDER_MSB2LSB;\n\t\t\t\tbreak;\n\t\t\tcase \'L\':\n\t\t\t\ttif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |\n\t\t\t\t FILLORDER_LSB2MSB;\n\t\t\t\tbreak;\n\t\t\tcase \'H\':\n\t\t\t\ttif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |\n\t\t\t\t HOST_FILLORDER;\n\t\t\t\tbreak;\n\t\t\tcase \'M\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags |= TIFF_MAPPED;\n\t\t\t\tbreak;\n\t\t\tcase \'m\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags &= ~TIFF_MAPPED;\n\t\t\t\tbreak;\n\t\t\tcase \'C\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags |= TIFF_STRIPCHOP;\n\t\t\t\tbreak;\n\t\t\tcase \'c\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags &= ~TIFF_STRIPCHOP;\n\t\t\t\tbreak;\n\t\t\tcase \'h\':\n\t\t\t\ttif->tif_flags |= TIFF_HEADERONLY;\n\t\t\t\tbreak;\n\t\t\tcase \'8\':\n\t\t\t\tif (m&O_CREAT)\n\t\t\t\t\ttif->tif_flags |= TIFF_BIGTIFF;\n\t\t\t\tbreak;\n\t\t}\n\tif ((m & O_TRUNC) ||\n\t !ReadOK(tif, &tif->tif_header, sizeof (TIFFHeaderClassic))) {\n\t\tif (tif->tif_mode == O_RDONLY) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Cannot read TIFF header");\n\t\t\tgoto bad;\n\t\t}\n\t\t#ifdef WORDS_BIGENDIAN\n\t\ttif->tif_header.common.tiff_magic = (tif->tif_flags & TIFF_SWAB)\n\t\t ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;\n\t\t#else\n\t\ttif->tif_header.common.tiff_magic = (tif->tif_flags & TIFF_SWAB)\n\t\t ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;\n\t\t#endif\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t{\n\t\t\ttif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;\n\t\t\ttif->tif_header.classic.tiff_diroff = 0;\n\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\tTIFFSwabShort(&tif->tif_header.common.tiff_version);\n\t\t\ttif->tif_header_size = sizeof(TIFFHeaderClassic);\n\t\t}\n\t\telse\n\t\t{\n\t\t\ttif->tif_header.common.tiff_version = TIFF_VERSION_BIG;\n\t\t\ttif->tif_header.big.tiff_offsetsize = 8;\n\t\t\ttif->tif_header.big.tiff_unused = 0;\n\t\t\ttif->tif_header.big.tiff_diroff = 0;\n\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t{\n\t\t\t\tTIFFSwabShort(&tif->tif_header.common.tiff_version);\n\t\t\t\tTIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);\n\t\t\t}\n\t\t\ttif->tif_header_size = sizeof (TIFFHeaderBig);\n\t\t}\n\t\tTIFFSeekFile( tif, 0, SEEK_SET );\n\t\tif (!WriteOK(tif, &tif->tif_header, (tmsize_t)(tif->tif_header_size))) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Error writing TIFF header");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) {\n\t\t\t#ifndef WORDS_BIGENDIAN\n\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t#endif\n\t\t} else {\n\t\t\t#ifdef WORDS_BIGENDIAN\n\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t#endif\n\t\t}\n\t\tif (!TIFFDefaultDirectory(tif))\n\t\t\tgoto bad;\n\t\ttif->tif_diroff = 0;\n\t\ttif->tif_dirlist = NULL;\n\t\ttif->tif_dirlistsize = 0;\n\t\ttif->tif_dirnumber = 0;\n\t\treturn (tif);\n\t}\n\tif (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&\n\t tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN\n\t #if MDI_SUPPORT\n\t &&\n\t #if HOST_BIGENDIAN\n\t tif->tif_header.common.tiff_magic != MDI_BIGENDIAN\n\t #else\n\t tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN\n\t #endif\n\t ) {\n\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t "Not a TIFF or MDI file, bad magic number %d (0x%x)",\n\t #else\n\t ) {\n\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t "Not a TIFF file, bad magic number %d (0x%x)",\n\t #endif\n\t\t tif->tif_header.common.tiff_magic,\n\t\t tif->tif_header.common.tiff_magic);\n\t\tgoto bad;\n\t}\n\tif (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) {\n\t\t#ifndef WORDS_BIGENDIAN\n\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t#endif\n\t} else {\n\t\t#ifdef WORDS_BIGENDIAN\n\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t#endif\n\t}\n\tif (tif->tif_flags & TIFF_SWAB)\n\t\tTIFFSwabShort(&tif->tif_header.common.tiff_version);\n\tif ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC)&&\n\t (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t "Not a TIFF file, bad version number %d (0x%x)",\n\t\t tif->tif_header.common.tiff_version,\n\t\t tif->tif_header.common.tiff_version);\n\t\tgoto bad;\n\t}\n\tif (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)\n\t{\n\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\tTIFFSwabLong(&tif->tif_header.classic.tiff_diroff);\n\t\ttif->tif_header_size = sizeof(TIFFHeaderClassic);\n\t}\n\telse\n\t{\n\t\tif (!ReadOK(tif, ((uint8*)(&tif->tif_header) + sizeof(TIFFHeaderClassic)), (sizeof(TIFFHeaderBig)-sizeof(TIFFHeaderClassic))))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Cannot read TIFF header");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t{\n\t\t\tTIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);\n\t\t\tTIFFSwabLong8(&tif->tif_header.big.tiff_diroff);\n\t\t}\n\t\tif (tif->tif_header.big.tiff_offsetsize != 8)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Not a TIFF file, bad BigTIFF offsetsize %d (0x%x)",\n\t\t\t tif->tif_header.big.tiff_offsetsize,\n\t\t\t tif->tif_header.big.tiff_offsetsize);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_header.big.tiff_unused != 0)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Not a TIFF file, bad BigTIFF unused %d (0x%x)",\n\t\t\t tif->tif_header.big.tiff_unused,\n\t\t\t tif->tif_header.big.tiff_unused);\n\t\t\tgoto bad;\n\t\t}\n\t\ttif->tif_header_size = sizeof(TIFFHeaderBig);\n\t\ttif->tif_flags |= TIFF_BIGTIFF;\n\t}\n\ttif->tif_flags |= TIFF_MYBUFFER;\n\ttif->tif_rawcp = tif->tif_rawdata = 0;\n\ttif->tif_rawdatasize = 0;\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = 0;\n\tswitch (mode[0]) {\n\t\tcase \'r\':\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t\ttif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;\n\t\t\telse\n\t\t\t\ttif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;\n\t\t\tif (tif->tif_flags & TIFF_MAPPED)\n\t\t\t{\n\t\t\t\ttoff_t n;\n\t\t\t\tif (TIFFMapFileContents(tif,(void**)(&tif->tif_base),&n))\n\t\t\t\t{\n\t\t\t\t\ttif->tif_size=(tmsize_t)n;\n\t\t\t\t\tassert((toff_t)tif->tif_size==n);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\ttif->tif_flags &= ~TIFF_MAPPED;\n\t\t\t}\n\t\t\tif (tif->tif_flags & TIFF_HEADERONLY)\n\t\t\t\treturn (tif);\n\t\t\tif (TIFFReadDirectory(tif)) {\n\t\t\t\ttif->tif_rawcc = (tmsize_t)-1;\n\t\t\t\ttif->tif_flags |= TIFF_BUFFERSETUP;\n\t\t\t\treturn (tif);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase \'a\':\n\t\t\tif (!TIFFDefaultDirectory(tif))\n\t\t\t\tgoto bad;\n\t\t\treturn (tif);\n\t}\nbad:\n\ttif->tif_mode = O_RDONLY;\n TIFFCleanup(tif);\nbad2:\n\treturn ((TIFF*)0);\n}', 'void\nTIFFCleanup(TIFF* tif)\n{\n\tif (tif->tif_mode != O_RDONLY)\n\t\tTIFFFlush(tif);\n\t(*tif->tif_cleanup)(tif);\n\tTIFFFreeDirectory(tif);\n\tif (tif->tif_dirlist)\n\t\t_TIFFfree(tif->tif_dirlist);\n\twhile( tif->tif_clientinfo )\n\t{\n\t\tTIFFClientInfoLink *psLink = tif->tif_clientinfo;\n\t\ttif->tif_clientinfo = psLink->next;\n\t\t_TIFFfree( psLink->name );\n\t\t_TIFFfree( psLink );\n\t}\n\tif (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))\n\t\t_TIFFfree(tif->tif_rawdata);\n\tif (isMapped(tif))\n\t\tTIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);\n\tif (tif->tif_fields && tif->tif_nfields > 0) {\n\t\tuint32 i;\n\t\tfor (i = 0; i < tif->tif_nfields; i++) {\n\t\t\tTIFFField *fld = tif->tif_fields[i];\n\t\t\tif (fld->field_bit == FIELD_CUSTOM &&\n\t\t\t strncmp("Tag ", fld->field_name, 4) == 0) {\n\t\t\t\t_TIFFfree(fld->field_name);\n\t\t\t\t_TIFFfree(fld);\n\t\t\t}\n\t\t}\n\t\t_TIFFfree(tif->tif_fields);\n\t}\n if (tif->tif_nfieldscompat > 0) {\n uint32 i;\n for (i = 0; i < tif->tif_nfieldscompat; i++) {\n if (tif->tif_fieldscompat[i].allocated_size)\n _TIFFfree(tif->tif_fieldscompat[i].fields);\n }\n _TIFFfree(tif->tif_fieldscompat);\n }\n\t_TIFFfree(tif);\n}', 'int\nTIFFFlush(TIFF* tif)\n{\n if( tif->tif_mode == O_RDONLY )\n return 1;\n if (!TIFFFlushData(tif))\n return (0);\n if( (tif->tif_flags & TIFF_DIRTYSTRIP)\n && !(tif->tif_flags & TIFF_DIRTYDIRECT)\n && tif->tif_mode == O_RDWR )\n {\n uint64 *offsets=NULL, *sizes=NULL;\n if( TIFFIsTiled(tif) )\n {\n if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets )\n && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes )\n && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, offsets )\n && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, sizes ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n }\n else\n {\n if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets )\n && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes )\n && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, offsets )\n && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, sizes ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n }\n }\n if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))\n && !TIFFRewriteDirectory(tif))\n return (0);\n return (1);\n}', 'int\nTIFFFlushData(TIFF* tif)\n{\n\tif ((tif->tif_flags & TIFF_BEENWRITING) == 0)\n\t\treturn (1);\n\tif (tif->tif_flags & TIFF_POSTENCODE) {\n\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\treturn (0);\n\t}\n\treturn (TIFFFlushData1(tif));\n}', 'int\nTIFFRewriteDirectory( TIFF *tif )\n{\n\tstatic const char module[] = "TIFFRewriteDirectory";\n\tif( tif->tif_diroff == 0 )\n\t\treturn TIFFWriteDirectory( tif );\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tif (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)\n\t\t{\n\t\t\ttif->tif_header.classic.tiff_diroff = 0;\n\t\t\ttif->tif_diroff = 0;\n\t\t\tTIFFSeekFile(tif,4,SEEK_SET);\n\t\t\tif (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t\t "Error updating TIFF header");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tuint32 nextdir;\n\t\t\tnextdir = tif->tif_header.classic.tiff_diroff;\n\t\t\twhile(1) {\n\t\t\t\tuint16 dircount;\n\t\t\t\tuint32 nextnextdir;\n\t\t\t\tif (!SeekOK(tif, nextdir) ||\n\t\t\t\t !ReadOK(tif, &dircount, 2)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory count");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabShort(&dircount);\n\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t nextdir+2+dircount*12, SEEK_SET);\n\t\t\t\tif (!ReadOK(tif, &nextnextdir, 4)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory link");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong(&nextnextdir);\n\t\t\t\tif (nextnextdir==tif->tif_diroff)\n\t\t\t\t{\n\t\t\t\t\tuint32 m;\n\t\t\t\t\tm=0;\n\t\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t\t nextdir+2+dircount*12, SEEK_SET);\n\t\t\t\t\tif (!WriteOK(tif, &m, 4)) {\n\t\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t\t "Error writing directory link");\n\t\t\t\t\t\treturn (0);\n\t\t\t\t\t}\n\t\t\t\t\ttif->tif_diroff=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnextdir=nextnextdir;\n\t\t\t}\n\t\t}\n\t}\n\telse\n\t{\n\t\tif (tif->tif_header.big.tiff_diroff == tif->tif_diroff)\n\t\t{\n\t\t\ttif->tif_header.big.tiff_diroff = 0;\n\t\t\ttif->tif_diroff = 0;\n\t\t\tTIFFSeekFile(tif,8,SEEK_SET);\n\t\t\tif (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t\t "Error updating TIFF header");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tuint64 nextdir;\n\t\t\tnextdir = tif->tif_header.big.tiff_diroff;\n\t\t\twhile(1) {\n\t\t\t\tuint64 dircount64;\n\t\t\t\tuint16 dircount;\n\t\t\t\tuint64 nextnextdir;\n\t\t\t\tif (!SeekOK(tif, nextdir) ||\n\t\t\t\t !ReadOK(tif, &dircount64, 8)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory count");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong8(&dircount64);\n\t\t\t\tif (dircount64>0xFFFF)\n\t\t\t\t{\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Sanity check on tag count failed, likely corrupt TIFF");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tdircount=(uint16)dircount64;\n\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t nextdir+8+dircount*20, SEEK_SET);\n\t\t\t\tif (!ReadOK(tif, &nextnextdir, 8)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory link");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong8(&nextnextdir);\n\t\t\t\tif (nextnextdir==tif->tif_diroff)\n\t\t\t\t{\n\t\t\t\t\tuint64 m;\n\t\t\t\t\tm=0;\n\t\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t\t nextdir+8+dircount*20, SEEK_SET);\n\t\t\t\t\tif (!WriteOK(tif, &m, 8)) {\n\t\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t\t "Error writing directory link");\n\t\t\t\t\t\treturn (0);\n\t\t\t\t\t}\n\t\t\t\t\ttif->tif_diroff=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnextdir=nextnextdir;\n\t\t\t}\n\t\t}\n\t}\n\treturn TIFFWriteDirectory( tif );\n}', 'int\nTIFFWriteDirectory(TIFF* tif)\n{\n\treturn TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);\n}', 'static int\nTIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)\n{\n\tstatic const char module[] = "TIFFWriteDirectorySec";\n\tuint32 ndir;\n\tTIFFDirEntry* dir;\n\tuint32 dirsize;\n\tvoid* dirmem;\n\tuint32 m;\n\tif (tif->tif_mode == O_RDONLY)\n\t\treturn (1);\n _TIFFFillStriles( tif );\n\tif (imagedone)\n\t{\n\t\tif (tif->tif_flags & TIFF_POSTENCODE)\n\t\t{\n\t\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t\t\t "Error post-encoding before directory write");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t(*tif->tif_close)(tif);\n\t\tif (tif->tif_rawcc > 0\n\t\t && (tif->tif_flags & TIFF_BEENWRITING) != 0 )\n\t\t{\n\t\t if( !TIFFFlushData1(tif) )\n {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Error flushing data before directory write");\n\t\t\treturn (0);\n }\n\t\t}\n\t\tif ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)\n\t\t{\n\t\t\t_TIFFfree(tif->tif_rawdata);\n\t\t\ttif->tif_rawdata = NULL;\n\t\t\ttif->tif_rawcc = 0;\n\t\t\ttif->tif_rawdatasize = 0;\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = 0;\n\t\t}\n\t\ttif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);\n\t}\n\tdir=NULL;\n\tdirmem=NULL;\n\tdirsize=0;\n\twhile (1)\n\t{\n\t\tndir=0;\n\t\tif (isimage)\n\t\t{\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_POSITION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBFILETYPE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COMPRESSION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_THRESHHOLDING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_FILLORDER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ORIENTATION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PLANARCONFIG))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PAGENUMBER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n if (tif->tif_dir.td_stripoffset != NULL &&\n !TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\n goto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COLORMAP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))\n\t\t\t{\n\t\t\t\tif (tif->tif_dir.td_extrasamples)\n\t\t\t\t{\n\t\t\t\t\tuint16 na;\n\t\t\t\t\tuint16* nb;\n\t\t\t\t\tTIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_INKNAMES))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\t{\n\t\t\t\tuint32 n;\n\t\t\t\tfor (n=0; n<tif->tif_nfields; n++) {\n\t\t\t\t\tconst TIFFField* o;\n\t\t\t\t\to = tif->tif_fields[n];\n\t\t\t\t\tif ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (o->get_field_type)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase TIFF_SETGET_ASCII:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tchar* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_ASCII);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pb);\n\t\t\t\t\t\t\t\t\tpa=(uint32)(strlen(pb));\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT16:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint16 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_SHORT);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,(uint16)o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT32:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_LONG);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,(uint16)o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_C32_UINT8:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tvoid* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_UNDEFINED);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE2);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==1);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pa,&pb);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\t\tassert(0);\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)\n\t\t{\n uint16 tag = (uint16)tif->tif_dir.td_customValues[m].info->field_tag;\n uint32 count = tif->tif_dir.td_customValues[m].count;\n\t\t\tswitch (tif->tif_dir.td_customValues[m].info->field_type)\n\t\t\t{\n\t\t\t\tcase TIFF_ASCII:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_UNDEFINED:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_BYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SBYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SSHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_RATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SRATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_FLOAT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_DOUBLE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tassert(0);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (dir!=NULL)\n\t\t\tbreak;\n\t\tdir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));\n\t\tif (dir==NULL)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (isimage)\n\t\t{\n\t\t\tif ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse\n\t\t\ttif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));\n\t\tif (pdiroff!=NULL)\n\t\t\t*pdiroff=tif->tif_diroff;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tdirsize=2+ndir*12+4;\n\t\telse\n\t\t\tdirsize=8+ndir*20+8;\n\t\ttif->tif_dataoff=tif->tif_diroff+dirsize;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\ttif->tif_dataoff=(uint32)tif->tif_dataoff;\n\t\tif ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_dataoff&1)\n\t\t\ttif->tif_dataoff++;\n\t\tif (isimage)\n\t\t\ttif->tif_curdir++;\n\t}\n\tif (isimage)\n\t{\n\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))\n\t\t{\n\t\t\tuint32 na;\n\t\t\tTIFFDirEntry* nb;\n\t\t\tfor (na=0, nb=dir; ; na++, nb++)\n\t\t\t{\n\t\t\t\tif( na == ndir )\n {\n TIFFErrorExt(tif->tif_clientdata,module,\n "Cannot find SubIFD tag");\n goto bad;\n }\n\t\t\t\tif (nb->tdir_tag==TIFFTAG_SUBIFD)\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+2+na*12+8;\n\t\t\telse\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+8+na*20+12;\n\t\t}\n\t}\n\tdirmem=_TIFFmalloc(dirsize);\n\tif (dirmem==NULL)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\tgoto bad;\n\t}\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tuint8* n;\n\t\tuint32 nTmp;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint16*)n=(uint16)ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabShort((uint16*)n);\n\t\tn+=2;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\tnTmp = (uint32)o->tdir_count;\n\t\t\t_TIFFmemcpy(n,&nTmp,4);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong((uint32*)n);\n\t\t\tn+=4;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,4);\n\t\t\tn+=4;\n\t\t\to++;\n\t\t}\n\t\tnTmp = (uint32)tif->tif_nextdiroff;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong(&nTmp);\n\t\t_TIFFmemcpy(n,&nTmp,4);\n\t}\n\telse\n\t{\n\t\tuint8* n;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint64*)n=ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong8((uint64*)n);\n\t\tn+=8;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t_TIFFmemcpy(n,&o->tdir_count,8);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong8((uint64*)n);\n\t\t\tn+=8;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,8);\n\t\t\tn+=8;\n\t\t\to++;\n\t\t}\n\t\t_TIFFmemcpy(n,&tif->tif_nextdiroff,8);\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong8((uint64*)n);\n\t}\n\t_TIFFfree(dir);\n\tdir=NULL;\n\tif (!SeekOK(tif,tif->tif_diroff))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\tif (!WriteOK(tif,dirmem,(tmsize_t)dirsize))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\t_TIFFfree(dirmem);\n\tif (imagedone)\n\t{\n\t\tTIFFFreeDirectory(tif);\n\t\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\t\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\t\t(*tif->tif_cleanup)(tif);\n\t\tTIFFCreateDirectory(tif);\n\t}\n\treturn(1);\nbad:\n\tif (dir!=NULL)\n\t\t_TIFFfree(dir);\n\tif (dirmem!=NULL)\n\t\t_TIFFfree(dirmem);\n\treturn(0);\n}', 'static int\nTIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)\n{\n static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";\n uint64* ma;\n uint32 mb;\n uint32* p;\n uint32* q;\n int o;\n if (dir==NULL)\n {\n (*ndir)++;\n return(1);\n }\n if( tif->tif_flags&TIFF_BIGTIFF )\n return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,\n tag,count,value);\n p = _TIFFmalloc(count*sizeof(uint32));\n if (p==NULL)\n {\n TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n return(0);\n }\n for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)\n {\n if (*ma>0xFFFFFFFF)\n {\n TIFFErrorExt(tif->tif_clientdata,module,\n "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");\n _TIFFfree(p);\n return(0);\n }\n *q= (uint32)(*ma);\n }\n o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);\n _TIFFfree(p);\n return(o);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}', 'static int\nTIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)\n{\n\tassert(count<0x40000000);\n\tassert(sizeof(uint32)==4);\n\tif (tif->tif_flags&TIFF_SWAB)\n\t\tTIFFSwabArrayOfLong(value,count);\n\treturn(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));\n}', 'void\nTIFFSwabArrayOfLong(register uint32* lp, tmsize_t n)\n{\n\tregister unsigned char *cp;\n\tregister unsigned char t;\n\tassert(sizeof(uint32)==4);\n\twhile (n-- > 0) {\n\t\tcp = (unsigned char *)lp;\n\t\tt = cp[3]; cp[3] = cp[0]; cp[0] = t;\n\t\tt = cp[2]; cp[2] = cp[1]; cp[1] = t;\n\t\tlp++;\n\t}\n}']
|
2,900
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)\n{\n if (r != a) {\n if (BN_copy(r, a) == NULL)\n return 0;\n }\n while (n > 0) {\n int max_shift;\n max_shift = BN_num_bits(m) - BN_num_bits(r);\n if (max_shift < 0) {\n BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);\n return 0;\n }\n if (max_shift > n)\n max_shift = n;\n if (max_shift) {\n if (!BN_lshift(r, r, max_shift))\n return 0;\n n -= max_shift;\n } else {\n if (!BN_lshift1(r, r))\n return 0;\n --n;\n }\n if (BN_cmp(r, m) >= 0) {\n if (!BN_sub(r, r, m))\n return 0;\n }\n }\n bn_check_top(r);\n return 1;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_lshift1(BIGNUM *r, const BIGNUM *a)\n{\n register BN_ULONG *ap, *rp, t, c;\n int i;\n bn_check_top(r);\n bn_check_top(a);\n if (r != a) {\n r->neg = a->neg;\n if (bn_wexpand(r, a->top + 1) == NULL)\n return (0);\n r->top = a->top;\n } else {\n if (bn_wexpand(r, a->top + 1) == NULL)\n return (0);\n }\n ap = a->d;\n rp = r->d;\n c = 0;\n for (i = 0; i < a->top; i++) {\n t = *(ap++);\n *(rp++) = ((t << 1) | c) & BN_MASK2;\n c = (t & BN_TBIT) ? 1 : 0;\n }\n if (c) {\n *rp = 1;\n r->top++;\n }\n bn_check_top(r);\n return (1);\n}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n int gt, lt;\n BN_ULONG t1, t2;\n if ((a == NULL) || (b == NULL)) {\n if (a != NULL)\n return (-1);\n else if (b != NULL)\n return (1);\n else\n return (0);\n }\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n if (a->neg)\n return (-1);\n else\n return (1);\n }\n if (a->neg == 0) {\n gt = 1;\n lt = -1;\n } else {\n gt = -1;\n lt = 1;\n }\n if (a->top > b->top)\n return (gt);\n if (a->top < b->top)\n return (lt);\n for (i = a->top - 1; i >= 0; i--) {\n t1 = a->d[i];\n t2 = b->d[i];\n if (t1 > t2)\n return (gt);\n if (t1 < t2)\n return (lt);\n }\n return (0);\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max;\n int add = 0, neg = 0;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg) {\n if (b->neg) {\n const BIGNUM *tmp;\n tmp = a;\n a = b;\n b = tmp;\n } else {\n add = 1;\n neg = 1;\n }\n } else {\n if (b->neg) {\n add = 1;\n neg = 0;\n }\n }\n if (add) {\n if (!BN_uadd(r, a, b))\n return 0;\n r->neg = neg;\n return 1;\n }\n max = (a->top > b->top) ? a->top : b->top;\n if (bn_wexpand(r, max) == NULL)\n return 0;\n if (BN_ucmp(a, b) < 0) {\n if (!BN_usub(r, b, a))\n return 0;\n r->neg = 1;\n } else {\n if (!BN_usub(r, a, b))\n return 0;\n r->neg = 0;\n }\n bn_check_top(r);\n return 1;\n}', 'int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n const BN_ULONG *ap, *bp;\n BN_ULONG *rp, carry, t1, t2;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n const BIGNUM *tmp;\n tmp = a;\n a = b;\n b = tmp;\n }\n max = a->top;\n min = b->top;\n dif = max - min;\n if (bn_wexpand(r, max + 1) == NULL)\n return 0;\n r->top = max;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n carry = bn_add_words(rp, ap, bp, min);\n rp += min;\n ap += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 + carry) & BN_MASK2;\n *(rp++) = t2;\n carry &= (t2 == 0);\n }\n *rp = carry;\n r->top += carry;\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.