]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/vmdh/vmdh-41-example.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / vmdh / vmdh-41-example.c
1 /*
2 * Copyright (c) 2006-2008,2010,2012,2014 Apple Inc. All Rights Reserved.
3 */
4
5 #include <Security/vmdh.h>
6 #include <Security/SecBase64.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 #include <corecrypto/ccdh.h>
11
12 #include "Security_regressions.h"
13
14 #if 0
15 #include <openssl/dh.h>
16 #endif
17
18 /* How to reach in the internals of SecDH/vmdh struct */
19 static inline ccdh_gp_t vmdh_gp(struct vmdh *dh)
20 {
21 ccdh_gp_t gp;
22 gp.gp = (ccdh_gp *)dh;
23 return gp;
24 }
25
26 static inline ccdh_full_ctx_t vmdh_priv(struct vmdh *dh)
27 {
28 void *p = dh;
29 cczp_t zp;
30 zp.zp = (struct cczp *) dh;
31 cc_size s = ccn_sizeof_n(cczp_n(zp));
32 ccdh_full_ctx_t priv = { .hdr = (struct ccdh_ctx_header *)(p+ccdh_gp_size(s)) };
33 return priv;
34 }
35
36 static uint32_t param_g = 5;
37
38 static const uint8_t param_p[] = {
39 0xD7, 0x48, 0x26, 0xCE, 0x1B, 0x1E, 0x26, 0xEE,
40 0x2B, 0x03, 0xAA, 0xAF, 0x3E, 0x1A, 0x82, 0x39,
41 0x43, 0x2E, 0xB4, 0x36, 0x71, 0xF7, 0x5A, 0x6B,
42 0x62, 0x8D, 0x40, 0xFC, 0x39, 0xB8, 0x85, 0xB5,
43 0xA2, 0x50, 0x56, 0xE3, 0xCB, 0xF1, 0xE4, 0xFE,
44 0xA8, 0x64, 0x63, 0xA0, 0x6F, 0x19, 0xF4, 0xB0,
45 0x12, 0x11, 0x82, 0x02, 0xBB, 0xBD, 0xFF, 0xA5,
46 0x8C, 0xED, 0xBF, 0xBE, 0xF0, 0xF6, 0x18, 0xDB,
47 };
48
49 static const uint8_t client_priv[] = {
50 0x48, 0x52, 0x51, 0x7B, 0x04, 0xF6, 0x8B, 0xF4,
51 0x25, 0x9D, 0x08, 0x9F, 0x22, 0xB2, 0xDC, 0x6D,
52 0x84, 0x36, 0x1B, 0xBB, 0xC9, 0xEE, 0x91, 0x2F,
53 0x24, 0xFD, 0xF5, 0xEF, 0xC4, 0x8B, 0xAB, 0x39,
54 0x77, 0xE5, 0x6B, 0x31, 0x82, 0x96, 0x3C, 0x6E,
55 0xDC, 0x0A, 0x40, 0x39, 0x8F, 0xF3, 0x4A, 0x63,
56 0x00, 0x39, 0x8C, 0x52, 0x01, 0xB9, 0x48, 0xF4,
57 0xB1, 0xD2, 0x3B, 0x24, 0xF3, 0x30, 0x0B, 0x61,
58 };
59
60 __unused static const uint8_t client_pub[] = {
61 0x73, 0x00, 0x67, 0x2E, 0x71, 0x38, 0x55, 0x6A,
62 0x9C, 0xA4, 0x1E, 0x7A, 0x53, 0x3A, 0x22, 0xD8,
63 0xF0, 0x37, 0xBD, 0x57, 0x9F, 0x4A, 0xD1, 0xD1,
64 0x16, 0x2F, 0x23, 0xB7, 0x3C, 0xBC, 0x15, 0x6A,
65 0x1D, 0x38, 0xE2, 0x2D, 0x4E, 0xCB, 0x8E, 0x9C,
66 0xFE, 0x86, 0x2A, 0xCA, 0xA5, 0xA6, 0xE6, 0x4E,
67 0x67, 0x2A, 0x0C, 0x37, 0x06, 0x95, 0x5B, 0xF5,
68 0xA8, 0x69, 0xBE, 0x72, 0x54, 0x0C, 0x63, 0xDD,
69 };
70
71 __unused static const uint8_t server_priv[] = {
72 0x7A, 0x20, 0x70, 0x42, 0xF2, 0x19, 0x0F, 0x54,
73 0xA4, 0xD6, 0x45, 0x85, 0xFF, 0xE3, 0x4F, 0x95,
74 0x77, 0x4E, 0x05, 0xA9, 0xCF, 0x60, 0xFA, 0x54,
75 0xEC, 0xBC, 0x8B, 0x67, 0xFD, 0x59, 0x12, 0xEF,
76 0xC0, 0xD3, 0x75, 0x66, 0x3F, 0xE4, 0x50, 0x97,
77 0x94, 0x12, 0xB4, 0xE3, 0xB2, 0x5E, 0x69, 0x90,
78 0x18, 0x56, 0xAF, 0x50, 0x2D, 0xB9, 0xC9, 0x11,
79 0x8B, 0xF7, 0x75, 0x70, 0xDF, 0x16, 0xCB, 0x8F,
80 };
81
82 static const uint8_t server_pub[] = {
83 0x36, 0x21, 0x01, 0x4B, 0x8C, 0xD4, 0xFD, 0xA3,
84 0xBA, 0x12, 0x7B, 0xD7, 0x48, 0x83, 0x74, 0x09,
85 0x74, 0x46, 0xAA, 0xE0, 0x22, 0x1B, 0x2F, 0x25,
86 0xE7, 0x25, 0x25, 0x4B, 0x7E, 0xAE, 0xEA, 0x30,
87 0xE0, 0xFD, 0xC8, 0x5A, 0xD5, 0xD2, 0x9C, 0x54,
88 0xCE, 0x0E, 0x8A, 0xB1, 0x16, 0xD8, 0xA3, 0xFA,
89 0xD9, 0x85, 0xA8, 0x42, 0x4E, 0xF5, 0x77, 0x9F,
90 0x2D, 0xAB, 0x4C, 0xE2, 0x1C, 0xE7, 0xCE, 0xD9,
91 };
92
93 static const uint8_t pw[] = {
94 0x31, 0x32, 0x33, 0x34
95 };
96
97 /* base64 would be:
98 rHWs3KX/1ubFTzfim+9mLw==
99 */
100 static const uint8_t pw_encr[] = {
101 0xac, 0x75, 0xac, 0xdc, 0xa5, 0xff, 0xd6, 0xe6,
102 0xc5, 0x4f, 0x37, 0xe2, 0x9b, 0xef, 0x66, 0x2f
103 };
104
105 #if 0
106 static void hexdump(const uint8_t *bytes, size_t len) {
107 size_t ix;
108 for (ix = 0; ix < len; ++ix) {
109 printf("%02X", bytes[ix]);
110 }
111 printf("\n");
112 }
113 #endif
114
115 /* Test basic add delete update copy matching stuff. */
116 static void tests(void)
117 {
118 vmdh_t vmdh;
119 ok((vmdh = vmdh_create(param_g, param_p, sizeof(param_p),
120 NULL, 0)), "vmdh_create");
121
122 /* No SecDH API to import a private key, so we have to reach inside the opaque struct */
123 ccdh_gp_t gp = vmdh_gp(vmdh);
124 ccdh_full_ctx_t priv = vmdh_priv(vmdh);
125 ok_status(ccdh_import_priv(gp, sizeof(client_priv), client_priv, priv), "Import private DH key");
126
127 uint8_t encpw[vmdh_encpw_len(sizeof(pw))];
128 size_t encpw_len = sizeof(encpw);
129
130 ok(vmdh_encrypt_password(vmdh, server_pub, sizeof(server_pub),
131 pw, sizeof(pw), encpw, &encpw_len), "vmdh_encrypt_password");
132 vmdh_destroy(vmdh);
133
134 is(encpw_len, sizeof(pw_encr), "check ciphertext size");
135 ok(!memcmp(encpw, pw_encr, sizeof(pw_encr)), "does ciphertext match?");
136
137 #if 0
138 hexdump(encpw, encpw_len);
139 hexdump(pw_encr, sizeof(pw_encr));
140
141 DH *dh = DH_new();
142 dh->p = BN_bin2bn(param_p, sizeof(param_p), BN_new());
143 BN_hex2bn(&dh->g, "05");
144 dh->pub_key = BN_bin2bn(client_pub, sizeof(client_pub), BN_new());
145 dh->priv_key = BN_bin2bn(client_priv, sizeof(client_priv), BN_new());
146 BIGNUM *bn_server_pub = BN_bin2bn(server_pub, sizeof(server_pub), BN_new());
147 unsigned char key[1024];
148 DH_compute_key(key, bn_server_pub, dh);
149 printf("shared secret:\n");
150 hexdump(key, 16);
151 #endif
152 }
153
154 int vmdh_41_example(int argc, char *const *argv)
155 {
156 plan_tests(5);
157
158 tests();
159
160 return 0;
161 }