aboutsummaryrefslogtreecommitdiffstats
path: root/src/_cffi_src/openssl/bignum.py
blob: 751018391d94eee8c0f68615e53604283f727577 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import absolute_import, division, print_function

INCLUDES = """
#include <openssl/bn.h>
"""

TYPES = """
typedef ... BN_CTX;
typedef ... BN_MONT_CTX;
typedef ... BIGNUM;
typedef int... BN_ULONG;
"""

FUNCTIONS = """
#define BN_FLG_CONSTTIME ...

void BN_set_flags(BIGNUM *, int);

BIGNUM *BN_new(void);
void BN_free(BIGNUM *);
void BN_clear_free(BIGNUM *);

int BN_rand_range(BIGNUM *, const BIGNUM *);

BN_CTX *BN_CTX_new(void);
void BN_CTX_free(BN_CTX *);

void BN_CTX_start(BN_CTX *);
BIGNUM *BN_CTX_get(BN_CTX *);
void BN_CTX_end(BN_CTX *);

BN_MONT_CTX *BN_MONT_CTX_new(void);
int BN_MONT_CTX_set(BN_MONT_CTX *, const BIGNUM *, BN_CTX *);
void BN_MONT_CTX_free(BN_MONT_CTX *);

BIGNUM *BN_dup(const BIGNUM *);

int BN_set_word(BIGNUM *, BN_ULONG);

const BIGNUM *BN_value_one(void);

char *BN_bn2hex(const BIGNUM *);
int BN_hex2bn(BIGNUM **, const char *);

int BN_bn2bin(const BIGNUM *, unsigned char *);
BIGNUM *BN_bin2bn(const unsigned char *, int, BIGNUM *);

int BN_num_bits(const BIGNUM *);

int BN_cmp(const BIGNUM *, const BIGNUM *);
int BN_is_negative(const BIGNUM *);
int BN_add(BIGNUM *, const BIGNUM *, const BIGNUM *);
int BN_sub(BIGNUM *, const BIGNUM *, const BIGNUM *);
int BN_nnmod(BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
int BN_mod_add(BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
               BN_CTX *);
int BN_mod_sub(BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
               BN_CTX *);
int BN_mod_mul(BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
               BN_CTX *);
int BN_mod_exp(BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
               BN_CTX *);
int BN_mod_exp_mont(BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
                    BN_CTX *, BN_MONT_CTX *);
int BN_mod_exp_mont_consttime(BIGNUM *, const BIGNUM *, const BIGNUM *,
                              const BIGNUM *, BN_CTX *, BN_MONT_CTX *);
BIGNUM *BN_mod_inverse(BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);

int BN_num_bytes(const BIGNUM *);

int BN_mod(BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);

/* The following 3 prime methods are exposed for Tribler. */
int BN_generate_prime_ex(BIGNUM *, int, int, const BIGNUM *,
                         const BIGNUM *, BN_GENCB *);
int BN_is_prime_ex(const BIGNUM *, int, BN_CTX *, BN_GENCB *);
const int BN_prime_checks_for_size(int);
"""

CUSTOMIZATIONS = """
"""
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apg.ui; import org.apg.Apg; import org.apg.AskForSecretKeyPassPhrase; import org.apg.Constants; import org.apg.Id; import org.apg.Constants.path; import org.apg.Id.dialog; import org.apg.Id.menu; import org.apg.Id.message; import org.apg.Id.type; import org.apg.Id.menu.option; import org.apg.R; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import android.app.Dialog; import android.content.Intent; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.ExpandableListContextMenuInfo; import android.widget.ExpandableListView.OnChildClickListener; import com.google.zxing.integration.android.IntentIntegrator; public class SecretKeyListActivity extends KeyListActivity implements OnChildClickListener { @Override public void onCreate(Bundle savedInstanceState) { mExportFilename = Constants.path.APP_DIR + "/secexport.asc"; mKeyType = Id.type.secret_key; super.onCreate(savedInstanceState); mList.setOnChildClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, Id.menu.option.import_keys, 0, R.string.menu_importKeys) .setIcon(android.R.drawable.ic_menu_add); menu.add(0, Id.menu.option.export_keys, 1, R.string.menu_exportKeys) .setIcon(android.R.drawable.ic_menu_save); menu.add(1, Id.menu.option.create, 2, R.string.menu_createKey) .setIcon(android.R.drawable.ic_menu_add); menu.add(3, Id.menu.option.search, 3, R.string.menu_search) .setIcon(android.R.drawable.ic_menu_search); menu.add(3, Id.menu.option.preferences, 4, R.string.menu_preferences) .setIcon(android.R.drawable.ic_menu_preferences); menu.add(3, Id.menu.option.about, 5, R.string.menu_about) .setIcon(android.R.drawable.ic_menu_info_details); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Id.menu.option.create: { createKey(); return true; } default: { return super.onOptionsItemSelected(item); } } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; int type = ExpandableListView.getPackedPositionType(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { // TODO: user id? menu.setHeaderTitle("Key"); menu.add(0, Id.menu.edit, 0, R.string.menu_editKey); menu.add(0, Id.menu.export, 1, R.string.menu_exportKey); menu.add(0, Id.menu.delete, 2, R.string.menu_deleteKey); menu.add(0, Id.menu.share, 2, R.string.menu_share); } } @Override public boolean onContextItemSelected(android.view.MenuItem menuItem) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo(); int type = ExpandableListView.getPackedPositionType(info.packedPosition); int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition); if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) { return super.onContextItemSelected(menuItem); } switch (menuItem.getItemId()) { case Id.menu.edit: { mSelectedItem = groupPosition; checkPassPhraseAndEdit(); return true; } case Id.menu.share: { mSelectedItem = groupPosition; long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem); String msg = keyId + "," + Apg.getFingerPrint(keyId);; new IntentIntegrator(this).shareText(msg); } default: { return super.onContextItemSelected(menuItem); } } } public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { mSelectedItem = groupPosition; checkPassPhraseAndEdit(); return true; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case Id.dialog.pass_phrase: { long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem); return AskForSecretKeyPassPhrase.createDialog(this, keyId, this); } default: { return super.onCreateDialog(id); } } } public void checkPassPhraseAndEdit() { long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem); String passPhrase = Apg.getCachedPassPhrase(keyId); if (passPhrase == null) { showDialog(Id.dialog.pass_phrase); } else { Apg.setEditPassPhrase(passPhrase); editKey(); } } @Override public void passPhraseCallback(long keyId, String passPhrase) { super.passPhraseCallback(keyId, passPhrase); Apg.setEditPassPhrase(passPhrase); editKey(); } private void createKey() { Apg.setEditPassPhrase(""); Intent intent = new Intent(this, EditKeyActivity.class); startActivityForResult(intent, Id.message.create_key); } private void editKey() { long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem); Intent intent = new Intent(this, EditKeyActivity.class); intent.putExtra(Apg.EXTRA_KEY_ID, keyId); startActivityForResult(intent, Id.message.edit_key); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case Id.message.create_key: // intentionally no break case Id.message.edit_key: { if (resultCode == RESULT_OK) { refreshList(); } break; } default: { break; } } super.onActivityResult(requestCode, resultCode, data); } }