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