Is there a function for setting a password to a post?

The password is a core field of a post, so it’s updated with wp_update_post(), just like the title or content:

wp_update_post(
  array(
    'ID'            => 123,
    'post_password' => 'abc123',
  )
);