DH * dh_new_group_asc(const char *gen, const char *modulus) { DH *dh = NULL; BIGNUM *p=NULL, *g=NULL; if ((dh = DH_new()) == NULL || (p = BN_new()) == NULL || (g = BN_new()) == NULL) goto null; if (BN_hex2bn(&p, modulus) == 0 || BN_hex2bn(&g, gen) == 0) { goto null; } if (DH_set0_pqg(dh, p, NULL, g) == 0) { goto null; } p = g = NULL; return (dh); null: BN_free(p); BN_free(g); DH_free(dh); return NULL; }
int _BN_rand(BIGNUM *rnd, int bits, int top,int bottom) { static int i32 = 0; static int i256 = 0; int rsts = 0; switch(bits) { case 32: rsts = BN_hex2bn(&rnd, hex_bn32_rand[i32++]); if (i32 >= sizeof(hex_bn32_rand)/sizeof(char *)) { i32 = 0; } break; case 256: rsts = BN_hex2bn(&rnd, hex_bn256_rand[i256++]); if (i256 >= sizeof(hex_bn256_rand)/sizeof(char *)) { i256 = 0; } break; default: break; } return rsts; }
static DH *tr_msg_decode_dh(json_t *jdh) { DH *dh = NULL; json_t *jp = NULL; json_t *jg = NULL; json_t *jpub_key = NULL; if (!(dh = malloc(sizeof(DH)))) { tr_crit("tr_msg_decode_dh(): Error allocating DH structure."); return NULL; } memset(dh, 0, sizeof(DH)); /* store required fields from dh object */ if ((NULL == (jp = json_object_get(jdh, "dh_p"))) || (NULL == (jg = json_object_get(jdh, "dh_g"))) || (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) { tr_debug("tr_msg_decode_dh(): Error parsing dh_info."); free(dh); return NULL; } BN_hex2bn(&(dh->p), json_string_value(jp)); BN_hex2bn(&(dh->g), json_string_value(jg)); BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key)); return dh; }
/* * Initialize everything necessary to check the signature of the given image. * Returns non-zero if successful, 0 otherwise. */ int init_checksum(char *keyfile) { char str[1024]; FILE *file; if (keyfile == NULL || (file = fopen(keyfile, "r")) == NULL) { fprintf(stderr, "%s: cannot open keyfile\n", keyfile); return 0; } if ((signature_key = RSA_new()) == NULL) { fprintf(stderr, "%s: cannot allocate RSA struct\n", keyfile); return 0; } fscanf(file, "%1024s", str); BN_hex2bn(&signature_key->n, str); fscanf(file, "%1024s", str); BN_hex2bn(&signature_key->e, str); fscanf(file, "%1024s", str); BN_hex2bn(&signature_key->dmp1, str); fscanf(file, "%1024s", str); BN_hex2bn(&signature_key->dmq1, str); fscanf(file, "%1024s", str); BN_hex2bn(&signature_key->iqmp, str); fclose(file); return 1; }
/** * @brief Deserialize the supplied buffer into an RSA public key. * * Allocate a new RSA keypair and fill in the modulus and the exponent from * the public key stored in buf. * * @param buf Buffer containing the key to deserialize. * @param len Length of the buffer. * * @return RSA *keypair on success, NULL on failure */ RSA *deserialize_rsa_pub_key(char *buf, size_t len) { (void)len; // TODO: use len to make sure we don't try to read past end of buffer /* get modulus */ uint32_t hexformodsize = *(uint32_t*)buf; char *hexformod = (char*)malloc(hexformodsize + 1); memcpy(hexformod, &buf[sizeof(uint32_t)], hexformodsize); hexformod[hexformodsize] = '\0'; DBGF("hexformod: %s", hexformod); /* get exponent */ uint32_t hexforexpsize = *(uint32_t*)&buf[sizeof(uint32_t) + hexformodsize]; char *hexforexp = (char*)malloc(hexforexpsize + 1); memcpy(hexforexp, &buf[2*sizeof(uint32_t) + hexformodsize], hexforexpsize); hexforexp[hexforexpsize] = '\0'; DBGF("hexforexp: %s", hexforexp); /* make the keypair */ RSA *keypair = RSA_new(); BN_hex2bn(&keypair->n, hexformod); // TODO: need BN_new() first? BN_hex2bn(&keypair->e, hexforexp); keypair->d = NULL; keypair->p = NULL; keypair->q = NULL; keypair->dmp1 = NULL; keypair->dmq1 = NULL; keypair->iqmp = NULL; free(hexformod); free(hexforexp); return keypair; }
static EVP_PKEY *create_pkey(neverbleed_t *nb, size_t key_index, const char *ebuf, const char *nbuf) { struct st_neverbleed_rsa_exdata_t *exdata; RSA *rsa; EVP_PKEY *pkey; if ((exdata = malloc(sizeof(*exdata))) == NULL) { fprintf(stderr, "no memory\n"); abort(); } exdata->nb = nb; exdata->key_index = key_index; rsa = RSA_new_method(nb->engine); RSA_set_ex_data(rsa, 0, exdata); if (BN_hex2bn(&rsa->e, ebuf) == 0) { fprintf(stderr, "failed to parse e:%s\n", ebuf); abort(); } if (BN_hex2bn(&rsa->n, nbuf) == 0) { fprintf(stderr, "failed to parse n:%s\n", nbuf); abort(); } rsa->flags |= RSA_FLAG_EXT_PKEY; pkey = EVP_PKEY_new(); EVP_PKEY_set1_RSA(pkey, rsa); RSA_free(rsa); return pkey; }
Boolean APSetKey(CFStringRef key) { hash = CFSTR(""); blacklist = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); // Create a new key rsaKey = RSA_new(); // Public exponent is always 3 BN_hex2bn(&rsaKey->e, "3"); CFMutableStringRef mutableKey = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, key); if (!mutableKey) return FALSE; CFIndex maximumCStringLength = CFStringGetMaximumSizeForEncoding(CFStringGetLength(mutableKey), kCFStringEncodingMacRoman) + 1; char *keyCStringBuffer = malloc(maximumCStringLength); // Determine if we have a hex or decimal key CFStringLowercase(mutableKey, NULL); if (CFStringHasPrefix(mutableKey, CFSTR("0x"))) { CFStringTrim(mutableKey, CFSTR("0x")); CFStringGetCString(mutableKey, keyCStringBuffer, maximumCStringLength, kCFStringEncodingMacRoman); BN_hex2bn(&rsaKey->n, keyCStringBuffer); } else { CFStringGetCString(mutableKey, keyCStringBuffer, maximumCStringLength, kCFStringEncodingMacRoman); BN_dec2bn(&rsaKey->n, keyCStringBuffer); } CFRelease(mutableKey); free(keyCStringBuffer); return TRUE; }
polypseud_ctx *polypseud_ctx_new() { polypseud_ctx *ctx = (polypseud_ctx*)malloc(sizeof(polypseud_ctx)); ctx->bn_ctx = BN_CTX_new(); ctx->p = BN_new(); ctx->a = BN_new(); ctx->b = BN_new(); BN_hex2bn(&ctx->p, "D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27"); BN_hex2bn(&ctx->a, "3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9F492F375A97D860EB4"); BN_hex2bn(&ctx->b, "520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539816F5EB4AC8FB1F1A6"); ctx->ec_group = EC_GROUP_new_curve_GFp(ctx->p, ctx->a, ctx->b, ctx->bn_ctx); if(ctx->ec_group == NULL) { BN_free(ctx->p); BN_free(ctx->a); BN_free(ctx->b); BN_CTX_free(ctx->bn_ctx); return NULL; } ctx->q = BN_new(); BN_hex2bn(&ctx->q, "D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311"); ctx->g = EC_POINT_new(ctx->ec_group); EC_POINT_hex2point(ctx->ec_group, "0443BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599C710AF8D0D39E2061114FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6AC7D35245D1692E8EE1", ctx->g, ctx->bn_ctx); EC_GROUP_set_generator(ctx->ec_group, ctx->g, ctx->q, BN_value_one()); return ctx; }
void test_Div_open_ssl() { myInt64 cycles; myInt64 start; int num_runs = NUM_RUNS; BN_CTX *ctx; ctx = BN_CTX_new(); BIGNUM *a = BN_new(); BIGNUM *b = BN_new(); BIGNUM *result = BN_new(); BIGNUM *rem = BN_new(); BN_hex2bn(&a, "7cd73b6fc007dfee34a23caf363ae67e8bb8782600000000032accceb"); BN_hex2bn(&b, "c69d8b898f0e43b4643a018e7b0569de6f8cf328e0bf6d59ace4e3bc2ca28d10"); start = start_tsc(); for(int i = 0; i < num_runs; i++) { BN_div(result, rem, b, a, ctx); } cycles = stop_tsc(start); double r; r = cycles / num_runs; printf("RDTSC instruction:\n %lf cycles measured => %lf seconds, assuming frequency is %lf MHz. (change in source file if different)\n", r, r/(FREQUENCY), (FREQUENCY)/1e6); BN_CTX_free(ctx); BN_free(a); BN_free(b); BN_free(result); BN_free(rem); }
static DH *dh_new_group_asc(const char *gen, const char *modulus) { DH *dh = NULL; if ((dh = DH_new()) == NULL) { printf("dh_new_group_asc: DH_new"); goto error; } // PとGは公開してもよい素数の組み合わせ if (BN_hex2bn(&dh->p, modulus) == 0) { printf("BN_hex2bn p"); goto error; } if (BN_hex2bn(&dh->g, gen) == 0) { printf("BN_hex2bn g"); goto error; } return (dh); error: DH_free(dh); return (NULL); }
int main(int argc,char **argv) { ops_create_info_t *info; const unsigned char *id; const char *nstr; const char *estr; BIGNUM *n=NULL; BIGNUM *e=NULL; if(argc != 4) { fprintf(stderr,"%s <n> <e> <user id>\n",argv[0]); exit(1); } nstr=argv[1]; estr=argv[2]; id=(unsigned char *)argv[3]; BN_hex2bn(&n,nstr); BN_hex2bn(&e,estr); info=ops_create_info_new(); ops_writer_set_fd(info,1); ops_write_rsa_public_key(time(NULL),n,e,info); ops_write_user_id(id,info); ops_create_info_delete(info); return 0; }
void Encode_RSA(unsigned char *in,int fd) { RSA *r; BIGNUM *bne,*bnn,*bnd; int bits = 1024, ret, len, flen, padding, i; unsigned char *key, *p; BIO *b; unsigned char *encData,*decData,*tmpData;//加密后的数据/解密后的数据/临时指针 /* Key data */ unsigned long e = 75011; /* 构建RSA数据结构 */ bne = BN_new(); bnd = BN_new(); bnn = BN_new(); ret = BN_set_word(bne, e); BN_hex2bn(&bnd, PRIVATE); BN_hex2bn(&bnn, MODULUS); r = RSA_new(); r->e=bne; r->d=bnd; r->n=bnn; /* Debug output key */ /*RSA_print_fp(stdout, r, 5);*/ /*准备输出的加密数据结构 */ flen = RSA_size(r);// - 11; encData = (unsigned char *)malloc(flen); bzero(encData, flen);//memset(encData, 0, flen); ret = RSA_private_encrypt(flen, in, encData, r, RSA_NO_PADDING); if(ret < 0){ JCG("Encrypt failed!\n"); return; } write(fd,encData,flen); tmpData=encData; #if 0 for (i=0; i<ret; i++) { JDG("0x%02x, ", *tmpData); if(i%16 == 7) JDG("\t"); else if(i%16 == 15) JDG("\n"); tmpData++; } JDG("\n"); JCG("end private encrypt "); #endif free(encData); RSA_free(r); }
bool read_rsa_private_key(void) { FILE *fp; char *fname, *key, *pubkey; struct stat s; if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) { if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) { logger(LOG_ERR, "PrivateKey used but no PublicKey found!"); return false; } myself->connection->rsa_key = RSA_new(); // RSA_blinding_on(myself->connection->rsa_key, NULL); BN_hex2bn(&myself->connection->rsa_key->d, key); BN_hex2bn(&myself->connection->rsa_key->n, pubkey); BN_hex2bn(&myself->connection->rsa_key->e, "FFFF"); free(key); free(pubkey); return true; } if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname)) xasprintf(&fname, "%s/rsa_key.priv", confbase); fp = fopen(fname, "r"); if(!fp) { logger(LOG_ERR, "Error reading RSA private key file `%s': %s", fname, strerror(errno)); free(fname); return false; } #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN) if(fstat(fileno(fp), &s)) { logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno)); free(fname); return false; } if(s.st_mode & ~0100700) logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname); #endif myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); fclose(fp); if(!myself->connection->rsa_key) { logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno)); free(fname); return false; } free(fname); return true; }
Person::Person(const char *p_str, const char *g_str) { p = NULL; g = NULL; B = NULL; hash = NULL; BN_hex2bn(&p, p_str); BN_hex2bn(&g, g_str); set_keys(); }
void get_own_data(sqlite3* db, char** nickname, BIGNUM* e, BIGNUM* n, BIGNUM* d) { sqlite3_stmt *stmt; char* statement = "SELECT * FROM keys WHERE id = 0"; sqlite3_prepare_v2(db, statement, strlen(statement) + 1 , &stmt, NULL); sqlite3_step(stmt); *nickname = (char*) sqlite3_column_text(stmt, 1); BN_hex2bn(&e, (const char*) sqlite3_column_text(stmt, 2)); BN_hex2bn(&n, (const char*) sqlite3_column_text(stmt, 3)); BN_hex2bn(&d, (const char*) sqlite3_column_text(stmt, 4)); }
static int test_BN_cmp(void) { BIGNUM *a, *b; a = BN_new(); b = BN_new(); if (!BN_set_word(a, 1)) return 1; if (!BN_set_word(b, 1)) return 1; if (BN_cmp(a, b) != 0) return 1; if (BN_cmp(b, a) != 0) return 1; if (!BN_set_word(b, 2)) return 1; if (BN_cmp(a, b) >= 0) return 1; if (BN_cmp(b, a) <= 0) return 1; BN_set_negative(b, 1); if (BN_cmp(a, b) <= 0) return 1; if (BN_cmp(b, a) >= 0) return 1; BN_free(a); BN_free(b); BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD1"); BN_hex2bn(&b, "50212A3B611D46642C825A16A354CE0FD4D85DD2"); if (BN_cmp(a, b) >= 0) return 1; if (BN_cmp(b, a) <= 0) return 1; BN_set_negative(b, 1); if (BN_cmp(a, b) <= 0) return 1; if (BN_cmp(b, a) >= 0) return 1; BN_free(a); BN_free(b); return 0; }
int main(int argc, char *argv[]) { BIGNUM *val; int ch; char *p, buf[LINE_MAX]; /* > max number of digits. */ ctx = BN_CTX_new(); val = BN_new(); if (val == NULL) errx(1, "can't initialise bignum"); while ((ch = getopt(argc, argv, "h")) != -1) switch (ch) { case 'h': hflag++; break; case '?': default: usage(); } argc -= optind; argv += optind; /* No args supplied, read numbers from stdin. */ if (argc == 0) for (;;) { if (fgets(buf, sizeof(buf), stdin) == NULL) { if (ferror(stdin)) err(1, "stdin"); exit (0); } for (p = buf; isblank(*p); ++p); if (*p == '\n' || *p == '\0') continue; if (*p == '-') errx(1, "negative numbers aren't permitted."); if (BN_dec2bn(&val, buf) == 0 && BN_hex2bn(&val, buf) == 0) errx(1, "%s: illegal numeric format.", buf); pr_fact(val); } /* Factor the arguments. */ else for (; *argv != NULL; ++argv) { if (argv[0][0] == '-') errx(1, "negative numbers aren't permitted."); if (BN_dec2bn(&val, argv[0]) == 0 && BN_hex2bn(&val, argv[0]) == 0) errx(1, "%s: illegal numeric format.", argv[0]); pr_fact(val); } exit(0); }
int next_pkey(RSA *pub, FILE *fp) { static char nbuf[2048]; static char ebuf[10]; if (fscanf(fp, "%s\t%s", nbuf, ebuf) != 2) return 0; BN_hex2bn(&pub->n, nbuf); BN_hex2bn(&pub->e, ebuf); return 1; }
void openssl_bioBN_math() { BIO *outs; BN_CTX *ctx; char num1[8], num2[8]; BIGNUM *bg1, *bg2, *tmp, *stp; bg1 = BN_new(); bg2 = BN_new(); tmp = BN_new(); ctx = BN_CTX_new(); strcpy(num1, "84"); strcpy(num2, "3"); BN_hex2bn(&bg1, num1); BN_hex2bn(&bg2, num2); outs = BIO_new(BIO_s_file()); BIO_set_fp(outs, stdout, BIO_NOCLOSE); printf("\nBIO_MATH as follow:\n"); BN_add(tmp, bg1, bg2); BIO_puts(outs, "\tbn(0x84 + 0x3) = 0x"); BN_print(outs, tmp); BIO_puts(outs, "\n"); BN_sub(tmp, bg1, bg2); BIO_puts(outs, "\tbn(0x84 - 0x3) = 0x"); BN_print(outs, tmp); BIO_puts(outs, "\n"); BN_mul(tmp, bg1, bg2, ctx); BIO_puts(outs, "\tbn(0x84 * 0x3) = 0x"); BN_print(outs, tmp); BIO_puts(outs, "\n"); BN_sqr(tmp, bg1, ctx); BIO_puts(outs, "\tbn(sqr(0x84)) = 0x"); BN_print(outs, tmp); BIO_puts(outs, "\n"); BN_div(tmp, stp, bg1, bg2, ctx); BIO_puts(outs, "\tbn(0x84 / 0x3) = 0x"); BN_print(outs, tmp); BIO_puts(outs, "\n"); BN_exp(tmp, bg1, bg2, ctx); BIO_puts(outs, "\tbn(0x84 e 0x03)= 0x"); BN_print(outs, tmp); BIO_puts(outs, "\n"); BN_free(bg1); BN_free(bg2); BN_free(tmp); BIO_free(outs); }
rsa_prvkey_t *rsa_prvkey_new_from_str(const char *str) { RSA *rsa = NULL; char n_buf[1024]; char e_buf[1024]; char d_buf[1024]; OPENSSL_assert(str); if (strlen(str) > sizeof(n_buf)) { fprintf(stderr, "invalid data\n"); return NULL; } if (sscanf(str, "%s %s %s", n_buf, e_buf, d_buf) != 3) { fprintf(stderr, "invalid data\n"); return NULL; } if (!(rsa = RSA_new())) { ERR_print_errors_fp(stderr); return NULL; } if (!BN_hex2bn(&rsa->n, n_buf)) { ERR_print_errors_fp(stderr); RSA_free(rsa); return NULL; } if (!BN_hex2bn(&rsa->e, e_buf)) { ERR_print_errors_fp(stderr); RSA_free(rsa); return NULL; } if (!BN_hex2bn(&rsa->d, d_buf)) { ERR_print_errors_fp(stderr); RSA_free(rsa); return NULL; } if (!RSA_check_key(rsa)) { ERR_print_errors_fp(stderr); RSA_free(rsa); return NULL; } return rsa; }
DH * dh_new_group_asc(const char *gen, const char *modulus) { DH *dh; if ((dh = DH_new()) == NULL) return NULL; if (BN_hex2bn(&dh->p, modulus) == 0 || BN_hex2bn(&dh->g, gen) == 0) { DH_free(dh); return NULL; } return (dh); }
int do_hash_padding( __in char* sz_key, __in byte* hex_in, __in int n_in_len, __out char** sz_hash ) { char sz_module[FX_RSA_MODULE_LEN+1] = {0}; char sz_exponent[FX_RSA_EXPNONT_LEN+1] = {0}; int ret = 0, flen = 0; BIGNUM *bnn, *bne; byte* out; RSA *r = NULL; /* * get the value of module/exponent from sz_key */ memcpy( sz_module, sz_key, FX_RSA_MODULE_LEN ); memcpy( sz_exponent, sz_key + FX_RSA_MODULE_LEN, FX_RSA_EXPNONT_LEN ); /* * do hash */ bnn = BN_new(); bne = BN_new(); BN_hex2bn(&bnn, sz_module); BN_hex2bn(&bne, sz_exponent); r = RSA_new(); r->n = bnn; r->e = bne; r->d = NULL; flen = RSA_size(r); out = (byte*)malloc( flen ); memset( out, 0, flen ); ret = RSA_public_encrypt( n_in_len, hex_in, out, r, RSA_PKCS1_PADDING ); if (ret < 0) { printf("Encrypt failed!\n"); log_string( "Encrypt failed!" ); return FX_ERROR_UNKOWN; } return byte_2_hex_str( out, sz_hash, ret ); }
DH * dh_new_group_asc(const char *gen, const char *modulus) { DH *dh; if ((dh = DH_new()) == NULL) fatal("dh_new_group_asc: DH_new"); if (BN_hex2bn(&dh->p, modulus) == 0) fatal("BN_hex2bn p"); if (BN_hex2bn(&dh->g, gen) == 0) fatal("BN_hex2bn g"); return (dh); }
bool CRsaEncDec::ReadPublickey(const std::string& keyfile, bool readfile) { if (m_publicKey != NULL) { RSA_free(m_publicKey); m_publicKey = NULL; } if (NULL == (m_publicKey = RSA_new())) return false; if (readfile) { FILE* hPubKeyFile = fopen(keyfile.c_str(), "rb"); if( hPubKeyFile == NULL ) { LOG.err_log("打开公钥文件失败:%s, [%s:%d]", keyfile.c_str(), __FILE__, __LINE__); return false; } RSA* res = PEM_read_RSA_PUBKEY(hPubKeyFile, &m_publicKey, 0, 0); fclose(hPubKeyFile); hPubKeyFile = NULL; if (!res) { LOG.err_log("读取公钥失败![%s:%d]", __FILE__, __LINE__); return false; } } else { BIGNUM *bnn = BN_new(); BIGNUM *bne = BN_new(); BIGNUM *bnd = BN_new(); BN_hex2bn(&bnn, MODULUS); BN_set_word(bne, PUBLIC_EXPONENT); BN_hex2bn(&bnd, PRIVATE_EXPONENT); m_publicKey->n = bnn; m_publicKey->e = bne; m_publicKey->d = bnd; return true; } return true; }
BIGNUM *BN_get(lua_State *L, int i) { BIGNUM *x = BN_new(); switch (lua_type(L, i)) { case LUA_TNUMBER: BN_set_word(x, lua_tointeger(L, i)); break; case LUA_TSTRING: { const char *s = lua_tostring(L, i); if (s[0] == 'X' || s[0] == 'x') BN_hex2bn(&x, s + 1); else BN_dec2bn(&x, s); break; } case LUA_TUSERDATA: BN_copy(x, CHECK_OBJECT(i, BIGNUM, "openssl.bn")); break; case LUA_TNIL: BN_free(x); x = NULL; break; } return x; }
int __openssl_initialize_dh(DH* pdh, int32_t bits_count){ int ret = ERROR_SUCCESS; //2. Create his internal p and g if ((pdh->p = BN_new()) == NULL) { ret = ERROR_OpenSslCreateP; return ret; } if ((pdh->g = BN_new()) == NULL) { ret = ERROR_OpenSslCreateG; return ret; } //3. initialize p and g if (BN_hex2bn(&pdh->p, RFC2409_PRIME_1024) == 0) { ret = ERROR_OpenSslParseP1024; return ret; } if (BN_set_word(pdh->g, 2) != 1) { ret = ERROR_OpenSslSetG; return ret; } //4. Set the key length pdh->length = bits_count; //5. Generate private and public key if (DH_generate_key(pdh) != 1) { ret = ERROR_OpenSslGenerateDHKeys; return ret; } return ret; }
static LUA_FUNCTION(openssl_hex) { size_t l = 0, pad = 0; const char* s = luaL_checklstring(L, 1, &l); int hl = 0; int encode = lua_isnoneornil(L, 2) ? 1 : lua_toboolean(L, 2); char* h = NULL; BIGNUM *B = NULL; if (encode) { h = OPENSSL_malloc(l * 2 + 1); to_hex(s, l, h); hl = strlen(h); } else { BN_hex2bn(&B, s); hl = l / 2; h = OPENSSL_malloc( hl + 1 ); pad = hl - BN_num_bytes(B); memset(h, 0, pad); BN_bn2bin(B, (unsigned char *)h+pad); h[hl] = '\0'; }; lua_pushlstring(L, (const char*)h, hl); OPENSSL_free(h); BN_free(B); return 1; }
void meteor_user_generate_S_string( struct SRPUser *usr, BN_CTX * ctx, BIGNUM * kgx, BIGNUM * aux, const char * B_str, char ** S_str) { BIGNUM *B = BN_new(); BIGNUM *bkgx = BN_new(); BIGNUM *S = BN_new(); BN_hex2bn( &B, B_str ); if ( !B ) goto cleanup_and_exit; BN_sub( bkgx, B, kgx ); if ( !bkgx ) goto cleanup_and_exit; BN_mod_exp(S, bkgx, aux, usr->ng->N, ctx); if ( !S ) goto cleanup_and_exit; *S_str = convert_to_lower( BN_bn2hex(S) ); cleanup_and_exit: BN_free(B); BN_free(bkgx); BN_free(S); }
static int gmp2bn(mpz_t g, BIGNUM *bn) { if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) && (BN_BITS2 == GMP_NUMB_BITS)) { /* The common case */ int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size; BN_zero(bn); if(bn_expand2 (bn, s) == NULL) return 0; bn->top = s; TINYCLR_SSL_MEMCPY(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0])); bn_correct_top(bn); bn->neg = g->_mp_size >= 0 ? 0 : 1; return 1; } else { int toret; char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10); if(!tmpchar) return 0; mpz_get_str(tmpchar, 16, g); toret = BN_hex2bn(&bn, tmpchar); OPENSSL_free(tmpchar); return toret; } }
static EC_KEY *mk_eckey(int nid, const char *str) { int ok = 0; EC_KEY *k = NULL; BIGNUM *priv = NULL; EC_POINT *pub = NULL; const EC_GROUP *grp; k = EC_KEY_new_by_curve_name(nid); if (!k) goto err; if(!BN_hex2bn(&priv, str)) goto err; if (!priv) goto err; if (!EC_KEY_set_private_key(k, priv)) goto err; grp = EC_KEY_get0_group(k); pub = EC_POINT_new(grp); if (!pub) goto err; if (!EC_POINT_mul(grp, pub, priv, NULL, NULL, NULL)) goto err; if (!EC_KEY_set_public_key(k, pub)) goto err; ok = 1; err: BN_clear_free(priv); EC_POINT_free(pub); if (ok) return k; EC_KEY_free(k); return NULL; }