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