I don't see a shortcut, so I'll answer based on brute-force case exhaustion. This won't be the fastest approach but a very robust one.
You're looking for the shortest connecting line from a point in cube A to a point in cube B. At both ends, this shortest line will surely not end inside the cube, but on a surface, edge or vertex of the cube (assuming the cubes don't overlap). So you get 9 cases:
- vertex / vertex
- vertex / edge
- vertex / surface
- edge / vertex
- edge / edge
- edge / surface
- surface / vertex
- surface / edge
- surface / surface
We can omit the surface / surface case, as it's only possible with surfaces parallel to each other, and in that situation some other case, using an edge or vertex, will give the same distance.
For each of the cases, you can find online a formula for computing the distance of the (infinitely extended) geometric objects, and then you should check for staying inside the finite surfaces or edges.
Finally, you can return the minimum of the valid distances.
Of course, there can be lots of optimizations, excluding cases that cannot produce shortest distances, e.g. surfaces pointing away from the other cube, but that needs careful analysis.