summaryrefslogtreecommitdiffstats
path: root/utils-python/seppl-gen-key
blob: 1690bb1091f381b954e592e2b5b7c8bbbace7e99 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python

# $Id$
#
# This file is part of seppl.
#
# seppl is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# seppl is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with seppl; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

from seppl_common import *
import getopt, sys

def genkey(a, name, f):

	n, b = find_alg_by_number(a)

	if b == 0:
		return -1

	fd = open("/dev/random", "r+")
	key = fd.read(b/8)
	fd.close()
	
	if (len(key) != b/8):
		sys.stderr("ERROR: Cannot generate randomness.\n")
		return

	if f == dump_key_xml:
		print "<seppl-keyring>"

	f(a, name, key)

	if f == dump_key_xml:
		print "</seppl-keyring>"

	return 0

algorithm = "aes"
bits = 128
name = "def"
func = dump_key_xml

def usage():
	global algorithm, bits, name, func
	x = { dump_key_xml : "disable", dump_key_bin : "enable" }
	
	print "%s:" % sys.argv[0]
	print "   -h --help                   Show this help"
	print "   -x --no-xml                 No xml output (%s)" % x[func]
	print "   -a --algorithm ALGORITHM    Specify algorithm (%s)" % algorithm
	print "   -b --bits BITS              Specify key length (%s)" % bits
	print "   -n --name NAME              Specify key name (%s)" % name


def main():
	global algorithm, bits, name, func
	
	try:	
		opts, args = getopt.getopt(sys.argv[1:], "hxa:b:n:", ["help", "no-xml", "algorithm=", "bits=", "name="])
	except getopt.GetoptError:
		usage()
		sys.exit(1)

	u = 0;

	for o, a in opts:
		if o in ("-h", "--help"):
			u = 1

		if o in ("-x", "--no-xml"):
			func = dump_key_bin

		if o in ("-a", "--algorithm"):
			algorithm = a

		if o in ("-b", "--bits"):
			bits = int(a)

		if o in ("-n", "--name"):
			name = a[:7]

	if u:
		usage()
		sys.exit(0)


	a = find_alg_by_name(algorithm, bits)

	if a == -1:
		sys.stderr.write("ERROR: Cipher not available\n")
		sys.exit(2)

	genkey(a, name, func)

if __name__ == "__main__":
	main()