aboutsummaryrefslogblamecommitdiff
path: root/utils/cores.c
blob: 1126e85e8ccf92876e88092690d1e98567a7bb93 (plain) (tree)
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
generated by cgit v1.2.3 (git 2.25.1) at 2024-09-21 00:32:31 +0000
an class="p">, subsequent_indent = " " * 2) def long_to_bytes(number, order): # # This is just plain nasty. # s = "%x" % number s = ("0" * (order/8 - len(s))) + s return s.decode("hex") def bytes_to_bits(bytes): # # This, on the other hand, is not just plain nasty, this is fancy nasty. # This is nasty with raisins in it. # s = bin(long(bytes.encode("hex"), 16))[2:] if len(s) % 8: s = ("0" * (8 - len(s) % 8)) + s return tuple(int(i) for i in s) ### def encode_sig(r, s, order): return long_to_bytes(r, order) + long_to_bytes(s, order) p256_sig = encode_sig(p256_r, p256_s, 256) p384_sig = encode_sig(p384_r, p384_s, 384) ### class ECPrivateKey(Sequence): componentType = NamedTypes( NamedType("version", Integer(namedValues = NamedValues(("ecPrivkeyVer1", 1)) ).subtype(subtypeSpec = Integer.subtypeSpec + SingleValueConstraint(1))), NamedType("privateKey", OctetString()), OptionalNamedType("parameters", ObjectIdentifier().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 0))), OptionalNamedType("publicKey", BitString().subtype(explicitTag = Tag(tagClassContext, tagFormatSimple, 1)))) def encode_key(d, Qx, Qy, order, oid): private_key = long_to_bytes(d, order) public_key = bytes_to_bits(chr(0x04) + long_to_bytes(Qx, order) + long_to_bytes(Qy, order)) parameters = oid key = ECPrivateKey() key["version"] = 1 key["privateKey"] = private_key key["parameters"] = parameters key["publicKey"] = public_key return DER_Encode(key) p256_key = encode_key(p256_d, p256_Qx, p256_Qy, 256, "1.2.840.10045.3.1.7") p384_key = encode_key(p384_d, p384_Qx, p384_Qy, 384, "1.3.132.0.34") ### print "/*" print " * ECDSA test data." print " * File automatically generated by", basename(argv[0]) print " */" curves = ("p256", "p384") vars = set() for name in dir(): head, sep, tail = name.partition("_") if head in curves: vars.add(tail) vars = sorted(vars) for curve in curves: order = int(curve[1:]) for var in vars: name = curve + "_" + var value = globals().get(name, None) if isinstance(value, (int, long)): value = long_to_bytes(value, order) if value is not None: print print "static const uint8_t %s[] = { /* %d bytes */" % (name, len(value)) print wrapper.fill(", ".join("0x%02x" % ord(v) for v in value)) print "};" print print "typedef struct {" print " hal_curve_name_t curve;" for var in vars: print " const uint8_t *%8s; size_t %8s_len;" % (var, var) print "} ecdsa_tc_t;" print print "static const ecdsa_tc_t ecdsa_tc[] = {" for curve in curves: print " { HAL_CURVE_%s," % curve.upper() for var in vars: name = curve + "_" + var if name in globals(): print " %-14s sizeof(%s)," % (name + ",", name) else: print " %-14s 0," % "NULL," print " }," print "};"